// verify-how-euripides-plays-are-dated.mjs: checks every number /strata/how-euripides-plays-are-dated/
// prints, by re-running the page's own scanner (engine.mjs) and dating arithmetic (clock.mjs)
// over the page's own copy of Murray's text (data/lines.json).
//
//   node verify-how-euripides-plays-are-dated.mjs          # the page's files
//   node verify-how-euripides-plays-are-dated.mjs --live   # also re-fetch the 19 Perseus TEI files
//                                                          # at the pinned commit and check that
//                                                          # data/lines.json is a faithful copy
//
// Needs only Node 18+. Inside the repository it tests the local files; from an empty directory it
// downloads engine.mjs, clock.mjs and the data from https://artwaste.land (the bytes your browser
// runs). Exit 0 = every check passed.

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

const here = dirname(fileURLToPath(import.meta.url));
const slug = 'how-euripides-plays-are-dated';
const localDir = join(here, 'public/strata', slug);
const remote = `https://artwaste.land/strata/${slug}/`;
const LIVE = process.argv.includes('--live');
async function loadModule(name) {
  const p = join(localDir, name); if (existsSync(p)) return import(p);
  const src = await (await fetch(remote + name)).text();
  return import('data:text/javascript;base64,' + Buffer.from(src).toString('base64'));
}
async function loadJSON(name) {
  const p = join(localDir, name); if (existsSync(p)) return JSON.parse(readFileSync(p, 'utf8'));
  return (await fetch(remote + name)).json();
}
console.error(`files: ${existsSync(localDir) ? localDir : remote}`);
const E = await loadModule('engine.mjs');
const C = await loadModule('clock.mjs');
const DATA = await loadJSON('data/lines.json');
const AUDIT = await loadJSON('data/audit.json');
const PUB = await loadJSON('data/published.json');

let fails = 0, passes = 0;
const ok = (c, msg) => { if (c) passes++; else fails++; console.log((c ? 'PASS  ' : 'FAIL  ') + msg); };

// ── 1. the scanner on lines whose scansion is not in doubt ────────────────────────
{
  const known = [
    ['Εἴθ’ ὤφελ’ Ἀργοῦς μὴ διαπτάσθαι σκάφος', 0, 'Medea 1'],
    ['Ἥκω Διὸς παῖς τήνδε Θηβαίων χθόνα', 0, 'Bacchae 1'],
    ['Οὐκ ἔστιν οὐδὲν δεινὸν ὧδ’ εἰπεῖν ἔπος', 0, 'Orestes 1'],
    ['ἀκόλαστον ἔσχε γλῶσσαν, αἰσχίστην νόσον.', 1, 'Orestes 10 (first-foot anapaest ἀκόλ-)'],
    ['ἐπεὶ δὲ Θησεὺς Κεκροπίαν λείπει χθόνα,', 1, 'Hippolytus 34 (Κεκροπίαν)'],
    ['αἳ δ’ ἐνδακοῦσαι στόμια πυριγενῆ γναθμοῖς', 2, 'Hippolytus 1223 (στόμια, πυριγενῆ)'],
    ['ὡς μήποτ’ ἄνδρα τὸν ἐμὸν αἰσχύνασ’ ἁλῶ,', 1, 'Hippolytus 420 (τὸν ἐμὸν)'],
  ];
  const bad = known.filter(([t, n]) => { const r = E.scanLine(t); return !r.trimeter || r.resolutions !== n; });
  ok(!bad.length, `${known.length} lines of settled scansion scan with the expected count${bad.length ? ': wrong on ' + bad.map(b => b[2]).join(', ') : ''}`);
  const ad = E.scanLine('ἡ δ’ οὖν γυνὴ κάτεισιν εἰς Ἅιδου δόμους.');
  ok(ad.trimeter && ad.resolutions === 0, `iota adscript on a capital (Ἅιδου = ᾍδου) is one long syllable, not two (Alcestis 73 scans with ${ad.resolutions} resolutions)`);
  const lyric = E.scanLine('ἰὼ ἰώ, πόνος ἐπὶ πόνῳ');
  ok(!lyric.trimeter, 'a short lyric cry does not pass as a trimeter');
}

