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.

22.5 KB
Raw Blame
"""Branch coverage for /history that the migration's successors leave open.

Ported from three Flask-era files the migration deleted — 75 test
functions between them:

* ``tests/web/routes/test_history_routes.py``
* ``tests/web/routes/test_history_routes_coverage.py``
* ``tests/web/routes/test_history_routes_extended.py``

Superseded and therefore NOT re-ported here:

* ``/history/report`` and ``/history/markdown`` — every 404/500/success
  branch is pinned by ``tests/web/routers/test_history_report_unit.py``.
* ``/history/log_count``'s 404 shape and success shape — same file's
  ``TestGetLogCount``.
* ``/history/logs``'s ``?limit=`` clamp (default 500, hard cap 5000,
  floor 1, non-numeric fallback) — pinned *on the bound SQL parameter*,
  which is strictly stronger than the old mock-call assertion, by
  ``tests/security/test_history_and_benchmark_limits_fastapi.py``.
* ``/history/api``'s oversized-limit clamp — pinned at the SQL layer by
  ``tests/web/test_pagination_clamping_census.py::
  TestSqlLevelLimitCensus::test_no_route_lets_an_oversized_limit_reach_sql``.
  (Note that ``test_history_pagination_params.py`` alone would NOT do:
  it asserts only ``status_code < 500``, which stays green with the
  clamp deleted.)
* Authentication on every ``/history`` route — pinned by
  ``tests/security/test_unauthenticated_reachability_census.py``.

What is recovered here is the residue: the response *shape* of
``get_history`` (including its sensitive-field exclusions and the #4560
column projection), and the per-branch progress/log reconstruction in
``get_research_status`` and ``get_research_details``, none of which any
successor executes.

``get_research_logs``'s defensive backfill is a special case — the router
source itself says so::

    # It is currently UNCOVERED: the test that exercised it,
    # test_logs_with_missing_fields_get_defaults, was removed with the
    # Flask suite and has no successor (0 hits in tests/).

That test is restored below.

The route functions are called directly with a mocked ``Request``,
matching the seam style of ``test_history_report_unit.py``.
"""

import json
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch

import pytest

HISTORY = "local_deep_research.web.routers.history"


# ---------------------------------------------------------------------------
# Seams
# ---------------------------------------------------------------------------


def _db_patch(session):
    """Patch ``get_user_db_session`` to yield ``session``."""

    @contextmanager
    def fake_db_session(*a, **kw):
        yield session

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


def _raising_db_patch(exc):
    """Patch ``get_user_db_session`` so entering the context raises."""

    @contextmanager
    def fake_db_session(*a, **kw):
        raise exc
        yield  # pragma: no cover - unreachable, keeps this a generator

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


def _join_chain_session(rows):
    """A session whose projected outerjoin/group_by/order_by/limit/offset
    query yields ``rows`` (the flat Rows ``get_history`` iterates)."""
    session = MagicMock()
    query = MagicMock()
    (
        query.outerjoin.return_value.group_by.return_value.order_by.return_value.limit.return_value.offset.return_value.all
    ).return_value = rows
    session.query.return_value = query
    return session, query


def _filter_chain_session(row):
    session = MagicMock()
    query = MagicMock()
    query.filter_by.return_value.first.return_value = row
    session.query.return_value = query
    return session


def _row(**overrides):
    """A flat history Row: the projected columns plus ``document_count``."""
    row = Mock()
    defaults = {
        "id": "test-id",
        "title": "Test Research",
        "query": "test query",
        "mode": "quick",
        "status": "completed",
        "created_at": "2024-01-01T10:00:00",
        "completed_at": "2024-01-01T10:05:00",
        "duration_seconds": 300,
        "research_meta": None,
        "chat_session_id": None,
        "document_count": 0,
    }
    defaults.update(overrides)
    for key, value in defaults.items():
        setattr(row, key, value)
    return row


def _entity(**overrides):
    """A full ResearchHistory row as ``filter_by().first()`` returns it."""
    row = Mock()
    defaults = {
        "id": "test-id",
        "query": "test query",
        "mode": "quick",
        "status": "completed",
        "created_at": "2024-01-01T10:00:00",
        "completed_at": "2024-01-01T10:05:00",
        "progress_log": "[]",
        "report_path": None,
    }
    defaults.update(overrides)
    for key, value in defaults.items():
        setattr(row, key, value)
    return row


