/** * The owner's phone against the personal object, end to end, on an invented life: * * cd apps/business && pnpm -s exec tsx scripts/__tests__/e2e-phone.ts * * Starts the Worker under `wrangler dev` with the personal object onboarded * (`dev-worker.ts`), imports the synthetic life.db into it, then runs the phone * core's own walk (`crates/cotton-ffi/src/phone/personal_object_e2e.rs`): * `personal.connect`, a sync to level, the object's tables, tools and Home * in the copy (a table the app ships no definition of among them), the todo * deck reading the copy, the * message window, a picture drawn at a card's width, and a todo the phone * makes and changes. Last, the object is asked whether it holds that todo as * the phone left it. wrangler dev is stopped and the temp dir removed however * it ends. */ import { spawnSync, type ChildProcess } from "node:child_process"; import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { APP, check, pnpm, startDevWorker, stop } from "./dev-worker.ts"; import { buildLifeDb, WS } from "./fixture.ts"; const CRATES = resolve(APP, "../../crates"); const tmp = mkdtempSync(join(tmpdir(), "e2e-phone-")); 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); }); try { const fixture = buildLifeDb(tmp); 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 phone", scopes: ["owner"], allow: "all" } }, credential); const tokenFile = join(tmp, "token"); writeFileSync(tokenFile, `${minted.token!}\n`); chmodSync(tokenFile, 0o600); const imported = pnpm(["import-personal", "--from", fixture.path, "--base", base, "--token", tokenFile, "--vault-key-file", fixture.vaultKeyPath, "--media-root", fixture.mediaRoot]); check(imported.status === 0, `the invented life is imported${imported.status ? `\n${imported.err}` : ""}`); const walk = spawnSync( "cargo", ["test", "-q", "-p", "cotton-ffi", "--lib", "phone::personal_object::e2e::the_phone_reads_and_writes_its_personal_object", "--", "--ignored", "--nocapture"], { cwd: CRATES, encoding: "utf8", env: { ...process.env, COTTON_E2E_BASE: base, COTTON_E2E_CREDENTIAL: credential, COTTON_E2E_DATABASE: WS } }, ); const said = /E2E-PHONE (\{.*\})/.exec(walk.stdout); const walked = walk.status === 0 && said; // A cargo that never started (walk.error) has no output to show, only why. check(walked, `the phone's walk passes${walked ? "" : `\n${walk.error ? String(walk.error) : `${walk.stdout?.slice(-4000) ?? ""}${walk.stderr?.slice(-4000) ?? ""}`}`}`); const { made, tables, quotas } = JSON.parse(said![1]!) as { made: string; tables: string[]; quotas: string[] }; // The fan draws a tool by its home table's definition: the copy holds the object's (the app bundles no table). check(tables.includes("quota"), `the copy holds the object's quota table (${tables.join(", ")})`); // And the made table's rows: every live quota row reaches the copy. check(quotas.join(",") === "q-1,q-2", `the object's live quota rows reach the copy (${quotas.join(", ")})`); type Row = { table: string; id: string; data: Record; deleted?: boolean }; const rows: Row[] = []; for (let query = "since=0"; ; ) { const page = await api<{ rows: Row[]; next?: string }>("GET", `/v1/sync?${query}`, undefined, credential); rows.push(...page.rows); if (!page.next) break; query = `cursor=${page.next}`; } const held = rows.find((r) => r.table === "todo" && r.id === made); check(held?.data.title === "An errand the phone wrote", `the object holds the todo under the id the phone made it with (${made})`); check(held?.data.status === "done", "as the phone last changed it"); console.log("e2e phone: passed"); } catch (error) { console.error((error as Error).message); process.exitCode = 1; } finally { cleanup(); }