#!/usr/bin/env node
// mutate.mjs: a check never seen failing has no demonstrated power to fail. Each plant below breaks one thing, and
// verify.mjs must go red for every one of them. The engine plants are the page's own engine.mjs with one small error
// written in (a parallel pair taken for a unit distance, no case split on triangles, the wrong rhombus, an edge parallel
// to another called a contradiction, the wrong norm, 120 degrees for 60, and no parallel test at all); each must be
// caught by the positive controls: it has to refute some graph that can be drawn.
//
// Usage: node mutate.mjs [--remote]   (passes --remote through to verify.mjs)
// --python-bridge launches the same Node verifier through Python, for environments that block Node's synchronous child launcher.
import { spawnSync } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const HERE = path.dirname(fileURLToPath(import.meta.url));
const extra = process.argv.includes('--remote') ? ['--remote'] : [];
const BRIDGE = process.argv.includes('--python-bridge');
const launch = argv => {
  if (!BRIDGE) return spawnSync(process.execPath, argv, { encoding: 'utf8' });
  const code = 'import json,subprocess,sys; r=subprocess.run(sys.argv[1:],capture_output=True,text=True); print(json.dumps({"status":r.returncode,"stdout":r.stdout,"stderr":r.stderr}))';
  const bridge = spawnSync('python3', ['-B', '-c', code, process.execPath, ...argv], { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 });
  try { return JSON.parse(bridge.stdout); } catch { return { status: null, stdout: '', stderr: bridge.stderr, error: bridge.error }; }
};
const PLANTS = [
  ['drop', 'one graph removed from the list'],
  ['dup', 'a relabelled copy of a graph added to the list'],
  ['coord', 'one coordinate of one drawing moved by a millionth'],
  ['coef', 'one coefficient in one proof changed'],
  ['petersen', 'one Petersen coordinate moved by a ten-millionth'],
  ['schade', 'u(22) taken as 61'],
  ['new-chain', 'the new chain starts from u(24) = 69 instead of the exact 68'],
  ['prose', 'the sentence says 33,163 where the printed transcript still says 33,162'],
  ['engine-unit', 'engine: any parallel pair taken for a unit distance'],
  ['engine-nosplit', 'engine: every triangle assumed to turn left'],
  ['engine-rhombus', 'engine: the rhombus written with adjacent corners paired'],
  ['engine-length', 'engine: any edge parallel to another called a contradiction'],
  ['engine-norm', 'engine: |p + q w|^2 taken as p^2 + q^2'],
  ['engine-angle', 'engine: triangles turned by 120 degrees, not 60'],
  ['engine-parallel', 'engine: no test that two differences are parallel'],
];
let caught = 0;
if (BRIDGE) console.log('Launch mode: explicit Python bridge; each test still runs the actual Node verifier');
for (const [plant, what] of PLANTS) {
  const r = launch([path.join(HERE, 'verify.mjs'), '--plant', plant, ...extra]);
  const fails = (r.stdout.match(/^ {2}FAIL .*/gm) || []).map((l) => l.trim().slice(5, 90));
  const red = r.status === 1 && fails.length > 0 && /\d+ passed, \d+ failed/.test(r.stdout);
  if (red) caught++;
  console.log(`${red ? 'caught' : 'MISSED'}  ${plant.padEnd(16)} ${what}${red ? `  (first red: ${fails[0]})` : `  (exit ${r.status})`}`);
}
console.log(`\n${caught} of ${PLANTS.length} plants caught`);
process.exit(caught === PLANTS.length ? 0 : 1);
