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.6 KB
Raw Blame
"""Port fidelity: a non-integer benchmark run id must 404, not 422.

On ``origin/main`` every benchmark run-id route was declared with Flask's
integer URL converter::

    @benchmark_bp.route("/api/status/<int:benchmark_run_id>", methods=["GET"])
    @benchmark_bp.route("/api/cancel/<int:benchmark_run_id>", methods=["POST"])
    @benchmark_bp.route("/api/results/<int:benchmark_run_id>", methods=["GET"])
    @benchmark_bp.route("/api/results/<int:benchmark_run_id>/export", ...)
    @benchmark_bp.route("/api/delete/<int:benchmark_run_id>", methods=["DELETE"])

``<int:...>`` matches ``\\d+`` only, so a request such as
``/benchmark/api/status/run123`` never matched a rule: Werkzeug's map raised
NotFound and the client got a **404**. main's own suite asserted exactly that
in five places (``tests/benchmarks/web_api/test_benchmark_routes.py``:
``test_get_status_route_exists``, ``test_get_results_route_exists``,
``test_cancel_route_exists``, ``test_delete_route_exists`` and the
``?limit=`` variant). Those test files were deleted by this migration, so
nothing pins the behaviour any more.

The port (``web/routers/benchmark.py``) re-declares the parameter as a typed
FastAPI path param, ``benchmark_run_id: int``. FastAPI answers a failed path
coercion with its request-validation response instead: **422**, with a body
whose ``detail`` is a *list* of error objects.

That is a wire-contract change on five endpoints. Every one of them
otherwise speaks a ``{"success": bool, "error": str}`` envelope — which is
what the benchmark dashboard reads (``pages/benchmark.html`` does
``data.success`` / ``'... ' + data.error``, ``pages/benchmark_results.html``
likewise). A 422 carries neither key, so a client that previously rendered
"Benchmark run not found" now renders ``undefined``.

Reachability, stated honestly: the shipped dashboard always interpolates a
server-supplied integer id and guards each call with
``if (!currentBenchmarkId) return;``, so the browser UI does not hit this on
its own. The regression is against out-of-band API clients and against the
status-code contract main's tests asserted, not a live UI break.

The behaviour is live and this audit may not modify ``src/``, so the
main-parity expectations below are ``xfail(strict=True)``: they record the
contract and flip to a hard failure the moment the port is brought back into
line (or drifts further).
"""

import os
import uuid

import pytest
from fastapi.testclient import TestClient

# The app's docs_url toggle reads PYTEST_CURRENT_TEST at app-import time;
# pytest only sets that per-test, not at collection, and TestClient(app)
# below imports the app at module-collection time. Force it now (mirrors
# test_metrics_benchmark_hostile_input.py / test_full_surface_smoke.py).
os.environ.setdefault("TESTING", "1")

# Imported so this file fails loudly if a route is renamed or removed,
# rather than silently "passing" against a 404 raised for the wrong reason.
from local_deep_research.web.routers.benchmark import (  # noqa: E402
    router as benchmark_router,
)

_BENCHMARK_PATHS = {
    r.path for r in benchmark_router.routes if hasattr(r, "path")
}

# (method, path template). The template's ``{}`` is the run id.
RUN_ID_ROUTES = [
    pytest.param("GET", "/benchmark/api/status/{}", id="status"),
    pytest.param("POST", "/benchmark/api/cancel/{}", id="cancel"),
    pytest.param("GET", "/benchmark/api/results/{}", id="results"),
    pytest.param("GET", "/benchmark/api/results/{}/export", id="export"),
    pytest.param("DELETE", "/benchmark/api/delete/{}", id="delete"),
]

# Values Flask's ``<int:...>`` converter refused to match (its regex is
# ``\d+``), so main answered 404 for each.
NON_INTEGER_IDS = [
    pytest.param("run123", id="alphanumeric"),
    pytest.param("null", id="js-null-literal"),
    pytest.param("abc", id="alphabetic"),
]

# A well-formed id that no run uses, so the route's own "not found" branch
# is what answers.
MISSING_INT_ID = 999999


