/** * TWO PHONES FOUND AT ONCE, against the real Worker (KEPT FOUR 3 B): * * cd apps/business && pnpm -s exec tsx scripts/__tests__/e2e-vault-race.ts * * Starts the Worker under `wrangler dev` with the personal object onboarded (`dev-worker.ts`), then the phone core's * own walk (`crates/cotton-ffi/src/phone/personal_object_e2e.rs`, `two_phones_founding_at_once_read_each_others_passwords`): * two phones, each with a password saved before sign-in, sign in to the new account at once; both hear no vault and * both post their founding; the object keeps one and tells the other `vault_founded`; the other is let in by the * typed-code approval, its unlock seals its password again under the account's key, and each reads the other's. * Last, the object's own rows: one founding, every wrap at one version, both passwords there and sealed. * wrangler dev is stopped and the temp dir removed however it ends. Invented data only. */ 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, OWNER, check, startDevWorker, stop } from "./dev-worker.ts"; import { WS } from "./fixture.ts"; const CRATES = resolve(APP, "../../crates"); const WORDS = ["an-invented-password-a", "an-invented-password-b"]; const tmp = mkdtempSync(join(tmpdir(), "e2e-vault-race-")); 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 walk = spawnSync( "bash", ["../scripts/cargo-target-guard.sh", "--run", "cargo", "test", "-q", "-p", "cotton-ffi", "--lib", "phone::personal_object::e2e::two_phones_founding_at_once", "--", "--ignored", "--nocapture"], { cwd: CRATES, encoding: "utf8", env: { ...process.env, COTTON_E2E_BASE: base, COTTON_E2E_PHONE: OWNER, COTTON_E2E_DATABASE: WS } }, ); const said = /E2E-VAULT-RACE (\{.*\})/.exec(walk.stdout); const walked = walk.status === 0 && said; check(walked, `the two phones' walk passes${walked ? "" : `\n${walk.error ? String(walk.error) : `${walk.stdout?.slice(-200000) ?? ""}${walk.stderr?.slice(-200000) ?? ""}`}`}`); const admin = await api<{ token?: string }>("POST", "/v1/act", { act: "mint_token", args: { name: "e2e vault race admin", scopes: ["owner"] } }, credential); const exported = await api<{ rows: Row[] }>("GET", "/v1/admin/export?limit=10000", undefined, admin.token!); const live = exported.rows.filter((r) => !r.deleted); const wraps = live.filter((r) => r.table === "vault_key" && typeof r.data.wrapped_vmk === "string"); check(wraps.filter((r) => r.id.startsWith("founder:")).length === 1, "one founding kept"); check(new Set(wraps.map((r) => r.data.vmk_version)).size === 1, "every wrap at one version"); const secrets = live.filter((r) => r.table === "secret"); check(secrets.length === 2 && secrets.every((r) => typeof r.data.value_enc === "string"), "both passwords on the object, sealed"); const text = JSON.stringify(exported.rows); check(WORDS.every((w) => !text.includes(w)), "no password in the clear on the object"); console.log("e2e vault race: passed"); } catch (error) { console.error((error as Error).message); process.exitCode = 1; } finally { cleanup(); }