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.

8.9 KB
Raw Blame
"""Two small clusters the research/settings ports left with no test at all:
the desktop-only kill switches, and the per-route database-failure
envelopes.

Ported from the Flask-era ``tests/web/routes/test_research_routes_deep_coverage.py``,
``..._extra_coverage.py``, ``..._config.py`` and ``test_settings_routes.py``,
all deleted by the FastAPI migration. The bulk of those files IS
superseded on the branch -- ``save_raw_config`` by
``tests/security/test_save_raw_config_blocked_keys_fastapi.py``,
``upload_pdf`` by ``tests/web/test_multipart_upload_boundary.py`` and
``test_collection_upload_http.py``, the report/export routes by
``tests/web/routes/test_report_api_contract.py`` and
``tests/web/routers/test_history_report_unit.py``, the status error
classification by ``test_research_status_error_guidance.py``, the queue
endpoints by ``tests/web/test_research_lifecycle_states.py``, and
terminate/delete/clear_history by that file plus
``tests/web/queue/test_queued_research_lifecycle_races.py``. What none of
them reach:

**The kill switches.** ``POST /open_file_location`` (research router) and
``POST /settings/open_file_location`` (settings router) exist only to
answer 403: opening a file explorer makes sense on a desktop install and
is a remote-triggered side effect on the server everyone else runs.
Nothing in ``tests/`` asserts either -- ``git grep`` for the refusal
message returns nothing -- so re-enabling one is a silent change.

**The database-failure envelopes.** Five handlers wrap their work in
``try/except`` and answer a specific ``{"status": "error", "message":
...}`` (or ``{"error": ...}``) with 500. None of those messages appears
anywhere in ``tests/``. A ``status_code == 500`` assertion alone would be
vacuous here -- the app's catch-all handler also answers 500 -- so each
test asserts the handler's own body, which the catch-all
(``{"error": "Server error"}``) does not produce.

Plus one line ``test_rag_upload_limits_source_of_truth.py`` stops short
of: that ``GET /api/config/limits`` still advertises ``allowed_mime_types``
at all. It pins ``max_file_size`` and ``max_files`` against
``FileUploadValidator``; dropping the third key from the response is
invisible to it, and the frontend's file picker is built from it.
"""

from contextlib import contextmanager
from unittest.mock import patch

import pytest

_RR = "local_deep_research.web.routers.research"


@pytest.fixture(autouse=True)
def _reset_rate_limiter_storage():
    try:
        from local_deep_research.web.dependencies.rate_limit import limiter

        storage = getattr(limiter, "_storage", None)
        if storage is not None and hasattr(storage, "reset"):
            storage.reset()
    except Exception:
        pass
    yield


def _exploding_session():
    """Make opening the per-user session raise, so the handler's own
    ``except`` arm is the only thing that can produce the response."""

    @contextmanager
    def _boom(*_args, **_kwargs):
        raise RuntimeError("database unavailable")
        yield  # pragma: no cover

    return patch(f"{_RR}.get_user_db_session", side_effect=_boom)


# ---------------------------------------------------------------------------
# Desktop-only kill switches
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "path",
    ["/open_file_location", "/settings/open_file_location"],
)
def test_open_file_location_is_always_refused(authenticated_client, path):
    """Both routes must answer 403 for an authenticated user.

    They open a file explorer on the machine running the server. On a
    desktop install that is the client's own machine; on every hosted
    install it is someone else's. The endpoints are kept only so the
    desktop UI's buttons resolve, and they are supposed to be inert --
    an authenticated 403, not a 200, is the whole contract.
    """
    response = authenticated_client.post(path, json={"path": "/tmp"})

    assert response.status_code == 403, response.text[:300]
    assert "disabled" in response.get_json()["message"].lower()


# ---------------------------------------------------------------------------
# GET /api/config/limits
# ---------------------------------------------------------------------------


