"""Unit tests for Ollama backend helpers."""
import httpx
from navi.exceptions import LLMConnectionError
from navi.llm.fallback import FallbackOllamaBackend, ServerEntry
from navi.llm.ollama import _base_options, _classify_error, _resolve_think
def test_classify_read_timeout_as_connection_error():
err = _classify_error(httpx.ReadTimeout("timed out"))
assert isinstance(err, LLMConnectionError)
def test_classify_empty_timeout_message_as_connection_error():
err = _classify_error(httpx.ReadTimeout(""))
assert isinstance(err, LLMConnectionError)
assert str(err) == "ReadTimeout"
def test_fallback_client_uses_expanded_timeout(monkeypatch):
import navi.llm.fallback as fallback_mod
import navi.config as config_mod
captured = {}
class FakeOllamaBackend:
def __init__(self, **kwargs):
captured.update(kwargs)
monkeypatch.setattr(fallback_mod, "OllamaBackend", FakeOllamaBackend)
monkeypatch.setattr(config_mod.settings, "ollama_request_timeout", 30)
monkeypatch.setattr(config_mod.settings, "llm_complete_timeout", 120)
monkeypatch.setattr(config_mod.settings, "llm_stream_first_chunk_timeout", 180)
backend = FallbackOllamaBackend([ServerEntry(host="http://ollama.test")])
backend._get_client(ServerEntry(host="http://ollama.test", api_key="secret"))
assert captured["host"] == "http://ollama.test"
assert captured["api_key"] == "secret"
assert captured["timeout"] == 180
def test_think_resolves_from_global_setting(monkeypatch):
import navi.config as config_mod
monkeypatch.setattr(config_mod.settings, "ollama_think", True)
assert _resolve_think(None) is True
assert _resolve_think(False) is False
def test_base_options_do_not_include_think(monkeypatch):
import navi.llm.ollama as ollama_mod
monkeypatch.setattr(ollama_mod.settings, "ollama_num_ctx", 1234)
opts = _base_options(0.2, top_k=20, top_p=0.8)
assert opts == {
"temperature": 0.2,
"num_ctx": 1234,
"top_k": 20,
"top_p": 0.8,
}