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.

9.8 KB
Raw Blame
"""Wiring tests: classic strategies emit chat-step-phase progress events.

The chat UI only renders progress events whose phase is in
``research_service._STEP_PHASES``, and only composes the expandable detail
for phases in ``_STEP_DETAIL_PHASES``. These tests drive the REAL
``analyze_topic`` loops (LLM/search mocked) and assert the full step
sequence a chat user sees: planning → searching → results observation →
synthesis — with the question lists / found sources riding in
``metadata["content"]``.

Regression guard for the gap where source-based / focused-iteration runs
produced almost no step timeline in chat mode because their phases
(``question_generation``, ``parallel_search``, custom strings) matched
nothing in the step vocabulary.
"""

from unittest.mock import MagicMock, patch

from local_deep_research.web.services.research_service import (
    _STEP_DETAIL_PHASES,
    _STEP_PHASES,
)


def _collect_progress(strategy):
    """Attach a recording callback; return the list of (message, metadata)."""
    events = []

    def callback(message, progress_percent, metadata):
        events.append((message, metadata))

    strategy.set_progress_callback(callback)
    return events


def _step_events(events):
    return [(msg, md) for msg, md in events if md.get("phase") in _STEP_PHASES]


class TestSourceBasedStepEvents:
    def _make_strategy(self):
        from local_deep_research.advanced_search_system.strategies.source_based_strategy import (
            SourceBasedSearchStrategy,
        )

        return SourceBasedSearchStrategy(
            model=MagicMock(),
            search=MagicMock(),
            all_links_of_system=[],
            use_cross_engine_filter=False,
            settings_snapshot={"search.iterations": {"value": 1}},
        )

    def test_full_run_emits_step_phase_sequence_with_detail(self):
        strategy = self._make_strategy()
        events = _collect_progress(strategy)

        results = [
            {"title": "Result A", "link": "http://a.com", "snippet": "sa"},
            {"title": "Result B", "link": "http://b.com", "snippet": "sb"},
        ]
        with (
            patch.object(
                strategy.question_generator,
                "generate_questions",
                return_value=["What is A?"],
            ),
            patch(
                "local_deep_research.advanced_search_system.strategies.source_based_strategy.run_parallel_searches",
                return_value=[
                    (0, {"question": "What is A?", "results": results})
                ],
            ),
            patch.object(
                strategy.citation_handler,
                "analyze_followup",
                return_value={"content": "Done", "documents": []},
            ),
        ):
            strategy.analyze_topic("test query")

        phases = [md["phase"] for _, md in _step_events(events)]
        # The chat timeline: init → planning → searching → results → synthesis
        assert phases[0] == "init"
        assert "search_planning" in phases
        assert "search" in phases
        assert "observation" in phases
        assert "synthesis" in phases
        assert phases.index("search_planning") < phases.index("search")
        assert phases.index("search") < phases.index("observation")
        assert phases.index("observation") < phases.index("synthesis")

        search_events = [
            (m, md) for m, md in events if md.get("phase") == "search"
        ]
        assert "What is A?" in search_events[0][1]["content"]

        obs = [(m, md) for m, md in events if md.get("phase") == "observation"]
        obs_msg, obs_md = obs[0]
        # The original query is searched verbatim alongside the LLM question.
        assert obs_msg.startswith("📄 Found 2 results from 2 searches")
        assert "• Result A (http://a.com)" in obs_md["content"]
        assert "• Result B (http://b.com)" in obs_md["content"]

    def test_detail_phases_are_step_phases(self):
        """Detail-carrying phases must be renderable steps at all."""
        assert _STEP_DETAIL_PHASES <= _STEP_PHASES

    def _make_two_iteration_strategy(self):
        from local_deep_research.advanced_search_system.strategies.source_based_strategy import (
            SourceBasedSearchStrategy,
        )

        return SourceBasedSearchStrategy(
            model=MagicMock(),
            search=MagicMock(),
            all_links_of_system=[],
            use_cross_engine_filter=False,
            settings_snapshot={"search.iterations": {"value": 2}},
        )

    def test_second_iteration_planning_lists_first_iterations_questions(self):
        """The iteration-2 search_planning detail must carry iteration 1's
        questions as populated by the REAL loop (guards the write-after-emit
        ordering of questions_by_iteration)."""
        strategy = self._make_two_iteration_strategy()
        events = _collect_progress(strategy)

        with (
            patch.object(
                strategy.question_generator,
                "generate_questions",
                side_effect=[["What is A?"], ["What is B?"]],
            ),
            patch(
                "local_deep_research.advanced_search_system.strategies.source_based_strategy.run_parallel_searches",
                return_value=[
                    (
                        0,
                        {
                            "question": "q",
                            "results": [{"title": "T", "link": "http://t.com"}],
                        },
                    )
                ],
            ),
            patch.object(
                strategy.citation_handler,
                "analyze_followup",
                return_value={"content": "Done", "documents": []},
            ),
        ):
            strategy.analyze_topic("test query")

        planning = [
            (m, md) for m, md in events if md.get("phase") == "search_planning"
        ]
        assert len(planning) == 2
        # Iteration 1: no detail, query in the message.
        assert "test query" in planning[0][0]
        assert "content" not in planning[0][1]
        # Iteration 2: iteration 1's actual questions (original query is
        # searched verbatim alongside the LLM question) in the detail.
        assert "iteration 2" in planning[1][0]
        assert "What is A?" in planning[1][1]["content"]
        assert "test query" in planning[1][1]["content"]

    def test_zero_question_iteration_still_emits_planning(self):
        """An iteration whose LLM yields no questions must still surface
        its planning step (search/observation are skipped); the following
        synthesis still runs on iteration 1's results."""
        strategy = self._make_two_iteration_strategy()
        events = _collect_progress(strategy)

        with (
            patch.object(
                strategy.question_generator,
                "generate_questions",
                side_effect=[["What is A?"], []],
            ),
            patch(
                "local_deep_research.advanced_search_system.strategies.source_based_strategy.run_parallel_searches",
                return_value=[
                    (
                        0,
                        {
                            "question": "q",
                            "results": [{"title": "T", "link": "http://t.com"}],
                        },
                    )
                ],
            ),
            patch.object(
                strategy.citation_handler,
                "analyze_followup",
                return_value={"content": "Done", "documents": []},
            ),
        ):
            strategy.analyze_topic("test query")

        phases = [md["phase"] for _, md in _step_events(events)]
        assert phases.count("search_planning") == 2
        assert phases.count("search") == 1
        assert phases.count("observation") == 1


