/** * The data key and a sealed table's versions, phone against the personal * object, end to end: * * cd apps/business && pnpm -s exec tsx scripts/__tests__/e2e-seal.ts * * Starts the Worker under `wrangler dev` with the personal object onboarded * (`dev-worker.ts`) and lays a synthetic sealed table, `e2e_diary` (no kit * table is sealed yet). Then the phone core's own walk * (`crates/cotton-ffi/src/phone/personal_object_e2e.rs`, * `the_phone_mints_the_data_key_and_seals_under_the_versions_the_object_learns`): * mint the data key, write a row sealed at version 1, advance the table * twice (a stale advance refused), and seal each next row under the version * its pull learned. Last, the object is read: one data key row, the table at * version 3, the three rows sealed at 1, 2 and 3, and none of their words * anywhere it keeps rows. Synthetic rows only; wrangler dev is stopped and * the temp dir removed however it ends. */ import { spawnSync, type ChildProcess } from "node:child_process"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { APP, check, startDevWorker, stop } from "./dev-worker.ts"; import { WS } from "./fixture.ts"; const CRATES = resolve(APP, "../../crates"); const WORDS = ["Pumpkin", "Quokka", "Zanzibar", "jetty", "compost"]; const DIARY = { format_version: 1, id: "e2e_diary", name: "Diary", plural: "Diaries", title: "body", date: "written_at", columns: [ { key: "written_at", name: "Written", type: "date" }, { key: "body", name: "Body", type: "longtext" }, { key: "mood", name: "Mood", type: "text" }, { key: "trashed_at", name: "Binned", type: "date" }, ], slip: { title: "body", subtitle: null, trailing: "written_at", photo: null, toggle: null }, tile: { face: "slip-list" }, page: [{ heading: null, columns: ["body", "mood", "written_at"] }], instructions: "A synthetic sealed table for the seal end-to-end walk.", seal: { clear: ["written_at", "trashed_at"], blind: ["body"] }, }; const tmp = mkdtempSync(join(tmpdir(), "e2e-seal-")); 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; }); const minted = await api<{ token?: string }>("POST", "/v1/act", { act: "mint_token", args: { name: "e2e seal", scopes: ["owner"], allow: "all" } }, credential); await api("POST", "/v1/admin/definitions/tables", { tables: [DIARY] }, minted.token!); check(true, "the synthetic sealed table is laid"); const walk = spawnSync( "cargo", ["test", "-q", "-p", "cotton-ffi", "--lib", "phone::personal_object::e2e::the_phone_mints_the_data_key", "--", "--ignored", "--nocapture"], { cwd: CRATES, encoding: "utf8", env: { ...process.env, COTTON_E2E_BASE: base, COTTON_E2E_CREDENTIAL: credential, COTTON_E2E_DATABASE: WS } }, ); const said = /E2E-SEAL (\{.*\})/.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(-4000) ?? ""}${walk.stderr?.slice(-4000) ?? ""}`}`}`); const { rows } = JSON.parse(said![1]!) as { rows: string[] }; const exported = await api<{ rows: Row[] }>("GET", "/v1/admin/export?limit=10000", undefined, minted.token!); const keys = exported.rows.filter((r) => r.table === "seal_key" && !r.deleted); check(keys.filter((r) => r.id === "dk").length === 1 && typeof keys.find((r) => r.id === "dk")!.data.dk_enc === "string", "one data key, wrapped"); check(keys.find((r) => r.id === "v:e2e_diary")?.data.v === 3, "the table at version 3"); const diary = rows.map((id) => exported.rows.find((r) => r.table === "e2e_diary" && r.id === id)); check(diary.every((r) => r && typeof r.data.sealed_enc === "string" && r.data.body === undefined && r.data.mood === undefined), "every row held sealed, no sealed column in the clear"); check(JSON.stringify(diary.map((r) => r!.data.seal_v)) === "[1,2,3]", `sealed at 1, 2 and 3 (${JSON.stringify(diary.map((r) => r?.data.seal_v))})`); const everything = JSON.stringify(exported); for (const w of WORDS) check(!everything.includes(w), `no "${w}" anywhere the object exports`); console.log("e2e seal: passed"); } catch (error) { console.error((error as Error).message); process.exitCode = 1; } finally { cleanup(); }