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.

10.4 KB
Raw Blame
"""Tests for pagination bounds enforcement (PR #1956).

Verifies:
- ``GET /history/api`` clamps limit/offset to safe ranges
- Context-overflow time series capped at 1000 for short periods

RE-PORT NOTES
-------------
The Flask originals asserted on ``inspect.getsource()`` of
``web.routes.history_routes.get_history`` and
``web.routes.context_overflow_api.get_context_overflow_metrics`` — they
grepped for the literal strings ``"min(limit, 500)"``, ``"max(1,"``,
``"max(0, offset)"`` and ``"limit(1000)"``. Both modules were deleted by the
migration (the handlers now live in ``web/routers/history.py`` and
``web/routers/context_overflow_api.py``), so the module skipped itself
whole.

Re-pointing the string-grep at the new modules would have been a one-line
change, but a source grep cannot tell a live clamp from a commented-out one
and breaks on any reformatting. These drive the REAL routes instead and
assert what reaches the database:

* the row counts prove the clamp took effect, and
* an ``Engine`` ``before_cursor_execute`` listener captures the LIMIT/OFFSET
  parameters actually bound into the statement.

The listener is load-bearing for the OFFSET case specifically: SQLite treats
a negative OFFSET as zero all by itself
(https://sqlite.org/lang_select.html), so ``?offset=-5`` returns identical
rows with and without ``max(0, offset)``. A row-based assertion there would
pass with the clamp deleted — vacuous coverage, which is worse than none.
The bound parameter is the only place the difference is observable.

``per_page``/``page`` clamping on the context-overflow endpoint (also named
in the original docstring) is already covered behaviourally by
``tests/web/routers/test_context_overflow_contract.py``
::test_pagination_params_are_clamped, and is not duplicated here.
"""

from contextlib import contextmanager
from datetime import UTC, datetime, timedelta
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

from local_deep_research.database.models import Base, ResearchHistory
from local_deep_research.database.models.metrics import TokenUsage

# get_history's own default when ?limit= is absent.
HISTORY_DEFAULT_LIMIT = 200
HISTORY_MAX_LIMIT = 500


@contextmanager
def _seeded_router_db(router_module: str, rows):
    """Patch ``router_module``'s ``get_user_db_session`` with a seeded DB.

    StaticPool shares the one in-memory connection between the seeding
    session and the handler (which runs on a TestClient worker thread), so
    the route executes its REAL SQL. Yields a list that accumulates
    ``(statement, parameters)`` for every statement the handler issues.
    """
    engine = create_engine(
        "sqlite://",
        connect_args={"check_same_thread": False},
        poolclass=StaticPool,
    )
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    seed = Session()
    seed.add_all(rows)
    seed.commit()
    seed.close()

    executed: list[tuple[str, object]] = []

    @event.listens_for(engine, "before_cursor_execute")
    def _capture(conn, cursor, statement, parameters, context, executemany):
        executed.append((statement, parameters))

    @contextmanager
    def _ctx(username=None, password=None, **kwargs):
        with Session() as session:
            yield session

    with patch(f"{router_module}.get_user_db_session", _ctx):
        yield executed


def _limit_offset_params(executed, table: str):
    """The (limit, offset) bound into the paged SELECT over ``table``."""
    for statement, parameters in executed:
        if table in statement and "LIMIT" in statement.upper():
            return tuple(parameters)[-2:]
    raise AssertionError(
        f"no paged SELECT over {table} was executed; the test never reached "
        f"the query it is about. Statements: "
        f"{[s.split(chr(10))[0][:80] for s, _ in executed]}"
    )


@pytest.fixture
def history_client(app):
    """A client authenticated by overriding the auth dependency.

    The per-user encrypted DB is replaced wholesale below, so going through
    a real register/login would only add cost.
    """
    from local_deep_research.web.dependencies.auth import require_auth

    app.dependency_overrides[require_auth] = lambda: "pagination_user"
    try:
        yield TestClient(app, raise_server_exceptions=False)
    finally:
        app.dependency_overrides.pop(require_auth, None)


def _history_rows(count: int):
    return [
        ResearchHistory(
            id=f"pag-{i:05d}",
            query=f"query {i}",
            mode="quick",
            status="completed",
            created_at=f"2024-01-01 00:00:{i % 60:02d}",
        )
        for i in range(count)
    ]


