// verify-before-the-moon-comes-up.mjs: checks every number /strata/before-the-moon-comes-up/
// prints, against the engine and data the page actually runs, and against second implementations
// written here.
//
//   node verify-before-the-moon-comes-up.mjs
//
// Needs only Node 18+. Inside the repository it tests the local files; from an empty directory it
// downloads the page's modules and data from https://artwaste.land (the bytes your browser runs)
// into a temporary folder and tests those. Exit 0 = every check passed. Takes about a minute.
//
// What it cannot check from outside: that PREREGISTRATION.md was pushed before the held-out tests
// were run. That order is recorded in this project's git history (commit dbce804b, 2026-09-26
// 06:38 UTC), which is private; the file itself is published beside the page.

import { existsSync, readFileSync, mkdtempSync, writeFileSync, mkdirSync } from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { dirname, join } from 'node:path';
import { tmpdir } from 'node:os';

const here = dirname(fileURLToPath(import.meta.url));
const slug = 'before-the-moon-comes-up';
const site = 'https://artwaste.land';
let dir = join(here, 'public/strata', slug);
// night.mjs imports the site's shared engine by relative path (../../tools/sky-now/ephem.mjs),
// so a download keeps the site's layout: <tmp>/strata/<slug>/ and <tmp>/tools/sky-now/.
const FILES = ['night.mjs', 'place.mjs', 'stats.mjs', 'index.html', 'data/spawn.json', 'data/results.json', 'data/jpl.json'];
if (!existsSync(join(dir, 'night.mjs'))) {
  const root = mkdtempSync(join(tmpdir(), slug + '-'));
  dir = join(root, 'strata', slug);
  mkdirSync(join(dir, 'data'), { recursive: true });
  mkdirSync(join(root, 'tools', 'sky-now'), { recursive: true });
  const get = async (url, to) => {
    const res = await fetch(url);
    if (!res.ok) { console.log(`FAIL  could not download ${url}: HTTP ${res.status}`); process.exit(1); }
    writeFileSync(to, Buffer.from(await res.arrayBuffer()));
  };
  for (const f of FILES) await get(`${site}/strata/${slug}/${f}`, join(dir, f));
  await get(`${site}/tools/sky-now/ephem.mjs`, join(root, 'tools', 'sky-now', 'ephem.mjs'));
}
console.error(`files: ${dir}`);
const imp = f => import(pathToFileURL(join(dir, f)).href);
const N = await imp('night.mjs'), P = await imp('place.mjs'), S = await imp('stats.mjs');
const J = f => JSON.parse(readFileSync(join(dir, f), 'utf8'));
const data = J('data/spawn.json'), results = J('data/results.json'), jpl = J('data/jpl.json');
const html = readFileSync(join(dir, 'index.html'), 'utf8');

let fails = 0, passes = 0;
const ok = (c, msg) => { if (c) passes++; else fails++; console.log((c ? 'PASS  ' : 'FAIL  ') + msg); };
const ms = s => Date.parse(s);
const page = {};
for (const m of html.matchAll(/data-n="([^"]+)">([^<]*)</g)) page[m[1]] = m[2].replace(/&minus;/g, '−').replace(/&deg;/g, '°');
const fmtInt = n => n.toLocaleString('en-US');
const sgn = (x, d) => (x < 0 ? '−' : '') + Math.abs(x).toFixed(d);
const pageIs = (key, expect, what) => ok(page[key] === expect, `page says ${what}: "${page[key]}" (computed: "${expect}")`);

// ── 0. one engine ───────────────────────────────────────────────────────────
ok(/from '\.\.\/\.\.\/tools\/sky-now\/ephem\.mjs'/.test(readFileSync(join(dir, 'night.mjs'), 'utf8')), 'night.mjs takes the Sun and Moon from the site\'s shared engine (/tools/sky-now/ephem.mjs), imported, not copied');

