| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import os |
| 13 | import unittest |
| 14 | from unittest import mock |
| 15 | |
| 16 | from src.segment.config import ESKI_LLM_ADLARI, SegmentConfig, SegmentConfigError |
| 17 | from src.segment.provider import build_provider |
| 18 | |
| 19 | |
| 20 | _CLEARED = { |
| 21 | name: "" |
| 22 | for name in ( |
| 23 | "LLM_API_KEY", |
| 24 | "LLM_BASE_URL", |
| 25 | "LLM_MODEL_FLASH", |
| 26 | "LLM_MODEL_PRO", |
| 27 | "LLM_MODEL_REASONER", |
| 28 | "LLM_MODEL_ROLE", |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | |
| 33 | def _env(**overrides): |
| 34 | values = dict(_CLEARED) |
| 35 | values.update(overrides) |
| 36 | return mock.patch.dict(os.environ, values, clear=False) |
| 37 | |
| 38 | |
| 39 | class CleanBreakTests(unittest.TestCase): |
| 40 | def test_the_new_names_work(self): |
| 41 | with _env(LLM_API_KEY="anahtar", LLM_BASE_URL="https://gecit.ornek/v1"): |
| 42 | cfg = SegmentConfig.from_env() |
| 43 | self.assertEqual(cfg.api_key, "anahtar") |
| 44 | self.assertEqual(cfg.base_url, "https://gecit.ornek/v1") |
| 45 | |
| 46 | def test_every_removed_name_is_refused(self): |
| 47 | for eski in ESKI_LLM_ADLARI: |
| 48 | with self.subTest(eski=eski): |
| 49 | with _env(**{eski: "deger"}): |
| 50 | with self.assertRaises(SegmentConfigError) as caught: |
| 51 | SegmentConfig.from_env() |
| 52 | self.assertIn(eski, str(caught.exception)) |
| 53 | self.assertIn("LLM_API_KEY", str(caught.exception)) |
| 54 | |
| 55 | def test_the_old_key_does_not_silently_work(self): |
| 56 | |
| 57 | with _env(DEEPSEEK_API_KEY="eski-anahtar"): |
| 58 | with self.assertRaises(SegmentConfigError): |
| 59 | SegmentConfig.from_env() |
| 60 | |
| 61 | def test_a_mixed_environment_is_refused_too(self): |
| 62 | |
| 63 | with _env(LLM_API_KEY="anahtar", DEEPSEEK_API_KEY="eski"): |
| 64 | with self.assertRaises(SegmentConfigError): |
| 65 | SegmentConfig.from_env() |
| 66 | |
| 67 | def test_no_key_at_all_means_no_key(self): |
| 68 | with _env(): |
| 69 | self.assertFalse(SegmentConfig.from_env().api_key) |
| 70 | |
| 71 | |
| 72 | class ModelKnobTests(unittest.TestCase): |
| 73 | def test_per_role_model_overrides(self): |
| 74 | with _env(LLM_MODEL_FLASH="gpt-x-mini", LLM_MODEL_PRO="gpt-x"): |
| 75 | cfg = SegmentConfig.from_env() |
| 76 | self.assertEqual(cfg.model_ids["flash"], "gpt-x-mini") |
| 77 | self.assertEqual(cfg.model_ids["pro"], "gpt-x") |
| 78 | |
| 79 | def test_role_selection_is_validated(self): |
| 80 | with _env(LLM_MODEL_ROLE="yok-boyle-rol"): |
| 81 | with self.assertRaises(SegmentConfigError): |
| 82 | SegmentConfig.from_env() |
| 83 | with _env(LLM_MODEL_ROLE="pro"): |
| 84 | self.assertEqual(SegmentConfig.from_env().model_role, "pro") |
| 85 | |
| 86 | |
| 87 | class GatewayTests(unittest.TestCase): |
| 88 | def test_the_default_gateway_can_be_replaced(self): |
| 89 | with _env(): |
| 90 | self.assertEqual(SegmentConfig.from_env().base_url, "https://api.deepseek.com") |
| 91 | |
| 92 | def test_a_base_url_without_a_key_never_selects_a_real_gateway(self): |
| 93 | with _env(LLM_BASE_URL="https://gecit.ornek/v1", LLM_API_KEY=""): |
| 94 | cfg = SegmentConfig.from_env() |
| 95 | self.assertFalse(cfg.api_key) |
| 96 | self.assertEqual(build_provider(cfg, mock=False).name, "mock") |
| 97 | |
| 98 | def test_a_key_selects_the_real_gateway(self): |
| 99 | with _env(LLM_API_KEY="anahtar", LLM_BASE_URL="https://gecit.ornek/v1"): |
| 100 | self.assertNotEqual(build_provider(SegmentConfig.from_env(), mock=False).name, "mock") |
| 101 | |
| 102 | |
| 103 | if __name__ == "__main__": |
| 104 | unittest.main() |