#!/usr/bin/env node
// verify.mjs — every number on /strata/nine-years-of-green/, recomputed from the
// primary source, which is Unicode's own BidiTest.txt rather than any copy of ours.
//
//   node verify.mjs                  # fetch Unicode 17.0.0's BidiTest.txt and check
//   node verify.mjs --version 15.0.0 # the version servo/unicode-bidi ships
//   node verify.mjs --file <path>    # use a local copy instead of the network
//   node verify.mjs --selftest       # prove the checks can fail
//
// It needs the network exactly once, for unicode.org. It does NOT need Rust, cargo
// or a checkout of unicode-bidi: the counting half of the finding is a property of
// the data file and the arithmetic of two operators, and that is what this checks.
// The half that needs a Rust toolchain (770241 comparisons actually executing and
// actually passing) is reproduce.sh, which is a different and heavier program.
//
// The cache lives beside this file so a second run is offline.

import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const HERE = dirname(fileURLToPath(import.meta.url));
const argv = process.argv.slice(2);
const arg = (name, dflt) => {
  const i = argv.indexOf(name);
  return i >= 0 && argv[i + 1] ? argv[i + 1] : dflt;
};
const SELFTEST = argv.includes('--selftest');
const VERSION = arg('--version', '17.0.0');
const LOCAL = arg('--file', null);

// ---------------------------------------------------------------- expectations
// Every figure the page states. Held here so a failure names the claim, not a line.
const EXPECT = {
  dataLines: 490846,
  perDirectionSpecified: 256747,
  totalSpecified: 770241,
  upstreamExecutes: 256747,
  upstreamExecutesLTR: 0,
  upstreamExecutesRTL: 0,
  histogram: { 2: 51303, 3: 182796, 4: 182796, 5: 51303, 7: 22648 },
  allThreeDirections: 22648, // bitset 7
  swapControlFailuresPerDirection: 234099, // 256747 - 22648
  cParseDiffersFromIntent: 8, // of 21 (bitset, bit) rows
  cVsRustDisagreements: 0, // of 21
  characterTestRows15: 91709,
  bugIntroduced: '2017-05-11',
  daysStanding: 3404, // to 2026-09-05
};

// ---------------------------------------------------------------- the source
async function getData() {
  if (LOCAL) return readFileSync(LOCAL, 'utf8');
  const url = `https://www.unicode.org/Public/${VERSION}/ucd/BidiTest.txt`;
  const cacheDir = join(HERE, '.cache');
  const cache = join(cacheDir, `BidiTest-${VERSION}.txt`);
  if (existsSync(cache)) return readFileSync(cache, 'utf8');
  process.stderr.write(`fetching ${url} ...\n`);
  const res = await fetch(url);
  if (!res.ok) throw new Error(`${url} -> HTTP ${res.status}`);
  const text = await res.text();
  mkdirSync(cacheDir, { recursive: true });
  writeFileSync(cache, text);
  return text;
}

// ---------------------------------------------------------------- the counting
function countBidiTest(text) {
  const hist = new Map();
  let dataLines = 0;
  const specified = [0, 0, 0];
  const upstream = [0, 0, 0]; // Rust's parse of `bitset & (1u8 << bit) == 1`
  for (const raw of text.split('\n')) {
    const line = raw.trim();
    if (!line || line[0] === '#' || line[0] === '@') continue;
    const parts = line.split(';');
    if (parts.length < 2) continue;
    const bitset = Number.parseInt(parts[1].trim(), 10);
    if (!Number.isInteger(bitset)) continue;
    dataLines++;
    hist.set(bitset, (hist.get(bitset) ?? 0) + 1);
    for (let bit = 0; bit < 3; bit++) {
      if (bitset & (1 << bit)) specified[bit]++;
      if ((bitset & (1 << bit)) === 1) upstream[bit]++; // the defect, modelled
    }
  }
  return { dataLines, specified, upstream, hist };
}

// ---------------------------------------------------------------- the operators
// Rust: `&` binds tighter than `==`   ->  (bitset & (1 << bit)) == 1
// C:    `==` binds tighter than `&`   ->  bitset & ((1 << bit) == 1)
// Intent:                                 (bitset & (1 << bit)) != 0
const rustParse = (bitset, bit) => ((bitset & (1 << bit)) === 1);
const cParse = (bitset, bit) => (bitset & (((1 << bit) === 1) ? 1 : 0)) !== 0;
const intent = (bitset, bit) => (bitset & (1 << bit)) !== 0;

function operatorTable() {
  let cDiffers = 0, disagree = 0, rows = 0;
  for (let bitset = 1; bitset < 8; bitset++) {
    for (let bit = 0; bit < 3; bit++) {
      rows++;
      if (cParse(bitset, bit) !== intent(bitset, bit)) cDiffers++;
      if (cParse(bitset, bit) !== rustParse(bitset, bit)) disagree++;
    }
  }
  return { rows, cDiffers, disagree };
}

// ---------------------------------------------------------------- the harness
const results = [];
function check(name, actual, expected) {
  const ok = JSON.stringify(actual) === JSON.stringify(expected);
  results.push({ name, ok, actual, expected });
  return ok;
}

