// mutate.mjs: does verify.mjs notice a broken engine?
// Run: node research/the-mound-that-breathes/mutate.mjs
// Each mutation is one small, plausible slip, applied to a temporary copy of the page's
// engine; the verifier is run against that copy (ENGINE=...) and must fail. Unchanged,
// it must pass. A check that cannot fail is not a check.
import { readFileSync, writeFileSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';

const here = new URL('.', import.meta.url);
const src = readFileSync(new URL('../../public/strata/the-mound-that-breathes/engine.mjs', here), 'utf8');
const MUTATIONS = [
  ['the penetration depth loses its factor of two', 'return Math.sqrt((2 * kappa) / (n * 2 * Math.PI / period));', 'return Math.sqrt(kappa / (n * 2 * Math.PI / period));'],
  ['the buried wave is damped but never delayed', 'const damp = Math.exp(-x / d), lag = x / d;', 'const damp = Math.exp(-x / d), lag = 0;'],
  ['higher harmonics reach as deep as the first', 'const d = depth(kappa, n, c.period);', 'const d = depth(kappa, 1, c.period);'],
  ['the cylinder is treated as a slab', "if (shape === 'cylinder') return cdiv([1, 0], I0(q));", "if (shape === 'cylinder') { const e1 = cexp(q), e2 = cexp([-r, -r]); return cdiv([2, 0], [e1[0] + e2[0], e1[1] + e2[1]]); }"],
  ['the drift across the plotted day is left in', "const drift = pts[pts.length - 1][1] - pts[0][1];\n  const s = [];", "const drift = 0;\n  const s = [];"],
  ['the periphery forgets the south side', "export const SIDES = ['north', 'east', 'south', 'west'];", "export const SIDES = ['north', 'east', 'west'];"],
  ['the centre is compared with the top instead', "const center = rel('center'), nest = rel('nest');", "const center = rel('top'), nest = rel('nest');"],
  ['the solstice quarters are centred a month late', 'const summer = q(354), winter = q(171);', 'const summer = q(19), winter = q(202);'],
  ['the stack pressure uses the wrong molar mass', 'const M = 0.028964, R = 8.314462618;', 'const M = 0.018015, R = 8.314462618;'],
];
const dir = mkdtempSync(join(tmpdir(), 'mound-mut-'));
const run = (code) => {
  const f = join(dir, 'engine.mjs'); writeFileSync(f, code);
  return spawnSync(process.execPath, [new URL('./verify.mjs', here).pathname], { env: { ...process.env, ENGINE: f }, encoding: 'utf8' });
};
let good = 0;
const base = run(src);
console.log(`unmutated engine: verifier ${base.status === 0 ? 'passes' : 'FAILS'}`);
for (const [name, from, to] of MUTATIONS) {
  if (!src.includes(from)) { console.log(`?? mutation target not found: ${name}`); continue; }
  const r = run(src.replace(from, to));
  const fails = (r.stdout.match(/^FAIL/gm) || []).length;
  const caught = r.status !== 0 && fails > 0; // a crash is not a caught mutation
  if (caught) good++;
  console.log(`${caught ? 'caught ' : 'MISSED '} ${name}${caught ? `  (${fails} check${fails === 1 ? '' : 's'} fail)` : ''}`);
}
rmSync(dir, { recursive: true, force: true });
console.log(`\n${good} of ${MUTATIONS.length} mutations caught`);
process.exit(base.status === 0 && good === MUTATIONS.length ? 0 : 1);