def test_upload_limits_advertise_the_allowed_mime_types(
    authenticated_client,
):
    """The frontend builds its file picker from this list, so the key has
    to be there and has to contain PDF -- the endpoint's own consumer,
    ``/api/upload/pdf``, is useless otherwise."""
    from local_deep_research.security import FileUploadValidator

    response = authenticated_client.get("/api/config/limits")

    assert response.status_code == 200, response.text[:300]
    body = response.get_json()
    assert isinstance(body["allowed_mime_types"], list)
    assert "application/pdf" in body["allowed_mime_types"]
    assert set(body["allowed_mime_types"]) == set(
        FileUploadValidator.ALLOWED_MIME_TYPES
    )


# ---------------------------------------------------------------------------
# Database-failure envelopes
# ---------------------------------------------------------------------------
#
# Each case is (method, path, expected body). The bodies differ per
# handler and none of them is the catch-all's {"error": "Server error"},
# so passing here means the handler caught the failure itself.

_DB_FAILURE_CASES = [
    pytest.param(
        "post",
        "/api/terminate/res-1",
        {"status": "error", "message": "Failed to terminate research"},
        id="terminate",
    ),
    pytest.param(
        "delete",
        "/api/delete/res-1",
        {"status": "error", "message": "Failed to delete research"},
        id="delete",
    ),
    pytest.param(
        "post",
        "/api/clear_history",
        {"status": "error", "message": "Failed to process request"},
        id="clear_history",
    ),
    pytest.param(
        "get",
        "/api/history",
        {"status": "error", "message": "Failed to process request"},
        id="history",
    ),
    pytest.param(
        "get",
        "/api/research/res-1",
        {"error": "An internal error has occurred"},
        id="details",
    ),
    pytest.param(
        "get",
        "/api/research/res-1/logs",
        {"error": "An internal error has occurred"},
        id="logs",
    ),
    pytest.param(
        "get",
        "/api/research/res-1/status",
        {"error": "Error checking research status"},
        id="status",
    ),
    pytest.param(
        "get",
        "/api/report/res-1",
        {"error": "An internal error has occurred"},
        id="report",
    ),
]


@pytest.mark.parametrize("method,path,expected", _DB_FAILURE_CASES)
def test_database_failure_returns_the_handlers_own_500_envelope(
    authenticated_client, method, path, expected
):
    """A per-user database that will not open must produce the route's
    own scrubbed 500, not an unhandled exception.

    The status code alone proves nothing -- the app's catch-all answers
    500 too -- so the body is the assertion. The catch-all returns
    ``{"error": "Server error"}``; every expectation below is something
    only the handler itself writes.
    """
    with _exploding_session():
        response = getattr(authenticated_client, method)(path)

    assert response.status_code == 500, response.text[:300]
    assert response.get_json() == expected


# ---------------------------------------------------------------------------
# Queue endpoints: the QueueManager failure envelope
# ---------------------------------------------------------------------------
#
# These two do not open a per-user session -- they call QueueManager
# directly -- so the exploding-session fixture cannot reach them.


@pytest.mark.parametrize(
    "path,method_name",
    [
        ("/api/queue/status", "get_user_queue"),
        ("/api/queue/res-1/position", "get_queue_position"),
    ],
)
def test_queue_endpoints_scrub_a_queue_manager_failure(
    authenticated_client, path, method_name
):
    """A QueueManager failure must come back as the route's own scrubbed
    envelope. As above, the status code alone is not evidence -- the
    app's catch-all also answers 500, with a different body."""
    with patch(
        f"local_deep_research.web.queue.QueueManager.{method_name}",
        side_effect=RuntimeError("queue backend down"),
    ):
        response = authenticated_client.get(path)

    assert response.status_code == 500, response.text[:300]
    assert response.get_json() == {
        "status": "error",
        "message": "Failed to process request",
    }


def test_queue_status_reports_success_on_the_happy_path(
    authenticated_client,
):
    """The positive control for the pair above, and the one assertion the
    branch's lifecycle test skips: it reads ``total`` and ``queue`` but
    never checks that the envelope says ``success``."""
    response = authenticated_client.get("/api/queue/status")

    assert response.status_code == 200, response.text[:300]
    body = response.get_json()
    assert body["status"] == "success"
    assert body["total"] == len(body["queue"])