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.

5.7 KB
Raw Blame
"""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.


@pytest.fixture
def seeded_news_db(tmp_path, request):
    """Real temp-file SQLite seeded with one completed news-style research.

    Its ``report_content`` is ``ANSWER_ONLY`` unless a test passes another
    body through indirect parametrization.
    """
    report_content = getattr(request, "param", ANSWER_ONLY)
    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=report_content,
            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


# A detailed report saved in NO_HYPERLINKS mode before the save site removed
# the generator's appended-sources sentinel: the answer, the sentinel, and
# the Sources block the split kept with the answer.
LEGACY_SENTINEL_BODY = (
    ANSWER_ONLY
    + "\n\n<!-- LDR_APPENDED_SOURCES_START -->\n\n"
    + "## Sources\n\n[1] Quantum Source 1\n   URL: https://q1.example\n"
)


@pytest.mark.parametrize(
    "seeded_news_db", [LEGACY_SENTINEL_BODY], indirect=True, ids=["legacy"]
)
def test_news_feed_findings_drop_the_sources_sentinel(seeded_news_db):
    """The sentinel is an HTML comment, which the card's renderMarkdown()
    shows as literal text, so it must not reach the findings."""
    findings = _feed_item(seeded_news_db)["findings"]
    assert "LDR_APPENDED_SOURCES_START" not in findings
    assert findings.startswith(ANSWER_ONLY)
    assert "URL: https://q1.example" 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")