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.

27.8 KB
Raw Blame
"""
Tests for BaseSearchStrategy.

Tests cover:
- Initialization with default and custom parameters
- Settings access via get_setting
- Progress callback functionality
- Error handling utilities
- Progress emission helpers
"""

import pytest
from types import MappingProxyType
from unittest.mock import Mock
from typing import Dict

from local_deep_research.advanced_search_system.strategies.base_strategy import (
    BaseSearchStrategy,
)
from local_deep_research.utilities.url_utils import FETCHED_TITLE_KEY


class ConcreteStrategy(BaseSearchStrategy):
    """Concrete implementation for testing abstract base class."""

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.search = None

    def analyze_topic(self, query: str) -> Dict:
        """Simple implementation for testing."""
        return {
            "findings": [],
            "iterations": 0,
            "questions": {},
            "formatted_findings": "",
            "current_knowledge": "",
        }


class TestBaseSearchStrategyInit:
    """Tests for BaseSearchStrategy initialization."""

    def test_init_default_values(self):
        """Initialize with default values."""
        strategy = ConcreteStrategy()

        assert strategy.all_links_of_system == []
        assert strategy.questions_by_iteration == {}
        assert strategy.settings_snapshot == {}
        assert strategy.progress_callback is None
        assert strategy.search_original_query is True

    def test_init_with_all_links(self):
        """Initialize with provided all_links_of_system."""
        links = [{"url": "https://example.com", "title": "Example"}]
        strategy = ConcreteStrategy(all_links_of_system=links)

        assert strategy.all_links_of_system is links
        assert len(strategy.all_links_of_system) == 1

    def test_init_with_settings_snapshot(self):
        """Initialize with settings snapshot."""
        snapshot = {
            "search.iterations": {"value": 3},
            "llm.temperature": {"value": 0.7},
        }
        strategy = ConcreteStrategy(settings_snapshot=snapshot)

        assert strategy.settings_snapshot == snapshot

    def test_init_with_questions_by_iteration(self):
        """Initialize with questions by iteration."""
        questions = {1: ["Q1", "Q2"], 2: ["Q3"]}
        strategy = ConcreteStrategy(questions_by_iteration=questions)

        assert strategy.questions_by_iteration is questions
        assert len(strategy.questions_by_iteration) == 2

    def test_init_search_original_query_false(self):
        """Initialize with search_original_query=False."""
        strategy = ConcreteStrategy(search_original_query=False)

        assert strategy.search_original_query is False

    def test_init_creates_new_dict_for_none_questions(self):
        """Initialize creates new dict for None questions_by_iteration."""
        strategy1 = ConcreteStrategy()
        strategy2 = ConcreteStrategy()

        # Each should have its own dict
        strategy1.questions_by_iteration["test"] = ["Q1"]

        assert "test" not in strategy2.questions_by_iteration

    def test_init_creates_new_list_for_none_links(self):
        """Initialize creates new list for None all_links_of_system."""
        strategy1 = ConcreteStrategy()
        strategy2 = ConcreteStrategy()

        # Each should have its own list
        strategy1.all_links_of_system.append({"url": "test"})

        assert len(strategy2.all_links_of_system) == 0


class TestGetSetting:
    """Tests for get_setting method."""

    def test_get_setting_with_value_dict(self):
        """Get setting from dict with 'value' key."""
        snapshot = {"search.iterations": {"value": 5}}
        strategy = ConcreteStrategy(settings_snapshot=snapshot)

        result = strategy.get_setting("search.iterations")

        assert result == 5

    def test_get_setting_direct_value(self):
        """Get setting with direct value (not dict)."""
        snapshot = {"simple_key": "simple_value"}
        strategy = ConcreteStrategy(settings_snapshot=snapshot)

        result = strategy.get_setting("simple_key")

        assert result == "simple_value"

    def test_get_setting_missing_key_returns_default(self):
        """Get missing setting returns default."""
        strategy = ConcreteStrategy()

        result = strategy.get_setting("nonexistent.key", default="fallback")

        assert result == "fallback"

    def test_get_setting_missing_key_returns_none(self):
        """Get missing setting returns None by default."""
        strategy = ConcreteStrategy()

        result = strategy.get_setting("nonexistent.key")

        assert result is None

    def test_get_setting_nested_value(self):
        """Get setting with nested value."""
        snapshot = {
            "complex.setting": {
                "value": {"nested": "data"},
                "ui_element": "json",
            }
        }
        strategy = ConcreteStrategy(settings_snapshot=snapshot)

        result = strategy.get_setting("complex.setting")

        assert result == {"nested": "data"}


