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.2 KB
Raw Blame
"""Unit tests for the reason string attached to blocked provider options.

``require_local_llm`` has three possible origins and the dropdown has to name
the right one. Naming the checkbox unconditionally (the first cut of #5662)
sent users who had set the ``private_only`` egress scope hunting for a
checkbox that was already unticked, and gave users under an operator env lock
no hint that the control was not theirs to change.

Port of ``tests/web/routes/test_provider_disabled_reason.py`` (#5749).
``_provider_disabled_reason`` and its ``_model_discovery_provider_decision``
callers were added to the deleted Flask ``web/routes/settings_routes.py``;
this ports them onto the FastAPI ``web/routers/settings.py`` module the
migration left them out of.
"""

from unittest.mock import patch

import pytest

from local_deep_research.security.egress.policy import (
    Decision,
    EgressContext,
    EgressScope,
    context_from_snapshot,
)
from local_deep_research.web.routers.settings import (
    _provider_disabled_reason,
)


def _context(scope: EgressScope) -> EgressContext:
    return EgressContext(
        scope=scope,
        primary_engine="searxng",
        require_local_llm=True,
        require_local_embeddings=False,
        username="tester",
    )


def _snapshot(scope: EgressScope) -> dict:
    """The snapshot the context above would have been built from."""
    return {"policy.egress_scope": scope.value}


@pytest.fixture
def no_env_lock():
    """Default: the setting is not locked by the server environment."""
    with patch(
        "local_deep_research.web.routers.settings.check_env_setting",
        return_value=None,
    ) as mock:
        yield mock


class TestProviderDisabledReason:
    def test_names_the_checkbox_when_the_user_set_it(self, no_env_lock):
        reason = _provider_disabled_reason(
            Decision(False, "provider_cloud_only"),
            _context(EgressScope.ADAPTIVE),
            _snapshot(EgressScope.ADAPTIVE),
        )

        assert 'Blocked by "Require Local LLM Endpoint"' in reason
        assert "egress scope" not in reason

    def test_names_the_scope_when_private_only_forced_the_flag(
        self, no_env_lock
    ):
        # context_from_snapshot() turns private_only into require_local_llm,
        # so the checkbox the old message named may well be unticked.
        reason = _provider_disabled_reason(
            Decision(False, "provider_cloud_only"),
            _context(EgressScope.PRIVATE_ONLY),
            _snapshot(EgressScope.PRIVATE_ONLY),
        )

        assert 'Blocked by egress scope "Private only"' in reason

    def test_says_so_when_the_server_locked_the_setting(self):
        with patch(
            "local_deep_research.web.routers.settings.check_env_setting",
            return_value="true",
        ):
            reason = _provider_disabled_reason(
                Decision(False, "provider_cloud_only"),
                _context(EgressScope.PRIVATE_ONLY),
                _snapshot(EgressScope.PRIVATE_ONLY),
            )

        # The env lock outranks the scope: no user-facing control changes it.
        # Asserted as the whole string, because "locked by the server" alone
        # still passes with the ``not env_locked`` guard removed -- the scope
        # branch would win and name a control the user cannot change.
        assert reason == (
            'Blocked by "Require Local LLM Endpoint" '
            "(cloud-only provider; locked by the server)"
        )
        assert "egress scope" not in reason

    @pytest.mark.parametrize(
        "policy_reason,expected",
        [
            ("provider_cloud_only", "cloud-only provider"),
            ("provider_url_unset", "no endpoint URL configured"),
            ("url_malformed", "endpoint URL is malformed"),
            ("provider_remote", "endpoint URL is not local"),
        ],
    )
    def test_explains_the_cause(self, no_env_lock, policy_reason, expected):
        reason = _provider_disabled_reason(
            Decision(False, policy_reason),
            _context(EgressScope.ADAPTIVE),
            _snapshot(EgressScope.ADAPTIVE),
        )

        assert expected in reason

    def test_unmapped_reason_falls_back_to_the_control_alone(self, no_env_lock):
        # A raw policy token like "no_snapshot" means nothing to a user, and
        # leaking one into an <option> label would be worse than saying less.
        reason = _provider_disabled_reason(
            Decision(False, "no_snapshot"),
            _context(EgressScope.ADAPTIVE),
            _snapshot(EgressScope.ADAPTIVE),
        )

        assert reason == 'Blocked by "Require Local LLM Endpoint"'
        assert "no_snapshot" not in reason


@pytest.mark.parametrize(
    "env_value",
    [
        # Both parsers agree these are falsy.
        False,
        "false",
        "0",
        "off",
        # parse_boolean() (HTML-checkbox semantics) calls all of these True,
        # but context_from_snapshot() coerces require_local_llm with the
        # policy's own {"true","1","yes","on"} vocabulary, so the policy is
        # NOT forcing locality and the message must not claim a server lock.
        "enabled",
        "T",
        "y",
        "disabled",
    ],
)
def test_false_env_lock_does_not_hide_the_private_scope_cause(env_value):
    """The reported lock must match the lock the policy actually enforces."""
    with patch(
        "local_deep_research.web.routers.settings.check_env_setting",
        return_value=env_value,
    ):
        reason = _provider_disabled_reason(
            Decision(False, "provider_cloud_only"),
            _context(EgressScope.PRIVATE_ONLY),
            _snapshot(EgressScope.PRIVATE_ONLY),
        )
    assert 'Blocked by egress scope "Private only"' in reason
    assert "locked by the server" not in reason


def test_names_adaptive_when_it_only_resolved_to_private_only():
    """The resolved scope is not the control the user can see.

    ``adaptive`` is the default scope, and ``context_from_snapshot`` resolves
    it to PRIVATE_ONLY whenever the primary search engine is local. Naming
    the resolved value sent a default-scope user hunting for a "Private only"
    selection their Egress Scope select never showed.
    """
    # Flat shape: SettingsManager.get_settings_snapshot() unwraps each
    # setting to its bare value (manager.py:1215-1219) before this function
    # ever sees it — production never hands it the nested {"value": ...}
    # form.
    snapshot = {
        "policy.egress_scope": "adaptive",
        "search.tool": "paperless",
    }
    with patch(
        "local_deep_research.settings.manager.check_env_setting",
        return_value=None,
    ):
        context = context_from_snapshot(snapshot, "paperless")
    # Production really does hand this function a resolved PRIVATE_ONLY here.
    assert context.scope == EgressScope.PRIVATE_ONLY
    assert context.require_local_llm is True

    with patch(
        "local_deep_research.web.routers.settings.check_env_setting",
        return_value=None,
    ):
        reason = _provider_disabled_reason(
            Decision(False, "provider_remote"), context, snapshot
        )

    assert reason == (
        'Blocked by egress scope "Adaptive" (resolved to Private only '
        "from your primary search engine; endpoint URL is not local)"
    )