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.

68.0 KB
Raw Blame
"""Port of ``tests/benchmarks/web_api/test_benchmark_routes_coverage.py``.

That file (72 tests, deleted by the Flask->FastAPI migration) is the only
place the *bodies* of the benchmark route handlers were driven end to end:
provider branching in both start endpoints, the evaluation-config source
choice, the dataset validation, every ``except`` block, the search-metric
aggregation, and the whole ``/api/search-quality`` tier table. Nothing on the
branch replaced it -- ``test_benchmark_delete_guard.py``,
``test_benchmark_export_metadata.py``,
``test_benchmark_results_persistence_error.py`` and
``test_benchmark_defaults_fence.py`` between them cover delete, export,
persistence_error and the 8192 default, and that is all.

Translation notes (plumbing only; assertions are the originals):

* ``Flask(__name__) + register_blueprint`` -> the real FastAPI app with
  ``require_auth`` dependency-overridden, following the idiom already used by
  ``test_benchmark_defaults_fence.py``.
* ``login_required``/``db_manager.is_user_connected`` patching -> the
  dependency override; the ``session["username"] = "testuser"`` dance goes
  away with it.
* ``benchmark_routes.get_user_db_session`` / ``benchmark_routes.
  SettingsManager`` -> the same names in ``web.routers.benchmark`` **plus**
  their source modules, because ``_start_benchmark_sync`` re-imports them
  locally.
* ``render_template_with_defaults`` -> ``templates.TemplateResponse``; the
  page tests assert the rendered 200 and the ``eval_settings`` context key.
* ``resp.get_json()`` -> ``resp.json()``.
* The two start endpoints share a ``3 per minute`` limiter bucket keyed on
  the client IP, so every test that posts to them gets its own
  ``X-Forwarded-For`` (same reason ``test_benchmark_defaults_fence.py``
  does it) and fetches a session CSRF token first.
"""

import enum
from contextlib import contextmanager
from datetime import datetime, UTC
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient


TEST_USERNAME = "testuser"


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_app():
    """The real FastAPI app with ``require_auth`` overridden."""
    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: TEST_USERNAME
    return app


