Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

7.5 KB
Raw Blame
"""Ollama probe branches ``test_check_ollama_unit.py`` leaves open.

Ported from ``tests/web/routes/test_ollama_status.py`` (22 tests), deleted
by the Flask->FastAPI migration. Most of that file IS superseded by
``tests/web/routers/test_check_ollama_unit.py`` (provider short-circuit,
configured URL, model precedence, case-insensitive match, connection error,
the 400 for an unconfigured model) and the two auth assertions are covered
by ``tests/security/test_unauthenticated_reachability_census.py``.

What is recovered here is the residue — every branch of
``_probe_ollama_tags`` and its two callers' outcome maps that no successor
executes:

* the **old Ollama API format** (a bare list rather than ``{"models": [...]}``)
  on *both* endpoints — the successor's ``_ollama_tags_response`` helper
  always builds the new nested shape, so the ``else: models = data`` branch
  is never taken;
* ``invalid_json`` on both endpoints — including the surprising-but-
  deliberate ``running: True`` the status endpoint answers with;
* ``bad_status`` and ``timeout`` on the *model* endpoint (the successor
  covers them only on the status endpoint);
* the empty-model-list message, which is a different string from the
  model-not-found one.

Same seams and call style as ``test_check_ollama_unit.py``: the route
functions are called directly with a mocked ``Request``, patching
``get_user_db_session`` / ``SettingsManager`` / ``safe_get``.
"""

from contextlib import contextmanager
from unittest.mock import Mock, patch

import requests

API = "local_deep_research.web.routers.api"


def _patched_settings(values):
    """Patch get_user_db_session + SettingsManager so get_setting(key, default)
    resolves from ``values`` (falling back to the call-site default)."""

    @contextmanager
    def fake_db_session(*a, **kw):
        yield Mock()

    sm = Mock()
    sm.get_setting.side_effect = lambda key, default=None: values.get(
        key, default
    )
    return (
        patch(f"{API}.get_user_db_session", side_effect=fake_db_session),
        patch(f"{API}.SettingsManager", return_value=sm),
    )


def _request(params=None):
    req = Mock()
    req.query_params = params or {}
    return req


def _old_format_response(model_names):
    """A pre-``{"models": [...]}`` Ollama /api/tags body: a bare list."""
    resp = Mock()
    resp.status_code = 200
    resp.json.return_value = [{"name": name} for name in model_names]
    return resp


def _unparseable_response():
    resp = Mock()
    resp.status_code = 200
    resp.json.side_effect = ValueError("bad json")
    return resp


def _status_response(status_code):
    resp = Mock()
    resp.status_code = status_code
    return resp


def _call_status(settings, safe_get_mock):
    from local_deep_research.web.routers.api import check_ollama_status

    p1, p2 = _patched_settings(settings)
    with p1, p2, patch(f"{API}.safe_get", safe_get_mock):
        return check_ollama_status(_request(), username="alice")


def _call_model(settings, safe_get_mock, params=None):
    from local_deep_research.web.routers.api import check_ollama_model

    p1, p2 = _patched_settings(settings)
    with p1, p2, patch(f"{API}.safe_get", safe_get_mock):
        return check_ollama_model(_request(params), username="alice")


class TestCheckOllamaStatusResidualBranches:
    def test_old_api_format_bare_list_is_counted(self):
        """``_probe_ollama_tags``'s ``else: models = data`` branch.

        A pre-``models``-key Ollama answers ``/api/tags`` with a bare
        list. Counting it as one model, not zero, is the whole point of
        the two-format parse.
        """
        safe_get = Mock(return_value=_old_format_response(["llama3"]))
        result = _call_status({"llm.provider": "ollama"}, safe_get)

        assert result["running"] is True
        assert result["model_count"] == 1

    def test_new_api_format_counts_every_model(self):
        """Positive control for the count itself: ``model_count`` is the
        length of the list, not a truthiness flag."""
        resp = Mock()
        resp.status_code = 200
        resp.json.return_value = {
            "models": [{"name": "llama3"}, {"name": "gemma3:12b"}]
        }
        result = _call_status(
            {"llm.provider": "ollama"}, Mock(return_value=resp)
        )

        assert result["running"] is True
        assert result["model_count"] == 2

    def test_unparseable_body_still_reports_the_service_as_running(self):
        """A 200 with a body that will not parse means Ollama answered —
        so ``running`` stays True and the message says the *data* was
        bad. Deliberately different from every other failure branch, and
        the reason a caller cannot infer "reachable" from ``running``
        alone."""
        safe_get = Mock(return_value=_unparseable_response())
        result = _call_status({"llm.provider": "ollama"}, safe_get)

        assert result["running"] is True
        assert "invalid" in result["message"].lower()
        assert "model_count" not in result


class TestCheckOllamaModelResidualBranches:
    _SETTINGS = {"llm.provider": "ollama", "llm.model": "llama3"}

    def test_old_api_format_bare_list_still_finds_the_model(self):
        safe_get = Mock(
            return_value=_old_format_response(["llama3", "codellama"])
        )
        result = _call_model(
            {"llm.provider": "ollama", "llm.model": "codellama"}, safe_get
        )

        assert result["available"] is True
        assert result["model"] == "codellama"

    def test_non_200_from_the_tags_api_reports_unavailable(self):
        safe_get = Mock(return_value=_status_response(500))
        result = _call_model(self._SETTINGS, safe_get)

        assert result["available"] is False
        assert result["model"] == "llama3"
        assert result["status_code"] == 500

    def test_unparseable_body_reports_a_json_parse_error(self):
        safe_get = Mock(return_value=_unparseable_response())
        result = _call_model(self._SETTINGS, safe_get)

        assert result["available"] is False
        assert result["error_type"] == "json_parse_error"

    def test_timeout_reports_a_timeout_not_a_connection_error(self):
        """The two network failures are distinct ``error_type`` values;
        the frontend renders different guidance for each."""
        safe_get = Mock(side_effect=requests.exceptions.Timeout("timed out"))
        result = _call_model(self._SETTINGS, safe_get)

        assert result["available"] is False
        assert result["error_type"] == "timeout"
        assert result["model"] == "llama3"

    def test_empty_model_list_says_no_models_not_model_missing(self):
        """An empty registry and a missing model are different operator
        problems ("nothing pulled" vs. "not among the N you have") and
        carry different messages."""
        resp = Mock()
        resp.status_code = 200
        resp.json.return_value = {"models": []}
        result = _call_model(self._SETTINGS, Mock(return_value=resp))

        assert result["available"] is False
        assert "no models" in result["message"].lower()

    def test_model_missing_from_a_populated_list_names_the_model(self):
        resp = Mock()
        resp.status_code = 200
        resp.json.return_value = {"models": [{"name": "llama3"}]}
        result = _call_model(
            {"llm.provider": "ollama", "llm.model": "nonexistent-model"},
            Mock(return_value=resp),
        )

        assert result["available"] is False
        assert "nonexistent-model" in result["message"]
        # Available models are not disclosed on the failure path.
        assert "all_models" not in result