// verify.mjs — the offline mirror of /strata/the-drift-that-has-nowhere-to-go/.
//
// DETECT, NEVER ASSERT. Every quantity below is measured by running the same
// engine the page runs, not recalled. This file imports
//     public/strata/the-drift-that-has-nowhere-to-go/engine.js
// directly, so there is no second copy of the simulation to drift out of step,
// and it also READS the shipped index.html and fails if any figure printed in
// that page's prose disagrees with what the engine produces.
//
// ── WHAT IS BEING CHECKED ───────────────────────────────────────────────────
//
// The page's argument has four moving parts, and each gets its own checks.
//
// 1. THE CEILING. The distance from one woman's onset to the nearest of another
//    woman's onsets cannot exceed about half a cycle. If the two are unrelated,
//    the striking of one clock is a moment picked at random inside the other's
//    cycle, so the expected distance to the nearest mark of a set spaced L apart
//    is L/4. Checked directly, and checked to be EXACTLY L/4 in the degenerate
//    case where every cycle has the same length.
//
// 2. THE FORCED RETURN. A bounded quantity with a mean must come back to it, so
//    pairs selected for starting far apart converge with no coupling present.
//    The generator used for that measurement (drawCohort/onsetSeries) contains
//    no interaction term at all, which is checked structurally as well as
//    numerically: the same seed produces byte-identical onsets for a woman
//    whether she is simulated alone or in a crowd.
//
// 3. THE TWO STATISTICS. The 1971-shaped before-and-after gap is compared with
//    the phase coherence of the whole group, each as a mean over independent
//    blocks with the spread between blocks. The claim on the page is NOT that
//    the first statistic is blind. It is that across the range of effects
//    anyone has reported in a human, its response is smaller than its own
//    scatter while the second statistic's is not. That is what is tested.
//
// 4. THE PULL. Two oscillators lock when the coupling beats the detuning. With
//    no cycle-to-cycle variability the boundary is a pull of half the mismatch,
//    which is the textbook Adler/Arnold-tongue result and is checked against it.
//    With real variability the bar rises above the largest shift ever published
//    from a human chemical signal (1.7 days, Stern & McClintock 1998).
//
// Plus: a sweep over the whole plausible parameter box, so the conclusion does
// not rest on one choice of population figures; and a re-derivation of the
// chi-square in Strassmann 1997 Table 3 from the counts as printed.
//
// ── WHAT IS NOT BEING CHECKED ───────────────────────────────────────────────
//
// Nobody else's fieldwork is re-run. The Dogon counts, sample sizes, p-values
// and quotations on the page are transcribed from the papers cited there. The
// single published analysis reproduced is the Table 3 chi-square, and it is
// reproduced from the columns Strassmann printed; agreement with the
// published 1.50 is the check on the transcription, not on the fieldwork.
//
// Refs: M. K. McClintock, Nature 229 (1971) 244. H. C. Wilson,
// Psychoneuroendocrinology 17 (1992) 565. B. I. Strassmann, Current
// Anthropology 38 (1997) 123. K. Stern and M. K. McClintock, Nature 392 (1998)
// 177. J. R. Bull et al., npj Digital Medicine 2 (2019) 83. H. Li et al.,
// npj Digital Medicine 6 (2023) 100. A. C. Cunningham et al., Scientific
// Reports 14 (2024) 10172.
//
// Run:  node research/the-drift-that-has-nowhere-to-go/verify.mjs

import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import * as E from '../../public/strata/the-drift-that-has-nowhere-to-go/engine.js';

const HERE = dirname(fileURLToPath(import.meta.url));
const PAGE = join(HERE, '..', '..', 'public', 'strata', 'the-drift-that-has-nowhere-to-go', 'index.html');

const results = [];
const ok = (name, pass, detail) => { results.push({ name, pass, detail }); };

const D = E.DEFAULTS;

// ═══ 1. THE CEILING ═════════════════════════════════════════════════════════

