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.

19.0 KB
Raw Blame
"""Tests for GET /api/research/{research_id}/logs/export.

Ported from the Flask-era ``tests/web/routes/test_research_routes.py::
TestExportResearchLogsApi`` (added by main's c969b1418, "feat(logs): add
streaming NDJSON export endpoint (#5259)") onto the FastAPI router. The
class did not exist at the merge base — the streaming endpoint itself
(``export_research_logs`` in
``local_deep_research.web.routers.research``) was already ported ahead
of this file, so only the tests needed porting.

Unlike ``/api/research/{id}/logs`` (which clamps to
``HISTORY_LOGS_HARD_CAP``), the export endpoint deliberately streams
every persisted row — see the router docstring. These tests hit the
real FastAPI app through ``authenticated_client`` (Starlette TestClient)
and patch ``get_user_db_session`` to hand back a session bound to a
throwaway in-memory SQLite engine, since the router opens a session
twice (existence check, then the streaming generator) and both calls
must see the same committed rows.
"""

import json
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"

# FastAPI runs sync route handlers (and StreamingResponse's sync generator
# body) via anyio's threadpool, so the existence check, the generator's
# first pull, and the test's own setup thread are not guaranteed to be the
# same OS thread. A plain ``sqlite:///:memory:`` connection refuses
# cross-thread use; StaticPool + check_same_thread=False keeps a single
# shared connection alive across all of them for the lifetime of the
# engine (the Flask-era version of this test didn't need this because the
# Werkzeug test client ran the whole request synchronously in one thread).
_ENGINE_KW = {
    "connect_args": {"check_same_thread": False},
    "poolclass": StaticPool,
}


def _build_engine_with_seed(num_logs, rid="test-rid"):
    """Build an in-memory SQLite engine, create the schema, seed one
    research + ``num_logs`` log rows, and return ``(engine, session)``
    bound to that engine. The caller must ``.dispose()`` the engine
    (via ``_teardown``) to release the sqlite connection.
    """
    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):
        session.add(
            ResearchLog(
                research_id=rid,
                timestamp=base_time + timedelta(minutes=i),
                message=f"Log {i}",
                module="test",
                function="test",
                line_no=i,
                level="INFO",
            )
        )
    session.commit()
    return engine, session


def _patch_session(session):
    """Patch ``get_user_db_session`` so every call (the router opens it
    twice: existence check + streaming generator) returns the same
    ``session`` via a context manager.
    """

    @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):
    """Close the session and dispose the engine so the underlying
    sqlite connection is released."""
    session.close()
    engine.dispose()


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


def test_returns_404_when_research_missing(authenticated_client):
    """A non-existent research must short-circuit before any streaming
    session is opened — otherwise the user pays for a generator that
    always emits zero rows."""
    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:
        with _patch_session(empty_session):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )
        assert resp.status_code == 404, resp.status_code
        assert resp.get_json()["error"] == "Research not found"
    finally:
        empty_session.close()
        engine.dispose()


def test_streams_all_logs_as_ndjson(authenticated_client):
    """Every persisted row must appear in the response body, oldest
    first, one JSON object per line, with the fields documented on the
    endpoint (including ``log_type`` aligned with ``/logs``).
    """
    num_logs = 7  # multiple rows to verify streaming NDJSON structure
    engine, seed = _build_engine_with_seed(num_logs)
    try:
        with _patch_session(seed):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )

        assert resp.status_code == 200, resp.status_code
        # NDJSON is one JSON object per line. The last line may lack a
        # trailing newline depending on the writer, so rstrip first.
        lines = resp.get_data(as_text=True).rstrip("\n").split("\n")
        assert len(lines) == num_logs, len(lines)

        parsed = [json.loads(line) for line in lines]
        messages = [entry["message"] for entry in parsed]
        assert messages == [f"Log {i}" for i in range(num_logs)]
        # Oldest-first: Log 0 precedes Log N-1.
        assert messages[0] == "Log 0"
        assert messages[-1] == f"Log {num_logs - 1}"

        for entry in parsed:
            assert set(entry.keys()) == {
                "id",
                "timestamp",
                "message",
                "level",
                "log_type",
                "module",
                "line_no",
            }
    finally:
        _teardown(engine, seed)


