| @@ -0,0 +1,83 @@ |
| 1 | +/** |
| 2 | + * Headless-Chromium smoke for the shipped toaster bundle: serves |
| 3 | + * examples/toaster.html with `bun <file>.html` (pebble's dev-server |
| 4 | + * pattern), loads it in the playwright-pinned Chromium with |
| 5 | + * --dump-dom + --virtual-time-budget, and asserts on the SMOKE:{...} |
| 6 | + * verdict the page writes into its DOM. Run: `bun run smoke` (build first: |
| 7 | + * the smoke exercises dist/toaster.js, not the sources). Exits non-zero on |
| 8 | + * failure. No test runner, no CI services. |
| 9 | + * |
| 10 | + * The pattern is pebble's examples/invokers-smoke.ts; the tablet scenario |
| 11 | + * lands with its engine rewrite (workstream L4). |
| 12 | + */ |
| 13 | +import { spawn } from "node:child_process"; |
| 14 | +import { readdirSync } from "node:fs"; |
| 15 | + |
| 16 | +const BUNDLE = `${import.meta.dir}/../dist/toaster.js`; |
| 17 | +if (!(await Bun.file(BUNDLE).exists())) { |
| 18 | + console.error("dist/toaster.js is missing — run `bun run build` first; the smoke exercises the shipped bundle, not the sources."); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +/** The newest playwright-cached Chromium (pebble's bench/profile.ts pattern). */ |
| 23 | +function chrome(): string { |
| 24 | + const cache = `${process.env.HOME}/.cache/ms-playwright`; |
| 25 | + const newest = readdirSync(cache) |
| 26 | + .filter(dir => /^chromium-\d+$/.test(dir)) |
| 27 | + .sort() |
| 28 | + .pop(); |
| 29 | + if (newest === undefined) throw new Error(`no playwright chromium under ${cache}`); |
| 30 | + return `${cache}/${newest}/chrome-linux64/chrome`; |
| 31 | +} |
| 32 | + |
| 33 | +/** `bun examples/toaster.html` on a random port; resolves with the origin. */ |
| 34 | +function serve(): Promise<{ origin: string; stop: () => void }> { |
| 35 | + const server = spawn("bun", [`${import.meta.dir}/toaster.html`, "--port=0"], { stdio: ["ignore", "pipe", "inherit"] }); |
| 36 | + return new Promise((resolve, reject) => { |
| 37 | + let buffer = ""; |
| 38 | + server.stdout.on("data", (chunk: Buffer) => { |
| 39 | + buffer += chunk.toString(); |
| 40 | + const match = buffer.match(/url: (http:\/\/localhost:\d+)\//); |
| 41 | + if (match) resolve({ origin: match[1], stop: () => server.kill() }); |
| 42 | + }); |
| 43 | + server.on("exit", code => reject(new Error(`dev server exited early (${code}): ${buffer}`))); |
| 44 | + setTimeout(() => reject(new Error(`dev server never printed its url: ${buffer}`)), 15000); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +/** Full DOM after the virtual time budget lets the page's async work settle. */ |
| 49 | +function dumpDom(origin: string): Promise<string> { |
| 50 | + const run = spawn( |
| 51 | + chrome(), |
| 52 | + ["--headless=new", "--disable-gpu", "--no-sandbox", "--virtual-time-budget=8000", "--dump-dom", origin], |
| 53 | + { stdio: ["ignore", "pipe", "ignore"] }, |
| 54 | + ); |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + let dom = ""; |
| 57 | + run.stdout.on("data", (chunk: Buffer) => (dom += chunk.toString())); |
| 58 | + run.on("exit", code => (code === 0 ? resolve(dom) : reject(new Error(`chromium exited ${code}`)))); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +const { origin, stop } = await serve(); |
| 63 | +try { |
| 64 | + const dom = await dumpDom(origin); |
| 65 | + const marker = dom.match(/SMOKE:(\{[^<]*\})/); |
| 66 | + if (!marker) throw new Error("no SMOKE verdict in the dumped DOM — the page script did not run to completion"); |
| 67 | + const verdict = JSON.parse(marker[1]); |
| 68 | + console.log(JSON.stringify(verdict, null, 2)); |
| 69 | + const failures = [ |
| 70 | + [verdict.ok === true, "overall verdict"], |
| 71 | + [verdict.registered === true, "<pebble-toaster> self-registered from the dist bundle"], |
| 72 | + [verdict.ariaLive === "polite", "the region is aria-live=polite"], |
| 73 | + [verdict.kinds?.join(",") === "success,error", "both announced toasts rendered as cards"], |
| 74 | + [verdict.roles?.join(",") === "status,alert", "polite/assertive channels: role=status, role=alert"], |
| 75 | + ].filter(([passed]) => !passed); |
| 76 | + if (failures.length > 0) { |
| 77 | + for (const [, what] of failures) console.error(`FAIL: ${what}`); |
| 78 | + process.exit(1); |
| 79 | + } |
| 80 | + console.log("toaster smoke: PASS"); |
| 81 | +} finally { |
| 82 | + stop(); |
| 83 | +} |