// ── 1. the ephemeris against JPL DE440s ─────────────────────────────────────
const eng = N.fullMoons(Date.UTC(1969, 11, 15), Date.UTC(2031, 0, 1));
const JF = jpl.fullMoons.map(ms);
const nearest = (arr, t) => arr.reduce((b, x) => Math.abs(x - t) < Math.abs(b - t) ? x : b, arr[0]);
let worstFm = 0;
for (const j of JF) worstFm = Math.max(worstFm, Math.abs(nearest(eng, j) - j) / 60000);
const engInRange = eng.filter(x => x > JF[0] - 15 * 864e5 && x < JF.at(-1) + 15 * 864e5).length;
ok(JF.length === 754 && engInRange === 754, `engine finds the same ${engInRange} full moons 1970-2030 as JPL (${JF.length})`);
ok(worstFm < 10, `engine full moons within ${worstFm.toFixed(2)} min of JPL (tolerance 10)`);
pageIs('engFm', worstFm.toFixed(1), 'the engine\'s worst full-moon error (min)');
let worstSunset = 0, worstRise = 0, flips = 0;
for (const q of jpl.nights) {
  const [y, m, d] = q.date.split('-').map(Number);
  const ev = N.evening(q.lat, q.lon, q.tz, y, m, d);
  worstSunset = Math.max(worstSunset, Math.abs(ev.sunset - ms(q.sunset)) / 1000);
  if (q.moonrise && ev.moonrise) worstRise = Math.max(worstRise, Math.abs(ev.moonrise - ms(q.moonrise)) / 1000);
  if ((q.moonAltAtSunsetDeg > -0.825) !== ev.moonUp) flips++;
}
ok(jpl.nights.length === 400 && flips === 0, `on ${jpl.nights.length} random evenings at the database's reefs, Moon up or down at sunset agrees with JPL on every one (${flips} disagreements)`);
ok(worstSunset < 60 && Math.ceil(worstSunset) === +page.engSunset, `sunset within ${worstSunset.toFixed(1)} s of JPL (page: ${page.engSunset} s)`);
ok(worstRise < 120 && Math.ceil(worstRise) === +page.engRise, `moonrise within ${worstRise.toFixed(1)} s of JPL (page: ${page.engRise} s)`);

// ── 2. the database's own full-moon table ───────────────────────────────────
const CF = data.csdFullMoons.map(ms);
const cErr = CF.map(c => Math.abs(c - nearest(JF, c)) / 60000).sort((a, b) => a - b);
ok(CF.length === 754 && cErr.at(-1) < 24 * 60, `all ${CF.length} rows of the database's full-moon table match a JPL full moon`);
pageIs('fmRows', String(CF.length), 'rows in the full-moon table');
pageIs('fmWorst', cErr.at(-1).toFixed(1), 'the table\'s worst error (min)');
pageIs('fmMedian', cErr[cErr.length >> 1].toFixed(1), 'the table\'s median error (min)');

// ── 3. every record re-placed on its evening ────────────────────────────────
const place = P.makePlacer();
const ci = Object.fromEntries(data.cols.map((c, i) => [c, i]));
const derived = ['nightsSinceFmDate', 'fmDate', 'fmLocalHour', 'fdeOffset', 'fdeOffset30', 'sunsetLocal', 'moonUp', 'darkGapH', 'moonriseAfterSunsetH', 'startAfterSunsetH'];
let mismatch = 0;
for (const a of data.rows) {
  const s = data.sites[a[ci.site]];
  const p = place(s[3], s[4], s[5], a[ci.date], a[ci.start]);
  for (const k of derived) if (p[k] !== a[ci[k]]) { mismatch++; break; }
}
ok(mismatch === 0, `all ${data.rows.length} records re-placed from site and date reproduce every derived column in spawn.json (${mismatch} mismatches)`);
const ROWS = S.expand(data);
pageIs('records', fmtInt(ROWS.length), 'the number of records');
// sites[] holds the database's SUB-sites; the paper's "101 sites" is the distinct names (fixed 2026-09-27, the page had printed 181 as sites)
pageIs('sites', String(new Set(data.sites.map(x => x[0] + '|' + x[1])).size), 'the number of sites (distinct names)');
pageIs('subsites', String(data.sites.length), 'the number of sub-sites');
const insitu = ROWS.filter(r => r.situation === 'In situ').length;
pageIs('insituN', fmtInt(insitu), 'in situ records');
pageIs('exsitu', fmtInt(ROWS.length - insitu), 'ex situ records');

