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.9 KB
Raw Blame
"""Tests that /api/results/<id> surfaces a ``persistence_error`` key when
``benchmark_service.get_result_persistence_error`` reports one.

Ported from the now-deleted Flask suite (``tests/web/...
test_benchmark_routes_coverage.py::TestGetBenchmarkResults::
test_results_surface_persistence_error``) for main commit 7ac42f43b
"fix(benchmarks): surface result persistence failures (#5256)". The fix
itself (``routers/benchmark.py::get_benchmark_results`` adding
``payload["persistence_error"]`` when the service reports one) survived
the FastAPI migration unchanged; only the Flask test harness was removed.

Follows the FastAPI TestClient pattern established in
``test_benchmark_export_metadata.py`` for the sibling ``/export`` route:
``require_auth`` is overridden and ``get_user_db_session`` /
``benchmark_service`` are patched directly on the router module.
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient


def _make_app():
    """Return the real FastAPI app with ``require_auth`` overridden so the
    route body executes as ``testuser`` without a real login/DB."""
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import require_auth

    app.dependency_overrides[require_auth] = lambda: "testuser"
    return app


@contextmanager
def _patch_auth_and_db():
    """Patch ``get_user_db_session`` (imported locally inside the handler
    from ``database.session_context``) so the route body runs against a
    mock session, and patch the ``benchmark_service`` singleton used by the
    router. Yields ``(mock_svc, mock_db)``.

    ``get_result_persistence_error`` defaults to ``None`` so callers that
    don't care about the persistence-error path get the same behavior as
    before this key existed.
    """
    mock_db = MagicMock()
    mock_svc = MagicMock()
    mock_svc.get_result_persistence_error.return_value = None

    @contextmanager
    def _session_ctx(username, *args, **kwargs):
        yield mock_db

    with (
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=_session_ctx,
        ),
        patch(
            "local_deep_research.web.routers.benchmark.benchmark_service",
            mock_svc,
        ),
    ):
        yield mock_svc, mock_db


def _make_results_query_router(*, results=None):
    """Side-effect for ``mock_db.query`` that routes by model class for the
    ``/api/results/<id>`` endpoint.

    The handler runs two queries:
      1. session.query(BenchmarkResult).filter(...).order_by(...).limit(...).all()
      2. session.query(SearchCall).filter(...).all()  (only when results
         carry a research_id)
    """

    def _route(model, *args):
        chain = MagicMock()
        chain.filter.return_value = chain
        chain.order_by.return_value = chain
        chain.limit.return_value = chain
        name = getattr(model, "__name__", "")
        if "SearchCall" in name:
            chain.all.return_value = []
        else:
            chain.all.return_value = results or []
        return chain

    return _route


@pytest.fixture(autouse=True)
def _clear_overrides():
    """Ensure the ``require_auth`` override added by ``_make_app`` doesn't
    leak into other tests sharing the module-level FastAPI app."""
    yield
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import require_auth

    app.dependency_overrides.pop(require_auth, None)


def _client(app):
    return TestClient(app, raise_server_exceptions=False)


class TestGetBenchmarkResultsPersistenceError:
    def test_results_surface_persistence_error(self):
        app = _make_app()
        safe_error = {
            "code": "database_write_failed",
            "message": "Benchmark results could not be saved.",
        }

        with _patch_auth_and_db() as (mock_svc, mock_db):
            mock_svc.get_result_persistence_error.return_value = safe_error
            mock_db.query.side_effect = _make_results_query_router(results=[])

            resp = _client(app).get("/benchmark/api/results/1")

        assert resp.status_code == 200
        body = resp.json()
        assert body["success"] is True
        assert body["results"] == []
        assert body["persistence_error"] == safe_error

    def test_results_omit_persistence_error_key_when_none(self):
        """No persistence_error key at all when the service reports none —
        keeps the payload shape unchanged for the common case."""
        app = _make_app()

        with _patch_auth_and_db() as (mock_svc, mock_db):
            mock_db.query.side_effect = _make_results_query_router(results=[])

            resp = _client(app).get("/benchmark/api/results/1")

        assert resp.status_code == 200
        body = resp.json()
        assert body["success"] is True
        assert "persistence_error" not in body