def _request(query=None):
    req = Mock()
    req.query_params = query or {}
    return req


def _call_get_history(session, query=None):
    from local_deep_research.web.routers.history import get_history

    with _db_patch(session):
        return get_history(_request(query), username="alice")


def _body(resp):
    return json.loads(resp.body)


# ---------------------------------------------------------------------------
# get_history — column projection (#4560)
# ---------------------------------------------------------------------------


def test_get_history_projects_columns_and_never_the_report_body():
    """``get_history`` must select only metadata columns, never the full
    ``ResearchHistory`` entity — querying the entity eagerly loads the
    large ``report_content`` Text body into memory for every row in the
    page. Regression guard for #4560: a revert to
    ``query(ResearchHistory)`` is output-identical and would otherwise
    pass every other test in this file silently.

    The sibling guard on ``DatabaseReportStorage.list_reports``
    (tests/storage/test_database_storage.py) covers a different function;
    this route had its own and lost it.
    """
    from local_deep_research.database.models import ResearchHistory

    session, _ = _join_chain_session([])
    result = _call_get_history(session)

    assert result["status"] == "success"
    # Identity checks: a SQLAlchemy column's __eq__ builds a SQL clause,
    # so `in` / `==` membership tests are unsafe here.
    selected = session.query.call_args.args
    assert selected, "get_history called session.query() with no arguments"
    assert not any(arg is ResearchHistory for arg in selected), (
        "get_history must not query the full ResearchHistory entity"
    )
    assert not any(arg is ResearchHistory.report_content for arg in selected), (
        "get_history must not load the report_content body"
    )


# ---------------------------------------------------------------------------
# get_history — pagination floors
# ---------------------------------------------------------------------------


def test_get_history_floors_limit_at_one_and_offset_at_zero():
    """``max(1, ...)`` / ``max(0, ...)``.

    The oversized-limit ceiling is pinned at the SQL layer by the
    pagination census; the *floors* are not — and ``LIMIT 0`` /
    ``OFFSET -5`` are the shapes a client actually sends by accident.
    """
    session, query = _join_chain_session([])
    result = _call_get_history(session, {"limit": "0", "offset": "-5"})

    assert result["status"] == "success"
    chain = (
        query.outerjoin.return_value.group_by.return_value.order_by.return_value
    )
    chain.limit.assert_called_once_with(1)
    chain.limit.return_value.offset.assert_called_once_with(0)


def test_get_history_clamps_an_oversized_limit_to_five_hundred():
    session, query = _join_chain_session([])
    _call_get_history(session, {"limit": "9999"})

    chain = (
        query.outerjoin.return_value.group_by.return_value.order_by.return_value
    )
    chain.limit.assert_called_once_with(500)


def test_get_history_passes_an_in_range_limit_through_unmodified():
    """Positive control: a handler that ignored ``?limit=`` entirely and
    always bound the default would pass both clamp tests above."""
    session, query = _join_chain_session([])
    _call_get_history(session, {"limit": "37", "offset": "5"})

    chain = (
        query.outerjoin.return_value.group_by.return_value.order_by.return_value
    )
    chain.limit.assert_called_once_with(37)
    chain.limit.return_value.offset.assert_called_once_with(5)


# ---------------------------------------------------------------------------
# get_history — response shape and what must never appear in it
# ---------------------------------------------------------------------------


def test_get_history_item_never_carries_the_reports_internals():
    """The list response is a metadata index, not a report dump.

    ``report_path``, ``progress_log`` and the raw ``research_meta`` (which
    is where ``settings_snapshot`` — API keys, tokens, base URLs — is
    persisted) must not appear on an item.
    """
    session, _ = _join_chain_session([_row(id="test-id-123")])
    result = _call_get_history(session)

    assert len(result["items"]) == 1
    item = result["items"][0]
    assert item["id"] == "test-id-123"
    assert item["document_count"] == 0
    assert "report_path" not in item
    assert "progress_log" not in item
    assert "research_meta" not in item
    assert "settings_snapshot" not in str(item.get("metadata", {}))


