#!/usr/bin/env node
// run-harness.js — drives both v5.5.93-3 (shipped, broken) and v5.5.93-4
// (draft, fixed) parsers through all 9 Verdict Table fixtures and prints
// a comparison. Exits 1 if v5.5.93-4 fails any expected-pathway assertion.
//
// This is the sandbox equivalent of "sign a PNG in the app, navigate to it,
// read the badge" — but deterministic, fast, and zero-network.

'use strict';

const { FIXTURES, DB_RECORDS } = require('./fixtures');
const { ServerMock } = require('./server-mock');
const V3 = require('./parsers-v5.5.93-3');
const V4 = require('./parsers-v5.5.93-4');

const PAD = (s, n) => String(s).padEnd(n);

function runAll(parsersModule) {
  const baseServer = new ServerMock(DB_RECORDS);
  const results = FIXTURES.map(fx => {
    // Wrap server so per-fixture flags can alter behavior (e.g. force
    // fast-path miss to exercise the LSB parse path end-to-end).
    const server = {
      fastPath: (h) => fx.disableFastPath ? { found: false } : baseServer.fastPath(h),
      verify:   (args) => baseServer.verify(args),
      pHashLookup: (p) => baseServer.pHashLookup(p),
    };
    let verdict;
    try {
      verdict = parsersModule.runVerify(fx.input, server);
    } catch (e) {
      verdict = { verdict: 'ERROR', terminal: 0, error: e.message };
    }
    return { name: fx.name, expected: fx.expected, got: verdict };
  });
  return results;
}

function fmt(row) {
  const ok = row.got.terminal === row.expected.terminal &&
             row.got.verdict  === row.expected.verdict;
  return {
    ok,
    line: `  ${ok ? 'PASS' : 'FAIL'}  ${PAD(row.name, 32)} ` +
          `expected=T${row.expected.terminal}:${PAD(row.expected.verdict, 18)} ` +
          `got=T${row.got.terminal}:${row.got.verdict}`,
  };
}

console.log('═══════════════════════════════════════════════════════════════════');
console.log(' v5.5.93-3  (shipped, broken)  — baseline run');
console.log('═══════════════════════════════════════════════════════════════════');
const v3Results = runAll(V3);
let v3Pass = 0, v3Fail = 0;
for (const r of v3Results) {
  const { ok, line } = fmt(r);
  console.log(line);
  if (ok) v3Pass++; else v3Fail++;
}
console.log(`  → ${v3Pass} pass, ${v3Fail} fail`);

console.log('');
console.log('═══════════════════════════════════════════════════════════════════');
console.log(' v5.5.93-4  (draft, fixed)     — verification run');
console.log('═══════════════════════════════════════════════════════════════════');
const v4Results = runAll(V4);
let v4Pass = 0, v4Fail = 0;
for (const r of v4Results) {
  const { ok, line } = fmt(r);
  console.log(line);
  if (ok) v4Pass++; else v4Fail++;
}
console.log(`  → ${v4Pass} pass, ${v4Fail} fail`);

console.log('');
console.log('─── summary ───────────────────────────────────────────────────────');
console.log(` v5.5.93-3 baseline: ${v3Pass}/${FIXTURES.length} terminal matches`);
console.log(` v5.5.93-4 fixed   : ${v4Pass}/${FIXTURES.length} terminal matches`);

// We EXPECT v5.5.93-3 to fail the signed/altered/resigned/creator/transformed
// and legacy fixtures — all of them rely on steg_id extraction, which is
// gated by _UUID_RE which cannot match canonical 32-hex.
// We EXPECT v5.5.93-4 to pass all 9.
if (v4Fail > 0) {
  console.error('');
  console.error('❌ v5.5.93-4 FAILED — fix is incomplete. Do not ship.');
  process.exit(1);
}
if (v3Fail === 0) {
  console.error('');
  console.error('❌ v5.5.93-3 UNEXPECTEDLY PASSED — test is not exercising the bug.');
  process.exit(2);
}
console.log('');
console.log('✅ v5.5.93-4 fix verified. Baseline correctly reproduces user\'s bug.');
