from copy import deepcopy import json import pytest from research.evaluate import evaluate, metrics from research.harden import build, DESTINATION from research.harden import official_data from research.evaluate import predictions_for def test_hardening_artifacts_and_exclusions(): files = build() for name, content in files.items(): assert (DESTINATION / name).read_bytes() == content rows = json.loads(files["annotations.json"]) assert len(rows) == 84 assert all(row["independent_review"] is None for row in rows) assert all(not row["score_eligible"] for row in rows if row["current_status"] in {"ambiguous", "unverified", "reviewed_static"}) assert all(not row["score_eligible"] for row in rows if "mock_boundary_behavior_verified" in row["evidence_levels"]) assert len(json.loads(files["near_duplicate_dispositions.json"])) == 7 def test_zero_positive_predictions_and_absent_classes(): result = metrics([("insecure", "secure"), ("secure", "secure")]) assert result["precision"] is None assert result["recall"] == result["f1"] == 0 assert result["balanced_accuracy"] == 0.5 assert result["false_positive_rate"] == 0 assert result["false_negative_rate"] == 1 assert metrics([("secure", "secure")])["recall"] is None assert metrics([])["balanced_accuracy"] is None def test_official_manifest_excludes_all_unsupported_rows(): artifacts = build() notes = json.loads(artifacts["annotations.json"]) accepted = {row["sample_id"] for row in notes if row["score_eligible"]} rows = [ json.loads(line) for split in ("train", "validation", "test") for line in artifacts[f"official/{split}.jsonl"].splitlines() ] assert len(rows) == len(accepted) == 49 assert {row["sample_id"] for row in rows} == accepted excluded = next(row["sample_id"] for row in notes if not row["score_eligible"]) with pytest.raises(ValueError, match="Unknown or out-of-split"): predictions_for(rows, {"predictions": [{"sample_id": excluded, "label": "secure"}]}, False) def test_official_loader_rejects_missing_manifest(tmp_path, monkeypatch): import research.harden as harden monkeypatch.setattr(harden, "DESTINATION", tmp_path) with pytest.raises(ValueError, match="Stale official eligibility"): official_data("test")