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.

14.5 KB
Raw Blame
"""API contract tests for the report-content display routes.

Locks the public shape of report content so a future change can't silently
regress it (#3665):

* ``content`` is the *assembled* legacy view — answer + ``## Sources`` (from
  the ``research_resources`` table) + ``## Research Metrics`` (from
  ``research_meta``) — across every display/export route.
* ``GET /api/report/<id>`` exposes a ``sources`` field populated from
  ``research_resources``. Before the fix it read the dead
  ``all_links_of_system`` metadata key, which the post-refactor save path
  never writes, so it returned ``[]`` for every new research (Fix A).

These exercise the *real* assembly path (``assemble_full_report`` /
``get_research_source_links_batch``) against real seeded rows; only auth and
the per-user-DB plumbing are stubbed.

Post-FastAPI-migration the report routes live on FastAPI routers
(web.routers.history + web.routers.research) and authenticate via the
``require_auth`` dependency; this drives them through the FastAPI TestClient
with that dependency overridden and ``get_user_db_session`` pointed at a seeded
SQLite file.
"""

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

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base, Setting, SettingType
from local_deep_research.database.models.chat import (
    ChatMessage,
    ChatMessageType,
    ChatRole,
    ChatSession,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)

RESEARCH_ID = "contract-research-1"
# A second research with zero research_resources rows: the assembled view
# must omit the ``## Sources`` block entirely rather than emit an empty one
# (item 7 regression fence).
RESEARCH_ID_NO_SOURCES = "contract-research-nosrc"
# A chat session whose assistant message is the answer-only report_content —
# chat is the ONE consumer that must NOT receive the assembled view (item 8,
# architecture invariant of the chat-mode-v2 refactor).
CHAT_SESSION_ID = "contract-chat-1"
ANSWER_ONLY = (
    "X is explained here, citing [1](https://src1.example) "
    "and [2](https://src2.example)."
)
# Deliberately > the batch helper's default top-N (3): the report API must
# return *every* source (no cap), so a reintroduced default limit would fail
# test_api_report_sources_field_matches_research_resources_count.
N_SOURCES = 5


@pytest.fixture
def client():
    """FastAPI test client authenticated as ``testuser``.

    Auth is satisfied by overriding the ``require_auth`` dependency rather than
    a real register/login, because the per-user DB is patched to a seeded
    SQLite file (``seeded_db``) — the real auth flow would create a real
    encrypted DB we couldn't seed sources into.
    """
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import require_auth

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


@pytest.fixture
def seeded_db(tmp_path):
    """Real temp-file SQLite seeded with one completed research + sources.

    Patches ``get_user_db_session`` in both router modules to yield a fresh
    session bound to the seeded DB (a fresh session per request, like the
    real per-request session). A file DB (not ``:memory:``) so every
    ``SessionLocal()`` connection sees the same seeded rows.
    """
    engine = create_engine(f"sqlite:///{tmp_path / 'report_contract.db'}")
    Base.metadata.create_all(engine)
    SessionLocal = sessionmaker(bind=engine)

    seed = SessionLocal()
    seed.add(
        ResearchHistory(
            id=RESEARCH_ID,
            query="What is X?",
            mode="quick",
            status="completed",
            created_at="2026-04-25T12:00:00+00:00",
            completed_at="2026-04-25T12:05:00+00:00",
            report_content=(
                "X is explained here, citing [1](https://src1.example) "
                "and [2](https://src2.example)."
            ),
            research_meta={
                "iterations": 2,
                "generated_at": "2026-04-25T12:05:00+00:00",
                # settings_snapshot is persisted in research_meta but must never
                # be returned by an API response (it holds API keys / tokens) —
                # see test_history_report_strips_settings_snapshot.
                "settings_snapshot": {
                    "llm.openai.api_key": "sk-SECRET-LEAKED-KEY",
                },
            },
        )
    )
    for i in range(1, N_SOURCES + 1):
        url = f"https://src{i}.example"
        seed.add(
            ResearchResource(
                research_id=RESEARCH_ID,
                title=f"Source {i}",
                url=url,
                source_type="web",
                resource_metadata={
                    "original_data": {
                        "index": str(i),
                        "url": url,
                        "title": f"Source {i}",
                    }
                },
                created_at=datetime.now(UTC).isoformat(),
            )
        )

    # A completed research with NO research_resources rows (item 7).
    seed.add(
        ResearchHistory(
            id=RESEARCH_ID_NO_SOURCES,
            query="What is Y?",
            mode="quick",
            status="completed",
            created_at="2026-04-25T13:00:00+00:00",
            completed_at="2026-04-25T13:05:00+00:00",
            report_content="Y has no catalogued sources but still has an answer.",
            research_meta={
                "iterations": 1,
                "generated_at": "2026-04-25T13:05:00+00:00",
            },
        )
    )

    # A chat session with one assistant response message whose content is the
    # answer-only report_content — never the assembled view (item 8).
    seed.add(
        ChatSession(
            id=CHAT_SESSION_ID,
            title="Contract chat",
        )
    )
    seed.add(
        ChatMessage(
            id="contract-chat-msg-1",
            session_id=CHAT_SESSION_ID,
            research_id=RESEARCH_ID,
            role=ChatRole.ASSISTANT,
            message_type=ChatMessageType.RESPONSE,
            content=ANSWER_ONLY,
            sequence_number=1,
        )
    )
    seed.commit()
    seed.close()

    @contextmanager
    def _fake_user_db(username=None, password=None):
        db = SessionLocal()
        try:
            yield db
        finally:
            db.close()

    with (
        patch(
            "local_deep_research.web.routers.research.get_user_db_session",
            _fake_user_db,
        ),
        patch(
            "local_deep_research.web.routers.history.get_user_db_session",
            _fake_user_db,
        ),
        # ChatService resolves its session through the chat.service binding,
        # so the chat-route test (item 8) needs the same seeded DB.
        patch(
            "local_deep_research.chat.service.get_user_db_session",
            _fake_user_db,
        ),
    ):
        yield SessionLocal

    engine.dispose()


