#!/usr/bin/env node
// verify-what-a-language-keeps.mjs
// ─────────────────────────────────────────────────────────────────────────────
// The check behind /strata/what-a-language-keeps/.
//
// Four jobs, in this order:
//
//   1. RE-DERIVE. The headline counts are recomputed here from the committed
//      Wiktionary snapshot and the hand audit, using this file's OWN string
//      arithmetic rather than by importing analyse.mjs, so agreeing with
//      results.json is a real comparison and not a tautology. McNemar's exact
//      test and the sign test are re-implemented here and cross-checked against
//      closed forms.
//   2. AUDIT THE AUDIT. Every kept pair's source word must actually appear in the
//      snapshotted wikitext of the entry that cites it, inside that entry's own
//      language section. Every dropped entry must really be in its category. The
//      hand audit is the one place a judgement enters this page, so it is the
//      place that gets checked hardest.
//   3. CHECK THE MEASURES ARE SYMMETRIC. The whole argument rests on head(k) and
//      tail(k) being the same size target. That is asserted on constructed cases,
//      including the mirror-image test: reverse both strings of every pair and the
//      head and tail rates must swap exactly.
//   4. READ THE SHIPPED BYTES. Every figure the page prints is asserted against
//      the artifacts, including the audio index and the SVG curve coordinates. A
//      page can be wrong in a way its data file is not.
//
//   node verify-what-a-language-keeps.mjs
//
// Offline, a few seconds, from a clean checkout. espeak-ng is needed only for the
// arm that uses it; without it those checks SKIP loudly rather than pass quietly.
// ─────────────────────────────────────────────────────────────────────────────

import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { LANGS, byName } from './research/minced-oaths-crosslinguistic/langs.mjs';
import { evidenceFor, machinePair, languageSection } from './research/minced-oaths-crosslinguistic/extract.mjs';
import { segments, classOf, letters } from './research/minced-oaths-crosslinguistic/phones.mjs';

const ROOT = path.dirname(fileURLToPath(import.meta.url));
const R = (p) => path.join(ROOT, p);
const SLUG = 'what-a-language-keeps';
const D = (f) => JSON.parse(fs.readFileSync(R(`research/minced-oaths-crosslinguistic/data/${f}`), 'utf8'));

let pass = 0, fail = 0, skip = 0;
const ok = (cond, label, detail) => {
  if (cond) { pass++; console.log(`  ✓ ${label}`); }
  else { fail++; console.log(`  ✗ ${label}${detail ? '  :  ' + detail : ''}`); }
};
const eq = (got, want, label) => ok(got === want, label, `got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`);
const near = (got, want, tol, label) => ok(Math.abs(got - want) <= tol, label, `got ${got}, want ${want} +/- ${tol}`);
const section = (s) => console.log(`\n── ${s} ` + '─'.repeat(Math.max(0, 64 - s.length)));

const RES = D('results.json');
const AUDIT = D('audit.json');
const MACHINE = D('machine.json');
const CATS = D('categories.json');
const ENTRIES = D('entries.json');
const PAGE = R(`public/strata/${SLUG}/index.html`);
const html = fs.existsSync(PAGE) ? fs.readFileSync(PAGE, 'utf8') : null;

console.log('WHAT A LANGUAGE KEEPS OF THE WORD IT WILL NOT SAY — verification\n');

// ═════════════════════════════════════════════════════════════════════════════
section('1. the snapshot is what it says it is');

const allPages = Object.values(CATS.categories).flat();
eq(LANGS.length, 33, '33 languages carry a minced-oath category on English Wiktionary');
eq(new Set(allPages).size, RES.counts.distinctPages, 'the distinct-page count matches the snapshot');
eq(Object.keys(CATS.categories).length, 33, 'every language in the table was fetched');
ok(Object.values(ENTRIES).every((e) => e === null || (typeof e.revid === 'number' && typeof e.wikitext === 'string')),
   'every fetched entry carries a revision id and its wikitext');
ok(allPages.every((t) => ENTRIES[t] !== undefined), 'every category member has an entry record');

// ═════════════════════════════════════════════════════════════════════════════
section('2. the mechanical rule, re-run here, reproduces machine.json');

