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.

7.6 KB
Raw Blame
"""
Path separation for the news headline/topic generators.

The synchronous entry points (``generate_headline`` / ``generate_topics``)
must reach the LLM through ``invoke`` and must never create an event loop:
langchain caches one async HTTP client per process, keyed only on
base_url/timeout, and that client stays bound to the loop that first used
it. Driving the async core on a throwaway ``asyncio.run`` loop therefore
leaves a closed-loop keep-alive connection behind, and the provider SDK
silently re-sends the request on the next call.

The async cores (``*_async``) are the mirror image: they must use
``ainvoke`` and never fall back to the blocking ``invoke``.
"""

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

import pytest

from local_deep_research.news.utils.headline_generator import (
    generate_headline,
    generate_headline_async,
)
from local_deep_research.news.utils.topic_generator import (
    generate_topics,
    generate_topics_async,
)


def _dual_llm(sync_content: str, async_content: str) -> MagicMock:
    """An LLM exposing both interfaces so either one could be chosen."""
    llm = MagicMock()
    llm.invoke.return_value = Mock(content=sync_content)
    llm.ainvoke = AsyncMock(return_value=Mock(content=async_content))
    return llm


class _EventLoopCreated(AssertionError):
    """Raised when the code under test tries to start an event loop."""


def _no_event_loop():
    """Patchers that make any attempt to start an event loop fail loudly."""
    boom = Mock(
        side_effect=_EventLoopCreated(
            "the synchronous path must not create an event loop"
        )
    )
    return (
        patch("asyncio.run", boom),
        patch("asyncio.new_event_loop", boom),
    )


class TestHeadlineSyncPath:
    """generate_headline() stays on invoke and starts no event loop."""

    def test_uses_invoke_and_not_ainvoke(self):
        llm = _dual_llm("Sync Headline", "Async Headline")

        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            result = generate_headline(query="q", findings="some findings")

        assert result == "Sync Headline"
        llm.invoke.assert_called_once()
        llm.ainvoke.assert_not_called()

    def test_starts_no_event_loop(self):
        # spec without "ainvoke": the sync path may not even look for it.
        llm = Mock(spec=["invoke", "close"])
        llm.invoke.return_value = Mock(content="Loop-free Headline")

        run_patch, new_loop_patch = _no_event_loop()
        with (
            patch(
                "local_deep_research.config.llm_config.get_llm",
                return_value=llm,
            ),
            run_patch,
            new_loop_patch,
        ):
            result = generate_headline(query="q", findings="some findings")

        assert result == "Loop-free Headline"

    def test_event_loop_guard_is_armed(self):
        """The guard used above really does fire on asyncio.run()."""

        async def _noop():
            return None

        coro = _noop()
        run_patch, _ = _no_event_loop()
        try:
            with run_patch, pytest.raises(_EventLoopCreated):
                asyncio.run(coro)
        finally:
            coro.close()


class TestHeadlineAsyncPath:
    """generate_headline_async() stays on ainvoke."""

    def test_uses_ainvoke_and_not_invoke(self):
        llm = _dual_llm("Sync Headline", "Async Headline")

        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            result = asyncio.run(
                generate_headline_async(query="q", findings="some findings")
            )

        assert result == "Async Headline"
        llm.ainvoke.assert_awaited_once()
        llm.invoke.assert_not_called()


class TestTopicsSyncPath:
    """generate_topics() stays on invoke and starts no event loop."""

    def test_uses_invoke_and_not_ainvoke(self):
        llm = _dual_llm('["Sync Topic"]', '["Async Topic"]')

        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            result = generate_topics(query="q", findings="findings")

        assert result == ["sync topic"]
        llm.invoke.assert_called_once()
        llm.ainvoke.assert_not_called()

    def test_starts_no_event_loop(self):
        llm = Mock(spec=["invoke", "close"])
        llm.invoke.return_value = Mock(content='["Loop Free"]')

        run_patch, new_loop_patch = _no_event_loop()
        with (
            patch(
                "local_deep_research.config.llm_config.get_llm",
                return_value=llm,
            ),
            run_patch,
            new_loop_patch,
        ):
            result = generate_topics(query="q", findings="findings")

        assert result == ["loop free"]


class TestTopicsAsyncPath:
    """generate_topics_async() stays on ainvoke."""

    def test_uses_ainvoke_and_not_invoke(self):
        llm = _dual_llm('["Sync Topic"]', '["Async Topic"]')

        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            result = asyncio.run(
                generate_topics_async(query="q", findings="findings")
            )

        assert result == ["async topic"]
        llm.ainvoke.assert_awaited_once()
        llm.invoke.assert_not_called()


class TestFailureMarkersUnchanged:
    """The sync entry points, and the async headline twin, keep their
    documented failure markers."""

    def test_headline_failure_marker(self):
        llm = Mock(spec=["invoke", "close"])
        llm.invoke.side_effect = RuntimeError("provider down")

        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            assert (
                generate_headline(query="q", findings="f")
                == "[Headline generation failed]"
            )

    def test_topics_failure_marker(self):
        llm = Mock(spec=["invoke", "close"])
        llm.invoke.side_effect = RuntimeError("provider down")

        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            assert generate_topics(query="q", findings="f") == [
                "[topic generation failed]"
            ]

    def test_headline_async_failure_marker(self):
        """The async twin must apply the same failure sentinel as the
        sync entry point, not silently return None."""
        llm = Mock(spec=["ainvoke", "close"])
        llm.ainvoke = AsyncMock(side_effect=RuntimeError("provider down"))

        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            result = asyncio.run(
                generate_headline_async(query="q", findings="f")
            )

        assert result == "[Headline generation failed]"


@pytest.mark.parametrize("asynchronous", [False, True])
def test_headline_extracts_only_text_from_content_blocks(asynchronous):
    content = [
        {"type": "thinking", "thinking": "internal reasoning"},
        {"type": "text", "text": '"Clear headline."'},
        {"type": "tool_use", "id": "tool-only", "name": "unused", "input": {}},
    ]
    llm = _dual_llm("", "")
    llm.invoke.return_value = Mock(content=content)
    llm.ainvoke.return_value = Mock(content=content)
    with patch(
        "local_deep_research.config.llm_config.get_llm", return_value=llm
    ):
        result = (
            asyncio.run(generate_headline_async(query="q", findings="report"))
            if asynchronous
            else generate_headline(query="q", findings="report")
        )
    assert result == "Clear headline"