CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

adobe/stardust

Redesign an existing website to make it better. Built on top of impeccable.

Quality

87%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide
SecuritybySnyk

Low

Low-risk findings worth noting

Overview
Quality
Evals
Security
Files

gate.test.mjsskills/replica/scripts/test/

#!/usr/bin/env node
// gate.sh contract test: argument parsing, --help, the horizontal-overflow assert (measure.mjs root line → exit 2
// over a PASS pixel line), the one capture retry on exit 1 (live, build, overflow probe; 3 / 4 / 124 final — 3 is the
// BLOCKED line: a bot challenge or 2xx denial page, no verdict, on either side), the --full probe fan-out and its
// exit-code rules, the overlay flags (--consent / --dismiss / --no-dismiss reaching both captures, the diff probes and
// chrome-parity), the font check per side (one `gate.sh: fonts on the <side> capture` line each after the overflow
// line from the capture records — the dead faces, every face loaded, or NOT CHECKED for a record whose fontsFailed is
// null or no readable record; information, the exit code unchanged; a failed live capture removes its record with
// live.png), with stub instruments (no browser, no network).
// The origin reference (the default origin): the first round freezes one (reference.mjs freeze, the round's overlay
// flags and locale) and gates against it, the next reuses it with no live capture; an unstable reference ends the
// round with exit 5 — no verdict file, no round recorded; a bot challenge is 3, a deadline 124; --live-origin and
// GATE_REFERENCE=live are the round as it was; the reference's mask reaches pixel-compare merged with --mask. The
// pixel criterion (pixel-compare --offset-max, the regime's bound or GATE_OFFSET_MAX_PX): aligned pass within the
// bound → 0, offsets over it → 2 with the offsets reason, alignment not used → the unaligned rule; verdict-<label>.json
// carries the round's pixel numbers. The sections written for the live-origin round run it through GATE_REFERENCE=live
// (the `gate` helper's default here); the reference sections pass DEF.
// Run: node skills/replica/scripts/test/gate.test.mjs
import { spawn } from 'node:child_process';
import { chmodSync, cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, unlinkSync, writeFileSync } from 'node:fs';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

const here = dirname(fileURLToPath(import.meta.url));
const scripts = join(here, '..');
let failures = 0;
function check(name, cond, extra = '') {
  console.log(`${cond ? 'ok  ' : 'FAIL'} ${name}${cond || !extra ? '' : ` — ${extra}`}`);
  if (!cond) failures++;
}

// Sandbox: stardust/scripts/{replica,diff} with the real gate.sh + run-capped and stub instruments.
// realpath: the scripts' main-module guards compare process.argv[1] with import.meta.url, and a symlinked temp dir
// (macOS /var → /private/var) would make every instrument a silent no-op.
const root = realpathSync(mkdtempSync(join(tmpdir(), 'gate-test-')));
const replica = join(root, 'stardust/scripts/replica');
const diff = join(root, 'stardust/scripts/diff');
mkdirSync(replica, { recursive: true });
mkdirSync(diff, { recursive: true });
cpSync(join(scripts, 'gate.sh'), join(replica, 'gate.sh'));
chmodSync(join(replica, 'gate.sh'), 0o755);
cpSync(join(scripts, 'run-capped.mjs'), join(replica, 'run-capped.mjs'));

// Stubs read their behaviour from environment variables so one sandbox covers every case. stitch-shot: per side
// (LIVE = the https://live.example URL, BUILD = anything else) an exit code (STUB_STITCH_RC_<side>) or a fail-ONCE
// marker file (STUB_FAIL_ONCE_<side>: exit 1 while the marker is absent, create it); every call appends its side to
// STUB_STITCH_COUNT so a test can count attempts, and its side + argv to STUB_STITCH_ARGS so a test can read the flags. A
// successful capture writes its capture record <stem>.capture.json (fixedLayers always — a capture the new way, reusable;
// fontsFailed = the comma list STUB_FONTS_<side>, or
// null + fontCheck "failed: …" under STUB_FONTS_NULL_<side> — a font check that could not run; STUB_NO_JSON_<side> writes
// none, STUB_BAD_JSON_<side> an unreadable one). measure: the root line gate.sh reads (STUB_OVERFLOW px over the
// viewport, STUB_MEASURE_RC exit code, STUB_MEASURE_NO_ROOT drops the line).
const stub = (dir, name, body) => writeFileSync(join(dir, name), `#!/usr/bin/env node\n${body}\n`);
// The stub captures; the browserless bookkeeping subcommands (\`baseline\`, \`record\`) are the real stitch-shot's, so
// gate.sh's baseline check and live.json go through the plugin's own reader and writer. STUB_RECORD_FAIL makes
// \`record\` exit 1 (a failed write).
const REAL_STITCH = join(scripts, 'stitch-shot.mjs');
stub(replica, 'stitch-shot.mjs', `
  import { appendFileSync, existsSync, writeFileSync } from 'node:fs';
  if (process.argv[2] === 'record' && process.env.STUB_RECORD_FAIL) { console.error('stitch-shot record: stub failure (STUB_RECORD_FAIL)'); process.exit(1); }
  if (process.argv[2] === 'record' || process.argv[2] === 'baseline') {
    const { spawnSync } = await import('node:child_process');
    const r = spawnSync(process.execPath, [${JSON.stringify(REAL_STITCH)}, ...process.argv.slice(2)], { stdio: 'inherit' });
    process.exit(r.status ?? 1);
  }
  const url = process.argv[2]; const out = process.argv[3];
  const side = /^https:\\/\\/live\\.example/.test(url) ? 'LIVE' : 'BUILD';
  if (process.env.STUB_STITCH_COUNT) appendFileSync(process.env.STUB_STITCH_COUNT, side + '\\n');
  if (process.env.STUB_STITCH_ARGS) appendFileSync(process.env.STUB_STITCH_ARGS, side + ' ' + process.argv.slice(2).join(' ') + '\\n');
  const once = process.env['STUB_FAIL_ONCE_' + side];
  if (once && !existsSync(once)) { writeFileSync(once, '1'); console.error('stitch-shot error: Target page, context or browser has been closed'); process.exit(1); }
  const rc = Number(process.env['STUB_STITCH_RC_' + side] || 0);
  if (rc) { console.error('stitch-shot error: stub exit ' + rc); process.exit(rc); }
  writeFileSync(out, 'png');
  const json = out.replace(/\\.png$/i, '') + '.capture.json';
  const fonts = process.env['STUB_FONTS_NULL_' + side] ? { fontsFailed: null, fontCheck: 'failed: Execution context was destroyed' }
    : { fontsFailed: (process.env['STUB_FONTS_' + side] || '').split(',').filter(Boolean) };
  if (json && process.env['STUB_BAD_JSON_' + side]) writeFileSync(json, '{ "url": ');
  else if (json && !process.env['STUB_NO_JSON_' + side]) writeFileSync(json, JSON.stringify({ url, out, width: 1440, vh: 900, settle: process.argv.includes('--settle'), ...fonts, fixedLayers: { capture: 'once', count: 0, late: 0 }, height: 2000 }, null, 2) + '\\n');
  console.log('stitched ' + out);`);
stub(replica, 'measure.mjs', `
  import { appendFileSync } from 'node:fs';
  if (process.env.STUB_MEASURE_COUNT) appendFileSync(process.env.STUB_MEASURE_COUNT, 'M\\n');
  const rc = Number(process.env.STUB_MEASURE_RC || 0);
  if (rc) { console.error('measure: ' + process.argv[2] + ' failed to load — stub exit ' + rc); process.exit(rc); }
  const w = Number(process.argv[process.argv.indexOf('--width') + 1]); const o = Number(process.env.STUB_OVERFLOW || 0);
  console.log('measure  ' + process.argv[2]); console.log('  widths ' + w + ' · 1 selector(s) · 1 props · first match'); console.log(''); console.log('@ ' + w + 'px');
  if (!process.env.STUB_MEASURE_NO_ROOT) console.log('  root  scrollWidth ' + String(w + o).padStart(5) + '  viewport ' + w + '  scrollHeight   5410' + (o ? '  ◄◄ OVERFLOW +' + o + 'px' : ''));
  console.log('  html'); console.log('       x     0  y      0  w  ' + w + '  h  5410  vis  ""'); console.log('      display: block');`);
// pixel-compare: the two legacy lines, then — through the REAL pixelVerdict / alignedLine (the rule's one home) — the
// aligned line and the \`pixel:\` line for the summary STUB_PIXEL_JSON describes ({ pct, aligned: { used, pct, steps } };
// default 1.2 %, alignment not used) under the --offset-max gate.sh passed; --json-out gets that summary (maskedRows =
// the rows of --mask). Exit: STUB_PIXEL_RC when set (no summary written for a non-verdict code), else the rule's.
// STUB_PIXEL_ARGS records the argv.
const REAL_PIXEL = join(scripts, 'pixel-compare.mjs');
stub(replica, 'pixel-compare.mjs', `
  import { appendFileSync, writeFileSync } from 'node:fs';
  import { alignedLine, pixelVerdict } from ${JSON.stringify(REAL_PIXEL)};
  const a = process.argv.slice(2); const arg = (f) => (a.includes(f) ? a[a.indexOf(f) + 1] : null);
  if (process.env.STUB_PIXEL_ARGS) appendFileSync(process.env.STUB_PIXEL_ARGS, a.join(' ') + '\\n');
  console.log('height delta 0px'); console.log('differing 1.20%');
  const forced = process.env.STUB_PIXEL_RC ? Number(process.env.STUB_PIXEL_RC) : null;
  if (forced !== null && forced !== 0 && forced !== 2) process.exit(forced);
  const given = JSON.parse(process.env.STUB_PIXEL_JSON || '{}');
  const pct = given.pct ?? 1.2; const al = given.aligned || { used: false, pct, steps: [], reason: 'no offset step found' };
  const aligned = { used: al.used, pct: al.pct ?? pct, steps: al.steps || [], totalShift: (al.steps || []).reduce((n, s) => n + Math.abs(s.shift), 0), maxOffset: 0, insertedRows: 0, deletedRows: 0, bands: [], reason: al.reason || null };
  const maskedRows = (arg('--mask') || '').split(',').filter(Boolean).reduce((n, m) => n + Number(m.split(':')[1].split('@')[0]), 0);
  const j = { pct, heightDelta: given.heightDelta ?? 0, compared: { width: 1440, height: given.height ?? 2000, mode: 'overlap' }, threshold: 10, maskedRows, bands: [], verdict: 'aligned', aligned };
  const om = arg('--offset-max'); const v = pixelVerdict(j, { threshold: 10, offsetMax: om === 'prototype' || om === 'published' ? om : om === null ? null : Number(om) });
  console.log(alignedLine(aligned, pct)); console.log(v.line);
  const rc = forced ?? (v.pass ? 0 : 2);
  if (arg('--json-out')) writeFileSync(arg('--json-out'), JSON.stringify({ ...j, pass: rc === 0, offsets: { bound: v.bound, totalShift: v.totalShift, pass: v.offsetsPass }, reason: v.reason }, null, 2) + '\\n');
  process.exit(rc);`);
