|
| 1 | +"""Tests for the Ollama plugin model type handler.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic_ai.models.openai import OpenAIChatModel, OpenAIResponsesModel |
| 7 | + |
| 8 | +from code_puppy.plugins.ollama.register_callbacks import ( |
| 9 | + _DEFAULT_OLLAMA_API_KEY, |
| 10 | + _DEFAULT_OLLAMA_BASE_URL, |
| 11 | + _get_ollama_model_types, |
| 12 | + create_ollama_model, |
| 13 | +) |
| 14 | + |
| 15 | +MODULE = "code_puppy.plugins.ollama.register_callbacks" |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_async_client(): |
| 20 | + with patch(f"{MODULE}.create_async_client") as mock: |
| 21 | + mock.return_value = MagicMock() |
| 22 | + yield mock |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def mock_get_custom_config(): |
| 27 | + with patch(f"{MODULE}.get_custom_config") as mock: |
| 28 | + mock.return_value = ( |
| 29 | + "http://remote:8080/v1", |
| 30 | + {"X-Key": "val"}, |
| 31 | + None, |
| 32 | + "custom-key", |
| 33 | + ) |
| 34 | + yield mock |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def mock_provider(): |
| 39 | + with patch(f"{MODULE}.OpenAIProvider") as mock: |
| 40 | + mock.return_value = MagicMock() |
| 41 | + yield mock |
| 42 | + |
| 43 | + |
| 44 | +def test_custom_endpoint_uses_get_custom_config( |
| 45 | + mock_async_client, mock_get_custom_config, mock_provider |
| 46 | +): |
| 47 | + model_config = { |
| 48 | + "name": "codellama:34b", |
| 49 | + "custom_endpoint": { |
| 50 | + "url": "http://remote:8080/v1", |
| 51 | + "api_key": "custom-key", |
| 52 | + }, |
| 53 | + } |
| 54 | + result = create_ollama_model("my-model", model_config, {}) |
| 55 | + |
| 56 | + mock_get_custom_config.assert_called_once_with(model_config) |
| 57 | + assert isinstance(result, OpenAIChatModel) |
| 58 | + |
| 59 | + |
| 60 | +def test_no_custom_endpoint_defaults_to_localhost( |
| 61 | + mock_async_client, mock_provider, monkeypatch |
| 62 | +): |
| 63 | + monkeypatch.delenv("OLLAMA_HOST", raising=False) |
| 64 | + model_config = {"name": "llama3:8b"} |
| 65 | + result = create_ollama_model("my-model", model_config, {}) |
| 66 | + |
| 67 | + assert isinstance(result, OpenAIChatModel) |
| 68 | + # Verify the client was created with empty headers and no verify |
| 69 | + mock_async_client.assert_called_once_with(headers={}, verify=None) |
| 70 | + |
| 71 | + |
| 72 | +def test_ollama_host_env_appends_v1(mock_async_client, mock_provider, monkeypatch): |
| 73 | + monkeypatch.setenv("OLLAMA_HOST", "http://myserver:11434") |
| 74 | + model_config = {"name": "gpt3:30b"} |
| 75 | + create_ollama_model("my-model", model_config, {}) |
| 76 | + |
| 77 | + # Check the provider was called with /v1 appended |
| 78 | + call_kwargs = mock_provider.call_args[1] |
| 79 | + assert call_kwargs["base_url"] == "http://myserver:11434/v1" |
| 80 | + |
| 81 | + |
| 82 | +def test_ollama_host_already_ends_with_v1( |
| 83 | + mock_async_client, mock_provider, monkeypatch |
| 84 | +): |
| 85 | + monkeypatch.setenv("OLLAMA_HOST", "http://myserver:11434/v1") |
| 86 | + model_config = {"name": "gpt3:30b"} |
| 87 | + create_ollama_model("my-model", model_config, {}) |
| 88 | + |
| 89 | + call_kwargs = mock_provider.call_args[1] |
| 90 | + assert call_kwargs["base_url"] == "http://myserver:11434/v1" |
| 91 | + |
| 92 | + |
| 93 | +def test_ollama_host_trailing_slash_stripped( |
| 94 | + mock_async_client, mock_provider, monkeypatch |
| 95 | +): |
| 96 | + monkeypatch.setenv("OLLAMA_HOST", "http://myserver:11434/") |
| 97 | + model_config = {"name": "gpt3:30b"} |
| 98 | + create_ollama_model("my-model", model_config, {}) |
| 99 | + |
| 100 | + call_kwargs = mock_provider.call_args[1] |
| 101 | + assert call_kwargs["base_url"] == "http://myserver:11434/v1" |
| 102 | + |
| 103 | + |
| 104 | +def test_ollama_host_empty_string_uses_default( |
| 105 | + mock_async_client, mock_provider, monkeypatch |
| 106 | +): |
| 107 | + monkeypatch.setenv("OLLAMA_HOST", "") |
| 108 | + model_config = {"name": "llama3:8b"} |
| 109 | + create_ollama_model("my-model", model_config, {}) |
| 110 | + |
| 111 | + call_kwargs = mock_provider.call_args[1] |
| 112 | + assert call_kwargs["base_url"] == _DEFAULT_OLLAMA_BASE_URL |
| 113 | + |
| 114 | + |
| 115 | +def test_returns_open_ai_chat_model_not_responses( |
| 116 | + mock_async_client, mock_provider, monkeypatch |
| 117 | +): |
| 118 | + monkeypatch.delenv("OLLAMA_HOST", raising=False) |
| 119 | + model_config = {"name": "llama3:8b"} |
| 120 | + result = create_ollama_model("my-model", model_config, {}) |
| 121 | + |
| 122 | + assert isinstance(result, OpenAIChatModel) |
| 123 | + assert not isinstance(result, OpenAIResponsesModel) |
| 124 | + |
| 125 | + |
| 126 | +def test_provider_is_set_on_model(mock_async_client, mock_provider, monkeypatch): |
| 127 | + monkeypatch.delenv("OLLAMA_HOST", raising=False) |
| 128 | + model_config = {"name": "llama3:8b"} |
| 129 | + result = create_ollama_model("my-model", model_config, {}) |
| 130 | + |
| 131 | + assert result is not None |
| 132 | + assert hasattr(result, "provider") |
| 133 | + assert result.provider is not None |
| 134 | + |
| 135 | + |
| 136 | +def test_uses_model_config_name(mock_async_client, mock_provider, monkeypatch): |
| 137 | + monkeypatch.delenv("OLLAMA_HOST", raising=False) |
| 138 | + model_config = {"name": "gpt3:30b"} |
| 139 | + result = create_ollama_model("config-key", model_config, {}) |
| 140 | + |
| 141 | + assert result is not None |
| 142 | + assert result.model_name == "gpt3:30b" |
| 143 | + |
| 144 | + |
| 145 | +def test_falls_back_to_model_name_key(mock_async_client, mock_provider, monkeypatch): |
| 146 | + monkeypatch.delenv("OLLAMA_HOST", raising=False) |
| 147 | + model_config = {} # No "name" key |
| 148 | + result = create_ollama_model("fallback-name", model_config, {}) |
| 149 | + |
| 150 | + assert result is not None |
| 151 | + assert result.model_name == "fallback-name" |
| 152 | + |
| 153 | + |
| 154 | +def test_returns_none_on_exception(monkeypatch): |
| 155 | + monkeypatch.delenv("OLLAMA_HOST", raising=False) |
| 156 | + model_config = { |
| 157 | + "name": "test", |
| 158 | + "custom_endpoint": {"url": "http://x", "api_key": "k"}, |
| 159 | + } |
| 160 | + with patch(f"{MODULE}.get_custom_config", side_effect=RuntimeError("boom")): |
| 161 | + result = create_ollama_model("bad-model", model_config, {}) |
| 162 | + assert result is None |
| 163 | + |
| 164 | + |
| 165 | +def test_get_ollama_model_types_structure(): |
| 166 | + result = _get_ollama_model_types() |
| 167 | + assert isinstance(result, list) |
| 168 | + assert len(result) == 1 |
| 169 | + entry = result[0] |
| 170 | + assert entry["type"] == "ollama" |
| 171 | + assert callable(entry["handler"]) |
| 172 | + assert entry["handler"] is create_ollama_model |
| 173 | + |
| 174 | + |
| 175 | +def test_api_key_defaults_to_ollama(mock_async_client, mock_provider, monkeypatch): |
| 176 | + monkeypatch.delenv("OLLAMA_HOST", raising=False) |
| 177 | + model_config = {"name": "llama3:8b"} |
| 178 | + create_ollama_model("my-model", model_config, {}) |
| 179 | + |
| 180 | + call_kwargs = mock_provider.call_args[1] |
| 181 | + assert call_kwargs["api_key"] == _DEFAULT_OLLAMA_API_KEY |
| 182 | + |
| 183 | + |
| 184 | +def test_api_key_fallback_when_custom_returns_none(mock_async_client, mock_provider): |
| 185 | + with patch(f"{MODULE}.get_custom_config") as mock_gcc: |
| 186 | + mock_gcc.return_value = ("http://x/v1", {}, None, None) |
| 187 | + create_ollama_model( |
| 188 | + "m", |
| 189 | + {"name": "t", "custom_endpoint": {"url": "http://x/v1"}}, |
| 190 | + {}, |
| 191 | + ) |
| 192 | + |
| 193 | + call_kwargs = mock_provider.call_args[1] |
| 194 | + assert call_kwargs["api_key"] == _DEFAULT_OLLAMA_API_KEY |
| 195 | + |
| 196 | + |
| 197 | +def test_default_constants(): |
| 198 | + assert _DEFAULT_OLLAMA_BASE_URL == "http://localhost:11434/v1" |
| 199 | + assert _DEFAULT_OLLAMA_API_KEY == "ollama" |
0 commit comments