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.

7.1 KB
Raw Blame
"""Unit tests for the /history/report and /history/markdown handlers.

These routes must rebuild the assembled legacy report shape via
``assemble_full_report`` (answer + Sources + Research Metrics) — the FastAPI
port originally read the answer-only ``storage.get_report*()`` body, silently
dropping the Sources/Metrics sections (#3665 regression, fixed in 495e68775).
The black-box contract lives in tests/web/routes/test_report_api_contract.py;
these unit tests pin the per-branch behaviour (404/500 paths and the
assemble_full_report delegation) with the route functions called directly.
"""

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

import pytest

HISTORY = "local_deep_research.web.routers.history"
ASSEMBLE = (
    "local_deep_research.web.services.report_assembly_service"
    ".assemble_full_report"
)


def _patched_db(research_row):
    """Patch get_user_db_session with a session whose ResearchHistory query
    returns ``research_row``."""
    session = Mock()
    session.query.return_value.filter_by.return_value.first.return_value = (
        research_row
    )

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

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


def _research_row():
    row = Mock()
    row.query = "What is X?"
    row.mode = "quick"
    row.created_at = "2026-06-01T12:00:00+00:00"
    row.completed_at = "2026-06-01T12:05:00+00:00"
    row.duration_seconds = 300
    row.research_meta = {"iterations": 2}
    return row


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


class TestGetReport:
    def _call(self, research_row, assemble=None):
        from local_deep_research.web.routers.history import get_report

        session, db_patch = _patched_db(research_row)
        with db_patch, patch(f"{ASSEMBLE}", assemble or Mock()) as mock_asm:
            result = get_report(Mock(), "rid-1", username="alice")
        return result, session, mock_asm

    def test_unknown_research_returns_404(self):
        resp, _, mock_asm = self._call(research_row=None)

        assert resp.status_code == 404
        assert _body(resp)["message"] == "Report not found"
        mock_asm.assert_not_called()

    def test_assembled_none_returns_404(self):
        resp, _, _ = self._call(
            _research_row(), assemble=Mock(return_value=None)
        )

        assert resp.status_code == 404
        assert _body(resp)["message"] == "Report content not found"

    def test_success_returns_assembled_content_and_metadata(self):
        row = _research_row()
        assembled = "answer\n\n## Sources\n- s\n\n## Research Metrics\n- m"
        result, session, mock_asm = self._call(
            row, assemble=Mock(return_value=assembled)
        )

        # The display shape comes from assemble_full_report(research, session)
        # — NOT from the answer-only storage.get_report*() body (#3665).
        mock_asm.assert_called_once_with(row, session)
        assert result["status"] == "success"
        assert result["content"] == assembled
        # DB fields merged with stored research_meta.
        assert result["metadata"]["query"] == "What is X?"
        assert result["metadata"]["duration"] == 300
        assert result["metadata"]["iterations"] == 2

    def test_empty_but_found_report_is_valid(self):
        """An existing row with no body/sources/metrics assembles to '' and is
        a valid 200-shaped response, not a 404."""
        result, _, _ = self._call(
            _research_row(), assemble=Mock(return_value="")
        )

        assert result["status"] == "success"
        assert result["content"] == ""

    def test_assemble_exception_returns_500(self):
        resp, _, _ = self._call(
            _research_row(), assemble=Mock(side_effect=RuntimeError("boom"))
        )

        assert resp.status_code == 500
        assert _body(resp)["message"] == "Failed to retrieve report"


class TestGetMarkdown:
    def _call(self, research_row, assemble=None):
        from local_deep_research.web.routers.history import get_markdown

        session, db_patch = _patched_db(research_row)
        with db_patch, patch(f"{ASSEMBLE}", assemble or Mock()) as mock_asm:
            result = get_markdown(Mock(), "rid-1", username="alice")
        return result, session, mock_asm

    def test_unknown_research_returns_404(self):
        resp, _, mock_asm = self._call(research_row=None)

        assert resp.status_code == 404
        mock_asm.assert_not_called()

    def test_assembled_none_returns_404(self):
        resp, _, _ = self._call(
            _research_row(), assemble=Mock(return_value=None)
        )

        assert resp.status_code == 404
        assert _body(resp)["message"] == "Report content not found"

    def test_success_returns_assembled_content(self):
        row = _research_row()
        assembled = "answer\n\n## Sources\n- s"
        result, session, mock_asm = self._call(
            row, assemble=Mock(return_value=assembled)
        )

        mock_asm.assert_called_once_with(row, session)
        assert result == {"status": "success", "content": assembled}

    def test_assemble_exception_returns_500(self):
        resp, _, _ = self._call(
            _research_row(), assemble=Mock(side_effect=RuntimeError("boom"))
        )

        assert resp.status_code == 500


class TestGetLogCount:
    """Unit tests for get_log_count — main's #5386 fix (verify the
    user-scoped research row before reading the log count DB, so a missing
    research id is distinct from a real research with zero logs)."""

    def _call(self, research_row, total_logs=0):
        from local_deep_research.web.routers.history import get_log_count

        session, db_patch = _patched_db(research_row)
        with (
            db_patch,
            patch(
                f"{HISTORY}.get_total_logs_for_research",
                return_value=total_logs,
            ) as mock_total,
        ):
            result = get_log_count(Mock(), "rid-1", username="alice")
        return result, mock_total

    def test_unknown_research_returns_404(self):
        result, mock_total = self._call(research_row=None)

        assert result.status_code == 404
        body = _body(result)
        assert body["status"] == "error"
        assert body["error"] == "Research not found"
        assert body["message"] == "Research not found"
        mock_total.assert_not_called()

    def test_success_returns_total_logs(self):
        result, mock_total = self._call(
            research_row=_research_row(), total_logs=15
        )

        assert result == {"status": "success", "total_logs": 15}
        mock_total.assert_called_once_with("rid-1", "alice")


@pytest.mark.parametrize("route_name", ["get_report", "get_markdown"])
def test_routes_do_not_use_answer_only_storage(route_name):
    """Regression fence for #3665: neither display route may read the
    answer-only storage.get_report*() body."""
    import inspect

    import local_deep_research.web.routers.history as history

    source = inspect.getsource(getattr(history, route_name))
    assert "get_report_storage" not in source
    assert "assemble_full_report" in source