@pytest.fixture(autouse=True)
def _clear_overrides():
    """Don't leak the ``require_auth`` override into other modules sharing
    the process-wide 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)


_ip_counter = iter(range(1, 60000))


def _client(app, isolate_rate_limit=False):
    client = TestClient(app, raise_server_exceptions=False)
    if isolate_rate_limit:
        n = next(_ip_counter)
        client.headers["X-Forwarded-For"] = f"10.77.{n // 250 % 250}.{n % 250}"
    return client


def _csrf(client):
    resp = client.get("/auth/csrf-token")
    assert resp.status_code == 200, resp.text
    return {"X-CSRFToken": resp.json()["csrf_token"]}


def _fake_settings(overrides=None):
    """Return a dict of settings and a mock SettingsManager."""
    defaults = {
        "search.iterations": 8,
        "search.questions_per_iteration": 5,
        "search.tool": "searxng",
        "search.search_strategy": "focused_iteration",
        "llm.model": "gpt-4",
        "llm.provider": "openai_endpoint",
        "llm.temperature": 0.7,
        "llm.max_tokens": 30000,
        "llm.context_window_unrestricted": True,
        "llm.context_window_size": 128000,
        "llm.local_context_window_size": 4096,
        "llm.openai_endpoint.url": "http://localhost:8080",
        "llm.openai_endpoint.api_key": "sk-test",
        "llm.openai.api_key": "sk-openai",
        "llm.anthropic.api_key": "sk-anthropic",
        "benchmark.evaluation.provider": "openai_endpoint",
        "benchmark.evaluation.model": "anthropic/claude-3.7-sonnet",
        "benchmark.evaluation.temperature": 0,
        "benchmark.evaluation.endpoint_url": "https://openrouter.ai/api/v1",
    }
    if overrides:
        defaults.update(overrides)

    mgr = MagicMock()
    mgr.get_setting.side_effect = lambda key, default=None: defaults.get(
        key, default
    )
    return defaults, mgr


@contextmanager
def _patch_auth_and_db(settings_overrides=None):
    """Patch the DB session provider, SettingsManager and the benchmark
    service singleton so the route bodies execute for real.

    Both import sites are patched for each name: the module-level ones in
    ``web.routers.benchmark`` and the source modules that
    ``_start_benchmark_sync`` re-imports locally.
    """
    _, mgr = _fake_settings(settings_overrides)
    mock_db_session = MagicMock()

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

    settings_manager_cls = MagicMock(return_value=mgr)

    with (
        patch(
            "local_deep_research.web.routers.benchmark.get_user_db_session",
            side_effect=_session_ctx,
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=_session_ctx,
        ),
        patch(
            "local_deep_research.web.routers.benchmark.SettingsManager",
            settings_manager_cls,
        ),
        patch(
            "local_deep_research.settings.SettingsManager",
            settings_manager_cls,
        ),
        patch(
            "local_deep_research.web.routers.benchmark.benchmark_service"
        ) as mock_svc,
    ):
        mock_svc.get_result_persistence_error.return_value = None
        yield mock_svc, mgr, mock_db_session


def _make_routed_query(
    *,
    runs=None,
    avg_processing=None,
    results=None,
    search_calls=None,
    search_calls_exc=None,
):
    """Side-effect for mock_db.query that routes by model class.

    Production code queries SearchCall directly on the same session, so
    ``session.query(Model)`` must route to the right mock chain depending on
    the model class name.
    """

    def _route(model, *args):
        chain = MagicMock()
        chain.filter.return_value = chain
        chain.order_by.return_value = chain
        chain.limit.return_value = chain

        if not isinstance(model, type):
            # func.avg(...) — not a class
            chain.scalar.return_value = avg_processing
            return chain

        name = getattr(model, "__name__", "")
        if "BenchmarkRun" in name:
            chain.all.return_value = runs or []
        elif "SearchCall" in name:
            if search_calls_exc:
                raise search_calls_exc
            chain.all.return_value = search_calls or []
        else:
            # BenchmarkResult or anything else
            chain.all.return_value = results or []
        return chain

    return _route


class _FakeStatus(enum.Enum):
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELLED = "cancelled"


class _FakeDatasetType(enum.Enum):
    SIMPLEQA = "simpleqa"
    BROWSECOMP = "browsecomp"


# ---------------------------------------------------------------------------
# datasets_config boundary contract
# ---------------------------------------------------------------------------


_VALID_SEARCH_CONFIG = {
    "search_tool": "searxng",
    "search_strategy": "focused_iteration",
}

_INVALID_DATASETS_CONFIGS = (
    pytest.param([], "datasets_config must be an object", id="outer-array"),
    pytest.param(
        "simpleqa", "datasets_config must be an object", id="outer-string"
    ),
    pytest.param(1, "datasets_config must be an object", id="outer-integer"),
    pytest.param(1.5, "datasets_config must be an object", id="outer-float"),
    pytest.param(True, "datasets_config must be an object", id="outer-boolean"),
    pytest.param(None, "datasets_config must be an object", id="outer-null"),
    pytest.param(
        {"simpleqa": []},
        "Each datasets_config entry must be an object",
        id="entry-array",
    ),
    pytest.param(
        {"simpleqa": "enabled"},
        "Each datasets_config entry must be an object",
        id="entry-string",
    ),
    pytest.param(
        {"simpleqa": 1},
        "Each datasets_config entry must be an object",
        id="entry-integer",
    ),
    pytest.param(
        {"simpleqa": 1.5},
        "Each datasets_config entry must be an object",
        id="entry-float",
    ),
    pytest.param(
        {"simpleqa": True},
        "Each datasets_config entry must be an object",
        id="entry-boolean",
    ),
    pytest.param(
        {"simpleqa": None},
        "Each datasets_config entry must be an object",
        id="entry-null",
    ),
    pytest.param(
        {"simpleqa": {"count": []}},
        "Dataset counts must be non-negative integers",
        id="count-array",
    ),
    pytest.param(
        {"simpleqa": {"count": {}}},
        "Dataset counts must be non-negative integers",
        id="count-object",
    ),
    pytest.param(
        {"simpleqa": {"count": "1"}},
        "Dataset counts must be non-negative integers",
        id="count-string",
    ),
    pytest.param(
        {"simpleqa": {"count": 1.0}},
        "Dataset counts must be non-negative integers",
        id="count-integral-float",
    ),
    pytest.param(
        {"simpleqa": {"count": 1.5}},
        "Dataset counts must be non-negative integers",
        id="count-fractional-float",
    ),
    pytest.param(
        {"simpleqa": {"count": True}},
        "Dataset counts must be non-negative integers",
        id="count-true",
    ),
    pytest.param(
        {"simpleqa": {"count": False}},
        "Dataset counts must be non-negative integers",
        id="count-false",
    ),
    pytest.param(
        {"simpleqa": {"count": None}},
        "Dataset counts must be non-negative integers",
        id="count-null",
    ),
    pytest.param(
        {"simpleqa": {"count": -1}},
        "Dataset counts must be non-negative integers",
        id="count-negative",
    ),
    pytest.param(
        {"simpleqa": {"count": 1}, "browsecomp": {"count": -1}},
        "Dataset counts must be non-negative integers",
        id="positive-does-not-mask-negative",
    ),
    pytest.param(
        {"simpleqa": {"count": 1}, "browsecomp": {"count": "2"}},
        "Dataset counts must be non-negative integers",
        id="positive-does-not-mask-wrong-type",
    ),
)


@contextmanager
def _block_benchmark_start_dependencies():
    """Expose every side effect that invalid dataset input must precede."""
    with (
        patch(
            "local_deep_research.web.routers.benchmark.get_user_db_session"
        ) as router_db,
        patch(
            "local_deep_research.database.session_context.get_user_db_session"
        ) as source_db,
        patch(
            "local_deep_research.web.routers.benchmark.SettingsManager"
        ) as router_settings,
        patch(
            "local_deep_research.settings.SettingsManager"
        ) as source_settings,
        patch(
            "local_deep_research.database.session_passwords."
            "session_password_store.get_session_password"
        ) as get_password,
        patch(
            "local_deep_research.web.routers.benchmark.benchmark_service."
            "create_benchmark_run"
        ) as create_run,
        patch(
            "local_deep_research.web.routers.benchmark.benchmark_service."
            "start_benchmark"
        ) as start_run,
    ):
        yield {
            "router_db": router_db,
            "source_db": source_db,
            "router_settings": router_settings,
            "source_settings": source_settings,
            "get_password": get_password,
            "create_run": create_run,
            "start_run": start_run,
        }


class TestDatasetsConfigBoundary:
    @pytest.mark.parametrize(
        "path", ("/benchmark/api/start", "/benchmark/api/start-simple")
    )
    @pytest.mark.parametrize(
        ("datasets_config", "expected_error"), _INVALID_DATASETS_CONFIGS
    )
    def test_start_routes_reject_invalid_nested_config_before_side_effects(
        self, path, datasets_config, expected_error
    ):
        app = _make_app()
        client = _client(app, isolate_rate_limit=True)
        headers = _csrf(client)

        with _block_benchmark_start_dependencies() as blocked:
            response = client.post(
                path,
                json={"datasets_config": datasets_config},
                headers=headers,
            )

        assert response.status_code == 400, response.text
        assert response.json() == {"error": expected_error}
        for dependency in blocked.values():
            dependency.assert_not_called()

    @pytest.mark.parametrize(
        ("datasets_config", "expected_error"), _INVALID_DATASETS_CONFIGS
    )
    def test_validate_config_reports_invalid_nested_config_without_side_effects(
        self, datasets_config, expected_error
    ):
        app = _make_app()
        client = _client(app)
        headers = _csrf(client)

        with _block_benchmark_start_dependencies() as blocked:
            response = client.post(
                "/benchmark/api/validate-config",
                json={
                    "search_config": _VALID_SEARCH_CONFIG,
                    "datasets_config": datasets_config,
                },
                headers=headers,
            )

        assert response.status_code == 200, response.text
        assert response.json() == {
            "valid": False,
            "errors": [expected_error],
            "total_examples": 0,
        }
        for dependency in blocked.values():
            dependency.assert_not_called()

    @pytest.mark.parametrize(
        "path", ("/benchmark/api/start", "/benchmark/api/start-simple")
    )
    def test_start_routes_accept_and_preserve_valid_mixed_config(self, path):
        app = _make_app()
        datasets_config = {
            "metadata_only": {"seed": 7},
            "disabled": {"count": 0},
            "simpleqa": {"count": 1001, "seed": 11},
        }

        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords."
                "session_password_store.get_session_password",
                return_value="pw",
            ):
                mock_svc.create_benchmark_run.return_value = 314
                mock_svc.start_benchmark.return_value = True
                client = _client(app, isolate_rate_limit=True)
                response = client.post(
                    path,
                    json={
                        "run_name": "mixed dataset contract",
                        "datasets_config": datasets_config,
                    },
                    headers=_csrf(client),
                )

        assert response.status_code == 200, response.text
        assert response.json()["success"] is True
        assert (
            mock_svc.create_benchmark_run.call_args.kwargs["datasets_config"]
            == datasets_config
        )

    @pytest.mark.parametrize(
        ("datasets_config", "total_examples"),
        (
            (
                {"disabled": {"count": 0}, "simpleqa": {"count": 2}},
                2,
            ),
            (
                {"metadata_only": {"seed": 7}, "simpleqa": {"count": 3}},
                3,
            ),
            (
                {
                    "simpleqa": {"count": 1001},
                    "browsecomp": {"count": 2},
                },
                1003,
            ),
        ),
    )
    def test_validate_config_accepts_valid_edge_combinations(
        self, datasets_config, total_examples
    ):
        app = _make_app()
        client = _client(app)
        response = client.post(
            "/benchmark/api/validate-config",
            json={
                "search_config": _VALID_SEARCH_CONFIG,
                "datasets_config": datasets_config,
            },
            headers=_csrf(client),
        )

        assert response.status_code == 200, response.text
        assert response.json() == {
            "valid": True,
            "errors": [],
            "total_examples": total_examples,
        }


# ---------------------------------------------------------------------------
# index route
# ---------------------------------------------------------------------------


class TestIndex:
    def test_index_renders_template(self):
        app = _make_app()
        with _patch_auth_and_db():
            with patch(
                "local_deep_research.web.routers.benchmark.templates.TemplateResponse",
                return_value=MagicMock(
                    __class__=MagicMock(),
                ),
            ) as mock_render:
                # Return a real Response so Starlette can send it.
                from starlette.responses import HTMLResponse

                mock_render.return_value = HTMLResponse("<html>ok</html>")
                resp = _client(app).get("/benchmark/")
                assert resp.status_code == 200
                mock_render.assert_called_once()
                context = mock_render.call_args.kwargs["context"]
                assert "eval_settings" in context


class TestResults:
    def test_results_page(self):
        app = _make_app()
        with _patch_auth_and_db():
            from starlette.responses import HTMLResponse

            with patch(
                "local_deep_research.web.routers.benchmark.templates.TemplateResponse",
                return_value=HTMLResponse("<html>results</html>"),
            ):
                resp = _client(app).get("/benchmark/results")
                assert resp.status_code == 200


# ---------------------------------------------------------------------------
# start_benchmark route
# ---------------------------------------------------------------------------


class TestStartBenchmark:
    def _post_start(self, app, json_data, raw=None):
        client = _client(app, isolate_rate_limit=True)
        headers = _csrf(client)
        if raw is not None:
            headers["Content-Type"] = "application/json"
            return client.post(
                "/benchmark/api/start", content=raw, headers=headers
            )
        return client.post(
            "/benchmark/api/start", json=json_data, headers=headers
        )

    def test_start_no_json_body(self):
        """A malformed body is a 400, not a 500."""
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_start(app, None, raw="not json")
            assert resp.status_code == 400

    def test_start_empty_datasets(self):
        app = _make_app()
        with _patch_auth_and_db():
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                resp = self._post_start(app, {"datasets_config": {}})
                assert resp.status_code == 400

    def test_start_datasets_all_zero_count(self):
        app = _make_app()
        with _patch_auth_and_db():
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                resp = self._post_start(
                    app, {"datasets_config": {"simpleqa": {"count": 0}}}
                )
                assert resp.status_code == 400

    def test_start_success_openai_endpoint_provider(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = "pw123"
                mock_svc.create_benchmark_run.return_value = 42
                mock_svc.start_benchmark.return_value = True
                resp = self._post_start(
                    app,
                    {
                        "run_name": "test run",
                        "datasets_config": {"simpleqa": {"count": 5}},
                    },
                )
                assert resp.status_code == 200, resp.text
                data = resp.json()
                assert data["success"] is True
                assert data["benchmark_run_id"] == 42
                # openai_endpoint branch of the provider fan-out.
                search_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "search_config"
                ]
                assert (
                    search_config["openai_endpoint_url"]
                    == "http://localhost:8080"
                )
                assert "openai_endpoint_api_key" not in search_config

    def test_start_success_openai_provider(self):
        app = _make_app()
        with _patch_auth_and_db({"llm.provider": "openai"}) as (
            mock_svc,
            _mgr,
            _db,
        ):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 10
                mock_svc.start_benchmark.return_value = True
                resp = self._post_start(
                    app, {"datasets_config": {"simpleqa": {"count": 2}}}
                )
                assert resp.status_code == 200, resp.text
                search_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "search_config"
                ]
                assert search_config["openai_api_key"] == "sk-openai"

    def test_start_success_anthropic_provider(self):
        app = _make_app()
        with _patch_auth_and_db({"llm.provider": "anthropic"}) as (
            mock_svc,
            _mgr,
            _db,
        ):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 11
                mock_svc.start_benchmark.return_value = True
                resp = self._post_start(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 200, resp.text
                search_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "search_config"
                ]
                assert search_config["anthropic_api_key"] == "sk-anthropic"

    def test_start_with_evaluation_config_in_data(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 99
                mock_svc.start_benchmark.return_value = True
                resp = self._post_start(
                    app,
                    {
                        "datasets_config": {"simpleqa": {"count": 3}},
                        "evaluation_config": {
                            "provider": "openai",
                            "model_name": "gpt-4",
                        },
                    },
                )
                assert resp.status_code == 200, resp.text
                # Verify evaluation_config was passed through
                call_kwargs = mock_svc.create_benchmark_run.call_args
                assert (
                    call_kwargs.kwargs["evaluation_config"]["provider"]
                    == "openai"
                )

    def test_start_eval_provider_openai(self):
        """Evaluation provider openai branch."""
        app = _make_app()
        with _patch_auth_and_db(
            {"benchmark.evaluation.provider": "openai"}
        ) as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 50
                mock_svc.start_benchmark.return_value = True
                resp = self._post_start(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 200, resp.text
                eval_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "evaluation_config"
                ]
                assert eval_config["openai_api_key"] == "sk-openai"

    def test_start_eval_provider_anthropic(self):
        """Evaluation provider anthropic branch."""
        app = _make_app()
        with _patch_auth_and_db(
            {"benchmark.evaluation.provider": "anthropic"}
        ) as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 51
                mock_svc.start_benchmark.return_value = True
                resp = self._post_start(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 200, resp.text
                eval_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "evaluation_config"
                ]
                assert eval_config["anthropic_api_key"] == "sk-anthropic"

    def test_start_benchmark_fails(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 77
                mock_svc.start_benchmark.return_value = False
                resp = self._post_start(
                    app, {"datasets_config": {"simpleqa": {"count": 5}}}
                )
                assert resp.status_code == 500
                assert resp.json()["success"] is False

    def test_start_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.side_effect = RuntimeError("boom")
                resp = self._post_start(
                    app, {"datasets_config": {"simpleqa": {"count": 5}}}
                )
                assert resp.status_code == 500


class TestStartBenchmarkSearchConfigSnapshot:
    """From ``test_benchmark_routes.py::TestSearchConfigSnapshotsLLMSettings``
    -- the one test in that file with a real assertion: the LLM settings that
    were live at start time must be frozen into ``search_config``."""

    def test_start_benchmark_captures_llm_settings_in_search_config(self):
        app = _make_app()
        overrides = {
            "llm.provider": "openai",
            "llm.max_tokens": 50000,
            "llm.context_window_unrestricted": False,
            "llm.context_window_size": 64000,
            "llm.local_context_window_size": 8192,
        }
        with _patch_auth_and_db(overrides) as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = "pw"
                mock_svc.create_benchmark_run.return_value = "run-123"
                mock_svc.start_benchmark.return_value = True
                client = _client(app, isolate_rate_limit=True)
                resp = client.post(
                    "/benchmark/api/start",
                    json={"datasets_config": {"simpleqa": {"count": 5}}},
                    headers=_csrf(client),
                )

        assert resp.status_code == 200, resp.text
        data = resp.json()
        assert data["success"] is True
        assert data["benchmark_run_id"] == "run-123"

        search_config = mock_svc.create_benchmark_run.call_args.kwargs[
            "search_config"
        ]
        assert search_config["max_tokens"] == 50000
        assert search_config["context_window_unrestricted"] is False
        assert search_config["context_window_size"] == 64000
        assert search_config["local_context_window_size"] == 8192


# ---------------------------------------------------------------------------
# get_running_benchmark
# ---------------------------------------------------------------------------


class TestGetRunningBenchmark:
    def test_running_found(self):
        app = _make_app()
        mock_run = MagicMock()
        mock_run.id = 1
        mock_run.run_name = "Run 1"
        mock_run.total_examples = 10
        mock_run.completed_examples = 3

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.filter.return_value = mock_query
            mock_query.order_by.return_value = mock_query
            mock_query.first.return_value = mock_run

            resp = _client(app).get("/benchmark/api/running")
            assert resp.status_code == 200, resp.text
            data = resp.json()
            assert data["success"] is True
            assert data["benchmark_run_id"] == 1

    def test_no_running(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.filter.return_value = mock_query
            mock_query.order_by.return_value = mock_query
            mock_query.first.return_value = None

            resp = _client(app).get("/benchmark/api/running")
            assert resp.status_code == 200
            assert resp.json()["success"] is False

    def test_running_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = RuntimeError("db error")
            resp = _client(app).get("/benchmark/api/running")
            assert resp.status_code == 500


# ---------------------------------------------------------------------------
# get_benchmark_status
# ---------------------------------------------------------------------------


class TestGetBenchmarkStatus:
    def test_status_found(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            mock_svc.get_benchmark_status.return_value = {
                "completed_examples": 5,
                "overall_accuracy": 0.8,
                "avg_time_per_example": 12.5,
                "estimated_time_remaining": 60,
            }
            resp = _client(app).get("/benchmark/api/status/1")
            assert resp.status_code == 200, resp.text
            data = resp.json()
            assert data["success"] is True
            assert data["status"]["completed_examples"] == 5

    def test_status_not_found(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            mock_svc.get_benchmark_status.return_value = None
            resp = _client(app).get("/benchmark/api/status/999")
            assert resp.status_code == 404

    def test_status_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            mock_svc.get_benchmark_status.side_effect = RuntimeError("boom")
            resp = _client(app).get("/benchmark/api/status/1")
            assert resp.status_code == 500


# ---------------------------------------------------------------------------
# cancel_benchmark
# ---------------------------------------------------------------------------


class TestCancelBenchmark:
    @staticmethod
    def _cancel(app, run_id=1):
        client = _client(app)
        return client.post(
            f"/benchmark/api/cancel/{run_id}", headers=_csrf(client)
        )

    def test_cancel_success(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            mock_svc.cancel_benchmark.return_value = True
            resp = self._cancel(app)
            assert resp.status_code == 200, resp.text
            assert resp.json()["success"] is True

    def test_cancel_failure(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            mock_svc.cancel_benchmark.return_value = False
            resp = self._cancel(app)
            assert resp.status_code == 500

    def test_cancel_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            mock_svc.cancel_benchmark.side_effect = RuntimeError("oops")
            resp = self._cancel(app)
            assert resp.status_code == 500


# ---------------------------------------------------------------------------
# get_benchmark_history
# ---------------------------------------------------------------------------


class TestGetBenchmarkHistory:
    def _make_run(self, run_id, status_val="completed", run_name=None):
        run = MagicMock()
        run.id = run_id
        run.run_name = run_name
        run.created_at = datetime(2025, 1, 1, tzinfo=UTC)
        # Provenance fields added in migration 0014 — None mimics a pre-0014
        # row, so the existing history tests cover the back-compat path.
        run.start_time = None
        run.ldr_version = None
        run.total_examples = 10
        run.completed_examples = 8
        run.overall_accuracy = 0.75
        run.status = MagicMock()
        run.status.value = status_val
        run.search_config = {"tool": "searxng"}
        run.evaluation_config = {"provider": "openai"}
        run.datasets_config = {"simpleqa": {"count": 10}}
        return run

    def test_history_empty(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.order_by.return_value = mock_query
            mock_query.limit.return_value = mock_query
            mock_query.all.return_value = []

            resp = _client(app).get("/benchmark/api/history")
            assert resp.status_code == 200, resp.text
            assert resp.json()["runs"] == []

    def test_history_with_runs_and_avg_processing_time(self):
        app = _make_app()
        run = self._make_run(1, run_name=None)

        mock_result = MagicMock()
        mock_result.research_id = "res-1"
        mock_search_call = MagicMock()
        mock_search_call.research_id = "res-1"
        mock_search_call.results_count = 20

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(
                runs=[run],
                avg_processing=15.5,
                results=[mock_result],
                search_calls=[mock_search_call],
            )

            resp = _client(app).get("/benchmark/api/history")
            assert resp.status_code == 200, resp.text
            data = resp.json()
            assert data["success"] is True
            assert len(data["runs"]) == 1
            # run_name falls back to "Benchmark #{id}"
            assert "Benchmark #1" in data["runs"][0]["run_name"]
            assert data["runs"][0]["avg_processing_time"] == 15.5
            assert data["runs"][0]["avg_search_results"] == 20

    def test_history_does_not_count_skipped_searches_as_requests(self):
        """Searches skipped for an unavailable engine sent no request."""
        from local_deep_research.metrics.search_tracker import (
            SEARCH_STATUS_SKIPPED,
        )

        app = _make_app()
        run = self._make_run(5)
        mock_result = MagicMock()
        mock_result.research_id = "res-5"
        calls = []
        for status, count in [
            ("success", 10),
            ("error", 0),
            (SEARCH_STATUS_SKIPPED, 0),
            (SEARCH_STATUS_SKIPPED, 0),
        ]:
            call = MagicMock()
            call.research_id = "res-5"
            call.results_count = count
            call.success_status = status
            calls.append(call)

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(
                runs=[run],
                avg_processing=None,
                results=[mock_result],
                search_calls=calls,
            )

            resp = _client(app).get("/benchmark/api/history")
            assert resp.status_code == 200, resp.text
            row = resp.json()["runs"][0]
            assert row["total_search_requests"] == 2
            assert row["avg_search_results"] == 10

    def test_history_avg_processing_time_none(self):
        """Branch: avg_result is None."""
        app = _make_app()
        run = self._make_run(2, run_name="Named Run")

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(
                runs=[run],
                avg_processing=None,
            )

            resp = _client(app).get("/benchmark/api/history")
            assert resp.status_code == 200, resp.text
            assert resp.json()["runs"][0]["avg_processing_time"] is None

    def test_history_search_metrics_exception(self):
        """Exception in search metrics calculation logged as warning.

        ``results`` must be non-empty with a research_id, otherwise the route
        short-circuits on ``if research_ids:`` and never reaches the
        SearchCall query that is supposed to blow up here.
        """
        app = _make_app()
        run = self._make_run(3)
        mock_result = MagicMock()
        mock_result.research_id = "res-3"

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(
                runs=[run],
                avg_processing=10.0,
                results=[mock_result],
                search_calls_exc=RuntimeError("no tracker"),
            )

            resp = _client(app).get("/benchmark/api/history")
            assert resp.status_code == 200, resp.text
            # The failure is swallowed as a warning, not surfaced.
            assert resp.json()["runs"][0]["avg_search_results"] is None

    def test_history_avg_time_exception(self):
        """Exception in avg processing time calculation."""
        app = _make_app()
        run = self._make_run(4)

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.order_by.return_value = mock_query
            mock_query.limit.return_value = mock_query
            mock_query.all.return_value = [run]
            mock_query.filter.side_effect = RuntimeError("avg fail")

            resp = _client(app).get("/benchmark/api/history")
            # Should still return 200 with avg_processing_time=None
            assert resp.status_code == 200, resp.text
            assert resp.json()["runs"][0]["avg_processing_time"] is None

    def test_history_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = RuntimeError("db fail")
            resp = _client(app).get("/benchmark/api/history")
            assert resp.status_code == 500

    def test_history_no_research_ids(self):
        """Branch where research_ids list is empty."""
        app = _make_app()
        run = self._make_run(5)

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.order_by.return_value = mock_query
            mock_query.limit.return_value = mock_query
            mock_query.all.side_effect = [[run], []]  # runs, then results
            mock_query.filter.return_value = mock_query
            mock_query.scalar.return_value = 5.0

            resp = _client(app).get("/benchmark/api/history")
            assert resp.status_code == 200, resp.text
            assert resp.json()["runs"][0]["avg_search_results"] is None


# ---------------------------------------------------------------------------
# get_benchmark_results
# ---------------------------------------------------------------------------


class TestGetBenchmarkResults:
    def _make_result(self, example_id="ex1", research_id="r1", completed=True):
        r = MagicMock()
        r.example_id = example_id
        r.dataset_type = _FakeDatasetType.SIMPLEQA
        r.question = "What is X?"
        r.correct_answer = "42"
        r.extracted_answer = "42"
        r.response = "The answer is 42"
        r.is_correct = True
        r.confidence = 0.95
        r.grader_response = "Correct"
        r.processing_time = 10.5
        r.sources = ["source1"]
        r.research_id = research_id
        r.completed_at = datetime(2025, 1, 1, tzinfo=UTC) if completed else None
        return r

    def test_results_success(self):
        app = _make_app()
        result = self._make_result()

        mock_search_call = MagicMock()
        mock_search_call.research_id = "r1"
        mock_search_call.results_count = 15

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(
                results=[result],
                search_calls=[mock_search_call],
            )

            resp = _client(app).get("/benchmark/api/results/1")
            assert resp.status_code == 200, resp.text
            data = resp.json()
            assert data["success"] is True
            assert len(data["results"]) == 1
            assert data["results"][0]["search_result_count"] == 15

    def test_results_with_limit_param(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query()

            resp = _client(app).get("/benchmark/api/results/1?limit=5")
            assert resp.status_code == 200, resp.text

    def test_results_no_completed_at(self):
        """Result with completed_at = None."""
        app = _make_app()
        result = self._make_result(completed=False)

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(results=[result])

            resp = _client(app).get("/benchmark/api/results/1")
            assert resp.status_code == 200, resp.text
            assert resp.json()["results"][0]["completed_at"] is None

    def test_results_no_research_id(self):
        """Result with research_id = None -> search_result_count = 0."""
        app = _make_app()
        result = self._make_result(research_id=None)

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(results=[result])

            resp = _client(app).get("/benchmark/api/results/1")
            assert resp.status_code == 200, resp.text
            assert resp.json()["results"][0]["search_result_count"] == 0

    def test_results_search_tracker_exception(self):
        """Exception fetching search metrics does not break the route."""
        app = _make_app()
        result = self._make_result()

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_routed_query(
                results=[result],
                search_calls_exc=RuntimeError("no tracker"),
            )

            resp = _client(app).get("/benchmark/api/results/1")
            assert resp.status_code == 200, resp.text

    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, _mgr, mock_db):
            mock_svc.get_result_persistence_error.return_value = safe_error
            mock_db.query.side_effect = _make_routed_query(results=[])

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

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

    def test_results_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            mock_svc.sync_pending_results.side_effect = RuntimeError("fail")
            resp = _client(app).get("/benchmark/api/results/1")
            assert resp.status_code == 500


# ---------------------------------------------------------------------------
# export_benchmark_results
# ---------------------------------------------------------------------------


def _make_export_query_router(*, run=None, results=None):
    """Side-effect for mock_db.query routing by model class for /export.

    The endpoint runs two queries:
      1. session.query(BenchmarkRun).options(...).filter(...).one_or_none()
      2. session.query(BenchmarkResult).options(...).filter(...)
         .order_by(...).all()
    """

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

    return _route


def _make_run_mock(
    *, ldr_version=None, start_time=None, settings_snapshot=None
):
    run = MagicMock()
    run.ldr_version = ldr_version
    run.start_time = start_time
    run.created_at = datetime(2025, 1, 1, tzinfo=UTC)
    run.settings_snapshot = settings_snapshot
    return run


class TestExportBenchmarkResults:
    def test_export_success(self):
        app = _make_app()
        mock_result = MagicMock()
        mock_result.example_id = "ex1"
        mock_result.dataset_type = _FakeDatasetType.SIMPLEQA
        mock_result.question = "Q?"
        mock_result.correct_answer = "A"
        mock_result.extracted_answer = "A"
        mock_result.is_correct = True
        mock_result.confidence = 0.9
        mock_result.processing_time = 5.0
        mock_result.completed_at = datetime(2025, 1, 1, tzinfo=UTC)

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_export_query_router(
                run=_make_run_mock(ldr_version="1.6.10"),
                results=[mock_result],
            )

            resp = _client(app).get("/benchmark/api/results/1/export")
            assert resp.status_code == 200, resp.text
            data = resp.json()
            assert data["success"] is True
            assert len(data["results"]) == 1
            # The whole point of /export: heavy columns stay out.
            assert "full_response" not in data["results"][0]
            assert "sources" not in data["results"][0]
            assert "grader_response" not in data["results"][0]

    def test_export_no_completed_at(self):
        app = _make_app()
        mock_result = MagicMock()
        mock_result.example_id = "ex2"
        mock_result.dataset_type = _FakeDatasetType.BROWSECOMP
        mock_result.question = "Q2?"
        mock_result.correct_answer = "B"
        mock_result.extracted_answer = "B"
        mock_result.is_correct = False
        mock_result.confidence = 0.5
        mock_result.processing_time = 3.0
        mock_result.completed_at = None

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = _make_export_query_router(
                run=_make_run_mock(),
                results=[mock_result],
            )

            resp = _client(app).get("/benchmark/api/results/1/export")
            assert resp.status_code == 200, resp.text
            assert resp.json()["results"][0]["completed_at"] is None

    def test_export_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = RuntimeError("fail")
            resp = _client(app).get("/benchmark/api/results/1/export")
            assert resp.status_code == 500


# ---------------------------------------------------------------------------
# get_saved_configs
# ---------------------------------------------------------------------------


class TestGetSavedConfigs:
    def test_configs_success(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = _client(app).get("/benchmark/api/configs")
            assert resp.status_code == 200, resp.text
            data = resp.json()
            assert data["success"] is True
            assert len(data["configs"]) == 2
            assert data["configs"][0]["name"] == "Quick Test"


# ---------------------------------------------------------------------------
# start_benchmark_simple
# ---------------------------------------------------------------------------


class TestStartBenchmarkSimple:
    def _post_simple(self, app, json_data, raw=None):
        client = _client(app, isolate_rate_limit=True)
        headers = _csrf(client)
        if raw is not None:
            headers["Content-Type"] = "application/json"
            return client.post(
                "/benchmark/api/start-simple", content=raw, headers=headers
            )
        return client.post(
            "/benchmark/api/start-simple", json=json_data, headers=headers
        )

    def test_simple_no_json(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_simple(app, None, raw="bad")
            assert resp.status_code == 400

    def test_simple_empty_datasets(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_simple(app, {"datasets_config": {}})
            assert resp.status_code == 400

    def test_simple_success_openai_endpoint(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = "pw"
                mock_svc.create_benchmark_run.return_value = 100
                mock_svc.start_benchmark.return_value = True
                resp = self._post_simple(
                    app,
                    {
                        "run_name": "simple test",
                        "datasets_config": {"simpleqa": {"count": 3}},
                    },
                )
                assert resp.status_code == 200, resp.text
                assert resp.json()["success"] is True
                search_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "search_config"
                ]
                assert (
                    search_config["openai_endpoint_url"]
                    == "http://localhost:8080"
                )

    def test_simple_openai_provider(self):
        app = _make_app()
        with _patch_auth_and_db({"llm.provider": "openai"}) as (
            mock_svc,
            _mgr,
            _db,
        ):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 101
                mock_svc.start_benchmark.return_value = True
                resp = self._post_simple(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 200, resp.text
                search_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "search_config"
                ]
                assert search_config["openai_api_key"] == "sk-openai"

    def test_simple_anthropic_provider(self):
        app = _make_app()
        with _patch_auth_and_db({"llm.provider": "anthropic"}) as (
            mock_svc,
            _mgr,
            _db,
        ):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 102
                mock_svc.start_benchmark.return_value = True
                resp = self._post_simple(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 200, resp.text
                search_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "search_config"
                ]
                assert search_config["anthropic_api_key"] == "sk-anthropic"

    def test_simple_eval_openai_provider(self):
        app = _make_app()
        with _patch_auth_and_db(
            {"benchmark.evaluation.provider": "openai"}
        ) as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 103
                mock_svc.start_benchmark.return_value = True
                resp = self._post_simple(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 200, resp.text
                eval_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "evaluation_config"
                ]
                assert eval_config["openai_api_key"] == "sk-openai"

    def test_simple_eval_anthropic_provider(self):
        app = _make_app()
        with _patch_auth_and_db(
            {"benchmark.evaluation.provider": "anthropic"}
        ) as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 104
                mock_svc.start_benchmark.return_value = True
                resp = self._post_simple(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 200, resp.text
                eval_config = mock_svc.create_benchmark_run.call_args.kwargs[
                    "evaluation_config"
                ]
                assert eval_config["anthropic_api_key"] == "sk-anthropic"

    def test_simple_start_fails(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.return_value = 200
                mock_svc.start_benchmark.return_value = False
                resp = self._post_simple(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 500

    def test_simple_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (mock_svc, _mgr, _db):
            with patch(
                "local_deep_research.database.session_passwords.session_password_store"
            ) as mock_pw:
                mock_pw.get_session_password.return_value = None
                mock_svc.create_benchmark_run.side_effect = RuntimeError("boom")
                resp = self._post_simple(
                    app, {"datasets_config": {"simpleqa": {"count": 1}}}
                )
                assert resp.status_code == 500


# ---------------------------------------------------------------------------
# validate_config
# ---------------------------------------------------------------------------


class TestValidateConfig:
    def _post_validate(self, app, json_data, raw=None):
        client = _client(app)
        headers = _csrf(client)
        if raw is not None:
            headers["Content-Type"] = "application/json"
            return client.post(
                "/benchmark/api/validate-config", content=raw, headers=headers
            )
        return client.post(
            "/benchmark/api/validate-config", json=json_data, headers=headers
        )

    def test_validate_valid(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_validate(
                app,
                {
                    "search_config": {
                        "search_tool": "searxng",
                        "search_strategy": "focused_iteration",
                    },
                    "datasets_config": {"simpleqa": {"count": 10}},
                },
            )
            assert resp.status_code == 200, resp.text
            data = resp.json()
            assert data["valid"] is True
            assert data["total_examples"] == 10

    def test_validate_no_data(self):
        app = _make_app()
        with _patch_auth_and_db():
            # Send non-dict JSON (list)
            resp = self._post_validate(app, None, raw="[]")
            assert resp.status_code == 200, resp.text
            assert resp.json()["valid"] is False

    def test_validate_missing_search_tool(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_validate(
                app,
                {
                    "search_config": {"search_strategy": "focused_iteration"},
                    "datasets_config": {"simpleqa": {"count": 10}},
                },
            )
            data = resp.json()
            assert data["valid"] is False
            assert any("Search tool" in e for e in data["errors"])

    def test_validate_missing_search_strategy(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_validate(
                app,
                {
                    "search_config": {"search_tool": "searxng"},
                    "datasets_config": {"simpleqa": {"count": 10}},
                },
            )
            data = resp.json()
            assert data["valid"] is False
            assert any("strategy" in e.lower() for e in data["errors"])

    def test_validate_no_datasets(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_validate(
                app,
                {
                    "search_config": {
                        "search_tool": "searxng",
                        "search_strategy": "focused_iteration",
                    },
                    "datasets_config": {},
                },
            )
            data = resp.json()
            assert data["valid"] is False
            assert any("dataset" in e.lower() for e in data["errors"])

    def test_validate_zero_total_examples(self):
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_validate(
                app,
                {
                    "search_config": {
                        "search_tool": "searxng",
                        "search_strategy": "focused_iteration",
                    },
                    "datasets_config": {"simpleqa": {"count": 0}},
                },
            )
            data = resp.json()
            assert data["valid"] is False

    def test_validate_large_count_accepted(self):
        """Large example counts pass validation (no artificial cap, #4080)."""
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_validate(
                app,
                {
                    "search_config": {
                        "search_tool": "searxng",
                        "search_strategy": "focused_iteration",
                    },
                    "datasets_config": {"simpleqa": {"count": 1001}},
                },
            )
            data = resp.json()
            assert data["valid"] is True
            assert data["total_examples"] == 1001

    def test_validate_datasets_config_with_non_dict_values(self):
        """datasets_config with valid structure but no count key."""
        app = _make_app()
        with _patch_auth_and_db():
            resp = self._post_validate(
                app,
                {
                    "search_config": {
                        "search_tool": "searxng",
                        "search_strategy": "focused_iteration",
                    },
                    "datasets_config": {"simpleqa": {}},
                },
            )
            data = resp.json()
            # count defaults to 0, so total_examples = 0
            assert data["valid"] is False
            assert data["total_examples"] == 0


# ---------------------------------------------------------------------------
# get_search_quality
# ---------------------------------------------------------------------------


class TestGetSearchQuality:
    """Tests for /benchmark/api/search-quality.

    The route reads RateLimitEstimate rows from the user DB and maps each
    to {engine_type, total_attempts, success_rate (0-100 scale), status
    (EXCELLENT/GOOD/CAUTION/WARNING/CRITICAL)}.
    """

    @staticmethod
    def _make_estimate(engine_type, success_rate, total_attempts=100):
        est = MagicMock()
        est.engine_type = engine_type
        est.total_attempts = total_attempts
        est.success_rate = success_rate
        return est

    @staticmethod
    def _fetch(estimates):
        """Run the route with the given estimates and return (resp, json)."""
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.return_value.all.return_value = estimates
            resp = _client(app).get("/benchmark/api/search-quality")
            return resp, resp.json()

    def test_requires_authentication(self):
        """Without the auth override the route must not serve data."""
        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)
        with _patch_auth_and_db():
            resp = _client(app).get(
                "/benchmark/api/search-quality", follow_redirects=False
            )
            assert resp.status_code in (401, 302, 403)

    def test_status_tier_excellent(self):
        resp, data = self._fetch([self._make_estimate("pubmed", 0.95)])
        assert resp.status_code == 200, resp.text
        assert data["success"] is True
        assert "timestamp" in data
        row = data["search_quality"][0]
        assert row["engine_type"] == "pubmed"
        assert row["status"] == "EXCELLENT"
        assert row["success_rate"] == 95.0
        assert row["total_attempts"] == 100

    def test_status_tier_good(self):
        _resp, data = self._fetch([self._make_estimate("google", 0.90)])
        assert data["search_quality"][0]["status"] == "GOOD"
        assert data["search_quality"][0]["success_rate"] == 90.0

    def test_status_tier_caution(self):
        _resp, data = self._fetch([self._make_estimate("google", 0.75)])
        assert data["search_quality"][0]["status"] == "CAUTION"

    def test_status_tier_warning(self):
        _resp, data = self._fetch([self._make_estimate("google", 0.50)])
        assert data["search_quality"][0]["status"] == "WARNING"

    def test_status_tier_critical(self):
        _resp, data = self._fetch([self._make_estimate("google", 0.30)])
        assert data["search_quality"][0]["status"] == "CRITICAL"

    def test_success_rate_unit_conversion(self):
        # success_rate is stored as 0-1 in the DB; route multiplies by 100.
        _resp, data = self._fetch([self._make_estimate("google", 0.873)])
        assert data["search_quality"][0]["success_rate"] == 87.3

    def test_multiple_engines(self):
        resp, data = self._fetch(
            [
                self._make_estimate("bing", 0.6),
                self._make_estimate("google", 0.95),
            ]
        )
        assert resp.status_code == 200, resp.text
        assert len(data["search_quality"]) == 2
        assert {r["engine_type"] for r in data["search_quality"]} == {
            "bing",
            "google",
        }

    def test_empty_engines(self):
        resp, data = self._fetch([])
        assert resp.status_code == 200, resp.text
        assert data["success"] is True
        assert data["search_quality"] == []

    def test_shape_excludes_legacy_fields(self):
        # Regression guard: the old get_search_quality_stats shape included
        # recent_avg_results / min_recent_results / max_recent_results /
        # sample_size. Those are gone (the underlying data lived only in
        # the per-request in-memory tracker). Asserting their absence
        # protects the benchmark.html JS from accidentally depending on
        # them again.
        _resp, data = self._fetch([self._make_estimate("google", 0.95)])
        assert set(data["search_quality"][0].keys()) == {
            "engine_type",
            "total_attempts",
            "success_rate",
            "status",
        }

    def test_search_quality_exception(self):
        # The route reads RateLimitEstimate rows from the user DB (no
        # get_tracker call), so a DB error is what surfaces a 500.
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = RuntimeError("db error")
            resp = _client(app).get("/benchmark/api/search-quality")
            assert resp.status_code == 500