{
  // The degenerate case with an exact answer: every cycle exactly L days long.
  const p = { ...D, sdBetween: 0, sdWithin: 0 };
  const women = E.drawCohort(1200, 365 * 3, E.mulberry32(11), p);
  const gaps = [];
  for (let i = 0; i + 1 < women.length; i += 2) {
    for (const d of E.pairSeries(women[i], women[i + 1])) if (Number.isFinite(d)) gaps.push(d);
  }
  const m = E.mean(gaps), want = p.meanLen / 4;
  ok('rigid cycles: mean nearest-onset gap = L/4',
    Math.abs(m - want) < 0.12,
    `measured ${m.toFixed(4)} d   L/4 = ${want.toFixed(4)} d   over ${gaps.length} observations`);

  // and its distribution is uniform on [0, L/2]: quartiles at L/8, L/4, 3L/8
  const q = [0.25, 0.5, 0.75].map((t) => E.quantile(gaps, t));
  const wantQ = [1, 2, 3].map((k) => k * p.meanLen / 8);
  ok('rigid cycles: the gap is uniform on [0, L/2]',
    q.every((v, i) => Math.abs(v - wantQ[i]) < 0.25),
    `quartiles ${q.map((v) => v.toFixed(2)).join(', ')}   expected ${wantQ.map((v) => v.toFixed(2)).join(', ')}`);

  const over = gaps.filter((d) => d > p.meanLen / 2 + 1e-9).length;
  ok('rigid cycles: nothing exceeds half a cycle', over === 0,
    `${over} of ${gaps.length} observations above ${(p.meanLen / 2).toFixed(2)} d`);
}

{
  // With real variability the ceiling softens but the average barely moves.
  const f = E.canonicalFindings();
  ok('real variability: mean gap stays near a quarter of a cycle',
    Math.abs(f.meanGap - f.quarterCycle) < 0.5,
    `measured ${f.meanGap.toFixed(4)} d   L/4 = ${f.quarterCycle.toFixed(4)} d   over ${f.nGaps} observations`);
  ok('real variability: about a quarter of all unrelated pairs sit within 3.5 days',
    f.withinHalfWeek > 0.20 && f.withinHalfWeek < 0.30,
    `${(f.withinHalfWeek * 100).toFixed(1)}%  (3.5 d is a quarter of the 14.65 d ceiling)`);
}

// ═══ 2. THE GENERATOR REALLY CANNOT COUPLE ══════════════════════════════════

{
  // Structural: a woman's onsets depend only on her own draws. Simulate the
  // same seed with a cohort of 2 and of 40; woman 0's series must be identical.
  const a = E.drawCohort(2, 400, E.mulberry32(4242));
  const b = E.drawCohort(40, 400, E.mulberry32(4242));
  const same = a[0].onsets.length === b[0].onsets.length &&
    a[0].onsets.every((t, i) => Math.abs(t - b[0].onsets[i]) < 1e-12);
  ok('uncoupled generator: crowd size cannot touch an individual',
    same, `woman 0 identical in a cohort of 2 and of 40 (${a[0].onsets.length} onsets)`);

  // Structural: the source of the default generator contains no cross-woman term.
  const src = readFileSync(join(HERE, '..', '..', 'public', 'strata',
    'the-drift-that-has-nowhere-to-go', 'engine.js'), 'utf8');
  const sig = (src.match(/export function onsetSeries\(([^)]*)\)/) || [, ''])[1];
  const params = sig.split(',').map((t) => t.split('=')[0].trim());
  ok('uncoupled generator: onsetSeries takes no other woman as input',
    params.join(',') === 'mu,days,normal,p,phase0',
    `declared parameters: (${params.join(', ')}) — none of them is another woman, a cohort or a partner`);
}

{
  // Numerical: her own variability alone carries an aligned pair apart, and the
  // closed form for the mean of |difference of two independent normals| holds.
  const drift = E.driftFromAligned();
  ok('one month of her own variability = sd*sqrt(2)*sqrt(2/pi)',
    Math.abs(drift.simulated - drift.predicted) < 0.12,
    `simulated ${drift.simulated.toFixed(4)} d   closed form ${drift.predicted.toFixed(4)} d   over ${drift.n} pairs`);
  ok('that drift exceeds the largest published human cycle shift (1.7 d)',
    drift.simulated > 1.7,
    `${drift.simulated.toFixed(2)} d per month against 1.7 d (Stern & McClintock 1998)`);
}

// ═══ 3. THE TWO STATISTICS ══════════════════════════════════════════════════

