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.

40.9 KB
Raw Blame
"""
Tests for TopicOrganizationStrategy.

Tests cover:
- Initialization and configuration
- Topic extraction from sources
- Topic relationship finding
- Relevance filtering
- Refinement questions
- Text generation
- Error handling
"""

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

import pytest


class TestTopicOrganizationStrategyInit:
    """Tests for TopicOrganizationStrategy initialization."""

    def test_init_with_required_params(self):
        """Initialize with required parameters."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        assert strategy.model is mock_model
        assert strategy.search is mock_search
        assert strategy.min_sources_per_topic == 1
        assert strategy.max_topics == 5

    def test_init_with_custom_params(self):
        """Initialize with custom parameters."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            min_sources_per_topic=3,
            max_topics=10,
            similarity_threshold=0.8,
            enable_refinement=True,
            max_refinement_iterations=5,
        )

        assert strategy.min_sources_per_topic == 3
        assert strategy.max_topics == 10
        assert strategy.similarity_threshold == 0.8
        assert strategy.enable_refinement is True
        assert strategy.max_refinement_iterations == 5

    def test_init_creates_source_strategy(self):
        """Initialize creates source gathering strategy."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        assert strategy.source_strategy is not None

    def test_init_with_focused_iteration(self):
        """Initialize with focused iteration strategy."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            use_focused_iteration=True,
        )

        assert strategy.use_focused_iteration is True

    def test_init_creates_topic_graph(self):
        """Initialize creates topic graph."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        assert strategy.topic_graph is not None

    def test_init_with_citation_handler(self):
        """Initialize with custom citation handler."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_citation_handler = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            citation_handler=mock_citation_handler,
        )

        assert strategy.citation_handler is mock_citation_handler


class TestDelegateProgress:
    @pytest.mark.parametrize("use_focused_iteration", [False, True])
    def test_no_text_delegate_does_not_forward_synthesis(
        self, use_focused_iteration: bool
    ):
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        strategy = TopicOrganizationStrategy(
            search=Mock(),
            model=Mock(),
            use_focused_iteration=use_focused_iteration,
        )
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy.source_strategy.progress_callback(
            "Planning searches", 10, {"phase": "search_planning"}
        )
        strategy.source_strategy.progress_callback(
            "Synthesizing sources", 90, {"phase": "synthesis"}
        )
        strategy._update_progress(
            "Generating answer", 85, {"phase": "output_generation"}
        )

        assert callback.call_args_list == [
            (("Planning searches", None, {"phase": "search_planning"}),),
            (("Generating answer", 85, {"phase": "output_generation"}),),
        ]

    @pytest.mark.parametrize(
        ("phase", "forwarded"),
        [
            ("search_planning", True),
            ("search", True),
            ("observation", True),
            ("termination_check", True),
            # A bare "planning" is not a phase any strategy emits; the
            # whitelist is exactly the gathering timeline plus the
            # termination check.
            ("planning", False),
            ("init", False),
            ("synthesis", False),
            ("output_generation", False),
        ],
    )
    def test_delegate_whitelist_is_exactly_the_gathering_timeline(
        self, phase: str, forwarded: bool
    ):
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        strategy = TopicOrganizationStrategy(search=Mock(), model=Mock())
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy.source_strategy.progress_callback("msg", 42, {"phase": phase})

        if forwarded:
            # Percentages never reach the outer bar: the delegate's own
            # 5-90% scale would yank it.
            callback.assert_called_once_with("msg", None, {"phase": phase})
        else:
            callback.assert_not_called()


