#!/usr/bin/env node // golden-baseline.mjs — regenerate the v8 Phase 5 golden baseline artifacts. // // node scripts/golden-baseline.mjs # print a summary, write nothing // node scripts/golden-baseline.mjs --write # (re-)bless the artifacts // // Re-blessing is a deliberate act. During Phase 5, a diff here means the // table swap under test changed observable behaviour — the correct response // is to roll the swap back, not to re-run this with --write. import { mkdirSync, writeFileSync, existsSync, readFileSync, readdirSync } from 'node:fs'; import { resolve, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { buildPatternDump, buildReferenceRun, buildSuiteCounts, serialize, } from './lib/golden-dump.mjs'; const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const GOLDEN_DIR = resolve(ROOT, 'tests/golden'); const write = process.argv.includes('--write'); // Opt-in: one node process per test file, sequentially. Minutes, not seconds. const withSuite = process.argv.includes('--suite'); const artifacts = [ ['patterns.json', await buildPatternDump(ROOT)], ['reference-run.json', await buildReferenceRun(ROOT)], ]; /** `fs.globSync` is Node 22+; package.json promises >=18, so walk by hand. */ function findTestFiles(dir, acc = []) { for (const e of readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name) )) { const full = resolve(dir, e.name); if (e.isDirectory()) findTestFiles(full, acc); else if (e.name.endsWith('.test.mjs')) acc.push(full); } return acc; } if (withSuite) { artifacts.push([ 'suite-counts.json', buildSuiteCounts(ROOT, findTestFiles(resolve(ROOT, 'tests'))), ]); } mkdirSync(GOLDEN_DIR, { recursive: true }); let drift = false; for (const [name, data] of artifacts) { const path = resolve(GOLDEN_DIR, name); const next = serialize(data); const prev = existsSync(path) ? readFileSync(path, 'utf8') : null; const status = prev === null ? 'new' : prev === next ? 'unchanged' : 'CHANGED'; if (status === 'CHANGED') drift = true; if (write) writeFileSync(path, next); console.log(`${write ? 'wrote' : 'checked'} tests/golden/${name} [${status}]`); } const [, dump] = artifacts[0]; const [, run] = artifacts[1]; console.log( `\n regex records : ${dump.counts.regex}` + `\n table records : ${dump.counts.table}` + `\n file digests : ${dump.counts.file}` + `\n reference run : ${run.summary.total} cases, ` + `${run.summary.matchingExpectation} match expectation` + `\n reachability : ${run.coverage.patternsReachable}/${run.coverage.patternsTotal} patterns` + ` (STATIC probe, not observed coverage — see tests/golden/README.md)` ); const suite = artifacts.find(([n]) => n === 'suite-counts.json'); if (suite) { const [, s] = suite; console.log( ` suite : ${s.totals.pass} pass / ${s.totals.fail} fail across ${s.totals.files} files` ); } if (!write && drift) { console.error('\nBaseline drift detected. Investigate before re-blessing with --write.'); process.exit(1); }