// verify.mjs: the Tier A offline mirror.
//
//   node research/the-clock-in-the-glass/verify.mjs
//
// It imports the SHIPPED reduction (public/strata/the-clock-in-the-glass/vsync.mjs) and
// the SHIPPED traces (data.js), and re-derives every number the page prints. It does not
// hold a copy of any rule the page holds: if the estimator is wrong, this file is wrong
// in the same direction and the assertions below still fail, because they are checked
// against ground truth that lives outside the estimator entirely, in the modelines.
//
// The assertions that CAN fail, and which the page would be wrong without:
//   - each virtual-display capture must land within its own measured scatter of a value
//     predicted from the modeline by a model with no free parameters;
//   - the two modelines chosen to break that model must behave as they were predicted to,
//     one with no offset and one with the largest offset the model can ask for;
//   - Firefox must land on the modeline and NOT on the model;
//   - each negative control must be refused, and by the specific gate it was built for;
//   - the clamped control must report a perfect coherence and a vanishing interval
//     around a wrong answer, which is the whole reason the interval is not a gate.

import { readFileSync } from 'node:fs';
import { execSync } from 'node:child_process';
import {
  NTSC, QUANTA, GATES, estimate, gate, timestampQuantumUs,
  modelineHz, truncatedHz, framesToSeparate, CANDIDATES,
} from '../../public/strata/the-clock-in-the-glass/vsync.mjs';
import { TRACES, FRAMES, decode } from '../../public/strata/the-clock-in-the-glass/data.js';

const dataDir = new URL('./data/', import.meta.url).pathname;
let fails = 0, n = 0;
const ok = (name, cond, detail = '') => {
  n++; if (!cond) fails++;
  console.log(`${cond ? '  ok  ' : 'FAIL  '}${name}${detail ? '   ' + detail : ''}`);
};

const decoded = TRACES.map((t) => ({ meta: t, ts: decode(t) }));
const scored = decoded.map(({ meta, ts }) => ({ meta, ts, r: estimate(ts), g: gate(estimate(ts)) }));
const by = Object.fromEntries(scored.map((s) => [s.meta.label, s]));

function spreadAt(ts, N) {
  const est = [];
  for (let s = 0; s + N <= ts.length; s += N) {
    const rr = estimate(ts.slice(s, s + N));
    if (rr.hz != null && rr.R > GATES.minR) est.push(rr.hz);
  }
  if (est.length < 3) return null;
  est.sort((a, b) => a - b);
  const m = est[est.length >> 1];
  return Math.max(Math.abs(est[est.length - 1] - m), Math.abs(m - est[0]));
}

console.log('\n── provenance ──');
for (const { meta, ts } of decoded) {
  const lab = JSON.parse(readFileSync(`${dataDir}${meta.label}.json`, 'utf8')).ts.slice(0, FRAMES);
  ok(`${meta.label}: shipped timestamps are bit-identical to the lab file`,
    lab.length === ts.length && lab.every((v, i) => v === ts[i]),
    `${ts.length} frames`);
}

console.log('\n── the NTSC arithmetic, against the standard ──');
const routes = Object.values(NTSC.routes);
ok('three independent routes give bit-identical doubles', routes.every((v) => v === routes[0]), routes[0].toFixed(9));
ok('SMPTE 170M 11.1: fsc = 315/88 MHz', Math.abs(NTSC.fscMHz - 315 / 88) < 1e-15, NTSC.fscMHz.toFixed(9));
ok('SMPTE 170M 11.2: fH truncates to the printed 15,734.265 ...Hz',
  Math.floor(NTSC.fH * 1000) / 1000 === 15734.265, NTSC.fH.toFixed(6));
ok('SMPTE 170M 11.3: fV rounds to the printed 59.9400599 4...Hz',
  NTSC.fV.toFixed(7) === '59.9400599', NTSC.fV.toFixed(10));
ok('BT.1700 Part B Table 1, 525 PAL column, writes the field rate as (60/1.001), and the computed value is that',
  Math.abs(NTSC.fV - 60 / 1.001) < 1e-12, `${NTSC.fV.toFixed(15)} against ${(60 / 1.001).toFixed(15)}`);
