| 1 | |
| 2 | |
| 3 | import json |
| 4 | import tempfile |
| 5 | import unittest |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from benchmark_qa_report import ( |
| 9 | RESULT_SCHEMA_VERSION, |
| 10 | build_json_report, |
| 11 | collect_results, |
| 12 | render_markdown, |
| 13 | write_reports, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | class _FakeEngine: |
| 18 | def handle(self, payload: dict) -> dict: |
| 19 | if payload["query"] == "doğru": |
| 20 | intent = "greeting" |
| 21 | text = "Merhaba" |
| 22 | response_id = "welcome_message" |
| 23 | fallback = False |
| 24 | outcome = "allow" |
| 25 | reason_code = "allowed" |
| 26 | else: |
| 27 | intent = None |
| 28 | text = "Bunu yanıtlayamam." |
| 29 | response_id = "fallback_clarification" |
| 30 | fallback = True |
| 31 | outcome = "fallback" |
| 32 | reason_code = "out_of_scope" |
| 33 | return { |
| 34 | "trace_id": payload["trace_id"], |
| 35 | "intent": intent, |
| 36 | "response_id": response_id, |
| 37 | "confidence": 0.75, |
| 38 | "fallback": fallback, |
| 39 | "auth_action": None, |
| 40 | "navigation": None, |
| 41 | "assistant_meta": { |
| 42 | "outcome": outcome, |
| 43 | "action_id": intent, |
| 44 | "reason_code": reason_code, |
| 45 | "navigation": None, |
| 46 | }, |
| 47 | "text": text, |
| 48 | } |
| 49 | |
| 50 | |
| 51 | class BenchmarkQaReportTests(unittest.TestCase): |
| 52 | def test_collects_pass_and_oos_from_full_response(self) -> None: |
| 53 | records = [ |
| 54 | {"question": "doğru", "expected_intent": "greeting", "role": "ziyaretci"}, |
| 55 | {"question": "kapsam dışı", "expected_intent": "oos", "role": "ogrenci"}, |
| 56 | ] |
| 57 | rows = collect_results(records, engine=_FakeEngine()) |
| 58 | self.assertEqual(len(rows), 2) |
| 59 | self.assertTrue(all(row["passed"] for row in rows)) |
| 60 | self.assertEqual(rows[1]["actual_intent"], "oos") |
| 61 | self.assertEqual(rows[0]["trace_id"], "benchmark-0001") |
| 62 | self.assertEqual(rows[0]["response"]["text"], "Merhaba") |
| 63 | |
| 64 | def test_combined_json_has_overall_and_role_summaries(self) -> None: |
| 65 | records = [ |
| 66 | {"question": "doğru", "expected_intent": "greeting", "role": "ziyaretci"}, |
| 67 | {"question": "kapsam dışı", "expected_intent": "oos", "role": "ogrenci"}, |
| 68 | ] |
| 69 | rows = collect_results(records, engine=_FakeEngine()) |
| 70 | report = build_json_report(rows) |
| 71 | |
| 72 | self.assertEqual(report["schema_version"], RESULT_SCHEMA_VERSION) |
| 73 | self.assertEqual(report["source"], "data/benchmark.jsonl") |
| 74 | self.assertEqual(report["summary"]["total"], 2) |
| 75 | self.assertEqual(report["summary"]["passed"], 2) |
| 76 | self.assertEqual(report["summary"]["failed"], 0) |
| 77 | self.assertEqual(report["summary"]["by_role"]["ziyaretci"]["total"], 1) |
| 78 | self.assertEqual(report["summary"]["by_role"]["ogrenci"]["total"], 1) |
| 79 | self.assertEqual(report["results"][1]["question"], records[1]["question"]) |
| 80 | self.assertEqual(report["results"][1]["role"], records[1]["role"]) |
| 81 | self.assertEqual(report["results"][1]["expected_intent"], "oos") |
| 82 | self.assertEqual(report["results"][1]["expected_contract"]["outcome"], "fallback") |
| 83 | |
| 84 | def test_writes_one_json_and_optional_markdown_from_same_run(self) -> None: |
| 85 | with tempfile.TemporaryDirectory() as temporary: |
| 86 | root = Path(temporary) |
| 87 | source = root / "benchmark.jsonl" |
| 88 | source.write_text( |
| 89 | json.dumps( |
| 90 | { |
| 91 | "question": "doğru", |
| 92 | "expected_intent": "greeting", |
| 93 | "role": "ziyaretci", |
| 94 | }, |
| 95 | ensure_ascii=False, |
| 96 | ) |
| 97 | + "\n", |
| 98 | encoding="utf-8", |
| 99 | ) |
| 100 | json_output = root / "results.json" |
| 101 | markdown_output = root / "report.md" |
| 102 | |
| 103 | _, _, rows = write_reports( |
| 104 | benchmark_path=source, |
| 105 | json_output=json_output, |
| 106 | markdown_output=markdown_output, |
| 107 | engine=_FakeEngine(), |
| 108 | ) |
| 109 | |
| 110 | report = json.loads(json_output.read_text(encoding="utf-8")) |
| 111 | self.assertEqual(len(rows), 1) |
| 112 | self.assertEqual(report["summary"]["total"], 1) |
| 113 | self.assertEqual(report["results"][0]["response"]["trace_id"], "benchmark-0001") |
| 114 | self.assertIn("### 001 · PASS", markdown_output.read_text(encoding="utf-8")) |
| 115 | |
| 116 | def test_markdown_contains_summary_metadata_and_every_answer(self) -> None: |
| 117 | rows = collect_results( |
| 118 | [{"question": "doğru", "expected_intent": "wrong", "role": "ziyaretci"}], |
| 119 | engine=_FakeEngine(), |
| 120 | ) |
| 121 | markdown = render_markdown(rows) |
| 122 | self.assertIn("FAIL: **1**", markdown) |
| 123 | self.assertIn("Beklenen intent: `wrong`", markdown) |
| 124 | self.assertIn("Gerçek intent: `greeting`", markdown) |
| 125 | self.assertIn("response_id: `welcome_message`", markdown) |
| 126 | self.assertIn("navigation: `None`", markdown) |
| 127 | self.assertIn("> Merhaba", markdown) |
| 128 | |
| 129 | |
| 130 | if __name__ == "__main__": |
| 131 | unittest.main() |