class TestTopicExtraction:
    """Tests for topic extraction methods."""

    def test_extract_topics_from_sources_empty(self):
        """Extract topics returns empty list for empty sources."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topics = strategy._extract_topics_from_sources([], "test query")

        assert topics == []

    def test_extract_topics_from_sources_creates_topics(self):
        """Extract topics creates topic objects."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()
        # Return "-" to create new topics
        mock_model.invoke.return_value = Mock(content="-")

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        # Initialize progress_callback
        strategy.progress_callback = None

        sources = [
            {
                "title": "Source 1",
                "snippet": "Content 1",
                "link": "http://test1.com",
            },
            {
                "title": "Source 2",
                "snippet": "Content 2",
                "link": "http://test2.com",
            },
        ]

        topics = strategy._extract_topics_from_sources(sources, "test query")

        assert isinstance(topics, list)

    def test_extract_topics_adds_to_existing(self):
        """Extract topics can add to existing topics."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()
        # Return "0" to add to first topic
        mock_model.invoke.return_value = Mock(content="0")

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        # Initialize progress_callback
        strategy.progress_callback = None

        existing_topic = Topic(
            id="existing1",
            title="Existing Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Lead content",
                "link": "http://lead.com",
            },
        )

        sources = [
            {
                "title": "New Source",
                "snippet": "New content",
                "link": "http://new.com",
            },
        ]

        topics = strategy._extract_topics_from_sources(
            sources, "test query", existing_topics=[existing_topic]
        )

        # The new source should be added to existing topic
        assert isinstance(topics, list)

    def test_extract_topics_deletes_irrelevant(self):
        """Extract topics handles delete response."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()
        # Return "d" to delete
        mock_model.invoke.return_value = Mock(content="d")

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        # Initialize progress_callback
        strategy.progress_callback = None

        sources = [
            {
                "title": "Irrelevant",
                "snippet": "Content",
                "link": "http://test.com",
            },
        ]

        topics = strategy._extract_topics_from_sources(sources, "test query")

        # No topics should be created
        assert topics == []

    def test_extract_topics_prompt_omits_url_credentials(self):
        """The per-source categorization prompt also drops URL credentials.

        This is the same leak as the lead-selection prompt, but earlier in
        the pipeline: ``source.get("link", "")`` is sent to the LLM
        verbatim (and logged in full for the first few sources), so a
        source URL with embedded credentials or an API-key query
        parameter must go through the shared redactor first.
        """
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.return_value = Mock(content="-")

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )
        strategy.progress_callback = None

        sources = [
            {
                "title": "Source 1",
                "snippet": "Content 1",
                "link": "https://alice:[email protected]/page?api_key=topsecret",
            },
        ]

        strategy._extract_topics_from_sources(sources, "test query")

        prompt = mock_model.invoke.call_args.args[0]
        assert "docs.example.com" in prompt
        assert "alice" not in prompt
        assert "s3cret" not in prompt
        assert "topsecret" not in prompt