class TestFocusedIterationStepEvents:
    def _make_strategy(self):
        from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (
            FocusedIterationStrategy,
        )

        # questions_per_iteration=2: iteration 1 is [original query] +
        # LLM questions, trimmed to the limit — 2 keeps one of each.
        return FocusedIterationStrategy(
            model=MagicMock(),
            search=MagicMock(),
            all_links_of_system=[],
            max_iterations=1,
            questions_per_iteration=2,
        )

    def test_full_run_emits_step_phase_sequence_with_detail(self):
        strategy = self._make_strategy()
        # Simple path: no browsecomp explorer.
        strategy.explorer = None
        strategy.use_browsecomp_optimization = False
        events = _collect_progress(strategy)

        results = [{"title": "Doc X", "link": "http://x.com"}]
        with (
            patch.object(
                strategy.question_generator,
                "generate_questions",
                return_value=["Focused Q1"],
            ),
            patch.object(
                strategy,
                "_execute_parallel_searches",
                return_value=results,
            ),
            patch.object(
                strategy.citation_handler,
                "analyze_followup",
                return_value={"content": "Done", "documents": []},
            ),
        ):
            strategy.analyze_topic("focused test query")

        phases = [md["phase"] for _, md in _step_events(events)]
        assert "search_planning" in phases
        assert "search" in phases
        assert "observation" in phases
        assert "synthesis" in phases
        assert phases.index("search") < phases.index("observation")

        search_events = [
            (m, md) for m, md in events if md.get("phase") == "search"
        ]
        assert "Focused Q1" in search_events[0][1]["content"]

        obs = [(m, md) for m, md in events if md.get("phase") == "observation"]
        assert "• Doc X (http://x.com)" in obs[0][1]["content"]