class TestProgressCallback:
    """Tests for progress callback functionality."""

    def test_set_progress_callback(self):
        """Set progress callback."""
        strategy = ConcreteStrategy()
        callback = Mock()

        strategy.set_progress_callback(callback)

        assert strategy.progress_callback is callback

    def test_update_progress_calls_callback(self):
        """Update progress calls callback with correct args."""
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy._update_progress("Test message", 50, {"phase": "test"})

        callback.assert_called_once_with("Test message", 50, {"phase": "test"})

    def test_update_progress_without_callback(self):
        """Update progress does nothing without callback."""
        strategy = ConcreteStrategy()

        # Should not raise
        strategy._update_progress("Test message", 50)

    def test_update_progress_none_metadata_becomes_empty_dict(self):
        """Update progress with None metadata becomes empty dict."""
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy._update_progress("Test message", 50, None)

        callback.assert_called_once_with("Test message", 50, {})

    def test_update_progress_optional_progress_percent(self):
        """Update progress with optional progress_percent."""
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy._update_progress("Test message")

        callback.assert_called_once_with("Test message", None, {})


class TestValidateSearchEngine:
    """Tests for _validate_search_engine method."""

    def test_validate_search_engine_missing(self):
        """Validate returns False when search engine missing."""
        strategy = ConcreteStrategy()
        strategy.search = None
        callback = Mock()
        strategy.set_progress_callback(callback)

        result = strategy._validate_search_engine()

        assert result is False
        callback.assert_called_once()
        call_args = callback.call_args
        assert "No search engine available" in call_args[0][0]
        assert call_args[0][1] == 100  # Progress should be 100%
        assert call_args[0][2]["phase"] == "error"

    def test_validate_search_engine_present(self):
        """Validate returns True when search engine present."""
        strategy = ConcreteStrategy()
        strategy.search = Mock()

        result = strategy._validate_search_engine()

        assert result is True

    def test_validate_search_engine_no_search_attribute(self):
        """Validate returns False when no search attribute."""
        strategy = ConcreteStrategy()
        del strategy.search

        result = strategy._validate_search_engine()

        assert result is False


