#!/usr/bin/env python3 """ Positive control for the defect-twin ledger gate. The gate's whole claim is that the ledger is COMPLETE — that every difference between control and twin is written down. A gate only ever shown passing has demonstrated nothing, so it is shown here failing on a twin carrying an edit the ledger does not record. That is the laundering case, and it is the only case the gate exists for. Fixtures are derived from the PROPERTY ("what would make 'the ledger is complete' false?") rather than from the code, per the discrimination principle. Usage: ./test_twin.py """ from __future__ import annotations import json import sys from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) from twin import apply_forward, apply_inverse, load_ledger # noqa: E402 CONTROL = HERE / "CONTROL-A-flagging-and-modifying.md" TWIN = HERE / "CONTROL-B-flagging-and-modifying-defective.md" LEDGER = HERE / "twin-ledger.json" failures: list[str] = [] def check(name: str, got, want, detail: str = "") -> None: if got != want: failures.append(f"{name}: expected {want!r}, got {got!r}. {detail}") print(f" FAIL {name}") else: print(f" ok {name}") for p in (CONTROL, TWIN, LEDGER): if not p.is_file(): failures.append(f"missing artifact: {p.name}") if not failures: control = CONTROL.read_text(encoding="utf-8") twin = TWIN.read_text(encoding="utf-8") ledger = load_ledger(LEDGER) print("Round trip on the real pair:") check("forward reproduces the twin", apply_forward(control, ledger), twin) check("inverse reproduces the control", apply_inverse(twin, ledger), control) print("\nPositive control — an UNLOGGED edit must be caught:") # The laundering case: a twin quietly altered beyond what the ledger records. laundered = twin.replace( "What opens is the report.", "What opens is the report, and nothing else." ) check("laundered twin actually differs", laundered != twin, True) check( "forward gate DETECTS the unlogged edit", apply_forward(control, ledger) != laundered, True, "a ledger that cannot detect an unlogged edit establishes no ground truth", ) check( "inverse gate DETECTS it too", apply_inverse(laundered, ledger) != control, True, ) print("\nPositive control — a ledger entry for an edit NOT made must be caught:") phantom = json.loads(LEDGER.read_text(encoding="utf-8")) phantom["defects"].append({ "id": "PHANTOM", "type": "TEST", "target": "", "undemonstrated": "", "why_invisible_to_checks": "", "edits": [{"find": "What opens is the report.", "replace": "What opens is the report, obviously."}], }) check( "forward gate DETECTS a recorded edit absent from the twin", apply_forward(control, phantom) != twin, True, ) print("\nEvery defect must be uniquely locatable:") for d in ledger["defects"]: for i, e in enumerate(d["edits"]): check(f"{d['id']}[{i}] find is unique in control", control.count(e["find"]), 1) check(f"{d['id']}[{i}] replace is unique in twin", twin.count(e["replace"]), 1) print("\nEvery defect carries the record a grader needs:") for d in ledger["defects"]: check(f"{d['id']} states what is undemonstrated", bool(d.get("undemonstrated", "").strip()), True) check(f"{d['id']} states why no check catches it", bool(d.get("why_invisible_to_checks", "").strip()), True) if failures: print(f"\nINSTRUMENT NOT VERIFIED — {len(failures)} failure(s):") for f in failures: print(f" - {f}") sys.exit(1) print("\nLedger gate verified, and shown failing on both laundering directions.")