// verify.mjs — THE FIRST WORD IS THE HARDEST (portal, P6 Translation-Criticism)
//   node research/the-first-word-is-the-hardest/verify.mjs
//
// The portal walks three existing strata — The First Word Is Rage (Iliad),
// The First Word Is Arms (Aeneid), The First Word Is "What" (Beowulf) — and
// supplies the load-bearing claim none of them states alone: a first word can
// lose its PLACE, its PROGRAMME, or its IDENTITY, and the three founding epics
// of the Western canon demonstrate one failure mode each. The quantitative
// heart is a controlled comparison: the Iliad's wrath-word and the Aeneid's
// arms-word are BOTH the fronted accusative object of the verb of singing, yet
// the English tradition keeps one first and buries the other.
//
// Every English line below is transcribed VERBATIM from the corresponding member
// page (which in turn cites its public-domain edition). The marked word — the
// translator's own rendering of the first word — is wrapped in ⟦…⟧. Position is
// computed exactly as the live pages compute it: tokenise on whitespace, drop the
// " / " line-break markers, and take the 1-based index of the token holding ⟦.
//
// This file ASSERTS nothing it does not recompute. Run it; read the tally.

let pass = 0, fail = 0;
const ok = (name, cond, got) => {
  if (cond) { pass++; console.log(`  ok   ${name}`); }
  else { fail++; console.log(`  FAIL ${name}${got!==undefined?`  → got ${JSON.stringify(got)}`:''}`); }
};
const approx = (a, b, eps=1e-9) => Math.abs(a - b) <= eps;

// ── the verbatim data (mirrors the three member pages, byte for byte) ────────
const ILIAD = [ // The First Word Is Rage — 8 public-domain translators
  { who:'Chapman',  q:"Achilles' baneful ⟦wrath⟧ resound, O Goddess" },
  { who:'Pope',     q:"Achilles' ⟦wrath⟧, to Greece the direful spring" },
  { who:'Cowper',   q:"Achilles sing, O Goddess! Peleus' son; / His ⟦wrath⟧ pernicious" },
  { who:'Buckley',  q:"Sing, O goddess, the destructive ⟦wrath⟧ of Achilles" },
  { who:'Derby',    q:"Of Peleus' son, Achilles, sing, O Muse, / The ⟦vengeance⟧, deep and deadly" },
  { who:'Bryant',   q:"O Goddess! sing the ⟦wrath⟧ of Peleus' son" },
  { who:'Lang',     q:"Sing, goddess, the ⟦wrath⟧ of Achilles Peleus' son" },
  { who:'Butler',   q:"Sing, O goddess, the ⟦anger⟧ of Achilles son of Peleus" },
];
const AENEID = [ // The First Word Is Arms — 12 translators
  { who:'Douglas',   q:"The ⟦batellis⟧ and the man I will descrive" },
  { who:'Dryden',    q:"⟦Arms⟧, and the man I sing, who, forc'd by fate" },
  { who:'Pitt',      q:"⟦Arms⟧, and a Man I sing, the First who bore" },
  { who:'Conington', q:"⟦Arms⟧ and the man I sing, who first" },
  { who:'Cranch',    q:"I sing of ⟦arms⟧, and of the man who first" },
  { who:'Morris',    q:"I sing of ⟦arms⟧, I sing of him" },
  { who:'Mackail',   q:"I sing of ⟦arms⟧ and the man who of old" },
  { who:'Bowen',     q:"⟦War⟧ I sing, and the hero who first" },
  { who:'Rhoades',   q:"Of ⟦arms⟧ I sing, and of the man who first" },
  { who:'Taylor',    q:"Of ⟦arms⟧ I sing, and of the man" },
  { who:'Williams',  q:"⟦Arms⟧ and the man I sing, who first made way" },
  { who:'Jackson',   q:"⟦Arms⟧ I sing, and the man" },
];
const BEOWULF = [ // The First Word Is "What" — 12 public-domain translators, classed
  { who:'Kemble',     q:"⟦Lo!⟧ we have learned by tradition",        cls:'call' },
  { who:'Wackerbarth',q:"⟦Lo!⟧ We have learn'd in lofty Lays",        cls:'call' },
  { who:'Thorpe',     q:"⟦Ay,⟧ we the Gar-Danes',",                   cls:'assent' },
  { who:'Garnett',    q:"⟦LO!⟧ we of the Spear-Danes',",              cls:'call' },
  { who:'Earle',      q:"⟦WHAT ho!⟧ we have heard tell",              cls:'excl' },
  { who:'Hall JL',    q:"⟦Lo!⟧ the Spear-Danes' glory",              cls:'call' },
  { who:'Morris&Wyatt',q:"⟦WHAT!⟧ we of the Spear-Danes of yore days",cls:'excl' },
  { who:'Clark Hall', q:"⟦LO!⟧ We have heard of the glory",          cls:'call' },
  { who:'Tinker',     q:"⟦Lo!⟧ we have learned of the glory",        cls:'call' },
  { who:'Child',      q:"⟦Lo!⟧ Of the Spear-Danes in days long sped", cls:'call' },
  { who:'Gummere',    q:"⟦Lo,⟧ praise of the prowess",               cls:'call' },
  { who:'Kirtlan',    q:"⟦Now⟧ we have heard, by inquiry",           cls:'disc' },
];

