#!/usr/bin/env python3 """Regression tests for sahdated's two recognition gaps (route 148, job 2940). Fixtures are #1549's minimal pair (producers-probe.py CASES, copied verbatim) plus the live `fetched_at` record shape #1549 reports. Each test is labelled GAP (expected to FAIL on sahdated/1.1.1 and PASS on 1.1.2) or CONTROL (expected to PASS on both). usage: python3 test_recognition_2940.py stdout is deterministic JSON (no clock values); exit 0 iff every GAP and CONTROL test passes. LIMIT tests document a known residual and do not set the exit status. """ import importlib.util, io, json, os, sys, tempfile spec = importlib.util.spec_from_file_location("sahdated_under_test", sys.argv[1]) sd = importlib.util.module_from_spec(spec); spec.loader.exec_module(sd) WRITE = (" with open(path, \"w\", encoding=\"utf-8\") as fh:\n" " fh.write(json.dumps(record))\n") CASES = { # verbatim from #1549 producers-probe.py "a_plain.py": "import json\n\n\ndef emit(path, record):\n" + WRITE, "b_mentions_read_only.py": "import json\n\n# the CALLER reads an instant field elsewhere; this writer still writes none\n\n\n" "def emit(path, record):\n" " looked_at = record.get(\"observed_at\")\n" " assert looked_at is not None or True\n" + WRITE, "c_routes_through_helper.py": "import json\n\nimport sahdated\n\n\ndef emit(path, record):\n" " sahdated.write(path, record, source=\"probe\")\n", "d_comment_names_one.py": "import json\n\n\ndef emit(path, record):\n" " # covered_at is added by the caller, not here\n" + WRITE, # controls added here "e_hand_rolled_dated.py": "import json\nrec = {'rows': rows, 'covered_at': '2026-09-23T00:00:00Z'}\n" "with open('out.json', 'w') as fh:\n json.dump(rec, fh)\n", "f_key_assigned.py": "import json\nout = {}\nout[\"covered_at\"] = stamp()\n" "open('o.json', 'w').write(json.dumps(out))\n", "g_no_writes.py": "import json\nprint('nothing written')\n", # LIMIT (known residual in 1.1.2, expected to fail on both): the window holds an instant KEY, but # the object actually dumped carries none. Found on this job's own reader_table_2940.py. "h_window_leak.py": "import json\ndated = {'rows': [], 'covered_at': '2026-09-23T00:00:00Z'}\nundated = {'rows': []}\n" "json.dump(undated, open('u.json', 'w'))\n", } RECORDS = { "fetched.json": {"route": 148, "fetched_at": "2026-09-23T17:05:00Z"}, # #1549's live near miss "plain.json": {"rows": [1, 2]}, # undated, no near miss "dated.json": {"rows": [1, 2], "covered_at": "2026-09-23T17:05:00Z"}, } results = [] def test(name, kind, cond, got): results.append({"test": name, "kind": kind, "pass": bool(cond), "got": got}) with tempfile.TemporaryDirectory() as td: for n, t in CASES.items(): io.open(os.path.join(td, n), "w", encoding="utf-8", newline="\n").write(t) for n, r in RECORDS.items(): json.dump(r, io.open(os.path.join(td, n), "w", encoding="utf-8")) rep = sd.producers([os.path.join(td, n) for n in sorted(CASES)]) st = {} for r in rep["sites"]: st.setdefault(os.path.basename(r["path"]), []).append(r["status"]) test("P1 plain undated writer is unguarded", "CONTROL", st.get("a_plain.py") == ["unguarded"], st.get("a_plain.py")) test("P2 a READ of an instant field does not guard the writer", "GAP", st.get("b_mentions_read_only.py") == ["unguarded"], st.get("b_mentions_read_only.py")) test("P2b a COMMENT naming an instant does not guard the writer", "GAP", st.get("d_comment_names_one.py") == ["unguarded"], st.get("d_comment_names_one.py")) test("P3 a helper-routed writer is visible as a routed site", "GAP", st.get("c_routes_through_helper.py") == ["routed"], st.get("c_routes_through_helper.py")) test("a hand-rolled writer that writes covered_at in a literal is guarded", "CONTROL", st.get("e_hand_rolled_dated.py") == ["guarded"], st.get("e_hand_rolled_dated.py")) test("a writer that assigns out['covered_at'] is guarded", "CONTROL", st.get("f_key_assigned.py") == ["guarded"], st.get("f_key_assigned.py")) test("a file with no writes has no site", "CONTROL", st.get("g_no_writes.py") is None, st.get("g_no_writes.py")) test("an undated object dumped beside a dated literal is unguarded", "LIMIT", st.get("h_window_leak.py") == ["unguarded"], st.get("h_window_leak.py")) def refusal(n): try: sd.load(os.path.join(td, n)); return None except sd.Undated as e: return str(e).replace(td, "") rf, rp, rd = refusal("fetched.json"), refusal("plain.json"), refusal("dated.json") test("fetched_at record is still REFUSED (list not silently widened)", "CONTROL", rf is not None, rf is not None) test("the refusal of a fetched_at record names the near miss", "GAP", rf is not None and "NEAR MISS" in rf and "fetched_at" in rf, rf) test("a plain undated record is refused without a near-miss note", "CONTROL", rp is not None and "NEAR MISS" not in rp, rp is not None and "NEAR MISS" not in rp) test("a covered_at record loads", "CONTROL", rd is None, rd) au = {os.path.basename(r["path"]): r for r in sd.audit([os.path.join(td, n) for n in sorted(RECORDS)])} test("audit marks the fetched_at record's near miss", "GAP", au["fetched.json"].get("near_miss") == ["fetched_at"], au["fetched.json"].get("near_miss")) out = {"module": sd.VERSION, "tests": results, "passed": sum(r["pass"] for r in results if r["kind"] != "LIMIT"), "total": len([r for r in results if r["kind"] != "LIMIT"]), "known_limits": {r["test"]: r["got"] for r in results if r["kind"] == "LIMIT"}, "failed_gap": [r["test"] for r in results if r["kind"] == "GAP" and not r["pass"]], "failed_control": [r["test"] for r in results if r["kind"] == "CONTROL" and not r["pass"]]} print(json.dumps(out, indent=1, sort_keys=True)) sys.exit(0 if out["passed"] == out["total"] else 1)