ok('it is bit-identical to 60000/1001, and only 7e-15 from 60/1.001, which is the division order',
  NTSC.fV === 60000 / 1001 && NTSC.fV !== 60 / 1.001,
  `difference ${Math.abs(NTSC.fV - 60 / 1.001).toExponential(2)} Hz`);
const gapUs = (1000 / NTSC.fV - 1000 / 60) * 1000;
ok('the 59.94 / 60.00 period gap is 16.7 microseconds', gapUs.toFixed(1) === '16.7', gapUs.toFixed(4));

console.log('\n── the timer quantum, recovered from the timestamps ──');
for (const [label, want] of [['sixty', 100], ['vkms-firefox-sixty', 20], ['headless-webkit', 1000], ['headless-firefox', 20]]) {
  ok(`${label}: gcd of the timestamps is ${want} us`, by[label].r.quantumUs === want, String(by[label].r.quantumUs));
}
ok('every recovered quantum on a capture with an unclamped clock is a published constant',
  scored.filter((s) => s.meta.kind !== 'control' && s.meta.label !== 'firefox-rfp')
    .every((s) => QUANTA.some((q) => q.us === s.r.quantumUs)));
ok('and the fingerprinting-resistant capture lands on a grain that is on NONE of them: measured here, not cited',
  by['firefox-rfp'].r.quantumUs === 16667 && !QUANTA.some((q) => q.us === by['firefox-rfp'].r.quantumUs),
  String(by['firefox-rfp'].r.quantumUs) + ' us');
ok('the quantum recovery is not vacuous: it returns null on unquantised data',
  timestampQuantumUs([0, 1.000001, 2.000002, 3.000005, 4.000011]) === null
  && timestampQuantumUs([0, 0.1, 0.2, 0.5, 1.7]) === 100);

console.log('\n── the golden traces against their modelines (the assertion that can fail) ──');
const rows = scored.filter((s) => s.meta.modeline && s.r.hz != null).map((s) => {
  const truth = modelineHz(s.meta.modeline.clock_kHz, s.meta.modeline.htotal, s.meta.modeline.vtotal);
  const model = truncatedHz(truth);
  const spread = spreadAt(s.ts, 300);
  return { ...s.meta, truth, model, meas: s.r.hz, spread, offset: s.r.hz - truth, predicted: model - truth };
});
ok('there are at least six captures with a modeline ground truth', rows.length >= 6, String(rows.length));
for (const t of rows.filter((x) => x.engine === 'chromium')) {
  ok(`${t.label}: Chromium lands within 3x its scatter of the rounded-period model`,
    Math.abs(t.meas - t.model) < 3 * t.spread,
    `meas ${t.meas.toFixed(6)}, model ${t.model.toFixed(6)}, scatter ${t.spread.toFixed(6)}`);
}
const ipRow = rows.find((t) => t.label === 'integer-period');
ok('the whole-microsecond modeline was predicted to show NO offset', ipRow.predicted === 0, (1e6 / ipRow.truth).toFixed(4) + ' us');
ok('and it shows none', Math.abs(ipRow.offset) < 2 * ipRow.spread,
  `offset ${ipRow.offset.toFixed(6)} against a scatter of ${ipRow.spread.toFixed(6)}`);
const wtRow = rows.find((t) => t.label === 'worst-truncation');
ok('the rounding-breaker modeline was predicted to show the largest possible offset',
  wtRow.predicted > 0.003, wtRow.predicted.toFixed(6) + ' Hz');
ok('and its measured offset is resolved from zero by more than 10x its scatter',
  wtRow.offset > 10 * wtRow.spread, `${wtRow.offset.toFixed(6)} against ${wtRow.spread.toFixed(6)}`);
ok('every Chromium capture sits ABOVE its modeline, the only direction the model allows',
  rows.filter((t) => t.engine === 'chromium' && t.predicted > 1e-6).every((t) => t.offset > 0));
for (const t of rows.filter((x) => x.engine === 'firefox')) {
  ok(`${t.label}: Firefox lands on the modeline, not on the model`,
    Math.abs(t.meas - t.truth) < Math.abs(t.meas - t.model) / 3,
    `off truth ${t.offset.toFixed(6)}, off model ${(t.meas - t.model).toFixed(6)}`);
}