class TestHistoryRoutesPagination:
    """Verify GET /history/api enforces limit/offset bounds."""

    def test_limit_clamped_to_max_500(self, history_client):
        """An oversized ?limit must not become an unbounded read.

        Seeded with 501 rows so 500-vs-all is observable: without
        ``min(limit, 500)`` the handler would stream every row it has.
        """
        with _seeded_router_db(
            "local_deep_research.web.routers.history", _history_rows(501)
        ) as executed:
            resp = history_client.get("/history/api?limit=99999")

        assert resp.status_code == 200, resp.text[:300]
        items = resp.json()["items"]
        assert len(items) == HISTORY_MAX_LIMIT, (
            f"?limit=99999 returned {len(items)} rows; the 500 cap is gone"
        )
        assert _limit_offset_params(executed, "research_history")[0] == (
            HISTORY_MAX_LIMIT
        )

    def test_limit_below_cap_is_honoured(self, history_client):
        """Positive control: the clamp must not flatten every request to 500.

        Without this, a handler that ignored ?limit entirely and always read
        500 rows would pass the cap test above.
        """
        with _seeded_router_db(
            "local_deep_research.web.routers.history", _history_rows(501)
        ) as executed:
            resp = history_client.get("/history/api?limit=10")

        assert resp.status_code == 200, resp.text[:300]
        assert len(resp.json()["items"]) == 10
        assert _limit_offset_params(executed, "research_history")[0] == 10

    @pytest.mark.parametrize(
        "raw_limit",
        [
            pytest.param("0", id="zero"),
            # SQLite reads LIMIT -1 as "no limit", so an unclamped negative
            # value returns the ENTIRE table — the worst case of the two.
            pytest.param("-1", id="negative"),
        ],
    )
    def test_limit_clamped_to_min_1(self, history_client, raw_limit):
        with _seeded_router_db(
            "local_deep_research.web.routers.history", _history_rows(20)
        ) as executed:
            resp = history_client.get(f"/history/api?limit={raw_limit}")

        assert resp.status_code == 200, resp.text[:300]
        assert len(resp.json()["items"]) == 1, (
            f"?limit={raw_limit} returned "
            f"{len(resp.json()['items'])} rows; expected the max(1, ...) "
            f"floor to yield exactly one"
        )
        assert _limit_offset_params(executed, "research_history")[0] == 1

    def test_offset_clamped_to_min_0(self, history_client):
        """A negative ?offset must reach the database as 0.

        Asserted on the bound parameter, not on rows: SQLite silently treats
        a negative OFFSET as 0, so the row set is identical either way and a
        response-shaped assertion here would pass with the clamp removed.
        """
        with _seeded_router_db(
            "local_deep_research.web.routers.history", _history_rows(20)
        ) as executed:
            resp = history_client.get("/history/api?limit=5&offset=-5")

        assert resp.status_code == 200, resp.text[:300]
        limit, offset = _limit_offset_params(executed, "research_history")
        assert limit == 5
        assert offset == 0, (
            f"a negative ?offset reached SQLite as {offset!r}; max(0, offset) "
            f"is gone"
        )

    def test_positive_offset_still_reaches_the_query(self, history_client):
        """Positive control for the clamp above: real offsets must survive.

        A handler that hard-coded ``offset = 0`` would satisfy the negative
        case while breaking every page after the first.
        """
        with _seeded_router_db(
            "local_deep_research.web.routers.history", _history_rows(20)
        ) as executed:
            resp = history_client.get("/history/api?limit=5&offset=7")

        assert resp.status_code == 200, resp.text[:300]
        assert _limit_offset_params(executed, "research_history") == (5, 7)


def _usage_rows(count: int):
    now = datetime.now(UTC)
    return [
        TokenUsage(
            research_id="pag-res",
            # Inside the default 30d window so the short-period branch runs.
            timestamp=now - timedelta(minutes=i % 600),
            model_provider="ollama",
            model_name="llama3",
            prompt_tokens=10,
            completion_tokens=5,
            total_tokens=15,
        )
        for i in range(count)
    ]


class TestContextOverflowCap:
    """Verify the chart's time-series query has a limit cap."""

    def test_short_period_capped_at_1000(self, history_client):
        """1001 rows in the window must yield 1000 chart points, not 1001.

        Every point is serialised into the JSON response, so an uncapped
        query turns a busy user's dashboard load into an unbounded payload.
        """
        with _seeded_router_db(
            "local_deep_research.web.routers.context_overflow_api",
            _usage_rows(1001),
        ):
            resp = history_client.get("/api/context-overflow")

        assert resp.status_code == 200, resp.text[:300]
        assert len(resp.json()["chart_data"]) == 1000, (
            f"chart_data has {len(resp.json()['chart_data'])} points; the "
            f"limit(1000) cap on the time-series query is gone"
        )

    def test_below_cap_returns_every_point(self, history_client):
        """Positive control: the cap must not truncate normal volumes."""
        with _seeded_router_db(
            "local_deep_research.web.routers.context_overflow_api",
            _usage_rows(25),
        ):
            resp = history_client.get("/api/context-overflow")

        assert resp.status_code == 200, resp.text[:300]
        assert len(resp.json()["chart_data"]) == 25