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.

31.7 KB
Raw Blame
"""Tests for _create_full_search_wrapper in search_engine_factory.py.

The wrapper now receives engine_config (with registry-injected module/class
data) directly instead of re-extracting it from the settings snapshot.
"""

from unittest.mock import Mock, patch

import pytest

from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
)
from local_deep_research.web_search_engines import search_engine_factory
from local_deep_research.web_search_engines.engine_registry import (
    ENGINE_REGISTRY,
)
from local_deep_research.web_search_engines.engines.full_search import (
    FullSearchResults,
)
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
)
from local_deep_research.web_search_engines.search_engine_factory import (
    _create_full_search_wrapper,
    create_search_engine,
    get_search,
)


_TEST_EGRESS_CONTEXT = EgressContext(
    scope=EgressScope.BOTH,
    primary_engine="searxng",
    require_local_llm=False,
    require_local_embeddings=False,
)


@pytest.fixture
def base_engine():
    """Provide a mock base engine."""
    return Mock()


@pytest.fixture
def mock_llm():
    """Provide a mock LLM."""
    return Mock()


class TestWrapperCreationConditions:
    """Tests for when the wrapper is and isn't created."""

    def test_wrapper_returns_base_engine_when_no_full_search_config(
        self, base_engine, mock_llm
    ):
        """Missing full_search_module/class in engine_config -> returns base engine."""
        engine_config = {
            "class_name": "TestEngine",
            # No full_search_module or full_search_class
        }

        result = _create_full_search_wrapper(
            "test_engine",
            base_engine,
            engine_config,
            mock_llm,
            {},
        )

        assert result is base_engine

    def test_wrapper_returns_base_engine_on_exception(
        self, base_engine, mock_llm
    ):
        """Exception in wrapper creation -> fallback to base engine."""
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            side_effect=ImportError("module not found"),
        ):
            result = _create_full_search_wrapper(
                "test_engine",
                base_engine,
                engine_config,
                mock_llm,
                {},
            )

        assert result is base_engine


class _FakeWrapperAcceptsAll:
    """Fake wrapper class that accepts specific init params."""

    def __init__(self, llm=None, web_search=None):
        self.llm = llm
        self.web_search = web_search


class _FakeWrapperWithApiKey:
    """Fake wrapper class that accepts api_key and llm."""

    def __init__(self, api_key=None, llm=None):
        self.api_key = api_key
        self.llm = llm


class _FakeWrapperWithSnapshot:
    """Fake wrapper class that accepts settings_snapshot."""

    def __init__(self, llm=None, web_search=None, settings_snapshot=None):
        self.llm = llm
        self.web_search = web_search
        self.settings_snapshot = settings_snapshot


class TestWrapperParameterFiltering:
    """Tests for parameter filtering during wrapper creation."""

    def test_wrapper_filters_init_params(self, base_engine, mock_llm):
        """Only params accepted by wrapper __init__ are passed."""
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }

        params = {
            "llm": mock_llm,
            "web_search": base_engine,
            "unsupported_param": "should_be_filtered",
            "another_unsupported": 42,
        }

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=_FakeWrapperAcceptsAll,
        ):
            result = _create_full_search_wrapper(
                "test_engine",
                base_engine,
                engine_config,
                mock_llm,
                params,
            )

        assert isinstance(result, _FakeWrapperAcceptsAll)
        assert result.llm is mock_llm
        assert result.web_search is base_engine

    def test_wrapper_forwards_settings_snapshot(self, base_engine, mock_llm):
        """The factory's settings_snapshot argument is forwarded when accepted."""
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }
        settings_snapshot = {
            "search.quality_check_urls": {"value": False},
        }

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=_FakeWrapperWithSnapshot,
        ):
            result = _create_full_search_wrapper(
                "test_engine",
                base_engine,
                engine_config,
                mock_llm,
                {},
                settings_snapshot=settings_snapshot,
            )

        assert isinstance(result, _FakeWrapperWithSnapshot)
        assert result.settings_snapshot is settings_snapshot