// ── 2. every play, rescanned, gives the numbers the page prints ─────────────────
const EXPECT = { // play: [spoken trimeters kept, resolutions per 100], unbracketed
  tlg001: [512, 39.65], tlg002: [761, 6.96], tlg003: [1009, 7.23], tlg004: [864, 6.94], tlg005: [946, 5.81],
  tlg006: [900, 16.33], tlg007: [886, 19.86], tlg008: [882, 16.89], tlg009: [932, 22.96], tlg010: [994, 28.37],
  tlg011: [751, 24.9], tlg012: [929, 21.53], tlg013: [1041, 30.64], tlg014: [1220, 36.23], tlg015: [1091, 35.11],
  tlg016: [1049, 49.86], tlg017: [886, 43.45], tlg018: [802, 43.64], tlg019: [651, 9.52],
};
const S = {};
let total = 0, kept = 0, invariant = true;
for (const p of DATA.plays) {
  const r = E.scanText(p.lines.map(([n, text, br]) => ({ n, text, bracketed: !!br })));
  S[p.id] = r; total += r.linesTotal; kept += r.unbracketed.trimeters;
  for (const l of r.lines) if (l.trimeter) {
    const resolvedElements = l.parse.filter(([, k]) => k === 2).length;
    if (resolvedElements !== l.resolutions || l.parse.length !== 12) invariant = false;
  }
}
ok(DATA.plays.length === 19 && total === 26383 && kept === 17106, `19 plays, ${total.toLocaleString('en')} lines read, ${kept.toLocaleString('en')} kept as spoken trimeters (page: 26,383 and 17,106)`);
ok(invariant, 'in every line that scans, syllables − 12 equals the number of resolved elements in the parse (the count does not depend on the parse)');
{
  const bad = Object.entries(EXPECT).filter(([id, [n, r]]) => S[id].unbracketed.trimeters !== n || S[id].unbracketed.perHundred !== r);
  ok(!bad.length, `per-play trimeters and resolutions per 100 match the page's table for all 19 plays${bad.length ? ': differ on ' + bad.map(b => b[0]).join(', ') : ''}`);
  const lo = Math.min(...Object.values(S).map(r => r.unbracketed.perHundred)), hi = Math.max(...Object.values(S).map(r => r.unbracketed.perHundred));
  ok(lo === 5.81 && hi === 49.86, `the range runs from ${lo} (Hippolytus) to ${hi} (Orestes) per 100 lines (page: 5.8 to 49.9)`);
}

