#!/usr/bin/env python3 """Negative controls for verify_route59_numbers.py (job #4332). Each test corrupts exactly one thing in a temporary copy of the inputs or results and asserts that the checker exits non-zero. Run: ../../.venv/Scripts/python.exe -m unittest test_verify_route59_numbers -v """ import json import os import shutil import subprocess import sys import unittest HERE = os.path.dirname(os.path.abspath(__file__)) PY = sys.executable # Control copies live inside the workspace (a system temp directory is outside the sandbox). CTL = os.path.join(HERE, "_ctl") def run(inp, res): p = subprocess.run([PY, os.path.join(HERE, "verify_route59_numbers.py"), inp, res], capture_output=True, text=True) return p.returncode, p.stdout + p.stderr class TestControls(unittest.TestCase): def setUp(self): shutil.rmtree(CTL, ignore_errors=True) os.makedirs(CTL, exist_ok=True) self.inp = os.path.join(CTL, "route59_inputs.json") self.res = os.path.join(CTL, "spectral_axis.json") shutil.copy(os.path.join(HERE, "route59_inputs.json"), self.inp) shutil.copy(os.path.join(HERE, "spectral_axis.json"), self.res) def tearDown(self): shutil.rmtree(CTL, ignore_errors=True) def _mutate_inp(self, fn): with open(self.inp) as fh: d = json.load(fh) fn(d) with open(self.inp, "w") as fh: json.dump(d, fh, indent=1) def _mutate_res(self, fn): with open(self.res) as fh: d = json.load(fh) fn(d) with open(self.res, "w") as fh: json.dump(d, fh, indent=1) def test_clean_pass(self): code, out = run(self.inp, self.res) self.assertEqual(code, 0, out) self.assertIn("RESULT: PASS", out) def test_corrupted_exponent_ledger(self): self._mutate_inp(lambda d: d["exponent_ledger"][1].update(rhs="8/200")) code, out = run(self.inp, self.res) self.assertNotEqual(code, 0) self.assertIn("FAIL rational moment deficit", out) def test_corrupted_kernel_modulus(self): self._mutate_inp(lambda d: d["symmetry_witnesses"][0].update(c=d["symmetry_witnesses"][0]["c"] + 1)) code, out = run(self.inp, self.res) self.assertNotEqual(code, 0) self.assertIn("FAIL", out) def test_corrupted_regression_pin(self): self._mutate_inp(lambda d: d["regression_cells"][0].update(S_abs=1.0)) code, out = run(self.inp, self.res) self.assertNotEqual(code, 0) self.assertIn("FAIL regression", out) def test_violated_operator_bound(self): def bad(d): for part in ("part1", "part2", "part3"): for cell in d[part].values(): if isinstance(cell, dict) and "A_op" in cell: cell["A_op"] = cell["S_abs"] * 0.5 return self._mutate_res(bad) code, out = run(self.inp, self.res) self.assertNotEqual(code, 0) self.assertIn("FAIL operator bound", out) def test_broken_spectral_identity(self): def bad(d): for part in ("part1", "part2", "part3"): for cell in d[part].values(): if isinstance(cell, dict) and "spectral_S_abs" in cell: cell["spectral_S_abs"] = cell["S_abs"] * 1.5 return self._mutate_res(bad) code, out = run(self.inp, self.res) self.assertNotEqual(code, 0) self.assertIn("FAIL spectral identity", out) def test_missing_shuffle_joint(self): def bad(d): for k in list(d["part2"]): if k.endswith("shuffle_joint"): del d["part2"][k] return self._mutate_res(bad) code, out = run(self.inp, self.res) self.assertNotEqual(code, 0) self.assertIn("FAIL", out) def test_tampered_threshold_saving(self): def bad(d): d["part2"]["E=400 z'/x=0.6"]["guaranteed_saving"] = 1.0 self._mutate_res(bad) code, out = run(self.inp, self.res) self.assertNotEqual(code, 0) self.assertIn("FAIL E-sweep", out) if __name__ == "__main__": unittest.main()