Newer
Older
navi-1 / tests / unit / llm / test_ollama.py
"""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 _classify_error


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