class TestLeadSourceReselection:
    """Tests for lead source reselection methods."""

    def test_reselect_lead_for_single_topic_few_sources(self):
        """Reselect lead returns False for topics with few sources."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topic = Topic(
            id="t1",
            title="Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        result = strategy._reselect_lead_for_single_topic(topic, [topic])

        assert result is False

    def test_reselect_lead_prompt_omits_url_credentials(self):
        """Source labels in the lead selection prompt drop URL userinfo."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        strategy = TopicOrganizationStrategy(search=Mock(), model=Mock())
        lead = {
            "title": "Lead",
            "snippet": "Content",
            "url": "https://alice:[email protected]/page",
        }
        topic = Topic(id="t1", title="Lead", lead_source=lead)
        topic.add_supporting_source(
            {
                "title": "Other",
                "snippet": "More",
                "url": "http://[email protected]:8080/a",
            }
        )
        topic.add_supporting_source(
            {
                "title": "Plain",
                "snippet": "",
                "url": "https://plain.example.net",
            }
        )

        with patch.object(
            strategy, "_invoke_model", return_value="0"
        ) as invoke:
            assert (
                strategy._reselect_lead_for_single_topic(topic, [topic])
                is False
            )

        prompt = invoke.call_args.args[0]
        assert "0: [docs.example.com] Lead" in prompt
        assert "1: [mirror.example.org:8080] Other" in prompt
        assert "2: [plain.example.net] Plain" in prompt
        assert "alice" not in prompt
        assert "s3cret" not in prompt
        assert "token" not in prompt

    @pytest.mark.parametrize(
        "bad_url",
        [
            # A password containing an unencoded "/" ends the authority
            # early, so urlparse(url).netloc.rpartition("@")[2] finds no
            # "@" in "alice:pa" and returns it unchanged -- leaking the
            # credential into the domain label.
            "https://alice:pa/[email protected]/page",
            # Same, with "?" in the password.
            "https://alice:[email protected]/page",
            # Same, with "#" in the password.
            "https://alice:pa#[email protected]/page",
            # A ";" authority parameter is not part of the host either.
            "https://host.example.com;api_key=topsecret/page",
        ],
    )
    def test_reselect_lead_prompt_omits_credentials_with_delimiter_passwords(
        self, bad_url: str
    ):
        """A password/parameter the naive split can't isolate is dropped.

        These shapes defeat ``urlparse(url).netloc.rpartition("@")[2]``
        (see the parametrized cases), so the label must come from the
        shared redactor, which fails closed to an empty label instead of
        echoing the un-isolated credential text.
        """
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        strategy = TopicOrganizationStrategy(search=Mock(), model=Mock())
        topic = Topic(
            id="t1",
            title="Lead",
            lead_source={"title": "Lead", "snippet": "", "url": bad_url},
        )
        topic.add_supporting_source(
            {
                "title": "Other",
                "snippet": "",
                "url": "https://plain.example.net",
            }
        )

        with patch.object(
            strategy, "_invoke_model", return_value="0"
        ) as invoke:
            strategy._reselect_lead_for_single_topic(topic, [topic])

        prompt = invoke.call_args.args[0]
        assert "0: []" in prompt
        assert "alice" not in prompt
        assert "topsecret" not in prompt


