#!/usr/bin/env node
// verify-is-your-attention-span-shorter-than-a-goldfish.mjs
//
// Offline gate for /strata/is-your-attention-span-shorter-than-a-goldfish/.
// Recomputes EVERY number the page shows, from first principles, and checks
// self-consistency — where possible two independent ways. Exits 0 iff all pass.
//
//   The claim under test: "human attention span fell to 8 s (from 12 s in 2000),
//   one below a goldfish's 9 s, because of phones." VERIFIED FABRICATION on both
//   halves (BBC More or Less, 10 Mar 2017: traced to Statistic Brain, which
//   produced no substantiating research; the NLM/NCBI and Associated Press it
//   listed had no record). Counter-fact: goldfish MEMORY is measured in weeks
//   (Gee, Stephenson & Wright 1994: a 4-week feeding-time schedule), not 3 s.
//
//   Every constant below mirrors the inline <script> in index.html exactly.

const PRIMARY_SOURCES = 0;      // primary sources backing the 8/9/12 stat
const MYTH_GOLDFISH_S = 3;      // myth: goldfish "3-second memory", seconds
const GEE_WEEKS = 4;            // Gee 1994 feeding schedule length, weeks
const SEC_PER_DAY = 86400;
const GEE_DAYS = GEE_WEEKS * 7;                 // 28 days
const GEE_SECONDS = GEE_DAYS * SEC_PER_DAY;     // 2,419,200 s
const memGapRatio = () => GEE_SECONDS / MYTH_GOLDFISH_S;         // 806,400
const memGapOOM = () => Math.log(memGapRatio()) / Math.LN10;     // ~5.906

// The "measure your own span" panel: the number is the visitor's own input, so
// there is no preset value to verify. What IS verifiable is the arithmetic the
// page runs on those inputs: swing = max(dull, engaging) / min(dull, engaging),
// which is > 1 whenever the two differ, i.e. attention has no single value.
const swing = (dull, engaging) => Math.max(dull, engaging) / Math.min(dull, engaging);

let pass = 0, fail = 0;
function check(name, cond, detail = '') {
  const ok = !!cond;
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${name}${detail ? '  —  ' + detail : ''}`);
  ok ? pass++ : fail++;
}
const approx = (a, b, tol = 1e-9) => Math.abs(a - b) < tol;

console.log('── attention-span myth — trace / span / memory checks ──\n');

// ── 1. The key number: primary sources backing the stat = 0 ─────────────
check('key number: primary sources backing 8s/9s/12s = 0', PRIMARY_SOURCES === 0,
  'BBC 2017 traced to Statistic Brain; NLM/NCBI + AP had no record');

// ── 2. Goldfish feeding schedule in seconds, computed TWO independent ways ─
{
  const wayA = GEE_WEEKS * 7 * SEC_PER_DAY;         // weeks → days → seconds
  const wayB = 28 * 24 * 60 * 60;                   // days → h → min → s
  check('4-week schedule = 2,419,200 s (two ways agree)',
    wayA === wayB && wayA === 2419200,
    `${wayA.toLocaleString('en-US')} = ${wayB.toLocaleString('en-US')}`);
}

// ── 3. The memory gap ratio the page prints ─────────────────────────────
check('memory gap ratio = 806,400×', memGapRatio() === 806400,
  `${GEE_SECONDS.toLocaleString('en-US')} / ${MYTH_GOLDFISH_S} = ${memGapRatio().toLocaleString('en-US')}`);

// ── 4. Gap is "~6 orders of magnitude" (log10 in (5.5, 6.0)) ────────────
check('gap ≈ 6 orders of magnitude', memGapOOM() > 5.5 && memGapOOM() < 6.0,
  `log10 = ${memGapOOM().toFixed(3)}`);

// ── 5. Cross-check the OOM a second way: log10(28*86400) − log10(3) ──────
{
  const alt = (Math.log(GEE_SECONDS) - Math.log(MYTH_GOLDFISH_S)) / Math.LN10;
  check('OOM via log-difference matches log-ratio', approx(alt, memGapOOM()),
    `${alt.toFixed(3)} = ${memGapOOM().toFixed(3)}`);
}

// ── 6. The myth's 3 s is dwarfed by the measured schedule ───────────────
check('myth 3 s ≪ Gee 4-week schedule', MYTH_GOLDFISH_S < GEE_SECONDS,
  `${MYTH_GOLDFISH_S} s vs ${GEE_SECONDS.toLocaleString('en-US')} s`);

// ── 7. The log-axis decade labels the page draws are correct ────────────
//    labels: 1s, 10s, 100s, 1000s≈17min, 1e4≈2.8h, 1e5≈1.2d, 1e6≈11.6d, 1e7≈116d
{
  const min17 = 1000 / 60;                 // 16.67 min
  const h28  = 1e4 / 3600;                 // 2.78 h
  const d12  = 1e5 / SEC_PER_DAY;          // 1.157 d
  const d116 = 1e6 / SEC_PER_DAY;          // 11.57 d
  const d116b = 1e7 / SEC_PER_DAY;         // 115.7 d
  const ok = Math.round(min17) === 17 && Math.abs(h28 - 2.8) < 0.05 &&
             Math.abs(d12 - 1.2) < 0.06 && Math.abs(d116 - 11.6) < 0.05 &&
             Math.abs(d116b - 116) < 0.5;
  check('log-axis decade labels (17 min, 2.8 h, 1.2 d, 11.6 d, 116 d) correct', ok,
    `1e3=${min17.toFixed(1)}min, 1e5=${d12.toFixed(2)}d, 1e6=${d116.toFixed(1)}d`);
}

// ── 8. Gee seconds sit at log10 ≈ 6.38, inside the [0,7] plotted range ───
{
  const l = Math.log(GEE_SECONDS) / Math.LN10;
  check('Gee schedule plots inside the 1s–1e7s axis', l > 0 && l < 7,
    `log10(${GEE_SECONDS.toLocaleString('en-US')}) = ${l.toFixed(2)}`);
}

// ── 9. "Attention has no single value": any two unequal durations swing >1 ─
{
  // representative examples (NOT lab values): a dull task vs an engaging one.
  const cases = [[8, 90], [5, 3600], [12, 1800]];
  let ok = true, detail = [];
  for (const [dull, fun] of cases) {
    const r = swing(dull, fun);
    if (!(r > 1)) ok = false;
    detail.push(`${dull}s/${fun}s→${r.toFixed(1)}×`);
  }
  check('any two unequal on-task times give a swing > 1× (no single value)', ok,
    detail.join('  '));
}

// ── 10. Swing is symmetric (order of the two tasks does not matter) ──────
check('swing(a,b) == swing(b,a)', approx(swing(8, 90), swing(90, 8)),
  `${swing(8, 90).toFixed(3)}`);

// ── 11. Honesty guard: we anchor goldfish memory in WEEKS, not months ───
//    The primary figure used on the page must be the 4-week schedule (28 d),
//    never a hard-stated month value.
check('goldfish memory anchored in weeks (28 d), not a hard month figure',
  GEE_DAYS === 28 && GEE_WEEKS === 4,
  `${GEE_WEEKS} weeks = ${GEE_DAYS} d`);

// ── 12. Sanity: the myth flips a real ranking. Goldfish MEMORY (weeks)
//    beats the myth's claim by the same ~6 OOM; there is no goldfish
//    ATTENTION measurement to compare at all (encoded as PRIMARY_SOURCES=0). ─
check('goldfish-attention measurements existing = 0 (invented, not mismeasured)',
  PRIMARY_SOURCES === 0);

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