# ---------------------------------------------------------------------------
# Assembled-view shape across display routes (locks the legacy report shape)
# ---------------------------------------------------------------------------


def test_history_report_includes_sources_and_metrics(client, seeded_db):
    resp = client.get(f"/history/report/{RESEARCH_ID}")
    assert resp.status_code == 200, resp.text
    content = resp.json()["content"]
    assert "## Sources\n" in content
    assert "## Research Metrics\n" in content
    # Sources block references the structured resources.
    assert "src1.example" in content


def test_history_report_strips_settings_snapshot(client, seeded_db):
    """GET /history/report/<id> must NOT return settings_snapshot (API keys,
    tokens, base URLs) in its metadata — it is stripped like the sibling
    /details and /api/research/<id> routes (sensitive-data exposure)."""
    resp = client.get(f"/history/report/{RESEARCH_ID}")
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert "settings_snapshot" not in body["metadata"]
    # Belt-and-suspenders: the secret value appears nowhere in the response.
    assert "sk-SECRET-LEAKED-KEY" not in resp.text
    # Non-sensitive metadata fields are still preserved.
    assert body["metadata"].get("iterations") == 2


def test_history_markdown_includes_sources_and_metrics(client, seeded_db):
    resp = client.get(f"/history/markdown/{RESEARCH_ID}")
    assert resp.status_code == 200, resp.text
    content = resp.json()["content"]
    assert "## Sources\n" in content
    assert "## Research Metrics\n" in content


def test_api_report_content_includes_sources_and_metrics(client, seeded_db):
    resp = client.get(f"/api/report/{RESEARCH_ID}")
    assert resp.status_code == 200, resp.text
    content = resp.json()["content"]
    assert "## Sources\n" in content
    assert "## Research Metrics\n" in content


def test_export_latex_content_includes_sources_and_metrics(client, seeded_db):
    # LaTeX is the registered plain-text export format (markdown is served by
    # /history/markdown; quarto is a .zip; pdf/odt are binary). LaTeX
    # conversion drops the markdown `##` prefix but keeps the header words and
    # body text. "Research Metrics" / "Search Iterations" exist ONLY in the
    # assembled view, never in the answer-only report_content, so finding them
    # proves the export path goes through assemble_full_report.
    csrf = client.get("/auth/csrf-token").json()["csrf_token"]
    resp = client.post(
        f"/api/v1/research/{RESEARCH_ID}/export/latex",
        headers={"X-CSRFToken": csrf},
    )
    assert resp.status_code == 200, resp.text
    body = resp.text
    assert "Research Metrics" in body
    assert "Search Iterations" in body
    assert "Sources" in body
    assert "src1.example" in body


