~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
"""
Tests for research_service synthesis and report generation.
Tests cover:
- Quick mode synthesis
- Report generation
- News search integration
"""
from unittest.mock import Mock, patch
import pytest
from tests.web.services.helpers import run_quick_mode_with_analyze_result
class TestQuickModeSynthesis:
"""Tests for quick mode synthesis.
These drive the REAL quick-mode synthesis/fallback logic in
``run_research_process`` (research_service.py ~1736-1900) via the
shared ``run_quick_mode_with_analyze_result`` / ``_search_error``
harnesses in helpers.py, instead of re-deriving the same if/elif
chain locally over a dict the test built and never passed anywhere.
"""
def test_quick_mode_synthesis_success(self):
"""Quick mode synthesis completes successfully."""
result = run_quick_mode_with_analyze_result(
{
"findings": [
{
"content": "Test summary.",
"phase": "Final synthesis",
}
],
"formatted_findings": "# Research Summary\n\nTest summary.",
"iterations": 2,
"current_knowledge": "",
}
)
assert result == {"clean_markdown": "Test summary."}
@pytest.mark.parametrize(
"formatted_findings",
[
"Error: context length exceeded", # token_limit
"Error: request timed out", # timeout
"Error: rate limit exceeded", # rate_limit
"Error: connection refused", # connection
"Error: LLM error during synthesis", # llm_error
"Error: something else entirely", # unknown
],
)
def test_quick_mode_synthesis_error_shaped_findings_recover(
self, formatted_findings
):
"""Every 'Error:'-shaped formatted_findings — regardless of which
keyword it carries — routes through the SAME recovery path and
falls back to current_knowledge rather than surfacing the raw
error text or crashing.
research_service.py classifies the keyword into an
``error_type`` ("token_limit"/"timeout"/"rate_limit"/
"connection"/"llm_error"/"unknown") purely to word the
*progress-bar message* shown while recovering — that label
never reaches the persisted report and isn't reliably
observable here (the "synthesis_error" progress event is
throttled off the socket stream in this harness, same as
production). What IS observable, and what this test pins, is
the actually-persisted outcome: recovery succeeds regardless of
which keyword triggered it.
"""
result = run_quick_mode_with_analyze_result(
{
"findings": [],
"formatted_findings": formatted_findings,
"iterations": 1,
"current_knowledge": "recovered knowledge",
}
)
assert result == {"clean_markdown": "recovered knowledge"}
def test_quick_mode_synthesis_fallback_cascade_level_1(self):
"""Quick mode synthesis uses synthesized content as first fallback."""
result = run_quick_mode_with_analyze_result(
{
"findings": [
{"content": "Search finding", "phase": "search"},
{
"content": "Good synthesis content",
"phase": "Final synthesis",
},
],
"formatted_findings": "Error: post-processing failed",
"iterations": 2,
"current_knowledge": "",
}
)
assert result == {"clean_markdown": "Good synthesis content"}
def test_quick_mode_synthesis_fallback_cascade_level_2(self):
"""A 'Final synthesis' finding that is ITSELF error-shaped is not
usable as an answer. The saved report uses accumulated knowledge
rather than persisting raw synthesis error text.
"""
result = run_quick_mode_with_analyze_result(
{
"findings": [
{
"content": "Error: synthesis failed",
"phase": "Final synthesis",
}
],
"formatted_findings": "Error: synthesis failed",
"current_knowledge": "Accumulated knowledge from search",
"iterations": 2,
}
)
assert result == {"clean_markdown": "Accumulated knowledge from search"}
def test_quick_mode_synthesis_fallback_cascade_level_3(self):
"""Quick mode synthesis combines findings as last fallback."""
result = run_quick_mode_with_analyze_result(
{
"findings": [
{"content": "Finding 1", "phase": "search"},
{"content": "Finding 2", "phase": "analysis"},
],
"formatted_findings": "Error: all synthesis failed",
"current_knowledge": "",
"iterations": 2,
}
)
markdown = result["clean_markdown"]
assert "Fallback Mode" in markdown
assert "Finding 1" in markdown
assert "Finding 2" in markdown
@pytest.mark.parametrize("prefix", ["Error:", " error:"])
def test_quick_mode_partial_findings_omit_synthesis_error_detail(
self, prefix
):
marker = "/srv/private/provider-internals.txt"
result = run_quick_mode_with_analyze_result(
{
"findings": [
{"content": "Useful finding", "phase": "search"},
{
"content": f"{prefix} provider failed at {marker}",
"phase": "synthesis",
},
],
"formatted_findings": f"{prefix} provider failed at {marker}",
"current_knowledge": "",
"iterations": 1,
}
)
assert "Useful finding" in result["clean_markdown"]
assert marker not in result["clean_markdown"]
def test_quick_mode_all_error_findings_use_error_report(self):
result = run_quick_mode_with_analyze_result(
{
"findings": [
{
"content": "Error: provider failed at /srv/private/provider-internals.txt",
"phase": "Final synthesis",
}
],
"formatted_findings": "Error: synthesis failed",
"current_knowledge": "",
"iterations": 1,
}
)
assert "error_report_message" in result
def test_quick_mode_synthesis_all_fallbacks_exhausted(self):
"""When there is nothing to fall back to at all (no findings, no
current_knowledge), the run reports failure via
ErrorReportGenerator instead of silently persisting a blank or
raw-error report."""
result = run_quick_mode_with_analyze_result(
{
"findings": [],
"formatted_findings": "Error: complete failure",
"current_knowledge": "",
"iterations": 0,
}
)
assert "error_report_message" in result
def test_quick_mode_synthesis_partial_content_recovery(self):
"""Quick mode synthesis recovers partial content from errors,
keeping only the non-error-shaped findings in the combined
fallback."""
result = run_quick_mode_with_analyze_result(
{
"findings": [
{"content": "Complete finding 1", "phase": "search"},
{"content": "Error: partial", "phase": "synthesis"},
{"content": "Complete finding 3", "phase": "analysis"},
],
"formatted_findings": "Error: synthesis incomplete",
"iterations": 2,
"current_knowledge": "",
}
)
markdown = result["clean_markdown"]
assert "Complete finding 1" in markdown
assert "Complete finding 3" in markdown
assert "Error: partial" not in markdown
def test_quick_mode_synthesis_context_overflow_recovery(self):
"""A context-length-exceeded synthesis error recovers via
current_knowledge, same as any other 'Error:'-shaped message."""
result = run_quick_mode_with_analyze_result(
{
"findings": [{"content": "Finding", "phase": "search"}],
"formatted_findings": (
"Error: maximum context length exceeded"
),
"iterations": 1,
"current_knowledge": "recovered after overflow",
}
)
assert result == {"clean_markdown": "recovered after overflow"}
@pytest.mark.skip(
reason=(
"self-referential and disconnected from quick-mode synthesis: "
'`"".join(chunks)` is plain stdlib string joining, asserted '
"against itself. The real chunk-accumulation this test's name "
"suggests belongs to a different code path entirely -- the "
"CHAT streaming callback (_make_chat_stream_callback / "
"streaming_state['chunks'] in research_service.py), not quick-"
"mode research synthesis, which does not stream partial "
"chunks at all (system.analyze_topic returns one results dict)."
)
)
def test_quick_mode_synthesis_streaming_response_handling(self):
"""Quick mode synthesis handles streaming responses."""
@pytest.mark.skip(
reason=(
"self-referential: defines a local `progress_callback` "
"closure, calls only that, and asserts on the list it "
"appended to itself -- exercises no production code. Real "
"progress-call ordering through the ACTUAL closure is "
"already covered by "
"test_research_service_progress_integration.py's "
"test_report_phase_sequence_climbs_through_closure (and the "
"other cases in that file), which drive the real "
"run_research_process closure end to end."
)
)
def test_quick_mode_synthesis_progress_callback_sequencing(self):
"""Quick mode synthesis calls progress callbacks in order."""
def test_quick_mode_synthesis_empty_results_handling(self):
"""Quick mode synthesis handles empty results: with neither
findings nor formatted_findings, the run fails loudly (via
ErrorReportGenerator) rather than silently persisting nothing."""
result = run_quick_mode_with_analyze_result(
{
"findings": [],
"formatted_findings": "",
"iterations": 0,
"current_knowledge": "",
}
)
assert "error_report_message" in result
class TestReportGeneration:
"""Tests for report generation."""
@pytest.mark.skip(
reason=(
"self-referential: patches get_citation_formatter/"
"get_user_db_session but never invokes production code through "
"them -- it calls `mock_fmt.format_document(content)` directly "
"on the mock it just configured and asserts on the mock's own "
"configured return value. TestQuickModeSynthesis in this file "
"now exercises the real quick-mode report path end-to-end "
"(via run_quick_mode_with_analyze_result), which is the "
"closest real equivalent to what this test's name promises."
)
)
def test_report_generation_success(self):
"""Report generation completes successfully."""
@patch("local_deep_research.exporters.ExporterRegistry")
def test_report_generation_pdf_export_success(self, mock_registry):
"""Report PDF export succeeds."""
mock_exporter = Mock()
mock_result = Mock()
mock_result.content = b"PDF content"
mock_result.filename = "report.pdf"
mock_result.mimetype = "application/pdf"
mock_exporter.export.return_value = mock_result
mock_registry.get_exporter.return_value = mock_exporter
from local_deep_research.web.services.research_service import (
export_report_to_memory,
)
content, filename, mimetype = export_report_to_memory(
"# Report", "pdf", title="Test"
)
assert content == b"PDF content"
assert filename.endswith(".pdf")
assert mimetype == "application/pdf"
@patch("local_deep_research.exporters.ExporterRegistry")
def test_report_generation_pdf_export_failure_recovery(self, mock_registry):
"""Report PDF export handles failure."""
mock_exporter = Mock()
mock_exporter.export.side_effect = Exception("PDF error")
mock_registry.get_exporter.return_value = mock_exporter
from local_deep_research.web.services.research_service import (
export_report_to_memory,
)
with pytest.raises(Exception) as exc_info:
export_report_to_memory("# Report", "pdf", title="Test")
assert "PDF error" in str(exc_info.value)
@pytest.mark.skip(
reason=(
"self-referential: patches get_user_db_session but calls "
"`mock_session.commit()` directly and asserts the mock was "
"called -- exercises no production code. No standalone "
"production symbol commits a report to the DB in isolation; "
"that happens inline inside run_research_process's quick-mode "
"block (research_service.py ~2065-2100), which is heavier "
"than this test's own DB-mock assertion actually needs to "
"pin. Not attempted here; left as a gap rather than a "
"manufactured pass."
)
)
def test_report_generation_database_commit_success(self):
"""Report generation commits to database."""
@pytest.mark.skip(
reason=(
"self-referential, same reasoning as "
"test_report_generation_database_commit_success: calls "
"`mock_session.commit()` (configured to raise) directly and "
"catches its own configured exception."
)
)
def test_report_generation_database_commit_failure(self):
"""Report generation handles database commit failure."""
def test_report_generation_metadata_json_parsing(self):
"""Report generation parses metadata JSON.
Calls the real ``_parse_research_metadata`` (research_service.py)
instead of raw ``json.loads`` -- that's the function
run_research_process actually calls to parse
``research.research_meta`` before merging in new report metadata.
"""
from local_deep_research.web.services.research_service import (
_parse_research_metadata,
)
metadata = _parse_research_metadata(
'{"iterations": 3, "generated_at": "2024-01-01T00:00:00Z"}'
)
assert metadata["iterations"] == 3
assert "generated_at" in metadata
def test_report_generation_metadata_invalid_json(self):
"""Report generation handles invalid metadata JSON."""
from local_deep_research.web.services.research_service import (
_parse_research_metadata,
)
assert _parse_research_metadata("invalid json {") == {}
@pytest.mark.skip(
reason=(
"self-referential: builds a bare `Mock()` named `mock_storage`, "
"calls its own `save_report` method, and asserts on the mock's "
"own configured return value -- exercises no production code. "
"The real storage abstraction is `get_report_storage()` "
"(local_deep_research.storage), invoked inline inside "
"run_research_process; not independently exercised here."
)
)
def test_report_generation_storage_abstraction(self):
"""Report generation uses storage abstraction."""
@pytest.mark.skip(
reason=(
"self-referential, same reasoning as "
"test_report_generation_storage_abstraction: raises IOError "
"from a bare Mock it configured itself and catches its own "
"configured exception."
)
)
def test_report_generation_file_write_error(self):
"""Report generation handles file write errors."""
def test_report_generation_path_creation(self):
"""Report generation creates output paths.
Calls the real ``_generate_report_path`` (research_service.py)
instead of building an unrelated ``Path`` locally and asserting
on its own string formatting.
"""
from local_deep_research.web.services.research_service import (
_generate_report_path,
)
path = _generate_report_path("test query")
assert path.name.startswith("research_report_")
assert path.suffix == ".md"
# Two different queries hash to different filenames.
assert _generate_report_path("a different query").name != path.name
@pytest.mark.skip(
reason=(
"self-referential and tautological: reassigns a local "
"`content` variable to `new_content` and asserts it equals "
"`new_content` -- true by construction regardless of any "
"production code. The real save path "
"(storage.save_report/get_report_storage) does not "
"expose an isolable 'overwrite' contract at this layer; not "
"attempted here."
)
)
def test_report_generation_existing_file_overwrite(self):
"""Report generation overwrites existing files."""
def test_report_generation_unicode_content_handling(self):
"""Report generation handles Unicode content.
Calls the real ``_extract_synthesized_answer`` (research_service.py)
with unicode content in the 'Final synthesis' finding, instead of
asserting Python string containment against a literal that was
never passed to any production code.
"""
from local_deep_research.web.services.research_service import (
_extract_synthesized_answer,
)
content = (
"# Report with Unicode\n\nTest: 日本語, émojis 🎉, symbols ∑∏∫"
)
results = {
"findings": [{"content": content, "phase": "Final synthesis"}]
}
extracted = _extract_synthesized_answer(results)
assert "日本語" in extracted
assert "🎉" in extracted
assert "∑" in extracted
class TestNewsSearchIntegration:
"""Tests for news search integration in research."""
def test_news_search_headline_generation(self):
"""News search generates headlines.
Calls the real ``generate_headline`` (news/utils/headline_generator.py)
-- the function run_research_process actually calls for news
searches (research_service.py's ``is_news_search`` branch) --
instead of asserting a literal string invented by the test.
With no findings, the LLM path short-circuits deterministically
(no LLM call/mocking needed), returning the documented graceful-
failure sentinel rather than raising.
"""
from local_deep_research.news.utils.headline_generator import (
generate_headline,
)
headline = generate_headline("test query", findings="")
assert headline
assert len(headline) < 100
assert headline == "[Headline generation failed]"
def test_news_search_topic_extraction(self):
"""News search extracts topics.
Calls the real ``generate_topics`` (news/utils/topic_generator.py)
instead of a hardcoded local list.
"""
from local_deep_research.news.utils.topic_generator import (
generate_topics,
)
topics = generate_topics("test query", findings="")
assert len(topics) > 0
assert all(isinstance(t, str) for t in topics)
def test_news_search_llm_failure_graceful_degradation(self):
"""News search degrades gracefully on LLM failure.
``generate_headline`` returns a sentinel string on failure
rather than raising or returning None -- confirmed via the real
function with no findings (the same deterministic no-LLM-call
path as test_news_search_headline_generation), instead of a
local try/except around a manually-raised exception.
"""
from local_deep_research.news.utils.headline_generator import (
generate_headline,
)
headline = generate_headline("test query", findings="")
assert headline is not None
assert headline == "[Headline generation failed]"
@pytest.mark.skip(
reason=(
"self-referential: builds a `metadata` dict with a "
"`subscription_id` key and asserts on that same dict -- no "
"production code involved. Subscription-status updates live "
"in the news subscription routes/service, not "
"research_service.py; out of scope for this module."
)
)
def test_news_search_subscription_updates(self):
"""News search updates subscription status."""
@pytest.mark.skip(
reason=(
"self-referential and duplicates coverage that already exists "
"for real: `bool(results.get('findings'))` on a locally-built "
"dict never reaches production code. The real "
"empty-results-in-quick-mode behavior is already pinned by "
"TestQuickModeSynthesis.test_quick_mode_synthesis_empty_"
"results_handling in this file, via the real "
"run_research_process path."
)
)
def test_news_search_empty_results_handling(self):
"""News search handles empty results."""
@pytest.mark.skip(
reason=(
"self-referential: asserts on a `rate_limits` dict built "
"inside the test body. News-search rate limiting is enforced "
"by the rate-limiter middleware/dependency (see "
"tests/security/test_rate_limiter_fastapi.py), not by "
"research_service.py; no matching production symbol here."
)
)
def test_news_search_rate_limiting_integration(self):
"""News search respects rate limits."""
@pytest.mark.skip(
reason=(
"self-referential: asserts on a `cached_results` dict built "
"inside the test body. There is no cache layer in "
"research_service.py's news path to bind this to -- news "
"result caching, if any, lives in the news search-engine/"
"service layer, out of scope for this module."
)
)
def test_news_search_cache_integration(self):
"""News search uses cache."""
@pytest.mark.skip(
reason=(
"self-referential: asserts on a `metadata` dict built and "
"queried inside the test body. The real news-metadata "
"handling (`is_news_search`/`search_type` driving the "
"headline/topic-generation branch) lives inline in "
"run_research_process (research_service.py ~2170) and would "
"need the full quick-mode harness plus a news-shaped results "
"dict to exercise meaningfully; not attempted here."
)
)
def test_news_search_metadata_storage(self):
"""News search stores metadata correctly."""
class TestCitationFormatting:
"""Tests for citation formatting in reports."""
@patch("local_deep_research.config.search_config.get_setting_from_snapshot")
def test_citation_formatter_domain_id_hyperlinks(self, mock_get_setting):
"""Citation formatter handles domain_id_hyperlinks mode."""
from local_deep_research.web.services.research_service import (
get_citation_formatter,
)
from local_deep_research.text_optimization import CitationMode
mock_get_setting.return_value = "domain_id_hyperlinks"
formatter = get_citation_formatter()
assert formatter.mode == CitationMode.DOMAIN_ID_HYPERLINKS
@patch("local_deep_research.config.search_config.get_setting_from_snapshot")
def test_citation_formatter_domain_id_always_hyperlinks(
self, mock_get_setting
):
"""Citation formatter handles domain_id_always_hyperlinks mode."""
from local_deep_research.web.services.research_service import (
get_citation_formatter,
)
from local_deep_research.text_optimization import CitationMode
mock_get_setting.return_value = "domain_id_always_hyperlinks"
formatter = get_citation_formatter()
assert formatter.mode == CitationMode.DOMAIN_ID_ALWAYS_HYPERLINKS
class TestSourceExtraction:
"""Tests for source extraction from search results."""
def test_source_extraction_from_findings(self):
"""Sources are extracted from findings correctly."""
from local_deep_research.utilities.search_utilities import (
extract_links_from_search_results,
)
search_results = [
{"link": "https://example.com/1", "title": "Result 1"},
{"link": "https://example.com/2", "title": "Result 2"},
]
links = extract_links_from_search_results(search_results)
assert len(links) == 2
def test_source_extraction_empty_results(self):
"""Source extraction handles empty results."""
from local_deep_research.utilities.search_utilities import (
extract_links_from_search_results,
)
search_results = []
links = extract_links_from_search_results(search_results)
assert links == []
def test_source_extraction_duplicate_links(self):
"""Source extraction does NOT deduplicate at this layer.
The original version of this test invented its own dedup logic
(``set(...)``) and asserted on that, rather than calling
``extract_links_from_search_results``. Calling the real function
shows it does no deduplication of its own -- both entries survive,
one per search result -- which is the actual, current contract:
dedup (if any) is the caller's responsibility. Pinning the real
behavior here instead of the assumed one.
"""
from local_deep_research.utilities.search_utilities import (
extract_links_from_search_results,
)
search_results = [
{"link": "https://example.com", "title": "Result 1"},
{"link": "https://example.com", "title": "Result 2"},
]
links = extract_links_from_search_results(search_results)
assert len(links) == 2
assert all(link["url"] == "https://example.com" for link in links)
class TestExtractSynthesizedAnswer:
"""Pin the contract for _extract_synthesized_answer.
Regression: quick-mode research used to save the full format_findings
blob (synthesized + sources + iteration questions + detailed
findings + ALL SOURCES) into research.report_content because the
quick-mode save site assigned ``clean_markdown =
raw_formatted_findings``. format_document_split only knows ##
Sources headers, so the blob's ## ALL SOURCES / ## DETAILED
FINDINGS sections survived into the chat view (visible in UI
test screenshots after page reload).
"""
def test_prefers_final_synthesis_finding(self):
from local_deep_research.web.services.research_service import (
_extract_synthesized_answer,
)
results = {
"findings": [
{"phase": "search", "content": "raw search hits"},
{"phase": "Final synthesis", "content": "the answer"},
],
"current_knowledge": "wider context",
}
assert _extract_synthesized_answer(results) == "the answer"
def test_falls_back_to_current_knowledge(self):
from local_deep_research.web.services.research_service import (
_extract_synthesized_answer,
)
# No Final synthesis finding (e.g. standard_strategy returns
# current_knowledge as the synthesized content)
results = {
"findings": [{"phase": "search", "content": "raw"}],
"current_knowledge": "the answer",
}
assert _extract_synthesized_answer(results) == "the answer"
def test_returns_empty_when_nothing_available(self):
from local_deep_research.web.services.research_service import (
_extract_synthesized_answer,
)
assert _extract_synthesized_answer({}) == ""
assert _extract_synthesized_answer({"findings": []}) == ""
assert (
_extract_synthesized_answer(
{"findings": [{"phase": "Final synthesis", "content": ""}]}
)
== ""
)
def test_does_not_return_format_findings_blob(self):
"""The whole point: never return the format_findings blob."""
from local_deep_research.web.services.research_service import (
_extract_synthesized_answer,
)
# Simulate the kind of blob format_findings produces — a
# synthesized answer followed by [N] (source nr: N) URL lines
# and the ## ALL SOURCES section. The function should ignore
# this if a clean Final synthesis content is also present.
formatted_blob = (
"Photosynthesis is the process by which plants...\n\n"
"[1] A History of Plant Sci [Q1 *****]\n"
" URL: https://example.com/1\n\n"
"## ALL SOURCES:\n"
"[1] A History of Plant Sci\n"
" URL: https://example.com/1\n\n"
)
results = {
"findings": [
{
"phase": "Final synthesis",
"content": (
"Photosynthesis is the process by which plants..."
),
}
],
"formatted_findings": formatted_blob,
"current_knowledge": (
"Photosynthesis is the process by which plants..."
),
}
out = _extract_synthesized_answer(results)
assert "## ALL SOURCES" not in out
assert "URL: https://example.com" not in out
assert "[1] A History of Plant Sci" not in out
assert out == "Photosynthesis is the process by which plants..."