let reMachine = 0, reAgree = 0;
for (const L of LANGS) {
  for (const title of CATS.categories[L.name] || []) {
    const page = ENTRIES[title];
    if (!page) continue;
    const { evidence } = evidenceFor(page.wikitext, L.name, L.code, { bare: false });
    const p = machinePair(title, evidence);
    const rec = MACHINE.find((m) => m.lang === L.name && m.title === title);
    if (p) reMachine++;
    if (rec && ((p && rec.pair && p.s === rec.pair.s) || (!p && !rec.pair))) reAgree++;
  }
}
eq(reMachine, RES.counts.machinePairs, `the pairing rule re-run here yields the same ${RES.counts.machinePairs} pairs`);
eq(reAgree, MACHINE.length, 'and names the same source word for every entry, pair-for-pair');

// the two bugs the page says were fixed: assert they are really fixed
{
  const kd = MACHINE.find((m) => m.lang === 'Polish' && m.title === 'kurdebalans');
  ok(kd && kd.pair && kd.pair.s !== 'fr', 'the der/inh/bor two-code bug is gone (Polish kurdebalans is not paired with the string "fr")');
  const ch = MACHINE.find((m) => m.lang === 'Polish' && m.title === 'cholewa');
  ok(ch && !ch.pair && ch.pairLoose && /dagnabbit/i.test(ch.pairLoose.s),
     'the bare-wikilink bug is gone under the strict rule and still visible under the loose one (Polish cholewa)');
  eq(MACHINE.filter((m) => m.pairLoose).length, RES.counts.machinePairsLoose, 'the loose arm is still computed and published');
}

// ═════════════════════════════════════════════════════════════════════════════
section('3. auditing the audit');

eq(Object.keys(AUDIT.pairs).length, MACHINE.filter((m) => m.pair).length,
   'every machine pair has exactly one audit line and there are no extras');

let sourcePresent = 0, sourceMissing = [];
for (const [key, a] of Object.entries(AUDIT.pairs)) {
  const i = key.indexOf('|');
  const lang = key.slice(0, i), title = key.slice(i + 1);
  if (a.verdict === 'drop') continue;
  const page = ENTRIES[title];
  const sec = page ? languageSection(page.wikitext, lang) : null;
  const src = a.verdict === 'fix' ? a.source : MACHINE.find((m) => m.lang === lang && m.title === title).pair.s;
  // Every substantial token of the source must occur in that language's own section of the
  // snapshotted wikitext. Tokens rather than the whole string, because a corrected source is
  // often reassembled from a template's parts: Wiktionary writes God's hooks as
  // "{{m|en|God|God-'s}} + {{m|en|'s hook|'s hooks}}", and Dutch godverdorie appears there
  // capitalised. Case-insensitive, and tokens under three characters are not evidence.
  // Parentheses inside a Wiktionary display form mark optional letters, so Finnish
  // "Juma(la)n kautta" is the full form Jumalan kautta with the medial syllable bracketed.
  // The section is therefore also searched with the bracket characters removed.
  const lower = sec ? (sec + '\n' + sec.replace(/[()]/g, '')).toLowerCase() : '';
  const toks = src.toLowerCase().split(/[^\p{L}\p{M}\p{N}]+/u).filter((w) => w.length >= 3);
  const need = toks.length ? toks : [src.toLowerCase()];
  if (sec && need.every((w) => lower.includes(w))) sourcePresent++;
  else sourceMissing.push(`${lang} ${title} < ${src}`);
}
ok(sourceMissing.length === 0,
   `all ${sourcePresent} kept sources really occur in the snapshotted section of the entry that cites them`,
   sourceMissing.slice(0, 4).join(' | '));

ok(Object.values(AUDIT.pairs).every((a) => a.verdict !== 'drop' || (a.why && a.why.length > 10)),
   'every dropped pair carries a stated reason');
ok(Object.values(AUDIT.pairs).every((a) => a.verdict !== 'fix' || (a.source && a.source.length)),
   'every corrected pair names its replacement source');
eq(Object.values(AUDIT.pairs).filter((a) => a.verdict === 'drop').length, RES.counts.dropped, 'the drop count matches');
eq(Object.values(AUDIT.pairs).filter((a) => a.verdict === 'fix').length, RES.counts.fixed, 'the correction count matches');
eq(Object.values(AUDIT.pairs).filter((a) => a.verdict !== 'drop').length, RES.counts.kept, 'the kept count matches');
ok(RES.dropped.length === RES.counts.dropped && RES.dropped.every((d) => d.why),
   'every drop is published with its reason, not silently discarded');