// ── 3. the pre-registered test ────────────────────────────────────────────────────
{
  const anchors = C.ANCHORS.map(a => ({ ...a, rate: S[a.id].unbracketed.perHundred }));
  ok(anchors.length === 8, '8 anchors, dated outside the verse');
  const loo = C.leaveOneOut(anchors);
  ok(Math.abs(loo.rmse - 5.32) < 0.005, `leave-one-out RMS error ${loo.rmse.toFixed(3)} years (page: 5.3), above the pre-registered 5-year bar`);
  const worst = loo.rows.reduce((a, b) => Math.abs(b.error) > Math.abs(a.error) ? b : a);
  ok(worst.en === 'Alcestis' && Math.abs(worst.error - 9.8) < 0.05, `worst held-out error: ${worst.en}, ${worst.error.toFixed(2)} years (page: Alcestis, 9.8)`);
  const best = loo.rows.filter(r => Math.abs(r.error) < 0.5).map(r => r.en).sort();
  ok(best.join() === 'Helen,Medea', `held out, Medea and Helen are dated within half a year (${best.join(', ')})`);
  const ooo = C.outOfOrderPairs(anchors);
  ok(ooo.length === 4 && ooo.every(([p, q]) => (p === 'Alcestis' || p === 'Medea') && q === 'Hippolytus' || p === 'Orestes' && (q === 'Bacchae' || q === 'Iphigenia at Aulis')),
    `4 of 27 anchor pairs out of order: Hippolytus below Alcestis and Medea, Bacchae and Iphigenia at Aulis below Orestes`);
  let pairs = 0; for (let i = 0; i < 8; i++) for (let j = i + 1; j < 8; j++) if (anchors[i].year !== anchors[j].year) pairs++;
  ok(pairs === 27, `27 = the 28 pairs of 8 anchors less the one same-year pair (Bacchae, Iphigenia at Aulis)`);
  const fit = C.fitClock(anchors);
  ok(Math.abs(1 / fit.b - 1.52) < 0.01, `the full fit adds ${(1 / fit.b).toFixed(2)} resolutions per 100 lines a year (page: 1.5)`);
  const date = id => Math.round(-(fit.a + fit.b * S[id].unbracketed.perHundred));
  ok(date('tlg004') === 431 && date('tlg019') === 429 && date('tlg015') === 413 && date('tlg001') === 410 && date('tlg006') === 425 && date('tlg008') === 425 && date('tlg007') === 423 && date('tlg012') === 422 && date('tlg009') === 421 && date('tlg010') === 417 && date('tlg013') === 416,
    `the clock dates Children of Heracles ${date('tlg004')}, Rhesus ${date('tlg019')}, Phoenician Women ${date('tlg015')}, Cyclops ${date('tlg001')} BC (page: 431, 429, 413, 410; and Andromache, Suppliants 425, Hecuba 423, Electra 422, Heracles 421, Ion 417, IT 416)`);
}


// ── 3b. the same test on two counts made by hand ────────────────────────────────
{
  const mine = Object.fromEntries(Object.entries(S).map(([id, r]) => [id, r.unbracketed.perHundred]));
  const ced = PUB.ceadel1941.values;
  const mul = Object.fromEntries(Object.entries(PUB.muller1866.values).map(([id, [a, b]]) => [id, 100 * a / b]));
  ok(Object.keys(ced).length === 18 && !('tlg001' in ced) && Object.keys(mul).length === 19, 'Ceadel: 18 plays (no Cyclops); Müller: 19');
  ok(PUB.ceadel1941.seaford_confirms.every(id => id in ced) && PUB.ceadel1941.seaford_confirms.length === 10, 'ten of the Ceadel figures are the ones Seaford 1982 quotes');
  const rho = (a, b) => {
    const ks = Object.keys(a).filter(k => b[k] != null);
    const rk = o => { const r = {}; ks.slice().sort((x, y) => o[x] - o[y]).forEach((k, i) => r[k] = i); return r; };
    const ra = rk(a), rb = rk(b), n = ks.length;
    return 1 - 6 * ks.reduce((t, k) => t + (ra[k] - rb[k]) ** 2, 0) / (n * (n * n - 1));
  };
  const r1 = rho(mine, ced), r2 = rho(mine, mul), r3 = rho(ced, mul);
  ok(r1.toFixed(3) === '0.992' && r2.toFixed(3) === '0.988' && r3.toFixed(3) === '0.998', `Spearman: this count vs Ceadel ${r1.toFixed(3)}, vs Müller ${r2.toFixed(3)}; Ceadel vs Müller ${r3.toFixed(3)}`);
  const test = src => { const a = C.ANCHORS.map(x => ({ ...x, rate: src[x.id] })); return [C.leaveOneOut(a).rmse, C.outOfOrderPairs(a).map(p => p.join('<')).sort().join()]; };
  const [e0, p0] = test(mine), [e1, p1] = test(ced), [e2, p2] = test(mul);
  ok(e1.toFixed(1) === '5.2' && e2.toFixed(1) === '5.4' && e0 > 5 && e1 > 5 && e2 > 5, `on the hand counts the leave-one-out error is ${e1.toFixed(2)} (Ceadel) and ${e2.toFixed(2)} (Müller): all three above five years`);
  ok(p0 === p1 && p1 === p2, `and all three counts put the same four anchor pairs out of order (${p0})`);
  const rat = Object.keys(ced).map(k => mine[k] / ced[k]).filter(r => r > 1).length;
  ok(rat === 18, 'this count exceeds Ceadel for all 18 plays (it keeps the proper-name resolutions he leaves out)');
}

