/** * The owner's phone as one device row on its personal object, across a * reinstall, end to end: * * cd apps/business && pnpm -s exec tsx scripts/__tests__/e2e-device.ts * * Starts the Worker under `wrangler dev` with the personal object onboarded * (`dev-worker.ts`) and 6,000 todos in it, so a pass is two pages. Then the * phone core's own walk (`crates/cotton-ffi/src/phone/personal_object_e2e.rs`, * `the_phone_is_one_device_row_on_its_object_across_a_reinstall`): the phone * signs in with its own device key, syncs, loses its container and keeps its * Keychain, connects again with the credential it kept and syncs. Last, the * object's device rows: one for the phone's device id, holding the object's * figures of the pass and the phone's bytes. wrangler dev is stopped and the * temp dir removed however it ends. */import { spawnSync, typeChildProcess } from"node:child_process";import { mkdtempSync, rmSync } from"node:fs";import { tmpdir } from"node:os";import { join, resolve } from"node:path";import { APP, check, OWNER, startDevWorker, stop } from"./dev-worker.ts";import { WS } from"./fixture.ts";const CRATES = resolve(APP, "../../crates");const TODOS = 6000;const tmp = mkdtempSync(join(tmpdir(), "e2e-device-"));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);});typeRow = { table: string; id: string; data: Record<string, unknown>; 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 device", scopes: ["owner"], allow: "all" } }, credential);for (let at = 0; at < TODOS; at += 1500) {const rows = Array.from({ length: Math.min(1500, TODOS - at) }, (_, i) => ({ table: "todo", data: { title: `errand ${at + i}`, status: "open" } }));await api("POST", "/v1/admin/import", { rows }, minted.token!); } check(true, `${TODOS} todos in the object`);const walk = spawnSync("cargo", ["test", "-q", "-p", "cotton-ffi", "--lib", "phone::personal_object::e2e::the_phone_is_one_device_row", "--", "--ignored", "--nocapture"], { cwd: CRATES, encoding: "utf8", env: { ...process.env, COTTON_E2E_BASE: base, COTTON_E2E_PHONE: OWNER, COTTON_E2E_DATABASE: WS } }, );const said = /E2E-DEVICE (\{.*\})/.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 phone = JSON.parse(said![1]!) as { credential: string; device_id: string; row: string };// Read whole through the export: a pull with the phone's credential would// move the row past its own pass (each page writes it), so it is not one.const exported = await api<{ rows: Row[] }>("GET", "/v1/admin/export?limit=10000", undefined, minted.token!);const mine = exported.rows.filter((r) => r.table === "device" && !r.deleted && r.data.device_id === phone.device_id); check(mine.length === 1, `one device row for the phone's device id across the reinstall (${mine.length})`);const row = mine[0]!; check(row.id === phone.row, "the row the object named to the phone"); console.log(` row ${JSON.stringify(row.data)}`); check(row.data.name === "E2E phone" && typeof row.data.platform === "string" && row.data.app_version === "1.0.0 (e2e)", "filed under what the phone says it is"); check(typeof row.data.synced_at === "string" && typeof row.data.sync_version === "number", "synced_at and sync_version, the object's"); check(typeof row.data.sync_rows_total === "number" && (row.data.sync_rows_total as number) >= TODOS, "sync_rows_total, the object's"); check(row.data.sync_rows_done === row.data.sync_rows_total && row.data.caught_up === true, "sync_rows_done of it and caught_up, the object's"); check(typeof row.data.sync_bytes === "number" && (row.data.sync_bytes as number) > 0, "sync_bytes, the phone's through /v1/act"); check((row.data.stalled_at ?? null) === null, "stalled_at clear: the phone never stalled"); console.log("e2e device: passed");} catch (error) { console.error((error asError).message); process.exitCode = 1;} finally { cleanup();}