// the audit must not have been written by the outcome: a dropped pair is dropped for a
// stated evidential reason, so drops should not be concentrated on tail-preserving pairs.
// This is checked structurally instead: no drop reason mentions a head, tail or statistic.
ok(!Object.values(AUDIT.pairs).some((a) => a.why && /head|tail|first phone|last phone|significan|p =/i.test(a.why)),
   'no audit reason refers to the measurement, so the audit cannot have been written by the outcome');

// ═════════════════════════════════════════════════════════════════════════════
section('4. the measures are symmetric, which is the whole argument');

const same = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
const HEAD = (s, e, k) => (s.length < k || e.length < k) ? null : same(s.slice(0, k), e.slice(0, k));
const TAIL = (s, e, k) => (s.length < k || e.length < k) ? null : same(s.slice(-k), e.slice(-k));

ok(HEAD(['a', 'b', 'c'], ['a', 'x', 'y'], 1) === true && TAIL(['a', 'b', 'c'], ['a', 'x', 'y'], 1) === false,
   'head and tail read the ends they claim to read');
ok(HEAD(['a'], ['a', 'b'], 2) === null && TAIL(['a'], ['a', 'b'], 2) === null,
   'both are undefined, not false, when a word is shorter than k');
ok(HEAD(['a', 'b'], ['a', 'b'], 2) === true && TAIL(['a', 'b'], ['a', 'b'], 2) === true,
   'both are true on an identical pair, so neither end is privileged by construction');

// THE MIRROR TEST. Reverse every phone string in the corpus; the head rate must become
// exactly the old tail rate and vice versa. If the two measures were not the same size
// target, this equality would fail.
{
  const rows = RES.pairs.filter((p) => p.arms.espeak);
  const h = rows.filter((p) => HEAD(p.arms.espeak.s, p.arms.espeak.e, 1) === true).length;
  const t = rows.filter((p) => TAIL(p.arms.espeak.s, p.arms.espeak.e, 1) === true).length;
  const rev = (a) => a.slice().reverse();
  const hR = rows.filter((p) => HEAD(rev(p.arms.espeak.s), rev(p.arms.espeak.e), 1) === true).length;
  const tR = rows.filter((p) => TAIL(rev(p.arms.espeak.s), rev(p.arms.espeak.e), 1) === true).length;
  ok(hR === t && tR === h, `mirroring the corpus swaps the two rates exactly (${h}/${t} becomes ${hR}/${tR})`);
}

// ═════════════════════════════════════════════════════════════════════════════
section('5. the headline, recomputed here from the stored phone strings');

const eligible = RES.eligible;
const poolable = (p) => (byName.get(p.lang)?.pooled !== false) && eligible.includes(p.lang);

function headline(arm) {
  const rows = RES.pairs.filter((p) => poolable(p) && p.arms[arm]);
  let h = 0, t = 0, b = 0, c = 0;
  for (const p of rows) {
    const H = HEAD(p.arms[arm].s, p.arms[arm].e, 1) === true;
    const T = TAIL(p.arms[arm].s, p.arms[arm].e, 1) === true;
    if (H) h++; if (T) t++;
    if (H && !T) b++; if (!H && T) c++;
  }
  return { n: rows.length, h, t, b, c, head: h / rows.length, tail: t / rows.length };
}

for (const arm of ['espeak', 'letters', 'wikt']) {
  const g = headline(arm);
  const w = RES.pooled[arm][0];
  eq(g.n, w.n, `${arm}: the pooled pair count matches (${g.n})`);
  near(g.head, w.headRate, 1e-12, `${arm}: first-phone rate ${(100 * g.head).toFixed(1)}% matches results.json`);
  near(g.tail, w.tailRate, 1e-12, `${arm}: last-phone rate ${(100 * g.tail).toFixed(1)}% matches results.json`);
  eq(g.b, w.mcnemar.b, `${arm}: discordant b matches`);
  eq(g.c, w.mcnemar.c, `${arm}: discordant c matches`);
  ok(g.head > g.tail, `${arm}: the first phone survives more often than the last`);
}