// ── 4. the database's night counts ──────────────────────────────────────────
const agree = ROWS.filter(r => r.csdFmDate === r.fmDate && r.csdNights === r.nightsSinceFmDate);
const dis = ROWS.filter(r => !(r.csdFmDate === r.fmDate && r.csdNights === r.nightsSinceFmDate));
pageIs('csdAgree', fmtInt(agree.length), 'records whose night count the database and this page agree on');
const india = dis.filter(r => r.tz === 5.5), eilat = dis.filter(r => r.siteName === 'Eilat');
ok(dis.length === 4 && india.length === 2 && eilat.length === 2, `the four disagreements are two Indian records at UTC+5:30 and two from Eilat (${dis.map(r => r.country + ' ' + r.date).join('; ')})`);
{
  const fm = nearest(JF, Date.UTC(2008, 2, 21, 18, 40));
  const loc = new Date(fm + 5.5 * 3600000);
  ok(Math.abs(fm - Date.UTC(2008, 2, 21, 18, 40)) < 3 * 60000 && loc.getUTCDate() === 22 && loc.getUTCHours() === 0,
    `JPL: the full moon of March 2008 was at ${new Date(fm).toISOString().slice(0, 16)} UTC, ${loc.toISOString().slice(11, 16)} on the 22nd at UTC+5:30; the database dated it ${india[0].csdFmDate}`);
  const e = eilat[0];
  const sunsetMs = N.evening(e.lat, e.lon, e.tz, 2016, 8, 3).sunset;
  const a = nearest(JF, Date.UTC(2016, 6, 20)), b = nearest(JF, Date.UTC(2016, 7, 18));
  const da = (sunsetMs - a) / 864e5, db = (b - sunsetMs) / 864e5, gap = Math.abs(da - db) * 1440;
  ok(e.csdFmDate === '2016-07-20' && e.fmDate === '2016-08-18' && da.toFixed(1) === '14.7' && db.toFixed(1) === '14.7' && gap < 60,
    `Eilat, 3 Aug 2016: ${da.toFixed(2)} days after one full moon and ${db.toFixed(2)} before the next (by JPL), ${gap.toFixed(0)} minutes apart; the database takes the nearer date, the page the nearer instant`);
}

// ── 5. the dark at the start ────────────────────────────────────────────────
const timed = ROWS.filter(r => r.startAfterSunsetH != null && r.startAfterSunsetH > 0 && r.startAfterSunsetH < 8);
const darkRule = r => r.moonUpAtStart === 0;
const nDark = timed.filter(darkRule).length;
// independent twin: classify each start by the Moon's rise and set EVENTS instead of its altitude.
// Moon down at sunset: dark if the next moonrise is after the start. Moon up at sunset: dark only
// if it set after sunset and before the start (and had not risen again).
let twin = 0, edge = 0;
for (const r of timed) {
  const d = r.date.split('-').map(Number);
  const ev = N.evening(r.lat, r.lon, r.tz, ...d);
  const st = ev.sunset + r.startAfterSunsetH * 3600000;
  let dark;
  if (!ev.moonUp) dark = ev.moonrise > st;
  else {
    const set = N.crossing(t => N.moonAlt(t, r.lat, r.lon), ev.sunset, ev.sunset + 12 * 3600000, -1);
    dark = set != null && set < st && !(ev.moonrise < st);
  }
  if (dark) twin++;
  if (dark !== darkRule(r) && Math.min(Math.abs(st - ev.moonrise), 1e12) < 5000) edge++;
}
ok(twin === nDark, `twin: classifying each start by moonrise and moonset events gives ${twin} starts in the dark, the Moon's altitude at the start instant gives ${nDark}`);
pageIs('timed', fmtInt(timed.length), 'timed evening starts');
pageIs('dark', fmtInt(nDark), 'starts with the Moon down');
pageIs('darkPct', (100 * nDark / timed.length).toFixed(1) + '%', 'the percentage');
for (const c of [4, 5, 6]) {
  const t = timed.filter(r => r.nightsSinceFmDate === c);
  pageIs('n' + c, `${t.filter(darkRule).length} of ${t.length}`, `night ${c}`);
}
ok(results.summary.darkAtStart === nDark && results.summary.timedEveningStarts === timed.length, 'results.json agrees');