console.log('\n── the negative controls ──');
const c60 = by['ctl-exact-60'];
ok('an exact 60.000000 Hz trace comes back 60, not 59.94',
  Math.abs(c60.r.hz - 60) < 0.001 && Math.abs(c60.r.hz - NTSC.fV) > 0.05, c60.r.hz.toFixed(6));
ok('and it is the one control that passes every gate', c60.g.pass);
const cs = by['ctl-shuffled'];
ok('a shuffled trace is refused', !cs.g.pass);
ok('and by monotonicity, not by coherence', cs.g.fail.some((f) => f.code === 'not-monotonic'));
{
  // The withdrawn claim, checked rather than repeated: coherence does not see a shuffle.
  const src = c60.ts, T = c60.r.periodMs;
  const sh = src.slice(); for (let i = sh.length - 1; i > 0; i--) { const j = (i * 7919 + 13) % (i + 1); const t = sh[i]; sh[i] = sh[j]; sh[j] = t; }
  const coh = (a) => { let x = 0, y = 0; for (const v of a) { const p = 2 * Math.PI * v / T; x += Math.cos(p); y += Math.sin(p); } return Math.hypot(x, y) / a.length; };
  ok('coherence is invariant under shuffling to floating-point summation order',
    Math.abs(coh(src) - coh(sh)) < 1e-12, `difference ${Math.abs(coh(src) - coh(sh)).toExponential(2)}`);
}
const cw = by['ctl-random-walk'];
ok('random intervals with the right mean fail the coherence gate',
  !cw.g.pass && cw.g.fail.some((f) => f.code === 'coherence'), 'R = ' + cw.r.R.toFixed(4));
const cc = by['ctl-clamped'];
ok('the clamped clock is refused', !cc.g.pass);
ok('and by the quantum gate, and by nothing else',
  cc.g.fail.length === 1 && cc.g.fail[0].code === 'quantum', cc.g.fail.map((f) => f.code).join(','));
ok('its coherence is a perfect 1.000000', cc.r.R.toFixed(6) === '1.000000');
ok('its interval vanishes at every printed decimal', cc.r.hzCI95 < 1e-12, cc.r.hzCI95.toExponential(2));
ok('and its answer is wrong by more than 0.01 Hz',
  Math.abs(cc.r.hz - cc.meta.truth) > 0.01, `${cc.r.hz.toFixed(6)} against ${cc.meta.truth}`);
ok('THE POINT: neither coherence nor the interval catches the clamped clock',
  cc.r.R > GATES.minR && cc.r.hzCI95 < 1e-12 && !cc.g.pass
  && cc.g.fail.every((f) => f.code !== 'coherence'));
const cb = by['ctl-busy-144'];
ok('the thrashing 144 Hz trace is refused', !cb.g.pass);
ok('and its interval is tiny while its error is enormous',
  cb.r.hzCI95 < 0.05 && Math.abs(cb.r.hz - cb.meta.truth) > 1,
  `±${cb.r.hzCI95.toFixed(6)} around an error of ${(cb.r.hz - cb.meta.truth).toFixed(2)} Hz`);
ok('a display that changes rate mid-trace is refused', !by['ctl-vrr'].g.pass);
ok('a real WebKit capture with no display is refused',
  !by['headless-webkit'].g.pass, 'R = ' + by['headless-webkit'].r.R.toFixed(4));

console.log('\n── the real fingerprinting-resistant capture, and the defect it found ──');
{
  const fr = by['firefox-rfp'];
  const codes = fr.g.fail.map((f) => f.code);
  ok('the privacy.resistFingerprinting capture is refused and prints no rate',
    !fr.g.pass && fr.r.hz != null, `it would have printed ${fr.r.hz.toFixed(6)} Hz`);
  ok('and the reason given is the coarse clock, not a reversal',
    codes.includes('quantum') && !codes.includes('not-monotonic'), codes.join(','));
  ok('its timestamps never step backwards', fr.r.nonMonotonic === 0, String(fr.r.nonMonotonic));
  ok('they repeat instead, and the repeats are counted as their own fault',
    fr.r.tied > 0 && codes.includes('tied'), `${fr.r.tied} repeats in ${fr.r.n} frames`);
  ok('THE DEFECT, DEMONSTRATED: a monotonicity test written as (delta <= 0) counts every one of those repeats as a step backwards',
    (() => { let bad = 0; for (let i = 1; i < fr.ts.length; i++) if (fr.ts[i] - fr.ts[i - 1] <= 0) bad++; return bad > 0 && bad >= fr.r.tied; })(),
    'which is what this page did, and what it told those readers');
  ok('the clamp is a whole frame wide at the rate the trace appears to report',
    fr.r.quantumUs / (fr.r.periodMs * 1000) >= 0.999, (fr.r.quantumUs / (fr.r.periodMs * 1000)).toFixed(4) + ' of a frame');
  ok('the tie gate refuses no trace that was not already being refused, so it is a better reason and not a new rule',
    scored.every((s) => {
      const codes2 = s.g.fail.map((f) => f.code);
      return !codes2.includes('tied') || codes2.some((c) => c !== 'tied');
    }), 'every trace the tie gate fires on is refused by something else too');
}