// reference.mjs: \`freeze\` is the stub — it appends REF to STUB_STITCH_COUNT and its argv to STUB_REF_ARGS, then exits
// STUB_REF_RC with reference.mjs's line for that code (2 = unstable: reference-unstable.json, nothing frozen; 3 = bot
// challenge), fails ONCE with 1 under STUB_REF_FAIL_ONCE (a marker file), sleeps STUB_REF_SLEEP ms first, or freezes:
// baseline.png + baseline.json in reference.mjs's shape — noiseFloor STUB_REF_FLOOR, the variable regions and maskArg
// of STUB_REF_MASK ("y:h,…"). \`mask\` / \`status\` are the real script's (no browser).
const REAL_REFERENCE = join(scripts, 'reference.mjs');
stub(replica, 'reference.mjs', `
  import { appendFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
  import { join } from 'node:path';
  const [cmd, ...rest] = process.argv.slice(2);
  if (cmd !== 'freeze') {
    const { spawnSync } = await import('node:child_process');
    const r = spawnSync(process.execPath, [${JSON.stringify(REAL_REFERENCE)}, cmd, ...rest], { stdio: 'inherit' });
    process.exit(r.status ?? 1);
  }
  const [url, dir] = rest; const width = Number(rest[rest.indexOf('--width') + 1]);
  if (process.env.STUB_STITCH_COUNT) appendFileSync(process.env.STUB_STITCH_COUNT, 'REF\\n');
  if (process.env.STUB_REF_ARGS) appendFileSync(process.env.STUB_REF_ARGS, rest.join(' ') + '\\n');
  if (process.env.STUB_REF_SLEEP) await new Promise((r) => setTimeout(r, Number(process.env.STUB_REF_SLEEP)));
  mkdirSync(dir, { recursive: true });
  const once = process.env.STUB_REF_FAIL_ONCE;
  if (once && !existsSync(once)) { writeFileSync(once, '1'); console.log('reference: not frozen — the priming capture failed (stitch-shot exit 1)'); process.exit(1); }
  const rc = Number(process.env.STUB_REF_RC || 0);
  if (rc === 2) { writeFileSync(join(dir, 'reference-unstable.json'), '{ "stable": false }'); console.log('reference: UNSTABLE — heights 2000 vs 2640 after priming; not frozen'); process.exit(2); }
  if (rc === 3) { console.log('reference: not frozen — the priming capture met a bot challenge (stitch-shot exit 3)'); process.exit(3); }
  if (rc) { console.log('reference: not frozen — the capture failed (stitch-shot exit ' + rc + ')'); process.exit(rc); }
  const maskArg = process.env.STUB_REF_MASK || ''; const variable = maskArg.split(',').filter(Boolean).map((m) => ({ y: Number(m.split(':')[0]), h: Number(m.split(':')[1]), pct: 12 }));
  const noiseFloor = Number(process.env.STUB_REF_FLOOR || 0);
  writeFileSync(join(dir, 'baseline.png'), 'frozen-reference-bytes');
  writeFileSync(join(dir, 'baseline.json'), JSON.stringify({ writtenBy: 'skills/replica/scripts/reference.mjs freeze', schema: 1, capturedAt: '2026-11-01T09:00:00.000Z', url, width, vh: 900, height: 2000, settle: true, chunks: 3,
    fontsFailed: [], fixedLayers: { capture: 'once', count: 0 }, primed: true, prime: { height: 2100, pct: 31.4, heightDelta: 100 }, captures: 2, heights: [2000, 2000], noiseFloor, heightSpread: 0, outside: 0,
    variable, variableShare: 0, maskArg, stable: true, reason: null, frozenAt: '2026-11-01T09:00:05.000Z' }, null, 2) + '\\n');
  console.log('reference: frozen ' + width + '×2000, primed, noise floor ' + noiseFloor.toFixed(2) + '%' + (maskArg ? ' in ' + variable.length + ' variable region(s) → mask ' + maskArg : ', no variable region'));`);
stub(replica, 'chrome-parity.mjs', `
  console.log('argv ' + process.argv.slice(2).join(' '));
  if (process.env.STUB_SLEEP) await new Promise((r) => setTimeout(r, Number(process.env.STUB_SLEEP)));
  const deltas = Number(process.env.STUB_CHROME_DELTAS || 0);
  console.log('chrome-parity @ 1440px'); console.log(deltas ? '✗ ' + deltas + ' delta(s) — fix these' : '✓ chrome parity');
  process.exit(deltas ? 2 : 0);`);
stub(diff, 'content-diff.mjs', `
  if (process.env.STUB_CD_RC) { console.error('content-diff error: build side answered HTTP 502'); process.exit(Number(process.env.STUB_CD_RC)); }
  const s = Number(process.env.STUB_STRUCTURAL || 0); const l = Number(process.env.STUB_LOST || 0);
  console.log('  live: 12 headings'); console.log('Findings: ' + (s || l ? (s + l + 2) + ' (' + s + ' structural 🔴)' : 'none — content + roles match'));
  console.log('Tiers: looks-same 🔴 ' + s + ' · nothing-lost 🟡 ' + l + ' · accepted 🟢 0 (owner 0, auto 0) · advisory ' + (s || l ? 2 : 0));`);
stub(diff, 'clip-probe.mjs', `
  console.log('argv ' + process.argv.slice(2).join(' '));
  const n = Number(process.env.STUB_CLIPPED || 0);
  console.log('Clipped: ' + n + ' (text clipped ' + n + ', text hidden 0, controls hidden 0, controls clipped 0; advisory: clamped 0)');
  process.exit(n ? 2 : 0);`);
stub(diff, 'content-presence.mjs', `
  console.log('argv ' + process.argv.slice(2).join(' '));
  const h = Number(process.env.STUB_HIDDEN || 0);
  console.log('Content: MISSING 0 (headings 0, links 0) / HIDDEN ' + h + ' (headings 0, links ' + h + ') / control-state 0');
  process.exit(h ? 2 : 0);`);
stub(diff, 'unit-geometry.mjs', `
  console.log('argv ' + process.argv.slice(2).join(' '));
  if (process.env.STUB_UNITS_RC === '2') { console.log('Units: 1 compared; elements within 2, off 3, hidden 1, missing 0 → FAIL'); process.exit(2); }
  console.log('unit-geometry: none declared for home in stardust/replica/units.json → n/a');`);
stub(diff, 'visual-diff.mjs', `
  console.log('argv ' + process.argv.slice(2).join(' ')); console.log('Visual diff @ 1440px'); console.log('build red flags (advisory): ');
  console.log('  • HEADING COLOR: h2 differs'); console.log('  • IMAGE DIMS: hero'); console.log('Full metrics JSON:'); console.log('{}');`);

// A build origin that serves the slug (the identity assertion needs it).
const server = createServer((_req, res) => res.end('<html><body>home-proposed</body></html>'));
await new Promise((r) => server.listen(0, '127.0.0.1', r));
const buildUrl = `http://127.0.0.1:${server.address().port}/home-proposed.html`;

// Asynchronous: the build origin above lives in this process, so the event loop must keep running while gate.sh
// fetches it (a synchronous spawn would block the server and read as "did not respond").
function gate(args, env = {}) {
  return new Promise((resolve) => {
    const child = spawn('bash', [join(replica, 'gate.sh'), ...args], {
      cwd: root,
      env: { ...process.env, GATE_REAP_MIN: '0', GATE_REFERENCE: 'live', STARDUST_ROUND_CAP: '0', ...env }, // the cap is rounds.test.mjs's subject: here rounds are recorded, never refused
    });
    let stdout = '';
    let stderr = '';
    child.stdout.on('data', (d) => (stdout += d));
    child.stderr.on('data', (d) => (stderr += d));
    child.on('close', (status) => resolve({ status, stdout, stderr }));
  });
}

// The default origin (the frozen reference): pass DEF as the env — the helper's own default is the live-origin round.
const DEF = { GATE_REFERENCE: '' };

// --help
let r = await gate(['--help']);
check('--help exits 0', r.status === 0, `status ${r.status}`);
check('--help prints the usage block', /Usage:/.test(r.stdout) && /--full/.test(r.stdout));
check('--help names the overflow assert and the capture retry', /Horizontal-overflow assert/.test(r.stdout) && /Capture retry/.test(r.stdout) && /overflow-<label>\.txt/.test(r.stdout));
check('--help documents the overlay flags', /Overlay dismissal/.test(r.stdout) && /--consent <sel>/.test(r.stdout) && /--dismiss <sel,…>/.test(r.stdout) && /--no-dismiss/.test(r.stdout), r.stdout.slice(0, 400));

// Plain pixel round (no --full): verdict lines, evidence, exit = pixel rc.
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter1']);
check('pixel round exits 0 on pass', r.status === 0, r.stderr.slice(0, 200));
check('pixel round prints the verdict lines', /height delta/.test(r.stdout) && /differing/.test(r.stdout));
check('live capture is written once and reused', existsSync(join(root, 'stardust/replica/gates/home-1440/live.png')));
check('no probe evidence without --full', !existsSync(join(root, 'stardust/replica/gates/home-1440/content-diff-iter1.txt')));
check('a clean root prints the overflow assert as ok, after the pixel lines', /differing[^\n]*\n[^]*^gate\.sh: overflow assert at 1440 — build scrollWidth 1440 = viewport → ok$/m.test(r.stdout), r.stdout);
check('the overflow probe leaves its evidence file', /scrollWidth/.test(readFileSync(join(root, 'stardust/replica/gates/home-1440/overflow-iter1.txt'), 'utf8')));