class TestTopicRelationships:
    """Tests for topic relationship methods."""

    def test_find_topic_relationships_single_topic(self):
        """Find relationships handles single topic."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topic = Topic(
            id="t1",
            title="Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        # Should not raise error
        strategy._find_topic_relationships([topic])


class TestRelevanceFiltering:
    """Tests for relevance filtering methods."""

    def test_filter_topics_by_relevance_empty(self):
        """Filter topics returns empty for empty input."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        result = strategy._filter_topics_by_relevance([], "test query")

        assert result == []

    def test_filter_topics_by_relevance_follows_per_topic_verdict(self):
        """Each topic is kept or dropped according to its own verdict.

        A per-topic split is the only assertion that separates the happy
        path from the ``except`` path: on an error the handler keeps every
        topic, so a test that only checks a "yes" topic survives passes
        even when the model call blows up.
        """
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.side_effect = [
            Mock(content="yes"),
            Mock(content="no"),
        ]

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        keep = Topic(
            id="t1",
            title="Relevant Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )
        drop = Topic(
            id="t2",
            title="Irrelevant Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://other.com",
            },
        )
        strategy.topic_graph.topics = {"t1": keep, "t2": drop}

        result = strategy._filter_topics_by_relevance([keep, drop], "q")

        assert [topic.id for topic in result] == ["t1"]
        # The rejected topic is also evicted from the graph; the error
        # path would have left both entries in place.
        assert set(strategy.topic_graph.topics) == {"t1"}

    def test_filter_topics_by_relevance_removes_irrelevant(self):
        """Filter topics removes irrelevant topics."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.return_value = Mock(content="no")

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topic = Topic(
            id="t1",
            title="Irrelevant Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        result = strategy._filter_topics_by_relevance([topic], "test query")

        assert len(result) == 0


class TestRefinementQuestions:
    """Tests for refinement question generation."""

    def test_generate_refinement_question_disabled(self):
        """Generate refinement question returns None when disabled."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            enable_refinement=False,
        )

        result = strategy._generate_refinement_question([], "test query")

        assert result is None

    def test_generate_refinement_question_no_topics(self):
        """Generate refinement question returns None for no topics."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            enable_refinement=True,
        )

        result = strategy._generate_refinement_question([], "test query")

        assert result is None

    def test_generate_refinement_question_returns_question(self):
        """Generate refinement question returns question string."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.return_value = Mock(
            content="What are the key factors?"
        )

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            enable_refinement=True,
        )

        topic = Topic(
            id="t1",
            title="Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        result = strategy._generate_refinement_question([topic], "test query")

        assert result is not None
        assert "?" in result or len(result) > 0

    def test_generate_refinement_question_none_verdict_records_nothing(self):
        """A "NONE" verdict suppresses the question and records nothing.

        The follow-up call in the same test is what separates this from
        the ``except`` path, which also returns ``None``: after the
        suppressed round the strategy must still produce and record a
        question when the model offers one.
        """
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.side_effect = [
            Mock(content="NONE"),
            Mock(content="Which regions are affected?"),
        ]

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            enable_refinement=True,
        )

        topic = Topic(
            id="t1",
            title="Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        assert strategy._generate_refinement_question([topic], "q") is None
        assert strategy.refinement_questions == []

        assert (
            strategy._generate_refinement_question([topic], "q")
            == "Which regions are affected?"
        )
        assert strategy.refinement_questions == ["Which regions are affected?"]


class TestTopicReorganization:
    """Tests for topic reorganization methods."""

    def test_reorganize_topics_single_topic(self):
        """Reorganize topics handles single topic."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topic = Topic(
            id="t1",
            title="Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        result = strategy._reorganize_topics([topic])

        assert result == [topic]


class TestAnalyzeTopic:
    """Tests for main analyze_topic method."""

    def test_analyze_topic_returns_expected_structure(self):
        """Analyze topic returns expected result structure."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.return_value = Mock(content="-")

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            generate_text=False,
        )

        # Initialize progress_callback
        strategy.progress_callback = None

        # Mock the source strategy
        with patch.object(
            strategy.source_strategy,
            "analyze_topic",
            return_value={
                "all_links_of_system": [
                    {
                        "title": "Source 1",
                        "snippet": "Content",
                        "link": "http://test.com",
                    }
                ],
                "iterations": 1,
                "questions_by_iteration": {},
            },
        ):
            result = strategy.analyze_topic("test query")

        assert isinstance(result, dict)
        assert "findings" in result
        assert "iterations" in result
        assert "topics" in result
        assert "topic_graph" in result

    def test_analyze_topic_no_sources(self):
        """Analyze topic handles no sources gracefully."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        # Mock the source strategy to return no sources
        with patch.object(
            strategy.source_strategy,
            "analyze_topic",
            return_value={
                "all_links_of_system": [],
                "iterations": 0,
                "questions_by_iteration": {},
            },
        ):
            result = strategy.analyze_topic("test query")

        assert result["topics"] == []
        assert result["source_count"] == 0

    def test_analyze_topic_calls_progress_callback(self):
        """Analyze topic calls progress callback."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.return_value = Mock(content="-")

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            generate_text=False,
        )

        callback = Mock()
        strategy.set_progress_callback(callback)

        # Make sure progress_callback is set
        assert strategy.progress_callback is callback

        with patch.object(
            strategy.source_strategy,
            "analyze_topic",
            return_value={
                "all_links_of_system": [
                    {
                        "title": "Source",
                        "snippet": "Content",
                        "link": "http://test.com",
                    }
                ],
                "iterations": 1,
                "questions_by_iteration": {},
            },
        ):
            strategy.analyze_topic("test query")

        # Callback should be called at least once through _update_progress
        assert callback.call_count >= 1