console.log('\n── the gates are not vacuous ──');
for (const code of ['coherence', 'quantum', 'not-monotonic', 'slip', 'tied']) {
  const fires = scored.filter((s) => !s.g.pass && s.g.fail.some((f) => f.code === code)).length;
  ok(`the ${code} gate fires on some traces and not on all`, fires >= 1 && fires < scored.length, `${fires}/${scored.length}`);
}
ok('at least one trace passes and at least one is refused',
  scored.some((s) => s.g.pass) && scored.some((s) => !s.g.pass),
  `${scored.filter((s) => s.g.pass).length} pass, ${scored.filter((s) => !s.g.pass).length} refused`);

console.log('\n── the kept error ──');
{
  const ts = by['vkms-firefox-worst'].ts;
  const bad = estimate(ts, { seed: 'median' }), good = estimate(ts);
  ok('a median seed refuses a clean Firefox capture', !gate(bad).pass, 'R = ' + bad.R.toFixed(4));
  ok('a mean seed accepts it', gate(good).pass, 'R = ' + good.R.toFixed(4));
  ok('the two disagree by more than 1 Hz', Math.abs(bad.hz - good.hz) > 1, `${bad.hz.toFixed(4)} vs ${good.hz.toFixed(4)}`);
  const ctlSame = scored.filter((s) => s.meta.kind === 'control')
    .every((s) => gate(estimate(s.ts, { seed: 'median' })).pass === s.g.pass);
  ok('and NO synthetic control would have caught it, over ALL of them, not only the ones that pass', ctlSame,
    `${scored.filter((s) => s.meta.kind === 'control').length} controls reach the same verdict under both seeds`);
  const flips = scored.filter((s) => s.g.pass && !gate(estimate(s.ts, { seed: 'median' })).pass);
  ok('while the real captures DO catch it, and this is the count the page prints',
    flips.length > 0 && flips.every((s) => s.meta.kind !== 'control'),
    flips.map((s) => `${s.meta.label} R ${estimate(s.ts, { seed: 'median' }).R.toFixed(4)}`).join(', '));
}

console.log('\n── the separation arithmetic ──');
{
  const nFrames = framesToSeparate(NTSC.fV, 60, 300);
  ok('separating 59.94 from 60.00 at 300 us of jitter needs under 100 frames', nFrames < 100, Math.ceil(nFrames) + ' frames');
  ok('and separating 60.000000 from the VESA DMT 60.003840 needs more than that',
    framesToSeparate(60, 65e6 / (1344 * 806), 300) > nFrames,
    Math.ceil(framesToSeparate(60, 65e6 / (1344 * 806), 300)) + ' frames');
  ok('the candidate list holds both the NTSC and the exact-60 rate',
    CANDIDATES.some((c) => c.hz === 60000 / 1001) && CANDIDATES.some((c) => c.hz === 60));
}