class TestEmitQuestionGenerationProgress:
    """Tests for _emit_question_generation_progress method.

    The events use the ``search_planning`` step phase and carry the full
    previous-question list in ``metadata["content"]`` (no "... and N more"
    sampling) so the chat step row can expand to show it.
    """

    def test_emit_first_iteration_progress(self):
        """Emit progress for first iteration."""
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy._emit_question_generation_progress(
            iteration=1, progress_percent=10, query="test query"
        )

        callback.assert_called_once()
        call_args = callback.call_args
        assert "test query" in call_args[0][0]
        assert "\n" not in call_args[0][0]  # one-line message
        assert call_args[0][1] == 10
        assert call_args[0][2]["phase"] == "search_planning"
        assert call_args[0][2]["iteration"] == 1
        assert call_args[0][2]["type"] == "milestone"

    def test_emit_later_iteration_lists_all_questions_in_content(self):
        """Later iterations: one-line message; ALL previous questions in
        metadata['content'] — the old 3-question sample with '... and N
        more' hid most of them."""
        strategy = ConcreteStrategy()
        strategy.questions_by_iteration = {
            1: ["Question 1", "Question 2", "Question 3", "Question 4"]
        }
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy._emit_question_generation_progress(
            iteration=2, progress_percent=50, source_count=10
        )

        callback.assert_called_once()
        call_args = callback.call_args
        assert "10 sources" in call_args[0][0]
        assert "\n" not in call_args[0][0]  # one-line message
        assert "more" not in call_args[0][0]
        metadata = call_args[0][2]
        assert metadata["phase"] == "search_planning"
        assert metadata["source_count"] == 10
        for q in strategy.questions_by_iteration[1]:
            assert q in metadata["content"]

    def test_emit_later_iteration_without_prev_questions_has_no_content(self):
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy._emit_question_generation_progress(
            iteration=2, progress_percent=50, source_count=0
        )

        callback.assert_called_once()
        assert "content" not in callback.call_args[0][2]

    def test_first_iteration_query_newlines_flattened_and_capped(self):
        """Chat's textarea allows Shift+Enter — a multi-line query must
        still produce a one-line message, capped with a visible marker."""
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        query = "line one\nline two " + "q" * 200
        strategy._emit_question_generation_progress(
            iteration=1, progress_percent=10, query=query
        )

        message = callback.call_args[0][0]
        assert "\n" not in message
        shown = message.split('"')[1]
        assert shown.startswith("line one line two ")
        assert len(shown) == 151  # 150 chars + truncation marker
        assert shown.endswith("…")

    def test_first_iteration_query_at_cap_is_not_marked(self):
        from local_deep_research.advanced_search_system.strategies.base_strategy import (
            _OBSERVATION_PREVIEW_MAX_CHARS,
        )

        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        query = "q" * _OBSERVATION_PREVIEW_MAX_CHARS
        strategy._emit_question_generation_progress(
            iteration=1, progress_percent=10, query=query
        )

        assert callback.call_args[0][0].split('"')[1] == query


class TestEmitSearchingProgress:
    """Tests for _emit_searching_progress method.

    The events use the ``search`` step phase; the full question list rides
    in ``metadata["content"]`` (no "... and N more" sampling).
    """

    def test_emit_searching_progress_basic(self):
        """Emit searching progress with questions."""
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        questions = ["Q1", "Q2", "Q3"]
        strategy._emit_searching_progress(
            iteration=1, questions=questions, progress_percent=25
        )

        callback.assert_called_once()
        call_args = callback.call_args
        assert call_args[0][0] == "Searching with 3 questions (iteration 1)"
        metadata = call_args[0][2]
        assert metadata["phase"] == "search"
        assert metadata["questions"] == questions
        for q in questions:
            assert q in metadata["content"]

    def test_emit_searching_progress_many_questions_all_listed(self):
        """All questions appear in the detail — no '... and N more'."""
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        questions = ["Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7"]
        strategy._emit_searching_progress(
            iteration=2, questions=questions, progress_percent=50
        )

        callback.assert_called_once()
        call_args = callback.call_args
        assert "more" not in call_args[0][0]
        for q in questions:
            assert q in call_args[0][2]["content"]


