pi-mono/packages/chord/test/next-node/react/stores.test.ts
Mario Zechner 5bc4145165 feat(chord): Chord v2 web and terminal surfaces (slice 11)
- Web: base bundles built from the app's entry at start (content-hashed, served
  forever), plugin scripts that register themselves and one combined file,
  WebScripts/TuiScripts showing the current set, startPage with the connection
  loop, in-place plugin reloads and a reload on a new base bundle.
- React bindings: HostProvider, useHold, useWatch, useRegistry, useRefState.
- Terminal: startTerminal, a launcher that signs in with a token, a token file or
  device pairing and always runs the app's terminal code in a sandboxed child
  process (pipes, empty environment, --permission granting nothing, no SQLite,
  a builtin allowlist and a frozen process inside); output filter, confirmed
  copy/open/attach actions; startTerminalRuntime and TuiScope inside.
- Themes: the Themes registry, ThemeChoice per principal, CSS variables on the
  page, terminal colours by role; CSS modules in plugin builds.
- App kit: startServer gains web and tui; startApp renamed startServer,
  exportAddress/importAddress renamed exportData/importData and export no longer
  stops the worker; ephemeral sessions end with their client's last link.
- Examples 56-web, 57-tui, 58-themes, 59-chat.
2026-11-02 21:31:59 +00:00

114 lines
3.7 KiB
TypeScript

// Tests of the external stores the React bindings read (slice 11 §3), without React: a store's snapshot is the
// watchable's value (`undefined` when it has none, never a throw), and subscribing calls back on every change until
// unsubscribed.
import { describe, expect, it } from "vitest";
import { BACKGROUND_CONTEXT as context } from "../../../src/context/index.ts";
import {
constant,
createHost,
defineFacet,
hostKind,
local,
memoryLinkPair,
method,
remote,
str,
} from "../../../src/next/index.ts";
import { watchStore } from "../../../src/next-react/stores.ts";
const Notes = local("test.notes", { version: 1, params: {}, members: { text: str(), write: method(str()) } });
describe("a watch store", () => {
it("reads the value and calls back on changes until unsubscribed", async () => {
const host = createHost({ onError: () => {} });
await host.load(
[
{
name: "@test/notes",
facet: defineFacet((h) =>
h.serve(Notes, (_params, self) => {
const text = self.writable("a");
return {
text: self.derive([text], ([value]) => value),
write: async (next) => {
text.set(next);
return null;
},
};
}),
),
},
],
context,
);
const notes = await host.get(Notes, {}, context);
const store = watchStore(notes.text);
expect(store.snapshot()).toBe("a");
let calls = 0;
const unsubscribe = store.subscribe(() => calls++);
await new Promise((resolve) => setTimeout(resolve, 10));
await notes.write("b", context);
for (let index = 0; index < 100 && store.snapshot() !== "b"; index++) await new Promise((r) => setTimeout(r, 5));
expect(store.snapshot()).toBe("b");
const seen = calls;
expect(seen).toBeGreaterThan(0);
unsubscribe();
await notes.write("c", context);
await new Promise((resolve) => setTimeout(resolve, 20));
expect(calls).toBe(seen);
notes.release();
await host.close(context);
});
it("calls back once a held remote reference binds after the snapshot was read", async () => {
const Server = hostKind("server");
const Greeting = remote("test.greeting", {
version: 1,
params: {},
hostAddress: () => Server(),
variants: { main: { state: { text: str() } } },
});
const server = createHost({ address: Server(), principal: { kind: "server" }, onError: () => {} });
await server.load(
[
{
name: "@test/greeting",
facet: defineFacet((h) =>
h.serve(Greeting, () => ({ variant: "main", members: { text: constant("hi") } })),
),
},
],
context,
);
const client = createHost({ onError: () => {} });
await client.load([], context);
const held = client.hold(Greeting, {}, context);
const store = watchStore(held.text);
expect(store.snapshot()).toBeUndefined();
let calls = 0;
const unsubscribe = store.subscribe(() => calls++);
const [mine, theirs] = memoryLinkPair();
server.accept(theirs, { principal: { kind: "user" } }, context);
client.connect(mine, { principal: { kind: "server" } }, context);
for (let index = 0; index < 200 && calls === 0; index++) await new Promise((resolve) => setTimeout(resolve, 5));
expect(calls).toBeGreaterThan(0);
expect(store.snapshot()).toBe("hi");
unsubscribe();
held.release();
await client.close(context);
await server.close(context);
});
it("gives undefined for no watchable and for one without a value", () => {
expect(watchStore(undefined).snapshot()).toBeUndefined();
expect(watchStore(undefined).subscribe(() => {})).toBeTypeOf("function");
const failing = {
get value(): string {
throw new Error("not ready");
},
watch: () => Promise.reject(new Error("x")),
};
expect(watchStore(failing).snapshot()).toBeUndefined();
});
});