def test_bypasses_history_logs_hard_cap(authenticated_client):
    """Unlike ``/logs``, the export endpoint must not clamp to
    ``HISTORY_LOGS_HARD_CAP`` — a long run's full log history should be
    downloadable even though the on-screen panel caps its window."""
    num_logs = 25
    engine, seed = _build_engine_with_seed(num_logs)
    try:
        with (
            _patch_session(seed),
            patch(f"{_RR}.HISTORY_LOGS_HARD_CAP", 2),
        ):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )

        assert resp.status_code == 200, resp.status_code
        lines = resp.get_data(as_text=True).rstrip("\n").split("\n")
        assert len(lines) == num_logs, len(lines)
    finally:
        _teardown(engine, seed)


def test_content_disposition_and_media_type(authenticated_client):
    """The response must be a downloadable attachment served as NDJSON."""
    engine, seed = _build_engine_with_seed(3)
    try:
        with _patch_session(seed):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )
        assert resp.status_code == 200, resp.status_code
        cd = resp.headers["content-disposition"]
        assert cd.startswith("attachment; filename=")
        assert cd.endswith('.jsonl"')
        assert resp.headers["content-type"].startswith("application/x-ndjson")
    finally:
        _teardown(engine, seed)


def test_zero_logs_returns_empty_body(authenticated_client):
    """A research with zero logs is a valid state (e.g. still running).
    The endpoint must return 200 + an empty body rather than erroring on
    the empty generator."""
    engine, seed = _build_engine_with_seed(0)
    try:
        with _patch_session(seed):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )
        assert resp.status_code == 200, resp.status_code
        assert resp.get_data() == b""
    finally:
        _teardown(engine, seed)


def _build_engine_with_interleaved_seed(num_logs, rid="test-rid", group=3):
    """Seed ``num_logs`` rows whose insertion (id) order deliberately
    DIFFERS from the endpoint's documented (timestamp asc, id asc) order.

    Timestamps DESCEND as ids ascend, in runs of ``group`` consecutive
    inserts sharing one timestamp — so the export must (a) not fall back
    to id order, and (b) break timestamp ties by id. With the endpoint's
    500-row hydration batches, a tie run also straddles batch boundaries
    (e.g. snapshot positions 498-500), which is the hard case: two rows
    with an identical timestamp land in different hydration sessions and
    only the snapshot's id tie-break keeps them ordered.

    Returns ``(engine, session, records)`` where ``records`` is a list of
    ``(id, timestamp, message, line_no)`` in insertion order (ids read
    back after commit, so no assumption about autoincrement start).
    """
    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)
    rows = []
    for i in range(num_logs):
        ts = base_time + timedelta(minutes=(num_logs - 1 - i) // group)
        rows.append(
            ResearchLog(
                research_id=rid,
                timestamp=ts,
                message=f"Log {i}",
                module="test",
                function="test",
                line_no=i,
                level="INFO",
            )
        )
    session.add_all(rows)
    session.commit()
    records = [(r.id, r.timestamp, r.message, r.line_no) for r in rows]
    return engine, session, records


def test_over_500_rows_preserves_timestamp_id_order_across_batches(
    authenticated_client,
):
    """A >500-row export must emit rows in exact (timestamp asc, id asc)
    order even though hydration happens in 500-row id batches: 1203 rows
    = 3 batches (500 + 500 + 203), seeded so id order != timestamp order
    and timestamp ties straddle both batch boundaries. A regression that
    emits per-batch DB order (id-ascending within a batch), re-sorts per
    batch, or reorders across the 500/1000 boundaries changes the id
    sequence and fails here."""
    num_logs = 1203
    engine, seed, records = _build_engine_with_interleaved_seed(num_logs)
    try:
        with _patch_session(seed):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )

        assert resp.status_code == 200, resp.status_code
        body = resp.get_data()
        # Byte-level NDJSON line count: exactly one "\n" per row, body
        # newline-terminated, no blank lines injected at batch joins.
        assert body.count(b"\n") == num_logs
        assert body.endswith(b"\n")
        lines = body.decode("utf-8").split("\n")
        assert lines[-1] == "" and len(lines) == num_logs + 1
        assert all(line for line in lines[:-1])  # no empty interior lines

        parsed = [json.loads(line) for line in lines[:-1]]

        expected = sorted(records, key=lambda r: (r[1], r[0]))
        # Seed sanity: the specified order must genuinely differ from id
        # order, otherwise this test could not catch an id-order fallback.
        assert [r[0] for r in expected] != sorted(r[0] for r in records)

        assert [e["id"] for e in parsed] == [r[0] for r in expected]
        # Messages must travel with their ids (batch hydration must not
        # pair one row's id with another row's payload).
        assert [e["message"] for e in parsed] == [r[2] for r in expected]
    finally:
        _teardown(engine, seed)


