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.

12.7 KB
Raw Blame
"""``GET /api/research/{research_id}/logs`` — ``?limit`` and ``?priority``.

Ported from the Flask-era ``tests/web/routes/test_research_routes.py::
TestGetResearchLogsApi``, deleted by the FastAPI migration. Its sibling
class (``TestExportResearchLogsApi``) was already ported to
``test_export_research_logs.py``; this half was not, and nothing on the
branch replaced it.

WHAT IS AT STAKE. The handler in
``local_deep_research.web.routers.research.get_research_logs`` carries
two pieces of logic that no other test reaches:

* a ``max(1, min(limit, HISTORY_LOGS_HARD_CAP))`` clamp. Delete it and
  ``?limit=-1`` reaches SQLite as ``LIMIT -1``, which SQLite defines as
  *unbounded* — a caller asking for one row gets every row of a run that
  can hold thousands.
* the ``?priority=diagnostic`` ordering the frontend log panel always
  sends: a ``CASE`` that ranks error/critical/fatal above warning above
  milestone/success above routine rows, takes the newest ``limit`` of
  that ranking, then re-sorts the survivors oldest-first for the wire.

``tests/security/test_history_and_benchmark_limits_fastapi.py`` pins a
clamp, but on ``GET /history/logs/{id}`` — a different handler in
``web/routers/history.py`` with its own limit code. It says nothing
about this route, and nothing about diagnostic ordering anywhere.

The assertions are on the *messages that came back*, not on row counts,
so a handler that ignored ``?limit`` entirely would fail rather than
pass vacuously.

Plumbing follows ``test_export_research_logs.py``: the real FastAPI app
via ``authenticated_client``, with ``get_user_db_session`` patched to
hand back a session on a throwaway in-memory SQLite engine. FastAPI runs
sync handlers on anyio's threadpool, so the engine needs StaticPool +
``check_same_thread=False`` to be usable from a thread other than the
one that seeded it.
"""

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

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

_RR = "local_deep_research.web.routers.research"

_ENGINE_KW = {
    "connect_args": {"check_same_thread": False},
    "poolclass": StaticPool,
}


def _build_engine_with_seed(num_logs, rid="test-rid", same_timestamp=False):
    """One ``ResearchHistory`` row (``rid``) plus ``num_logs``
    ``ResearchLog`` rows messaged ``Log 0``..``Log N-1``, inserted
    oldest-first so the autoincrement ``id`` rises with the message
    index. Rows are one minute apart unless ``same_timestamp`` is set, in
    which case they all share one timestamp — used to prove the ``id``
    tie-break makes the newest-N selection deterministic.

    Returns ``(engine, session)``; the caller must ``_teardown`` both.
    """
    from local_deep_research.database.models import (
        Base,
        ResearchHistory,
        ResearchLog,
    )

    engine = create_engine("sqlite:///:memory:", **_ENGINE_KW)
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    session.add(
        ResearchHistory(
            id=rid,
            query="q",
            mode="quick",
            status="completed",
            created_at="2025-01-01T00:00:00+00:00",
        )
    )
    base_time = datetime(2025, 1, 1, tzinfo=timezone.utc)
    for i in range(num_logs):
        offset = timedelta(0) if same_timestamp else timedelta(minutes=i)
        session.add(
            ResearchLog(
                research_id=rid,
                timestamp=base_time + offset,
                message=f"Log {i}",
                module="test",
                function="test",
                line_no=i,
                level="INFO",
            )
        )
    session.commit()
    return engine, session


def _patch_session(session):
    @contextmanager
    def fake_db_session(*_args, **_kwargs):
        yield session

    return patch(f"{_RR}.get_user_db_session", side_effect=fake_db_session)


def _teardown(engine, session):
    session.close()
    engine.dispose()


def _messages(resp):
    return [row["message"] for row in resp.get_json()]