class TestApiKeyExtraction:
    """Tests for API key extraction from settings snapshots."""

    def test_api_key_from_settings_snapshot_dict_format(
        self, base_engine, mock_llm
    ):
        """API key extraction from {"value": "key"} format in settings."""
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }
        settings_snapshot = {
            "search.engine.web.brave.api_key": {"value": "test-api-key-123"},
        }

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=_FakeWrapperWithApiKey,
        ):
            result = _create_full_search_wrapper(
                "brave",
                base_engine,
                engine_config,
                mock_llm,
                {},
                settings_snapshot=settings_snapshot,
            )

        assert isinstance(result, _FakeWrapperWithApiKey)
        assert result.api_key == "test-api-key-123"

    def test_api_key_from_settings_snapshot_plain_format(
        self, base_engine, mock_llm
    ):
        """API key extraction from plain string format in settings."""
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }
        settings_snapshot = {
            "search.engine.web.brave.api_key": "plain-api-key-456",
        }

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=_FakeWrapperWithApiKey,
        ):
            result = _create_full_search_wrapper(
                "brave",
                base_engine,
                engine_config,
                mock_llm,
                {},
                settings_snapshot=settings_snapshot,
            )

        assert isinstance(result, _FakeWrapperWithApiKey)
        assert result.api_key == "plain-api-key-456"

    def test_api_key_missing_from_settings(self, base_engine, mock_llm):
        """When settings key is missing, wrapper still works — API key
        may come from other sources or be optional."""
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }
        settings_snapshot = {
            # No api_key in settings
        }

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=_FakeWrapperWithApiKey,
        ):
            result = _create_full_search_wrapper(
                "brave",
                base_engine,
                engine_config,
                mock_llm,
                {},
                settings_snapshot=settings_snapshot,
            )

        assert isinstance(result, _FakeWrapperWithApiKey)
        # api_key should be None since it's not in settings
        assert result.api_key is None


class TestWrapperExtractionLanguage:
    """Provider ISO codes must not be forwarded as justext language names."""

    def test_wrapper_uses_language_name_not_provider_code(
        self, base_engine, mock_llm
    ):
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }
        params = {
            "language": "de",
            "search_language": "German",
            "max_results": 10,
        }

        result = _create_full_search_wrapper(
            "searxng",
            base_engine,
            engine_config,
            mock_llm,
            params,
            settings_snapshot={"search.search_language": {"value": "German"}},
            egress_context=_TEST_EGRESS_CONTEXT,
        )

        assert isinstance(result, FullSearchResults)
        assert result.language == "German"

    def test_wrapper_reverse_maps_iso_code_when_name_absent(
        self, base_engine, mock_llm
    ):
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }

        result = _create_full_search_wrapper(
            "searxng",
            base_engine,
            engine_config,
            mock_llm,
            {"language": "de", "max_results": 10},
            egress_context=_TEST_EGRESS_CONTEXT,
        )

        assert isinstance(result, FullSearchResults)
        assert result.language == "German"

    def test_wrapper_drops_unknown_iso_code(self, base_engine, mock_llm):
        engine_config = {
            "full_search_module": ".engines.full_search",
            "full_search_class": "FullSearchResults",
        }

        result = _create_full_search_wrapper(
            "searxng",
            base_engine,
            engine_config,
            mock_llm,
            {"language": "xx", "max_results": 10},
            egress_context=_TEST_EGRESS_CONTEXT,
        )

        assert isinstance(result, FullSearchResults)
        assert result.language == "English"


# ---------------------------------------------------------------------------
# Tests: End-to-end get_search() FullSearchResults wrapper coverage (#5883)
# ---------------------------------------------------------------------------

FULL_SEARCH_ENGINES = sorted(
    name
    for name, entry in ENGINE_REGISTRY.items()
    if entry.full_search_class is not None
)


class _MockBaseEngine(BaseSearchEngine):
    """Mock search engine for testing factory routing without network calls."""

    def __init__(self, *args, **kwargs):
        self.language = kwargs.get("language")
        super().__init__(*args, **kwargs)

    def _get_previews(self, query: str):
        return []