class TestFormattingMethods:
    """Tests for formatting helper methods."""

    def test_format_single_topic_with_sources(self):
        """Format single topic includes source information."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topic = Topic(
            id="t1",
            title="Test Topic",
            lead_source={
                "title": "Lead Source",
                "snippet": "Lead content",
                "link": "http://lead.com",
            },
        )
        topic.add_supporting_source(
            {
                "title": "Support Source",
                "snippet": "Support content",
                "link": "http://support.com",
            }
        )

        result = strategy._format_single_topic_with_sources(topic)

        assert "Lead Source" in result
        assert "Support Source" in result

    def test_format_topic_graph_as_knowledge(self):
        """Format topic graph creates readable knowledge output."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topic = Topic(
            id="t1",
            title="Test Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        result = strategy._format_topic_graph_as_knowledge(
            [topic], "test query"
        )

        assert "Topic Graph" in result
        assert "test query" in result

    def test_format_topic_graph_empty(self):
        """Format topic graph handles empty topics."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        result = strategy._format_topic_graph_as_knowledge([], "test query")

        assert "No topics" in result

    def test_format_topic_findings(self):
        """Format topic findings creates comprehensive output."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        topic = Topic(
            id="t1",
            title="Test Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        result = strategy._format_topic_findings([topic], "test query")

        assert "Topic Organization" in result


