// node --test examples/web/hello/hello-cubit.test.mjs (after `cargo wasm`) // The cubit with the real hello and statusphere modules, runModule and the Node wasm runner; // only OAuth, the PLC directory and the PDS are stubbed. import assert from "node:assert/strict"; import { mkdtemp } from "node:fs/promises"; import { registerHooks } from "node:module"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { test } from "node:test"; // The page resolves atlibs/ through its import map; Node gets the same mapping here. registerHooks({ resolve(specifier, context, next) { const lib = specifier.startsWith("atlibs/") && new URL(`../../../javascript/src/${specifier.slice("atlibs/".length)}`, import.meta.url).href; return next(lib || specifier, context); }, }); const { HelloCubit } = await import("./hello-cubit.js"); const { localModule } = await import("../../../javascript/src/run-module/index.js"); const { createNodeContext, fileFetch } = await import("../../../javascript/src/server/index.js"); const did = "did:plc:lpfuqerea3deuoyrn7ojser4"; const modules = new URL("./modules/", import.meta.url); const local = (name) => localModule(new URL(`${name}.wasm`, modules), new URL(`${name}.json`, modules), fileFetch); // The public network: the PLC directory and one PDS holding statuses. const records = [{ uri: `at://${did}/xyz.statusphere.status/a`, value: { status: "🙂" } }]; async function network(url) { if (url === `https://plc.directory/${did}`) return Response.json({ service: [{ id: "#atproto_pds", serviceEndpoint: "https://pds.example" }] }); if (url.startsWith("https://pds.example/xrpc/com.atproto.repo.listRecords?")) return Response.json({ records }); return new Response("not found", { status: 404 }); } // A signed-in OAuth session: fetchHandler is what sessionXrpc calls through (DPoP and refresh live there). function session(writes) { return { did, async fetchHandler(path, init) { if (path.startsWith("/xrpc/com.atproto.repo.listRecords?")) return Response.json({ records }); if (path === "/xrpc/com.atproto.repo.createRecord") { const body = JSON.parse(init.body); writes.push(body); return Response.json({ uri: `at://${did}/${body.collection}/b`, cid: "bafy" }); } return Response.json({ error: "InvalidRequest" }, { status: 400 }); }, }; } async function cubitWith(signedIn) { const ctx = createNodeContext({ id: "hello-test", stateDir: await mkdtemp(join(tmpdir(), "hello-")), fetch: network }); const client = Promise.resolve({ init: async () => signedIn && { session: signedIn }, signIn: async () => {}, revoke: async () => {} }); const cubit = new HelloCubit({ ctx, client, hello: local("hello"), statusphere: local("statusphere") }); await cubit.init(); return cubit; } test("count runs signed out: a public read of the account's PDS", async () => { const cubit = await cubitWith(undefined); assert.equal(cubit.state.session, "signed-out"); await cubit.count({ did, collection: "xyz.statusphere.status" }); assert.deepEqual(cubit.state.count, { status: "done", result: { collection: "xyz.statusphere.status", records: 1 }, error: null }); }); test("setStatus writes through the user's session", async () => { const writes = []; const cubit = await cubitWith(session(writes)); assert.equal(cubit.state.did, did); await cubit.setStatus("🦋"); assert.equal(cubit.state.status.status, "done", cubit.state.status.error); assert.equal(cubit.state.status.result.status, "🦋"); assert.equal(writes[0].collection, "xyz.statusphere.status"); await cubit.setStatus("🙂"); assert.equal(cubit.state.status.result.unchanged, true, "the latest status already matches"); });