// ── 6. how much later the Moon rises each evening ──────────────────────────
{
  const cycles = new Map();
  for (const r of ROWS) cycles.set(r.lat + '|' + r.lon + '|' + r.tz + '|' + r.fmDate, r);
  const delays = [];
  for (const r of cycles.values()) {
    const d0 = r.fmDate.split('-').map(Number);
    let prev = null;
    for (let k = 1; k <= 8; k++) {
      const dk = N.addDays(d0, k);
      const ev = N.evening(r.lat, r.lon, r.tz, ...dk);
      const t = ev.moonUp ? null : ev.moonrise - N.localMidnight(...dk, r.tz);
      if (prev != null && t != null) delays.push((t - prev) / 60000);
      prev = t;
    }
  }
  delays.sort((a, b) => a - b);
  const q = p => (Math.round(delays[Math.floor(p * delays.length)] * 10) / 10).toFixed(1);
  pageIs('delayMedian', q(0.5), 'the median nightly moonrise delay (min)');
  pageIs('delayRange', `${q(0.1)} to ${q(0.9)}`, 'its 10th to 90th percentile');
  ok(cycles.size === 727, `${cycles.size} lunar cycles (a reef and a full-moon date)`);
  pageIs('cycles', String(cycles.size), 'lunar cycles');
  const cyc = [...cycles.values()];
  const early = cyc.filter(r => r.fmLocalHour < 10), late = cyc.filter(r => r.fmLocalHour >= 21);
  pageIs('early', `${early.filter(r => r.fdeOffset === 0).length} of ${early.length}`, 'full moons before 10:00 whose first dark dusk was that evening');
  pageIs('late', `${late.filter(r => r.fdeOffset === 1).length} of ${late.length}`, 'full moons from 21:00 whose first dark dusk was the next evening');
  const exc = early.filter(r => r.fdeOffset !== 0);
  ok(exc.length === 1 && exc[0].siteName === 'Kochi' && exc[0].fdeOffset === -1 && Math.round(exc[0].lat) === 33, `the one early exception is ${exc.map(r => `${r.siteName} (${r.lat.toFixed(1)}°N, ${r.fmDate}), first dark dusk the evening before`).join('')}`);
}

// ── 7. the tests, rerun ─────────────────────────────────────────────────────
const T = results.tests;
const same = (a, b) => Math.abs(a.beta - b.beta) < 1e-12 && Math.abs(a.lo - b.lo) < 1e-12 && Math.abs(a.hi - b.hi) < 1e-12 && a.events === b.events && a.verdict === b.verdict;
const GBR = 'Central and northern Great Barrier Reef', SGBR = 'Southern Great Barrier Reef (including Keppel Islands and Capricorn Bunker Reefs)';
const re = {
  all: S.test(S.subset(ROWS, 'all')),
  heldout: S.test(S.subset(ROWS, 'heldout')),
  sgbr: S.test(S.subset(ROWS, SGBR)),
  palau: S.test(S.subset(ROWS, 'Palau')),
};
ok(same(re.all, T.exploratory.all), `all regions rerun: beta ${re.all.beta.toFixed(3)} [${re.all.lo.toFixed(3)}, ${re.all.hi.toFixed(3)}], ${re.all.events} events, identical to results.json`);
for (const e of S.EXPLORED) { const r = S.test(S.subset(ROWS, e)); ok(same(r, T.exploratory[e]), `${e}: beta ${r.beta.toFixed(3)} [${r.lo.toFixed(3)}, ${r.hi.toFixed(3)}] (${r.verdict})`); }
ok(same(re.heldout, T.preregistered.heldout) && re.heldout.hi < 1, `PRE-REGISTERED held out: beta ${re.heldout.beta.toFixed(3)} [${re.heldout.lo.toFixed(3)}, ${re.heldout.hi.toFixed(3)}], ${re.heldout.events} events: ${re.heldout.verdict}`);
ok(same(re.sgbr, T.preregistered[SGBR]), `pre-registered southern GBR: beta ${re.sgbr.beta.toFixed(3)} [${re.sgbr.lo.toFixed(3)}, ${re.sgbr.hi.toFixed(3)}], ${re.sgbr.events} events`);
ok(same(re.palau, T.preregistered.Palau), `pre-registered Palau: beta ${re.palau.beta.toFixed(3)} [${re.palau.lo.toFixed(3)}, ${re.palau.hi.toFixed(3)}], ${re.palau.events} events`);
const heldRegions = new Set(S.subset(ROWS, 'heldout').map(r => r.ecoregion));
ok(![...heldRegions].some(r => S.EXPLORED.includes(r)) && heldRegions.size > 20, `the held-out set shares no region with the six explored ones (${heldRegions.size} regions)`);
pageIs('hoBeta', sgn(re.heldout.beta, 2), 'held-out beta');
pageIs('hoCI', `${sgn(re.heldout.lo, 2)} to ${sgn(re.heldout.hi, 2)}`, 'held-out interval');
pageIs('hoEvents', String(re.heldout.events), 'held-out events');
pageIs('allBeta', sgn(re.all.beta, 2), 'pooled beta');
pageIs('allCI', `${sgn(re.all.lo, 2)} to ${sgn(re.all.hi, 2)}`, 'pooled interval');
pageIs('allEvents', String(re.all.events), 'pooled events');
ok(re.all.hi - re.all.lo < 1, `the pooled interval is ${(re.all.hi - re.all.lo).toFixed(2)} wide, under 1, so it can tell a timer (1) from none (0)`);
pageIs('gbrBeta', sgn(T.exploratory[GBR].beta, 2), 'GBR beta');
pageIs('palBeta', sgn(re.palau.beta, 2), 'Palau beta');