class TestEmitSearchResultsObservation:
    """Tests for _emit_search_results_observation method."""

    def test_emit_observation_with_results(self):
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        results = [
            {"title": "Result A", "link": "http://a.com"},
            {"title": "Result B", "url": "http://b.com"},
            {"snippet": "no title or link"},
            "not-a-dict",
        ]
        strategy._emit_search_results_observation(
            iteration=1,
            questions=["Q1", "Q2"],
            results=results,
            progress_percent=40,
        )

        callback.assert_called_once()
        call_args = callback.call_args
        assert (
            call_args[0][0]
            == "📄 Found 4 results from 2 searches (iteration 1)"
        )
        metadata = call_args[0][2]
        assert metadata["phase"] == "observation"
        assert metadata["iteration"] == 1
        assert "• Result A (http://a.com)" in metadata["content"]
        assert "• Result B (http://b.com)" in metadata["content"]
        assert "• Untitled" in metadata["content"]

    def test_emit_observation_no_results_has_no_content(self):
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        strategy._emit_search_results_observation(
            iteration=2, questions=["Q1"], results=[], progress_percent=60
        )

        callback.assert_called_once()
        call_args = callback.call_args
        assert (
            call_args[0][0] == "📄 Found 0 results from 1 search (iteration 2)"
        )
        assert "content" not in call_args[0][2]

    def test_emit_observation_detail_is_capped(self):
        from local_deep_research.advanced_search_system.strategies.base_strategy import (
            _OBSERVATION_DETAIL_MAX_CHARS,
        )

        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        results = [
            {"title": "T" * 200, "link": f"http://x{i}.com"} for i in range(40)
        ]
        strategy._emit_search_results_observation(
            iteration=1, questions=["Q"], results=results, progress_percent=40
        )

        content = callback.call_args[0][2]["content"]
        assert len(content) == _OBSERVATION_DETAIL_MAX_CHARS + 2
        assert content.endswith(" …")


class TestAbstractMethod:
    """Tests for abstract method enforcement."""

    def test_cannot_instantiate_base_class(self):
        """Cannot instantiate BaseSearchStrategy directly."""
        with pytest.raises(TypeError, match="abstract"):
            BaseSearchStrategy()

    def test_subclass_must_implement_analyze_topic(self):
        """Subclass must implement analyze_topic."""

        class IncompleteStrategy(BaseSearchStrategy):
            pass

        with pytest.raises(TypeError, match="abstract"):
            IncompleteStrategy()


class TestStrategySubclassing:
    """Tests for strategy subclassing patterns."""

    def test_subclass_can_override_methods(self):
        """Subclass can override methods."""

        class CustomStrategy(BaseSearchStrategy):
            def analyze_topic(self, query: str) -> Dict:
                self._update_progress("Custom progress", 50)
                return {"custom": True}

            def _update_progress(self, message, progress=None, metadata=None):
                # Custom progress handling
                super()._update_progress(
                    f"[Custom] {message}", progress, metadata
                )

        strategy = CustomStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)

        result = strategy.analyze_topic("test")

        assert result["custom"] is True
        callback.assert_called_once()
        assert "[Custom]" in callback.call_args[0][0]

    def test_subclass_can_add_attributes(self):
        """Subclass can add custom attributes."""

        class ExtendedStrategy(BaseSearchStrategy):
            def __init__(self, custom_param=None, **kwargs):
                super().__init__(**kwargs)
                self.custom_param = custom_param

            def analyze_topic(self, query: str) -> Dict:
                return {"param": self.custom_param}

        strategy = ExtendedStrategy(custom_param="test_value")

        assert strategy.custom_param == "test_value"
        result = strategy.analyze_topic("query")
        assert result["param"] == "test_value"