{
  const rows = [0, 1, 1.7, 3, 6, 9].map((pull) => ({ pull, s: E.protocolStats(pull), c: E.coherenceStats(pull) }));
  const nullP = rows[0].s, nullC = rows[0].c;
  const z = (v, m0, s0) => Math.abs(v - m0) / s0;

  ok('the 1971 statistic reports a large convergence at ZERO coupling',
    Math.abs(nullP.mean) > 3,
    `${nullP.mean.toFixed(2)} +/- ${nullP.sd.toFixed(2)} days, with no interaction term in the world`);

  const at3 = rows.find((r) => r.pull === 3);
  ok('and it barely moves when the truth changes to 3 days per cycle',
    z(at3.s.mean, nullP.mean, nullP.sd) < 1.5,
    `${at3.s.mean.toFixed(2)} vs ${nullP.mean.toFixed(2)}, a separation of ${z(at3.s.mean, nullP.mean, nullP.sd).toFixed(2)} of its own run-to-run sd`);

  ok('the coherence statistic does move over the same change',
    z(at3.c.mean, nullC.mean, nullC.sd) > 1.5,
    `r ${nullC.mean.toFixed(3)} -> ${at3.c.mean.toFixed(3)}, a separation of ${z(at3.c.mean, nullC.mean, nullC.sd).toFixed(2)} sd`);

  // Not a claim that the gap statistic is blind. At a pull of one day per cycle
  // NEITHER statistic separates; the point is that from 1.7 days per cycle
  // upwards, coherence pulls away and the gap statistic does not.
  const from17 = rows.filter((r) => r.pull >= 1.7);
  ok('from 1.7 days per cycle upward, coherence separates and the gap statistic does not',
    from17.every((r) => z(r.c.mean, nullC.mean, nullC.sd) > z(r.s.mean, nullP.mean, nullP.sd)),
    rows.slice(1).map((r) => `${r.pull}d: gap ${z(r.s.mean, nullP.mean, nullP.sd).toFixed(1)}sd vs coh ${z(r.c.mean, nullC.mean, nullC.sd).toFixed(1)}sd`).join('  '));

  ok('the coherence null sits at the finite-N incoherent floor, not at zero',
    Math.abs(nullC.mean - E.incoherentFloor(E.R_N)) < 0.06,
    `measured ${nullC.mean.toFixed(4)}   sqrt(pi)/2/sqrt(${E.R_N}) = ${E.incoherentFloor(E.R_N).toFixed(4)}`);

  ok('the coherence statistic is monotone in the true coupling',
    rows.every((r, i) => i === 0 || r.c.mean > rows[i - 1].c.mean - 0.02),
    rows.map((r) => `${r.pull}d:${r.c.mean.toFixed(3)}`).join('  '));
}

{
  // The far-starting pairs converge; the close-starting pairs spread. Both, with
  // no coupling. That symmetry is the signature of regression, not attraction.
  const r = E.runProtocol({ truePull: 0 });
  ok('with no coupling, far-starting pairs close AND close-starting pairs open',
    r.aboveChange < -2 && r.belowChange > 2,
    `above median ${r.aboveFirst.toFixed(2)} -> ${r.aboveLast.toFixed(2)} d;  below median ${r.belowFirst.toFixed(2)} -> ${r.belowLast.toFixed(2)} d`);
  ok('both groups land on the same place, the population mean gap',
    Math.abs(r.aboveLast - r.belowLast) < 1.6,
    `${r.aboveLast.toFixed(2)} vs ${r.belowLast.toFixed(2)}, against a population mean of about ${(D.meanLen / 4).toFixed(2)}`);
}

// ═══ 4. THE PULL YOU WOULD NEED ═════════════════════════════════════════════

{
  // The case with an exact answer: no variability, so locking happens when the
  // pull reaches half the mismatch (the Adler / Arnold-tongue boundary).
  const c = E.lockingCurve({ jitter: 0, dLs: [1, 2, 3, 4, 6], stepPull: 0.1, maxPull: 6 });
  const errs = c.map((q) => (q.pull === null ? 99 : Math.abs(q.pull - q.dL / 2)));
  ok('no variability: the locking boundary is a pull of half the mismatch',
    Math.max(...errs) <= 0.31,
    c.map((q) => `dL=${q.dL}: needed ${q.pull === null ? '>6' : q.pull.toFixed(2)} vs dL/2=${(q.dL / 2).toFixed(2)}`).join('  '));
}

