"""Tests for the self-lint gate, which makes the card answerable from the package.""" from __future__ import annotations import ast import importlib.util import json import re from pathlib import Path _ROOT = Path(__file__).resolve().parent.parent # The kernel bundle carries a tools/run_gates.py of its own, so this file loads # the bundle runner by path instead of by package name. _spec = importlib.util.spec_from_file_location( "codex_python_gates_runner", _ROOT / "tools" / "run_gates.py" ) assert _spec is not None and _spec.loader is not None _run_gates = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(_run_gates) _card_promises = _run_gates._card_promises _defined_names = _run_gates._defined_names gate_self_lint = _run_gates.gate_self_lint def test_the_self_lint_gate_confirms_the_real_package() -> None: res = gate_self_lint() assert res.verdict == "CONFIRMED" assert "all defined" in res.note def test_a_name_the_card_promises_that_no_module_defines_is_not_a_promise_kept() -> None: fake_card = ( "# Card\n\n## Contents\n\n" "- `x/mod.py`: does a thing (`real_name`, `invented_name`).\n\n" "## Next\n\nNothing.\n" ) promises = _card_promises(fake_card) assert promises == ["real_name", "invented_name"] own = _defined_names({"mod.py": "def real_name():\n return 1\n"}) assert "real_name" in own assert "invented_name" not in own def test_the_defined_names_see_cli_verbs_choices_tools_and_constants() -> None: cli_text = ( '"""Cli."""\n' "import argparse\n\n\n" "TOOLS = {'mypy': ()}\n\n\n" "def build_parser():\n" " parser = argparse.ArgumentParser()\n" " sub = parser.add_subparsers()\n" " sub.add_parser('select-tests')\n" " check = sub.add_parser('patterns')\n" " check.add_argument('action', choices=('list', 'apply'))\n" " return parser\n" ) own = _defined_names({"cli.py": cli_text}) for name in ("select-tests", "patterns", "list", "apply", "TOOLS", "mypy", "build_parser"): assert name in own def test_names_outside_parentheses_are_not_checked_promises() -> None: fake_card = ( "# Card\n\n## Contents\n\n" "- `x/mod.py`: carries `unparenthesised` outside any parentheses.\n\n" "## Next\n\nNothing.\n" ) assert _card_promises(fake_card) == [] def test_the_digest_anchors_the_report_to_the_tree_it_was_run_on(tmp_path: Path) -> None: first = tmp_path / "a.py" second = tmp_path / "b.txt" first.write_text("x = 1\n", encoding="utf-8", newline="\n") second.write_text("note\n", encoding="utf-8", newline="\n") before = _run_gates.bundle_digest([second, first], tmp_path) assert _run_gates.bundle_digest([first, second], tmp_path) == before first.write_text("x = 2\n", encoding="utf-8", newline="\n") assert _run_gates.bundle_digest([first, second], tmp_path) != before def test_the_report_in_the_tree_describes_the_tree_it_sits_in() -> None: report = json.loads((_ROOT / "results" / "gates.json").read_text(encoding="utf-8")) assert report["sources_sha256"] == _run_gates.bundle_digest( _run_gates.digest_input_files(_ROOT) ) def test_the_gates_line_in_the_card_matches_the_saved_report() -> None: """The card's gate tally is not a sentence to trust; it is a checked equality.""" readme = (_ROOT / "README.md").read_text(encoding="utf-8", newline="") report = json.loads((_ROOT / "results" / "gates.json").read_text(encoding="utf-8")) line = re.search( r"GATES: (\w+) \((\d+) gates: (\d+) CONFIRMED, (\d+) REJECTED, (\d+) ABSTAIN\)", readme, ) assert line is not None verdict, total, confirmed, rejected, abstain = line.groups() gate_names = [gate["name"] for gate in report["gates"]] assert int(total) == len(report["gates"]) == len(_run_gates.GATES) assert gate_names == [name for name, _ in _run_gates.GATES] counts = report["counts"] assert int(confirmed) == counts.get("CONFIRMED", 0) assert int(rejected) == counts.get("REJECTED", 0) assert int(abstain) == counts.get("ABSTAIN", 0) assert verdict == report["verdict"] def test_the_numbers_sentence_in_the_card_is_countable_from_the_tree() -> None: """Gates, tests, test files, and node files in the card come from the tree, not from memory.""" readme = (_ROOT / "README.md").read_text(encoding="utf-8", newline="") sentence = re.search( r"The gate runner is where this bundle states its numbers out loud: (\d+) gates, " r"(\d+) tests across (\d+) files,\n(\d+) files under `nodes/`, and a kernel it does not carry\.", readme, ) assert sentence is not None gates, tests, files, node_files = (int(value) for value in sentence.groups()) assert gates == len(_run_gates.GATES) test_files = sorted((_ROOT / "tests").glob("test_*.py")) assert files == len(test_files) collected = 0 for path in test_files: tree = ast.parse(path.read_text(encoding="utf-8")) collected += sum( 1 for node in tree.body if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name.startswith("test_") ) assert tests == collected assert node_files == sum(1 for path in (_ROOT / "nodes").rglob("*") if path.is_file())