#!/usr/bin/env node /** * Gate for the headed browser tool family (tools/web/headed/). * * node tools/web/check-headed-tools.mjs * * Without launching Chrome, checks: * - every tool on disk is registered in docs/web/HEADED-TOOLS.md, and every * registered tool exists; * - every tool answers --help with exit 0 and rejects an unknown option with * exit 64 (the shared CLI contract); * - every tool parses argv through normalizeArgs(); * - every receipt under tests/golden/{GPU-PROBE,HEADED-SMOKE,GPU-SOAK}-* * carries schemaVersion 1, acceptance NOT_CLAIMED and a tool name that * maps to a file on disk. * * Exit 0 PASS, 1 FAIL. It never starts a browser, so it needs no DISPLAY. */ import { spawnSync } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; const REPO = path.resolve(import.meta.dirname, "../.."); const HEADED = path.join(REPO, "tools/web/headed"); const DOC = path.join(REPO, "docs/web/HEADED-TOOLS.md"); const GOLDEN = path.join(REPO, "tests/golden"); const TOPICS = ["GPU-PROBE", "HEADED-SMOKE", "GPU-SOAK"]; const failures = []; const check = (label, run) => { try { run(); } catch (error) { failures.push(`${label}: ${error.message}`); } }; const fail = (message) => { throw new Error(message); }; const walk = (dir) => fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { const full = path.join(dir, entry.name); return entry.isDirectory() ? walk(full) : [full]; }); const tools = walk(HEADED) .filter((file) => /\.mjs$/.test(file) && !file.includes(`${path.sep}lib${path.sep}`)) .map((file) => path.relative(HEADED, file).split(path.sep).join("/")) .sort(); const libs = walk(path.join(HEADED, "lib")).map((file) => path.basename(file)).sort(); const fixtures = walk(path.join(HEADED, "fixtures")).map((file) => path.relative(HEADED, file).split(path.sep).join("/")).sort(); const doc = fs.readFileSync(DOC, "utf8"); check("doc exists and names every tool", () => { const missing = tools.filter((tool) => !doc.includes(`\`${tool}\``)); if (missing.length) fail(`not in HEADED-TOOLS.md: ${missing.join(", ")}`); }); check("doc lists every lib and fixture", () => { const missing = [...libs.map((lib) => `lib/${lib}`), ...fixtures].filter((item) => !doc.includes(`\`${item}\``)); if (missing.length) fail(`not in HEADED-TOOLS.md: ${missing.join(", ")}`); }); check("doc has no stale tool rows", () => { const rows = [...doc.matchAll(/\| `((?:probe|smoke|soak)\/[^`]+\.mjs)`/g)].map((match) => match[1]); const stale = rows.filter((row) => !tools.includes(row)); if (stale.length) fail(`listed but absent: ${stale.join(", ")}`); }); check("doc states the shared exit codes", () => { for (const code of ["`0`", "`1`", "`2`", "`64`"]) if (!doc.includes(code)) fail(`exit code ${code} not documented`); }); const modules = ["browser.mjs", "crash-watch.mjs", "receipt.mjs", "preflight.mjs", "liveness.mjs", "serve.mjs"]; check("shared lib modules present", () => { const missing = modules.filter((module) => !fs.existsSync(path.join(HEADED, "lib", module))); if (missing.length) fail(`missing lib: ${missing.join(", ")}`); }); for (const tool of tools) { const file = path.join(HEADED, tool); check(`${tool} --help`, () => { const run = spawnSync(process.execPath, [file, "--help"], { encoding: "utf8", timeout: 20_000 }); if (run.status !== 0) fail(`exit ${run.status}: ${run.stderr.trim().slice(0, 160)}`); if (!/usage:/i.test(run.stdout)) fail("no usage line on stdout"); }); check(`${tool} rejects unknown option`, () => { const run = spawnSync(process.execPath, [file, "--definitely-not-an-option"], { encoding: "utf8", timeout: 20_000 }); if (run.status !== 64) fail(`exit ${run.status}, expected 64`); }); check(`${tool} normalizes argv`, () => { const source = fs.readFileSync(file, "utf8"); if (!/import\s*{[^}]*normalizeArgs[^}]*}\s*from/.test(source)) fail("does not import normalizeArgs from lib/browser.mjs"); if (!/normalizeArgs\(process\.argv\.slice\(2\)\)/.test(source)) fail("does not run process.argv through normalizeArgs()"); }); } const toolNames = new Set(tools.map((tool) => path.basename(tool, ".mjs"))); const receipts = fs.existsSync(GOLDEN) ? fs.readdirSync(GOLDEN) .filter((name) => TOPICS.some((topic) => name.startsWith(`${topic}-`))) .map((name) => path.join(GOLDEN, name, "report.json")) .filter((file) => fs.existsSync(file)) : []; check(`receipts parse and follow the schema (${receipts.length} found)`, () => { for (const file of receipts) { const report = JSON.parse(fs.readFileSync(file, "utf8")); const where = path.relative(REPO, file); if (report.schemaVersion !== 1) fail(`${where}: schemaVersion ${report.schemaVersion}`); if (report.acceptance !== "NOT_CLAIMED") fail(`${where}: acceptance ${report.acceptance}`); if (typeof report.url !== "string" || !/^https?:/.test(report.url)) fail(`${where}: bad url`); if (!Array.isArray(report.flags) && !(Array.isArray(report.combos) && report.combos.every((combo) => Array.isArray(combo.flags)))) { fail(`${where}: neither flags nor per-combo flags is a list`); } if (!toolNames.has(report.tool)) fail(`${where}: tool "${report.tool}" has no file under tools/web/headed/`); } }); if (failures.length) { console.log(JSON.stringify({ status: "FAIL", tools: tools.length, receipts: receipts.length, failures }, null, 2)); process.exit(1); } console.log(JSON.stringify({ status: "PASS", tools: tools.length, libs: libs.length, fixtures: fixtures.length, receipts: receipts.length }, null, 2));