import { type ChildProcess, spawn } from "node:child_process"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { delimiter, dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { fauxAssistantMessage } from "@earendil-works/pi-ai"; import { expect, it } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; import { findChromium, openCdp } from "../src/layout.ts"; import type { CanvasState } from "../src/store.ts"; async function until( check: () => Promise, label: string, ): Promise { const deadline = Date.now() + 10000; while (Date.now() < deadline) { if (await check()) return; await new Promise((resolve) => setTimeout(resolve, 40)); } throw new Error(`Browser proof timed out: ${label}`); } async function stop(browser: ChildProcess): Promise { if (browser.exitCode !== null || browser.signalCode !== null) return; await new Promise((resolve) => { const timer = setTimeout(() => { browser.kill("SIGKILL"); }, 2000); browser.once("exit", () => { clearTimeout(timer); resolve(); }); browser.kill("SIGTERM"); }); } it.runIf(process.env["PI_CANVAS_NATIVE_TESTS"] === "1")( "replays frozen-viewer agent commits with separate Undo and protects an unsent draft on resume", async () => { const executable = await findChromium(); const profile = await mkdtemp(join(tmpdir(), "pi-canvas-viewer-proof-")); let pi: PiSession | undefined; let browser: ChildProcess | undefined; let cdp: Awaited> | undefined; try { pi = await createPiSession({ extensionPaths: [ fileURLToPath( new URL("../../../dist/canvas/index.ts", import.meta.url), ), ], env: { PATH: "/canvas-test-no-executables", TERMUX_VERSION: undefined }, responses: Array.from({ length: 30 }, () => fauxAssistantMessage("")), }); const session = pi; let url = ""; const ctx = session.session.extensionRunner.createCommandContext(); ctx.ui.notify = (text) => { const match = text.match(/http:\/\/127\.0\.0\.1:\d+\/#\S+/u); if (match) url = match[0]; }; await session.extensions[0]!.commands.get("canvas")!.handler("open", ctx); const headers = { Authorization: `Bearer ${new URL(url).hash.slice(1)}` }; const state = async (): Promise => ( await fetch(new URL("/api/state", url), { headers, signal: AbortSignal.timeout(2000), }) ).json() as Promise; const scene = async ( elements: CanvasState["elements"], revision: number, ) => fetch(new URL("/api/action", url), { method: "POST", headers: { ...headers, Origin: new URL(url).origin, "Content-Type": "application/json", }, body: JSON.stringify({ kind: "scene", clientId: "remote-proof", sequence: revision + 1, activity: "active", revision, elements, }), signal: AbortSignal.timeout(2000), }); await scene( [ { id: "box", type: "rectangle", x: 400, y: 220, width: 100, height: 80, }, ], 0, ); browser = spawn( executable, [ "--headless=new", "--remote-debugging-port=0", `--user-data-dir=${profile}`, "--no-first-run", "--disable-gpu", "about:blank", ], { stdio: ["ignore", "ignore", "pipe"], env: { ...process.env, PATH: [dirname(executable), process.env["PATH"]].join(delimiter), }, }, ); const processHandle = browser; const endpoint = await new Promise((resolve, reject) => { let text = ""; const timer = setTimeout( () => reject(new Error("Viewer startup timed out")), 15000, ); processHandle.once("error", reject); processHandle.stderr!.on("data", (chunk: Buffer) => { text = (text + chunk.toString()).slice(-8192); const match = text.match(/DevTools listening on (ws:\/\/[^\s]+)/u); if (match?.[1]) { clearTimeout(timer); resolve(match[1]); } }); }); const targetUrl = new URL(endpoint); targetUrl.protocol = "http:"; targetUrl.pathname = "/json/new"; targetUrl.search = `?${encodeURIComponent(url)}`; const target = (await ( await fetch(targetUrl, { method: "PUT", signal: AbortSignal.timeout(10000), }) ).json()) as { webSocketDebuggerUrl: string }; cdp = await openCdp( target.webSocketDebuggerUrl, AbortSignal.timeout(15000), ); const client = cdp; const evaluate = async (expression: string): Promise => { const value = await client.call("Runtime.evaluate", { expression, awaitPromise: true, returnByValue: true, }); if (value["exceptionDetails"]) { const details = value["exceptionDetails"] as { text?: string; exception?: { description?: string }; }; throw new Error( `Browser proof script failed: ${details.exception?.description ?? details.text ?? "Unknown browser exception"}`, ); } return (value["result"] as { value: T }).value; }; await client.call("Emulation.setFocusEmulationEnabled", { enabled: true, }); await until( () => evaluate( "!!document.querySelector('canvas.static') && document.body.innerText.includes('Shared with Pi')", ), "initial viewer", ); await until(async () => { const current = await state(); return current.elements[0]?.["version"] !== undefined; }, "native initialization acknowledgement"); await evaluate( "window.__renderedRevision=-1;const old=window.fetch;window.fetch=async function(i,o){const b=o?.body&&JSON.parse(o.body);const r=await old.call(this,i,o);if(b?.kind==='activity'&&b.activity==='settled'&&r.ok)window.__renderedRevision=b.revision;return r};true", ); await client.call("Page.setWebLifecycleState", { state: "frozen" }); const draw = async (dx: number) => { const current = await state(); return session.extensions[0]!.tools.get("canvas")!.definition.execute( "background-draw", { action: "draw", baseRevision: current.revision, title: "Frozen viewer transaction", moves: [{ ids: ["box"], dx, dy: 0 }], }, undefined, undefined, session.session.extensionRunner.createToolContext( "background-draw", undefined, ), ); }; await draw(20); await draw(30); const committed = await state(); expect(committed.elements[0]).toMatchObject({ x: 450, y: 220 }); await client.call("Page.setWebLifecycleState", { state: "active" }); await client.call("Emulation.setFocusEmulationEnabled", { enabled: true, }); await evaluate("window.dispatchEvent(new Event('focus')); true"); await until( () => evaluate( `window.__renderedRevision >= ${committed.revision} && !document.querySelector('[aria-label="Undo"]').disabled`, ), "catch-up native Undo", ); // Native UI Undo changes the editor, then ordinary scene saving acknowledges it. await evaluate( "document.querySelector('[aria-label=\"Undo\"]').click(); true", ); await until( async () => (await state()).elements[0]?.x === 420, "first agent Undo", ); await evaluate( "document.querySelector('[aria-label=\"Undo\"]').click(); true", ); await until( async () => (await state()).elements[0]?.x === 400, "second agent Undo", ); await evaluate( "document.querySelector('[aria-label=\"Redo\"]').click(); true", ); await until( async () => (await state()).elements[0]?.x === 420, "first agent Redo", ); await evaluate( "document.querySelector('[aria-label=\"Redo\"]').click(); true", ); await until( async () => (await state()).elements[0]?.x === 450, "second agent Redo", ); await evaluate( "window.__heldSave = null; const original = window.fetch; window.fetch = function(i,o) { const b=o?.body&&JSON.parse(o.body); if(b?.kind==='scene') { window.__heldSave=b; return new Promise(()=>{}); } return original.call(this,i,o); }; true", ); await client.call("Input.dispatchMouseEvent", { type: "mouseMoved", x: 450, y: 260, }); await client.call("Input.dispatchMouseEvent", { type: "mousePressed", x: 450, y: 260, button: "left", clickCount: 1, }); await client.call("Input.dispatchMouseEvent", { type: "mouseMoved", x: 470, y: 280, button: "left", buttons: 1, }); await client.call("Input.dispatchMouseEvent", { type: "mouseReleased", x: 470, y: 280, button: "left", clickCount: 1, }); await until( () => evaluate("!!window.__heldSave"), "held local draft", ); const held = await evaluate( "window.__heldSave.elements", ); expect(held.find((element) => element.id === "box")).toMatchObject({ x: 470, y: 240, }); await client.call("Page.setWebLifecycleState", { state: "frozen" }); const shared = await state(); await scene( [ ...shared.elements, { id: "foreign", type: "rectangle", x: 700, y: 220, width: 80, height: 80, }, ], shared.revision, ); await client.call("Page.setWebLifecycleState", { state: "active" }); await client.call("Emulation.setFocusEmulationEnabled", { enabled: true, }); await evaluate("window.dispatchEvent(new Event('focus')); true"); await until( () => evaluate("!!document.querySelector('.conflict')"), "dirty resync conflict", ); await evaluate( "(()=>{const original=URL.createObjectURL; URL.createObjectURL=function(blob){window.__download=blob;return original(blob)};document.querySelector('[data-testid=\"main-menu-trigger\"]').click();return true})()", ); await evaluate( "[...document.querySelectorAll('button')].find(b=>b.textContent.includes('Download drawing')).click();true", ); const downloaded = await evaluate<{ elements: CanvasState["elements"] }>( "(async()=>JSON.parse(await window.__download.text()))()", ); expect( downloaded.elements.find((element) => element.id === "box"), ).toMatchObject({ x: 470, y: 240 }); expect( downloaded.elements.some((element) => element.id === "foreign"), ).toBe(false); expect( (await state()).elements.some((element) => element.id === "foreign"), ).toBe(true); expect(session.errors).toEqual([]); } finally { cdp?.close(); if (browser) await stop(browser); await pi?.dispose(); await rm(profile, { recursive: true, force: true, maxRetries: 5, retryDelay: 100, }); } }, 90000, );