def _get_logs(authenticated_client, session, query=""):
    with _patch_session(session):
        return authenticated_client.get(f"/api/research/test-rid/logs{query}")


def _set_levels(session, **by_index):
    """Overwrite the ``level`` of seeded rows by their message index."""
    from local_deep_research.database.models import ResearchLog

    rows = session.query(ResearchLog).order_by(ResearchLog.id).all()
    for idx, level in by_index.items():
        rows[int(idx)].level = level
    session.commit()


# ---------------------------------------------------------------------------
# Authentication and the not-found short circuit
# ---------------------------------------------------------------------------


def test_requires_authentication(client):
    """Should require authentication."""
    resp = client.get("/api/research/test-id/logs", follow_redirects=False)
    assert resp.status_code == 401, resp.status_code


def test_missing_research_is_a_404(authenticated_client):
    """A non-existent research id must 404, not answer an empty list."""
    from local_deep_research.database.models import Base

    engine = create_engine("sqlite:///:memory:", **_ENGINE_KW)
    Base.metadata.create_all(engine)
    empty_session = sessionmaker(bind=engine)()
    try:
        resp = _get_logs(authenticated_client, empty_session)
        assert resp.status_code == 404, resp.status_code
    finally:
        _teardown(engine, empty_session)


# ---------------------------------------------------------------------------
# ?limit
# ---------------------------------------------------------------------------


def test_no_limit_returns_all_logs_oldest_first(authenticated_client):
    """Omitting ?limit preserves the public contract: every row, asc."""
    engine, session = _build_engine_with_seed(10)
    try:
        resp = _get_logs(authenticated_client, session)
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == [f"Log {i}" for i in range(10)]
    finally:
        _teardown(engine, session)


def test_limit_returns_newest_n_oldest_first(authenticated_client):
    """?limit=N returns the newest N rows, still oldest-first."""
    engine, session = _build_engine_with_seed(10)
    try:
        resp = _get_logs(authenticated_client, session, "?limit=3")
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == ["Log 7", "Log 8", "Log 9"]
    finally:
        _teardown(engine, session)


def test_limit_is_clamped_to_at_least_one(authenticated_client):
    """?limit=0 clamps up to 1 -> just the single newest row."""
    engine, session = _build_engine_with_seed(10)
    try:
        resp = _get_logs(authenticated_client, session, "?limit=0")
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == ["Log 9"]
    finally:
        _teardown(engine, session)


def test_negative_limit_clamps_to_one(authenticated_client):
    """?limit=-5 clamps to 1 — NOT SQLite's ``LIMIT -1`` (= unbounded).

    The clamp runs before ``.limit()``, so a negative value can never
    reach SQL as a no-op limit. Delete the ``max(1, ...)`` and this test
    sees all ten rows.
    """
    engine, session = _build_engine_with_seed(10)
    try:
        resp = _get_logs(authenticated_client, session, "?limit=-5")
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == ["Log 9"]
    finally:
        _teardown(engine, session)


def test_malformed_limit_falls_back_to_all_logs(authenticated_client):
    """A non-integer ?limit is treated as absent, preserving the
    return-all contract rather than erroring. (Flask got this from
    ``request.args.get(type=int)``; the FastAPI handler reproduces it
    with an explicit try/except around ``int()``.)"""
    engine, session = _build_engine_with_seed(10)
    try:
        resp = _get_logs(authenticated_client, session, "?limit=abc")
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == [f"Log {i}" for i in range(10)]
    finally:
        _teardown(engine, session)


def test_limit_above_hard_cap_is_clamped(authenticated_client):
    """?limit above HISTORY_LOGS_HARD_CAP is clamped to the cap. The cap
    is patched to a small value so the clamp is observable without
    seeding thousands of rows."""
    engine, session = _build_engine_with_seed(10)
    try:
        with patch(f"{_RR}.HISTORY_LOGS_HARD_CAP", 2):
            resp = _get_logs(authenticated_client, session, "?limit=999999")
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == ["Log 8", "Log 9"]
    finally:
        _teardown(engine, session)


