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.1 KB
Raw Blame
"""Three guards on ``research.py`` that lost their only test in the port.

Ported from the Flask-era ``tests/web/routes/test_research_routes.py``
(``TestGetHistoryApi``, ``TestGetResearchDetailsApi``,
``TestGetResearchStatusApi``), deleted by the FastAPI migration. The
handlers survived the port intact; the tests did not, and no branch test
replaced them:

* ``GET /api/history`` — the ``max(1, ...)`` limit FLOOR and the
  ``max(0, ...)`` offset floor from #4560.
  ``tests/web/test_pagination_clamping_census.py`` pins the *ceiling*
  (``?limit=999999`` reaching SQL as <= 500) for this path, and pins the
  negative-limit case only for ``/library/api/documents``,
  ``/notes/api/notes`` and the chat routes. Delete ``max(1, ...)`` here
  and ``?limit=-1`` reaches SQLite as ``LIMIT -1``, which SQLite defines
  as unbounded — the whole history table, ``research_meta`` snapshots
  included, in one response.

* ``GET /api/history`` — the column PROJECTION from the same PR. A
  revert to ``query(ResearchHistory)`` is output-identical (it just
  eagerly loads every row's ``report_content`` Text body), so no
  response-level test can catch it. This is the reason the original was
  written as an identity check on the ``query()`` arguments.

* ``GET /api/research/{id}/status`` — the ``ResearchLog.id`` tie-break on
  the latest-milestone lookup. Without the secondary sort key, which
  milestone survives ``.first()`` among rows sharing the newest timestamp
  is SQL-undefined.

Plus the two 404s the branch only pins as "does not 500"
(``test_full_surface_smoke.py``): a change to "200 with an empty body"
would pass there and fails here.

Plumbing follows ``tests/web/routers/test_export_research_logs.py``: the
real FastAPI app through ``authenticated_client``, with
``get_user_db_session`` patched. The two clamp/projection tests keep the
original's MagicMock session, because the assertion is about the
arguments the handler passes to SQLAlchemy, not about rows. The
milestone test uses a real in-memory session, because a mocked
``.first()`` ignores ``order_by`` and would pass with the tie-break
deleted.
"""

from contextlib import contextmanager
from datetime import datetime, timezone
from unittest.mock import MagicMock, 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 handlers on anyio's threadpool, so a session seeded on
# the test's thread is used from another one; StaticPool +
# check_same_thread=False keeps one shared sqlite connection alive across
# both. (The Werkzeug test client ran the whole request in one thread and
# did not need this.)
_ENGINE_KW = {
    "connect_args": {"check_same_thread": False},
    "poolclass": StaticPool,
}


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 _mock_history_session():
    """A MagicMock session whose ``query(...).order_by(...).limit(...)
    .offset(...).all()`` chain — the one ``get_history`` drives — yields
    no rows, so the handler returns an empty listing and the test can
    inspect the arguments it bound."""
    session = MagicMock()
    session.query.return_value.order_by.return_value.limit.return_value.offset.return_value.all.return_value = []
    return session


# ---------------------------------------------------------------------------
# GET /api/history — pagination floors
# ---------------------------------------------------------------------------


def test_history_pagination_is_floored(authenticated_client):
    """``?limit=-1&offset=-5`` must reach SQL as ``LIMIT 1 OFFSET 0``.

    ``-1`` is the dangerous one: SQLite treats ``LIMIT -1`` as *no limit*,
    so without ``max(1, ...)`` the caller who asked for the smallest
    possible page gets the entire table (#4560).
    """
    session = _mock_history_session()
    with _patch_session(session):
        response = authenticated_client.get("/api/history?limit=-1&offset=-5")
    assert response.status_code == 200, response.status_code

    records_q = session.query.return_value.order_by.return_value
    records_q.limit.assert_called_once_with(1)
    records_q.limit.return_value.offset.assert_called_once_with(0)