# ---------------------------------------------------------------------------
# delete_benchmark_run
# ---------------------------------------------------------------------------


class TestDeleteBenchmarkRun:
    @staticmethod
    def _delete(app, run_id=1):
        client = _client(app)
        return client.delete(
            f"/benchmark/api/delete/{run_id}", headers=_csrf(client)
        )

    def test_delete_success(self):
        app = _make_app()
        mock_run = MagicMock()
        mock_run.id = 1
        mock_run.status = MagicMock()
        mock_run.status.value = "completed"

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.filter.return_value = mock_query
            mock_query.first.return_value = mock_run
            mock_query.delete.return_value = None

            resp = self._delete(app)
            assert resp.status_code == 200, resp.text
            assert resp.json()["success"] is True

    def test_delete_not_found(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.filter.return_value = mock_query
            mock_query.first.return_value = None

            resp = self._delete(app, 999)
            assert resp.status_code == 404

    def test_delete_in_progress(self):
        app = _make_app()
        mock_run = MagicMock()
        mock_run.status = MagicMock()
        mock_run.status.value = "in_progress"

        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_query = MagicMock()
            mock_db.query.return_value = mock_query
            mock_query.filter.return_value = mock_query
            mock_query.first.return_value = mock_run

            resp = self._delete(app)
            assert resp.status_code == 400
            error = resp.json()["error"].lower()
            assert "running" in error or "cancel" in error

    def test_delete_exception(self):
        app = _make_app()
        with _patch_auth_and_db() as (_svc, _mgr, mock_db):
            mock_db.query.side_effect = RuntimeError("db error")
            resp = self._delete(app)
            assert resp.status_code == 500