class TestRegistryFullSearchWrapperEndToEnd:
    """End-to-end tests verifying get_search wraps engines declaring full_search_class.

    Covers the full path from search_snippets_only setting to FullSearchResults wrapper
    for every engine in ENGINE_REGISTRY that declares a full_search_class (#5883).
    """

    @pytest.fixture(autouse=True)
    def mock_engine_construction(self, monkeypatch):
        """Mock get_safe_module_class so base engine construction succeeds without network."""
        real_get_safe = search_engine_factory.get_safe_module_class

        def _fake_get_safe_module_class(module_path: str, class_name: str):
            if class_name == "FullSearchResults":
                return real_get_safe(module_path, class_name)
            return _MockBaseEngine

        monkeypatch.setattr(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            _fake_get_safe_module_class,
        )

    def test_full_search_registry_not_empty(self):
        """Ensure ENGINE_REGISTRY contains engines declaring full_search_class."""
        assert len(FULL_SEARCH_ENGINES) >= 5
        assert set(FULL_SEARCH_ENGINES).issuperset(
            {"brave", "google_pse", "mojeek", "searxng", "serpapi"}
        )

    @pytest.mark.parametrize("engine_name", FULL_SEARCH_ENGINES)
    def test_get_search_snippets_only_false_wraps_in_full_search(
        self, engine_name, mock_llm
    ):
        """When search_snippets_only=False and supports_full_search=True, get_search wraps in FullSearchResults."""
        settings_snapshot = {
            "search.tool": {"value": engine_name},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

        result = get_search(
            search_tool=engine_name,
            llm_instance=mock_llm,
            search_snippets_only=False,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        assert isinstance(result, FullSearchResults), (
            f"Expected {engine_name} to be wrapped in FullSearchResults when search_snippets_only=False"
        )
        assert isinstance(result.web_search, _MockBaseEngine)
        assert result.llm is mock_llm
        assert result.egress_context is not None

    @pytest.mark.parametrize("engine_name", FULL_SEARCH_ENGINES)
    def test_get_search_snippets_only_true_returns_bare_engine(
        self, engine_name, mock_llm
    ):
        """When search_snippets_only=True, get_search returns the unwrapped bare engine."""
        settings_snapshot = {
            "search.tool": {"value": engine_name},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

        result = get_search(
            search_tool=engine_name,
            llm_instance=mock_llm,
            search_snippets_only=True,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        assert not isinstance(result, FullSearchResults), (
            f"Expected {engine_name} to NOT be wrapped in FullSearchResults when search_snippets_only=True"
        )
        assert isinstance(result, _MockBaseEngine)

    @pytest.mark.parametrize("engine_name", FULL_SEARCH_ENGINES)
    def test_get_search_supports_full_search_false_returns_bare_engine(
        self, engine_name, mock_llm
    ):
        """When supports_full_search=False in settings, get_search returns bare engine even with search_snippets_only=False."""
        settings_snapshot = {
            "search.tool": {"value": engine_name},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": False,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

        result = get_search(
            search_tool=engine_name,
            llm_instance=mock_llm,
            search_snippets_only=False,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        assert not isinstance(result, FullSearchResults), (
            f"Expected {engine_name} to NOT be wrapped in FullSearchResults when supports_full_search=False"
        )
        assert isinstance(result, _MockBaseEngine)

    @pytest.mark.parametrize("engine_name", FULL_SEARCH_ENGINES)
    def test_create_search_engine_snapshot_snippets_only_false_wraps(
        self, engine_name, mock_llm
    ):
        """Direct create_search_engine callers without explicit flags (e.g. MCP) wrap when
        the snapshot says search.snippets_only=False, matching get_search."""
        settings_snapshot = {
            "search.tool": {"value": engine_name},
            "search.snippets_only": {"value": False},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

        result = create_search_engine(
            engine_name,
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        assert isinstance(result, FullSearchResults), (
            f"Expected {engine_name} to be wrapped in FullSearchResults when "
            "search.snippets_only=False via settings_snapshot"
        )
        assert isinstance(result.web_search, _MockBaseEngine)

    @pytest.mark.parametrize("engine_name", FULL_SEARCH_ENGINES)
    def test_create_search_engine_snapshot_snippets_only_true_bare(
        self, engine_name, mock_llm
    ):
        """Direct create_search_engine callers stay unwrapped when snippets_only=True."""
        settings_snapshot = {
            "search.tool": {"value": engine_name},
            "search.snippets_only": {"value": True},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

        result = create_search_engine(
            engine_name,
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        assert not isinstance(result, FullSearchResults), (
            f"Expected {engine_name} to stay unwrapped when search.snippets_only=True"
        )
        assert isinstance(result, _MockBaseEngine)

    @pytest.mark.parametrize("engine_name", FULL_SEARCH_ENGINES)
    def test_create_search_engine_explicit_use_full_search_false_wins(
        self, engine_name, mock_llm
    ):
        """Explicit use_full_search=False is not overridden by snippets_only=False."""
        settings_snapshot = {
            "search.tool": {"value": engine_name},
            "search.snippets_only": {"value": False},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

        result = create_search_engine(
            engine_name,
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
            use_full_search=False,
        )

        assert not isinstance(result, FullSearchResults)

    @pytest.mark.parametrize("engine_name", FULL_SEARCH_ENGINES)
    def test_create_search_engine_forced_snippets_only_beats_snapshot_false(
        self, engine_name, mock_llm
    ):
        """Explicit search_snippets_only=True and use_full_search=False beats snapshot snippets_only=False."""
        settings_snapshot = {
            "search.tool": {"value": engine_name},
            "search.snippets_only": {"value": False},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

        result = create_search_engine(
            engine_name,
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
            search_snippets_only=True,
            use_full_search=False,
        )

        assert not isinstance(result, FullSearchResults)
        assert result.search_snippets_only is True

    def _quality_check_disabled_snapshot(self, engine_name):
        return {
            "search.tool": {"value": engine_name},
            "search.snippets_only": {"value": False},
            "search.quality_check_urls": {"value": False},
            f"search.engine.web.{engine_name}.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            f"search.engine.web.{engine_name}.api_key": {
                "value": "mock-api-key"
            },
        }

    def _assert_factory_wrapper_skips_url_filter(
        self, wrapper, snapshot, mock_llm
    ):
        """Run the real FullSearchResults path and require the LLM URL filter off."""
        assert isinstance(wrapper, FullSearchResults)
        assert wrapper.settings_snapshot is snapshot

        wrapper.web_search.invoke = Mock(
            return_value=[
                {"link": "https://example.com/1", "title": "Result 1"},
                {"link": "https://example.com/2", "title": "Result 2"},
            ]
        )
        mock_llm.reset_mock()

        with (
            patch(
                "local_deep_research.web_search_engines.engines.full_search.QUALITY_CHECK_DDG_URLS",
                True,
            ),
            patch(
                "local_deep_research.web_search_engines.engines.full_search.validate_url",
                return_value=True,
            ),
            patch(
                "local_deep_research.web_search_engines.engines.full_search.batch_fetch_and_extract",
                return_value={
                    "https://example.com/1": "Content 1",
                    "https://example.com/2": "Content 2",
                },
            ),
        ):
            results = wrapper.run("test query")

        mock_llm.invoke.assert_not_called()
        assert len(results) == 2

    def test_get_search_quality_check_urls_false_skips_llm_filter(
        self, mock_llm
    ):
        """get_search must forward the snapshot so quality_check_urls=False works."""
        engine_name = "searxng"
        settings_snapshot = self._quality_check_disabled_snapshot(engine_name)

        wrapper = get_search(
            search_tool=engine_name,
            llm_instance=mock_llm,
            search_snippets_only=False,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        self._assert_factory_wrapper_skips_url_filter(
            wrapper, settings_snapshot, mock_llm
        )

    def test_create_search_engine_quality_check_urls_false_skips_llm_filter(
        self, mock_llm
    ):
        """create_search_engine must forward the snapshot so the toggle works."""
        engine_name = "searxng"
        settings_snapshot = self._quality_check_disabled_snapshot(engine_name)

        wrapper = create_search_engine(
            engine_name,
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        self._assert_factory_wrapper_skips_url_filter(
            wrapper, settings_snapshot, mock_llm
        )

    def test_searxng_keeps_provider_code_off_wrapper_language(self, mock_llm):
        settings_snapshot = {
            "search.tool": {"value": "searxng"},
            "search.snippets_only": {"value": False},
            "search.search_language": {"value": "German"},
            "search.engine.web.searxng.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
        }

        result = create_search_engine(
            "searxng",
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
            programmatic_mode=True,
        )

        assert isinstance(result, FullSearchResults)
        assert result.language == "German"
        assert result.web_search.language == "de"


# ---------------------------------------------------------------------------
# Tests: the private result-fetch grant on the wrapper (#2477)
# ---------------------------------------------------------------------------

_WRAPPER_CONFIG = {
    "full_search_module": ".engines.full_search",
    "full_search_class": "FullSearchResults",
}


class _GrantedEngine(_MockBaseEngine):
    """Engine stub that resolved a private result-fetch grant, the way
    SearXNG does from the instance approval plus the env-only opt-in."""

    allow_private_result_fetch = True


class TestWrapperEgressContext:
    def test_missing_context_returns_snippets_only_engine(self, mock_llm):
        engine = _MockBaseEngine(llm=mock_llm, programmatic_mode=True)
        result = _create_full_search_wrapper(
            "searxng", engine, dict(_WRAPPER_CONFIG), mock_llm, {}
        )
        assert result is engine

    def test_caller_params_cannot_supply_or_replace_context(self, mock_llm):
        engine = _MockBaseEngine(llm=mock_llm, programmatic_mode=True)
        forged = object()
        params = {"egress_context": forged}

        without_context = _create_full_search_wrapper(
            "searxng", engine, dict(_WRAPPER_CONFIG), mock_llm, params
        )
        assert without_context is engine

        with_context = _create_full_search_wrapper(
            "searxng",
            engine,
            dict(_WRAPPER_CONFIG),
            mock_llm,
            params,
            egress_context=_TEST_EGRESS_CONTEXT,
        )
        assert isinstance(with_context, FullSearchResults)
        assert with_context.egress_context is _TEST_EGRESS_CONTEXT


class TestWrapperPrivateResultFetchGrant:
    """``FullSearchResults.allow_private_ips`` is set from the base engine's
    resolved grant only (``BaseSearchEngine.allow_private_result_fetch``),
    defaults to False, and cannot be opened from caller params."""

    def _wrap(self, base_engine, mock_llm, params=None):
        return _create_full_search_wrapper(
            "test_engine",
            base_engine,
            dict(_WRAPPER_CONFIG),
            mock_llm,
            params or {},
            egress_context=_TEST_EGRESS_CONTEXT,
        )

    def test_engine_without_grant_keeps_wrapper_strict(self, mock_llm):
        engine = _MockBaseEngine(llm=mock_llm, programmatic_mode=True)
        wrapper = self._wrap(engine, mock_llm)
        assert isinstance(wrapper, FullSearchResults)
        assert wrapper.allow_private_ips is False

    def test_engine_grant_reaches_wrapper(self, mock_llm):
        engine = _GrantedEngine(llm=mock_llm, programmatic_mode=True)
        wrapper = self._wrap(engine, mock_llm)
        assert isinstance(wrapper, FullSearchResults)
        assert wrapper.web_search is engine
        assert wrapper.allow_private_ips is True

    def test_caller_params_cannot_open_the_grant(self, mock_llm):
        """The signature filter would otherwise forward a stray
        ``allow_private_ips`` kwarg; the engine's own resolution is the
        only source."""
        engine = _MockBaseEngine(llm=mock_llm, programmatic_mode=True)
        wrapper = self._wrap(engine, mock_llm, {"allow_private_ips": True})
        assert wrapper.allow_private_ips is False

    @pytest.mark.parametrize(
        "grant", ["true", 1, None], ids=["str", "int", "none"]
    )
    def test_non_bool_grant_fails_closed(self, mock_llm, grant):
        engine = _MockBaseEngine(llm=mock_llm, programmatic_mode=True)
        engine.allow_private_result_fetch = grant
        assert self._wrap(engine, mock_llm).allow_private_ips is False

    def test_non_engine_base_fails_closed(self, mock_llm):
        """A base that is not a BaseSearchEngine (attribute access on a
        Mock yields a truthy Mock) hands over no grant."""
        assert self._wrap(Mock(), mock_llm).allow_private_ips is False

    def test_registry_engines_hold_no_grant_by_default(self):
        """Only an engine that resolves a grant in ``__init__`` (SearXNG)
        can hand one over; the class default everywhere else stays False,
        so the other wrapped engines are untouched."""
        from local_deep_research.security.module_whitelist import (
            get_safe_module_class,
        )

        assert BaseSearchEngine.allow_private_result_fetch is False
        for name in FULL_SEARCH_ENGINES:
            entry = ENGINE_REGISTRY[name]
            engine_class = get_safe_module_class(
                entry.module_path, entry.class_name
            )
            assert engine_class.allow_private_result_fetch is False, name


class TestFetchProgressPipelineWiring:
    """Pipeline-strategy live path for bulk-fetch progress (#7193)."""

    def test_get_search_forwards_progress_callback_to_wrapper(self, mock_llm):
        """get_search(..., progress_callback=hook) reaches FullSearchResults."""
        hook = Mock()
        settings_snapshot = {
            "search.tool": {"value": "searxng"},
            "search.engine.web.searxng.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            "search.engine.web.searxng.api_key": {"value": "mock-api-key"},
        }
        # Patch the engine class resolution to avoid live imports.
        from local_deep_research.web_search_engines import (
            search_engine_factory,
        )

        real_gsmc = search_engine_factory.get_safe_module_class

        def _fake_gsmc(module_path: str, class_name: str):
            if class_name == "FullSearchResults":
                return real_gsmc(module_path, class_name)
            return _MockBaseEngine

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            _fake_gsmc,
        ):
            result = get_search(
                search_tool="searxng",
                llm_instance=mock_llm,
                search_snippets_only=False,
                settings_snapshot=settings_snapshot,
                programmatic_mode=True,
                progress_callback=hook,
            )
        assert isinstance(result, FullSearchResults)
        assert result.progress_callback is hook

    def test_base_engine_set_fetch_progress_forwards_to_full_search(
        self, mock_llm
    ):
        """BaseSearchEngine late-binding reaches an existing wrapper."""
        engine = _MockBaseEngine(llm=mock_llm, programmatic_mode=True)
        engine.include_full_content = True
        engine.full_search = FullSearchResults(llm=mock_llm, web_search=Mock())
        hook = Mock()
        engine.set_fetch_progress_callback(hook)
        assert engine._fetch_progress_callback is hook
        assert engine.full_search.progress_callback is hook

    def test_create_search_engine_stamps_base_engine_hook(self, mock_llm):
        """Direct create_search_engine(..., progress_callback) stamps base."""
        from local_deep_research.web_search_engines import (
            search_engine_factory,
        )

        hook = Mock()
        settings_snapshot = {
            "search.tool": {"value": "searxng"},
            "search.engine.web.searxng.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
            "search.engine.web.searxng.api_key": {"value": "mock-api-key"},
        }
        real_gsmc = search_engine_factory.get_safe_module_class

        def _fake_gsmc(module_path: str, class_name: str):
            if class_name == "FullSearchResults":
                return real_gsmc(module_path, class_name)
            return _MockBaseEngine

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            _fake_gsmc,
        ):
            result = create_search_engine(
                "searxng",
                llm=mock_llm,
                settings_snapshot=settings_snapshot,
                programmatic_mode=True,
                search_snippets_only=True,
                use_full_search=False,
                progress_callback=hook,
            )
        # Snippets-only: no wrapper, but the base engine keeps the hook
        # for a future _init_full_search call.
        assert not isinstance(result, FullSearchResults)
        assert result._fetch_progress_callback is hook