~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test(report): complete #3665 report-content API contract suite (#4844)
LearningCircuitf4e1707972e0
diff --git a/tests/api/test_research_functions_extended.py b/tests/api/test_research_functions_extended.pynew file mode 100644index 0000000000..53cfef6288--- /dev/null+++ b/tests/api/test_research_functions_extended.py@@ -0,0 +1,46 @@+"""Contract tests for the programmatic SDK report entry point (#3665).++Mirrors the MCP ``generate_report`` contract test: the SDK+``generate_report`` returns the in-memory report generator's assembled+``content`` verbatim — including the ``## Sources`` block — without a DB+read or any stripping. Pins the SDK-keeps-assembled-shape invariant the+audit confirmed.+"""++from unittest.mock import MagicMock, patch++import local_deep_research.api.research_functions as rf+++def test_generate_report_content_includes_sources():+ assembled = (+ "# Research Report\n\n## Introduction\n\nBody text.\n\n"+ "## Sources\n\n[1] https://src.example\n\n"+ "## Research Metrics\n\nSearch Iterations: 2\n"+ )++ fake_system = MagicMock()+ fake_system.analyze_topic.return_value = {"findings": "x"}++ fake_generator = MagicMock()+ fake_generator.generate_report.return_value = {+ "content": assembled,+ "metadata": {"query": "q"},+ }++ # Mock the research pipeline so no real search/LLM work runs; the SDK must+ # pass the generator's assembled content straight through.+ with (+ patch.object(rf, "_init_search_system", return_value=fake_system),+ patch.object(+ rf, "IntegratedReportGenerator", return_value=fake_generator+ ),+ patch.object(rf, "_close_system"),+ ):+ result = rf.generate_report(query="q", settings_snapshot={})++ assert "content" in result+ # Verbatim passthrough: the SDK returns the generator's assembled content+ # unmodified. Full equality catches truncation/stripping, not just the+ # absence of the ## Sources header.+ assert result["content"] == assembleddiff --git a/tests/mcp/test_server.py b/tests/mcp/test_server.pyindex c38285d904..a93fac576f 100644--- a/tests/mcp/test_server.py+++ b/tests/mcp/test_server.py@@ -133,6 +133,33 @@ class TestGenerateReport:call_kwargs = mock_generate_report.call_args[1]assert call_kwargs["searches_per_section"] == 3+ def test_generate_report_preserves_assembled_sources_block(self):+ """MCP surfaces the generator's in-memory assembled content verbatim,+ including the ``## Sources`` block — it never DB-reads or strips it.++ Pins the MCP-keeps-assembled-shape invariant the #3665 audit+ confirmed (in-memory generator output, never a DB read).+ """+ from local_deep_research.mcp.server import generate_report++ assembled = (+ "# Research Report\n\n## Introduction\n\nBody.\n\n"+ "## Sources\n\n[1] https://src.example\n\n"+ "## Research Metrics\n\nSearch Iterations: 2\n"+ )+ with patch(+ "local_deep_research.mcp.server.ldr_generate_report",+ return_value={"content": assembled, "metadata": {"query": "q"}},+ ):+ result = generate_report(query="q")++ assert result["status"] == "success"+ # Verbatim passthrough: the assembled content (incl. ## Sources and the+ # trailing ## Research Metrics) survives unmodified. Full equality —+ # substring presence alone wouldn't catch truncation after ## Sources.+ assert result["content"] == assembled+ assert result["metadata"] == {"query": "q"}+class TestAnalyzeDocuments:"""Tests for the analyze_documents tool."""diff --git a/tests/news/test_news_api_contract.py b/tests/news/test_news_api_contract.pynew file mode 100644index 0000000000..8e524ea44f--- /dev/null+++ b/tests/news/test_news_api_contract.py@@ -0,0 +1,136 @@+"""API contract tests for the news feed report shape (#3665 Fix B).++After the chat-mode-v2 refactor, ``report_content`` stores only the+synthesized answer. The news feed must therefore expose:++* ``findings`` — the answer-only ``report_content`` verbatim (no embedded+ ``## Sources`` block), and+* ``links`` — the structured top-N source URLs read from the+ ``research_resources`` table (not parsed out of ``report_content``).++These exercise the real ``get_news_feed`` research-history path against a+seeded DB; only the per-user-DB session is patched.+"""++from contextlib import contextmanager+from datetime import datetime, UTC+from unittest.mock import patch++import pytest+from sqlalchemy import create_engine+from sqlalchemy.orm import sessionmaker++from local_deep_research.database.models import Base+from local_deep_research.database.models.research import (+ ResearchHistory,+ ResearchResource,+)++RESEARCH_ID = "news-contract-research-1"+# "latest news" in the query makes get_news_feed treat the row as a news item.+QUERY = "latest news on quantum computing breakthroughs"+# Answer-only report content — inline [N](url) citations, NO ## Sources block.+ANSWER_ONLY = (+ "Quantum error correction crossed a threshold this year, "+ "citing [1](https://q1.example) and [2](https://q2.example)."+)+N_SOURCES = 4 # > the feed's top-N (3): links must be capped, not unbounded.+++def seeded_news_db(tmp_path):+ """Real temp-file SQLite seeded with one completed news-style research."""+ engine = create_engine(f"sqlite:///{tmp_path / 'news_contract.db'}")+ Base.metadata.create_all(engine)+ SessionLocal = sessionmaker(bind=engine)++ seed = SessionLocal()+ seed.add(+ ResearchHistory(+ id=RESEARCH_ID,+ query=QUERY,+ title="Quantum Computing Breakthroughs",+ mode="quick",+ status="completed",+ created_at="2026-04-25T12:00:00+00:00",+ completed_at="2026-04-25T12:05:00+00:00",+ report_content=ANSWER_ONLY,+ research_meta={"is_news_search": True},+ )+ )+ for i in range(1, N_SOURCES + 1):+ url = f"https://q{i}.example"+ seed.add(+ ResearchResource(+ research_id=RESEARCH_ID,+ title=f"Quantum Source {i}",+ url=url,+ source_type="web",+ resource_metadata={+ "original_data": {+ "index": str(i),+ "url": url,+ "title": f"Quantum Source {i}",+ }+ },+ created_at=datetime.now(UTC).isoformat(),+ )+ )+ seed.commit()+ seed.close()++ @contextmanager+ def _fake_user_db(username=None, password=None):+ db = SessionLocal()+ try:+ yield db+ finally:+ db.close()++ # get_news_feed imports get_user_db_session from session_context at call+ # time, so patch it at the source module.+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ _fake_user_db,+ ):+ yield++ engine.dispose()+++def _feed_item(seeded_news_db):+ from local_deep_research.news.api import get_news_feed++ result = get_news_feed(user_id="testuser", limit=10, use_cache=False)+ items = result["news_items"]+ matches = [it for it in items if it.get("research_id") == RESEARCH_ID]+ assert matches, f"seeded research not surfaced in feed: {items}"+ return matches[0]+++def test_news_feed_findings_is_answer_only(seeded_news_db):+ item = _feed_item(seeded_news_db)+ findings = item["findings"]+ assert findings == ANSWER_ONLY+ # The post-refactor contract: no assembled ## Sources blob in findings.+ assert "## Sources" not in findings+++def test_news_feed_links_array_populated_from_research_resources(+ seeded_news_db,+):+ item = _feed_item(seeded_news_db)+ links = item["links"]+ assert isinstance(links, list)+ # Exactly the top-N cap: the feed passes limit=3 (news/api.py), so 3 of+ # the 4 seeded resources come back. == 3 (not <= 3) fences the cap AND+ # the source: report_content only cites [1]/[2] inline, so a regression+ # that parsed links out of report_content could yield at most 2.+ assert len(links) == 3+ urls = {link.get("url") for link in links}+ # At least one link is a research_resources-only source (q3/q4), absent+ # from the two inline citations in report_content — proves the links are+ # read FROM the table, not parsed out of the answer text.+ assert urls - {"https://q1.example", "https://q2.example"}, urls+ for link in links:+ assert link.get("url")diff --git a/tests/web/routes/test_report_api_contract.py b/tests/web/routes/test_report_api_contract.pyindex b24d5b2139..37743a6def 100644--- a/tests/web/routes/test_report_api_contract.py+++ b/tests/web/routes/test_report_api_contract.py@@ -25,12 +25,30 @@ from sqlalchemy import create_enginefrom sqlalchemy.orm import sessionmakerfrom local_deep_research.database.models import Base+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.@@ -126,6 +144,43 @@ def seeded_db(tmp_path):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()@@ -146,6 +201,12 @@ def seeded_db(tmp_path):"local_deep_research.web.routes.history_routes.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@@ -232,3 +293,52 @@ def test_api_report_sources_field_matches_research_resources_count(assert resp.status_code == 200, resp.datasources = resp.get_json()["sources"]assert len(sources) == N_SOURCES+++# ---------------------------------------------------------------------------+# Graceful empty-sources shape (item 7)+# ---------------------------------------------------------------------------+++def test_response_with_no_sources_omits_sources_block_gracefully(+ mock_auth_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 = mock_auth_client.get(f"/api/report/{RESEARCH_ID_NO_SOURCES}")+ assert resp.status_code == 200, resp.data+ payload = resp.get_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(mock_auth_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 = mock_auth_client.get(+ f"/api/chat/sessions/{CHAT_SESSION_ID}/messages"+ )+ assert resp.status_code == 200, resp.data+ payload = resp.get_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