class TestDetailListOneLinePerEntry:
    """Every entry in an expandable detail list must occupy exactly one
    line.

    The step message states a count ("Found 3 results", "Searching with 5
    questions") and the detail lists that many bullets. Externally sourced
    text — scraped page titles, PDF metadata, LLM-generated questions —
    can contain newlines, which would split one entry across two visual
    lines (the second with no bullet marker), so the rendered list stops
    matching the stated count.
    """

    def _emit(self, method, **kwargs):
        strategy = ConcreteStrategy()
        callback = Mock()
        strategy.set_progress_callback(callback)
        getattr(strategy, method)(**kwargs)
        callback.assert_called_once()
        return callback.call_args[0]

    def test_result_title_with_newline_stays_one_bullet(self):
        message, _, metadata = self._emit(
            "_emit_search_results_observation",
            iteration=1,
            questions=["Q1"],
            results=[
                {
                    "title": "Fed holds rates\nMarkets react",
                    "link": "http://r.com",
                },
                {"title": "Second", "link": "http://s.com"},
            ],
            progress_percent=40,
        )
        content = metadata["content"]
        assert content.count("\n") == 1  # 2 entries -> exactly 1 separator
        assert len(content.splitlines()) == 2
        assert all(ln.startswith("• ") for ln in content.splitlines())
        assert "• Fed holds rates Markets react (http://r.com)" in content
        assert "Found 2 results" in message

    def test_result_title_without_link_stays_one_bullet(self):
        _, _, metadata = self._emit(
            "_emit_search_results_observation",
            iteration=1,
            questions=["Q1"],
            results=[{"title": "No link\nsecond line"}],
            progress_percent=40,
        )
        assert metadata["content"] == "• No link second line"

    def test_searched_question_with_newline_stays_one_bullet(self):
        _, _, metadata = self._emit(
            "_emit_searching_progress",
            iteration=1,
            questions=["What is A?\nAnd also B?", "Plain question"],
            progress_percent=25,
        )
        content = metadata["content"]
        assert len(content.splitlines()) == 2
        assert "• What is A? And also B?" in content

    def test_previous_question_with_newline_stays_one_bullet(self):
        strategy = ConcreteStrategy()
        strategy.questions_by_iteration = {1: ["Multi\nline question", "Plain"]}
        callback = Mock()
        strategy.set_progress_callback(callback)
        strategy._emit_question_generation_progress(
            iteration=2, progress_percent=50, source_count=4
        )
        content = callback.call_args[0][2]["content"]
        # "Previous questions:" header + one line per question
        assert len(content.splitlines()) == 3
        assert "• Multi line question" in content

    def test_carriage_returns_and_tabs_are_flattened(self):
        """\\r, \\r\\n and tabs break the one-line contract just as \\n does."""
        _, _, metadata = self._emit(
            "_emit_searching_progress",
            iteration=1,
            questions=["bare\rCR", "crlf\r\nhere", "tab\tseparated"],
            progress_percent=25,
        )
        content = metadata["content"]
        assert len(content.splitlines()) == 3
        assert "• bare CR" in content
        assert "• crlf here" in content
        assert "• tab separated" in content

    def test_iteration_one_query_with_carriage_return_stays_one_line(self):
        message, _, _ = self._emit(
            "_emit_question_generation_progress",
            iteration=1,
            progress_percent=10,
            query="line one\r\nline two\rline three",
        )
        assert "\n" not in message and "\r" not in message
        assert "line one line two line three" in message


class TestFormatCitations:
    """_format_citations forwards content prose so uncited hits leave
    the Sources block (#5379)."""

    def _results(self):
        return [
            {"title": "A", "link": "https://cited.test/a", "index": "1"},
            {"title": "B", "link": "https://uncited.test/b", "index": "2"},
        ]

    def test_format_citations_forwards_body_to_filter(self, monkeypatch):
        """Regression: dropping prose= would silently disable filtering."""
        import local_deep_research.utilities.search_utilities as su

        captured = {}

        def mock_format(all_links, prose=None, uncited_mode="fallback"):
            captured["prose"] = prose
            captured["uncited_mode"] = uncited_mode
            return "formatted"

        monkeypatch.setattr(su, "format_links_to_markdown", mock_format)
        strategy = ConcreteStrategy()

        out = strategy._format_citations("Body text [1].", self._results())

        assert "## Sources" in out
        assert captured["prose"] == "Body text [1]."
        assert captured["uncited_mode"] == "fallback"

    def test_format_citations_filters_uncited_sources(self):
        strategy = ConcreteStrategy()

        out = strategy._format_citations(
            "Findings show progress [1].", self._results()
        )

        assert "https://cited.test/a" in out
        assert "https://uncited.test/b" not in out

    def test_format_citations_disabled_snapshot_keeps_all(self):
        strategy = ConcreteStrategy(
            settings_snapshot={"report.uncited_sources_mode": "disabled"}
        )

        out = strategy._format_citations(
            "Findings show progress [1].", self._results()
        )

        assert "https://cited.test/a" in out
        assert "https://uncited.test/b" in out

    def test_format_citations_strict_snapshot_drops_uncited(self):
        strategy = ConcreteStrategy(
            settings_snapshot={"report.uncited_sources_mode": "strict"}
        )

        out = strategy._format_citations("No citations here.", self._results())

        assert out == "No citations here."

    def test_format_citations_keeps_a_fetched_page_title(self):
        """This path runs the results through
        ``extract_links_from_search_results`` first, and that rebuilds each
        link from a fixed key list. The page title a fetch recorded was not
        on it, so the live report kept rendering the search listing's
        spelling while a direct render of the same entry showed the clean
        one (#6024).
        """
        strategy = ConcreteStrategy()
        results = [
            {
                "title": "Listing title | SEO suffix",
                "link": "https://cited.test/a",
                "index": "1",
                FETCHED_TITLE_KEY: "Page title",
            }
        ]

        out = strategy._format_citations("Findings [1].", results)

        assert "Page title" in out
        assert "SEO suffix" not in out

    def test_format_citations_empty_results_unchanged(self):
        strategy = ConcreteStrategy()

        assert strategy._format_citations("Body [1].", []) == "Body [1]."