// independent twin for beta: minimise the sum of squares with group intercepts by golden-section search
{
  const rows = S.subset(ROWS, 'all');
  const g = new Map();
  for (const r of rows) { const k = r.taxon + '|' + r.ecoregion; if (!g.has(k)) g.set(k, []); g.get(k).push(r); }
  const groups = [...g.values()].filter(v => v.length >= 2);
  const sse = b => { let s = 0; for (const v of groups) { const a = v.reduce((t, r) => t + r.nightsSinceFmDate - b * r.fdeOffset, 0) / v.length; for (const r of v) s += (r.nightsSinceFmDate - a - b * r.fdeOffset) ** 2; } return s; };
  let lo = -5, hi = 5; const phi = (Math.sqrt(5) - 1) / 2;
  for (let i = 0; i < 90; i++) { const m1 = hi - phi * (hi - lo), m2 = lo + phi * (hi - lo); if (sse(m1) < sse(m2)) hi = m2; else lo = m1; }
  const bt = (lo + hi) / 2;
  ok(Math.abs(bt - re.all.beta) < 1e-6, `twin: least squares with a separate intercept per species-on-reef, minimised by golden-section search, gives beta ${bt.toFixed(6)} (stats.mjs: ${re.all.beta.toFixed(6)})`);
}

