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.

2.8 KB
Raw Blame
"""Regression tests for issue #4615 in news_analyzer.py.

The file already imported ``get_llm_response_text`` and used it when parsing
news items; the three narrative generators still read ``response.content``
directly and called ``.strip()`` on it. With Anthropic-style list content that
raised AttributeError inside a broad ``except``, so each generator returned an
empty result instead of the model's text.
"""

import asyncio
from unittest.mock import AsyncMock, Mock, patch

from local_deep_research.news.core.news_analyzer import NewsAnalyzer


NEWS_ITEMS = [
    {
        "headline": "Headline one",
        "summary": "Summary one",
        "category": "tech",
        "impact_level": "high",
        "topics": ["agents"],
    }
]


def _analyzer(text):
    response = Mock()
    response.content = [{"type": "text", "text": text}]
    analyzer = NewsAnalyzer(llm_client=AsyncMock())
    analyzer.llm_client.ainvoke.return_value = response
    return analyzer


def test_generate_big_picture_returns_model_text():
    analyzer = _analyzer("The broader trend is consolidation.")

    assert (
        asyncio.run(analyzer.generate_big_picture(NEWS_ITEMS))
        == "The broader trend is consolidation."
    )


def test_generate_watch_for_splits_lines():
    analyzer = _analyzer("- First signal\n- Second signal")

    watch_for = asyncio.run(analyzer.generate_watch_for(NEWS_ITEMS))

    assert any("First signal" in entry for entry in watch_for)
    assert any("Second signal" in entry for entry in watch_for)


def test_generate_patterns_returns_model_text():
    analyzer = _analyzer("Two unrelated stories share a supply chain cause.")

    assert (
        asyncio.run(analyzer.generate_patterns(NEWS_ITEMS))
        == "Two unrelated stories share a supply chain cause."
    )


def test_topic_generation_preserves_analyzer_settings_snapshot():
    snapshot = {"llm.provider": "openai", "policy.egress_scope": "public_only"}
    analyzer = NewsAnalyzer(llm_client=Mock(), settings_snapshot=snapshot)
    with patch(
        "local_deep_research.news.core.news_analyzer.generate_topics_async",
        new_callable=AsyncMock,
        return_value=["configured topic"],
    ) as topics:
        result = asyncio.run(
            analyzer.extract_topics(
                [
                    {
                        "headline": "H",
                        "summary": "S",
                        "category": "C",
                        "id": "item",
                    }
                ]
            )
        )
    topics.assert_awaited_once_with(
        query="H",
        findings="S",
        category="C",
        max_topics=3,
        settings_snapshot=snapshot,
    )
    assert topics.await_args.kwargs["settings_snapshot"] is snapshot
    assert result[0]["name"] == "configured topic"