tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
85 lignes · 3898 octetsBrut · Historique
1/**
2 * Headless-Chromium smoke for a shipped bundle whose fixture page drives
3 * itself: `bun examples/page-smoke.ts <name>` serves examples/<name>.html
4 * with `bun <file>.html` (toaster-smoke.ts's pattern), loads it in the
5 * playwright-pinned Chromium with --dump-dom + --virtual-time-budget, and
6 * reads the SMOKE:{"checks": {…}} verdict the page (examples/<name>.page.ts)
7 * writes into its DOM. Every check must be true. Build first: the page
8 * imports dist/<name>.js, not the sources. Exits non-zero on failure.
9 *
10 * The fixtures for vote, more, lite-youtube and combobox use it; each stubs
11 * the server (fetch) in the page, so no backend runs.
12 */
13import { spawn } from "node:child_process";
14import { readdirSync } from "node:fs";
15
16const name = process.argv[2];
17if (!name) {
18 console.error("usage: bun examples/page-smoke.ts <name> (serves examples/<name>.html)");
19 process.exit(2);
20}
21const BUNDLE = `${import.meta.dir}/../dist/${name}.js`;
22if (!(await Bun.file(BUNDLE).exists())) {
23 console.error(`dist/${name}.js is missing — run \`bun run build\` first; the smoke exercises the shipped bundle, not the sources.`);
24 process.exit(1);
25}
26
27/** The newest playwright-cached Chromium (pebble's bench/profile.ts pattern). */
28function chrome(): string {
29 const cache = `${process.env.HOME}/.cache/ms-playwright`;
30 const newest = readdirSync(cache)
31 .filter(dir => /^chromium-\d+$/.test(dir))
32 .sort()
33 .pop();
34 if (newest === undefined) throw new Error(`no playwright chromium under ${cache}`);
35 return `${cache}/${newest}/chrome-linux64/chrome`;
36}
37
38/** `bun examples/<name>.html` on a random port; resolves with the origin. */
39function serve(): Promise<{ origin: string; stop: () => void }> {
40 const server = spawn("bun", [`${import.meta.dir}/${name}.html`, "--port=0"], { stdio: ["ignore", "pipe", "inherit"] });
41 return new Promise((resolve, reject) => {
42 let buffer = "";
43 server.stdout.on("data", (chunk: Buffer) => {
44 buffer += chunk.toString();
45 const match = buffer.match(/url: (http:\/\/localhost:\d+)\//);
46 if (match) resolve({ origin: match[1]!, stop: () => server.kill() });
47 });
48 server.on("exit", code => reject(new Error(`dev server exited early (${code}): ${buffer}`)));
49 setTimeout(() => reject(new Error(`dev server never printed its url: ${buffer}`)), 15000);
50 });
51}
52
53/** Full DOM after the virtual time budget lets the page's async work settle; a phone-sized window. */
54function dumpDom(origin: string): Promise<string> {
55 const run = spawn(
56 chrome(),
57 ["--headless=new", "--disable-gpu", "--no-sandbox", "--window-size=390,844", "--virtual-time-budget=8000", "--dump-dom", origin],
58 { stdio: ["ignore", "pipe", "ignore"] },
59 );
60 return new Promise((resolve, reject) => {
61 let dom = "";
62 run.stdout.on("data", (chunk: Buffer) => (dom += chunk.toString()));
63 run.on("exit", code => (code === 0 ? resolve(dom) : reject(new Error(`chromium exited ${code}`))));
64 });
65}
66
67const { origin, stop } = await serve();
68try {
69 const dom = await dumpDom(origin);
70 const marker = dom.match(/SMOKE:(\{[^<]*\})/);
71 if (!marker) throw new Error("no SMOKE verdict in the dumped DOM — the page script did not run to completion");
72 const verdict = JSON.parse(marker[1]!.replaceAll("&quot;", '"').replaceAll("&amp;", "&")) as {
73 checks: Record<string, boolean>;
74 observed?: unknown;
75 };
76 console.log(JSON.stringify(verdict, null, 2));
77 const failures = Object.entries(verdict.checks).filter(([, passed]) => passed !== true);
78 if (Object.keys(verdict.checks).length === 0) failures.push(["the page made no checks", false]);
79 if (failures.length > 0) {
80 for (const [what] of failures) console.error(`FAIL: ${what}`);
81 process.exitCode = 1; // not process.exit(): the finally must stop the dev server, or it outlives the smoke
82 } else console.log(`${name} smoke: PASS`);
83} finally {
84 stop();
85}