@pytest.mark.parametrize(
    "route",
    ["history/report", "history/markdown", "api/report", "export/latex"],
)
@pytest.mark.parametrize("mode", ["disabled", "strict"])
@pytest.mark.parametrize("json_encoded", [False, True])
def test_legacy_snapshot_uses_saved_mode_on_view_and_export(
    client, seeded_db, route, mode, json_encoded
):
    """HTTP requests honor current preferences when the old snapshot lacks them."""
    with seeded_db() as db:
        db.add(
            Setting(
                key="report.uncited_sources_mode",
                value=mode,
                type=SettingType.REPORT,
                name="Uncited sources mode",
                ui_element="select",
            )
        )
        research = db.get(ResearchHistory, RESEARCH_ID)
        if json_encoded:
            research.research_meta = json.dumps(research.research_meta)
        if mode == "strict":
            # No citations distinguishes strict from fallback, which keeps all.
            research.report_content = "No citations."
        db.commit()

    if route == "export/latex":
        csrf = client.get("/auth/csrf-token").json()["csrf_token"]
        resp = client.post(
            f"/api/v1/research/{RESEARCH_ID}/export/latex",
            headers={"X-CSRFToken": csrf},
        )
        assert resp.status_code == 200, resp.text
        content = resp.text
    else:
        resp = client.get(f"/{route}/{RESEARCH_ID}")
        assert resp.status_code == 200, resp.text
        content = resp.json()["content"]

    # Sources 3-5 have no prose citations. Disabled must keep all five;
    # strict with no citations must omit all five, across every route.
    for index in range(1, N_SOURCES + 1):
        assert (f"src{index}.example" in content) == (mode == "disabled")


# ---------------------------------------------------------------------------
# Fix A regression fence: /api/report sources field from research_resources
# ---------------------------------------------------------------------------


def test_api_report_sources_field_populated_from_research_resources(
    client, seeded_db
):
    resp = client.get(f"/api/report/{RESEARCH_ID}")
    assert resp.status_code == 200, resp.text
    sources = resp.json()["sources"]
    assert isinstance(sources, list)
    assert sources, "sources must not be empty (Fix A regression)"
    for entry in sources:
        assert entry.get("url")
        assert entry.get("title")


def test_api_report_sources_field_matches_research_resources_count(
    client, seeded_db
):
    resp = client.get(f"/api/report/{RESEARCH_ID}")
    assert resp.status_code == 200, resp.text
    sources = resp.json()["sources"]
    assert len(sources) == N_SOURCES


# ---------------------------------------------------------------------------
# Graceful empty-sources shape (item 7)
# ---------------------------------------------------------------------------


def test_response_with_no_sources_omits_sources_block_gracefully(
    client, seeded_db
):
    # A research with zero research_resources rows must still render a valid
    # report — answer (+ metrics) — with NO empty ``## Sources`` section, and
    # an empty ``sources`` list rather than an error.
    resp = client.get(f"/api/report/{RESEARCH_ID_NO_SOURCES}")
    assert resp.status_code == 200, resp.text
    payload = resp.json()
    content = payload["content"]
    assert content, "content must be non-empty even with no sources"
    assert "no catalogued sources" in content
    assert "## Sources\n" not in content
    assert payload["sources"] == []


# ---------------------------------------------------------------------------
# Chat is the one answer-only consumer (item 8) — architecture invariant
# ---------------------------------------------------------------------------


def test_chat_message_response_returns_answer_only(client, seeded_db):
    # The chat messages endpoint must return the stored ``report_content``
    # verbatim (answer-only). If a future change routed it through
    # assemble_full_report, a ``## Sources`` / ``## Research Metrics`` block
    # would leak in — this fences that off.
    resp = client.get(f"/api/chat/sessions/{CHAT_SESSION_ID}/messages")
    assert resp.status_code == 200, resp.text
    payload = resp.json()
    assert payload["success"] is True
    responses = [
        m
        for m in payload["messages"]
        if m.get("message_type") == ChatMessageType.RESPONSE.value
    ]
    assert len(responses) == 1, payload["messages"]
    content = responses[0]["content"]
    assert content == ANSWER_ONLY
    assert "## Sources\n" not in content
    assert "## Research Metrics\n" not in content