/** * The 15-minute unlock window end to end, against a local Worker: * * cd apps/business && pnpm -s exec tsx scripts/__tests__/e2e-unlock.ts * * Starts the Worker under `wrangler dev` with WINDOW_KEY set and the * personal object onboarded (`dev-worker.ts`), adds one synthetic sealed * table (no kit table is sealed) and mints an owner token for the * "assistant". Then the phone core's own walk * (`crates/cotton-ffi/src/phone/personal_object_e2e.rs`, * `an_unlock_window_opens_a_sealed_row_for_the_token_that_asked_and_ends`): * sign in, found the vault, keep and publish the person key, mint the data * key, write a sealed row, request_unlock over MCP, allow from the phone * core, read the row in the window, end, advance. Last, the object's own * rows: the person key published, the grant ended by the phone, the seal * version moved, and no synthetic word anywhere in rows. wrangler dev is * stopped and the temp dir removed however it ends. Synthetic rows only. */ import { spawnSync, type ChildProcess } from "node:child_process"; import { randomBytes } from "node:crypto"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { KITS } from "../../src/kits.ts"; import { APP, check, OWNER, startDevWorker, stop } from "./dev-worker.ts"; import { WS } from "./fixture.ts"; const CRATES = resolve(APP, "../../crates"); const SECRET_WORDS = ["zanzibar", "quokka", "marmalade", "pelican"]; /** The synthetic sealed table: the date clear, body and mood sealed, the body by word tokens. */ const DIARY = { format_version: 1, id: "sealed_diary", name: "Sealed diary", plural: "Sealed diaries", title: "body", date: "written_at", columns: [ { key: "written_at", type: "date" }, { key: "body", type: "longtext", required: true }, { key: "mood", type: "text" }, ], seal: { clear: ["written_at"], blind: ["body"] }, }; const tmp = mkdtempSync(join(tmpdir(), "e2e-unlock-")); let wrangler: ChildProcess | null = null; const cleanup = (): void => { stop(wrangler); wrangler = null; rmSync(tmp, { recursive: true, force: true }); }; process.on("SIGINT", () => { cleanup(); process.exit(130); }); type Row = { table: string; id: string; data: Record; deleted?: boolean }; try { const { base, api, credential } = await startDevWorker(tmp, WS, (child) => { wrangler = child; }, { WINDOW_KEY: randomBytes(32).toString("base64url") }); // The personal kit's definitions again, with the synthetic sealed table beside them. const personal = KITS.personal!; const admin = await api<{ token?: string }>("POST", "/v1/act", { act: "mint_token", args: { name: "e2e unlock admin", scopes: ["owner"], allow: "all" } }, credential); await api("PUT", "/v1/admin/definitions", { tools: personal.own, tables: [...personal.tables, DIARY], flavour: { flavour: "personal" } }, admin.token!); check(true, "the synthetic sealed table is defined"); const assistant = await api<{ token?: string }>("POST", "/v1/act", { act: "mint_token", args: { name: "Claude (claude.ai)", scopes: ["owner"], allow: "all" } }, credential); const walk = spawnSync( "cargo", ["test", "-q", "-p", "cotton-ffi", "--lib", "phone::personal_object::e2e::an_unlock_window_opens", "--", "--ignored", "--nocapture"], { cwd: CRATES, encoding: "utf8", env: { ...process.env, COTTON_E2E_BASE: base, COTTON_E2E_PHONE: OWNER, COTTON_E2E_DATABASE: WS, COTTON_E2E_TOKEN: assistant.token! } }, ); const said = /E2E-UNLOCK (\{.*\})/.exec(walk.stdout); const walked = walk.status === 0 && said; check(walked, `the phone's walk passes${walked ? "" : `\n${walk.error ? String(walk.error) : `${walk.stdout?.slice(-6000) ?? ""}${walk.stderr?.slice(-6000) ?? ""}`}`}`); const phone = JSON.parse(said![1]!) as { person: string; row: string; request: string; grant: string; moved: string[]; seal_v: { v: number } }; console.log(` phone ${JSON.stringify(phone)}`); const exported = await api<{ rows: Row[] }>("GET", "/v1/admin/export?limit=10000", undefined, admin.token!); const live = exported.rows.filter((r) => !r.deleted); const key = live.find((r) => r.table === "person_key"); check(key?.id === phone.person && typeof key.data.public_key === "string" && String(key.data.public_key).startsWith(phone.person), "the phone published its person key, by fingerprint"); const grant = live.find((r) => r.table === "unlock_grant" && r.id === phone.grant); check(grant?.data.ended_by === "phone" && typeof grant.data.ended_at === "string" && !("wrapped" in grant.data), "the grant ended by the phone, keeping no wrap"); const request = live.find((r) => r.table === "unlock_request" && r.id === phone.request); check(request?.data.state === "granted", "the request was granted"); // The pass after the end advances it (the after-pass hook), so the walk's own call may find nothing left to move. const version = live.find((r) => r.table === "seal_key" && r.id === "v:sealed_diary"); check(version?.data.v === 2 && phone.seal_v.v === 2, "sealed_diary moved to seal version 2 after the window, on the object and the phone"); const diary = live.find((r) => r.table === "sealed_diary" && r.id === phone.row); check(typeof diary?.data.sealed_enc === "string" && !("body" in (diary?.data ?? {})), "the row is sealed on the object"); const text = JSON.stringify(exported.rows).toLowerCase(); check(SECRET_WORDS.every((w) => !text.includes(w)), "no synthetic word in the object's rows"); console.log("e2e unlock: passed"); } catch (error) { console.error((error as Error).message); process.exitCode = 1; } finally { cleanup(); }