// Horizontal-overflow assert: the build document is wider than its viewport → FAIL (exit 2) over a PASS pixel line.
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'ovf1'], { STUB_OVERFLOW: '13' });
check('a build scrollWidth over the viewport fails the round (exit 2) although pixels passed', r.status === 2, `status ${r.status} ${r.stderr.slice(0, 300)}`);
check('the overflow verdict line names width, scrollWidth, viewport and the px over', /^gate\.sh: OVERFLOW at 1440 — build scrollWidth 1453 > viewport 1440 \(\+13px\) → FAIL \(hard assert: no iteration cap waives horizontal overflow; evidence stardust\/replica\/gates\/home-1440\/overflow-ovf1\.txt\)$/m.test(r.stdout), r.stdout);
check('the pixel verdict lines are still printed before it', /differing 1\.20%[^]*gate\.sh: OVERFLOW/.test(r.stdout));
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'ovf2', '--full'], { STUB_OVERFLOW: '40' });
check('under --full an overflow is a verdict (2): the probes still run and the round exits 2', r.status === 2 && /^content-diff: none/m.test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'ovf3'], { STUB_OVERFLOW: '5', STUB_PIXEL_RC: '124' });
check('an overflow never manufactures a verdict from a pixel deadline: exit stays 124, the line is still printed', r.status === 124 && /^gate\.sh: OVERFLOW at 1440 — build scrollWidth 1445/m.test(r.stdout), `status ${r.status}`);
// Tolerance: integer rounding of a subpixel width is not an overflow (default 4 px, GATE_OVERFLOW_TOLERANCE).
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'tol1'], { STUB_OVERFLOW: '3' });
check('+3px is inside the default 4px tolerance: exit 0 and the ok line names the px and the tolerance', r.status === 0 && /^gate\.sh: overflow assert at 1440 — build scrollWidth 1443 vs viewport 1440 \(\+3px, within the 4px rounding tolerance\) → ok$/m.test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'tol2'], { STUB_OVERFLOW: '4' });
check('+4px (the boundary) is still ok', r.status === 0 && /within the 4px rounding tolerance\) → ok$/m.test(r.stdout), `status ${r.status}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'tol3'], { STUB_OVERFLOW: '5' });
check('+5px fails the round (exit 2)', r.status === 2 && /^gate\.sh: OVERFLOW at 1440 — build scrollWidth 1445 > viewport 1440 \(\+5px\) → FAIL/m.test(r.stdout), `status ${r.status}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'tol4'], { STUB_OVERFLOW: '1', GATE_OVERFLOW_TOLERANCE: '0' });
check('GATE_OVERFLOW_TOLERANCE=0 makes the assert exact: +1px fails', r.status === 2 && /\(\+1px\) → FAIL/.test(r.stdout), `status ${r.status}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'tol5'], { STUB_OVERFLOW: '0', GATE_OVERFLOW_TOLERANCE: 'four' });
check('a non-numeric GATE_OVERFLOW_TOLERANCE is a usage error (exit 125) before any capture', r.status === 125 && /whole number of px/.test(r.stderr), `status ${r.status} ${r.stderr}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'ovf4'], { STUB_MEASURE_NO_ROOT: '1' });
check('an overflow probe without a root line is exit 1 with the fix named (never a silent pass)', r.status === 1 && /printed no root line/.test(r.stderr), `status ${r.status} ${r.stderr}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'ovf5'], { STUB_MEASURE_RC: '124' });
check('an overflow probe deadline is exit 124, no verdict', r.status === 124 && /overflow probe hit its deadline/.test(r.stderr), `status ${r.status} ${r.stderr}`);

// Capture retry: exit 1 is retried once — on the build capture, the live capture and the overflow probe; 3 / 124 are final.
const countFile = join(root, 'stitch-count.txt');
const attempts = () => (existsSync(countFile) ? readFileSync(countFile, 'utf8').trim().split('\n').filter(Boolean) : []);
const resetCount = () => { if (existsSync(countFile)) unlinkSync(countFile); };
resetCount();
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'retry1'], { STUB_FAIL_ONCE_BUILD: join(root, 'once-build'), STUB_STITCH_COUNT: countFile });
check('a build capture that exits 1 once is retried and the round passes', r.status === 0, `status ${r.status} ${r.stderr.slice(0, 300)}`);
check('the retry is said on stderr', /^gate\.sh: build capture exited 1 — retrying once \(a capture error under load is not a verdict\)$/m.test(r.stderr), r.stderr);
check('exactly two build attempts, live cached (no live attempt)', attempts().join(',') === 'BUILD,BUILD', attempts().join(','));
resetCount();
writeFileSync(join(root, 'stardust/replica/gates/home-1440/live.capture.json'), JSON.stringify({ chunks: 6 }));
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'relive'], { STUB_STITCH_COUNT: countFile });
check('a cached live.png captured before fixed layers were captured once is re-taken, said on stdout', r.status === 0 && attempts().join(',') === 'LIVE,BUILD' && /live\.png predates the fixed-layer capture/.test(r.stdout), `status ${r.status} attempts ${attempts().join(',')} ${r.stdout.slice(0, 300)}`);
resetCount();
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'retry2'], { STUB_STITCH_RC_BUILD: '1', STUB_STITCH_COUNT: countFile });
check('two exit-1 build captures end the round with exit 1 and say re-queue, never a FAIL', r.status === 1 && /build capture failed \(exit 1\) — not comparing \(twice: no verdict — re-queue the round\)/.test(r.stderr), `status ${r.status} ${r.stderr}`);
check('no third attempt', attempts().join(',') === 'BUILD,BUILD', attempts().join(','));
check('no overflow line, no pixel verdict after a failed capture', !/overflow assert|OVERFLOW|differing/.test(r.stdout), r.stdout);
resetCount();
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'retry3'], { STUB_STITCH_RC_BUILD: '3', STUB_STITCH_COUNT: countFile });
check('exit 3 (bot challenge) is final on the first attempt — not retried', r.status === 3 && attempts().join(',') === 'BUILD' && !/retrying/.test(r.stderr), `status ${r.status} attempts ${attempts().join(',')}`);
check('the blocked class is named — BLOCKED (exit 3), a bot challenge or 2xx denial page, no verdict, escalate --headed — never "failed"', /^gate\.sh: build capture BLOCKED \(exit 3\) — a bot challenge or 2xx denial page, not the page: no verdict for this round; escalate --headed$/m.test(r.stderr) && !/build capture failed/.test(r.stderr), r.stderr);
resetCount();
r = await gate(['wall', 'https://live.example/wall', buildUrl, '1440', 'retry3b', '--marker', 'home-proposed'], { STUB_STITCH_RC_LIVE: '3', STUB_STITCH_COUNT: countFile });
check('a live capture that exits 3 ends the round with exit 3 on the first attempt: the live BLOCKED line, no build attempt, no live.png left to be reused as the source', r.status === 3 && attempts().join(',') === 'LIVE' && /^gate\.sh: live capture BLOCKED \(exit 3\) — a bot challenge or 2xx denial page, not the page: no verdict for this round; escalate --headed$/m.test(r.stderr) && !/retrying/.test(r.stderr) && !existsSync(join(root, 'stardust/replica/gates/wall-1440/live.png')), `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
resetCount();
r = await gate(['about', 'https://live.example/about', buildUrl, '1440', 'retry4', '--marker', 'home-proposed'], { STUB_FAIL_ONCE_LIVE: join(root, 'once-live'), STUB_STITCH_COUNT: countFile });
check('a live capture that exits 1 once is retried; the round passes and live.png is kept', r.status === 0 && existsSync(join(root, 'stardust/replica/gates/about-1440/live.png')), `status ${r.status} ${r.stderr.slice(0, 300)}`);
check('live attempted twice, then the build once', attempts().join(',') === 'LIVE,LIVE,BUILD', attempts().join(','));
resetCount();
r = await gate(['press', 'https://live.example/press', buildUrl, '1440', 'retry5', '--marker', 'home-proposed'], { STUB_STITCH_RC_LIVE: '1', STUB_STITCH_COUNT: countFile });
check('two exit-1 live captures: exit 1, re-queue named, no partial live.png left to be reused', r.status === 1 && /live capture failed \(exit 1\) — not comparing \(twice: no verdict — re-queue the round\)/.test(r.stderr) && !existsSync(join(root, 'stardust/replica/gates/press-1440/live.png')) && attempts().join(',') === 'LIVE,LIVE', `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
const mCount = join(root, 'measure-count.txt');
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'retry6'], { STUB_MEASURE_RC: '1', STUB_MEASURE_COUNT: mCount });
check('an overflow probe that exits 1 twice is exit 1 (no verdict) after one retry', r.status === 1 && /overflow probe exited 1 — retrying once/.test(r.stderr) && /overflow probe failed \(exit 1\) — no verdict, re-queue the round: measure: /.test(r.stderr) && readFileSync(mCount, 'utf8').trim().split('\n').length === 2, `status ${r.status} ${r.stderr}`);

// Unknown flag
r = await gate(['home', 'https://live.example/', buildUrl, '1440', '--bogus']);
check('unknown flag exits 125', r.status === 125);

// The font check per side (header § Fonts): both captures leave their capture record; one line per side after the
// overflow line — information for the loop, never a verdict.
const fontsLine = (side, rest) => new RegExp(`^gate\\.sh: fonts on the ${side} capture — ${rest}$`, 'm');
const gdir = (slug) => `stardust/replica/gates/${slug}-1440`;
r = await gate(['fonts1', 'https://live.example/fonts1', buildUrl, '1440', 'f1', '--marker', 'home-proposed']);
check('both captures leave their capture record (<side>.capture.json in the evidence dir)', r.status === 0
  && existsSync(join(root, gdir('fonts1'), 'live.capture.json')) && existsSync(join(root, gdir('fonts1'), 'build.capture.json')), `status ${r.status} ${r.stderr.slice(0, 300)}`);
check('both records were written by a --settle capture (a symmetric instrument)', JSON.parse(readFileSync(join(root, gdir('fonts1'), 'live.capture.json'), 'utf8')).settle === true
  && JSON.parse(readFileSync(join(root, gdir('fonts1'), 'build.capture.json'), 'utf8')).settle === true);
check('a clean round prints "every declared face loaded" for both sides, after the overflow line', fontsLine('live', 'every declared face loaded').test(r.stdout) && fontsLine('build', 'every declared face loaded').test(r.stdout)
  && /overflow assert[^\n]*\n[^]*fonts on the live capture[^\n]*\n[^]*fonts on the build capture/.test(r.stdout), r.stdout);
r = await gate(['fonts1', 'https://live.example/fonts1', buildUrl, '1440', 'f2', '--marker', 'home-proposed'], { STUB_FONTS_BUILD: 'Brand Sans,Brand Serif Fallback' });
check('dead faces on the build side are named on its line — DEAD FACE(S) with both causes — and the round still passes (information, not a verdict)', r.status === 0
  && fontsLine('build', 'DEAD FACE\\(S\\) Brand Sans, Brand Serif Fallback: the capture renders fallback type for them \\(a file that failed to fetch, or a local\\(\\) source this browser cannot resolve — recreation-procedure\\.md § Fonts policy\\)').test(r.stdout)
  && fontsLine('live', 'every declared face loaded').test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['fonts1', 'https://live.example/fonts1', buildUrl, '1440', 'f3', '--marker', 'home-proposed'], { STUB_FONTS_BUILD: 'X', STUB_PIXEL_RC: '2' });
check('the fonts lines are printed on a failing round too, before the exit', r.status === 2 && fontsLine('build', 'DEAD FACE\\(S\\) X: .*').test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['fonts2', 'https://live.example/fonts2', buildUrl, '1440', 'f4', '--marker', 'home-proposed'], { STUB_FONTS_NULL_LIVE: '1' });
check('a live record whose font check could not run (fontsFailed null) prints NOT CHECKED with the record\'s reason — never "every face loaded"; exit 0', r.status === 0
  && fontsLine('live', 'NOT CHECKED \\(Execution context was destroyed\\) — no face is known to have loaded').test(r.stdout) && !fontsLine('live', 'every declared face loaded').test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['fonts3', 'https://live.example/fonts3', buildUrl, '1440', 'f5', '--marker', 'home-proposed'], { STUB_NO_JSON_LIVE: '1', STUB_BAD_JSON_BUILD: '1' });
check('no record (an older stitch-shot) or an unreadable one prints NOT CHECKED naming the record path and the fix, per side', r.status === 0
  && fontsLine('live', `NOT CHECKED \\(no readable capture record at ${gdir('fonts3')}/live.capture\\.json — delete live\\.png to re-take it with the check\\)`).test(r.stdout)
  && fontsLine('build', `NOT CHECKED \\(no readable capture record at ${gdir('fonts3')}/build.capture\\.json — re-copy stitch-shot\\.mjs \\(replica SKILL\\.md § Setup\\)\\)`).test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['fonts2', 'https://live.example/fonts2', buildUrl, '1440', 'f6', '--marker', 'home-proposed']);
check('a later round reuses live.png AND its record: the live line still reads the cached capture\'s check (NOT CHECKED), the build side is fresh', r.status === 0
  && fontsLine('live', 'NOT CHECKED \\(Execution context was destroyed\\).*').test(r.stdout) && fontsLine('build', 'every declared face loaded').test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['fonts4', 'https://live.example/fonts4', buildUrl, '1440', 'f7', '--marker', 'home-proposed'], { STUB_STITCH_RC_LIVE: '1' });
check('a failed live capture leaves neither live.png nor live.capture.json behind', r.status === 1 && !existsSync(join(root, gdir('fonts4'), 'live.png')) && !existsSync(join(root, gdir('fonts4'), 'live.capture.json')), `status ${r.status}`);

// --full, everything green
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter2', '--full']);
check('--full exits 0 when every probe passes', r.status === 0, `status ${r.status} ${r.stderr.slice(0, 600)}`);
check('--full prints one content-diff verdict', /^content-diff: none — content \+ roles match/m.test(r.stdout), r.stdout);
check('--full prints the visual flag count and heads', /^visual-diff: 2 advisory flag\(s\) — HEADING COLOR: h2 differs; IMAGE DIMS: hero;/m.test(r.stdout), r.stdout);
check('--full prints the chrome-parity summary', /^chrome-parity: ✓ chrome parity/m.test(r.stdout), r.stdout);
check('--full prints the clip-probe verdict on the build side and skips content-presence in the prototype regime', /^clip-probe: Clipped: 0 /m.test(r.stdout) && !/^content-presence:/m.test(r.stdout) && !existsSync(join(root, 'stardust/replica/gates/home-1440/content-presence-iter2.txt')), r.stdout);
check('--full writes the probe evidence files', ['content-diff-iter2.txt', 'visual-diff-iter2.txt', 'chrome-parity-iter2.txt', 'clip-iter2.txt'].every((f) => existsSync(join(root, 'stardust/replica/gates/home-1440', f))));
check('--full keeps the full reports out of stdout', !/12 headings/.test(r.stdout) && !/Full metrics/.test(r.stdout));

// --full, structural red → 2 even though pixels pass
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter3', '--full'], { STUB_STRUCTURAL: '1' });
check('a structural content red fails the round (exit 2)', r.status === 2, `status ${r.status}`);
check('the structural count is on the verdict line, with the tiers', /^content-diff: 3 \(1 structural 🔴\) — looks-same 🔴 1 · nothing-lost 🟡 0 /m.test(r.stdout), r.stdout);

// --full, an invisible loss only (nothing-lost 🟡) → 2: the second tier fails the round too; the round's record names its files
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'lost1', '--full'], { STUB_LOST: '2' });
check('a nothing-lost 🟡 fails the round (exit 2) with 0 structural 🔴', r.status === 2 && /^content-diff: 4 \(0 structural 🔴\) — looks-same 🔴 0 · nothing-lost 🟡 2 /m.test(r.stdout), `status ${r.status} ${r.stdout}`);
{
  const vj = join(root, 'stardust/replica/gates/home-1440/verdict-lost1.json');
  const v = existsSync(vj) ? JSON.parse(readFileSync(vj, 'utf8')) : null;
  check('verdict-<label>.json records this round: each probe\'s rc and evidence file, the pixel rc + diff, the two tiers', v && v.round === 'lost1' && v.width === 1440 && v.nothingLost === 2 && v.structural === 0 && v.pixel.diff.endsWith('diff-lost1.png') && v.probes['content-diff'].evidence.endsWith('content-diff-lost1.txt') && v.probes['chrome-parity'].rc === 0 && !v.probes['content-presence'], JSON.stringify(v));
}

// --full, chrome deltas → 2
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter4', '--full'], { STUB_CHROME_DELTAS: '3' });
check('chrome deltas fail the round (exit 2)', r.status === 2, `status ${r.status}`);
check('the chrome summary line is printed', /^chrome-parity: ✗ 3 delta\(s\)/m.test(r.stdout), r.stdout);

// --full, pixel fail rules over green probes
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter5', '--full'], { STUB_PIXEL_RC: '2' });
check('a pixel fail is the round verdict (exit 2)', r.status === 2, `status ${r.status}`);

// --full, a probe deadline → 124, never a verdict
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter6', '--full'], { STUB_SLEEP: '3000', GATE_PROBE_TIMEOUT: '1' });
check('a probe deadline makes the round exit 124', r.status === 124, `status ${r.status}`);
check('the deadline is named on the verdict line', /^chrome-parity: DEADLINE \(exit 124\)/m.test(r.stdout), r.stdout);

// --full, a probe that errors gave no verdict → 1, never a pass
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter6b', '--full'], { STUB_CD_RC: '1' });
check('an errored probe makes the round exit 1, not 0', r.status === 1, `status ${r.status}`);
check('the error is named on the verdict line', /^content-diff: ERROR \(exit 1\) — content-diff error: build side answered HTTP 502/m.test(r.stdout), r.stdout);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter6c', '--full'], { STUB_CD_RC: '3' });
check('a bot challenge on a probe passes through as exit 3', r.status === 3, `status ${r.status}`);

// --full, the pixel step gave no verdict → the probes are skipped, exit = pixel rc
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter6d', '--full'], { STUB_PIXEL_RC: '124' });
check('pixel rc 124 under --full skips the probes and exits 124', r.status === 124, `status ${r.status}`);
check('the skip is said in one line', /^gate\.sh: probes skipped — the pixel round gave no verdict \(exit 124\)/m.test(r.stderr), r.stderr);
check('no probe ran or left evidence after a pixel deadline', !/^(content-diff|visual-diff|chrome-parity): /m.test(r.stdout) && !existsSync(join(root, 'stardust/replica/gates/home-1440/content-diff-iter6d.txt')));
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter6e', '--full'], { STUB_PIXEL_RC: '1' });
check('pixel rc 1 skips the probes and exits 1', r.status === 1 && /probes skipped/.test(r.stderr) && !existsSync(join(root, 'stardust/replica/gates/home-1440/chrome-parity-iter6e.txt')), `status ${r.status}`);

// --main / --no-dismiss reach the probes (recorded by the stub through its argv → evidence file)
stub(diff, 'content-diff.mjs', `console.log('argv ' + process.argv.slice(2).join(' ')); console.log('Findings: none — content + roles match');`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'iter7', '--full', '--main', '#content', '--no-dismiss']);
const cdPath = join(root, 'stardust/replica/gates/home-1440/content-diff-iter7.txt');
const cd = existsSync(cdPath) ? readFileSync(cdPath, 'utf8') : `(missing ${cdPath}; stderr: ${r.stderr.slice(0, 300)})`;
check('--main is passed to the diff probes', /--main #content/.test(cd), cd);
check('--no-dismiss drops the dismiss flag', !/--dismiss/.test(cd), cd);

// Overlay dismissal, one vocabulary (header § Overlay dismissal): --consent / --dismiss reach BOTH stitched captures
// (symmetric), the diff probes (joined into their one --dismiss value) and chrome-parity; a default round dismisses
// with the built-in candidates (captures: no flag; probes: bare --dismiss); --no-dismiss reaches the captures and
// chrome-parity as --no-dismiss and drops the probes' flag. Fresh slugs so the live capture runs (live.png is cached per
// slug × width); the selectors carry spaces and commas on purpose — the quoting through the arrays is the contract.
const argsFile = join(root, 'stitch-args.txt');
const stitchArgs = () => (existsSync(argsFile) ? readFileSync(argsFile, 'utf8').trim().split('\n') : []);
const resetArgs = () => { if (existsSync(argsFile)) unlinkSync(argsFile); };
const evidence = (slug, name) => { const p = join(root, `stardust/replica/gates/${slug}-1440/${name}`); return existsSync(p) ? readFileSync(p, 'utf8') : `(missing ${p})`; };
const CONSENT = '#consent-yes, .consent-box-yes'; const EXTRA = '.promo-close,#newsletter-close';
resetArgs();
r = await gate(['consent1', 'https://live.example/consent1', buildUrl, '1440', 'c1', '--full', '--marker', 'home-proposed', '--consent', CONSENT, '--dismiss', EXTRA], { STUB_STITCH_ARGS: argsFile });
check('--consent / --dismiss reach the live capture (after --settle) and the build capture alike, values intact', r.status === 0 && stitchArgs().length === 2
  && stitchArgs()[0] === `LIVE https://live.example/consent1 stardust/replica/gates/consent1-1440/live.png --width 1440 --settle --consent ${CONSENT} --dismiss ${EXTRA}`
  && stitchArgs()[1] === `BUILD ${buildUrl} stardust/replica/gates/consent1-1440/build.png --width 1440 --settle --consent ${CONSENT} --dismiss ${EXTRA}`, `status ${r.status} | ${stitchArgs().join(' | ')} ${r.stderr.slice(0, 200)}`);
check('the diff probes get the same selectors joined into one --dismiss list', new RegExp(`--main main --dismiss ${CONSENT},${EXTRA}$`, 'm').test(evidence('consent1', 'content-diff-c1.txt')) && new RegExp(`--main main --dismiss ${CONSENT},${EXTRA} --out `).test(evidence('consent1', 'visual-diff-c1.txt')), evidence('consent1', 'content-diff-c1.txt') + evidence('consent1', 'visual-diff-c1.txt'));
check('chrome-parity gets --consent and --dismiss', new RegExp(`--live-cache \\S+ --consent ${CONSENT} --dismiss ${EXTRA}$`, 'm').test(evidence('consent1', 'chrome-parity-c1.txt')), evidence('consent1', 'chrome-parity-c1.txt'));
resetArgs();
r = await gate(['consent2', 'https://live.example/consent2', buildUrl, '1440', 'c2', '--full', '--marker', 'home-proposed'], { STUB_STITCH_ARGS: argsFile });
check('a default round dismisses with the built-in candidates: no overlay flag on either capture, bare --dismiss on the probes, none on chrome-parity', r.status === 0 && stitchArgs().length === 2
  && stitchArgs().every((l) => !/--consent|--dismiss|--no-dismiss/.test(l))
  && /--main main --dismiss$/m.test(evidence('consent2', 'content-diff-c2.txt')) && /--main main --dismiss --out /.test(evidence('consent2', 'visual-diff-c2.txt'))
  && !/--consent|--dismiss/.test(evidence('consent2', 'chrome-parity-c2.txt')), `status ${r.status} | ${stitchArgs().join(' | ')} | ${evidence('consent2', 'content-diff-c2.txt')}`);
resetArgs();
r = await gate(['consent3', 'https://live.example/consent3', buildUrl, '1440', 'c3', '--full', '--marker', 'home-proposed', '--no-dismiss'], { STUB_STITCH_ARGS: argsFile });
check('--no-dismiss reaches both captures and chrome-parity as --no-dismiss and drops the probes\' flag', r.status === 0 && stitchArgs().length === 2
  && stitchArgs().every((l) => / --no-dismiss$/.test(l) && !/--consent|--dismiss /.test(l))
  && !/--dismiss/.test(evidence('consent3', 'content-diff-c3.txt')) && !/--dismiss/.test(evidence('consent3', 'visual-diff-c3.txt')) && / --no-dismiss$/m.test(evidence('consent3', 'chrome-parity-c3.txt')) && !/--consent|--dismiss /.test(evidence('consent3', 'chrome-parity-c3.txt')), `status ${r.status} | ${stitchArgs().join(' | ')}`);
resetArgs();
r = await gate(['consent3', 'https://live.example/consent3', buildUrl, '1440', 'c4', '--no-dismiss', '--consent', '#consent-yes'], { STUB_STITCH_ARGS: argsFile });
check('--no-dismiss with --consent is a usage error (exit 125) before any capture', r.status === 125 && /contradicts/.test(r.stderr) && stitchArgs().length === 0, `status ${r.status} ${r.stderr}`);
r = await gate(['consent3', 'https://live.example/consent3', buildUrl, '1440', 'c5', '--consent']);
check('--consent without a selector is a usage error', r.status !== 0 && /--consent needs a selector/.test(r.stderr), `status ${r.status} ${r.stderr}`);

// #125: element defects fail a --full round; content-presence and unit-geometry join in the published regime / with a families file
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'clip1', '--full'], { STUB_CLIPPED: '3' });
check('--full: a clipped count > 0 on the build side fails the round (exit 2) with its verdict line', r.status === 2 && /^clip-probe: Clipped: 3 /m.test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'pub1', '--full'], { GATE_REGIME: 'published', STUB_HIDDEN: '284' });
check('--full in the published regime runs content-presence live vs build and a HIDDEN link fails the round', r.status === 2 && /^content-presence: Content: MISSING 0 .*HIDDEN 284/m.test(r.stdout) && existsSync(join(root, 'stardust/replica/gates/home-1440/content-presence-pub1.txt')), `status ${r.status} ${r.stdout}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'pub2', '--full'], { GATE_REGIME: 'published' });
check('--full in the published regime passes when content-presence is clean', r.status === 0 && /^content-presence: Content: MISSING 0 .*HIDDEN 0/m.test(r.stdout), `status ${r.status} ${r.stdout}`);
check('no unit-geometry line without a families file', !/^unit-geometry:/m.test(r.stdout));
mkdirSync(join(root, 'stardust/replica'), { recursive: true });
writeFileSync(join(root, 'stardust/replica/units.json'), JSON.stringify({ cards: { origin: '.a', build: '.b', pages: ['home'] } }));
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'u1', '--full']);
check('with stardust/replica/units.json the round runs unit-geometry --families --stage --slug and prints its line', r.status === 0 && /^unit-geometry: unit-geometry: none declared/m.test(r.stdout) && /--families stardust\/replica\/units.json --stage prototype --slug home/.test(readFileSync(join(root, 'stardust/replica/gates/home-1440/units-u1.txt'), 'utf8')), `status ${r.status} ${r.stdout}`);
r = await gate(['home', 'https://live.example/', buildUrl, '1440', 'u2', '--full'], { STUB_UNITS_RC: '2' });
check('a required unit off (unit-geometry exit 2) fails the round', r.status === 2 && /^unit-geometry: Units: 1 compared/m.test(r.stdout), `status ${r.status} ${r.stdout}`);
unlinkSync(join(root, 'stardust/replica/units.json'));

// --gate-against-baseline (the opt-in origin baseline; default OFF). Flag ON with a frozen baseline: no live capture,
// baseline.png is the origin side (copied over live.png), live.json says origin "baseline", one stdout line names the
// capture time. Flag ON without a usable baseline (missing, lone PNG, frozen for another width or URL): exit 125 with
// one stderr line BEFORE any capture — never a live shot in its place. Flag OFF: a round is the one it always was —
// a baseline beside it changes nothing, a baseline-derived live.png is retaken with one line, a live.png with no
// record is reused. The bookkeeping goes through the real stitch-shot (the stub forwards `baseline` / `record`).
const liveJson = (slug) => { const f = join(root, gdir(slug), 'live.json'); return existsSync(f) ? readFileSync(f, 'utf8') : null; };
const livePng = (slug) => { const f = join(root, gdir(slug), 'live.png'); return existsSync(f) ? readFileSync(f, 'utf8') : null; };
const noTmp = (slug) => !readdirSync(join(root, gdir(slug))).some((f) => f.endsWith('.tmp'));
const PROV = { writtenBy: 'skills/replica/scripts/stitch-shot.mjs --freeze-baseline', schema: 1, capturedAt: '2026-10-19T08:00:00.000Z', url: 'https://live.example/', width: 1440, vh: 900, height: 2000, settle: true, chunks: 3, fontsFailed: [], fixedLayers: { capture: 'once', count: 1 } };
const freezeBaseline = (slug, prov = PROV) => {
  const d = join(root, gdir(slug));
  mkdirSync(d, { recursive: true });
  writeFileSync(join(d, 'baseline.png'), 'baseline-png-bytes');
  writeFileSync(join(d, 'baseline.json'), `${JSON.stringify(prov, null, 2)}\n`);
};
r = await gate(['--help']);
check('--help names the origin reference: the default, --live-origin, GATE_REFERENCE=live, --gate-against-baseline as a no-op, exit 5', /Origin reference/.test(r.stdout) && /--live-origin/.test(r.stdout) && /GATE_REFERENCE=live/.test(r.stdout)
  && /--gate-against-baseline\s+accepted, a no-op/.test(r.stdout) && /5 origin reference UNSTABLE/.test(r.stdout) && /reference\.mjs freeze/.test(r.stdout), r.stdout.slice(0, 200));
check('--help documents the pixel criterion and its env: aligned % ≤ threshold AND total offset ≤ GATE_OFFSET_MAX_PX (8 prototype; published the larger of 8 and 1 %)', /Pixel criterion/.test(r.stdout) && /GATE_OFFSET_MAX_PX/.test(r.stdout)
  && /larger\s+of 8 and 1 % of the origin height/.test(r.stdout) && /offsets: 3 step\(s\), 37px total/.test(r.stdout), r.stdout.slice(0, 200));

resetCount();
freezeBaseline('blon');
r = await gate(['blon', 'https://live.example/', buildUrl, '1440', 'bl1', '--marker', 'home-proposed', '--gate-against-baseline'], { STUB_STITCH_COUNT: countFile });
check('flag ON with a frozen baseline: the round passes with no live capture — only the build side stitches', r.status === 0 && attempts().join(',') === 'BUILD', `status ${r.status} attempts ${attempts().join(',')} ${r.stderr.slice(0, 300)}`);
check('the baseline is the pixel-compare origin: live.png is a copy of baseline.png, no temp file left', livePng('blon') === 'baseline-png-bytes' && noTmp('blon'));
check('live.json records origin "baseline", at = the capture time and originBaseline { capturedAt, height }, written by gate.sh --gate-against-baseline', (() => {
  const lj = JSON.parse(liveJson('blon') || '{}');
  return lj.writtenBy === 'gate.sh --gate-against-baseline' && lj.origin === 'baseline' && lj.url === 'https://live.example/' && lj.width === 1440 && lj.at === PROV.capturedAt
    && JSON.stringify(lj.originBaseline) === JSON.stringify({ capturedAt: PROV.capturedAt, height: PROV.height });
})(), liveJson('blon'));
check('one stdout line names the frozen reference and its capture time among the verdict lines — a baseline frozen by stitch-shot has no measured noise floor, and the line says so', /differing[^]*^gate\.sh: reference: frozen 2026-10-19T08:00:00\.000Z, noise floor not measured \(frozen without priming — reference\.mjs freeze --force measures it\) — 0 rows excluded$/m.test(r.stdout), r.stdout);
check('the pixel verdict and the overflow assert still print (the rest of the round is unchanged)', /differing 1\.20%/.test(r.stdout) && /overflow assert at 1440/.test(r.stdout), r.stdout);
check('the live fonts line of a flagged round reads the frozen baseline\'s check (live.png is its copy and has no capture record)', /^gate\.sh: fonts on the live capture — every declared face loaded \(the frozen baseline's check\)$/m.test(r.stdout), r.stdout);

// No usable baseline (missing, frozen for another width or URL, a lone PNG): the round freezes one — once — and gates
// against it; the reason the old one was unusable is on the line.
resetCount();
r = await gate(['blmiss', 'https://live.example/', buildUrl, '1440', 'bl2', '--marker', 'home-proposed', '--gate-against-baseline'], { STUB_STITCH_COUNT: countFile });
check('no baseline: the round freezes a reference (one freeze, then the build), says why, and passes against it', r.status === 0 && attempts().join(',') === 'REF,BUILD'
  && /^gate\.sh: no usable frozen reference for blmiss@1440 \(no frozen baseline in stardust\/replica\/gates\/blmiss-1440 \(baseline\.png \+ baseline\.json\)\) — freezing one: a priming capture, then two kept \(reference\.mjs freeze\)$/m.test(r.stdout)
  && livePng('blmiss') === 'frozen-reference-bytes' && JSON.parse(liveJson('blmiss') || '{}').origin === 'baseline', `status ${r.status} attempts ${attempts().join(',')} ${r.stdout} ${r.stderr}`);
for (const [slug, prov, why] of [['blstale', { ...PROV, width: 360 }, /stale: .*was frozen at 360px, this round is 1440px/], ['blurl', { ...PROV, url: 'https://live.example/other' }, /was frozen for https:\/\/live\.example\/other, this round's live URL is https:\/\/live\.example\//], ['bllone', null, /baseline\.png has no baseline\.json beside it \(an interrupted freeze\)/]]) {
  resetCount();
  if (prov) freezeBaseline(slug, prov); else { mkdirSync(join(root, gdir(slug)), { recursive: true }); writeFileSync(join(root, gdir(slug), 'baseline.png'), 'baseline-png-bytes'); }
  r = await gate([slug, 'https://live.example/', buildUrl, '1440', 'bl3', '--marker', 'home-proposed', '--gate-against-baseline'], { STUB_STITCH_COUNT: countFile });
  check(`${slug}: an unusable baseline (another width / another URL / a lone PNG) is frozen again for this round's URL and width — its reason named, never compared`, r.status === 0 && attempts().join(',') === 'REF,BUILD'
    && why.test(r.stdout) && livePng(slug) === 'frozen-reference-bytes' && JSON.parse(readFileSync(join(root, gdir(slug), 'baseline.json'), 'utf8')).width === 1440, `status ${r.status} attempts ${attempts().join(',')} ${r.stdout} ${r.stderr}`);
}

