import hashlib import json import unittest from pathlib import Path from generate_source_semantics_contract import OUTPUT, canonical, generate ROOT = Path(__file__).resolve().parents[1] class SourceSemanticsContractTests(unittest.TestCase): def test_contract(self): report = json.loads(OUTPUT.read_text(encoding="utf-8")) self.assertEqual(report["schema"], "linuxcnc-source-semantics-contract.v1") self.assertEqual(report["status"], "verified") self.assertEqual(report["context_id"], "linuxcnc-2.10.0-pre0-wasm-m1-py-c1") self.assertTrue(any(rule["id"] == "axis_open_file_optional" for rule in report["rules"])) self.assertTrue(any(rule["id"] == "halfile_missing_classification" for rule in report["rules"])) self.assertEqual(report["control_panel_contract"]["projection_only"], True) self.assertTrue(all(item["status"] == "present" for item in report["inputs"])) def test_self_hash_and_determinism(self): before = OUTPUT.read_bytes() report = generate() self.assertEqual(before, OUTPUT.read_bytes()) unsigned = {key: value for key, value in report.items() if key != "report_sha256"} self.assertEqual(report["report_sha256"], hashlib.sha256(canonical(unsigned).encode()).hexdigest()) if __name__ == "__main__": unittest.main()