def test_get_history_metadata_is_allowlisted_down_to_is_news_search():
    """``filter_research_metadata`` is an allowlist, not a denylist: a
    settings_snapshot full of secrets must not survive it, and neither
    must any other unrecognised key."""
    session, _ = _join_chain_session(
        [
            _row(
                id="test-id-news",
                research_meta={
                    "is_news_search": True,
                    "settings_snapshot": {"api_key": "sk-secret-key-12345"},
                },
            )
        ]
    )
    item = _call_get_history(session)["items"][0]

    assert item["metadata"] == {"is_news_search": True}
    rendered = str(item)
    assert "settings_snapshot" not in rendered
    assert "api_key" not in rendered
    assert "sk-secret-key-12345" not in rendered


@pytest.mark.parametrize(
    "research_meta",
    [None, {"strategy": "evidence_based"}, "not valid json {{"],
)
def test_get_history_metadata_defaults_to_is_news_search_false(research_meta):
    """Missing, unrecognised and unparseable ``research_meta`` all
    degrade to the same safe default rather than 500-ing or leaking the
    raw value through."""
    session, _ = _join_chain_session([_row(research_meta=research_meta)])
    item = _call_get_history(session)["items"][0]

    assert item["metadata"] == {"is_news_search": False}


def test_get_history_surfaces_the_chat_session_id_only_when_present():
    with_chat, without_chat = (
        _row(id="a", chat_session_id="chat-1"),
        _row(id="b", chat_session_id=None),
    )
    session, _ = _join_chain_session([with_chat, without_chat])
    items = _call_get_history(session)["items"]

    assert items[0]["metadata"]["chat_session_id"] == "chat-1"
    assert "chat_session_id" not in items[1]["metadata"]


# ---------------------------------------------------------------------------
# get_history — duration recalculation
# ---------------------------------------------------------------------------


def test_get_history_recalculates_a_null_duration_from_the_timestamps():
    session, _ = _join_chain_session(
        [
            _row(
                duration_seconds=None,
                created_at="2024-06-01T10:00:00",
                completed_at="2024-06-01T10:05:00",
            )
        ]
    )
    item = _call_get_history(session)["items"][0]

    assert item["duration_seconds"] == 300


def test_get_history_leaves_the_duration_null_when_the_timestamps_will_not_parse():
    """The recalculation is best-effort: an unparseable timestamp logs and
    leaves the field None — it must not take the whole listing down."""
    session, _ = _join_chain_session(
        [
            _row(
                duration_seconds=None,
                created_at="not-a-date",
                completed_at="also-not-a-date",
            )
        ]
    )
    item = _call_get_history(session)["items"][0]

    assert item["duration_seconds"] is None


def test_get_history_keeps_a_duration_that_is_already_set():
    session, _ = _join_chain_session([_row(duration_seconds=300)])
    assert _call_get_history(session)["items"][0]["duration_seconds"] == 300


# ---------------------------------------------------------------------------
# get_history — failure envelope
# ---------------------------------------------------------------------------


def test_get_history_answers_500_with_an_empty_item_list_when_the_db_fails():
    """A bare ``return {...}`` here would serialise as HTTP 200, the
    client's ``response.ok`` check would pass, and the UI would render an
    empty history as if the user had none. The status code is the whole
    guard."""
    from local_deep_research.web.routers.history import get_history

    with _raising_db_patch(RuntimeError("db down")):
        resp = get_history(_request(), username="alice")

    assert resp.status_code == 500
    body = _body(resp)
    assert body["status"] == "error"
    assert body["items"] == []
    assert "message" in body


# ---------------------------------------------------------------------------
# get_research_status
# ---------------------------------------------------------------------------


def _call_status(row, snapshot):
    from local_deep_research.web.routers.history import get_research_status

    with (
        _db_patch(_filter_chain_session(row)),
        patch(f"{HISTORY}.get_active_research_snapshot", return_value=snapshot),
    ):
        return get_research_status(_request(), "rid-1", username="alice")


