#!/usr/bin/env node /** * Validate a headed viewport-navigation receipt. * * node tools/web/check-viewport-navigation-headed.mjs [--dir ] [--report ] [--allow-headless] * * Without arguments the newest tests/golden/VIEWPORT-NAV-HEADED-* directory that * contains a report.json is validated. Prints {status, cases, artifacts} on PASS, * {status:"FAIL", errors} on stderr and exits 1 otherwise. */ import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const errors = []; const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); function parseArgs(argv) { const take = (name) => { const index = argv.indexOf(`--${name}`); return index >= 0 && argv[index + 1] ? argv[index + 1] : null; }; return { dir: take("dir"), report: take("report"), allowHeadless: argv.includes("--allow-headless") }; } function newestReportDir(root) { if (!fs.existsSync(root)) return null; return fs.readdirSync(root) .filter((name) => name.startsWith("VIEWPORT-NAV-HEADED-")) .map((name) => path.join(root, name)) .filter((dir) => fs.existsSync(path.join(dir, "report.json"))) .sort((a, b) => fs.statSync(path.join(b, "report.json")).mtimeMs - fs.statSync(path.join(a, "report.json")).mtimeMs)[0] ?? null; } function pngSize(file) { const bytes = fs.readFileSync(file); if (bytes.length < 24 || bytes.toString("latin1", 1, 4) !== "PNG") return null; return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20) }; } const args = parseArgs(process.argv.slice(2)); const reportDir = args.report ? path.dirname(path.resolve(args.report)) : args.dir ? path.resolve(args.dir) : newestReportDir(path.join(REPO_ROOT, "tests/golden")); const reportFile = args.report ? path.resolve(args.report) : reportDir ? path.join(reportDir, "report.json") : null; if (!reportFile || !fs.existsSync(reportFile)) { console.error(`no headed receipt found${reportDir ? `: ${reportFile ?? reportDir}` : " under tests/golden/VIEWPORT-NAV-HEADED-*"}`); process.exit(1); } let report; try { report = JSON.parse(fs.readFileSync(reportFile, "utf8")); } catch (error) { console.error(`report.json is not valid JSON: ${error.message}`); process.exit(1); } if (report.schemaVersion !== 1) errors.push(`schemaVersion is ${report.schemaVersion}, expected 1`); if (report.status !== "PASS") errors.push(`status is ${report.status}, expected PASS`); if (report.acceptance !== "NOT_CLAIMED") errors.push(`acceptance is ${report.acceptance}, expected NOT_CLAIMED`); if (!Array.isArray(report.errors) || report.errors.length > 0) errors.push(`errors is not empty: ${JSON.stringify(report.errors)}`); const environment = report.environment ?? {}; if (environment.browser !== "chromium-headed" && !args.allowHeadless) errors.push(`environment.browser is ${environment.browser}, expected chromium-headed`); if (environment.gpuMode !== "hardware" && !args.allowHeadless) errors.push(`environment.gpuMode is ${environment.gpuMode}, expected hardware`); if (!environment.display) errors.push("environment.display is empty (headed runs need X11)"); if (typeof environment.chrome !== "string" || environment.chrome.length === 0) errors.push("environment.chrome is missing"); const candidate = report.candidate; if (typeof candidate !== "string" || !path.isAbsolute(candidate)) errors.push(`candidate is not an absolute path: ${candidate}`); else { if (!fs.existsSync(candidate)) errors.push(`candidate directory is missing: ${candidate}`); const checks = [ ["index.html", "indexSha256"], ["wasm-module-manifest.json", "moduleManifestSha256"], ["vendor/blender/web_engine.wasm", "wasmSha256"], ]; for (const [relative, key] of checks) { const file = path.join(candidate, relative); if (!fs.existsSync(file)) { errors.push(`candidate is missing ${relative}`); continue; } const actual = sha256(file); const expected = report.release?.[key]; if (actual !== expected) errors.push(`${relative} sha256 ${actual} does not match receipt ${expected}`); } if (!fs.existsSync(path.join(candidate, "teach/fixtures/monkey-head.glb"))) errors.push("candidate is missing teach/fixtures/monkey-head.glb"); } const summary = report.summary ?? {}; if (summary.fail !== 0) errors.push(`summary.fail is ${summary.fail}`); if (summary.blocked !== 0) errors.push(`summary.blocked is ${summary.blocked}`); if (!Number.isSafeInteger(summary.total) || summary.total < 1) errors.push(`summary.total is ${summary.total}`); const cases = Array.isArray(report.cases) ? report.cases : []; if (cases.length !== summary.total) errors.push(`cases length ${cases.length} does not match summary.total ${summary.total}`); for (const id of summary.expectedCaseIds ?? []) { const found = cases.find((item) => item.caseId === id); if (!found) errors.push(`expected case ${id} is missing`); else if (found.status !== "PASS") errors.push(`expected case ${id} is ${found.status}`); } if ((summary.missingCaseIds ?? []).length > 0) errors.push(`missingCaseIds: ${summary.missingCaseIds.join(", ")}`); let artifactCount = 0; for (const item of cases) { for (const artifact of item.artifacts ?? []) { artifactCount++; const file = path.resolve(reportDir, artifact.path ?? ""); if (!file.startsWith(reportDir + path.sep)) { errors.push(`artifact escapes the receipt directory: ${artifact.path}`); continue; } if (!fs.existsSync(file)) { errors.push(`artifact is missing: ${artifact.path}`); continue; } const bytes = fs.readFileSync(file); if (crypto.createHash("sha256").update(bytes).digest("hex") !== artifact.sha256) errors.push(`artifact sha256 mismatch: ${artifact.path}`); if (artifact.kind === "png") { const size = pngSize(file); if (!size || size.width <= 0 || size.height <= 0) errors.push(`artifact is not a decodable PNG: ${artifact.path}`); } else if (artifact.kind === "json") { try { JSON.parse(bytes.toString("utf8")); } catch { errors.push(`artifact is not valid JSON: ${artifact.path}`); } } } } if (errors.length > 0) { console.error(JSON.stringify({ status: "FAIL", errors }, null, 2)); process.exit(1); } console.log(JSON.stringify({ status: "PASS", report: reportFile, cases: cases.length, artifacts: artifactCount, candidate, wasmSha256: report.release?.wasmSha256 }, null, 2));