// the profile curve, recomputed
{
  const rows = RES.pairs.filter((p) => poolable(p) && p.arms.espeak);
  for (let k = 1; k <= 6; k++) {
    for (const [side, F] of [['head', HEAD], ['tail', TAIL]]) {
      const def = rows.filter((p) => F(p.arms.espeak.s, p.arms.espeak.e, k) !== null);
      const yes = def.filter((p) => F(p.arms.espeak.s, p.arms.espeak.e, k) === true).length;
      const w = RES.pooled.espeak[0].profile[side][k - 1];
      ok(def.length === w.n && yes === w.yes, `profile ${side}(${k}) = ${yes}/${def.length} matches`);
    }
  }
  const P = RES.pooled.espeak[0].profile;
  ok([0, 1, 2, 3, 4, 5].every((i) => P.head[i].rate > P.tail[i].rate),
     'the head curve is above the tail curve at every k from 1 to 6, as the page claims');
}

// ═════════════════════════════════════════════════════════════════════════════
section('6. the statistics, re-implemented and cross-checked');

function lnChoose(n, k) { let r = 0; for (let i = 1; i <= k; i++) r += Math.log(n - k + i) - Math.log(i); return r; }
function binomExact(x, n) {
  if (n === 0) return 1;
  const pmf = (k) => Math.exp(lnChoose(n, k) - n * Math.LN2);
  const obs = pmf(x) * (1 + 1e-9);
  let p = 0;
  for (let k = 0; k <= n; k++) if (pmf(k) <= obs) p += pmf(k);
  return Math.min(1, p);
}
near(binomExact(0, 1), 1, 1e-12, 'binomial: 0 of 1 gives p = 1');
near(binomExact(0, 10), 2 * 0.5 ** 10, 1e-12, 'binomial: 0 of 10 gives 2 x 2^-10, the closed form');
near(binomExact(1, 10), 2 * (1 + 10) * 0.5 ** 10, 1e-12, 'binomial: 1 of 10 matches the closed form');
near(binomExact(5, 10), 1, 1e-12, 'binomial: the most likely outcome gives p = 1');

for (const arm of ['espeak', 'letters', 'wikt']) {
  const g = headline(arm);
  const p = binomExact(Math.min(g.b, g.c), g.b + g.c);
  near(Math.log10(p), Math.log10(RES.pooled[arm][0].mcnemar.p), 1e-9, `${arm}: McNemar p reproduces (${p.toPrecision(3)})`);
  ok(p < 1e-5, `${arm}: and it is decisive`);
}