def test_batched_stream_is_byte_identical_to_single_pass_serialization(
    authenticated_client,
):
    """The batched stream must be byte-for-byte what a single-pass
    serialization of the (timestamp, id)-ordered rows would produce —
    700 rows (2 hydration batches) with interleaved timestamps. Catches
    any batch-join artifact (extra/missing newlines or separators,
    per-batch re-ordering, dropped or duplicated rows) and pins the
    NDJSON field layout consumers rely on."""
    num_logs = 700
    engine, seed, records = _build_engine_with_interleaved_seed(num_logs)
    try:
        with _patch_session(seed):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )

        assert resp.status_code == 200, resp.status_code
        expected_body = "".join(
            json.dumps(
                {
                    "id": row_id,
                    "timestamp": ts.isoformat(),
                    "message": message,
                    "level": "INFO",
                    "log_type": "INFO",
                    "module": "test",
                    "line_no": line_no,
                },
                default=str,
            )
            + "\n"
            for (row_id, ts, message, line_no) in sorted(
                records, key=lambda r: (r[1], r[0])
            )
        ).encode("utf-8")
        assert resp.get_data() == expected_body
    finally:
        _teardown(engine, seed)


def test_rows_deleted_between_snapshot_and_hydration_are_skipped(
    authenticated_client,
):
    """Rows deleted after the id snapshot but before batch hydration must
    be silently skipped: every surviving row still streams, in order,
    with no error and no truncation. The endpoint opens sessions in a
    fixed sequence (1: existence check, 2: id snapshot, 3+: one per
    hydration batch), so deleting on the 3rd open lands exactly in the
    snapshot-to-hydration window. A regression that raises on a missing
    id would trip the generator's except-and-truncate path and fail the
    line-count assertion."""
    from local_deep_research.database.models import ResearchLog

    num_logs = 603  # 2 hydration batches: 500 + 103
    engine, seed = _build_engine_with_seed(num_logs)
    try:
        all_ids = [
            row_id
            for (row_id,) in seed.query(ResearchLog.id)
            .order_by(ResearchLog.id.asc())
            .all()
        ]
        assert len(all_ids) == num_logs
        # Doomed rows span both batches and both batch edges.
        doomed_positions = [0, 250, 499, 500, num_logs - 1]
        doomed_ids = {all_ids[p] for p in doomed_positions}

        calls = {"n": 0}

        @contextmanager
        def fake_db_session(*_args, **_kwargs):
            calls["n"] += 1
            if calls["n"] == 3:
                # First hydration open: snapshot (open #2) already taken.
                seed.query(ResearchLog).filter(
                    ResearchLog.id.in_(doomed_ids)
                ).delete(synchronize_session=False)
                seed.commit()
            yield seed

        with patch(f"{_RR}.get_user_db_session", side_effect=fake_db_session):
            resp = authenticated_client.get(
                "/api/research/test-rid/logs/export"
            )

        assert resp.status_code == 200, resp.status_code
        # existence + snapshot + 2 hydration batches; also proves the
        # deletion really happened inside the snapshot->hydration window.
        assert calls["n"] == 4, calls["n"]

        lines = resp.get_data(as_text=True).rstrip("\n").split("\n")
        assert len(lines) == num_logs - len(doomed_positions), len(lines)
        messages = [json.loads(line)["message"] for line in lines]
        assert messages == [
            f"Log {i}"
            for i in range(num_logs)
            if i not in set(doomed_positions)
        ]
    finally:
        _teardown(engine, seed)