def test_status_of_an_unknown_research_is_a_404_error_envelope():
    resp = _call_status(row=None, snapshot=None)

    assert resp.status_code == 404
    body = _body(resp)
    assert body["status"] == "error"
    assert body["error"] == "Research not found"
    assert "not found" in body["message"].lower()


def test_status_of_an_active_research_comes_from_the_live_snapshot():
    """When the registry has an entry, its progress and log win over
    anything persisted — the DB row lags a running research."""
    snapshot = {
        "progress": 42,
        "status": "in_progress",
        "log": [{"time": "10:01", "message": "step 1"}],
        "settings": None,
    }
    result = _call_status(
        _entity(status="in_progress", progress_log="[]"), snapshot
    )

    assert result["progress"] == 42
    assert result["log"] == snapshot["log"]


def test_status_of_a_completed_research_is_100_and_replays_the_stored_log():
    from local_deep_research.constants import ResearchStatus

    result = _call_status(
        _entity(
            status=ResearchStatus.COMPLETED,
            progress_log='[{"time": "10:00", "message": "done"}]',
        ),
        snapshot=None,
    )

    assert result["progress"] == 100
    assert result["log"] == [{"time": "10:00", "message": "done"}]


def test_status_of_an_inactive_unfinished_research_is_zero():
    result = _call_status(
        _entity(
            status="in_progress",
            progress_log='[{"time": "10:00", "message": "started"}]',
        ),
        snapshot=None,
    )

    assert result["progress"] == 0
    assert result["log"] == [{"time": "10:00", "message": "started"}]


@pytest.mark.parametrize(
    ("status", "expected_progress"),
    [("completed", 100), ("failed", 0)],
)
def test_an_unparseable_progress_log_degrades_to_an_empty_list(
    status, expected_progress
):
    """Both replay branches parse ``progress_log`` defensively: a corrupt
    row must still render a status page, not 500."""
    result = _call_status(
        _entity(status=status, progress_log="not valid json {{"),
        snapshot=None,
    )

    assert result["progress"] == expected_progress
    assert result["log"] == []


# ---------------------------------------------------------------------------
# get_research_details
# ---------------------------------------------------------------------------


def _call_details(row, snapshot, db_logs=(), strategy="standard", db_exc=None):
    from local_deep_research.web.routers.history import get_research_details

    db = (
        _raising_db_patch(db_exc)
        if db_exc is not None
        else _db_patch(_filter_chain_session(row))
    )
    with (
        db,
        patch(f"{HISTORY}.get_logs_for_research", return_value=list(db_logs)),
        patch(
            f"{HISTORY}.get_research_strategy", return_value=strategy
        ) as mock_strategy,
        patch(f"{HISTORY}.get_active_research_snapshot", return_value=snapshot),
    ):
        return (
            get_research_details(_request(), "rid-1", username="alice"),
            mock_strategy,
        )


def test_details_of_an_unknown_research_is_a_404_error_envelope():
    resp, _ = _call_details(row=None, snapshot=None)

    assert resp.status_code == 404
    body = _body(resp)
    assert body["status"] == "error"
    assert "not found" in body["message"].lower()


def test_details_merges_in_memory_logs_and_drops_the_duplicates_by_time():
    """A running research has logs the DB has not yet been given. The
    route unions them keyed on ``time`` and re-sorts, so the panel does
    not show the same line twice or out of order.
    """
    db_logs = [
        {"time": "10:00:00", "message": "step 1"},
        {"time": "10:01:00", "message": "step 2"},
    ]
    memory_logs = [
        {"time": "10:01:00", "message": "step 2"},  # duplicate -> dropped
        {"time": "10:02:00", "message": "step 3"},  # unique -> added
    ]
    result, _ = _call_details(
        _entity(status="in_progress"),
        snapshot={"progress": 55, "log": memory_logs},
        db_logs=db_logs,
    )

    assert result["progress"] == 55
    assert len(result["log"]) == 3
    times = [entry["time"] for entry in result["log"]]
    assert times == sorted(times)
    assert times == ["10:00:00", "10:01:00", "10:02:00"]