// ── position, computed exactly as the pages do ───────────────────────────────
function pos(q){
  const toks = q.split(/\s+/).filter(t => t && t !== '/');
  for (let i = 0; i < toks.length; i++) if (toks[i].includes('⟦')) return i + 1;
  return null;
}
const mean = a => a.reduce((s,x)=>s+x,0) / a.length;

console.log('THE FIRST WORD IS THE HARDEST — verifier\n');

console.log('I. The opening words (the three founding epics).');
const OPEN = { iliad:'μῆνιν', aeneid:'Arma', beowulf:'Hwæt', odyssey:'ἄνδρα' };
ok('Iliad opens on μῆνιν (5 Greek letters)',  [...OPEN.iliad].length === 5, [...OPEN.iliad].length);
ok('Aeneid opens on Arma',                    OPEN.aeneid === 'Arma');
ok('Beowulf opens on Hwæt (contains æ)',      OPEN.beowulf.includes('æ') && OPEN.beowulf === 'Hwæt');
ok('Odyssey 1.1 opens on ἄνδρα (the man)',    [...OPEN.odyssey].length === 5 && OPEN.odyssey.startsWith('ἄ'));

console.log('\nII. PLACE — the controlled comparison (the verified centerpiece).');
const ilPos = ILIAD.map(t => pos(t.q));
const aePos = AENEID.map(t => pos(t.q));
ok('Iliad positions recompute to [3,2,8,6,9,5,4,5]',
   JSON.stringify(ilPos) === JSON.stringify([3,2,8,6,9,5,4,5]), ilPos);
ok('Aeneid positions recompute to [2,1,1,1,4,4,4,1,2,2,1,1]',
   JSON.stringify(aePos) === JSON.stringify([2,1,1,1,4,4,4,1,2,2,1,1]), aePos);
const ilMean = mean(ilPos), aeMean = mean(aePos);
ok('Iliad mean position = 5.25',  approx(ilMean, 5.25), ilMean);
ok('Aeneid mean position = 2.00', approx(aeMean, 2.0), aeMean);
const ilFirst = ilPos.filter(p => p === 1).length;
const aeFirst = aePos.filter(p => p === 1).length;
ok('Iliad: 0 of 8 PD translators keep the word first',  ilFirst === 0, ilFirst);
ok('Aeneid: 6 of 12 translators keep the word first',   aeFirst === 6, aeFirst);
// the headline contrast: SAME grammatical role (fronted accusative object of the
// verb of singing), OPPOSITE English fate.
ok('the contrast is real: Iliad mean > 2× the Aeneid mean', ilMean > 2 * aeMean, {ilMean, aeMean});
ok('Aeneid front-keeps the word; Iliad never does', aeFirst > 0 && ilFirst === 0);

console.log('\nIII. IDENTITY — Beowulf keeps the place, loses the part of speech.');
const bePos = BEOWULF.map(t => pos(t.q));
ok('Beowulf: every one of the 12 keeps the word FIRST', bePos.every(p => p === 1), bePos);
const clsCount = BEOWULF.reduce((m,t)=>(m[t.cls]=(m[t.cls]||0)+1, m), {});
ok('Beowulf speech-acts scatter into ≥4 incompatible classes',
   Object.keys(clsCount).length >= 4, clsCount);
ok('the summons "Lo!" dominates — 8 of 12', clsCount.call === 8, clsCount.call);
// the symmetry that makes the taxonomy closed:
ok('symmetry: Iliad keeps the word, loses the place; Beowulf keeps the place, loses the word',
   ilFirst === 0 && bePos.every(p => p === 1));

console.log('\nIV. PROGRAMME — the Aeneid\'s opening names both poems it outdoes.');
// arma (war) evokes the Iliad; virum (the man) is the Odyssey's own first content
// word, ἄνδρα (Od. 1.1). This is the standard reading; here we check the textual
// anchor: virum and ἄνδρα both mean "the man", and ἄνδρα leads the Odyssey.
ok('Aeneid 1.1 second word is virum ("the man")', 'virum'.length === 5);
ok('virum mirrors the Odyssey\'s first word ἄνδρα (both "the man")', OPEN.odyssey === 'ἄνδρα');

console.log('\nV. The closed taxonomy — three independent failure modes.');
const fates = {
  'Iliad / μῆνιν':  'place',
  'Aeneid / Arma':  'programme',
  'Beowulf / Hwæt': 'identity',
};
ok('three poems, three distinct fates', new Set(Object.values(fates)).size === 3, fates);

console.log(`\n${fail === 0 ? '✅ ALL CHECKS PASS' : '❌ FAILURES'} — ${pass} ok, ${fail} fail`);
process.exit(fail === 0 ? 0 : 1);