// ── 8. sensitivity, joint model, species ────────────────────────────────────
{
  const g = S.subset(ROWS, GBR), all = S.subset(ROWS, 'all');
  const perm = S.permutation(g);
  ok(Math.abs(perm.p - T.sensitivity[GBR].permutation.p) < 1e-12, `GBR permutation p = ${perm.p.toFixed(3)} (1,000 within-region shuffles of which events had a late dark dusk)`);
  pageIs('gbrP', perm.p.toFixed(3), 'the GBR permutation p');
  const anyOfSix = 1 - (1 - perm.p) ** 6;
  ok(anyOfSix > 1 / 7 && anyOfSix < 1 / 6 + 0.01, `the chance of a p this small in at least one of six independent looks is ${anyOfSix.toFixed(3)}, "roughly one chance in six"`);
  const g30 = S.test(S.with30(g));
  ok(same(g30, T.sensitivity[GBR].darkGapAtLeast30min), `GBR with a 30-minute dark gap: ${g30.beta.toFixed(3)} [${g30.lo.toFixed(3)}, ${g30.hi.toFixed(3)}]`);
  pageIs('gbr30', sgn(g30.beta, 2), 'GBR 30-minute beta');
  pageIs('gbr30CI', `${sgn(g30.lo, 2)} to ${sgn(g30.hi, 2)}`, 'its interval');
  const ins = S.test(all.filter(r => r.situation === 'In situ'));
  ok(same(ins, T.sensitivity.all.inSituOnly), `in situ only: ${ins.beta.toFixed(3)} [${ins.lo.toFixed(3)}, ${ins.hi.toFixed(3)}]`);
  pageIs('insitu', sgn(ins.beta, 2), 'in situ beta');
  pageIs('insituCI', `${sgn(ins.lo, 2)} to ${sgn(ins.hi, 2)}`, 'its interval');
  const j = S.jointTest(all);
  ok(Math.abs(j.delta - T.joint.all.delta) < 1e-12 && Math.abs(j.hourCI[1] - T.joint.all.hourCI[1]) < 1e-12 && j.deltaCI[1] < 1, `joint model: dark dusk ${j.delta.toFixed(3)} [${j.deltaCI.map(x => x.toFixed(3)).join(', ')}], clock time ${j.hour.toFixed(3)} [${j.hourCI.map(x => x.toFixed(3)).join(', ')}]`);
  pageIs('jDelta', sgn(j.delta, 2), 'joint dark-dusk coefficient');
  pageIs('jDeltaCI', `${sgn(j.deltaCI[0], 2)} to ${sgn(j.deltaCI[1], 2)}`, 'its interval');
  pageIs('jHour', sgn(j.hour, 2), 'joint clock-time coefficient');
  pageIs('jHourCI', `${sgn(j.hourCI[0], 2)} to ${sgn(j.hourCI[1], 2)}`, 'its interval');
  const dsp = ROWS.filter(r => r.taxon === 'Dipsastraea speciosa'), lyu = dsp.filter(r => r.siteName === 'Lyudao');
  pageIs('dspN', String(dsp.length), 'Dipsastraea speciosa records');
  pageIs('lyuN', String(lyu.length), 'of them at Lyudao');
  ok(lyu.every(r => r.fdeOffset === 0 && r.ref === 'Lin & Nozawa 2017' && r.country === 'Taiwan'), `every Lyudao record is from "Lin & Nozawa 2017" and every Lyudao cycle had its first dark dusk on the full-moon date`);
  // spread of one species on one reef, the limit named in section 6
  const gg = new Map();
  for (const r of all) { const k = r.taxon + '|' + r.ecoregion; if (!gg.has(k)) gg.set(k, []); gg.get(k).push(r.nightsSinceFmDate); }
  let ss = 0, n = 0;
  for (const v of gg.values()) { if (v.length < 2) continue; const m = v.reduce((a, b) => a + b) / v.length; for (const x of v) { ss += (x - m) ** 2; n++; } }
  pageIs('spread', Math.sqrt(ss / n).toFixed(1), 'the within-group spread (nights)');
}

// ── 9b. the sources: every DOI printed, and linked, is the one checked against crossref ──
{
  // Looked up 2026-09-26 at api.crossref.org (and the dataset at figshare/Newcastle): each resolves to the work cited.
  const DOIS = ['10.1007/BF00428562', '10.1007/s00338-025-02618-9', '10.1038/s41467-023-38070-y', '10.1038/s41597-020-00793-8',
    '10.1073/pnas.2101985118', '10.1126/science.223.4641.1186', '10.25405/data.ncl.13082333.v1'];
  const linked = [...new Set([...html.matchAll(/https:\/\/doi\.org\/([^"]+)"/g)].map(m => m[1]))].sort();
  const printed = [...new Set([...html.matchAll(/>doi:([^<]+)</g)].map(m => m[1]))].sort();
  ok(JSON.stringify(linked) === JSON.stringify([...DOIS].sort()), `the page links exactly the ${DOIS.length} checked DOIs (${linked.length} linked)`);
  ok(JSON.stringify(printed) === JSON.stringify([...DOIS].sort()), `and prints each one as it links it (${printed.length} printed)`);
  ok(data.source.includes('10.1038/s41597-020-00793-8') && data.source.includes('10.25405/data.ncl.13082333.v1') && data.source.includes('CC0'), 'spawn.json names the database paper, the dataset DOI and its CC0 licence');
}

// ── 9. the page's prose carries no em dash of its own ──────────────────────
{
  const body = html.replace(/<blockquote>[\s\S]*?<\/blockquote>/g, '');
  ok(!body.includes('—'), 'no em dash in the page outside quotations');
}

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