|
| 1 | +"""Unit tests for LLM controller model resolution logic. |
| 2 | +
|
| 3 | +Tests that _resolve_user_settings correctly applies: |
| 4 | +1. Explicit model passes through unchanged |
| 5 | +2. Empty model resolves to user default |
| 6 | +3. No user default falls back to DEFAULT_CHAT_MODEL |
| 7 | +4. Unauthenticated user gets DEFAULT_CHAT_MODEL |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 12 | +from src.controllers.llm import LLMController |
| 13 | +from src.constants.llm import DEFAULT_CHAT_MODEL |
| 14 | + |
| 15 | + |
| 16 | +class FakeSettings: |
| 17 | + """Minimal stand-in for user settings.""" |
| 18 | + |
| 19 | + def __init__(self, default_model=None): |
| 20 | + self.default_model = default_model |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def mock_store(): |
| 25 | + return MagicMock() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def mock_config(): |
| 30 | + return {"configurable": {}, "metadata": {}} |
| 31 | + |
| 32 | + |
| 33 | +class TestResolveUserSettings: |
| 34 | + """Test _resolve_user_settings model resolution.""" |
| 35 | + |
| 36 | + @pytest.mark.asyncio |
| 37 | + async def test_explicit_model_passes_through(self, mock_store, mock_config): |
| 38 | + """When client sends an explicit model, it should be used as-is.""" |
| 39 | + controller = LLMController(user_id="user-1", store=mock_store, config=mock_config) |
| 40 | + |
| 41 | + with patch.object(controller, "_resolve_user_settings", wraps=controller._resolve_user_settings): |
| 42 | + with patch("src.controllers.llm.UserSettingsRepo") as MockRepo: |
| 43 | + instance = MockRepo.return_value |
| 44 | + instance._get_or_create = AsyncMock( |
| 45 | + return_value=FakeSettings(default_model="anthropic:claude-sonnet-4") |
| 46 | + ) |
| 47 | + instance._decrypt_keys = MagicMock(return_value={}) |
| 48 | + |
| 49 | + model, api_key = await controller._resolve_user_settings("openai:gpt-4o") |
| 50 | + |
| 51 | + assert model == "openai:gpt-4o" |
| 52 | + |
| 53 | + @pytest.mark.asyncio |
| 54 | + async def test_empty_model_resolves_to_user_default(self, mock_store, mock_config): |
| 55 | + """When client sends empty model, user's default_model should be used.""" |
| 56 | + controller = LLMController(user_id="user-1", store=mock_store, config=mock_config) |
| 57 | + |
| 58 | + with patch("src.controllers.llm.UserSettingsRepo") as MockRepo: |
| 59 | + instance = MockRepo.return_value |
| 60 | + instance._get_or_create = AsyncMock( |
| 61 | + return_value=FakeSettings(default_model="anthropic:claude-sonnet-4") |
| 62 | + ) |
| 63 | + instance._decrypt_keys = MagicMock(return_value={}) |
| 64 | + |
| 65 | + model, _ = await controller._resolve_user_settings("") |
| 66 | + |
| 67 | + assert model == "anthropic:claude-sonnet-4" |
| 68 | + |
| 69 | + @pytest.mark.asyncio |
| 70 | + async def test_no_user_default_falls_back_to_system(self, mock_store, mock_config): |
| 71 | + """When user has no default_model, system DEFAULT_CHAT_MODEL should be used.""" |
| 72 | + controller = LLMController(user_id="user-1", store=mock_store, config=mock_config) |
| 73 | + |
| 74 | + with patch("src.controllers.llm.UserSettingsRepo") as MockRepo: |
| 75 | + instance = MockRepo.return_value |
| 76 | + instance._get_or_create = AsyncMock( |
| 77 | + return_value=FakeSettings(default_model=None) |
| 78 | + ) |
| 79 | + instance._decrypt_keys = MagicMock(return_value={}) |
| 80 | + |
| 81 | + model, _ = await controller._resolve_user_settings("") |
| 82 | + |
| 83 | + assert model == DEFAULT_CHAT_MODEL |
| 84 | + |
| 85 | + @pytest.mark.asyncio |
| 86 | + async def test_unauthenticated_user_gets_system_default(self, mock_store, mock_config): |
| 87 | + """Unauthenticated users (no user_id) should get DEFAULT_CHAT_MODEL.""" |
| 88 | + controller = LLMController(user_id=None, store=mock_store, config=mock_config) |
| 89 | + |
| 90 | + model, api_key = await controller._resolve_user_settings("") |
| 91 | + |
| 92 | + assert model == DEFAULT_CHAT_MODEL |
| 93 | + assert api_key is None |
| 94 | + |
| 95 | + @pytest.mark.asyncio |
| 96 | + async def test_unauthenticated_user_explicit_model_preserved(self, mock_store, mock_config): |
| 97 | + """Unauthenticated users with explicit model should keep it.""" |
| 98 | + controller = LLMController(user_id=None, store=mock_store, config=mock_config) |
| 99 | + |
| 100 | + model, api_key = await controller._resolve_user_settings("openai:gpt-4o") |
| 101 | + |
| 102 | + assert model == "openai:gpt-4o" |
| 103 | + assert api_key is None |
| 104 | + |
| 105 | + |
| 106 | +class TestLLMRequestModelDefault: |
| 107 | + """Test that LLMRequest.model defaults to None.""" |
| 108 | + |
| 109 | + def test_model_defaults_to_none(self): |
| 110 | + """LLMRequest without model field should default to None.""" |
| 111 | + from src.schemas.entities.llm import LLMRequest |
| 112 | + |
| 113 | + payload = { |
| 114 | + "input": {"messages": [{"role": "user", "content": "Hello"}]}, |
| 115 | + } |
| 116 | + request = LLMRequest(**payload) |
| 117 | + assert request.model is None |
| 118 | + |
| 119 | + def test_model_explicit_value_preserved(self): |
| 120 | + """LLMRequest with explicit model should preserve it.""" |
| 121 | + from src.schemas.entities.llm import LLMRequest |
| 122 | + |
| 123 | + payload = { |
| 124 | + "input": {"messages": [{"role": "user", "content": "Hello"}]}, |
| 125 | + "model": "openai:gpt-4o", |
| 126 | + } |
| 127 | + request = LLMRequest(**payload) |
| 128 | + assert request.model == "openai:gpt-4o" |
| 129 | + |
| 130 | + def test_model_empty_string_preserved(self): |
| 131 | + """LLMRequest with empty string model should preserve it.""" |
| 132 | + from src.schemas.entities.llm import LLMRequest |
| 133 | + |
| 134 | + payload = { |
| 135 | + "input": {"messages": [{"role": "user", "content": "Hello"}]}, |
| 136 | + "model": "", |
| 137 | + } |
| 138 | + request = LLMRequest(**payload) |
| 139 | + assert request.model == "" |
0 commit comments