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.

6.5 KB
Raw Blame
"""``/benchmark/api/start`` and ``/api/start-simple`` share ONE rate bucket.

Both routes kick off the same expensive work -- a full LLM + search loop that
``benchmark.py`` describes as costing "tens of LLM completions" -- and the
module caps that at ``3 per minute`` for exactly that reason.

Applying ``@limiter.limit(...)`` to each route separately gives each its OWN
bucket, so the pair allowed **6** starts a minute: twice the documented cap,
reachable by simply alternating the two URLs. ``metrics.py`` documents the
identical mistake being fixed for the journal endpoints, where three routes
each got their own 60/min bucket (180/min combined) and defeated an
anti-enumeration throttle.

HOW THE BUCKET IS CONSUMED WITHOUT STARTING A BENCHMARK. slowapi's decorator
wraps the endpoint, so a request is counted *before* the handler body runs but
*after* ``Depends(require_auth)`` resolves. A request carrying a non-object
JSON body is therefore counted in full and then rejected by the handler's own
``json_body_error`` 400, long before any benchmark thread is spawned. Every
request below uses that shape, so this file starts nothing and cleans up
nothing.

WHY NOT ASSERT THE SCOPE STRING. ``limiter.shared_limit(scope="benchmark_start")``
registers the same scope for both routes, and asserting that is one dict
lookup -- but it pins slowapi's bookkeeping rather than the property that
matters, and would keep passing if the decorator stopped being applied to one
of the routes. These tests fire real requests and count real 429s.
"""

import uuid

import pytest
from fastapi.testclient import TestClient

from local_deep_research.web.dependencies.rate_limit import limiter

# A parseable body that is NOT an object: consumes the rate-limit slot, then
# loses to the handler's own `isinstance(data, dict)` guard.
_NON_OBJECT_BODY = b"null"

_START = "/benchmark/api/start"
_START_SIMPLE = "/benchmark/api/start-simple"

#: Declared in benchmark.py as _BENCHMARK_START_RATE_LIMIT.
_LIMIT_PER_MINUTE = 3


@pytest.fixture(autouse=True)
def rate_limiting_enforced():
    """Force slowapi enforcement ON for each test, then restore.

    ``limiter.enabled`` is resolved from env at import time and CI runs
    with ``LDR_DISABLE_RATE_LIMITING=true``, so without this every request
    below is admitted and the bucket-sharing assertions never see a 429.
    Same pattern as ``tests/web/routers/test_auth_rate_limits.py``.
    """
    original = limiter.enabled
    limiter.enabled = True
    yield
    limiter.enabled = original
    try:
        limiter.reset()
    except Exception:
        pass


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

    Function-scoped, unlike the module-scoped fixture in the sibling
    run-id file: these tests deliberately exhaust a rate-limit bucket, and
    the limiter keys on the client, so sharing one client across tests
    would let the first test's exhausted bucket fail the second.
    """
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)
    user = f"test_bucket_{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]}"
        )
    tok = c.get("/auth/csrf-token")
    if tok.status_code == 200 and tok.json().get("csrf_token"):
        c.headers.update({"X-CSRFToken": tok.json()["csrf_token"]})
    yield c
    c.post("/auth/logout", follow_redirects=False)


def _post(client, url):
    return client.post(
        url,
        content=_NON_OBJECT_BODY,
        headers={"Content-Type": "application/json"},
    )


def test_a_non_object_body_is_rejected_without_starting_a_benchmark(
    auth_client,
):
    """Premise of every test below.

    If this body ever started a real run, the tests here would be spawning
    LLM work rather than counting rate-limit slots.
    """
    resp = _post(auth_client, _START)
    assert resp.status_code == 400, (
        f"expected the handler's json_body_error 400 for a non-object body, "
        f"got {resp.status_code}: {resp.text[:200]}"
    )
    assert resp.status_code != 429, "bucket was already exhausted"


def test_alternating_the_two_start_routes_shares_one_bucket(auth_client):
    """The regression itself.

    With per-route buckets each URL allows 3, so alternating gets 6 through
    and this never sees a 429. With one shared bucket the 4th request is
    refused no matter which of the two URLs it goes to.
    """
    urls = [_START, _START_SIMPLE] * _LIMIT_PER_MINUTE
    statuses = [_post(auth_client, u).status_code for u in urls]

    accepted = [s for s in statuses if s != 429]
    refused = [s for s in statuses if s == 429]

    assert len(accepted) == _LIMIT_PER_MINUTE, (
        f"expected exactly {_LIMIT_PER_MINUTE} requests to be admitted across "
        f"BOTH start routes, but {len(accepted)} got through "
        f"(statuses={statuses}). Each route has its own bucket, so the "
        f"documented '3 per minute' cap is really "
        f"{len(accepted)} per minute."
    )
    assert refused, "no request was rate-limited at all"


def test_the_cap_is_not_reachable_twice_by_switching_routes(auth_client):
    """Exhaust via one route, then check the *other* route is also refused.

    This is the direction that separates a shared bucket from two buckets
    that merely happen to have the same limit: a per-route bucket leaves
    /api/start-simple completely untouched after /api/start is exhausted.
    """
    for _ in range(_LIMIT_PER_MINUTE):
        _post(auth_client, _START)

    spillover = _post(auth_client, _START_SIMPLE)

    assert spillover.status_code == 429, (
        f"/api/start-simple answered {spillover.status_code} after "
        f"/api/start had already consumed the whole {_LIMIT_PER_MINUTE}/min "
        f"cap; the two routes are not sharing a bucket"
    )