#!/usr/bin/env python3 """Tests for stepcheck4460.py: clean exit 0, and negative controls that each corrupt one served input and confirm the corresponding check fires. Uses a scratch directory under outputs/job4460 (makedirs, not mkdtemp: the file sandbox blocks tempfile here). """ import json import os import shutil import subprocess import sys import unittest HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.abspath(os.path.join(HERE, "..", "..")) PY = sys.executable TMP = os.path.join(HERE, "_test_tmp") CASE = os.path.join(TMP, "case") def build_case(): if os.path.isdir(CASE): shutil.rmtree(CASE) os.makedirs(CASE) shutil.copy(os.path.join(HERE, "stepcheck4460.py"), CASE) shutil.copytree(os.path.join(HERE, "served"), os.path.join(CASE, "served")) def run_case(): p = subprocess.run([PY, os.path.join(CASE, "stepcheck4460.py"), os.path.join(CASE, "out.json")], capture_output=True, text=True) out = None if os.path.exists(os.path.join(CASE, "out.json")): out = json.load(open(os.path.join(CASE, "out.json"), encoding="utf-8")) return p.returncode, out, p.stdout + p.stderr def patch(name, fn): p = os.path.join(CASE, "served", name) d = json.load(open(p, encoding="utf-8")) fn(d) json.dump(d, open(p, "w", encoding="utf-8")) class CleanRun(unittest.TestCase): def test_clean_exit_zero(self): build_case() rc, out, log = run_case() self.assertEqual(rc, 0, log) self.assertEqual(out["n_checks"], 15) self.assertEqual(out["failed"], []) self.assertEqual(out["decision"], "progress") class NegativeControls(unittest.TestCase): def setUp(self): build_case() def assert_fails(self, cid): rc, out, log = run_case() self.assertNotEqual(rc, 0, log) self.assertIn(cid, out["failed"]) def test_corrupt_c_eff(self): patch("f1787_parity_gate.json", lambda d: d["G3_contamination_constant"].__setitem__("c_eff(theta=1)", 1.9)) self.assert_fails("C4.c_eff_theta") def test_corrupt_published_max_ratio(self): patch("f1787_parity_gate.json", lambda d: d["T2_level_theta_test"]["theta=1.0,F2=1"] .__setitem__("max_ratio_to_c_eff", 9.99)) self.assert_fails("C5.reproduce_published") def test_corrupt_route_revision(self): patch("route-36.json", lambda d: _set(d, "revision", 3)) self.assert_fails("C2.route_state") def test_corrupt_route_event(self): patch("route-36.json", lambda d: _set(d, "events", [])) self.assert_fails("C2.route_state") def test_give_the_route_a_next_job(self): patch("route-36.json", lambda d: _set(d, "next_job_id", 4119)) self.assert_fails("C2b.next_job") def test_strip_a_served_attachment(self): os.remove(os.path.join(CASE, "served", "f101_fold-arithmetic-bridge.md")) self.assert_fails("C3.attachment_sha") def test_break_a_served_attachment_hash(self): p = os.path.join(CASE, "served", "f1978_step36_findings.json") open(p, "a", encoding="utf-8").write(" ") self.assert_fails("C3.attachment_sha") def test_remove_smith_locator_from_1978(self): def fn(d): s = json.dumps(d).replace("2511.14810", "0000.00000") d.clear() d.update(json.loads(s)) patch("f1978_step36_findings.json", fn) self.assert_fails("C11.part_c_on_record") def test_put_smith_locator_in_the_route_prior_art(self): # the locator's absence from the route's own prior art is what makes # #1978 (not #659/#661/#1787) the return that settles part (c) p = os.path.join(CASE, "served", "return-659.json") open(p, "a", encoding="utf-8").write(" 2511.14810\n") self.assert_fails("C11.part_c_on_record") def test_prop3_level_one_claim(self): p = os.path.join(CASE, "served", "f101_fold-arithmetic-bridge.md") t = open(p, encoding="utf-8").read() anchor = "**Proposition 3.** Fix k >= 1, u >= 2 and A > 0." self.assertIn(anchor, t) open(p, "w", encoding="utf-8").write( t.replace(anchor, "**Proposition 3.** level X^(1-epsilon) Fix k >= 1, u >= 2 and A > 0.", 1)) self.assert_fails("C6.prop3_level") def _set(d, key, val): if "body" in d and key in d["body"]: d["body"][key] = val else: d[key] = val if __name__ == "__main__": unittest.main(verbosity=2)