| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from src.eval.gates import (GATES, by_dimension, evaluate_gates, render_matrix, |
| 14 | summary) |
| 15 | |
| 16 | |
| 17 | def _payload(**over): |
| 18 | base = { |
| 19 | "overall": { |
| 20 | "n": 200, "n_guardrail_applicable": 67, "n_abstained": 45, |
| 21 | "retrieval": {"recall_at_5": 0.90, "recall_at_10": 0.93, |
| 22 | "recall_at_20": 0.99, "mrr_value": 0.88, |
| 23 | "ndcg_at_10": 0.90, |
| 24 | "all_evidence_recall_at_20": 0.95}, |
| 25 | "citation": {"recall_page": 0.95, "precision_page": 0.90}, |
| 26 | "guardrail_pass_rate": 1.0, "fail_closed_rate": 1.0, |
| 27 | "judge": {"n_judged": 40, "n_judge_selected": 40, |
| 28 | "faithfulness_penalized": 0.98}, |
| 29 | }, |
| 30 | "items": [], |
| 31 | } |
| 32 | base["overall"].update(over) |
| 33 | return base |
| 34 | |
| 35 | |
| 36 | class GateTableTests(unittest.TestCase): |
| 37 | def test_every_gate_has_id_name_and_note_or_path(self): |
| 38 | for g in GATES: |
| 39 | with self.subTest(g.gid): |
| 40 | self.assertTrue(g.gid and g.ad) |
| 41 | |
| 42 | if g.path is None: |
| 43 | self.assertTrue(g.not_, f"{g.gid}: ölçülmüyor ama not yok") |
| 44 | |
| 45 | def test_gate_ids_unique(self): |
| 46 | ids = [g.gid for g in GATES] |
| 47 | self.assertEqual(len(ids), len(set(ids))) |
| 48 | |
| 49 | def test_security_gates_have_equal_mvp_and_full_thresholds(self): |
| 50 | |
| 51 | for g in GATES: |
| 52 | if g.gid.startswith("G-"): |
| 53 | with self.subTest(g.gid): |
| 54 | self.assertEqual(g.mvp, g.tam) |
| 55 | |
| 56 | def test_full_thresholds_are_never_looser_than_mvp(self): |
| 57 | for g in GATES: |
| 58 | if g.mvp is None or g.tam is None: |
| 59 | continue |
| 60 | with self.subTest(g.gid): |
| 61 | if g.higher_is_better: |
| 62 | self.assertGreaterEqual(g.tam, g.mvp) |
| 63 | else: |
| 64 | self.assertLessEqual(g.tam, g.mvp) |
| 65 | |
| 66 | |
| 67 | class EvaluationTests(unittest.TestCase): |
| 68 | def test_unmeasured_gates_are_listed_not_hidden(self): |
| 69 | |
| 70 | |
| 71 | rows = evaluate_gates(_payload()) |
| 72 | unmeasured = [r for r in rows if not r["olculuyor"]] |
| 73 | self.assertGreater(len(unmeasured), 0) |
| 74 | for r in unmeasured: |
| 75 | with self.subTest(r["gid"]): |
| 76 | self.assertIn(r["durum"], ("ÖLÇÜLMEDİ", "ATLANDI")) |
| 77 | self.assertTrue(r["not"], f"{r['gid']}: neden yazılmamış") |
| 78 | self.assertNotEqual(r["durum"], "GEÇTİ") |
| 79 | |
| 80 | def test_gate_passes_only_when_ci_lower_clears_threshold(self): |
| 81 | rows = {r["gid"]: r for r in evaluate_gates(_payload())} |
| 82 | |
| 83 | self.assertEqual(rows["G-05"]["olculen"], 1.0) |
| 84 | self.assertEqual(rows["G-05"]["durum"], "GEÇMEDİ") |
| 85 | |
| 86 | def test_point_estimate_one_does_not_pass(self): |
| 87 | |
| 88 | rows = {r["gid"]: r for r in evaluate_gates(_payload(fail_closed_rate=1.0))} |
| 89 | self.assertEqual(rows["C-04"]["olculen"], 1.0) |
| 90 | |
| 91 | self.assertEqual(rows["C-04"]["durum"], "GEÇMEDİ") |
| 92 | |
| 93 | def test_mean_metric_without_item_values_is_unmeasured(self): |
| 94 | |
| 95 | rows = {r["gid"]: r for r in evaluate_gates(_payload())} |
| 96 | self.assertIsNotNone(rows["K-03"]["olculen"]) |
| 97 | self.assertIsNone(rows["K-03"]["ci"]) |
| 98 | self.assertEqual(rows["K-03"]["durum"], "ÖLÇÜLMEDİ") |
| 99 | |
| 100 | def test_mean_metric_with_item_values_gets_ci(self): |
| 101 | p = _payload() |
| 102 | p["items"] = [{"retrieval": {"recall_at_5": v}} for v in |
| 103 | [1.0, 1.0, 1.0, 0.9, 1.0, 1.0, 0.95, 1.0, 1.0, 1.0]] |
| 104 | rows = {r["gid"]: r for r in evaluate_gates(p)} |
| 105 | self.assertIsNotNone(rows["K-03"]["ci"]) |
| 106 | self.assertIn(rows["K-03"]["durum"], ("GEÇTİ", "GEÇMEDİ")) |
| 107 | |
| 108 | def test_kademe_switches_thresholds(self): |
| 109 | mvp = {r["gid"]: r["esik"] for r in evaluate_gates(_payload(), "mvp")} |
| 110 | tam = {r["gid"]: r["esik"] for r in evaluate_gates(_payload(), "tam")} |
| 111 | self.assertEqual(mvp["A-02"], 0.85) |
| 112 | self.assertEqual(tam["A-02"], 0.99) |
| 113 | |
| 114 | def test_gate_absent_in_mvp_is_skipped(self): |
| 115 | rows = {r["gid"]: r for r in evaluate_gates(_payload(), "mvp")} |
| 116 | self.assertEqual(rows["C-05"]["durum"], "ATLANDI") |
| 117 | rows_t = {r["gid"]: r for r in evaluate_gates(_payload(), "tam")} |
| 118 | self.assertNotEqual(rows_t["C-05"]["durum"], "ATLANDI") |
| 119 | |
| 120 | def test_bad_kademe_rejected(self): |
| 121 | with self.assertRaises(ValueError): |
| 122 | evaluate_gates(_payload(), "uydurma") |
| 123 | |
| 124 | def test_empty_payload_does_not_crash(self): |
| 125 | rows = evaluate_gates({}) |
| 126 | self.assertEqual(len(rows), len(GATES)) |
| 127 | self.assertTrue(all(r["durum"] in ("ÖLÇÜLMEDİ", "ATLANDI") for r in rows)) |
| 128 | |
| 129 | |
| 130 | class RenderTests(unittest.TestCase): |
| 131 | def test_matrix_contains_every_gate_and_a_summary(self): |
| 132 | md = render_matrix(_payload(), "mvp") |
| 133 | for g in GATES: |
| 134 | self.assertIn(g.gid, md) |
| 135 | self.assertIn("Özet:", md) |
| 136 | self.assertIn("ÖLÇEN KOD YOK", md) |
| 137 | |
| 138 | def test_matrix_declares_source_of_truth(self): |
| 139 | |
| 140 | md = render_matrix(_payload()) |
| 141 | self.assertIn("benchmark.md", md) |
| 142 | self.assertIn("Kadir", md) |
| 143 | |
| 144 | def test_summary_counts_add_up(self): |
| 145 | rows = evaluate_gates(_payload()) |
| 146 | s = summary(rows) |
| 147 | self.assertEqual(sum(s.values()), len(rows)) |
| 148 | |
| 149 | |
| 150 | class ByDimensionTests(unittest.TestCase): |
| 151 | def test_groups_by_any_field(self): |
| 152 | items = [{"unite": "U1", "id": "a"}, {"unite": "U1", "id": "b"}, |
| 153 | {"unite": "U2", "id": "c"}] |
| 154 | g = by_dimension(items, "unite") |
| 155 | self.assertEqual(sorted(g), ["U1", "U2"]) |
| 156 | self.assertEqual(len(g["U1"]), 2) |
| 157 | |
| 158 | def test_missing_field_becomes_none_bucket(self): |
| 159 | g = by_dimension([{"id": "a"}], "senaryo") |
| 160 | self.assertIn(None, g) |
| 161 | |
| 162 | |
| 163 | if __name__ == "__main__": |
| 164 | unittest.main() |