def test_details_of_a_completed_research_reports_full_progress():
    from local_deep_research.constants import ResearchStatus

    result, _ = _call_details(
        _entity(status=ResearchStatus.COMPLETED),
        snapshot=None,
        strategy="smart",
    )

    assert result["progress"] == 100
    assert result["strategy"] == "smart"
    assert result["research_id"] == "rid-1"
    assert result["query"] == "test query"


def test_details_of_an_inactive_unfinished_research_reports_zero_progress():
    result, _ = _call_details(_entity(status="in_progress"), snapshot=None)

    assert result["progress"] == 0


def test_details_scopes_the_strategy_lookup_to_the_authenticated_user():
    """The strategy lives in the *user's* encrypted DB. The route must
    pass the authenticated username explicitly rather than relying on any
    ambient/session fallback inside the service function — that fallback
    is what makes a cross-user read possible."""
    _, mock_strategy = _call_details(_entity(), snapshot=None)

    mock_strategy.assert_called_once_with("rid-1", username="alice")


def test_details_answers_500_when_the_database_lookup_raises():
    resp, _ = _call_details(
        row=None, snapshot=None, db_exc=RuntimeError("boom")
    )

    assert resp.status_code == 500
    body = _body(resp)
    assert body["status"] == "error"
    assert "database" in body["message"].lower()


# ---------------------------------------------------------------------------
# get_research_logs
# ---------------------------------------------------------------------------


def _call_logs(row, logs):
    from local_deep_research.web.routers.history import get_research_logs

    with (
        _db_patch(_filter_chain_session(row)),
        patch(f"{HISTORY}.get_logs_for_research", return_value=list(logs)),
    ):
        return get_research_logs(_request(), "rid-1", username="alice")


def test_logs_of_an_unknown_research_is_a_404_error_envelope():
    resp = _call_logs(row=None, logs=[])

    assert resp.status_code == 404
    body = _body(resp)
    assert body["status"] == "error"
    assert "not found" in body["message"].lower()


def test_logs_with_every_field_present_are_returned_unchanged():
    raw = [
        {"time": "10:00:00", "message": "step 1", "type": "info"},
        {"time": "10:01:00", "message": "step 2", "type": "warning"},
    ]
    result = _call_logs(_entity(), raw)

    assert result["status"] == "success"
    assert result["logs"] == raw


def test_logs_with_missing_fields_get_defaults():
    """The defensive backfill in ``get_research_logs``.

    The router source names this exact test as its only coverage and
    records that it was deleted with the Flask suite: every row handed to
    the frontend must carry ``time`` / ``message`` / ``type``, and any
    extra key the formatter added must survive the backfill untouched.
    """
    raw = [
        {"time": "10:00", "message": "ok", "type": "info"},
        {"extra": "custom_field"},  # missing time, message and type
    ]
    result = _call_logs(_entity(), raw)

    assert len(result["logs"]) == 2
    second = result["logs"][1]
    assert second["time"] == ""
    assert second["message"] == "No message"
    assert second["type"] == "info"
    assert second["extra"] == "custom_field"


# ---------------------------------------------------------------------------
# get_log_count
# ---------------------------------------------------------------------------


def test_log_count_does_not_reach_the_count_query_when_the_session_fails():
    """A database failure must propagate, not fall through to the
    log-count read with an unverified research id."""
    from local_deep_research.web.routers.history import get_log_count

    with (
        _raising_db_patch(RuntimeError("database unavailable")),
        patch(f"{HISTORY}.get_total_logs_for_research") as mock_total,
    ):
        with pytest.raises(RuntimeError, match="database unavailable"):
            get_log_count(_request(), "rid-1", username="alice")

    mock_total.assert_not_called()


# ---------------------------------------------------------------------------
# history_page
# ---------------------------------------------------------------------------


def test_history_page_renders_the_history_template():
    from local_deep_research.web.routers.history import history_page

    with patch(f"{HISTORY}.templates") as mock_templates:
        mock_templates.TemplateResponse.return_value = "<html>history</html>"
        request = _request()
        result = history_page(request, username="alice")

    assert result == "<html>history</html>"
    assert (
        mock_templates.TemplateResponse.call_args.kwargs["name"]
        == "pages/history.html"
    )