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.

5.0 KB
Raw Blame
"""``/benchmark/api/start`` and ``/start-simple`` refuse a grader with no key.

Grading errors are recorded per example, so without the pre-check a
benchmark whose grader endpoint has no matching key would run every
example's research and then fail to grade each one. The routes resolve the
grader key up front (same code as the grader) and return a 400 with a fixed
message instead.
"""

from unittest.mock import patch

import pytest

from local_deep_research.benchmarks.graders import GRADER_KEY_ERROR_MESSAGE
from tests.web.routers.test_benchmark_routes_coverage_port import (
    _clear_overrides,  # noqa: F401  (autouse fixture)
    _client,
    _csrf,
    _make_app,
    _patch_auth_and_db,
)

RESEARCH_KEY = "sk-research-gateway-key"


def _all_settings(grader_key=None):
    snapshot = {
        "llm.openai_endpoint.url": {
            "value": "https://llm-gateway.corp.example/v1",
            "ui_element": "text",
        },
        "llm.openai_endpoint.api_key": {
            "value": RESEARCH_KEY,
            "ui_element": "password",
        },
        "benchmark.evaluation.provider": {
            "value": "openai_endpoint",
            "ui_element": "select",
        },
        "benchmark.evaluation.endpoint_url": {
            "value": "https://openrouter.ai/api/v1",
            "ui_element": "text",
        },
    }
    if grader_key is not None:
        snapshot["benchmark.evaluation.api_key"] = {
            "value": grader_key,
            "ui_element": "password",
        }
    return snapshot


def _post(path, all_settings, extra_body=None):
    app = _make_app()
    with _patch_auth_and_db() as (mock_svc, mgr, _db):
        mgr.get_all_settings.return_value = all_settings
        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
            client = _client(app, isolate_rate_limit=True)
            resp = client.post(
                path,
                json={
                    "run_name": "r",
                    "datasets_config": {"simpleqa": {"count": 1}},
                    **(extra_body or {}),
                },
                headers=_csrf(client),
            )
        return resp, mock_svc


@pytest.mark.parametrize(
    "path", ("/benchmark/api/start", "/benchmark/api/start-simple")
)
def test_grader_without_a_matching_key_is_refused_before_the_run(path):
    resp, mock_svc = _post(path, _all_settings())

    assert resp.status_code == 400, resp.text
    assert resp.json()["error"] == GRADER_KEY_ERROR_MESSAGE
    assert RESEARCH_KEY not in resp.text
    mock_svc.create_benchmark_run.assert_not_called()
    mock_svc.start_benchmark.assert_not_called()


@pytest.mark.parametrize(
    "path", ("/benchmark/api/start", "/benchmark/api/start-simple")
)
def test_grader_with_its_own_key_starts(path):
    resp, mock_svc = _post(path, _all_settings(grader_key="sk-grader"))

    assert resp.status_code == 200, resp.text
    mock_svc.create_benchmark_run.assert_called_once()


PROVIDER_ONLY = {"evaluation_config": {"provider": "openai_endpoint"}}


def test_provider_only_request_config_is_checked_against_the_grader_endpoint():
    """A request-supplied evaluation_config cannot carry a URL; the grader
    uses benchmark.evaluation.endpoint_url, and the pre-check covers it."""
    resp, mock_svc = _post(
        "/benchmark/api/start", _all_settings(), PROVIDER_ONLY
    )

    assert resp.status_code == 400, resp.text
    assert resp.json()["error"] == GRADER_KEY_ERROR_MESSAGE
    mock_svc.create_benchmark_run.assert_not_called()


def test_provider_only_request_config_with_a_grader_key_starts():
    resp, mock_svc = _post(
        "/benchmark/api/start",
        _all_settings(grader_key="sk-grader"),
        PROVIDER_ONLY,
    )

    assert resp.status_code == 200, resp.text
    assert mock_svc.create_benchmark_run.call_args.kwargs[
        "evaluation_config"
    ] == {"provider": "openai_endpoint"}


@pytest.mark.parametrize(
    "provider", ["", "   ", '""', "''", '"OpenAI_Endpoint"']
)
def test_request_provider_uses_the_grader_default(provider):
    resp, mock_svc = _post(
        "/benchmark/api/start",
        _all_settings(),
        {"evaluation_config": {"provider": provider}},
    )
    assert resp.status_code == 400
    assert resp.json()["error"] == GRADER_KEY_ERROR_MESSAGE
    mock_svc.create_benchmark_run.assert_not_called()


@pytest.mark.parametrize(
    "provider", ["", "   ", '""', "''", '"OpenAI_Endpoint"']
)
def test_request_provider_is_saved_in_canonical_form(provider):
    resp, mock_svc = _post(
        "/benchmark/api/start",
        _all_settings(grader_key="synthetic-grader-key"),
        {"evaluation_config": {"provider": provider}},
    )
    assert resp.status_code == 200
    assert mock_svc.create_benchmark_run.call_args.kwargs[
        "evaluation_config"
    ] == {"provider": "openai_endpoint"}