"""Unit tests for the exploit -> regression-test generator.""" from __future__ import annotations from pathlib import Path from fable_selfplay.detectors import ExploitRecord from fable_selfplay.exploit_to_test import generate_regression_test def _generate(record_dict: dict, out_dir: Path) -> Path: record = ExploitRecord.from_dict(record_dict) return Path(generate_regression_test(record, out_dir)) def test_generated_file_exists_and_names_the_exploit(exploit_records, tmp_path): record = exploit_records[0] path = _generate(record, tmp_path) assert path.exists() source = path.read_text(encoding="utf-8") assert record["exploit_id"] in source assert "def test_" in source def test_generated_test_is_valid_python(exploit_records, tmp_path): for record in exploit_records: path = _generate(record, tmp_path) source = path.read_text(encoding="utf-8") compile(source, str(path), "exec") # raises SyntaxError if broken def test_generation_is_deterministic(exploit_records, tmp_path): record = exploit_records[0] first = _generate(record, tmp_path / "a").read_text(encoding="utf-8") second = _generate(record, tmp_path / "b").read_text(encoding="utf-8") assert first == second def test_all_shipped_exploits_generate_unique_files(exploit_records, tmp_path): paths = [_generate(record, tmp_path) for record in exploit_records] names = [path.name for path in paths] assert len(names) == len(set(names)), f"filename collision: {names}" assert len(paths) == len(exploit_records) def test_generated_tests_match_shipped_regression_suite(exploit_records, tmp_path): """The committed tests/regression/ files must be exactly what the generator produces today — guaranteeing nobody hand-edited a regression test into something weaker than the recorded exploit.""" repo_regression = Path(__file__).resolve().parents[1] / "regression" for record in exploit_records: generated = _generate(record, tmp_path / "check") committed = repo_regression / generated.name assert committed.exists(), ( f"no committed regression test for {record['exploit_id']} " f"(expected {committed})" ) assert committed.read_text(encoding="utf-8") == \ generated.read_text(encoding="utf-8"), ( f"committed {committed.name} differs from generator output; " "regression tests must only ever be produced by the pipeline" )