import { chromium, expect, test } from "@playwright/test"; import AxeBuilder from "@axe-core/playwright"; import { readFile } from "node:fs/promises"; import vector from "../../public/hidden-path/v1/example.json" with { type: "json" }; const workbench = "/work/hidden-path?d=3"; const verified = "Proof verified."; test.use({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true, deviceScaleFactor: 3, }); test("a mobile visitor creates a private proof and an independent browser verifies its URL and file", async ({ page, context, baseURL, }, testInfo) => { test.setTimeout(90000); await page.goto(workbench, { waitUntil: "networkidle" }); const requests: { url: string; method: string; body: string | null }[] = []; context.on("request", (r) => { requests.push({ url: r.url(), method: r.method(), body: r.postData() }); }); await expect( page.getByRole("button", { name: "Generate proof" }), ).toBeDisabled(); await expect( page.getByRole("button", { name: "Step to vertex 2" }), ).toBeDisabled(); await page.getByRole("button", { name: "Step to vertex 1" }).tap(); await page.getByRole("button", { name: "Undo step" }).tap(); for (const v of [3, 4, 7, 8]) await page.getByRole("button", { name: "Step to vertex " + v }).tap(); await page.screenshot({ path: testInfo.outputPath("private-path.png"), fullPage: true, }); // Hold the public key request until the memory probe is attached. Observe // explicit WASM memory reservations, not process RSS or JS heap usage. let resume!: () => void; const gate = new Promise((resolve) => { resume = resolve; }); await context.route( "**/hidden-path/v1/verification-key.json", async (route) => { await gate; await route.continue(); }, ); const reservations: number[] = []; page.on("console", (msg) => { if (msg.text().startsWith("wasm-pages=")) reservations.push(Number(msg.text().slice(11))); }); const workerEvent = page.waitForEvent("worker"); await page.getByRole("button", { name: "Generate proof" }).tap(); const worker = await workerEvent; try { await worker.evaluate(() => { WebAssembly.Memory = new Proxy(WebAssembly.Memory, { construct(target, args: [WebAssembly.MemoryDescriptor]) { console.info("wasm-pages=" + args[0].initial); return Reflect.construct(target, args); }, }); }); } finally { resume(); } await expect(page.locator(".path-status")).toHaveText(verified, { timeout: 30000, }); expect(reservations.length).toBeGreaterThan(0); expect(Math.max(...reservations)).toBeLessThanOrEqual(128); const timing = await page.locator(".path-metric").innerText(); console.log( "Mobile emulation:", timing, "explicit WASM allocations (64 KiB pages):", reservations, ); await expect(page.locator("html")).toHaveJSProperty("scrollWidth", 390); const link = await page .getByLabel("Verification link", { exact: true }) .inputValue(); const [download] = await Promise.all([ page.waitForEvent("download"), page.getByRole("button", { name: "Download JSON" }).tap(), ]); const file = await readFile((await download.path())!); const artifact = JSON.parse(file.toString()); expect(Object.keys(artifact).sort()).toEqual(Object.keys(vector).sort()); expect(Object.keys(artifact.proof).sort()).toEqual( Object.keys(vector.proof).sort(), ); expect(artifact.publicSignals).toHaveLength(1); expect(new URL(link).hash.length).toBeLessThan(2000); expect( JSON.parse( Buffer.from(new URL(link).hash.slice(3), "base64url").toString(), ), ).toEqual(artifact); expect( await page.evaluate(async () => [ localStorage.length, sessionStorage.length, (await indexedDB.databases()).length, ]), ).toEqual([0, 0, 0]); expect(requests.length).toBeGreaterThan(0); for (const request of requests) { const url = new URL(request.url); expect(url.origin).toBe(baseURL); expect(request.method).toBe("GET"); expect(request.body).toBeNull(); expect(url.search + url.hash).toBe(""); expect(url.pathname).toMatch( /^\/(?:_next\/static\/[^?]+|hidden-path\/v1\/(?:verification-key\.json|circuit\.wasm|proving\.zkey))$/, ); } const recipient = await chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL, }); try { const fresh = await recipient.newContext({ baseURL }); const seen: string[] = []; fresh.on("request", (r) => { seen.push(r.url()); }); const other = await fresh.newPage(); await other.goto(link); await expect(other.locator(".path-status")).toHaveText(verified); await other.getByRole("link", { name: "Skip to content" }).focus(); await other.keyboard.press("Enter"); await expect(other.locator("main")).toBeFocused(); await expect(other).toHaveURL(link); await expect(other.locator(".path-status")).toHaveText(verified); await expect( other.getByRole("button", { name: /Step to vertex/ }), ).toHaveCount(0); await other.locator(".path-tamper summary").click(); await other .getByRole("button", { name: "Test altered commitment" }) .click(); await expect(other.locator(".path-tamper [role=status]")).toContainText( "Altered copy rejected.", ); expect( await other.getByLabel("Verification link", { exact: true }).inputValue(), ).toBe(link); await other.goto("/verify/hidden-path"); await other .getByLabel("Import proof JSON") .setInputFiles({ name: "receipt.json", mimeType: "application/json", buffer: file, }); await expect(other.locator(".path-status")).toHaveText(verified); expect(seen.some((url) => url.includes("verification-key.json"))).toBe( true, ); expect( seen.some((url) => /#p=|proving\.zkey|circuit\.wasm/.test(url)), ).toBe(false); } finally { await recipient.close(); } await page.getByRole("button", { name: "Generate proof" }).tap(); await expect(page.locator(".path-status")).toHaveText(verified); await expect( page.getByLabel("Verification link", { exact: true }), ).not.toHaveValue(link); await page.getByRole("button", { name: "Reset route" }).tap(); await expect(page.getByLabel("Verification link")).toHaveCount(0); }); test("receiver rejects malformed and oversized inputs, corrupt keys and changed commitments", async ({ page, context, }) => { await page.goto("/verify/hidden-path#p=!"); await expect(page.locator(".path-status")).toContainText( "incomplete or too large", ); const upload = (buffer: Buffer) => page .getByLabel("Import proof JSON") .setInputFiles({ name: "test.json", mimeType: "application/json", buffer, }); await upload(Buffer.alloc(4097)); await expect(page.locator(".path-status")).toContainText("4 KiB"); await context.route("**/verification-key.json", (route) => route.fulfill({ body: "{}", contentType: "application/json" }), ); await upload(Buffer.from(JSON.stringify(vector))); await expect(page.locator(".path-status")).toContainText("integrity check"); await context.unroute("**/verification-key.json"); await page.getByRole("button", { name: "Verify again" }).click(); await expect(page.locator(".path-status")).toHaveText(verified); await upload( Buffer.from(JSON.stringify({ ...vector, publicSignals: ["1"] })), ); await expect(page.locator(".path-status")).toContainText("Proof rejected."); await expect(page.getByRole("button", { name: "Download JSON" })).toHaveCount( 0, ); }); test("a cancelled verification can be retried and unsupported browsers get an explanation", async ({ page, context, }) => { const link = "/verify/hidden-path#p=" + Buffer.from(JSON.stringify(vector)).toString("base64url"); let resume!: () => void; const gate = new Promise((resolve) => { resume = resolve; }); await context.route("**/verification-key.json", async (route) => { await gate; await route.continue().catch(() => {}); }); await page.goto(link); await page.getByRole("button", { name: "Cancel" }).click(); resume(); await expect(page.locator(".path-status")).toContainText("cancelled"); await page.getByRole("button", { name: "Verify again" }).click(); await expect(page.locator(".path-status")).toHaveText(verified); await page.addInitScript(() => Object.defineProperty(window, "Worker", { value: undefined }), ); await page.reload(); await expect(page.locator(".path-status")).toContainText("This device needs"); }); test("the experiment and recipient controls reflow and pass automated accessibility checks", async ({ page, }) => { await page.setViewportSize({ width: 320, height: 740 }); for (const url of [ workbench, "/verify/hidden-path#p=" + Buffer.from(JSON.stringify(vector)).toString("base64url"), ]) { await page.goto(url); if (url.startsWith("/verify")) await expect(page.locator(".path-status")).toHaveText(verified); expect( await page.evaluate( () => document.documentElement.scrollWidth <= innerWidth, ), ).toBe(true); const result = await new AxeBuilder({ page }) .withTags(["wcag2a", "wcag2aa", "wcag21a", "wcag21aa", "wcag22aa"]) .analyze(); expect( result.violations.map((v) => [v.id, v.nodes.map((n) => n.target)]), ).toEqual([]); } });