|
| 1 | +"""Tests for OrcaRouter routing through the OpenAI-compatible client.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +from funclip.llm.openai_api import ( |
| 8 | + ORCAROUTER_API_BASE, |
| 9 | + openai_call, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def _mock_completion(content="ok"): |
| 14 | + completion = MagicMock() |
| 15 | + completion.choices = [MagicMock()] |
| 16 | + completion.choices[0].message.content = content |
| 17 | + return completion |
| 18 | + |
| 19 | + |
| 20 | +class TestOrcaRouterRouting(unittest.TestCase): |
| 21 | + def test_orcarouter_prefix_uses_gateway_base_url(self): |
| 22 | + client = MagicMock() |
| 23 | + client.chat.completions.create.return_value = _mock_completion("clip plan") |
| 24 | + |
| 25 | + with patch("funclip.llm.openai_api.OpenAI", return_value=client) as openai_cls: |
| 26 | + result = openai_call( |
| 27 | + "orca-key", |
| 28 | + "orcarouter/auto", |
| 29 | + "subtitle text", |
| 30 | + "find highlights", |
| 31 | + ) |
| 32 | + |
| 33 | + self.assertEqual(result, "clip plan") |
| 34 | + openai_cls.assert_called_once_with( |
| 35 | + api_key="orca-key", |
| 36 | + base_url=ORCAROUTER_API_BASE, |
| 37 | + ) |
| 38 | + # OrcaRouter is a multi-provider gateway: the model ID must keep the |
| 39 | + # `orcarouter/` prefix so the router knows which namespace to route to. |
| 40 | + call_kwargs = client.chat.completions.create.call_args[1] |
| 41 | + self.assertEqual(call_kwargs["model"], "orcarouter/auto") |
| 42 | + |
| 43 | + def test_orcarouter_api_key_falls_back_to_env(self): |
| 44 | + client = MagicMock() |
| 45 | + client.chat.completions.create.return_value = _mock_completion() |
| 46 | + |
| 47 | + with patch.dict(os.environ, {"ORCAROUTER_API_KEY": "env-orca-key"}, clear=False): |
| 48 | + with patch("funclip.llm.openai_api.OpenAI", return_value=client) as openai_cls: |
| 49 | + openai_call("", "orcarouter/auto", "text") |
| 50 | + |
| 51 | + openai_cls.assert_called_once_with( |
| 52 | + api_key="env-orca-key", |
| 53 | + base_url=ORCAROUTER_API_BASE, |
| 54 | + ) |
| 55 | + call_kwargs = client.chat.completions.create.call_args[1] |
| 56 | + self.assertEqual(call_kwargs["model"], "orcarouter/auto") |
| 57 | + |
| 58 | + def test_orcarouter_api_base_env_overrides(self): |
| 59 | + client = MagicMock() |
| 60 | + client.chat.completions.create.return_value = _mock_completion() |
| 61 | + |
| 62 | + with patch.dict( |
| 63 | + os.environ, |
| 64 | + {"ORCAROUTER_API_BASE": "https://gateway.example.com/v1"}, |
| 65 | + clear=False, |
| 66 | + ): |
| 67 | + with patch("funclip.llm.openai_api.OpenAI", return_value=client) as openai_cls: |
| 68 | + openai_call("orca-key", "orcarouter/fusion", "text") |
| 69 | + |
| 70 | + openai_cls.assert_called_once_with( |
| 71 | + api_key="orca-key", |
| 72 | + base_url="https://gateway.example.com/v1", |
| 73 | + ) |
| 74 | + |
| 75 | + def test_empty_orcarouter_model_raises(self): |
| 76 | + with self.assertRaises(ValueError): |
| 77 | + openai_call("key", "orcarouter/", "text") |
| 78 | + |
| 79 | + def test_missing_orcarouter_key_does_not_fall_back_to_openai_key(self): |
| 80 | + with patch.dict( |
| 81 | + os.environ, |
| 82 | + {"OPENAI_API_KEY": "openai-only-key"}, |
| 83 | + clear=True, |
| 84 | + ): |
| 85 | + with patch("funclip.llm.openai_api.OpenAI") as openai_cls: |
| 86 | + with self.assertRaisesRegex(ValueError, "ORCAROUTER_API_KEY"): |
| 87 | + openai_call("", "orcarouter/auto", "text") |
| 88 | + |
| 89 | + openai_cls.assert_not_called() |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + unittest.main() |
0 commit comments