| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | import json |
| 18 | import os |
| 19 | import tempfile |
| 20 | import time |
| 21 | import unittest |
| 22 | import warnings |
| 23 | |
| 24 | from src import costlog |
| 25 | from src.pricing import Usage |
| 26 | |
| 27 | |
| 28 | def _dolgu(yol, n): |
| 29 | with open(yol, "w", encoding="utf-8") as f: |
| 30 | for i in range(1, n + 1): |
| 31 | f.write(json.dumps({ |
| 32 | "run_id": f"R{i:05d}", "ts": "2026-09-12T00:00:00Z", |
| 33 | "module": "chat", "model": "deepseek-chat", "tier": "peak", |
| 34 | "items": 1, "config": {}, "usage": {"in_hit": 0, "in_miss": 10, |
| 35 | "out": 5, "reasoning": 0}, "cost_usd": 0.0005, |
| 36 | "unit_cost_usd": 0.0005, "quality": {}, "note": "x" * 40}) + "\n") |
| 37 | |
| 38 | |
| 39 | class HotPathTests(unittest.TestCase): |
| 40 | def setUp(self): |
| 41 | self.d = tempfile.mkdtemp() |
| 42 | self.ledger = os.path.join(self.d, "runs.jsonl") |
| 43 | self.md = os.path.join(self.d, "Maliyet.md") |
| 44 | self.u = Usage(input_cache_hit=0, input_cache_miss=10, output=5) |
| 45 | |
| 46 | def _kaydet(self): |
| 47 | return costlog.record(module="chat", model="deepseek-chat", usage=self.u, |
| 48 | ledger=self.ledger, maliyet=self.md) |
| 49 | |
| 50 | def test_render_is_not_in_the_request_path(self): |
| 51 | |
| 52 | self.assertFalse(costlog.RENDER_ON_RECORD) |
| 53 | _dolgu(self.ledger, 100) |
| 54 | self._kaydet() |
| 55 | self.assertFalse(os.path.exists(self.md), "istek yolunda render yapıldı") |
| 56 | |
| 57 | def test_latency_does_not_grow_with_ledger_size(self): |
| 58 | |
| 59 | sureler = {} |
| 60 | for n in (1_000, 50_000): |
| 61 | _dolgu(self.ledger, n) |
| 62 | t = time.perf_counter() |
| 63 | for _ in range(5): |
| 64 | self._kaydet() |
| 65 | sureler[n] = (time.perf_counter() - t) / 5 |
| 66 | self.assertLess(sureler[50_000], 0.05, "50k satırda record() hâlâ yavaş") |
| 67 | |
| 68 | self.assertLess(sureler[50_000], sureler[1_000] * 10 + 0.01) |
| 69 | |
| 70 | def test_ids_stay_monotonic_without_scanning_everything(self): |
| 71 | _dolgu(self.ledger, 1_000) |
| 72 | r1 = self._kaydet() |
| 73 | r2 = self._kaydet() |
| 74 | self.assertEqual(r1["run_id"], "R1001") |
| 75 | self.assertEqual(r2["run_id"], "R1002") |
| 76 | |
| 77 | def test_broken_last_line_does_not_break_id_assignment(self): |
| 78 | _dolgu(self.ledger, 10) |
| 79 | with open(self.ledger, "a", encoding="utf-8") as f: |
| 80 | f.write("{yarim satir\n") |
| 81 | self.assertEqual(self._kaydet()["run_id"], "R0011") |
| 82 | |
| 83 | def test_empty_ledger_starts_at_one(self): |
| 84 | self.assertEqual(self._kaydet()["run_id"], "R0001") |
| 85 | |
| 86 | def test_render_can_still_be_enabled_explicitly(self): |
| 87 | from unittest import mock |
| 88 | _dolgu(self.ledger, 10) |
| 89 | with mock.patch.object(costlog, "RENDER_ON_RECORD", True): |
| 90 | self._kaydet() |
| 91 | self.assertTrue(os.path.exists(self.md)) |
| 92 | |
| 93 | |
| 94 | class NeverBreakTheAnswerTests(unittest.TestCase): |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | def test_dirless_path_does_not_raise(self): |
| 104 | import os as _os |
| 105 | onceki = _os.getcwd() |
| 106 | d = tempfile.mkdtemp() |
| 107 | try: |
| 108 | _os.chdir(d) |
| 109 | rec = costlog.record(module="chat", model="deepseek-chat", |
| 110 | usage=Usage(), ledger="runs.jsonl", |
| 111 | maliyet="Maliyet.md") |
| 112 | self.assertEqual(rec["run_id"], "R0001") |
| 113 | finally: |
| 114 | _os.chdir(onceki) |
| 115 | |
| 116 | def test_record_safe_swallows_and_warns(self): |
| 117 | with warnings.catch_warnings(record=True) as w: |
| 118 | warnings.simplefilter("always") |
| 119 | out = costlog.record_safe(module="chat", model="deepseek-chat", |
| 120 | usage=Usage(), |
| 121 | ledger="/dev/null/yok/runs.jsonl") |
| 122 | self.assertIsNone(out) |
| 123 | self.assertTrue(any(issubclass(x.category, RuntimeWarning) for x in w)) |
| 124 | |
| 125 | def test_generators_default_to_the_safe_recorder(self): |
| 126 | |
| 127 | import inspect |
| 128 | from src.generate import generator, question_gen |
| 129 | from src.summarize import summarizer |
| 130 | for mod in (generator, question_gen, summarizer): |
| 131 | with self.subTest(mod.__name__): |
| 132 | self.assertIn("costlog.record_safe", inspect.getsource(mod)) |
| 133 | |
| 134 | |
| 135 | class RenderCliTests(unittest.TestCase): |
| 136 | def test_cli_renders_from_the_ledger(self): |
| 137 | d = tempfile.mkdtemp() |
| 138 | ledger = os.path.join(d, "runs.jsonl") |
| 139 | out = os.path.join(d, "Maliyet.md") |
| 140 | _dolgu(ledger, 20) |
| 141 | self.assertEqual(costlog.main(["render", "--ledger", ledger, "--out", out]), 0) |
| 142 | self.assertTrue(os.path.exists(out)) |
| 143 | |
| 144 | |
| 145 | if __name__ == "__main__": |
| 146 | unittest.main() |