console.log('\n── the interval, calibrated against measured scatter ──');
{
  const ratios = scored.filter((s) => s.meta.kind === 'measured').map((s) => {
    const sp = spreadAt(s.ts, 300);
    const cis = [];
    for (let i = 0; i + 300 <= s.ts.length; i += 300) { const rr = estimate(s.ts.slice(i, i + 300)); if (rr.hzCI95 != null && rr.R > GATES.minR) cis.push(rr.hzCI95); }
    cis.sort((a, b) => a - b);
    return sp && cis.length ? sp / cis[cis.length >> 1] : null;
  }).filter((x) => x != null);
  const worst = Math.max(...ratios);
  ok('the reported interval is optimistic on at least one real capture', worst > 2, `worst ratio ${worst.toFixed(1)}`);
  ok('and pessimistic on at least one other, so the page must print a range and not a factor',
    Math.min(...ratios) < 1, `best ratio ${Math.min(...ratios).toFixed(2)}`);
  // The old form of this asserted only that a scatter had been computed, which could not
  // fail for the reason its name gave. This reads the code that renders section 6 and
  // looks for the interval in it.
  const pageSrc = readFileSync(new URL('../../public/strata/the-clock-in-the-glass/page.js', import.meta.url).pathname, 'utf8');
  const fillTrunc = pageSrc.slice(pageSrc.indexOf('function fillTrunc'), pageSrc.indexOf('const ML_PRESETS'));
  ok('and section 6 is rendered by code that never touches the interval',
    fillTrunc.length > 200 && !/\bci\b|hzCI95/.test(fillTrunc),
    `${fillTrunc.length} characters of fillTrunc(), no reference to ci or hzCI95`);
  console.log(`        ratios: ${ratios.map((x) => x.toFixed(2)).join(', ')}`);
}

console.log('\n── what the prose says about the captures, against the capture files ──');
{
  const vkms = ['sixty', 'fiftynineninefour', 'worst-truncation', 'integer-period', 'dmt-1024x768',
    'onetwenty', 'vkms-firefox-sixty', 'vkms-firefox-worst'];
  const lab = Object.fromEntries(vkms.map((l) => [l, JSON.parse(readFileSync(`${dataDir}${l}.json`, 'utf8'))]));
  const total = vkms.reduce((a, l) => a + lab[l].spanSeconds, 0);
  ok('the eight virtual-display captures total a little under four minutes, which is what the page says',
    total > 200 && total < 240, `${total.toFixed(1)} s`);
  ok('and it is nowhere near the half hour an earlier draft of the page claimed', total < 600, `${(total / 60).toFixed(2)} minutes`);
  const modeId = (l) => (lab[l].xrandr.match(/Virtual-1 connected[^\n]*\((0x[0-9a-f]+)\)/) || [])[1];
  for (const [a, b] of [['sixty', 'vkms-firefox-sixty'], ['worst-truncation', 'vkms-firefox-worst']]) {
    ok(`${a} and ${b} were captured on the SAME X mode, by id, so nothing was reprogrammed between the two engines`,
      modeId(a) != null && modeId(a) === modeId(b), `${modeId(a)} and ${modeId(b)}`);
    const gapMin = Math.abs(Date.parse(lab[b].capturedAt) - Date.parse(lab[a].capturedAt)) / 60000;
    ok(`and they are minutes apart in one session, not the same minute the page used to claim`,
      gapMin > 1 && gapMin < 30, `${gapMin.toFixed(1)} minutes apart`);
  }
  ok('the six distinct modelines the page counts are actually six distinct modelines',
    new Set(vkms.map((l) => `${lab[l].modeline.clock_kHz}/${lab[l].modeline.htotal}/${lab[l].modeline.vtotal}`)).size === 6);
  ok('Chromium covers all six of them and Firefox covers two',
    new Set(vkms.filter((l) => lab[l].engine === 'chromium').map((l) => lab[l].modeline.clock_kHz)).size === 6
    && new Set(vkms.filter((l) => lab[l].engine === 'firefox').map((l) => lab[l].modeline.clock_kHz)).size === 2);
}

console.log('\n── the copyrighted sources are quoted, not redistributed ──');
{
  const pageSrc = readFileSync(new URL('../../public/strata/the-clock-in-the-glass/page.js', import.meta.url).pathname, 'utf8');
  const pdfs = execSync('git ls-files research/the-clock-in-the-glass/ | grep -ci "\\.pdf$" || true',
    { cwd: new URL('../../', import.meta.url).pathname, encoding: 'utf8' }).trim();
  ok('no standards PDF is committed under research/the-clock-in-the-glass/', pdfs === '0', `${pdfs} committed`);
  ok('and the page does not claim one is',
    !/committed to research\/the-clock-in-the-glass\/data/.test(pageSrc),
    'the Sources entry says quoted and not redistributed');
}

console.log(`\n${n - fails} / ${n} offline checks pass`);
process.exit(fails === 0 ? 0 : 1);