// the permutation null: re-run a short version with a different seed and check it lands
// in the same place, so the published chance rate is not an artefact of one seed
{
  const L = 'French';
  const rows = RES.pairs.filter((p) => p.lang === L && p.arms.espeak);
  const es = rows.map((p) => p.arms.espeak.e), ss = rows.map((p) => p.arms.espeak.s);
  let a = 987654321 >>> 0;
  const rnd = () => { a = (a + 0x6D2B79F5) >>> 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
  const N = 20000; let sum = 0;
  for (let i = 0; i < N; i++) {
    const sp = ss.slice();
    for (let j = sp.length - 1; j > 0; j--) { const k = Math.floor(rnd() * (j + 1)); [sp[j], sp[k]] = [sp[k], sp[j]]; }
    let h = 0;
    for (let j = 0; j < es.length; j++) if (HEAD(sp[j], es[j], 1) === true) h++;
    sum += h / es.length;
  }
  const published = RES.languages[L].arms.espeak.perm.head.permMean;
  near(sum / N, published, 0.02, `the French chance rate reproduces under a different seed (${(100 * sum / N).toFixed(1)}% against a published ${(100 * published).toFixed(1)}%)`);
  ok(RES.languages[L].arms.espeak.headRate > published * 3, 'and the observed French rate is far above it');
}

// ═════════════════════════════════════════════════════════════════════════════
section('7. the registered prediction, and its refutation');

const PRE = fs.readFileSync(R('research/minced-oaths-crosslinguistic/PREREGISTRATION.md'), 'utf8');
ok(/sacrebleu, morbleu, parbleu/.test(PRE), 'the pre-registration names the -bleu family in advance');
ok(/the `-bleu`\s*\n?\s*sub-family, taken alone, shows a tail advantage/.test(PRE.replace(/\s+/g, ' ').replace(/ /g, ' ')) || /sub-family, taken alone, shows a tail advantage/.test(PRE),
   'and states the prediction that the family would show a tail advantage');
ok(RES.bleu.length >= 6, `${RES.bleu.length} members of the family are in the data`);
ok(RES.bleu.every((b) => b.espeak && b.espeak.head === true), 'every one of them keeps the first phone, against the prediction');
ok(RES.bleu.every((b) => b.espeak && b.espeak.tail === true), 'and every one keeps the last phone too, so the middle is what goes');
ok(RES.languages.French.arms.espeak.headRate >= RES.languages.English.arms.espeak.headRate,
   'French is not weaker than English, which is the specific thing the prediction got wrong');

// ═════════════════════════════════════════════════════════════════════════════
section('8. the approximant replication, and the check that keeps it honest');

{
  // recount the classes here from the stored phone strings
  const rows = RES.pairs.filter((p) => poolable(p) && p.arms.espeak);
  const ap = (segs) => segs.filter((g) => ['liquid', 'glide'].includes(classOf(g))).length;
  const diffs = rows.map((p) => ap(p.arms.espeak.e) - ap(p.arms.espeak.s));
  const pos = diffs.filter((d) => d > 0).length, neg = diffs.filter((d) => d < 0).length;
  const A = RES.h3.espeak.literal.approximant.count;
  eq(pos, A.pos, `approximants rise in ${pos} pairs, matching the results file`);
  eq(neg, A.neg, `and fall in ${neg}`);
  near(binomExact(Math.min(pos, neg), pos + neg), A.p, 1e-9, 'the sign-test p reproduces');
  ok(pos > neg && A.p < 0.05, 'the 2023 approximant result replicates outside English');

  // the pre-registered whole-inventory check really ships
  const CL = ['stop', 'fricative', 'affricate', 'nasal', 'liquid', 'glide', 'vowel'];
  ok(CL.every((c) => RES.h3.espeak.literal[c]), 'every phoneme class is reported, not only the one under test');
  ok(CL.every((c) => html && html.includes(`<td>${c}</td>`)), 'and every one of them is on the page');
  ok(RES.h3.espeak.literal.approximant.rate.p > 0.05,
     'on rates, which controls for word length, the approximant effect does not survive, and the page says so');
  ok(RES.h3.espeak.literal.fricative.rate.p < 0.05 && RES.h3.espeak.literal.fricative.rate.mean < 0,
     'what survives the length control is fricatives falling');
  ok(RES.h3.espeak.literal.nasal.rate.p < 0.05 && RES.h3.espeak.literal.nasal.rate.mean > 0,
     'and nasals rising');
  ok(RES.h3.espeak.rhoticAsLiquid.approximant.count.pos > RES.h3.espeak.rhoticAsLiquid.approximant.count.neg,
     'the replication also holds under the cited paper\'s own coding, which counts every rhotic');
}

// ═════════════════════════════════════════════════════════════════════════════
section('9. sensitivity: the result survives every subset the page claims');

for (const s of RES.pooled.espeak) {
  if (!s.n) continue;
  ok(s.headRate > s.tailRate && s.mcnemar.p < 0.05,
     `"${s.label}": ${(100 * s.headRate).toFixed(1)}% against ${(100 * s.tailRate).toFixed(1)}%, p = ${s.mcnemar.p.toPrecision(2)}`);
}
{
  const noEng = RES.pooled.espeak.find((s) => s.label === 'excluding English');
  ok(noEng && noEng.headRate > RES.pooled.espeak[0].headRate,
     'removing English raises the head rate, so the effect is not an English artefact');
}

// ═════════════════════════════════════════════════════════════════════════════
section('10. transcription arms disagree about phones and agree about verdicts');

for (const [k, v] of Object.entries(RES.agreement)) {
  if (!v.n) continue;
  ok(v.head / v.n > 0.9, `${k}: first-phone verdicts agree ${(100 * v.head / v.n).toFixed(1)}% of ${v.n}`);
}
ok(RES.agreement['espeak~wikt'].identicalStrings / RES.agreement['espeak~wikt'].n < 0.5,
   'while the phone strings themselves are usually NOT identical, which is what makes the agreement worth stating');
ok(['espeak', 'wikt', 'letters'].every((a) => RES.pooled[a][0].headRate > RES.pooled[a][0].tailRate),
   'the direction of the result is the same in all three systems, so it is not source-dependent');

// letters: recompute independently from the raw words, trusting nothing stored
{
  const rows = RES.pairs.filter((p) => poolable(p) && p.arms.letters);
  let h = 0;
  for (const p of rows) {
    const e = letters(p.e), s = letters(p.s);
    if (e && s && e.segs[0] === s.segs[0]) h++;
  }
  near(h / rows.length, RES.pooled.letters[0].headRate, 1e-12,
     'the letters arm recomputes from the raw words alone, with no stored phone strings');
}

// ═════════════════════════════════════════════════════════════════════════════
section('11. espeak, where it is load-bearing');

let espeakOk = true;
try { execFileSync('espeak-ng', ['--version'], { stdio: 'ignore' }); } catch { espeakOk = false; }
if (!espeakOk) { skip++; console.log('  ~ espeak-ng not installed (sudo apt-get install -y espeak-ng); its checks skipped'); }
else {
  const run = (w, v) => execFileSync('espeak-ng', ['-v', v, '-q', '--ipa', '--', w], { encoding: 'utf8' }).replace(/\n/g, ' ').trim();
  eq(segments(run('mierda', 'es')).join('|'), RES.pairs.find((p) => p.lang === 'Spanish' && p.e === 'miércoles').arms.espeak.s.join('|'),
     'espeak re-synthesises the Spanish source string stored in the results');
  eq(segments(run('sacrebleu', 'fr-fr')).join('|'), RES.bleu.find((b) => b.e === 'sacrebleu').espeak ? segments(RES.bleu.find((b) => b.e === 'sacrebleu').espeak.eIpa).join('|') : '',
     'and the French one');
  ok(segments('t͡ʃa').length === 2, 'a tie bar fuses an affricate into one phone');
  eq(classOf('t͡ʃ'), 'affricate', 'and that phone is classified as an affricate');
  eq(classOf('ʁ'), 'fricative', 'IPA-literal: uvular /ʁ/ is a fricative');
  eq(classOf('ʁ', { rhoticAsLiquid: true }), 'liquid', 'and under the cited paper\'s coding it is a liquid');
}

// ═════════════════════════════════════════════════════════════════════════════
section('12. the shipped bytes');

if (!html) { fail++; console.log('  ✗ the page is missing'); }
else {
  const has = (s, label) => ok(html.includes(s), label, `missing: ${JSON.stringify(String(s).slice(0, 70))}`);
  const pc1 = (x) => (100 * x).toFixed(1);
  const P = RES.pooled;

  has(`${pc1(P.espeak[0].headRate)} per cent`, 'the espeak head rate the page prints is the one in the results');
  has(`${pc1(P.espeak[0].tailRate)} per cent`, 'and the tail rate');
  has(P.espeak[0].mcnemar.p.toPrecision(2), 'and the McNemar p');
  has(`${pc1(P.letters[0].headRate)} against ${pc1(P.letters[0].tailRate)}`, 'the letters arm is printed with both its rates');
  has(`${pc1(P.wikt[0].headRate)} against ${pc1(P.wikt[0].tailRate)}`, 'and the Wiktionary-IPA arm');
  has(`${RES.counts.kept} pairs`, 'the kept-pair count');
  has(`${RES.counts.languagesWithCategory}</b> languages`, 'the language count');
  has(`<b>${RES.counts.machinePairs}</b> pairs from <b>${RES.counts.distinctPages}</b>`, 'the machine-pair and page counts');
  has(`${RES.counts.dropped} were dropped and ${RES.counts.fixed} were corrected`, 'the audit counts');
  has(`${RES.counts.phrases} have a phrase`, 'the phrase count');
  has(`${RES.counts.swaps} are swaps`, 'the swap count');

  // every dropped pair's reason is actually on the page
  ok(RES.dropped.every((d) => html.includes(d.e)), 'every dropped entry is named on the page');

  // per-language table rows must carry the real numbers
  let rowsOk = 0;
  for (const L of eligible) {
    const a = RES.languages[L].arms.espeak;
    if (!a || !a.n) continue;
    if (html.includes(`<td>${L}</td><td class="n">${a.n}</td><td class="n b">${Math.round(100 * a.headRate)}</td><td class="n">${Math.round(100 * a.tailRate)}</td>`)) rowsOk++;
  }
  eq(rowsOk, eligible.filter((L) => RES.languages[L].arms.espeak?.n).length,
     'every language row in the table carries its real n, head and tail');

  // the SVG curve must be drawn from the real profile, not decoration
  {
    const P6 = RES.pooled.espeak[0].profile;
    const W = 640, H = 300, ml = 46, mr = 16, mt = 18, mb = 40;
    const x = (k) => ml + (k - 1) * (W - ml - mr) / 5;
    const y = (r) => mt + (1 - r) * (H - mt - mb);
    const want = P6.head.map((d, i) => `${i ? 'L' : 'M'}${x(d.k).toFixed(1)},${y(d.rate).toFixed(1)}`).join(' ');
    ok(html.includes(want), 'the head curve path is plotted from the real profile rates');
    const wantT = P6.tail.map((d, i) => `${i ? 'L' : 'M'}${x(d.k).toFixed(1)},${y(d.rate).toFixed(1)}`).join(' ');
    ok(html.includes(wantT), 'and so is the tail curve');
  }

  // the embedded payload must match the results, pair for pair
  {
    const m = html.match(/<script id="data" type="application\/json">([\s\S]*?)<\/script>/);
    ok(!!m, 'the page embeds its data payload');
    if (m) {
      const pay = JSON.parse(m[1].replace(/\\u003c/g, '<'));
      eq(pay.pairs.length, RES.pairs.length, 'the payload carries every pair');
      const bad = pay.pairs.filter((p) => {
        const r = RES.pairs.find((q) => q.lang === p.l && q.e === p.e);
        return !r || r.s !== p.s;
      });
      eq(bad.length, 0, 'and each payload pair names the same source as the results file');
      ok(pay.pairs.every((p) => !p.a.espeak || (Array.isArray(p.a.espeak.e) && p.a.espeak.e.length)),
         'and each carries the phone strings the instrument colours');
    }
  }

  // the citation the page prints must be the corrected one
  has('30(3), 1103 to 1114', 'the cited paper is given its real volume and pages');
  has('10.3758/s13423-022-02202-0', 'and its DOI');
  has('Study 2', 'and the right study number');
  ok(!/Psychonomic Bulletin &amp; Review<\/i> 29/.test(html), 'and not the wrong volume');

  // prior art the page leans on
  for (const s of ['Allan and Burridge', 'Vallery', 'Pimentel', 'Wong (2016)', 'Marslen-Wilson'])
    has(s, `prior art credited: ${s}`);
  has('could not read', 'the one piece of prior art that could not be obtained is declared as such');

  // the honesty furniture
  has('espeak-ng synthesis of the exact strings', 'the audio is labelled as synthesis, not recording');
  has('It is on the record because it is wrong', 'the failed prediction is stated plainly');
  has('Chinese is reported and never pooled', 'the pre-registered Chinese exclusion is stated');
  ok(!html.includes('—'), 'no em dashes in the page prose (the house rule)');

  // audio index must line up with what the page offers
  {
    const idx = JSON.parse(fs.readFileSync(R(`public/strata/${SLUG}/audio/index.json`), 'utf8'));
    let clips = 0, missing = 0;
    for (const [lang, a] of Object.entries(idx.languages)) {
      if (!fs.existsSync(R(`public/strata/${SLUG}/audio/${a.file}`))) missing++;
      clips += a.clips.length;
      for (const c of a.clips) if (!RES.pairs.some((p) => p.lang === lang && p.e === c.e && p.s === c.s)) missing++;
    }
    eq(missing, 0, `all ${Object.keys(idx.languages).length} audio sprites exist and every one of their ${clips} clips is a real pair`);
    ok(Object.values(idx.languages).every((a) => a.clips.every((c) => c.ts[1] > 0 && c.te[1] > 0 && c.te[0] > c.ts[0])),
       'and every clip has a positive duration with the source before the euphemism');
  }
}

// ═════════════════════════════════════════════════════════════════════════════
console.log(`\n${'═'.repeat(66)}`);
console.log(`${pass}/${pass + fail} checks passed` + (skip ? `, ${skip} skipped` : '') + (fail ? `, ${fail} FAILED` : ''));
process.exit(fail ? 1 : 0);