def test_history_query_projects_columns_not_the_full_entity(
    authenticated_client,
):
    """``get_history`` must project only the metadata columns it
    consumes, never the full ``ResearchHistory`` entity — querying the
    entity eagerly loads the large ``report_content`` Text body into
    memory for every row. A revert is output-identical, so this is the
    only shape of test that can catch it.
    """
    from local_deep_research.database.models import ResearchHistory

    session = _mock_history_session()
    with _patch_session(session):
        response = authenticated_client.get("/api/history")
    assert response.status_code == 200, response.status_code

    # Identity checks: a SQLAlchemy column's __eq__ builds a SQL clause,
    # so `in` / `==` membership tests are unsafe here. Inspect EVERY
    # query() call (not a positional index) so the guard stays robust if
    # queries are reordered or added.
    all_selected = [
        arg for call in session.query.call_args_list for arg in call.args
    ]
    assert all_selected, (
        "the handler issued no query() call at all — this test would pass "
        "vacuously; the mock chain no longer matches get_history"
    )
    assert not any(arg is ResearchHistory for arg in all_selected), (
        "get_history must not query the full ResearchHistory entity"
    )
    assert not any(
        arg is ResearchHistory.report_content for arg in all_selected
    ), "get_history must not load the report_content body"


# ---------------------------------------------------------------------------
# GET /api/research/{id} — the not-found short circuit
# ---------------------------------------------------------------------------


def test_research_details_missing_research_is_a_404(authenticated_client):
    """A research id that does not exist must 404, not 200-with-nothing."""
    session = MagicMock()
    session.query.return_value.filter.return_value.first.return_value = None
    with _patch_session(session):
        response = authenticated_client.get("/api/research/test-id")
    assert response.status_code == 404, response.status_code


# ---------------------------------------------------------------------------
# GET /api/research/{id}/status — the latest-milestone tie-break
# ---------------------------------------------------------------------------


def test_latest_milestone_tie_breaks_equal_timestamps_by_id(
    authenticated_client,
):
    """The ``/status`` latest-milestone ``.first()`` picks the highest-id
    milestone among rows sharing the latest timestamp — deterministic,
    not the SQL-undefined arbitrary row a single-key ``order_by`` allows.

    Driven through real SQL rather than a mocked ``.first()``, which
    ignores ``order_by`` entirely.

    HONESTY NOTE, carried over from the original: on SQLite this
    behavioural half alone does NOT go red when ``ResearchLog.id.desc()``
    is deleted — with equal timestamps SQLite happens to scan in a order
    that returns the highest rowid first anyway. That is exactly the
    problem: the answer is engine-defined, not guaranteed. So the
    secondary sort key is ALSO pinned at the source below, in the style
    of ``test_research_status_error_guidance.py::TestChainShapeIsPinned``,
    which is what actually fails if it is dropped.
    """
    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)()
    try:
        session.add(
            ResearchHistory(
                id="test-rid",
                query="q",
                mode="quick",
                status="completed",
                created_at="2025-01-01T00:00:00+00:00",
            )
        )
        # Three MILESTONE rows sharing one timestamp; ids rise with
        # insertion, so "Milestone 2" is the most recently inserted.
        shared_time = datetime(2025, 1, 1, tzinfo=timezone.utc)
        for i in range(3):
            session.add(
                ResearchLog(
                    research_id="test-rid",
                    timestamp=shared_time,
                    message=f"Milestone {i}",
                    module="test",
                    function="test",
                    line_no=i,
                    level="MILESTONE",
                )
            )
        session.commit()

        with _patch_session(session):
            response = authenticated_client.get("/api/research/test-rid/status")

        assert response.status_code == 200, response.status_code
        assert response.get_json()["log_entry"]["message"] == "Milestone 2"
    finally:
        session.close()
        engine.dispose()


def test_latest_milestone_lookup_declares_the_id_tie_break():
    """Source-level half of the test above: the milestone lookup must
    order by ``ResearchLog.id.desc()`` as well as by timestamp.

    Asserted on the source because on SQLite the behavioural assertion
    cannot distinguish the two orderings (see the note above), while the
    guarantee the code is making — a deterministic "latest milestone"
    on any engine — depends entirely on the secondary key being there.
    """
    import inspect

    from local_deep_research.web.routers import research

    source = inspect.getsource(research.get_research_status)
    marker = 'filter_by(research_id=research_id, level="MILESTONE")'
    assert marker in source, (
        "the latest-milestone lookup has moved or changed shape; this "
        "guard is now pointing at nothing and must be retargeted"
    )
    tail = source[source.index(marker) : source.index(marker) + 400]
    assert "ResearchLog.id.desc()" in tail, (
        "the latest-milestone lookup lost its ResearchLog.id tie-break, so "
        "which milestone wins among rows sharing the newest timestamp is "
        "SQL-undefined"
    )