{
  const cReal = E.lockingCurve({ jitter: D.sdWithin, dLs: [0.5, 1, 2, 3, 4, 6] });
  const reachable = cReal.filter((q) => q.pull !== null && q.pull <= 1.7);
  ok('with real variability, no mismatch on the axis is reachable by a 1.7 d pull',
    reachable.length === 0,
    cReal.map((q) => `dL=${q.dL}:${q.pull === null ? '>10' : q.pull.toFixed(2)}`).join('  ') + '   (largest published shift 1.7 d)');

  ok('the bar rises monotonically with her own variability',
    (() => {
      const need = [0, 1, 2, 3, 4].map((j) => E.lockingCurve({ jitter: j, dLs: [2] })[0].pull);
      return need.every((v, i) => i === 0 || v === null || v >= need[i - 1] - 0.26);
    })(),
    [0, 1, 2, 3, 4].map((j) => `sd=${j}:${(E.lockingCurve({ jitter: j, dLs: [2] })[0].pull ?? 99).toFixed(2)}`).join('  '));
}

// ═══ 5. WHAT A YEAR CONTAINS ════════════════════════════════════════════════

{
  const y = E.yearTogether({});
  ok('a year of two unrelated women contains the coincidence almost always',
    y.overlap > 0.85 && y.close > 0.4,
    `bleeding overlapped at least once: ${(y.overlap * 100).toFixed(1)}%;  a month starting within a day: ${(y.close * 100).toFixed(1)}%   (n=${y.n} pairs)`);
  ok('and contains the opposite evidence just as often',
    y.far > 0.7 && y.both > 0.3,
    `a month 12+ days apart: ${(y.far * 100).toFixed(1)}%;  both experiences in the same year: ${(y.both * 100).toFixed(1)}%`);
  // and the figure we print is the conservative one
  const long = E.yearTogether({ p: { ...D, mensesDays: 6.2 } });
  ok('the bleeding duration we chose understates the overlap rather than flattering it',
    long.overlap >= y.overlap,
    `4.0 d (Bull 2019, used) -> ${(y.overlap * 100).toFixed(1)}%;  6.2 d (Najmabadi 2020, diary) -> ${(long.overlap * 100).toFixed(1)}%`);
}

// ═══ 6. THE WHOLE PLAUSIBLE PARAMETER BOX ═══════════════════════════════════

{
  // The published figures disagree with one another, so the conclusion must not
  // depend on which of them we picked. Sweep the range each is reported over.
  const MEANS = [27.7, 28.5, 29.3, 30.3];
  const BETW = [2.0, 3.2, 4.5];
  const WITH = [2.6, 3.9, 5.1];
  let worstConv = Infinity, worstReach = 0, n = 0, badCells = [], reachCells = [];
  for (const meanLen of MEANS) for (const sdBetween of BETW) for (const sdWithin of WITH) {
    const p = { ...D, meanLen, sdBetween, sdWithin };
    const s = E.protocolStats(0, p);
    n++;
    if (Math.abs(s.mean) < worstConv) worstConv = Math.abs(s.mean);
    if (Math.abs(s.mean) < 2) badCells.push(`${meanLen}/${sdBetween}/${sdWithin}=${s.mean.toFixed(2)}`);
    const lock = E.lockingCurve({ jitter: sdWithin, dLs: [2], p })[0].pull;
    if (lock !== null && lock <= 1.7) { worstReach++; reachCells.push(`${meanLen}/${sdBetween}/${sdWithin}`); }
  }
  ok('the artifact appears everywhere in the plausible parameter box',
    badCells.length === 0,
    `${n} cells swept (mean cycle ${MEANS.join('/')}, between-woman sd ${BETW.join('/')}, within-woman sd ${WITH.join('/')}); smallest apparent convergence anywhere ${worstConv.toFixed(2)} d`);
  ok('and a 1.7 d pull holds a 2-day mismatch in almost none of them',
    worstReach <= 3 && reachCells.every((c) => c.endsWith('/2.6')),
    `${worstReach} of ${n} cells reachable by the largest published shift` +
    (worstReach ? `; every exception uses the lowest within-woman variability, 2.6 d: ${reachCells.join(' ')}` : ''));
}

// ═══ 7. THE ONE PUBLISHED ANALYSIS REPRODUCED ═══════════════════════════════

