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.

6.0 KB
Raw Blame
"""Request paths must report an invalid setting, not work around it.

A strict settings read raises ``SettingsValueError`` for an invalid
configured value. These request paths used to catch every exception and
render defaults, skip the subscription egress check, or (the root page, see
tests/web/test_root_route_failure_recovery.py) log the user out. Each now
lets the error reach the app's handler, which answers with the value-free
diagnostic, or refuses the operation naming the setting.
"""

from contextlib import nullcontext
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock, patch

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from starlette.requests import Request

from local_deep_research.settings.exceptions import SettingsValueError

SETTINGS_MANAGER = "local_deep_research.utilities.db_utils.get_settings_manager"


def _error(key="search.iterations", source="value"):
    return SettingsValueError(
        key, source=source, reason="expected a valid number value"
    )


# ---------------------------------------------------------------------------
# The app's SettingsValueError handler
# ---------------------------------------------------------------------------


@pytest.fixture(scope="module")
def handler_client():
    from local_deep_research.web.fastapi_app import (
        _register_exception_handlers,
    )

    mini = FastAPI()
    _register_exception_handlers(mini)

    @mini.get("/api/settings-boom")
    async def api_boom():
        raise _error()

    @mini.get("/page-boom")
    async def page_boom():
        raise _error(source="environment")

    @mini.get("/api/plain-value-error")
    async def plain_value_error():
        raise ValueError("internal detail")

    return TestClient(mini, raise_server_exceptions=False)


def test_api_request_gets_the_diagnostic(handler_client):
    response = handler_client.get("/api/settings-boom")

    assert response.status_code == 500
    assert response.json() == {"error": str(_error())}


def test_page_request_gets_the_diagnostic(handler_client):
    response = handler_client.get("/page-boom", headers={"accept": "text/html"})

    assert response.status_code == 500
    assert "search.iterations" in response.text
    assert "from environment" in response.text
    assert "expected a valid number value" in response.text


def test_other_value_errors_keep_the_generic_body(handler_client):
    response = handler_client.get("/api/plain-value-error")

    assert response.status_code == 500
    assert response.json() == {"error": "Server error"}


# ---------------------------------------------------------------------------
# Pages that rendered defaults
# ---------------------------------------------------------------------------


def test_library_page_does_not_render_defaults_for_an_invalid_setting():
    from local_deep_research.web.routers import library

    manager = Mock()
    manager.get_setting.side_effect = _error(
        "research_library.shared_library", "environment"
    )
    with (
        patch.object(
            library,
            "get_user_db_session",
            lambda *a, **k: nullcontext(Mock()),
        ),
        patch.object(library, "get_settings_manager", return_value=manager),
        pytest.raises(SettingsValueError) as raised,
    ):
        library.library_page(SimpleNamespace(query_params={}), "alice")

    assert raised.value.key == "research_library.shared_library"


def test_subscription_form_is_not_prefilled_with_defaults():
    from local_deep_research.web.routers import news_pages

    settings = {"iterations": 3}
    manager = Mock()
    manager.get_setting.side_effect = _error()
    with (
        patch(SETTINGS_MANAGER, return_value=manager),
        pytest.raises(SettingsValueError),
    ):
        news_pages._load_user_settings(settings, MagicMock(), "alice")

    assert settings == {"iterations": 3}


def test_edit_subscription_page_does_not_report_a_loading_error():
    from local_deep_research.web.routers import news_pages

    request = Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/news/subscriptions/s1/edit",
            "headers": [],
            "session": {"username": "alice"},
        }
    )
    with (
        patch.object(news_pages, "_load_user_settings", side_effect=_error()),
        patch(
            "local_deep_research.news.api.get_subscription",
            return_value={"id": "s1"},
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            lambda *a, **k: nullcontext(MagicMock()),
        ),
        patch.object(news_pages.templates, "TemplateResponse") as render,
        pytest.raises(SettingsValueError),
    ):
        news_pages.edit_subscription_page(request, "s1", username="alice")

    render.assert_not_called()


# ---------------------------------------------------------------------------
# News subscription egress pre-check
# ---------------------------------------------------------------------------


def test_subscription_is_refused_naming_the_invalid_setting():
    from local_deep_research.news.api import _validate_subscription_policy

    manager = Mock()
    manager.get_settings_snapshot.side_effect = _error("policy.egress_scope")
    with patch(SETTINGS_MANAGER, return_value=manager):
        reason = _validate_subscription_policy(Mock(), "alice", "arxiv", None)

    assert reason == str(_error("policy.egress_scope"))


def test_invalid_setting_is_not_reported_as_a_misconfigured_policy():
    from local_deep_research.news.api import _validate_subscription_policy

    manager = Mock()
    manager.get_settings_snapshot.return_value = {"search.tool": "arxiv"}
    with (
        patch(SETTINGS_MANAGER, return_value=manager),
        patch(
            "local_deep_research.security.egress.policy.context_from_snapshot",
            side_effect=_error("llm.provider"),
        ),
    ):
        reason = _validate_subscription_policy(Mock(), "alice", "arxiv", None)

    assert reason == str(_error("llm.provider"))