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.

13.2 KB
Raw Blame
"""
Detailed behavior tests for working strategies.

Tests specific features and behaviors of strategies beyond basic functionality.
"""

from unittest.mock import patch

from loguru import logger


class TestSourceBasedStrategy:
    """Detailed tests for SourceBasedSearchStrategy."""

    def test_logs_questions_from_canonical_setting(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        from local_deep_research.search_system_factory import create_strategy

        settings = dict(strategy_settings_snapshot)
        settings["search.iterations"] = {"value": 1, "type": "int"}
        settings["search.questions"] = {"value": 1, "type": "int"}
        settings["search.questions_per_iteration"] = {
            "value": 7,
            "type": "int",
        }
        strategy = create_strategy(
            strategy_name="source-based",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=settings,
        )

        with patch(
            "local_deep_research.advanced_search_system.strategies."
            "source_based_strategy.logger"
        ) as mock_logger:
            strategy.analyze_topic("Test query")

        messages = [
            call.args[0]
            for call in mock_logger.info.call_args_list
            if call.args
        ]
        assert any(
            "questions_per_iteration: 7" in message for message in messages
        )

    def test_finds_sources_from_search_results(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that source-based strategy extracts sources from search results."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="source-based",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("Test query")

        # Should have accumulated links (or at least not crash)
        assert isinstance(strategy.all_links_of_system, (list, set, dict))
        logger.info(
            f"Source-based found {len(strategy.all_links_of_system)} sources"
        )

    def test_generates_questions_for_sources(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that source-based strategy generates questions."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="source-based",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("Test query")

        # Should have generated questions
        assert len(strategy.questions_by_iteration) > 0
        logger.info(f"Generated questions: {strategy.questions_by_iteration}")

    def test_returns_formatted_findings(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that result includes formatted_findings."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="source-based",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Test query")

        assert "formatted_findings" in result or "findings" in result


class TestNewsStrategy:
    """Detailed tests for NewsAggregationStrategy."""

    def test_handles_news_queries(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that news strategy handles news-specific queries."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="news",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Latest developments in AI")

        assert isinstance(result, dict)
        logger.info(f"News strategy returned keys: {list(result.keys())}")


class TestFocusedIterationStrategy:
    """Detailed tests for FocusedIterationStrategy."""

    def test_uses_knowledge_accumulation(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that focused iteration accumulates knowledge."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="focused-iteration",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Deep research topic")

        # Should have findings
        assert "findings" in result or "current_knowledge" in result

    def test_tracks_previous_searches(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that focused iteration tracks previous searches."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="focused-iteration",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("Research topic")

        # Should have questions tracked
        assert strategy.questions_by_iteration is not None

    def test_does_not_accumulate_results_across_calls(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test that successive analyze_topic calls do not accumulate search results."""
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        strategy = create_strategy(
            strategy_name="focused-iteration",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        strategy.analyze_topic("First query")
        first_len = len(strategy.all_search_results)
        assert first_len > 0

        strategy.analyze_topic("Second query")
        # Should be fresh, not cumulative
        assert len(strategy.all_search_results) == first_len

    def test_citation_offset_uses_existing_all_links_count(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """analyze_topic must pass nr_of_links=len(all_links_of_system) to the
        citation handler, not a hardcoded 0. Otherwise detailed-report
        subsections all start citations at [1] and collide on the shared
        bibliography."""
        import copy
        from unittest.mock import patch

        # Return fresh copies per call so dict identity does not leak
        # indices across calls.
        template_results = strategy_mock_search.run.return_value
        strategy_mock_search.run.side_effect = lambda *a, **kw: copy.deepcopy(
            template_results
        )

        # Pre-populate all_links_of_system to simulate a prior subsection
        # that already added 7 sources.
        prior_links = [
            {
                "title": f"prior-{i}",
                "link": f"http://prior-{i}.example",
                "snippet": f"prior snippet {i}",
                "full_content": f"prior content {i}",
            }
            for i in range(7)
        ]
        prior_count = len(prior_links)
        # create_strategy with shared all_links_of_system
        from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (  # noqa: E501
            FocusedIterationStrategy,
        )

        strategy = FocusedIterationStrategy(
            model=strategy_mock_llm,
            search=strategy_mock_search,
            all_links_of_system=prior_links,
            settings_snapshot=strategy_settings_snapshot,
            max_iterations=1,
        )

        with patch.object(
            strategy.citation_handler,
            "analyze_followup",
            return_value={
                "content": "ok",
                "documents": [],
            },
        ) as spy:
            strategy.analyze_topic("Some query")

        # The handler MUST have been called once.
        assert spy.call_count == 1
        # And the nr_of_links kwarg MUST reflect the pre-populated count
        # so citations continue, not restart at [1]. (Note: prior_links
        # is the same list object as strategy.all_links_of_system and
        # gets extended during analyze_topic, so capture the prior
        # length before the call.)
        kwargs = spy.call_args.kwargs
        assert kwargs["nr_of_links"] == prior_count

    def test_report_mode_indices_stay_sequential_across_sections(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """In detailed report mode, IntegratedReportGenerator calls
        analyze_topic() once per subsection against a SHARED
        all_links_of_system. After subsection N, the next subsection must
        continue citation indices (e.g. [16]) rather than restart at [1].
        This is the regression covered by #4851."""
        import copy
        from unittest.mock import patch

        # Fresh dicts per search.run() call so that previously assigned
        # "index" values do not bleed across subsections.
        template_results = strategy_mock_search.run.return_value
        strategy_mock_search.run.side_effect = lambda *a, **kw: copy.deepcopy(
            template_results
        )

        shared_links: list = []
        from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (  # noqa: E501
            FocusedIterationStrategy,
        )

        strategy = FocusedIterationStrategy(
            model=strategy_mock_llm,
            search=strategy_mock_search,
            all_links_of_system=shared_links,
            settings_snapshot=strategy_settings_snapshot,
            max_iterations=1,
        )

        # Stub citation handler so it does not need a real LLM. The real
        # handler mutates result["index"] in place; we replicate that here
        # so we can inspect all_links_of_system afterwards.
        def fake_analyze_followup(
            question, search_results, previous_knowledge, nr_of_links
        ):
            for i, r in enumerate(search_results):
                if isinstance(r, dict) and "index" not in r:
                    r["index"] = str(i + nr_of_links + 1)
            return {"content": "ok", "documents": []}

        with patch.object(
            strategy.citation_handler,
            "analyze_followup",
            side_effect=fake_analyze_followup,
        ):
            strategy.analyze_topic("Subsection A")
            count_after_a = len(shared_links)
            assert count_after_a > 0
            strategy.analyze_topic("Subsection B")

        # Total sources = A's + B's
        assert len(shared_links) == 2 * count_after_a

        # Index of the first source in subsection B must be one past the
        # last index from subsection A — i.e. citations continued, they
        # did not restart at [1].
        subsection_a_indices = [
            int(shared_links[i]["index"]) for i in range(count_after_a)
        ]
        subsection_b_indices = [
            int(shared_links[i]["index"])
            for i in range(count_after_a, len(shared_links))
        ]
        assert subsection_b_indices[0] == max(subsection_a_indices) + 1
        # And there must be no overlap between the two index ranges.
        assert not (set(subsection_a_indices) & set(subsection_b_indices))


class TestTopicOrganizationStrategy:
    """Tests for TopicOrganizationStrategy."""

    def test_topic_organization_works(
        self,
        strategy_mock_llm,
        strategy_mock_search,
        strategy_settings_snapshot,
    ):
        """Test topic organization strategy."""
        from loguru import logger
        from local_deep_research.search_system_factory import (
            create_strategy,
        )

        # Initialize MILESTONE log level if not already defined
        # This is normally done during web app initialization
        try:
            logger.level("MILESTONE")
        except ValueError:
            logger.level("MILESTONE", no=26, color="<magenta><bold>")

        strategy = create_strategy(
            strategy_name="topic-organization",
            model=strategy_mock_llm,
            search=strategy_mock_search,
            settings_snapshot=strategy_settings_snapshot,
        )

        result = strategy.analyze_topic("Multi-topic research question")

        assert isinstance(result, dict)