class TestFirstUsableText:
    """#6941: the centralized content-or-response predicate the three
    strategy assembly sites share."""

    def test_prefers_content_when_usable(self):
        strategy = ConcreteStrategy()
        assert (
            strategy.first_usable_text({"content": "Synthesized"})
            == "Synthesized"
        )

    def test_content_wins_over_a_usable_response(self):
        # Both fields usable: only the candidate order decides, so this is
        # the test that fails if content and response are swapped.
        strategy = ConcreteStrategy()
        assert (
            strategy.first_usable_text(
                {"content": "From content", "response": "From response"},
                "fallback",
            )
            == "From content"
        )

    def test_content_wins_regardless_of_key_insertion_order(self):
        # Precedence is the candidate tuple, not the mapping's key order.
        strategy = ConcreteStrategy()
        assert (
            strategy.first_usable_text(
                {"response": "From response", "content": "From content"}
            )
            == "From content"
        )

    def test_content_wins_for_non_dict_mappings_and_static_calls(self):
        # A read-only mapping, called both through the class (it is a
        # staticmethod) and through an instance.
        result = MappingProxyType(
            {"content": "From content", "response": "From response"}
        )
        assert ConcreteStrategy.first_usable_text(result) == "From content"
        assert ConcreteStrategy().first_usable_text(result) == "From content"

    @pytest.mark.parametrize(
        "content",
        ["", "   ", " \n\t", None],
        ids=["empty", "spaces", "whitespace", "none"],
    )
    def test_response_is_used_when_content_is_empty_blank_or_none(
        self, content
    ):
        strategy = ConcreteStrategy()
        assert (
            strategy.first_usable_text(
                {"content": content, "response": "From response"},
                "fallback",
            )
            == "From response"
        )

    def test_falls_back_to_response_when_content_is_unusable(self):
        strategy = ConcreteStrategy()
        result = strategy.first_usable_text(
            {"content": " \n\t", "response": "From response"}
        )
        assert result == "From response"

    def test_blank_or_non_string_candidates_do_not_hide_a_valid_sibling(
        self,
    ):
        strategy = ConcreteStrategy()
        assert (
            strategy.first_usable_text(
                {"content": ["unexpected"], "response": "From response"}
            )
            == "From response"
        )
        assert strategy.first_usable_text({"content": 7}) is None

    def test_returns_the_default_when_neither_field_is_usable(self):
        strategy = ConcreteStrategy()
        assert strategy.first_usable_text({}, "fallback") == "fallback"
        assert (
            strategy.first_usable_text(
                {"content": "", "response": None}, "fallback"
            )
            == "fallback"
        )
        assert strategy.first_usable_text({}) is None