~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 the report_assembly_service module."""
import json
from datetime import datetime, UTC
from types import SimpleNamespace
from unittest.mock import MagicMock
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,
)
from local_deep_research.utilities.search_utilities import (
format_links_to_markdown,
)
from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY
from local_deep_research.web.services.report_assembly_service import (
_build_metrics_markdown,
_build_sources_markdown,
assemble_full_report,
get_research_source_links,
get_research_source_links_batch,
)
@pytest.fixture
def db_session(tmp_path):
"""Fresh per-test SQLite session with all models created."""
db_path = tmp_path / "assembly_test.db"
engine = create_engine(f"sqlite:///{db_path}")
Base.metadata.create_all(engine)
SessionLocal = sessionmaker(bind=engine)
sess = SessionLocal()
yield sess
sess.close()
engine.dispose()
def _mk_research(db_session, **kwargs):
"""Insert a ResearchHistory row with sensible defaults."""
defaults = dict(
id=kwargs.pop("id", "test-research-1"),
query=kwargs.pop("query", "What is X?"),
mode=kwargs.pop("mode", "quick"),
status=kwargs.pop("status", "completed"),
created_at=kwargs.pop("created_at", "2026-04-25T12:00:00+00:00"),
completed_at=kwargs.pop("completed_at", "2026-04-25T12:05:00+00:00"),
report_content=kwargs.pop("report_content", "Answer body."),
research_meta=kwargs.pop("research_meta", None),
)
defaults.update(kwargs)
research = ResearchHistory(**defaults)
db_session.add(research)
db_session.commit()
return research
def _mk_resource(
db_session,
research_id,
*,
url="https://example.com/a",
title="Example",
index="1",
journal_quality=None,
resource_metadata=None,
):
"""Insert a ResearchResource row mirroring save_research_sources's shape."""
if resource_metadata is None:
original_data = {"index": index, "url": url, "title": title}
if journal_quality is not None:
original_data["journal_quality"] = journal_quality
resource_metadata = {"original_data": original_data}
r = ResearchResource(
research_id=research_id,
title=title,
url=url,
source_type="web",
resource_metadata=resource_metadata,
created_at=datetime.now(UTC).isoformat(),
)
db_session.add(r)
db_session.commit()
return r
# ---------------------------------------------------------------------------
# assemble_full_report
# ---------------------------------------------------------------------------
class TestAssembleFullReport:
def test_includes_answer_sources_metrics(self, db_session):
research = _mk_research(
db_session,
report_content="The answer text [1].",
research_meta={
"iterations": 3,
"generated_at": "2026-04-25T12:05:00+00:00",
},
)
_mk_resource(db_session, research.id, url="https://a.com", title="A")
out = assemble_full_report(research, db_session)
assert "The answer text [1]." in out
assert "## Sources" in out
assert "https://a.com" in out
assert "## Research Metrics" in out
assert "Search Iterations: 3" in out
def test_omits_sources_when_no_resources(self, db_session):
research = _mk_research(
db_session,
report_content="Just an answer.",
research_meta={},
completed_at=None,
)
out = assemble_full_report(research, db_session)
# No resources → no Sources section. No metadata + no completed_at
# → no Metrics section. Just the answer.
assert out == "Just an answer."
def test_metrics_appears_when_completed_at_set_even_without_meta(
self, db_session
):
"""completed_at alone is enough to render the Metrics section."""
research = _mk_research(
db_session,
report_content="Answer body.",
research_meta=None,
completed_at="2026-04-25T12:05:00+00:00",
)
out = assemble_full_report(research, db_session)
assert "## Research Metrics" in out
assert "Generated at: 2026-04-25T12:05:00+00:00" in out
def test_omits_metrics_when_no_metadata(self, db_session):
research = _mk_research(
db_session,
report_content="Answer.",
research_meta=None,
completed_at=None,
)
out = assemble_full_report(research, db_session)
# No sources, no metadata, no completed_at → just the answer.
assert out == "Answer."
def test_handles_none_research(self, db_session):
# None research → None return (distinct from "" which means
# "research exists but has no body / sources / metrics yet").
# Callers map None → HTTP 404 and "" → HTTP 200 empty.
assert assemble_full_report(None, db_session) is None
def test_handles_none_resource_metadata(self, db_session):
"""Defensive isinstance check survives a row with metadata=None."""
research = _mk_research(db_session, report_content="x")
# Bypass _mk_resource's auto-built metadata; insert a row with None.
r = ResearchResource(
research_id=research.id,
url="https://nometa.com",
title="No Meta",
source_type="web",
resource_metadata=None,
created_at=datetime.now(UTC).isoformat(),
)
db_session.add(r)
db_session.commit()
out = assemble_full_report(research, db_session)
assert "https://nometa.com" in out
def test_legacy_row_with_inline_sources_not_double_rendered(
self, db_session
):
"""Pre-refactor rows already embed ## Sources in report_content.
Without the legacy-row guard the assembler would append a freshly
built Sources block on top, producing two `## Sources` headings.
"""
legacy_body = (
"The answer body.\n\n"
"## Sources\n\n"
"[1] Old Source (source nr: 1)\n"
" URL: https://old.com\n"
)
research = _mk_research(db_session, report_content=legacy_body)
# Add a structured resource that, without the guard, would render
# as a SECOND `## Sources` block.
r = ResearchResource(
research_id=research.id,
url="https://new.com",
title="New",
source_type="web",
resource_metadata={"original_data": {"index": 1}},
created_at=datetime.now(UTC).isoformat(),
)
db_session.add(r)
db_session.commit()
out = assemble_full_report(research, db_session)
assert out.count("## Sources") == 1
# The legacy body's existing source URL is preserved as-is; the
# structured `https://new.com` is suppressed because the legacy
# block already covers that section.
assert "https://old.com" in out
assert "https://new.com" not in out
def test_legacy_row_drops_the_appended_sources_sentinel(self, db_session):
"""A detailed report saved in NO_HYPERLINKS mode (or after the
citation formatter raised) before the save site removed it carries
the generator's sentinel, an HTML comment that renderMarkdown()
shows as literal text. The assembled report must not include it."""
from local_deep_research.text_optimization.citation_formatter import (
LDR_APPENDED_SOURCES_SENTINEL,
)
legacy_body = (
"The answer body.\n\n"
f"{LDR_APPENDED_SOURCES_SENTINEL}\n\n"
"## Sources\n\n"
"[1] Old Source (source nr: 1)\n"
" URL: https://old.com\n"
)
research = _mk_research(db_session, report_content=legacy_body)
out = assemble_full_report(research, db_session)
assert LDR_APPENDED_SOURCES_SENTINEL not in out
assert out.startswith("The answer body.\n\n")
# Still recognised as a legacy row: one Sources block, the saved one.
assert out.count("## Sources") == 1
assert "https://old.com" in out
def test_legacy_row_with_repeated_source_not_double_rendered(
self, db_session
):
"""A formatter-grouped source can carry multiple citation indices."""
legacy_sources = format_links_to_markdown(
[
{
"title": "Old Source",
"url": "https://old.com",
"index": index,
}
for index in (1, 3)
]
)
assert legacy_sources.startswith("[1, 3]")
legacy_body = "The answer body.\n\n## Sources\n\n" + legacy_sources
research = _mk_research(db_session, report_content=legacy_body)
_mk_resource(
db_session,
research.id,
url="https://new.com",
title="New",
)
out = assemble_full_report(research, db_session)
assert out.count("## Sources") == 1
assert "https://old.com" in out
assert "https://new.com" not in out
def test_legacy_row_with_indexless_source_not_double_rendered(
self, db_session
):
"""A persisted formatter row can have an empty citation index."""
legacy_sources = format_links_to_markdown(
[
{
"title": "Index-less Old Source",
"url": "https://old.com",
}
]
)
assert legacy_sources.startswith(
"[] Index-less Old Source (source nr: )\n URL: https://old.com"
)
legacy_body = "The answer body.\n\n## Sources\n\n" + legacy_sources
research = _mk_research(db_session, report_content=legacy_body)
_mk_resource(
db_session,
research.id,
url="https://new.com",
title="New",
)
out = assemble_full_report(research, db_session)
assert out.count("## Sources") == 1
assert "https://old.com" in out
assert "https://new.com" not in out
def test_legacy_row_with_inline_metrics_not_double_rendered(
self, db_session
):
"""Same guard for the `## Research Metrics` section."""
legacy_body = "Body.\n\n## Research Metrics\n- Generated at: 2025-01-01"
research = _mk_research(
db_session,
report_content=legacy_body,
research_meta={"iterations": 7},
)
out = assemble_full_report(research, db_session)
assert out.count("## Research Metrics") == 1
# Iteration count from the new path is NOT appended because the
# legacy block already owns that section.
assert (
"iterations" not in out.lower().replace("research metrics", "")
or "7" not in out
)
def test_inline_sources_in_prose_does_not_trigger_legacy_guard(
self, db_session
):
"""Regression for the loose-substring trap: prose that mentions
``## Sources`` mid-line must NOT trip the legacy guard (line-
anchored regex). Otherwise a new-row LLM answer that quotes a
markdown snippet would lose its structured Sources block."""
body = (
"Use a markdown heading like ## Sources to list references "
"(this is just an explanation of markdown)."
)
research = _mk_research(db_session, report_content=body)
r = ResearchResource(
research_id=research.id,
url="https://kept.com",
title="Kept",
source_type="web",
resource_metadata={"original_data": {"index": 1}},
created_at=datetime.now(UTC).isoformat(),
)
db_session.add(r)
db_session.commit()
out = assemble_full_report(research, db_session)
# The structured Sources block should still render even though the
# body contains the substring `## Sources`.
assert "https://kept.com" in out
# The literal `## Sources` from prose plus the appended header
# gives count == 2 — both legitimate occurrences.
assert out.count("## Sources") == 2
def test_placeholder_sources_heading_does_not_trigger_legacy_guard(
self, db_session
):
"""An LLM placeholder heading is not an embedded bibliography."""
body = (
"Section 1 prose [[1]](https://example.com/a).\n\n"
"## Sources\n"
"[Note: The framework will append the consolidated "
"'## Sources' block after this subsection.]\n\n"
"Section 2 prose [[2]](https://example.com/b).\n\n"
"## Sources\n"
"[Note: The framework will append a consolidated "
"'## Sources' block to the entire report.]\n\n"
"Section 3 prose [[3]](https://example.com/c)."
)
research = _mk_research(db_session, report_content=body)
_mk_resource(
db_session,
research.id,
url="https://structured.example/source",
title="Structured source",
)
out = assemble_full_report(research, db_session)
assert "https://structured.example/source" in out
assert out.count("\n## Sources\n") == 3
assert out.rfind("\n## Sources\n") > out.index("Section 3 prose")
def test_falls_back_to_row_order_when_index_missing(
self, db_session, caplog
):
research = _mk_research(db_session, report_content="x")
# Insert two resources where original_data has no index.
for url in ("https://a.com", "https://b.com"):
r = ResearchResource(
research_id=research.id,
url=url,
title=url,
source_type="web",
resource_metadata={"original_data": {}},
created_at=datetime.now(UTC).isoformat(),
)
db_session.add(r)
db_session.commit()
# Capture loguru output by intercepting via the standard logger.
# _build_sources_markdown logs at DEBUG so we just verify no
# crash and both rows render.
out = assemble_full_report(research, db_session)
assert "https://a.com" in out
assert "https://b.com" in out
# ---------------------------------------------------------------------------
# _build_metrics_markdown
# ---------------------------------------------------------------------------
class TestBuildMetricsMarkdown:
@pytest.mark.parametrize("json_encoded", [False, True])
def test_renders_iterations_and_generated_at(
self, db_session, json_encoded
):
metadata = {"iterations": 5, "generated_at": "2026-01-01"}
research = _mk_research(
db_session,
research_meta=json.dumps(metadata) if json_encoded else metadata,
)
md = _build_metrics_markdown(research)
assert "Search Iterations: 5" in md
assert "Generated at: 2026-01-01" in md
def test_falls_back_to_completed_at_when_generated_at_missing(
self, db_session
):
research = _mk_research(
db_session,
research_meta={"iterations": 2},
completed_at="2026-02-02T00:00:00+00:00",
)
md = _build_metrics_markdown(research)
assert "Generated at: 2026-02-02T00:00:00+00:00" in md
@pytest.mark.parametrize("metadata", [None, "not-json", "[]", [], True])
def test_returns_empty_when_no_data(self, db_session, metadata):
research = _mk_research(
db_session, research_meta=metadata, completed_at=None
)
assert _build_metrics_markdown(research) == ""
# ---------------------------------------------------------------------------
# _build_sources_markdown
# ---------------------------------------------------------------------------
class TestBuildSourcesMarkdown:
def test_uses_original_index_when_present(self, db_session):
research = _mk_research(db_session)
_mk_resource(
db_session, research.id, url="https://x.com", title="X", index="3"
)
md = _build_sources_markdown(research, db_session)
assert "[3]" in md
assert "https://x.com" in md
def test_returns_empty_when_no_resources(self, db_session):
research = _mk_research(db_session)
assert _build_sources_markdown(research, db_session) == ""
def test_index_zero_is_not_treated_as_missing(self, db_session):
"""Citation index 0 is a valid value; only None / '' triggers fallback."""
research = _mk_research(db_session)
_mk_resource(
db_session, research.id, url="https://z.com", title="Z", index=0
)
md = _build_sources_markdown(research, db_session)
assert "[0]" in md or "0]" in md # exact rendering may vary
def test_saved_row_renders_the_fetched_page_title(self, db_session):
"""The ``title`` column holds the spelling the search engine
scraped; the page's own is in ``original_data``, where the collector
recorded it. Rendering from the row took the column.
"""
research = _mk_research(db_session)
listing = "A comprehensive guide to X | 2026 | Archive"
_mk_resource(
db_session,
research.id,
url="https://x.test/doc",
title=listing,
resource_metadata={
"original_data": {
"index": "1",
"url": "https://x.test/doc",
"title": listing,
FETCHED_TITLE_KEY: "X - Wikipedia",
}
},
)
md = _build_sources_markdown(research, db_session)
assert "X - Wikipedia" in md
assert "comprehensive guide" not in md
def test_saved_row_without_the_key_keeps_its_title(self, db_session):
"""Which is every row written before this change."""
research = _mk_research(db_session)
_mk_resource(
db_session, research.id, url="https://y.test/d", title="Y title"
)
assert "Y title" in _build_sources_markdown(research, db_session)
# ---------------------------------------------------------------------------
# get_research_source_links
# ---------------------------------------------------------------------------
class TestGetResearchSourceLinks:
def test_returns_top_n_in_row_order(self, db_session):
research = _mk_research(db_session)
for i, url in enumerate(
["https://a.com", "https://b.com", "https://c.com", "https://d.com"]
):
_mk_resource(
db_session,
research.id,
url=url,
title=f"T{i}",
index=str(i + 1),
)
links = get_research_source_links(research.id, db_session, limit=3)
assert len(links) == 3
assert [link["url"] for link in links] == [
"https://a.com",
"https://b.com",
"https://c.com",
]
def test_returns_empty_when_no_resources(self, db_session):
research = _mk_research(db_session)
assert get_research_source_links(research.id, db_session) == []
def test_skips_non_http_urls(self, db_session):
research = _mk_research(db_session)
_mk_resource(db_session, research.id, url="ftp://nope.com", title="X")
_mk_resource(
db_session,
research.id,
url="https://yes.com",
title="Y",
index="2",
)
links = get_research_source_links(research.id, db_session)
assert [link["url"] for link in links] == ["https://yes.com"]
def test_limit_counts_distinct_sources_not_rows(self, db_session):
"""Three rows for one URL must not fill a top-3 card with one source.
A strategy stores one ``research_resources`` row per piece of
evidence, so one URL legitimately appears several times with
different snippets (#5894). Before the dedup this returned the
SAME url three times — zero diversity in a "top 3 sources" card.
"""
research = _mk_research(db_session)
for i in range(3):
_mk_resource(
db_session,
research.id,
url="https://dup.com/page",
title=f"Dup {i}",
index="1",
)
for i, url in enumerate(["https://b.com", "https://c.com"], start=2):
_mk_resource(
db_session, research.id, url=url, title=f"T{i}", index=str(i)
)
links = get_research_source_links(research.id, db_session, limit=3)
assert [link["url"] for link in links] == [
"https://dup.com/page",
"https://b.com",
"https://c.com",
]
# First occurrence wins, so the title stays the one the earliest
# row carried rather than the last duplicate's.
assert links[0]["title"] == "Dup 0"
def test_dedup_uses_the_canonical_key_the_bibliography_groups_by(
self, db_session
):
"""Noisy spellings of one URL are one source, exactly as in Sources.
Grouping on the raw string instead would let a ``utm_``-tagged or
fragment-bearing spelling of a page count as a second source here
while ``format_links_to_markdown`` renders it as one line.
"""
research = _mk_research(db_session)
for i, url in enumerate(
[
"https://a.com/page",
"https://A.com/page/?utm_source=news",
"https://a.com/page#section",
"https://d.com/page",
],
start=1,
):
_mk_resource(
db_session, research.id, url=url, title=f"T{i}", index=str(i)
)
links = get_research_source_links(research.id, db_session, limit=3)
assert [link["url"] for link in links] == [
"https://a.com/page",
"https://d.com/page",
]
def test_falls_back_to_domain_when_title_missing(self, db_session):
research = _mk_research(db_session)
r = ResearchResource(
research_id=research.id,
url="https://www.foo.com/page",
title="",
source_type="web",
resource_metadata={"original_data": {"index": "1"}},
created_at=datetime.now(UTC).isoformat(),
)
db_session.add(r)
db_session.commit()
links = get_research_source_links(research.id, db_session)
assert links[0]["title"] == "foo.com"
# ---------------------------------------------------------------------------
# get_research_source_links_batch
# ---------------------------------------------------------------------------
class TestGetResearchSourceLinksBatch:
def test_groups_links_by_research_id(self, db_session):
r1 = _mk_research(db_session, id="r1")
r2 = _mk_research(db_session, id="r2")
_mk_resource(db_session, r1.id, url="https://r1a.com", title="r1a")
_mk_resource(db_session, r2.id, url="https://r2a.com", title="r2a")
_mk_resource(db_session, r2.id, url="https://r2b.com", title="r2b")
batch = get_research_source_links_batch(["r1", "r2"], db_session)
assert len(batch["r1"]) == 1
assert len(batch["r2"]) == 2
assert batch["r1"][0]["url"] == "https://r1a.com"
def test_empty_input_returns_empty_dict(self, db_session):
assert get_research_source_links_batch([], db_session) == {}
def test_research_with_no_resources_maps_to_empty_list(self, db_session):
_mk_research(db_session, id="empty-r")
batch = get_research_source_links_batch(["empty-r"], db_session)
assert batch == {"empty-r": []}
def test_limit_counts_distinct_sources_per_research(self, db_session):
"""Batch dedups like its single-research sibling, and per research.
The seen-set must be scoped to one research id: a URL cited by two
different researches has to survive in both buckets.
"""
r1 = _mk_research(db_session, id="dr1")
r2 = _mk_research(db_session, id="dr2")
for i in range(3):
_mk_resource(
db_session, r1.id, url="https://dup.com/p", title=f"D{i}"
)
_mk_resource(db_session, r1.id, url="https://other.com/p", title="O")
_mk_resource(db_session, r2.id, url="https://dup.com/p", title="R2")
batch = get_research_source_links_batch(["dr1", "dr2"], db_session)
assert [link["url"] for link in batch["dr1"]] == [
"https://dup.com/p",
"https://other.com/p",
]
# Not suppressed by dr1 having already seen this URL.
assert [link["url"] for link in batch["dr2"]] == ["https://dup.com/p"]
def test_unlimited_still_dedups(self, db_session):
"""``limit=None`` (the report API) uncaps the count, not the dedup."""
r = _mk_research(db_session, id="unl")
for i in range(4):
_mk_resource(db_session, r.id, url="https://one.com/p", title="X")
batch = get_research_source_links_batch(["unl"], db_session, limit=None)
assert [link["url"] for link in batch["unl"]] == ["https://one.com/p"]
def test_respects_limit_per_research(self, db_session):
r = _mk_research(db_session)
for i, url in enumerate(
["https://a.com", "https://b.com", "https://c.com"]
):
_mk_resource(
db_session, r.id, url=url, title=f"T{i}", index=str(i + 1)
)
batch = get_research_source_links_batch([r.id], db_session, limit=2)
assert len(batch[r.id]) == 2
# ---------------------------------------------------------------------------
# Non-string row values (regression: #5900 / #5894)
# ---------------------------------------------------------------------------
class _FakeQuery:
"""Minimal stand-in for the query chain both source-link helpers build.
A real SQLite session cannot carry these rows: the point of the tests
below is a ``url`` that is not a string, which the column would coerce
or reject before the code under test ever saw it.
"""
def __init__(self, rows):
self._rows = rows
def filter(self, *args, **kwargs):
return self
def filter_by(self, *args, **kwargs):
return self
def order_by(self, *args, **kwargs):
return self
def limit(self, *args, **kwargs):
return self
def all(self):
return list(self._rows)
def __iter__(self):
# get_research_source_links walks the query lazily rather than
# calling .all(), so both access shapes must work.
return iter(self._rows)
class _FakeSession:
def __init__(self, rows):
self._rows = rows
def query(self, *args, **kwargs):
return _FakeQuery(self._rows)
def _row(url, title="T", research_id="r1"):
return SimpleNamespace(research_id=research_id, url=url, title=title)
class TestSourceLinksNonStringValues:
"""A row whose ``url`` is not a ``str`` must be skipped, never raise.
``_dedup_key`` -> ``canonical_url_key`` -> ``_parse_library_citation``
unpacks ``str.partition("#")`` into three names. Handed a non-str that
merely *looks* truthy and string-like, that unpack raises
``ValueError: not enough values to unpack (expected 3, got 0)``. In the
news feed this happened inside a blanket ``except Exception`` that
re-raised it as a ``DatabaseAccessException``, hiding the real defect.
"""
def test_magicmock_row_is_skipped_not_fatal(self):
"""The exact shape the news API tests supply.
``mock_session.query(...)`` is stubbed once and reused, so
``get_research_source_links_batch`` receives auto-specced
``MagicMock`` rows whose ``.url`` responds truthily to ``strip``
and ``startswith`` but is not a string.
"""
row = MagicMock()
row.research_id = "r1"
# .url and .title deliberately left as auto-created attributes.
batch = get_research_source_links_batch(["r1"], _FakeSession([row]))
assert batch == {"r1": []}
@pytest.mark.parametrize("bad_url", [12345, None, object(), b"https://x"])
def test_non_string_url_skipped_good_rows_survive(self, bad_url):
rows = [_row(bad_url), _row("https://ok.com/x", title="OK")]
batch = get_research_source_links_batch(["r1"], _FakeSession(rows))
assert batch["r1"] == [{"url": "https://ok.com/x", "title": "OK"}]
def test_non_string_title_falls_back_to_domain(self):
"""A bad title must not put a repr on a news card."""
batch = get_research_source_links_batch(
["r1"], _FakeSession([_row("https://ok.com/x", title=object())])
)
assert batch["r1"] == [{"url": "https://ok.com/x", "title": "ok.com"}]
def test_single_research_variant_guards_too(self):
"""The unbatched helper shares ``_format_source_link``; assert it."""
rows = [_row(MagicMock()), _row("https://ok.com/x", title="OK")]
links = get_research_source_links("r1", _FakeSession(rows), limit=3)
assert links == [{"url": "https://ok.com/x", "title": "OK"}]
class TestUncitedSourcesFiltering:
"""assemble_full_report forwards the answer body so uncited rows
leave the Sources block (#5379)."""
def test_assemble_forwards_body_to_filter(self, db_session, monkeypatch):
"""Regression: dropping body= would silently disable filtering."""
import local_deep_research.web.services.report_assembly_service as ras
captured = {}
def mock_format(all_links, prose=None, uncited_mode="fallback"):
captured["prose"] = prose
captured["uncited_mode"] = uncited_mode
return "formatted-sources"
monkeypatch.setattr(ras, "format_links_to_markdown", mock_format)
research = _mk_research(db_session, report_content="Answer [1].")
_mk_resource(db_session, research.id)
assemble_full_report(research, db_session)
assert captured["prose"] == "Answer [1]."
assert captured["uncited_mode"] == "fallback"
def test_assemble_filters_uncited_resource_from_body(
self, db_session, monkeypatch
):
"""End to end through the real filter."""
monkeypatch.setattr(
"local_deep_research.config.thread_settings.get_setting_from_snapshot",
lambda key, default=None, **kwargs: default,
)
research = _mk_research(db_session, report_content="Cited claim [1].")
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
_mk_resource(
db_session,
research.id,
url="https://uncited.test/b",
title="B",
index="2",
)
out = assemble_full_report(research, db_session)
assert "https://cited.test/a" in out
assert "https://uncited.test/b" not in out
def test_assemble_disabled_mode_keeps_all(self, db_session):
"""Saved ``disabled`` is honored with no worker settings context.
Previously ``_build_sources_markdown`` resolved the mode from the
thread context only, so a report viewed/exported outside the
research worker always fell back to filtering even when the user
saved ``disabled``. Mocking the settings getter (as before) masks
that gap, so this uses the row's own ``settings_snapshot``.
"""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
research = _mk_research(
db_session,
report_content="Cited claim [1].",
research_meta={
"settings_snapshot": {
"report.uncited_sources_mode": {"value": "disabled"}
}
},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
_mk_resource(
db_session,
research.id,
url="https://uncited.test/b",
title="B",
index="2",
)
out = assemble_full_report(research, db_session)
assert "https://cited.test/a" in out
assert "https://uncited.test/b" in out
def test_assemble_strict_mode_saved_drops_uncited(self, db_session):
"""Saved ``strict`` is honored with no worker settings context.
``No citations here.`` parses to nothing: ``fallback`` would keep
the full bibliography while ``strict`` drops it. Asserting the
drop proves the saved preference — not the thread-context
fallback — decided the render.
"""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={
"settings_snapshot": {
"report.uncited_sources_mode": {"value": "strict"}
}
},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
_mk_resource(
db_session,
research.id,
url="https://uncited.test/b",
title="B",
index="2",
)
out = assemble_full_report(research, db_session)
assert "https://cited.test/a" not in out
assert "https://uncited.test/b" not in out
assert "## Sources" not in out
def test_assemble_chat_row_backfilled_snapshot_honors_strict(
self, db_session
):
"""Chat/follow-up rows gain view/export parity via back-fill (#6747).
Those creation paths store only ``{"submission": ...}``; the
completion merge back-fills the run's snapshot. Simulating that
merge here proves a chat-shaped row renders ``strict`` instead
of the ``fallback`` it rendered before the fix.
"""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
from local_deep_research.web.services.research_service import (
_ensure_settings_snapshot,
_parse_research_metadata,
)
clear_settings_context()
chat_meta = {
"submission": {
"chat_session_id": "sess",
"message_id": "msg",
"research_mode": "quick",
}
}
worker_snapshot = {"report.uncited_sources_mode": {"value": "strict"}}
merged = _parse_research_metadata(chat_meta)
_ensure_settings_snapshot(merged, worker_snapshot)
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta=merged,
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
out = assemble_full_report(research, db_session)
assert "## Sources" not in out
assert "https://cited.test/a" not in out
def _mk_setting(db_session, key, value, ui_element="select"):
"""Insert one row into the ``settings`` table (user's current prefs)."""
from local_deep_research.database.models import Setting, SettingType
row = Setting(
key=key,
value=value,
type=SettingType.REPORT,
name="Test setting",
ui_element=ui_element,
)
db_session.add(row)
db_session.commit()
return row
class TestLegacySnapshotlessRows:
"""Legacy rows without a saved mode use the requester's setting.
Chat/follow-up research created AND completed before the snapshot
back-fill keeps ``{"submission": ...}`` forever — no snapshot can be
recovered for those rows. Older snapshots may also lack this newer
setting. View/export resolves both from the requesting user's current
saved preference instead of silently rendering ``fallback``.
"""
def test_snapshotless_row_honors_current_saved_strict(self, db_session):
"""Legacy row + saved ``strict`` drops uncited sources on view."""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
_mk_setting(db_session, "report.uncited_sources_mode", "strict")
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={
"submission": {
"chat_session_id": "sess",
"message_id": "msg",
"research_mode": "quick",
}
},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
out = assemble_full_report(research, db_session)
assert "## Sources" not in out
assert "https://cited.test/a" not in out
@pytest.mark.parametrize(
"snapshot",
[{}, {"llm.model": {"value": "old-model"}}],
ids=["empty", "unrelated-setting"],
)
@pytest.mark.parametrize("current_mode", ["disabled", "strict"])
def test_legacy_snapshot_without_mode_uses_current_preference(
self, db_session, snapshot, current_mode
):
"""A pre-setting snapshot is not an override for this setting."""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
_mk_setting(db_session, "report.uncited_sources_mode", current_mode)
research = _mk_research(
db_session,
report_content=(
"Cited claim [1]."
if current_mode == "disabled"
else "No citations here."
),
research_meta={"settings_snapshot": snapshot},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
_mk_resource(
db_session,
research.id,
url="https://uncited.test/b",
title="B",
index="2",
)
out = assemble_full_report(research, db_session)
if current_mode == "disabled":
assert "https://uncited.test/b" in out
else:
assert "## Sources" not in out
def test_explicit_snapshot_without_mode_preserves_row_mode(
self, db_session
):
"""An unrelated caller snapshot must not hide a stored mode."""
_mk_setting(db_session, "report.uncited_sources_mode", "disabled")
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={
"settings_snapshot": {
"report.uncited_sources_mode": {"value": "strict"}
}
},
)
_mk_resource(db_session, research.id, url="https://cited.test/a")
out = assemble_full_report(
research,
db_session,
settings_snapshot={"llm.model": {"value": "new-model"}},
)
assert "## Sources" not in out
def test_explicit_snapshot_mode_overrides_row_and_current(self, db_session):
"""A caller that actually supplies the mode keeps highest priority."""
_mk_setting(db_session, "report.uncited_sources_mode", "strict")
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={
"settings_snapshot": {
"report.uncited_sources_mode": {"value": "strict"}
}
},
)
_mk_resource(db_session, research.id, url="https://cited.test/a")
out = assemble_full_report(
research,
db_session,
settings_snapshot={
"report.uncited_sources_mode": {"value": "disabled"}
},
)
assert "https://cited.test/a" in out
def test_snapshotless_row_without_saved_setting_renders_fallback(
self, db_session
):
"""No snapshot and no saved value keeps the legacy render."""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
# Unrelated row: keeps the settings table non-empty (no default
# seeding) while leaving the mode itself unset.
_mk_setting(db_session, "report.citation_format", "number_hyperlinks")
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={"submission": {}},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
out = assemble_full_report(research, db_session)
assert "## Sources" in out
assert "https://cited.test/a" in out
def test_row_snapshot_beats_current_saved_setting(self, db_session):
"""A persisted snapshot stays generation-faithful over a newer pref."""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
_mk_setting(db_session, "report.uncited_sources_mode", "strict")
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={
"settings_snapshot": {
"report.uncited_sources_mode": {"value": "disabled"}
}
},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
out = assemble_full_report(research, db_session)
assert "## Sources" in out
assert "https://cited.test/a" in out
def test_malformed_current_setting_degrades_to_fallback(self, db_session):
"""A hand-edited current value can never wipe a bibliography."""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
_mk_setting(db_session, "report.uncited_sources_mode", "legacy")
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={"submission": {}},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
out = assemble_full_report(research, db_session)
assert "## Sources" in out
assert "https://cited.test/a" in out
def test_explicit_mode_beats_current_saved_setting(self, db_session):
"""Callers passing ``uncited_mode`` still win over the DB value."""
from local_deep_research.config.thread_settings import (
clear_settings_context,
)
clear_settings_context()
_mk_setting(db_session, "report.uncited_sources_mode", "strict")
research = _mk_research(
db_session,
report_content="No citations here.",
research_meta={"submission": {}},
)
_mk_resource(
db_session,
research.id,
url="https://cited.test/a",
title="A",
index="1",
)
out = assemble_full_report(
research, db_session, uncited_mode="disabled"
)
assert "## Sources" in out
assert "https://cited.test/a" in out