def test_tie_break_on_equal_timestamps_is_deterministic(authenticated_client):
    """When rows share a timestamp, ``id`` tie-breaks so ?limit selects
    the highest-id (most recently inserted) rows deterministically —
    without the secondary key the surviving rows at the boundary are
    SQL-undefined. Log i has id i+1, so newest-3-by-id is Log 7/8/9,
    oldest-first."""
    engine, session = _build_engine_with_seed(10, same_timestamp=True)
    try:
        resp = _get_logs(authenticated_client, session, "?limit=3")
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == ["Log 7", "Log 8", "Log 9"]
    finally:
        _teardown(engine, session)


# ---------------------------------------------------------------------------
# ?priority=diagnostic
# ---------------------------------------------------------------------------


def test_diagnostic_priority_preserves_old_warnings_and_errors(
    authenticated_client,
):
    """The log-panel window keeps diagnostics even when they predate the
    newest routine rows, then returns the selected rows oldest-first."""
    engine, session = _build_engine_with_seed(10)
    try:
        _set_levels(session, **{"0": "WARNING", "1": "ERROR"})
        resp = _get_logs(
            authenticated_client, session, "?limit=4&priority=diagnostic"
        )
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == ["Log 0", "Log 1", "Log 8", "Log 9"]
    finally:
        _teardown(engine, session)


def test_diagnostic_priority_milestones(authenticated_client):
    """Milestone logs are prioritized above routine logs."""
    engine, session = _build_engine_with_seed(10)
    try:
        _set_levels(session, **{"0": "MILESTONE"})
        resp = _get_logs(
            authenticated_client, session, "?limit=3&priority=diagnostic"
        )
        assert resp.status_code == 200, resp.status_code
        assert _messages(resp) == ["Log 0", "Log 8", "Log 9"]
    finally:
        _teardown(engine, session)


def test_diagnostic_priority_loguru_tiers(authenticated_client):
    """Loguru aliases share the frontend's diagnostic priority tiers:
    CRITICAL/FATAL/ERROR rank with errors, SUCCESS with MILESTONE."""
    engine, session = _build_engine_with_seed(10)
    try:
        _set_levels(
            session,
            **{
                "0": "CRITICAL",
                "1": "FATAL",
                "2": "ERROR",
                "3": "CRITICAL",
                "4": "WARNING",
                "5": "SUCCESS",
                "6": "MILESTONE",
            },
        )

        overflow = _get_logs(
            authenticated_client, session, "?limit=3&priority=diagnostic"
        )
        assert overflow.status_code == 200, overflow.status_code
        assert _messages(overflow) == ["Log 1", "Log 2", "Log 3"]

        all_tiers = _get_logs(
            authenticated_client, session, "?limit=7&priority=diagnostic"
        )
        assert all_tiers.status_code == 200, all_tiers.status_code
        assert _messages(all_tiers) == [f"Log {i}" for i in range(7)]
    finally:
        _teardown(engine, session)


def test_diagnostic_priority_overflow(authenticated_client):
    """When a single tier (errors) alone exceeds limit, the oldest errors
    and every lower-priority category are dropped."""
    engine, session = _build_engine_with_seed(10)
    try:
        _set_levels(
            session,
            **{
                "0": "ERROR",
                "1": "ERROR",
                "2": "ERROR",
                "3": "ERROR",
                "4": "ERROR",
                "5": "WARNING",
            },
        )
        resp = _get_logs(
            authenticated_client, session, "?limit=3&priority=diagnostic"
        )
        assert resp.status_code == 200, resp.status_code
        # Errors are the highest tier and there are five of them; under
        # limit=3 only the three newest errors survive. The warning and
        # every routine row are dropped.
        assert _messages(resp) == ["Log 2", "Log 3", "Log 4"]
    finally:
        _teardown(engine, session)