/** * Headless-Chromium smoke for the shipped toaster bundle: serves * examples/toaster.html with `bun .html` (pebble's dev-server * pattern), loads it in the playwright-pinned Chromium with * --dump-dom + --virtual-time-budget, and asserts on the SMOKE:{...} * verdict the page writes into its DOM. Run: `bun run smoke` (build first: * the smoke exercises dist/toaster.js, not the sources). Exits non-zero on * failure. No test runner, no CI services. * * The pattern is pebble's examples/invokers-smoke.ts; the tablet scenario * lands with its engine rewrite (workstream L4). */ import { spawn } from "node:child_process"; import { readdirSync } from "node:fs"; const BUNDLE = `${import.meta.dir}/../dist/toaster.js`; if (!(await Bun.file(BUNDLE).exists())) { console.error("dist/toaster.js is missing — run `bun run build` first; the smoke exercises the shipped bundle, not the sources."); process.exit(1); } /** The newest playwright-cached Chromium (pebble's bench/profile.ts pattern). */ function chrome(): string { const cache = `${process.env.HOME}/.cache/ms-playwright`; const newest = readdirSync(cache) .filter(dir => /^chromium-\d+$/.test(dir)) .sort() .pop(); if (newest === undefined) throw new Error(`no playwright chromium under ${cache}`); return `${cache}/${newest}/chrome-linux64/chrome`; } /** `bun examples/toaster.html` on a random port; resolves with the origin. */ function serve(): Promise<{ origin: string; stop: () => void }> { const server = spawn("bun", [`${import.meta.dir}/toaster.html`, "--port=0"], { stdio: ["ignore", "pipe", "inherit"] }); return new Promise((resolve, reject) => { let buffer = ""; server.stdout.on("data", (chunk: Buffer) => { buffer += chunk.toString(); const match = buffer.match(/url: (http:\/\/localhost:\d+)\//); if (match) resolve({ origin: match[1], stop: () => server.kill() }); }); server.on("exit", code => reject(new Error(`dev server exited early (${code}): ${buffer}`))); setTimeout(() => reject(new Error(`dev server never printed its url: ${buffer}`)), 15000); }); } /** Full DOM after the virtual time budget lets the page's async work settle. */ function dumpDom(origin: string): Promise { const run = spawn( chrome(), ["--headless=new", "--disable-gpu", "--no-sandbox", "--virtual-time-budget=8000", "--dump-dom", origin], { stdio: ["ignore", "pipe", "ignore"] }, ); return new Promise((resolve, reject) => { let dom = ""; run.stdout.on("data", (chunk: Buffer) => (dom += chunk.toString())); run.on("exit", code => (code === 0 ? resolve(dom) : reject(new Error(`chromium exited ${code}`)))); }); } const { origin, stop } = await serve(); try { const dom = await dumpDom(origin); const marker = dom.match(/SMOKE:(\{[^<]*\})/); if (!marker) throw new Error("no SMOKE verdict in the dumped DOM — the page script did not run to completion"); const verdict = JSON.parse(marker[1]); console.log(JSON.stringify(verdict, null, 2)); const failures = [ [verdict.ok === true, "overall verdict"], [verdict.registered === true, " self-registered from the dist bundle"], [verdict.ariaLive === "polite", "the region is aria-live=polite"], [verdict.kinds?.join(",") === "success,error", "both announced toasts rendered as cards"], [verdict.roles?.join(",") === "status,alert", "polite/assertive channels: role=status, role=alert"], ].filter(([passed]) => !passed); if (failures.length > 0) { for (const [, what] of failures) console.error(`FAIL: ${what}`); process.exit(1); } console.log("toaster smoke: PASS"); } finally { stop(); }