const text = await getData();
const src = LOCAL ?? `unicode.org BidiTest ${VERSION}`;
const { dataLines, specified, upstream, hist } = countBidiTest(text);
const ops = operatorTable();
const histObj = Object.fromEntries([...hist.entries()].sort((a, b) => a[0] - b[0]));

check('BidiTest.txt data lines', dataLines, EXPECT.dataLines);
check('specified, auto-LTR', specified[0], EXPECT.perDirectionSpecified);
check('specified, explicit LTR', specified[1], EXPECT.perDirectionSpecified);
check('specified, explicit RTL', specified[2], EXPECT.perDirectionSpecified);
check('specified, total', specified[0] + specified[1] + specified[2], EXPECT.totalSpecified);
check("upstream's filter runs, auto-LTR", upstream[0], EXPECT.upstreamExecutes);
check("upstream's filter runs, LTR", upstream[1], EXPECT.upstreamExecutesLTR);
check("upstream's filter runs, RTL", upstream[2], EXPECT.upstreamExecutesRTL);
check('bitset histogram', histObj, EXPECT.histogram);
check('bitsets 1 and 6 never occur', [hist.get(1) ?? 0, hist.get(6) ?? 0], [0, 0]);
check('lines specifying all three directions', hist.get(7) ?? 0, EXPECT.allThreeDirections);
check(
  'swap control: failures per direction = specified - all-three',
  specified[1] - (hist.get(7) ?? 0),
  EXPECT.swapControlFailuresPerDirection,
);
check('fraction executed is one third', upstream[0] * 3, EXPECT.totalSpecified);
check('C parse differs from intent, of 21 rows', ops.cDiffers, EXPECT.cParseDiffersFromIntent);
check('C parse vs Rust parse disagreements, of 21', ops.disagree, EXPECT.cVsRustDisagreements);
check('operator table rows', ops.rows, 21);

// The negative control. A check that cannot fail is not a check, so prove these can.
if (SELFTEST) {
  const before = results.length;
  // NB the data lines read `LRE; 7`, with a space. The first version of this
  // mutation matched /;7$/ and therefore matched nothing, and reported a green
  // control while changing not one byte. A control that cannot fire is worse than
  // no control, because it looks like evidence.
  const mutation = /;\s*7$/gm;
  if (!mutation.test(text)) throw new Error('selftest mutation matches nothing; it would prove nothing');
  mutation.lastIndex = 0;
  const mutated = countBidiTest(text.replace(mutation, '; 3'));
  check(
    'SELFTEST: mutating every bitset 7 to 3 must change the RTL count',
    mutated.specified[2] !== specified[2],
    true,
  );
  check(
    'SELFTEST: a wrong expectation must be reported as wrong',
    dataLines === EXPECT.dataLines + 1,
    false,
  );
  check('SELFTEST: the C parse must not equal the intent everywhere', ops.cDiffers > 0, true);
  process.stderr.write(`selftest added ${results.length - before} checks\n`);
}

// ---------------------------------------------------------------- the report
const pad = (s, n) => String(s).padEnd(n);
console.log(`\nsource: ${src}`);
console.log(`\n${pad('direction', 26)}${'specified'.padStart(12)}${'upstream runs'.padStart(16)}`);
for (const [i, name] of ['auto-LTR', 'explicit LTR', 'explicit RTL'].entries()) {
  console.log(pad(name, 26) + String(specified[i]).padStart(12) + String(upstream[i]).padStart(16));
}
console.log(
  pad('TOTAL', 26) +
    String(specified.reduce((a, b) => a + b, 0)).padStart(12) +
    String(upstream.reduce((a, b) => a + b, 0)).padStart(16),
);
console.log(`\nbitset histogram: ${JSON.stringify(histObj)}`);
console.log(
  `the expression, over all 21 (bitset, bit) rows:\n` +
    `  C parse differs from the intent on ${ops.cDiffers}\n` +
    `  C parse and Rust parse disagree on ${ops.disagree}`,
);

console.log('');
let failed = 0;
for (const r of results) {
  if (!r.ok) failed++;
  console.log(
    `${r.ok ? 'PASS' : 'FAIL'}  ${r.name}` +
      (r.ok ? '' : `\n        expected ${JSON.stringify(r.expected)}\n        actual   ${JSON.stringify(r.actual)}`),
  );
}
console.log(`\n${results.length - failed}/${results.length} checks passed.`);

// Two figures this program cannot check, named rather than implied:
console.log(
  `\nNot checked here (they need a git clone and a Rust toolchain: see reproduce.sh):\n` +
    `  - that the ${EXPECT.totalSpecified} comparisons all PASS once the filter is fixed\n` +
    `  - that the bug dates to ${EXPECT.bugIntroduced}, ${EXPECT.daysStanding} days before 2026-09-05\n` +
    `  - the ${EXPECT.characterTestRows15} BidiCharacterTest.txt rows, which were never affected`,
);

process.exit(failed === 0 ? 0 : 1);
