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.

4.7 KB
Raw Blame
"""Invalid provider settings must not select anonymous or fallback behavior."""

from unittest.mock import patch

import pytest

from local_deep_research.llm.providers.implementations.custom_anthropic_endpoint import (
    CustomAnthropicEndpointProvider,
)
from local_deep_research.llm.providers.implementations.custom_openai_endpoint import (
    CustomOpenAIEndpointProvider,
)
from local_deep_research.llm.providers.implementations.llamacpp import (
    LlamaCppProvider,
)
from local_deep_research.llm.providers.implementations.lmstudio import (
    LMStudioProvider,
)
from local_deep_research.llm.providers.implementations.ollama import (
    OllamaProvider,
)
from local_deep_research.llm.providers.implementations.openai import (
    OpenAIProvider,
)
from local_deep_research.settings.exceptions import SettingsValueError


def _invalid_number_entry():
    return {"value": "not-a-number", "ui_element": "number"}


@pytest.mark.parametrize("provider", [OllamaProvider, OpenAIProvider])
@pytest.mark.parametrize("method", ["build_bearer_header", "has_api_key"])
def test_invalid_credentials_are_not_treated_as_missing(provider, method):
    snapshot = {provider.api_key_setting: _invalid_number_entry()}

    with pytest.raises(SettingsValueError, match=provider.api_key_setting):
        getattr(provider, method)(snapshot)


def _ollama_snapshot():
    return {
        "llm.ollama.url": "http://localhost:11434",
        "llm.local_context_window_size": 8192,
        "llm.supports_max_tokens": False,
        "llm.ollama.api_key": _invalid_number_entry(),
    }


def test_invalid_ollama_credentials_prevent_anonymous_client_construction():
    with patch(
        "local_deep_research.llm.providers.implementations.ollama.ChatOllama"
    ) as constructor:
        with pytest.raises(SettingsValueError, match="llm.ollama.api_key"):
            OllamaProvider.create_llm(
                model_name="test-model", settings_snapshot=_ollama_snapshot()
            )

    constructor.assert_not_called()


def test_invalid_ollama_credentials_prevent_availability_request():
    with patch(
        "local_deep_research.llm.providers.implementations.ollama.safe_get"
    ) as request:
        with pytest.raises(SettingsValueError, match="llm.ollama.api_key"):
            OllamaProvider.is_available(_ollama_snapshot())

    request.assert_not_called()


@pytest.mark.parametrize(
    "provider", [CustomAnthropicEndpointProvider, CustomOpenAIEndpointProvider]
)
@pytest.mark.parametrize("invalid_field", ["api_key_setting", "url_setting"])
def test_custom_endpoint_availability_does_not_replace_invalid_settings(
    provider,
    invalid_field,
):
    invalid_key = getattr(provider, invalid_field)
    snapshot = {
        provider.api_key_setting: None,
        provider.url_setting: "http://localhost:9090",
        invalid_key: _invalid_number_entry(),
    }

    with pytest.raises(SettingsValueError, match=invalid_key):
        provider.is_available(snapshot)


@pytest.mark.parametrize("provider", [LMStudioProvider, LlamaCppProvider])
@pytest.mark.parametrize("invalid_field", ["api_key_setting", "url_setting"])
def test_local_provider_availability_does_not_hide_invalid_settings(
    provider,
    invalid_field,
):
    invalid_key = getattr(provider, invalid_field)
    snapshot = {
        provider.api_key_setting: None,
        provider.url_setting: "http://localhost:9090/v1",
        invalid_key: _invalid_number_entry(),
    }

    with patch("local_deep_research.security.safe_get") as request:
        with pytest.raises(SettingsValueError, match=invalid_key):
            provider.is_available(snapshot)

    request.assert_not_called()


def test_get_llm_propagates_invalid_settings_from_registered_provider(
    monkeypatch,
):
    from local_deep_research.config import llm_config
    from local_deep_research.security.egress import policy
    from types import SimpleNamespace

    monkeypatch.setattr(llm_config, "is_llm_registered", lambda *a, **k: True)
    monkeypatch.setattr(
        llm_config,
        "get_llm_from_registry",
        lambda *a, **k: OllamaProvider.create_llm,
    )
    monkeypatch.setattr(
        policy, "resolve_run_primary_engine", lambda *a, **k: "test-primary"
    )
    monkeypatch.setattr(
        policy,
        "context_from_snapshot",
        lambda *a, **k: SimpleNamespace(require_local_llm=False),
    )
    with patch(
        "local_deep_research.llm.providers.implementations.ollama.ChatOllama"
    ) as constructor:
        with pytest.raises(SettingsValueError, match="llm.ollama.api_key"):
            llm_config.get_llm(
                provider="ollama",
                model_name="test-model",
                settings_snapshot=_ollama_snapshot(),
            )

    constructor.assert_not_called()