@pytest.fixture(scope="module")
def auth_client():
    """Authenticated TestClient for a throwaway user, CSRF header attached.

    Module-scoped so the whole file runs against one registered user
    (same pattern as test_metrics_benchmark_hostile_input.py::auth_client).
    """
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)

    user = f"test_runid_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    def _csrf():
        c.get("/auth/login")
        r = c.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = c.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Auth bootstrap broken: login returned {resp.status_code} "
            f"(expected 302): {resp.text[:300]}"
        )

    csrf_resp = c.get("/auth/csrf-token")
    if csrf_resp.status_code == 200:
        token = csrf_resp.json().get("csrf_token")
        if token:
            c.headers.update({"X-CSRFToken": token})

    yield c

    c.post("/auth/logout", follow_redirects=False)


def test_run_id_routes_under_test_still_exist():
    """Guards the premise of every case below.

    Router-relative paths already carry the router's own prefix
    (``APIRouter(prefix="/benchmark")``), so these are the full paths.
    If one of these moves, the cases below would exercise a plain
    unrouted-URL 404 and prove nothing.
    """
    assert "/benchmark/api/status/{benchmark_run_id}" in _BENCHMARK_PATHS
    assert "/benchmark/api/cancel/{benchmark_run_id}" in _BENCHMARK_PATHS
    assert "/benchmark/api/results/{benchmark_run_id}" in _BENCHMARK_PATHS
    assert (
        "/benchmark/api/results/{benchmark_run_id}/export" in _BENCHMARK_PATHS
    )
    assert "/benchmark/api/delete/{benchmark_run_id}" in _BENCHMARK_PATHS


@pytest.mark.parametrize(
    "method,template",
    [
        pytest.param("GET", "/benchmark/api/status/{}", id="status"),
        pytest.param("DELETE", "/benchmark/api/delete/{}", id="delete"),
    ],
)
def test_missing_run_answers_the_success_error_envelope(
    auth_client, method, template
):
    """The contract the non-integer cases below are measured against.

    A *well-formed* id for a run that does not exist takes the route's own
    "not found" branch, which is identical on main and on the port:
    404 with a JSON object carrying ``success: false`` and a string
    ``error``. This is a live assertion, not an xfail — it is what the
    benchmark dashboard's ``data.success`` / ``data.error`` reads.
    """
    resp = auth_client.request(
        method, template.format(MISSING_INT_ID), follow_redirects=False
    )

    assert resp.status_code == 404, (
        f"{method} {template.format(MISSING_INT_ID)} -> "
        f"{resp.status_code}: {resp.text[:200]}"
    )
    body = resp.json()
    assert isinstance(body, dict), body
    assert body.get("success") is False, body
    assert isinstance(body.get("error"), str), body


@pytest.mark.xfail(
    strict=True,
    reason=(
        "Port regression: benchmark.py declares the path parameter as "
        "`benchmark_run_id: int`, so FastAPI answers a failed coercion with "
        "its 422 request-validation response. main used Flask's "
        "`<int:benchmark_run_id>` converter, whose \\d+ regex simply did not "
        "match, producing a 404. Fix by taking the id as `str` and doing an "
        "explicit int() parse that returns the route's own 404 "
        '{"success": false, "error": ...} envelope on failure.'
    ),
)
@pytest.mark.parametrize("method,template", RUN_ID_ROUTES)
@pytest.mark.parametrize("bad_id", NON_INTEGER_IDS)
def test_non_integer_run_id_is_404_not_422(
    auth_client, method, template, bad_id
):
    """main returned 404 for a non-integer run id; the port returns 422.

    Asserts main's full contract — the status code AND the envelope — so a
    partial fix (404 with FastAPI's ``detail`` list still in the body) does
    not read as parity.
    """
    resp = auth_client.request(
        method, template.format(bad_id), follow_redirects=False
    )

    assert resp.status_code == 404, (
        f"{method} {template.format(bad_id)} -> {resp.status_code} "
        f"(main: 404): {resp.text[:200]}"
    )
    body = resp.json()
    assert isinstance(body, dict), body
    assert body.get("success") is False, body
    assert isinstance(body.get("error"), str), body