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.

5.1 KB
Raw Blame
"""The ``openai_endpoint`` API key stays bound to its configured origin.

``get_llm(openai_endpoint_url=...)`` overlays a per-call endpoint onto the
settings snapshot. The provider reads its key from
``llm.openai_endpoint.api_key``, which belongs to ``llm.openai_endpoint.url``.
These tests pin that the stored key only follows an override that resolves
to the same origin (scheme, host, port), and that an explicit
``openai_endpoint_api_key`` is what the client receives otherwise.

Only the langchain client class is patched; settings resolution, the
overlay, the egress gate and the provider factory run for real. Every URL is
a loopback address, so nothing leaves the machine.
"""

import pytest
from unittest.mock import patch

from langchain_core.language_models.fake_chat_models import FakeListChatModel

from local_deep_research.config.llm_config import get_llm
from local_deep_research.llm.providers.base import OPTIONAL_API_KEY_PLACEHOLDER

OPENAI_BASE_CHAT = "local_deep_research.llm.providers.openai_base.ChatOpenAI"

RESEARCH_KEY = "sk-research-endpoint-key"
GRADER_KEY = "sk-separate-grader-key"


@pytest.fixture(autouse=True, scope="module")
def _ensure_providers_registered():
    from local_deep_research.llm.providers import discover_providers

    discover_providers(force_refresh=True)


def _snapshot(research_url="http://localhost:9000/v1", **overrides):
    snap = {
        "llm.supports_max_tokens": True,
        "llm.max_tokens": 4096,
        "rate_limiting.llm_enabled": False,
        "search.tool": "searxng",
        "llm.openai_endpoint.api_key": RESEARCH_KEY,
    }
    if research_url is not None:
        snap["llm.openai_endpoint.url"] = research_url
    snap.update(overrides)
    return snap


def _client_kwargs(snapshot, **get_llm_kwargs):
    with patch(
        OPENAI_BASE_CHAT, return_value=FakeListChatModel(responses=["ok"])
    ) as mock_chat:
        get_llm(
            provider="openai_endpoint",
            model_name="judge-model",
            settings_snapshot=snapshot,
            **get_llm_kwargs,
        )
    return mock_chat.call_args.kwargs


class TestDifferentOriginWithholdsStoredKey:
    @pytest.mark.parametrize(
        "override",
        [
            "http://localhost:8000/v1",  # different port
            "http://127.0.0.1:9000/v1",  # different host text
            "https://localhost:9000/v1",  # different scheme
        ],
    )
    def test_override_on_another_origin_does_not_receive_research_key(
        self, override
    ):
        snapshot = _snapshot()
        kwargs = _client_kwargs(snapshot, openai_endpoint_url=override)

        assert kwargs["base_url"] == override
        assert kwargs["api_key"] != RESEARCH_KEY
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER
        # The caller's snapshot is left untouched.
        assert snapshot["llm.openai_endpoint.api_key"] == RESEARCH_KEY

    def test_unset_research_url_compares_against_provider_default(self):
        """With no stored URL the key belongs to the provider default."""
        kwargs = _client_kwargs(
            _snapshot(research_url=None),
            openai_endpoint_url="http://localhost:8000/v1",
        )
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER


class TestSameOriginReusesStoredKey:
    @pytest.mark.parametrize(
        ("research_url", "override"),
        [
            ("http://localhost:9000/v1", "http://localhost:9000/v1"),
            # Path and trailing slash do not change the origin.
            ("http://localhost:9000/v1", "http://localhost:9000/other/path/"),
            # Host case does not change the origin.
            ("http://localhost:9000/v1", "http://LocalHost:9000/v1"),
            # An explicit default port equals the implicit one.
            ("http://localhost/v1", "http://localhost:80/v1"),
        ],
    )
    def test_same_origin_override_reuses_research_key(
        self, research_url, override
    ):
        kwargs = _client_kwargs(
            _snapshot(research_url=research_url),
            openai_endpoint_url=override,
        )
        assert kwargs["api_key"] == RESEARCH_KEY


class TestExplicitEndpointKey:
    def test_explicit_key_is_sent_to_a_different_origin(self):
        kwargs = _client_kwargs(
            _snapshot(),
            openai_endpoint_url="http://localhost:8000/v1",
            openai_endpoint_api_key=GRADER_KEY,
        )
        assert kwargs["base_url"] == "http://localhost:8000/v1"
        assert kwargs["api_key"] == GRADER_KEY

    def test_explicit_key_wins_over_same_origin_research_key(self):
        kwargs = _client_kwargs(
            _snapshot(),
            openai_endpoint_url="http://localhost:9000/v1",
            openai_endpoint_api_key=GRADER_KEY,
        )
        assert kwargs["api_key"] == GRADER_KEY

    def test_non_string_explicit_key_is_rejected(self):
        with pytest.raises(ValueError, match="openai_endpoint_api_key"):
            get_llm(
                provider="openai_endpoint",
                model_name="judge-model",
                openai_endpoint_url="http://localhost:9000/v1",
                openai_endpoint_api_key=123,
                settings_snapshot=_snapshot(),
            )