def test_log_export_rate_limit_decorator_is_attached():
    """The 10/minute shared 'log_export' limit must be wired onto the
    registered route (inspected via limiter/route internals rather than
    brute-forcing 11 requests): registered as a static limit (so slowapi
    checks it at call time, after SessionMiddleware, where the per-user
    key works), with the per-user key func and the log-export exempt
    hook (which layers a HEAD carve-out on top of the api-exempt rule —
    see ``tests/web/routes/test_research_log_export_rate_limit.py``),
    and the endpoint FastAPI actually serves must be the limiter's
    wrapper — not the bare function."""
    from local_deep_research.web.dependencies.rate_limit import (
        _api_user_key,
        limiter,
    )
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.routers.research import (
        _log_export_exempt,
    )

    name = f"{_RR}.export_research_logs"
    assert name in limiter._Limiter__marked_for_limiting
    # Static limit -> _route_limits: SlowAPIMiddleware exempts it and the
    # decorator enforces at call time (see test_rate_limit_coverage.py).
    assert name in limiter._route_limits
    (lim,) = limiter._route_limits[name]  # exactly one limit
    assert lim.scope == "log_export"
    assert lim.limit.amount == 10
    assert lim.limit.GRANULARITY.name == "minute"
    assert lim.key_func is _api_user_key
    assert lim.exempt_when is _log_export_exempt

    # FastAPI auto-registers a HEAD route alongside a declared GET route
    # (same endpoint, separate APIRoute object) -- filter to GET so this
    # doesn't assert on that unrelated framework behavior.
    routes = [
        r
        for r in app.routes
        if getattr(r, "path", None) == "/api/research/{research_id}/logs/export"
        and "GET" in getattr(r, "methods", ())
    ]
    assert len(routes) == 1, routes
    endpoint = routes[0].endpoint
    # The registered endpoint must be the limiter's functools-wrapped
    # closure over THIS limiter instance, wrapping the original function.
    assert f"{endpoint.__module__}.{endpoint.__name__}" == name
    wrapped = getattr(endpoint, "__wrapped__", None)
    assert wrapped is not None and wrapped is not endpoint
    closure_cells = [
        cell.cell_contents for cell in (endpoint.__closure__ or [])
    ]
    assert any(cell is limiter for cell in closure_cells)


def test_filename_sanitization_prevents_header_breakout(authenticated_client):
    """Research IDs containing quotes or special characters must have
    those stripped from the Content-Disposition header so header
    breakout is impossible.
    """
    engine, seed = _build_engine_with_seed(0, rid='test-rid"extra')
    try:
        with _patch_session(seed):
            resp = authenticated_client.get(
                "/api/research/test-rid%22extra/logs/export"
            )
        assert resp.status_code == 200, resp.status_code
        cd = resp.headers["content-disposition"]
        assert 'filename="research_logs_test-ridextra.jsonl"' in cd
    finally:
        _teardown(engine, seed)