resetCount();
freezeBaseline('bloff');
r = await gate(['bloff', 'https://live.example/', buildUrl, '1440', 'bl6', '--marker', 'home-proposed'], { STUB_STITCH_COUNT: countFile });
check('GATE_REFERENCE=live with a baseline beside the round: the round as it was — a fresh live shot, then the build; no live.json, the reference line says live, live.png is the live capture', r.status === 0 && attempts().join(',') === 'LIVE,BUILD'
  && liveJson('bloff') === null && livePng('bloff') === 'png' && !/reference: frozen/.test(r.stdout + r.stderr) && /^gate\.sh: reference: a live capture \(--live-origin \/ GATE_REFERENCE=live, or no reference\.mjs\) — not primed, no noise floor measured, no rows excluded$/m.test(r.stdout), `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
resetCount();
r = await gate(['bloff', 'https://live.example/', buildUrl, '1440', 'bl7', '--marker', 'home-proposed'], { STUB_STITCH_COUNT: countFile });
check('… and the next round reuses that live.png (BUILD only), as on every round before this flag existed', r.status === 0 && attempts().join(',') === 'BUILD' && liveJson('bloff') === null, `attempts ${attempts().join(',')}`);

resetCount();
writeFileSync(join(root, gdir('blon'), 'live.capture.json'), '{ "stale": "a live round\'s record" }\n');
r = await gate(['blon', 'https://live.example/', buildUrl, '1440', 'bl8', '--marker', 'home-proposed'], { STUB_STITCH_COUNT: countFile });
check('flag dropped after a flagged round: the baseline-derived live.png is retaken with one line — LIVE then BUILD, live.json removed, live.png is the live capture', r.status === 0 && attempts().join(',') === 'LIVE,BUILD'
  && /^gate\.sh: live\.png is the frozen origin reference's copy; this round gates against the live origin \(--live-origin \/ GATE_REFERENCE=live\) — retaking it$/m.test(r.stderr) && (r.stderr.match(/retaking/g) || []).length === 1
  && liveJson('blon') === null && livePng('blon') === 'png', `status ${r.status} attempts ${attempts().join(',')} ${r.stderr} ${liveJson('blon')}`);
check('… and the stale capture record (live.capture.json) went with the retaken live.png — the retake wrote a fresh one', existsSync(join(root, gdir('blon'), 'live.capture.json')) && !readFileSync(join(root, gdir('blon'), 'live.capture.json'), 'utf8').includes('stale'));
resetCount();
r = await gate(['blon', 'https://live.example/', buildUrl, '1440', 'bl9', '--marker', 'home-proposed'], { STUB_STITCH_COUNT: countFile });
check('… the next unflagged round reuses that fresh live.png (BUILD only, no retake line)', r.status === 0 && attempts().join(',') === 'BUILD' && !/retaking/.test(r.stderr), `attempts ${attempts().join(',')} ${r.stderr}`);
resetCount();
writeFileSync(join(root, gdir('blon'), 'live.capture.json'), '{ "stale": "the live round\'s record" }\n');
r = await gate(['blon', 'https://live.example/', buildUrl, '1440', 'bl10', '--marker', 'home-proposed', '--gate-against-baseline'], { STUB_STITCH_COUNT: countFile });
check('a flagged round after a live one takes the baseline again: BUILD only, live.png is baseline.png, live.json origin "baseline", no temp file', r.status === 0 && attempts().join(',') === 'BUILD'
  && livePng('blon') === 'baseline-png-bytes' && JSON.parse(liveJson('blon') || '{}').origin === 'baseline' && noTmp('blon'), `attempts ${attempts().join(',')} ${liveJson('blon')}`);
check('… and the live round\'s capture record (live.capture.json) is removed with the copy — no record describes bytes it did not measure', !existsSync(join(root, gdir('blon'), 'live.capture.json')));

// A record that cannot be written (STUB_RECORD_FAIL): the round ends (exit 1, one line) with live.png removed, so the
// baseline bytes never sit under a missing record for the next unflagged round to reuse as a live shot.
resetCount();
r = await gate(['blon', 'https://live.example/', buildUrl, '1440', 'bl11', '--marker', 'home-proposed', '--gate-against-baseline'], { STUB_STITCH_COUNT: countFile, STUB_RECORD_FAIL: '1' });
check('a flagged round whose live.json write fails: exit 1 with one line, no capture spent, neither live.png nor live.json left', r.status === 1 && attempts().length === 0
  && /^gate\.sh: could not write stardust\/replica\/gates\/blon-1440\/live\.json for the frozen origin — not comparing \(a later round would read the copy as a live shot\); re-run the round$/m.test(r.stderr)
  && livePng('blon') === null && liveJson('blon') === null, `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
resetCount();
r = await gate(['blon', 'https://live.example/', buildUrl, '1440', 'bl12', '--marker', 'home-proposed'], { STUB_STITCH_COUNT: countFile });
check('… so the next unflagged round captures live (LIVE,BUILD) instead of reusing baseline bytes', r.status === 0 && attempts().join(',') === 'LIVE,BUILD' && livePng('blon') === 'png', `attempts ${attempts().join(',')} ${r.stderr}`);
check('the frozen baseline itself is untouched by every round above', readFileSync(join(root, gdir('blon'), 'baseline.png'), 'utf8') === 'baseline-png-bytes' && JSON.parse(readFileSync(join(root, gdir('blon'), 'baseline.json'), 'utf8')).capturedAt === PROV.capturedAt);

// ---- The origin reference as the DEFAULT origin, and the aligned pixel criterion (DEF: no GATE_REFERENCE) ------------
// rounds.mjs is the real one for this section, so "recorded" and "not recorded" are read from rounds.json.
cpSync(join(scripts, 'rounds.mjs'), join(replica, 'rounds.mjs'));
const refArgsFile = join(root, 'ref-args.txt'); const pixelArgsFile = join(root, 'pixel-args.txt'); const stitchArgsFile = join(root, 'stitch-args-ref.txt');
const lines = (f) => (existsSync(f) ? readFileSync(f, 'utf8').trim().split('\n').filter(Boolean) : []);
const resetRefArgs = () => { for (const f of [refArgsFile, pixelArgsFile, stitchArgsFile]) if (existsSync(f)) unlinkSync(f); };
const roundsOf = (slug) => { try { return Object.values(JSON.parse(readFileSync(join(root, gdir(slug), 'rounds.json'), 'utf8')).stages).flatMap((st) => st.rounds); } catch { return []; } };
const verdictJson = (slug, label) => { const f = join(root, gdir(slug), `verdict-${label}.json`); return existsSync(f) ? JSON.parse(readFileSync(f, 'utf8')) : null; };
const REFENV = { ...DEF, STUB_STITCH_COUNT: countFile, STUB_REF_ARGS: refArgsFile, STUB_PIXEL_ARGS: pixelArgsFile, STUB_STITCH_ARGS: stitchArgsFile };
const refRound = (slug, label, extra = [], env = {}) => gate([slug, `https://live.example/${slug}`, buildUrl, '1440', label, '--marker', 'home-proposed', ...extra], { ...REFENV, ...env });
const steps = (...pairs) => { let c = 0; return pairs.map(([y, shift]) => { c += shift; return { y, shift, cumulative: c }; }); };
const aligned = (pct, alignedPct, st, extra = {}) => JSON.stringify({ pct, aligned: { used: true, pct: alignedPct, steps: st }, ...extra });

// First round: no reference → frozen (one freeze, the round's flags), then gated against; second round: reused.
resetCount(); resetRefArgs();
r = await refRound('ref1', 'r1', ['--consent', '#accept-all', '--dismiss', '.promo .close', '--locale', 'en-GB'], { STUB_REF_FLOOR: '2.31', STUB_REF_MASK: '392:56,4000:652' });
check('default origin, first round: a reference is frozen (one freeze, no stitched live capture), then the build is captured and the round passes', r.status === 0 && attempts().join(',') === 'REF,BUILD', `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
check('the freeze gets the live URL, the evidence dir, the width, the capture deadline and the round\'s overlay flags and locale', lines(refArgsFile).length === 1
  && lines(refArgsFile)[0] === `https://live.example/ref1 ${gdir('ref1')} --width 1440 --timeout 300 --consent #accept-all --dismiss .promo .close --locale en-GB`, lines(refArgsFile).join('|'));
check('… and the build capture gets the same flags and locale', /^BUILD .* --consent #accept-all --dismiss \.promo \.close --locale en-GB$/.test(lines(stitchArgsFile)[0] || ''), lines(stitchArgsFile).join('|'));
check('the freeze is announced with the reason, and reference.mjs\'s own line is relayed', /^gate\.sh: no usable frozen reference for ref1@1440 \(no frozen baseline in .*\) — freezing one/m.test(r.stdout) && /^gate\.sh: reference: frozen 1440×2000, primed, noise floor 2\.31% in 2 variable region\(s\) → mask 392:56,4000:652$/m.test(r.stdout), r.stdout);
check('the round gates against the frozen reference: live.png is its copy, live.json says origin "baseline"', livePng('ref1') === 'frozen-reference-bytes' && JSON.parse(liveJson('ref1') || '{}').origin === 'baseline');
check('one verdict line says what the round was gated against: capture time, noise floor, regions, rows excluded', /^gate\.sh: reference: frozen 2026-11-01T09:00:00\.000Z, noise floor 2\.31% in 2 region\(s\) — 708 rows excluded$/m.test(r.stdout), r.stdout);
check('the reference\'s mask reaches pixel-compare, with --offset-max at the regime\'s default and --json-out in the evidence dir', (() => { const a = lines(pixelArgsFile)[0] || ''; return / --offset-max prototype /.test(a) && a.includes(` --json-out ${gdir('ref1')}/pixel-r1.json`) && / --mask 392:56,4000:652$/.test(a); })(), lines(pixelArgsFile).join('|'));
check('a round that passed on the unaligned number still passes (alignment not used → the unaligned rule), and the pixel: line says so', /^pixel: 1\.20% \(alignment not used — the unaligned number rules\) → PASS$/m.test(r.stdout), r.stdout);
check('the round is recorded once, as a pass', roundsOf('ref1').length === 1 && roundsOf('ref1')[0].verdict === 'pass' && roundsOf('ref1')[0].label === 'r1', JSON.stringify(roundsOf('ref1')));
check('verdict-<label>.json is written for a pixel-only round too: stage, the pixel numbers, the masked rows, the noise floor, the reference', (() => {
  const v = verdictJson('ref1', 'r1');
  return v && v.round === 'r1' && v.width === 1440 && v.stage === 'prototype' && v.pixel.rc === 0 && v.pixel.pct === 1.2 && v.pixel.alignedPct === 1.2 && v.pixel.alignedUsed === false && v.pixel.totalShift === 0
    && Array.isArray(v.pixel.steps) && v.pixel.steps.length === 0 && v.pixel.maskedRows === 708 && v.pixel.noiseFloor === 2.31 && v.pixel.diff.endsWith('diff-r1.png') && v.pixel.json.endsWith('pixel-r1.json')
    && v.reference.origin === 'frozen' && v.reference.capturedAt === '2026-11-01T09:00:00.000Z' && v.reference.primed === true && v.reference.maskArg === '392:56,4000:652'
    && JSON.stringify(v.probes) === '{}' && v.structural === 0 && v.nothingLost === 0 && typeof v.at === 'string';
})(), JSON.stringify(verdictJson('ref1', 'r1')));
resetCount(); resetRefArgs();
r = await refRound('ref1', 'r2', ['--mask', '10:20', '--mask', '300:40@310']);
check('second round: the frozen reference is reused — no freeze, no live capture, only the build', r.status === 0 && attempts().join(',') === 'BUILD' && lines(refArgsFile).length === 0 && !/freezing one/.test(r.stdout), `status ${r.status} attempts ${attempts().join(',')}`);
check('a caller --mask (repeatable) is merged with the reference\'s: the caller\'s first, then the self-varying rows', / --mask 10:20,300:40@310,392:56,4000:652$/.test(lines(pixelArgsFile)[0] || '') && verdictJson('ref1', 'r2').pixel.maskedRows === 768, lines(pixelArgsFile).join('|'));
resetCount();
r = await refRound('ref1', 'r3', ['--full']);
check('a --full round on the default origin: the probes run against the live URL, the verdict file keeps the pixel numbers beside the probes', r.status === 0 && attempts().join(',') === 'BUILD' && /^content-diff: none/m.test(r.stdout)
  && readFileSync(join(root, gdir('ref1'), 'content-diff-r3.txt'), 'utf8').length > 0 && verdictJson('ref1', 'r3').pixel.noiseFloor === 2.31 && verdictJson('ref1', 'r3').probes['content-diff'].rc === 0 && verdictJson('ref1', 'r3').pixel.rc === 0, `status ${r.status} ${r.stdout}`);

check('a finished --full round leaves no pending mark in its verdict file', verdictJson('ref1', 'r3').pending === undefined);
// An unstable reference: no verdict, a distinct exit, nothing recorded.
r = await refRound('refunl', 'u0', ['--locale', 'de-DE'], { STUB_REF_RC: '2' });
check('the forced-freeze command of the exit-5 message carries the round\'s locale and overlay flags', r.status === 5 && /--width 1440 --locale de-DE --force$/m.test(r.stderr), r.stderr);
resetCount(); resetRefArgs();
r = await refRound('refun', 'u1', ['--full'], { STUB_REF_RC: '2' });
check('an UNSTABLE reference ends the round with exit 5 after ONE freeze: no retry, no build capture, no pixel line, no probe', r.status === 5 && attempts().join(',') === 'REF' && !/differing|overflow assert|content-diff/.test(r.stdout), `status ${r.status} attempts ${attempts().join(',')} ${r.stdout}`);
check('reference.mjs\'s one-line reason is printed, then the way out: pin the state (overlay flags, --locale) or freeze --force after reading reference-unstable.json', /^gate\.sh: reference: UNSTABLE — heights 2000 vs 2640 after priming; not frozen$/m.test(r.stderr)
  && new RegExp(`^gate\\.sh: REFERENCE UNSTABLE \\(exit 5\\) — .*NO VERDICT, the round is not counted\\. Pin the page's state and re-run — overlay flags \\(--consent <sel> / --dismiss <sel,…>\\), --locale <tag> — or read ${gdir('refun')}/reference-unstable\\.json \\(\\+ ref-\\*\\.png\\) and, when the variation is the page's own, freeze it anyway: node .*reference\\.mjs freeze "https://live\\.example/refun" ${gdir('refun')} --width 1440 --force$`, 'm').test(r.stderr), r.stderr);
check('no verdict file, no round recorded, no baseline, no live.png', !readdirSync(join(root, gdir('refun'))).some((f) => /^verdict-|^baseline\.|^live\.png$|^pixel-/.test(f)) && roundsOf('refun').length === 0, `${readdirSync(join(root, gdir('refun'))).join(',')} ${JSON.stringify(roundsOf('refun'))}`);
resetCount();
r = await refRound('refun', 'u2');
check('the next round freezes again (nothing was frozen) and, the page now stable, passes', r.status === 0 && attempts().join(',') === 'REF,BUILD', `status ${r.status} attempts ${attempts().join(',')}`);

// No-verdict exits of the freeze: a bot challenge (3), a deadline (124), a capture error (1, retried once).
resetCount();
r = await refRound('refbot', 'b1', [], { STUB_REF_RC: '3' });
check('a bot challenge during the freeze is exit 3 on the first attempt — the BLOCKED line, no build capture, nothing recorded, no verdict file', r.status === 3 && attempts().join(',') === 'REF'
  && /^gate\.sh: reference capture BLOCKED \(exit 3\) — a bot challenge or 2xx denial page, not the page: no verdict for this round; escalate --headed$/m.test(r.stderr) && roundsOf('refbot').length === 0 && verdictJson('refbot', 'b1') === null, `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
resetCount();
r = await refRound('refslow', 's1', [], { STUB_REF_SLEEP: '6000', GATE_REFERENCE_TIMEOUT: '1' });
check('a freeze over GATE_REFERENCE_TIMEOUT is exit 124 — no verdict, not retried, nothing recorded', r.status === 124 && attempts().join(',') === 'REF' && /^gate\.sh: reference freeze hit its deadline \(exit 124\) — no verdict, re-run the round/m.test(r.stderr) && roundsOf('refslow').length === 0, `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
resetCount();
r = await refRound('refonce', 'o1', [], { STUB_REF_FAIL_ONCE: join(root, 'once-ref') });
check('a freeze that exits 1 once is retried once and the round passes', r.status === 0 && attempts().join(',') === 'REF,REF,BUILD' && /^gate\.sh: reference freeze exited 1 — retrying once/m.test(r.stderr), `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
resetCount();
r = await refRound('referr', 'e1', [], { STUB_REF_RC: '1' });
check('two exit-1 freezes: exit 1, re-queue named, no verdict', r.status === 1 && attempts().join(',') === 'REF,REF' && /reference capture failed \(exit 1\) — not comparing \(twice: no verdict — re-queue the round\)/.test(r.stderr) && roundsOf('referr').length === 0, `status ${r.status} ${r.stderr}`);
r = await refRound('ref1', 'd1', [], { STUB_PIXEL_RC: '124' });
check('a pixel deadline on the default origin is still exit 124 with no verdict file and no round recorded', r.status === 124 && verdictJson('ref1', 'd1') === null && !roundsOf('ref1').some((x) => x.label === 'd1'), `status ${r.status}`);
r = await refRound('ref1', 'd2', [], { STUB_STITCH_RC_BUILD: '3' });
check('a blocked build capture on the default origin is still exit 3', r.status === 3 && verdictJson('ref1', 'd2') === null, `status ${r.status}`);

// --live-origin: the round as it was, for one round; GATE_REFERENCE=live the same for every round (the helper default).
resetCount();
r = await refRound('reflive', 'l1', ['--live-origin']);
check('--live-origin: a live capture, then the build — no freeze, no baseline written, live.png is the capture, the reference line says live', r.status === 0 && attempts().join(',') === 'LIVE,BUILD' && livePng('reflive') === 'png'
  && !existsSync(join(root, gdir('reflive'), 'baseline.png')) && liveJson('reflive') === null && /^gate\.sh: reference: a live capture/m.test(r.stdout) && verdictJson('reflive', 'l1').reference.origin === 'live' && verdictJson('reflive', 'l1').pixel.noiseFloor === null, `status ${r.status} attempts ${attempts().join(',')}`);
check('… and pixel-compare gets no reference mask', !/--mask/.test(lines(pixelArgsFile).pop() || ''), lines(pixelArgsFile).pop());
resetCount();
r = await gate(['reflive', 'https://live.example/reflive', buildUrl, '1440', 'l2', '--marker', 'home-proposed'], { STUB_STITCH_COUNT: countFile, GATE_REFERENCE: 'live' });
check('GATE_REFERENCE=live: the same for every round — the cached live.png is reused, no freeze', r.status === 0 && attempts().join(',') === 'BUILD' && !existsSync(join(root, gdir('reflive'), 'baseline.png')), `status ${r.status} attempts ${attempts().join(',')}`);
resetCount();
r = await gate(['reflive', 'https://live.example/reflive', buildUrl, '1440', 'l3', '--marker', 'home-proposed', '--gate-against-baseline'], { STUB_STITCH_COUNT: countFile, GATE_REFERENCE: 'live' });
check('--gate-against-baseline is accepted and means the default: over GATE_REFERENCE=live it freezes and gates against the reference', r.status === 0 && attempts().join(',') === 'REF,BUILD' && livePng('reflive') === 'frozen-reference-bytes', `status ${r.status} attempts ${attempts().join(',')} ${r.stderr}`);
r = await gate(['reflive', 'https://live.example/reflive', buildUrl, '1440', 'l4', '--marker', 'home-proposed'], { GATE_REFERENCE: 'sometimes' });
check('GATE_REFERENCE takes frozen or live only (exit 125)', r.status === 125 && /GATE_REFERENCE must be frozen or live/.test(r.stderr), `status ${r.status} ${r.stderr}`);
resetCount();
unlinkSync(join(replica, 'rounds.mjs')); const refStub = readFileSync(join(replica, 'reference.mjs'), 'utf8'); unlinkSync(join(replica, 'reference.mjs'));
r = await gate(['refnone', 'https://live.example/refnone', buildUrl, '1440', 'n1', '--marker', 'home-proposed'], { ...DEF, STUB_STITCH_COUNT: countFile });
check('a project copy without reference.mjs: one stdout line, the round gates against a live capture', r.status === 0 && attempts().join(',') === 'LIVE,BUILD' && /^gate\.sh: .*reference\.mjs missing — this round gates against a live capture/m.test(r.stdout), `status ${r.status} attempts ${attempts().join(',')} ${r.stdout}`);
writeFileSync(join(replica, 'reference.mjs'), refStub); cpSync(join(scripts, 'rounds.mjs'), join(replica, 'rounds.mjs'));

// The pixel criterion: aligned % ≤ threshold AND total offset ≤ the bound (8 prototype; published the larger of 8 and 1 %).
r = await refRound('px1', 'a1', [], { STUB_PIXEL_JSON: aligned(16.62, 1.23, steps([103, -5])) });
check('aligned % within the threshold, offsets within the bound → exit 0 although the unaligned number is over the threshold', r.status === 0, `status ${r.status} ${r.stdout}`);
check('the aligned line of pixel-compare and the pixel: line — both percentages, the offset list, the bound — are printed after the pixel lines', /differing 1\.20%\n^aligned: 1\.23% \(unaligned 16\.62%\) — 1 offset step: −5px at y=103$/m.test(r.stdout)
  && /^pixel: aligned 1\.23% \(unaligned 16\.62%\) → PASS — offsets: 1 step\(s\), 5px total — −5px at y=103 \(bound 8px\)$/m.test(r.stdout), r.stdout);
check('the verdict file carries both percentages, alignedUsed, the total shift and the steps', (() => { const v = verdictJson('px1', 'a1').pixel; return v.rc === 0 && v.pct === 16.62 && v.alignedPct === 1.23 && v.alignedUsed === true && v.totalShift === 5 && JSON.stringify(v.steps) === JSON.stringify(steps([103, -5])) && v.offsetBound === 8 && v.reason === null; })(), JSON.stringify(verdictJson('px1', 'a1')));
r = await refRound('px1', 'a2', [], { STUB_PIXEL_JSON: aligned(16.62, 1.23, steps([103, -5], [2125, -20], [3300, -12])) });
check('aligned % within the threshold but offsets over the bound → exit 2, and the reason names the offsets, not a percentage', r.status === 2
  && /^pixel: aligned 1\.23% \(unaligned 16\.62%\) → FAIL — offsets: 3 step\(s\), 37px total — −5px at y=103, −20px at y=2125, −12px at y=3300 \(bound 8px\)$/m.test(r.stdout), `status ${r.status} ${r.stdout}`);
check('… recorded as a FAIL round, the verdict file says why', roundsOf('px1').map((x) => x.verdict).join(',') === 'pass,fail' && (() => { const v = verdictJson('px1', 'a2').pixel; return v.rc === 2 && v.totalShift === 37 && v.steps.length === 3 && /^offsets: 3 step\(s\), 37px total/.test(v.reason) && !/%/.test(v.reason); })(), JSON.stringify(verdictJson('px1', 'a2')));
resetRefArgs();
r = await refRound('px2', 'a3', [], { STUB_PIXEL_JSON: aligned(16.62, 1.23, steps([103, -5], [2125, -20], [3300, -12]), { height: 5000 }), GATE_REGIME: 'published' });
check('published regime: the bound is the larger of 8 and 1 % of the origin height — 37 px pass on a 5000 px origin (bound 50)', r.status === 0 && / --offset-max published /.test(lines(pixelArgsFile)[0] || '') && /\(bound 50px\)$/m.test(r.stdout) && verdictJson('px2', 'a3').stage === 'published' && verdictJson('px2', 'a3').pixel.offsetBound === 50, `status ${r.status} ${r.stdout}`);
resetRefArgs();
r = await refRound('px3', 'a4', [], { STUB_PIXEL_JSON: aligned(16.62, 1.23, steps([103, -5], [2125, -20], [3300, -12])), GATE_OFFSET_MAX_PX: '40' });
check('GATE_OFFSET_MAX_PX overrides the bound: 37 px pass at 40', r.status === 0 && / --offset-max 40 /.test(lines(pixelArgsFile)[0] || '') && /\(bound 40px\)$/m.test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await refRound('px3', 'a5', [], { STUB_PIXEL_JSON: aligned(16.62, 1.23, steps([103, -5], [2125, -20], [3300, -12])), GATE_OFFSET_MAX_PX: '36' });
check('… and fail at 36', r.status === 2, `status ${r.status}`);
r = await refRound('px3', 'a6', [], { GATE_OFFSET_MAX_PX: '8px' });
check('GATE_OFFSET_MAX_PX must be a whole number (exit 125)', r.status === 125 && /GATE_OFFSET_MAX_PX must be a whole number of px/.test(r.stderr), `status ${r.status} ${r.stderr}`);
r = await refRound('px4', 'a7', [], { STUB_PIXEL_JSON: aligned(30.1, 12, steps([103, 4])) });
check('the aligned % over the threshold fails on the percentage', r.status === 2 && /^pixel: aligned 12\.00% \(unaligned 30\.10%\) > 10% → FAIL — offsets: 1 step\(s\), 4px total/m.test(r.stdout) && verdictJson('px4', 'a7').pixel.reason === 'pixel aligned 12% (unaligned 30.1%) > 10%', `status ${r.status} ${r.stdout}`);
r = await refRound('px5', 'a8', [], { STUB_PIXEL_JSON: JSON.stringify({ pct: 16.62, aligned: { used: false, pct: 16.62, steps: [], reason: 'aligned number not lower than the unaligned one' } }) });
check('alignment not used (used: false) → the unaligned number rules: 16.62 % fails', r.status === 2 && /^pixel: 16\.62% \(alignment not used — the unaligned number rules\) > 10% → FAIL$/m.test(r.stdout) && verdictJson('px5', 'a8').pixel.alignedUsed === false && verdictJson('px5', 'a8').pixel.alignedPct === 16.62, `status ${r.status} ${r.stdout}`);
r = await refRound('px5', 'a9', [], { STUB_PIXEL_JSON: JSON.stringify({ pct: 9.5, aligned: { used: false, pct: 9.5, steps: [] } }) });
check('… and 9.5 % passes, as it did before alignment', r.status === 0, `status ${r.status}`);
r = await refRound('px6', 'a10', [], { STUB_PIXEL_JSON: aligned(9.5, 4, steps([400, 3], [900, -5])) });
check('a round that passed on the unaligned number (9.5 %) still passes when its offsets are within the bound (8 px) …', r.status === 0, `status ${r.status} ${r.stdout}`);
r = await refRound('px6', 'a11', [], { STUB_PIXEL_JSON: aligned(9.5, 4, steps([400, 3], [900, -6])) });
check('… and fails only when they exceed it (9 px): the one new way to fail', r.status === 2 && /→ FAIL — offsets: 2 step\(s\), 9px total/.test(r.stdout), `status ${r.status} ${r.stdout}`);
r = await refRound('px7', 'a12', [], { STUB_PIXEL_JSON: aligned(16.62, 1.23, steps([1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1], [8, 1], [9, 1], [10, 1], [11, 1], [12, 1], [13, 1], [14, 1])) });
check('the offset list prints at most 6 steps then "… N more"; the verdict file keeps at most 12', r.status === 2 && /\+1px at y=6, … 8 more \(bound 8px\)$/m.test(r.stdout) && verdictJson('px7', 'a12').pixel.steps.length === 12 && verdictJson('px7', 'a12').pixel.totalShift === 14, `status ${r.status} ${r.stdout}`);
r = await refRound('px8', 'a13', [], { STUB_PIXEL_JSON: aligned(16.62, 1.23, steps([103, -5])), STUB_OVERFLOW: '13' });
check('the overflow assert still rules an aligned PASS (exit 2), and the verdict file records the round\'s exit', r.status === 2 && /OVERFLOW at 1440/.test(r.stdout) && verdictJson('px8', 'a13').pixel.rc === 2, `status ${r.status}`);
unlinkSync(join(replica, 'rounds.mjs'));

// Disk (#156): a round keeps only its own diff image and vdiff dir; earlier rounds keep their text and JSON evidence.
const kdir = join(root, 'stardust/replica/gates/keep-1440');
for (const d of ['vdiff-iter1', 'vdiff-iter2']) mkdirSync(join(kdir, d), { recursive: true });
for (const f of ['diff-iter1.png', 'diff-final.png', 'vdiff-iter1/eds-full.png', 'diff-iter2.png', 'vdiff-iter2/eds-full.png', 'chrome-header-diff.png', 'baseline.png', 'verdict-iter1.json', 'content-diff-iter1.txt']) writeFileSync(join(kdir, f), 'x');
r = await gate(['keep', 'https://live.example/', buildUrl, '1440', 'iter2', '--marker', 'home-proposed']);
check('a round deletes earlier rounds\' diff-*.png and vdiff-*/ before comparing', r.status === 0 && !existsSync(join(kdir, 'diff-iter1.png')) && !existsSync(join(kdir, 'diff-final.png')) && !existsSync(join(kdir, 'vdiff-iter1')), `status ${r.status} ${readdirSync(kdir).join(',')}`);
check('… and keeps everything else: this round\'s diff and vdiff dir, captures, the frozen baseline, crop diffs, earlier rounds\' text and JSON', ['diff-iter2.png', 'vdiff-iter2/eds-full.png', 'live.png', 'build.png', 'baseline.png', 'chrome-header-diff.png', 'verdict-iter1.json', 'content-diff-iter1.txt'].every((f) => existsSync(join(kdir, f))), readdirSync(kdir).join(','));

server.close();
if (process.env.GATE_TEST_KEEP) console.log(`sandbox kept at ${root} (build url was ${buildUrl})`);
else rmSync(root, { recursive: true, force: true });
console.log(failures ? `\n${failures} check(s) failed` : '\nall checks passed');
process.exit(failures ? 1 : 0);

skills

README.md

tile.json