class TestTextGeneration:
    """Tests for text generation methods."""

    def test_generate_topic_based_text_no_topics(self):
        """Generate topic based text returns empty for no topics."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        mock_search = Mock()
        mock_model = Mock()

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
        )

        result = strategy._generate_topic_based_text([], "test query")

        assert result == ""

    def test_generate_topic_based_text_with_topics(self):
        """Generate topic based text creates text from topics."""
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        mock_search = Mock()
        mock_model = Mock()
        mock_model.invoke.return_value = Mock(
            content="Generated text about topic."
        )

        # Create citation handler mock
        mock_citation = Mock()
        mock_citation._create_documents.return_value = []
        mock_citation._format_sources.return_value = ""

        strategy = TopicOrganizationStrategy(
            search=mock_search,
            model=mock_model,
            citation_handler=mock_citation,
        )

        topic = Topic(
            id="t1",
            title="Test Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

        result = strategy._generate_topic_based_text([topic], "test query")

        assert len(result) > 0


class _SyncOnlyLLM:
    """LLM stub exposing ``invoke`` and a non-functional ``ainvoke``.

    ``ainvoke`` records every call it receives to ``self.async_prompts``
    but always raises ``AttributeError``, so it still fails the async path
    the same way a genuinely sync-only object would -- while leaving a
    trail (``async_prompts``) that tests can assert against to catch a
    regression that bridges to the async side through a shape other than
    ``ainvoke`` being missing entirely (e.g. ``run_until_complete`` /
    ``get_event_loop``).
    """

    def __init__(self, body: str):
        self.body = body
        self.prompts: list[str] = []
        self.async_prompts: list[str] = []

    def invoke(self, prompt, *args, **kwargs):
        self.prompts.append(str(prompt))
        return Mock(content=self.body)

    async def ainvoke(self, prompt, *args, **kwargs):
        self.async_prompts.append(str(prompt))
        raise AttributeError(
            "_SyncOnlyLLM.ainvoke is a recording stub, not a usable async path"
        )


class TestModelInvocation:
    """Tests for the shared ``_invoke_model`` / ``_ainvoke_model`` pair."""

    @staticmethod
    def _strategy(model):
        from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (
            TopicOrganizationStrategy,
        )

        return TopicOrganizationStrategy(
            search=Mock(), model=model, enable_refinement=True
        )

    @staticmethod
    def _topic():
        from local_deep_research.advanced_search_system.findings.topic import (
            Topic,
        )

        return Topic(
            id="t1",
            title="Topic",
            lead_source={
                "title": "Lead",
                "snippet": "Content",
                "link": "http://test.com",
            },
        )

    def test_invoke_model_normalizes_and_stays_synchronous(self):
        """``_invoke_model`` calls ``invoke`` and extracts ``.content``."""
        model = _SyncOnlyLLM("  answer  ")
        strategy = self._strategy(model)

        assert strategy._invoke_model("prompt").strip() == "answer"
        assert model.prompts == ["prompt"]

    def test_invoke_model_propagates_errors(self):
        """Call-site ``except`` blocks still see the model's exception."""
        model = Mock()
        model.invoke.side_effect = RuntimeError("boom")
        strategy = self._strategy(model)

        with pytest.raises(RuntimeError, match="boom"):
            strategy._invoke_model("prompt")

    def test_converted_site_never_starts_an_event_loop(self):
        """A converted call site runs without creating an event loop.

        langchain's async httpx client is process-cached and loop-bound,
        so a per-call throwaway loop breaks or re-sends every request
        after the first (#6293). ``asyncio.run`` and
        ``asyncio.new_event_loop`` are patched to raise -- but
        ``_generate_refinement_question`` wraps the call in a bare
        ``except Exception``, so a regression that reaches for either
        patched function is swallowed rather than propagated to the test.
        The real discriminator is ``len(model.prompts)``: it is 1 only if
        ``_invoke_model`` reached ``model.invoke`` directly, with no event
        loop in between.
        """
        model = _SyncOnlyLLM("Which regions are affected?")
        strategy = self._strategy(model)

        def _no_loop(*args, **kwargs):
            raise AssertionError("the sync path must not start an event loop")

        with (
            patch.object(asyncio, "run", _no_loop),
            patch.object(asyncio, "new_event_loop", _no_loop),
        ):
            question = strategy._generate_refinement_question(
                [self._topic()], "test query"
            )

        assert question == "Which regions are affected?"
        assert len(model.prompts) == 1
        assert model.async_prompts == []

    def test_extract_topics_site_never_starts_an_event_loop(self):
        """The per-source extraction loop stays synchronous too.

        ``asyncio.run`` and ``asyncio.new_event_loop`` are patched to
        raise, but the per-source loop wraps each call in a bare
        ``except Exception`` and, on failure, creates a recovery topic for
        that source -- so ``assert len(topics) == 2`` alone would pass
        under the regression too, since one recovery topic per source
        looks the same as the two "real" topics this test expects. The
        real discriminator is ``len(model.prompts)``: it can only be 2 if
        ``_invoke_model`` reached ``model.invoke`` for both sources, with
        no event loop in between.
        """
        model = _SyncOnlyLLM("-")
        strategy = self._strategy(model)

        def _no_loop(*args, **kwargs):
            raise AssertionError("the sync path must not start an event loop")

        sources = [
            {
                "title": "Source A",
                "snippet": "Content A",
                "link": "http://a.test",
            },
            {
                "title": "Source B",
                "snippet": "Content B",
                "link": "http://b.test",
            },
        ]

        with (
            patch.object(asyncio, "run", _no_loop),
            patch.object(asyncio, "new_event_loop", _no_loop),
        ):
            topics = strategy._extract_topics_from_sources(
                sources, "test query"
            )

        # "-" means "new topic" for each source.
        assert len(topics) == 2
        assert len(model.prompts) == 2
        assert model.async_prompts == []

    @pytest.mark.asyncio
    async def test_ainvoke_model_awaits_ainvoke(self):
        """The async twin awaits ``ainvoke`` and never touches ``invoke``."""
        model = Mock()
        model.ainvoke = AsyncMock(return_value=Mock(content="async answer"))
        strategy = self._strategy(model)

        assert await strategy._ainvoke_model("prompt") == "async answer"
        model.ainvoke.assert_awaited_once_with("prompt")
        model.invoke.assert_not_called()

    @pytest.mark.asyncio
    async def test_ainvoke_model_requires_ainvoke(self):
        """No sync fallback: a sync-only object is a programming error."""
        strategy = self._strategy(_SyncOnlyLLM("answer"))

        with pytest.raises(AttributeError):
            await strategy._ainvoke_model("prompt")