// ── 4. the two shelves ─────────────────────────────────────────────────────────────
{
  const ALPHA = ['tlg014', 'tlg012', 'tlg004', 'tlg009', 'tlg008', 'tlg018', 'tlg013', 'tlg010', 'tlg001'];
  const col = new Intl.Collator('el', { sensitivity: 'base' });
  const order = Object.keys(E.PLAYS).sort((a, b) => col.compare(E.PLAYS[a].gr, E.PLAYS[b].gr));
  const first = Math.min(...ALPHA.map(id => order.indexOf(id))), last = Math.max(...ALPHA.map(id => order.indexOf(id)));
  const inside = order.slice(first, last + 1).filter(id => !ALPHA.includes(id));
  ok(last - first === 9 && inside.length === 1 && inside[0] === 'tlg005',
    `in Greek alphabetical order the nine run ${E.PLAYS[order[first]].gr} to ${E.PLAYS[order[last]].gr}, broken only by ${inside.map(id => E.PLAYS[id].gr).join(', ')} (Hippolytus, a select play)`);
  const initials = [...new Set(ALPHA.map(id => E.PLAYS[id].gr.normalize('NFD')[0]))].sort().join('');
  ok(initials === 'ΕΗΙΚ', `their initials are ${initials.split('').join(', ')}`);
  const mean = ids => ids.reduce((s, id) => s + S[id].unbracketed.perHundred, 0) / ids.length;
  const sel = Object.keys(E.PLAYS).filter(id => !ALPHA.includes(id));
  ok(Math.abs(mean(ALPHA) - 27.4) < 0.05 && Math.abs(mean(sel) - 21.9) < 0.05, `mean rate ${mean(ALPHA).toFixed(2)} (E-K nine) and ${mean(sel).toFixed(2)} (select ten) (page: 27.4 and 21.9)`);
}

// ── 5. the blind audit ─────────────────────────────────────────────────────────────
{
  let agree = 0, total = 0, reader = 0;
  for (const a of AUDIT.lines) {
    const r = E.scanLine(a.text);
    total += r.resolutions; reader += a.reader;
    if (r.trimeter && r.resolutions === a.reader) agree++;
  }
  ok(AUDIT.lines.length === 150 && agree === 150 && total === 46 && reader === 46, `blind audit: the scanner agrees with the hand scansion on ${agree} of ${AUDIT.lines.length} lines, ${total} resolutions each (page: 150 of 150, 46)`);
  const soft = AUDIT.lines.filter(a => /synizesis/.test(a.note) && /\b0 if|2 if/.test(a.note)).length;
  ok(soft === 4, `${soft} of those lines would lose one resolution under an optional synizesis the metre does not require (page: 4, so 46 could be 42)`);
}

// ── 6. --live: the page's text is Perseus's text ──────────────────────────────────
if (LIVE) {
  const base = 'https://raw.githubusercontent.com/PerseusDL/canonical-greekLit/bcc5df0602f3b3fe6fefe1e1d575602a25ab1db6/data/tlg0006/';
  let same = 0;
  for (const p of DATA.plays) {
    const xml = await (await fetch(`${base}${p.id}/tlg0006.${p.id}.perseus-grc2.xml`)).text();
    const L = E.readLines(xml).map(l => [l.n, l.text, l.bracketed ? 1 : 0]);
    if (JSON.stringify(L) === JSON.stringify(p.lines)) same++;
    else console.log(`      ${p.en}: differs from the Perseus file`);
  }
  ok(same === 19, `live: all ${same} of 19 plays in data/lines.json equal a fresh read of the Perseus TEI at the pinned commit`);
}

console.log(`\n${passes} passed, ${fails} failed`);
process.exit(fails ? 1 : 0);