{
  // Strassmann 1997, Current Anthropology 38(1):123-129, Table 3. Days of a
  // 736-day census carrying k menstrual onsets, observed and expected under
  // independence, as printed in the paper.
  const obs = [346, 260, 101, 25, 4];
  const exp = [359.7, 254.7, 93.0, 23.3, 4.5];
  const chi2 = (o, e) => o.reduce((s, v, i) => s + (v - e[i]) ** 2 / e[i], 0);
  const chi = chi2(obs, exp);
  ok('Strassmann 1997 Table 3: the printed chi-square recomputes from the printed columns',
    Math.abs(chi - 1.50) < 0.005,
    `recomputed ${chi.toFixed(4)}   published 1.50   (this is also the check on our transcription)`);

  const days = obs.reduce((a, b) => a + b, 0), onsets = obs.reduce((a, b, i) => a + b * i, 0);
  const lam = onsets / days;
  const flat = []; let pr = Math.exp(-lam);
  for (let k = 0; k < 5; k++) { flat.push(days * pr); pr *= lam / (k + 1); }
  const chiFlat = chi2(obs, flat);
  const upperTail = (x, df) => { let s = 0, t = 1; for (let k = 0; k < df / 2; k++) { s += t; t *= (x / 2) / (k + 1); } return Math.exp(-x / 2) * s; };
  ok('and the verdict does not depend on how the expectation was modelled',
    upperTail(chi, 4) > 0.1 && upperTail(chiFlat, 4) > 0.1,
    `her expectation: chi2=${chi.toFixed(3)}, p=${upperTail(chi, 4).toFixed(3)};  a plain Poisson at ${lam.toFixed(4)}/day: chi2=${chiFlat.toFixed(3)}, p=${upperTail(chiFlat, 4).toFixed(3)}`);
}

// ═══ 8. THE PAGE ITSELF ═════════════════════════════════════════════════════

{
  // Read the shipped HTML and confirm that every figure printed in its prose is
  // a figure this engine produces. A page whose prose has drifted from its own
  // engine is exactly the failure this whole stratum is about.
  const html = readFileSync(PAGE, 'utf8');
  const f = E.canonicalFindings();
  const table = new Map(E.ledgerRows(f).map((r) => [r[0], r[2]]));
  const prose = html.slice(0, html.indexOf('<script type="module">'));
  const printed = [...prose.matchAll(/data-key="([^"]+)"[^>]*>([^<]*)</g)].map((m) => [m[1], m[2].trim()]);
  const bad = printed.filter(([k, v]) => !table.has(k) || table.get(k) !== v);
  ok('every data-key figure in the page prose matches the engine',
    printed.length >= 8 && bad.length === 0,
    `${printed.length} figures checked` + (bad.length ? '; MISMATCH ' + bad.map(([k, v]) => `${k}: page "${v}" vs engine "${table.get(k)}"`).join(', ') : ''));

  ok('the page prints the command that runs this file',
    html.includes('research/the-drift-that-has-nowhere-to-go/verify.mjs'),
    'the reproduce-it-yourself line resolves to this verifier');

  ok('the page states the coupling in its default world is zero',
    /no interaction term/.test(html),
    'the claim that the simulated women cannot influence one another is made on the page and checked above');

  // the Dogon panel's constants must be the ones this verifier used
  ok('the page ships the same Strassmann counts this file reproduces',
    html.includes('[346, 260, 101, 25, 4]') && html.includes('[359.7, 254.7, 93.0, 23.3, 4.5]'),
    'observed and expected columns identical in page and verifier');
}

// ── report ──────────────────────────────────────────────────────────────────
console.log('Checks:');
let passed = 0;
for (const r of results) {
  console.log(`  ${r.pass ? 'PASS' : 'FAIL'}  ${r.name}\n        ${r.detail}`);
  if (r.pass) passed++;
}
console.log('\n== scope');
console.log('  This verifier does NOT re-run anybody else\'s fieldwork, and does not prove that no');
console.log('  coupling between menstrual cycles exists. It reproduces the arithmetic of the artifact,');
console.log('  shows the classic statistic barely separates zero coupling from any published effect');
console.log('  size, shows a 1.7 d pull cannot hold a typical pair against real cycle variability,');
console.log('  and re-derives one published chi-square from its own printed columns. The empirical');
console.log('  null results on the page are cited (Strassmann 1997; Trevathan et al. 1993;');
console.log('  Ziomkiewicz 2006; Yang & Schank 2006), not reproduced.');
console.log(`\n${passed}/${results.length} checks passed.`);
process.exit(passed === results.length ? 0 : 1);
