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.

88.1 KB
Raw Blame
"""
Comprehensive tests for search_engine_factory.py covering gaps not
addressed by existing test_search_engine_factory.py and
test_factory_full_search_wrapper.py.

Focus areas:
- API key retrieval paths (settings dict, plain value, engine_config fallback)
- LLM pass-through when not required but engine accepts it
- kwargs overriding default_params
- max_results default logic end-to-end
- Display label fallback end-to-end through create_search_engine
- use_full_search triggering the wrapper path
- LLM relevance filter application on engine instances
- get_search parameter routing for wikinews, max_filtered_results
- Edge cases: empty default_params, engine with **kwargs signature
"""

import inspect as _inspect
from unittest.mock import Mock, patch

import pytest


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_engine_class(*param_names, class_attrs=None):
    """Create a real class with a controlled __init__ signature.

    Builds a class whose inspect.signature(__init__) exposes exactly
    the requested parameter names, but whose __init__ actually accepts
    **kwargs so the factory can instantiate it.
    """
    params = [
        _inspect.Parameter("self", _inspect.Parameter.POSITIONAL_OR_KEYWORD)
    ]
    for name in param_names:
        params.append(
            _inspect.Parameter(
                name, _inspect.Parameter.POSITIONAL_OR_KEYWORD, default=None
            )
        )

    class _Eng:
        _call_kwargs = None

        def __init__(self, **kwargs):
            _Eng._call_kwargs = kwargs
            # Set kwargs as instance attributes so the factory can inspect them
            # (e.g. hasattr(engine, "llm") and engine.llm)
            for k, v in kwargs.items():
                setattr(self, k, v)

    _Eng.__init__.__signature__ = _inspect.Signature(params)

    # Attach optional class-level attributes (e.g. is_scientific)
    if class_attrs:
        for k, v in class_attrs.items():
            setattr(_Eng, k, v)

    return _Eng


def _engine_config(**overrides):
    """Return a minimal engine configuration dict."""
    base = {
        "module_path": "some.module",
        "class_name": "SomeEngine",
        "default_params": {},
    }
    base.update(overrides)
    return base


def _patches(registry_return=None, config_return=None, class_return=None):
    """Context-manager helper that patches registry, search_config, and get_safe_module_class."""
    import contextlib

    @contextlib.contextmanager
    def _ctx():
        # Mock evaluate_engine so these factory tests (which use fake
        # engine names like "myeng") don't get rejected by the egress
        # PEP for not being in ENGINE_REGISTRY. The PEP is exercised
        # separately in tests/security/test_egress_policy.py.
        from local_deep_research.security.egress.policy import Decision

        with (
            patch(
                "local_deep_research.web_search_engines.search_engine_factory.retriever_registry"
            ) as mock_reg,
            patch(
                "local_deep_research.web_search_engines.search_engine_factory.search_config"
            ) as mock_sc,
            patch(
                "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class"
            ) as mock_gsmc,
            patch(
                "local_deep_research.security.egress.policy.evaluate_engine",
                return_value=Decision(True, "test_bypass"),
            ),
        ):
            mock_reg.get.return_value = registry_return
            if config_return is not None:
                mock_sc.return_value = config_return
            if class_return is not None:
                mock_gsmc.return_value = class_return
            yield mock_reg, mock_sc, mock_gsmc

    return _ctx()


# ---------------------------------------------------------------------------
# Tests: API key retrieval
# ---------------------------------------------------------------------------


class TestApiKeyRetrieval:
    """Test the various paths for obtaining API keys."""

    def test_api_key_from_settings_snapshot_dict_format(self):
        """API key found in settings_snapshot as {value: ...} dict."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("api_key", "max_results")
        snapshot = {
            "search.engine.web.myeng.api_key": {"value": "secret-key-123"},
        }

        with _patches(
            config_return={"myeng": _engine_config(requires_api_key=True)},
            class_return=EngCls,
        ):
            result = create_search_engine("myeng", settings_snapshot=snapshot)

        assert result is not None
        assert EngCls._call_kwargs["api_key"] == "secret-key-123"

    def test_api_key_from_settings_snapshot_plain_string(self):
        """API key found in settings_snapshot as a plain string."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("api_key", "max_results")
        snapshot = {
            "search.engine.web.myeng.api_key": "plain-key-456",
        }

        with _patches(
            config_return={"myeng": _engine_config(requires_api_key=True)},
            class_return=EngCls,
        ):
            result = create_search_engine("myeng", settings_snapshot=snapshot)

        assert result is not None
        assert EngCls._call_kwargs["api_key"] == "plain-key-456"

    def test_api_key_fallback_to_engine_config(self):
        """API key not in settings_snapshot but present in engine_config."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("api_key", "max_results")
        snapshot = {"dummy": "value"}  # no api_key setting

        with _patches(
            config_return={
                "myeng": _engine_config(
                    requires_api_key=True, api_key="from-config"
                )
            },
            class_return=EngCls,
        ):
            result = create_search_engine("myeng", settings_snapshot=snapshot)

        assert result is not None
        assert EngCls._call_kwargs["api_key"] == "from-config"


# ---------------------------------------------------------------------------
# Tests: LLM pass-through
# ---------------------------------------------------------------------------


class TestLlmPassThrough:
    """Test LLM injection into engine constructors."""

    def test_llm_passed_when_requires_llm_true(self):
        """LLM is always injected when requires_llm=True."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("llm", "max_results")
        mock_llm = Mock()

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            create_search_engine(
                "eng", llm=mock_llm, settings_snapshot={"x": 1}
            )

        assert EngCls._call_kwargs["llm"] is mock_llm

    def test_llm_passed_when_not_required_but_accepted_and_provided(self):
        """LLM is passed through when engine accepts it and it was provided,
        even without requires_llm=True."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("llm", "max_results")
        mock_llm = Mock()

        with _patches(
            config_return={"eng": _engine_config()},  # no requires_llm
            class_return=EngCls,
        ):
            create_search_engine(
                "eng", llm=mock_llm, settings_snapshot={"x": 1}
            )

        assert EngCls._call_kwargs["llm"] is mock_llm

    def test_llm_not_passed_when_engine_does_not_accept_it(self):
        """LLM is filtered out when engine __init__ doesn't list it."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")  # no 'llm' param
        mock_llm = Mock()

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine(
                "eng", llm=mock_llm, settings_snapshot={"x": 1}
            )

        assert "llm" not in EngCls._call_kwargs

    def test_llm_none_still_injected_when_requires_llm_true(self):
        """When requires_llm=True but llm=None, llm=None is still passed
        (engine handles degraded mode)."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("llm", "max_results")

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=None, settings_snapshot={"x": 1}
            )

        assert result is not None
        assert EngCls._call_kwargs["llm"] is None


# ---------------------------------------------------------------------------
# Tests: kwargs override default_params
# ---------------------------------------------------------------------------


class TestKwargsOverrideDefaults:
    """Test that kwargs override default_params from config."""

    def test_kwargs_override_default_params(self):
        """User-supplied kwargs should take precedence over default_params."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "region")

        with _patches(
            config_return={
                "eng": _engine_config(
                    default_params={"max_results": 5, "region": "us"}
                )
            },
            class_return=EngCls,
        ):
            create_search_engine(
                "eng",
                settings_snapshot={"x": 1},
                max_results=42,
            )

        # max_results from kwargs (42) should override default (5)
        assert EngCls._call_kwargs["max_results"] == 42
        # region from default_params should still be present
        assert EngCls._call_kwargs["region"] == "us"

    def test_empty_default_params(self):
        """Engine with no default_params should still work."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")

        with _patches(
            config_return={"eng": _engine_config(default_params={})},
            class_return=EngCls,
        ):
            create_search_engine(
                "eng", settings_snapshot={"x": 1}, max_results=10
            )

        assert EngCls._call_kwargs["max_results"] == 10

    def test_missing_default_params_key(self):
        """Config without default_params key should use empty dict."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")
        cfg = _engine_config()
        del cfg["default_params"]

        with _patches(
            config_return={"eng": cfg},
            class_return=EngCls,
        ):
            create_search_engine(
                "eng", settings_snapshot={"x": 1}, max_results=7
            )

        assert EngCls._call_kwargs["max_results"] == 7


# ---------------------------------------------------------------------------
# Tests: max_results defaults
# ---------------------------------------------------------------------------


class TestMaxResultsDefaults:
    """Test max_results default resolution through the actual function."""

    def test_max_results_from_settings_dict_value(self):
        """max_results from settings_snapshot as {value: N}."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")
        snapshot = {"search.max_results": {"value": 33}}

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("eng", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["max_results"] == 33

    def test_max_results_from_settings_plain_int(self):
        """max_results from settings_snapshot as plain integer."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")
        snapshot = {"search.max_results": 17}

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("eng", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["max_results"] == 17

    def test_max_results_default_20_when_not_in_settings(self):
        """max_results defaults to 20 when not in settings_snapshot."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")
        snapshot = {"dummy": "value"}

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("eng", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["max_results"] == 20

    def test_explicit_max_results_kwarg_not_overridden(self):
        """Explicit max_results kwarg should not be overridden by defaults."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")
        snapshot = {"search.max_results": {"value": 50}}

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine(
                "eng", settings_snapshot=snapshot, max_results=5
            )

        # Explicit kwarg should win
        assert EngCls._call_kwargs["max_results"] == 5


class TestTimePeriodSnapshotFallback:
    """create_search_engine() pulls search.time_period from the snapshot.

    Direct callers (langgraph-agent strategy tools, the MCP server) build
    engines via create_search_engine() without a time_period kwarg; the
    snapshot fallback is what makes the user's Settings → Search → Time
    Period choice reach them (#4936).
    """

    def test_time_period_from_snapshot_dict_value(self):
        """search.time_period as {value: ...} reaches a forwarding engine."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={"tavily": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "d"

    def test_time_period_from_snapshot_bare_value(self):
        """Bare snapshot values are honored too — run_research_process's
        per-request override writes the raw string, not a {value: ...} dict."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": "m"}

        with _patches(
            config_return={"tavily": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "m"

    def test_explicit_time_period_kwarg_beats_snapshot(self):
        """An explicit kwarg (e.g. from get_search) is never overridden."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={"tavily": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine(
                "tavily", settings_snapshot=snapshot, time_period="w"
            )

        assert EngCls._call_kwargs["time_period"] == "w"

    def test_per_engine_value_beats_snapshot(self):
        """An explicit (non-inherit) per-engine default_params.time_period
        overrides the global setting — the point of #5587's inherit design:
        a user who deliberately set the per-engine field wins."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={
                "tavily": _engine_config(default_params={"time_period": "y"})
            },
            class_return=EngCls,
        ):
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "y"

    def test_per_engine_inherit_falls_back_to_snapshot(self):
        """The "inherit" sentinel means "use the global setting"."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={
                "tavily": _engine_config(
                    default_params={"time_period": "inherit"}
                )
            },
            class_return=EngCls,
        ):
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "d"

    def test_inherit_falls_back_to_all_when_no_global_setting(self):
        """ "inherit" must never reach an engine: with no global setting in the
        snapshot the factory falls back to "all" (no filter), NOT the engine's
        class default — brave/serpapi default to a restrictive "y", which would
        silently re-pin them to Past-year in this corner."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"dummy": "value"}

        with _patches(
            config_return={
                "tavily": _engine_config(
                    default_params={"time_period": "inherit"}
                )
            },
            class_return=EngCls,
        ):
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "all"

    def test_explicit_kwarg_beats_per_engine_value(self):
        """An explicit caller kwarg outranks even a non-inherit per-engine
        value (top of the precedence chain)."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")

        with _patches(
            config_return={
                "tavily": _engine_config(default_params={"time_period": "y"})
            },
            class_return=EngCls,
        ):
            create_search_engine(
                "tavily",
                settings_snapshot={"search.time_period": {"value": "d"}},
                time_period="w",
            )

        assert EngCls._call_kwargs["time_period"] == "w"

    def test_brave_receives_global_setting(self):
        """Brave is in TIME_PERIOD_ENGINES since #5587: with no per-engine
        override it follows the global setting like the other engines."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={"brave": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("brave", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "d"

    def test_non_forwarding_engine_not_injected(self):
        """Engines outside TIME_PERIOD_ENGINES never get the global value
        injected."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={"myeng": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("myeng", settings_snapshot=snapshot)

        assert "time_period" not in EngCls._call_kwargs

    @pytest.mark.parametrize("junk", ["Y", " y", "7", "", None, 7, ["d"]])
    def test_invalid_per_engine_value_raises_instead_of_selecting_global(
        self, junk
    ):
        """Malformed per-engine choices fail; only explicit inherit uses global."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={
                "tavily": _engine_config(default_params={"time_period": junk})
            },
            class_return=EngCls,
        ):
            with pytest.raises(
                ValueError, match="tavily.default_params.time_period"
            ):
                create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs is None

    @pytest.mark.parametrize("junk", ["Y", "garbage", "", None, 7])
    def test_invalid_per_engine_value_without_global_raises(self, junk):
        """A missing global value cannot legitimize a malformed per-engine choice."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"dummy": "value"}

        with _patches(
            config_return={
                "tavily": _engine_config(default_params={"time_period": junk})
            },
            class_return=EngCls,
        ):
            with pytest.raises(
                ValueError, match="tavily.default_params.time_period"
            ):
                create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs is None

    def test_explicit_inherit_kwarg_resolves_to_global(self):
        """An explicit time_period="inherit" kwarg means "resolve normally"
        — identical to omitting the kwarg, so the global setting applies."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "d"}}

        with _patches(
            config_return={"tavily": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine(
                "tavily", settings_snapshot=snapshot, time_period="inherit"
            )

        assert EngCls._call_kwargs["time_period"] == "d"

    def test_corrupted_global_value_raises(self):
        """Global inherit/junk is invalid rather than an unrestricted search."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": {"value": "inherit"}}

        with _patches(
            config_return={"tavily": _engine_config()},
            class_return=EngCls,
        ):
            with pytest.raises(ValueError, match="search.time_period"):
                create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs is None

    @pytest.mark.parametrize("engine", ["tavily", "serpapi", "brave"])
    def test_wrapped_null_global_falls_back_to_all(self, engine):
        """A stored global row with no value (a metadata-wrapped null, the
        shape of a database-backed snapshot) means "not set": it falls back to
        the documented "all" default, as it did before inherit semantics,
        rather than failing every search."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {
            "search.time_period": {"value": None, "ui_element": "select"}
        }

        with _patches(
            config_return={engine: _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine(engine, settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "all"

    def test_bare_none_global_falls_back_to_all(self):
        """A stored null reaches the resolver as a bare None once
        SettingsManager.get_settings_snapshot() unwraps the entry (REST
        /api/v1, scheduled news runs). It means "not set" there exactly as the
        wrapped {"value": None} does, so it falls back to "all"."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")

        with _patches(
            config_return={"tavily": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine(
                "tavily", settings_snapshot={"search.time_period": None}
            )

        assert EngCls._call_kwargs["time_period"] == "all"

    def test_brave_absent_global_falls_back_to_all_not_class_default(self):
        """Finding A: with the global setting ABSENT from the snapshot (a
        partial/programmatic snapshot), Brave — whose class default is a
        restrictive "y" (Past Year) — must fall back to "all" (no filter), not
        silently re-pin to a one-year window."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        with _patches(
            config_return={"brave": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("brave", settings_snapshot={"dummy": "v"})

        assert EngCls._call_kwargs["time_period"] == "all"

    def test_serpapi_invalid_global_raises_before_construction(self):
        """An invalid operator global value fails instead of changing the filter."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"search.time_period": "not-a-valid-period"}
        with _patches(
            config_return={"serpapi": _engine_config()},
            class_return=EngCls,
        ):
            with pytest.raises(ValueError, match="search.time_period"):
                create_search_engine("serpapi", settings_snapshot=snapshot)

        assert EngCls._call_kwargs is None

    def test_null_default_params_does_not_crash(self):
        """A config row whose default_params is JSON null (None) must not crash
        the merge (``{**None}`` -> TypeError); None is treated as empty and the
        engine is still created with the resolved global time_period."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        with _patches(
            config_return={"tavily": _engine_config(default_params=None)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "tavily",
                settings_snapshot={"search.time_period": {"value": "w"}},
            )

        assert result is not None
        assert EngCls._call_kwargs["time_period"] == "w"


class TestApplyTimePeriodOverride:
    """apply_time_period_override(): the per-request override helper used by
    run_research_process — must beat per-engine choices for that run."""

    def test_sets_global_and_resets_present_per_engine_keys(self):
        from local_deep_research.web_search_engines.search_engine_factory import (
            apply_time_period_override,
        )

        snapshot = {
            "search.time_period": {"value": "all"},
            "search.engine.web.serpapi.default_params.time_period": {
                "value": "y"
            },
            "search.engine.web.tavily.default_params.time_period": "all",
            "unrelated.key": {"value": "x"},
        }

        apply_time_period_override(snapshot, "d")

        assert snapshot["search.time_period"] == {"value": "d"}
        assert snapshot[
            "search.engine.web.serpapi.default_params.time_period"
        ] == {"value": "inherit"}
        assert (
            snapshot["search.engine.web.tavily.default_params.time_period"]
            == "inherit"
        )
        assert snapshot["unrelated.key"] == {"value": "x"}

    @pytest.mark.parametrize("wrapped", [True, False])
    def test_env_pinned_per_engine_key_is_not_reset_to_inherit(
        self, monkeypatch, wrapped
    ):
        """An LDR_*_DEFAULT_PARAMS_TIME_PERIOD env var is operator policy and
        must survive a per-request override. The pin is recognized from the
        environment itself, so raw-valued and metadata-wrapped entries are
        protected alike; a neighboring key without a pin is still reset."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            apply_time_period_override,
        )

        monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
        monkeypatch.delenv(
            "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
            raising=False,
        )
        monkeypatch.setenv(
            "LDR_SEARCH_ENGINE_WEB_BRAVE_DEFAULT_PARAMS_TIME_PERIOD", "w"
        )
        pinned = {"value": "w", "editable": False} if wrapped else "w"
        saved = {"value": "y", "editable": True} if wrapped else "y"
        snapshot = {
            "search.time_period": {"value": "all"},
            "search.engine.web.brave.default_params.time_period": pinned,
            "search.engine.web.serpapi.default_params.time_period": saved,
        }

        apply_time_period_override(snapshot, "d")

        assert snapshot["search.time_period"] == {"value": "d"}
        # The pinned value survives unchanged, metadata and all.
        assert (
            snapshot["search.engine.web.brave.default_params.time_period"]
            == pinned
        )
        # The unpinned per-engine value is reset so the request wins.
        assert snapshot[
            "search.engine.web.serpapi.default_params.time_period"
        ] == ({"value": "inherit", "editable": True} if wrapped else "inherit")

    def test_non_editable_entries_without_an_env_pin_are_still_reset(
        self, monkeypatch
    ):
        """SettingsManager.get_all_settings sets editable=False on every
        database-backed entry while app.lock_settings is on, and the research
        start snapshot is taken that way. That marker alone is not an env
        pin: a per-request period must still replace explicit per-engine
        choices on a locked installation."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            apply_time_period_override,
        )

        monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
        for engine in ("BRAVE", "SERPAPI", "TAVILY"):
            monkeypatch.delenv(
                f"LDR_SEARCH_ENGINE_WEB_{engine}_DEFAULT_PARAMS_TIME_PERIOD",
                raising=False,
            )
        snapshot = {
            "search.time_period": {"value": "all", "editable": False},
            "search.engine.web.brave.default_params.time_period": {
                "value": "w",
                "editable": False,
            },
            "search.engine.web.serpapi.default_params.time_period": {
                "value": "y",
                "editable": False,
            },
            "search.engine.web.tavily.default_params.time_period": {
                "value": "m",
                "editable": False,
            },
        }

        apply_time_period_override(snapshot, "d")

        assert snapshot["search.time_period"] == {
            "value": "d",
            "editable": False,
        }
        for engine in ("brave", "serpapi", "tavily"):
            assert snapshot[
                f"search.engine.web.{engine}.default_params.time_period"
            ] == {"value": "inherit", "editable": False}

    def test_absent_per_engine_keys_stay_absent(self):
        from local_deep_research.web_search_engines.search_engine_factory import (
            apply_time_period_override,
        )

        snapshot = {"search.time_period": "all"}

        apply_time_period_override(snapshot, "w")

        assert snapshot["search.time_period"] == "w"
        assert (
            "search.engine.web.brave.default_params.time_period" not in snapshot
        )

    def test_override_wins_end_to_end_against_per_engine_choice(self):
        """Full chain: override applied to a snapshot whose per-engine value
        would otherwise win — the engine must receive the override. The
        mocked search_config derives default_params from the LIVE snapshot
        (like the real extraction does), so this genuinely exercises the
        helper's per-engine reset: without it, the per-engine "y" would win
        and the assertion below would fail."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            apply_time_period_override,
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {
            "search.time_period": {"value": "all"},
            "search.engine.web.tavily.default_params.time_period": {
                "value": "y"
            },
        }
        apply_time_period_override(snapshot, "d")

        def _config_from_snapshot(*args, **kw):
            snap = kw.get("settings_snapshot") or {}
            per = snap.get(
                "search.engine.web.tavily.default_params.time_period"
            )
            if isinstance(per, dict):
                per = per.get("value")
            default_params = {} if per is None else {"time_period": per}
            return {"tavily": _engine_config(default_params=default_params)}

        with _patches(class_return=EngCls) as (_mock_reg, mock_sc, _mock_gsmc):
            mock_sc.side_effect = _config_from_snapshot
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "d"

    def test_per_engine_choice_wins_without_override_helper(self):
        """Control for the end-to-end test above: the same snapshot WITHOUT
        apply_time_period_override applied — the per-engine "y" must win,
        proving the snapshot-derived config path is live in both tests."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {
            "search.time_period": {"value": "d"},
            "search.engine.web.tavily.default_params.time_period": {
                "value": "y"
            },
        }

        def _config_from_snapshot(*args, **kw):
            snap = kw.get("settings_snapshot") or {}
            per = snap.get(
                "search.engine.web.tavily.default_params.time_period"
            )
            if isinstance(per, dict):
                per = per.get("value")
            default_params = {} if per is None else {"time_period": per}
            return {"tavily": _engine_config(default_params=default_params)}

        with _patches(class_return=EngCls) as (_mock_reg, mock_sc, _mock_gsmc):
            mock_sc.side_effect = _config_from_snapshot
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "y"

    def test_no_snapshot_key_falls_back_to_all(self):
        """Without search.time_period in the snapshot, the factory injects the
        neutral "all" (no filter) fallback rather than leaving the engine's own
        constructor default to apply (restrictive "y" for brave/serpapi)."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "time_period")
        snapshot = {"dummy": "value"}

        with _patches(
            config_return={"tavily": _engine_config()},
            class_return=EngCls,
        ):
            create_search_engine("tavily", settings_snapshot=snapshot)

        assert EngCls._call_kwargs["time_period"] == "all"


# ---------------------------------------------------------------------------
# Tests: Display label fallback (end-to-end)
# ---------------------------------------------------------------------------


class TestDisplayLabelFallback:
    """Test display label -> config key resolution through the actual function."""

    def test_display_label_resolved_to_config_key(self):
        """A display label like '🔬 OpenAlex (Scientific)' should resolve
        to the config key whose display_name matches."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")

        with _patches(
            config_return={
                "openalex": _engine_config(display_name="OpenAlex"),
            },
            class_return=EngCls,
        ):
            result = create_search_engine(
                "\U0001f52c OpenAlex (Scientific)",
                settings_snapshot={"x": 1},
            )

        assert result is not None

    def test_unresolvable_label_raises_value_error(self):
        """Display label that doesn't match any config FAILS CLOSED.
        Previously fell back to 'auto' silently — see plan C2."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")

        with _patches(
            config_return={
                "auto": _engine_config(),
                "other": _engine_config(display_name="Other"),
            },
            class_return=EngCls,
        ):
            with pytest.raises(ValueError, match="Unknown search engine"):
                create_search_engine(
                    "\U0001f50d Unknown (Category)",
                    settings_snapshot={"x": 1},
                )

    def test_plain_unknown_engine_raises_value_error(self):
        """Plain unknown engine name FAILS CLOSED (no more silent rewrite to 'auto').
        See plan C2."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")

        with _patches(
            config_return={"auto": _engine_config()},
            class_return=EngCls,
        ):
            with pytest.raises(ValueError, match="Unknown search engine"):
                create_search_engine(
                    "nonexistent_plain_name",
                    settings_snapshot={"x": 1},
                )

    def test_collection_key_skips_display_label_fallback_warning(self):
        """collection_<uuid> is a config key, not a UI label — do not emit the
        deprecated display-label fallback warning before fail-closed."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")
        collection_key = "collection_e8d53954-1111-2222-3333-444444444444"

        with _patches(
            config_return={"wikipedia": _engine_config()},
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory.logger"
            ) as mock_logger:
                mock_logger.bind.return_value = mock_logger
                with pytest.raises(ValueError, match="Unknown search engine"):
                    create_search_engine(
                        collection_key,
                        settings_snapshot={"x": 1},
                    )

                warning_msgs = [
                    str(c.args[0]) if c.args else ""
                    for c in mock_logger.warning.call_args_list
                ]
                assert not any(
                    "display label fallback" in m for m in warning_msgs
                ), warning_msgs
                # Still audit-log the rejection itself.
                mock_logger.bind.assert_called()


# ---------------------------------------------------------------------------
# Tests: use_full_search wrapper trigger
# ---------------------------------------------------------------------------


class TestUseFullSearchWrapper:
    """Test that use_full_search=True triggers _create_full_search_wrapper."""

    def test_use_full_search_calls_wrapper(self):
        """When use_full_search=True and engine supports it, wrapper is called."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")
        mock_wrapper_result = Mock()

        with _patches(
            config_return={"eng": _engine_config(supports_full_search=True)},
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory._create_full_search_wrapper"
            ) as mock_wrapper:
                mock_wrapper.return_value = mock_wrapper_result

                result = create_search_engine(
                    "eng",
                    settings_snapshot={"x": 1},
                    use_full_search=True,
                )

        assert mock_wrapper.called
        assert result is mock_wrapper_result

    def test_use_full_search_false_does_not_call_wrapper(self):
        """When use_full_search=False, wrapper is not called."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")

        with _patches(
            config_return={"eng": _engine_config(supports_full_search=True)},
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory._create_full_search_wrapper"
            ) as mock_wrapper:
                create_search_engine(
                    "eng",
                    settings_snapshot={"x": 1},
                    use_full_search=False,
                )

        assert not mock_wrapper.called

    def test_supports_full_search_false_does_not_call_wrapper(self):
        """Engine without supports_full_search does not trigger wrapper."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results")

        with _patches(
            config_return={"eng": _engine_config(supports_full_search=False)},
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory._create_full_search_wrapper"
            ) as mock_wrapper:
                create_search_engine(
                    "eng",
                    settings_snapshot={"x": 1},
                    use_full_search=True,
                )

        assert not mock_wrapper.called

    def test_snippets_only_false_from_snapshot_derives_use_full_search(self):
        """settings_snapshot search.snippets_only=False wraps like get_search."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "search_snippets_only")
        mock_wrapper_result = Mock()

        with _patches(
            config_return={"eng": _engine_config(supports_full_search=True)},
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory._create_full_search_wrapper"
            ) as mock_wrapper:
                mock_wrapper.return_value = mock_wrapper_result
                result = create_search_engine(
                    "eng",
                    settings_snapshot={
                        "search.snippets_only": {"value": False}
                    },
                )

        assert mock_wrapper.called
        assert result is mock_wrapper_result

    def test_snippets_only_true_from_snapshot_does_not_wrap(self):
        """settings_snapshot search.snippets_only=True does not wrap."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "search_snippets_only")

        with _patches(
            config_return={"eng": _engine_config(supports_full_search=True)},
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory._create_full_search_wrapper"
            ) as mock_wrapper:
                create_search_engine(
                    "eng",
                    settings_snapshot={"search.snippets_only": True},
                )

        assert not mock_wrapper.called

    def test_explicit_use_full_search_false_overrides_snippets_only_false(self):
        """Explicit use_full_search=False wins over derived wrap."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "search_snippets_only")

        with _patches(
            config_return={"eng": _engine_config(supports_full_search=True)},
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory._create_full_search_wrapper"
            ) as mock_wrapper:
                create_search_engine(
                    "eng",
                    settings_snapshot={"search.snippets_only": False},
                    use_full_search=False,
                )

        assert not mock_wrapper.called

    def test_full_search_module_triggers_wrap(self):
        """Engines declaring ``full_search_module`` (registry engines) get
        wrapped the same way as engines declaring ``supports_full_search``
        (runtime engines) — the factory wrapper is the canonical fetcher.
        """
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("max_results", "search_snippets_only")
        mock_wrapper_result = Mock()

        with _patches(
            config_return={
                "eng": _engine_config(
                    full_search_module=".engines.full_search"
                ),
            },
            class_return=EngCls,
        ):
            with patch(
                "local_deep_research.web_search_engines.search_engine_factory._create_full_search_wrapper"
            ) as mock_wrapper:
                mock_wrapper.return_value = mock_wrapper_result
                result = create_search_engine(
                    "eng",
                    settings_snapshot={"search.snippets_only": False},
                )

        assert mock_wrapper.called
        assert result is mock_wrapper_result

    def test_posts_search_snippets_only_when_engine_swallows_kwargs(self):
        """Engines that accept **kwargs but do not forward search_snippets_only
        still get the flag stamped on the BaseSearchEngine instance."""
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        class SwallowEngine(BaseSearchEngine):
            def __init__(self, llm=None, settings_snapshot=None, **kwargs):
                super().__init__(llm=llm, settings_snapshot=settings_snapshot)

            def _get_previews(self, query):
                return []

            def _get_full_content(self, items):
                return items

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=SwallowEngine,
        ):
            engine = create_search_engine(
                "eng",
                settings_snapshot={"search.snippets_only": False},
                programmatic_mode=True,
            )

        assert engine.search_snippets_only is False

    def test_no_wrap_keeps_inner_snippets_only_false(self):
        """Without wrap, engines that asked for full content keep it."""
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        class SwallowEngine(BaseSearchEngine):
            def __init__(self, llm=None, settings_snapshot=None, **kwargs):
                super().__init__(llm=llm, settings_snapshot=settings_snapshot)

            def _get_previews(self, query):
                return []

            def _get_full_content(self, items):
                return items

        with _patches(
            config_return={"eng": _engine_config(supports_full_search=False)},
            class_return=SwallowEngine,
        ):
            engine = create_search_engine(
                "eng",
                settings_snapshot={"search.snippets_only": False},
                programmatic_mode=True,
            )

        assert engine.search_snippets_only is False


# ---------------------------------------------------------------------------
# Tests: LLM relevance filter
# ---------------------------------------------------------------------------


class TestLlmRelevanceFilter:
    """Test LLM relevance filter application on created engine instances."""

    def test_per_engine_filter_enabled(self):
        """Per-engine setting enables filter when engine has LLM."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("llm", "max_results")
        mock_llm = Mock()
        snapshot = {
            "search.engine.web.eng.default_params.enable_llm_relevance_filter": {
                "value": True
            },
        }

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot=snapshot
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is True

    def test_per_engine_filter_disabled(self):
        """Per-engine setting can explicitly disable filter."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "llm",
            "max_results",
            class_attrs={"needs_llm_relevance_filter": True},
        )
        mock_llm = Mock()
        snapshot = {
            "search.engine.web.eng.default_params.enable_llm_relevance_filter": {
                "value": False
            },
        }

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot=snapshot
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is False

    def test_auto_detection_needs_llm_relevance_filter_engine(self):
        """Engines with needs_llm_relevance_filter=True get filter auto-enabled."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "llm",
            "max_results",
            class_attrs={"needs_llm_relevance_filter": True},
        )
        mock_llm = Mock()

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot={"x": 1}
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is True

    def test_auto_detection_engine_without_needs_llm_relevance_filter(self):
        """Engines without needs_llm_relevance_filter do not get filter auto-enabled."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "llm",
            "max_results",
            class_attrs={"is_generic": True},
        )
        mock_llm = Mock()

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot={"x": 1}
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is False

    def test_global_skip_does_not_override_needs_llm_relevance_filter(self):
        """Global skip_relevance_filter=True does NOT disable for needs_llm_relevance_filter engines."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "llm",
            "max_results",
            class_attrs={"needs_llm_relevance_filter": True},
        )
        mock_llm = Mock()
        snapshot = {
            "search.skip_relevance_filter": {"value": True},
        }

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot=snapshot
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is True

    def test_global_skip_applies_to_unclassified_engine(self):
        """Global skip_relevance_filter=True disables for engines without needs_llm_relevance_filter."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "llm",
            "max_results",
        )
        mock_llm = Mock()
        snapshot = {
            "search.skip_relevance_filter": {"value": True},
        }

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot=snapshot
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is False

    def test_per_engine_overrides_needs_llm_relevance_filter(self):
        """Per-engine setting False disables filter even for needs_llm_relevance_filter engines."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "llm",
            "max_results",
            class_attrs={"needs_llm_relevance_filter": True},
        )
        mock_llm = Mock()
        snapshot = {
            "search.engine.web.eng.default_params.enable_llm_relevance_filter": {
                "value": False
            },
        }

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot=snapshot
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is False

    def test_no_llm_means_no_filter(self):
        """Filter is not applied when engine has no LLM."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "max_results",
            class_attrs={"needs_llm_relevance_filter": True},
        )

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            result = create_search_engine("eng", settings_snapshot={"x": 1})

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is False


# ---------------------------------------------------------------------------
# Tests: removed meta engines ('auto', 'meta', 'parallel', ...) are rejected
# ---------------------------------------------------------------------------


class TestRemovedMetaEnginesRejected:
    """The parallel/auto meta engines were removed — no special-case
    short-circuit remains, so they fail like any unknown engine."""

    @pytest.mark.parametrize(
        "engine_name", ["auto", "meta", "parallel", "parallel_scientific"]
    )
    def test_removed_engine_raises_value_error(self, engine_name):
        """Removed meta engine names raise ValueError mentioning the removal."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry"
        ) as mock_reg:
            mock_reg.get.return_value = None
            with pytest.raises(ValueError, match="meta engines were removed"):
                create_search_engine(
                    engine_name,
                    llm=Mock(),
                    settings_snapshot={"s": 1},
                )


# ---------------------------------------------------------------------------
# Tests: Retriever path
# ---------------------------------------------------------------------------


class TestRetrieverPath:
    """Test retriever registry path."""

    def test_retriever_max_results_from_kwargs(self):
        """max_results kwarg is forwarded to RetrieverSearchEngine."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        mock_retriever = Mock()
        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry"
        ) as mock_reg:
            mock_reg.get.return_value = mock_retriever
            with patch(
                "local_deep_research.web_search_engines.engines.search_engine_retriever.RetrieverSearchEngine"
            ) as mock_cls:
                mock_cls.return_value = Mock()
                create_search_engine(
                    "my_retriever",
                    settings_snapshot={"x": 1},
                    max_results=25,
                )
                call_kwargs = mock_cls.call_args[1]
                assert call_kwargs["max_results"] == 25

    def test_retriever_default_max_results(self):
        """Default max_results=10 when not specified."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        mock_retriever = Mock()
        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry"
        ) as mock_reg:
            mock_reg.get.return_value = mock_retriever
            with patch(
                "local_deep_research.web_search_engines.engines.search_engine_retriever.RetrieverSearchEngine"
            ) as mock_cls:
                mock_cls.return_value = Mock()
                create_search_engine(
                    "my_retriever",
                    settings_snapshot={"x": 1},
                )
                call_kwargs = mock_cls.call_args[1]
                assert call_kwargs["max_results"] == 10


# ---------------------------------------------------------------------------
# Tests: get_search parameter routing
# ---------------------------------------------------------------------------


class TestGetSearchParameterRouting:
    """Test get_search routes parameters correctly for various engines."""

    def test_get_search_wikinews_params(self):
        """Wikinews gets search_snippets_only, adaptive_search, time_period."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        snapshot = {
            "search.engine.web.wikinews.adaptive_search": {"value": False},
        }

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="wikinews",
                llm_instance=Mock(),
                time_period="m",
                search_snippets_only=True,
                search_language="French",
                settings_snapshot=snapshot,
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["time_period"] == "m"
            assert call_kwargs["search_snippets_only"] is True
            assert call_kwargs["search_language"] == "French"
            assert call_kwargs["adaptive_search"] is False

    def test_get_search_tavily_forwards_time_period(self):
        """Tavily gets time_period forwarded (regression guard for #4936)."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="tavily",
                llm_instance=Mock(),
                time_period="w",
                settings_snapshot={},
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["time_period"] == "w"

    def test_get_search_default_defers_time_period_resolution(self):
        """get_search() with no explicit time_period must NOT forward one:
        forwarding a default here would override per-engine
        default_params.time_period in create_search_engine()'s resolution
        (#5587). Pinning this so a future revert to an "all"/"y" signature
        default fails loudly.
        """
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="tavily",
                llm_instance=Mock(),
                settings_snapshot={},
            )

            call_kwargs = mock_create.call_args[1]
            assert "time_period" not in call_kwargs

    def test_get_search_sofya_params(self):
        """Sofya gets search_snippets_only forwarded: it derives its request
        depth from it (paid 'basic' depth only when content is consumed)."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="sofya",
                llm_instance=Mock(),
                search_snippets_only=True,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["search_snippets_only"] is True

    def test_get_search_max_filtered_results(self):
        """max_filtered_results is passed when provided."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="some_engine",
                llm_instance=Mock(),
                max_filtered_results=5,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["max_filtered_results"] == 5

    def test_get_search_max_filtered_results_none(self):
        """max_filtered_results=None is not passed as a parameter."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="some_engine",
                llm_instance=Mock(),
                max_filtered_results=None,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert "max_filtered_results" not in call_kwargs

    def test_get_search_google_pse_params(self):
        """google_pse gets region, safe_search, use_full_search, and search_language."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="google_pse",
                llm_instance=Mock(),
                region="de",
                safe_search=False,
                search_snippets_only=False,
                search_language="German",
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["region"] == "de"
            assert call_kwargs["safe_search"] is False
            assert call_kwargs["use_full_search"] is True
            assert call_kwargs["search_language"] == "German"

    def test_get_search_mojeek_params(self):
        """mojeek gets region, safe_search, use_full_search but not search_language."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="mojeek",
                llm_instance=Mock(),
                region="uk",
                safe_search=True,
                search_snippets_only=True,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["region"] == "uk"
            assert call_kwargs["safe_search"] is True
            assert call_kwargs["use_full_search"] is False
            assert "search_language" not in call_kwargs

    def test_get_search_searxng_params(self):
        """searxng gets use_full_search based on search_snippets_only, without region/safe_search."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="searxng",
                llm_instance=Mock(),
                region="us",
                safe_search=False,
                search_snippets_only=False,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert "region" not in call_kwargs
            assert "safe_search" not in call_kwargs
            assert call_kwargs["use_full_search"] is True
            assert "search_language" not in call_kwargs

    def test_get_search_searxng_snippets_only_true(self):
        """searxng with search_snippets_only=True sets use_full_search=False."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="searxng",
                llm_instance=Mock(),
                search_snippets_only=True,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["use_full_search"] is False

    def test_get_search_programmatic_mode_forwarded(self):
        """programmatic_mode is forwarded to create_search_engine."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="some_engine",
                llm_instance=Mock(),
                programmatic_mode=True,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert call_kwargs["programmatic_mode"] is True

    def test_get_search_engine_without_region_params(self):
        """Engines not in the region list don't get region params."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
        ) as mock_create:
            mock_create.return_value = Mock(run=Mock())

            get_search(
                search_tool="arxiv",
                llm_instance=Mock(),
                region="us",
                safe_search=True,
                settings_snapshot={"x": 1},
            )

            call_kwargs = mock_create.call_args[1]
            assert "region" not in call_kwargs
            assert "safe_search" not in call_kwargs
            assert "use_full_search" not in call_kwargs


# ---------------------------------------------------------------------------
# Tests: Exception handling
# ---------------------------------------------------------------------------


class TestExceptionHandling:
    """Test exception handling in create_search_engine."""

    def test_engine_init_raises_returns_none(self):
        """Exception during __init__ of engine class returns None."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        class BrokenEngine:
            def __init__(self):
                raise RuntimeError("init failed")

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=BrokenEngine,
        ):
            result = create_search_engine("eng", settings_snapshot={"x": 1})

        assert result is None

    def test_missing_module_path_returns_none(self):
        """Config missing module_path should cause exception -> None."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        bad_config = {"class_name": "Foo", "default_params": {}}

        with _patches(
            config_return={"eng": bad_config},
        ):
            result = create_search_engine("eng", settings_snapshot={"x": 1})

        assert result is None


# ---------------------------------------------------------------------------
# Tests: settings_snapshot requirement
# ---------------------------------------------------------------------------


class TestSettingsSnapshotRequired:
    """Test that settings_snapshot=None raises RuntimeError for
    non-retriever engines."""

    def test_parallel_also_needs_settings_snapshot(self):
        """'parallel' no longer short-circuits — like every other engine name
        it requires a settings_snapshot."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry"
        ) as mock_reg:
            mock_reg.get.return_value = None
            with pytest.raises(RuntimeError, match="settings_snapshot"):
                create_search_engine(
                    "parallel", llm=Mock(), settings_snapshot=None
                )


# ---------------------------------------------------------------------------
# Tests: per-engine filter with plain bool (not dict)
# ---------------------------------------------------------------------------


class TestPerEngineFilterPlainBool:
    """Test per-engine LLM relevance filter setting as plain bool."""

    def test_per_engine_filter_plain_true(self):
        """Per-engine setting as plain True enables filter."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class("llm", "max_results")
        mock_llm = Mock()
        snapshot = {
            "search.engine.web.eng.default_params.enable_llm_relevance_filter": True,
        }

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot=snapshot
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is True

    def test_global_skip_filter_plain_bool(self):
        """Global skip_relevance_filter as plain True works."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = _make_engine_class(
            "llm",
            "max_results",
            class_attrs={"is_scientific": True},
        )
        mock_llm = Mock()
        snapshot = {
            "search.skip_relevance_filter": True,
        }

        with _patches(
            config_return={"eng": _engine_config(requires_llm=True)},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng", llm=mock_llm, settings_snapshot=snapshot
            )

        assert result is not None
        assert getattr(result, "enable_llm_relevance_filter", False) is False


# ---------------------------------------------------------------------------
# Tests: programmatic_mode propagation to engines that swallow **kwargs
# ---------------------------------------------------------------------------


class TestProgrammaticModePostConstructionPatch:
    """Most concrete engines (Serper, Tavily, Wikipedia, etc.) accept
    ``**kwargs`` in their constructor but do not forward them to
    ``BaseSearchEngine.__init__``. As a result, ``programmatic_mode``
    passed by the factory was silently dropped and the engine ended up
    with the BaseSearchEngine default (False), mismatching what the API
    caller asked for. The factory now applies the requested mode
    post-construction via ``_configure_programmatic_mode``."""

    def _make_kwargs_swallowing_engine(self):
        """Build a real BaseSearchEngine subclass that mirrors the
        Serper/Tavily/Wikipedia pattern: accepts ``**kwargs`` without
        forwarding to ``super().__init__``."""
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        class _SwallowingEngine(BaseSearchEngine):
            def __init__(self, max_results: int = 10, **kwargs):
                super().__init__(max_results=max_results)

            def _get_previews(self, query):
                return []

            def _get_full_content(self, items):
                return items

        return _SwallowingEngine

    def test_engine_swallowing_kwargs_still_gets_programmatic_mode(self):
        """Engine doesn't forward kwargs -> factory patches it post-construction."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = self._make_kwargs_swallowing_engine()

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            result = create_search_engine(
                "eng",
                settings_snapshot={"x": 1},
                programmatic_mode=True,
            )

        assert result is not None
        # Without the post-construction patch this would be False
        # because _SwallowingEngine.__init__ swallowed programmatic_mode.
        assert result.programmatic_mode is True
        # And the rate_tracker should be the per-instance programmatic one,
        # not the global shared tracker returned by get_tracker().
        from local_deep_research.web_search_engines.rate_limiting.tracker import (
            AdaptiveRateLimitTracker,
        )

        assert isinstance(result.rate_tracker, AdaptiveRateLimitTracker)
        assert result.rate_tracker.programmatic_mode is True

    def test_engine_swallowing_kwargs_default_mode_unchanged(self):
        """When programmatic_mode is False (default), no patch is needed
        and the engine ends up in shared-tracker mode."""
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        EngCls = self._make_kwargs_swallowing_engine()

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=EngCls,
        ):
            result = create_search_engine("eng", settings_snapshot={"x": 1})

        assert result is not None
        assert result.programmatic_mode is False

    def test_retriever_path_propagates_programmatic_mode(self):
        """The retriever early-return path returns before the post-construction
        patch — verify programmatic_mode is passed at construction so the
        engine still ends up in the requested mode.
        """
        from langchain_core.retrievers import BaseRetriever
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        class _DummyRetriever(BaseRetriever):
            def _get_relevant_documents(self, query, *, run_manager):
                return []

        retriever = _DummyRetriever()

        # Retriever PEP now requires a snapshot (plan H3). Pass a
        # minimal snapshot + mock evaluate_retriever to allow the
        # registered retriever through so we can test programmatic_mode.
        from local_deep_research.security.egress.policy import Decision

        with (
            patch(
                "local_deep_research.web_search_engines.search_engine_factory.retriever_registry"
            ) as mock_reg,
            patch(
                "local_deep_research.security.egress.policy.evaluate_retriever",
                return_value=Decision(True, "test_bypass"),
            ),
        ):
            mock_reg.get.return_value = retriever
            mock_reg.get_metadata.return_value = {"is_local": True}
            result = create_search_engine(
                "my_rag",
                programmatic_mode=True,
                settings_snapshot={"policy.egress_scope": "both"},
            )

        assert result is not None
        assert result.programmatic_mode is True


# ---------------------------------------------------------------------------
# Tests: factory enforces programmatic_mode regardless of subclass __init__
# pattern. Three patterns exist in the wild — see the Subclass contract
# section in BaseSearchEngine for the recommended approach.
# ---------------------------------------------------------------------------


class TestProgrammaticModeContract:
    """Factory must produce the requested ``programmatic_mode`` regardless
    of how an engine subclass handles its kwargs:

    1. ``swallow_no_forward``: Serper/Tavily/Wikipedia pattern. Subclass
       accepts ``**kwargs`` but doesn't forward them to ``super().__init__``.
       The factory's post-construction patch is the safety net here.
    2. ``forward_kwargs``: Subclass accepts ``**kwargs`` and forwards them.
       The base ``__init__`` sets ``programmatic_mode`` directly.
    3. ``named_param``: Subclass names ``programmatic_mode`` explicitly
       in its signature and forwards it. Base handles it during init."""

    @pytest.mark.parametrize(
        "swallow_pattern",
        ["swallow_no_forward", "forward_kwargs", "named_param"],
    )
    def test_factory_enforces_programmatic_mode_across_swallow_patterns(
        self, swallow_pattern
    ):
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )
        from local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        if swallow_pattern == "swallow_no_forward":

            class _Eng(BaseSearchEngine):
                def __init__(self, max_results: int = 10, **kwargs):
                    super().__init__(max_results=max_results)

                def _get_previews(self, query):
                    return []

                def _get_full_content(self, items):
                    return items

        elif swallow_pattern == "forward_kwargs":

            class _Eng(BaseSearchEngine):  # type: ignore[no-redef]
                def __init__(self, max_results: int = 10, **kwargs):
                    super().__init__(max_results=max_results, **kwargs)

                def _get_previews(self, query):
                    return []

                def _get_full_content(self, items):
                    return items

        else:  # named_param

            class _Eng(BaseSearchEngine):  # type: ignore[no-redef]
                def __init__(
                    self,
                    max_results: int = 10,
                    programmatic_mode: bool = False,
                    **kwargs,
                ):
                    super().__init__(
                        max_results=max_results,
                        programmatic_mode=programmatic_mode,
                    )

                def _get_previews(self, query):
                    return []

                def _get_full_content(self, items):
                    return items

        with _patches(
            config_return={"eng": _engine_config()},
            class_return=_Eng,
        ):
            result = create_search_engine(
                "eng",
                settings_snapshot={"x": 1},
                programmatic_mode=True,
            )

        from local_deep_research.web_search_engines.rate_limiting.tracker import (
            AdaptiveRateLimitTracker,
        )

        assert result is not None
        assert result.programmatic_mode is True, (
            f"Pattern {swallow_pattern!r}: factory did not enforce "
            f"programmatic_mode=True"
        )
        assert isinstance(result.rate_tracker, AdaptiveRateLimitTracker), (
            f"Pattern {swallow_pattern!r}: rate_tracker should be the "
            f"per-instance AdaptiveRateLimitTracker, got "
            f"{type(result.rate_tracker).__name__}"
        )


def test_factory_logging_checks_instance_is_available_attribute():
    """Factory logging reads instance _is_available attribute if present."""
    from local_deep_research.web_search_engines.search_engine_base import (
        BaseSearchEngine,
    )
    from local_deep_research.web_search_engines.search_engine_factory import (
        get_search,
    )

    class _MockEngine(BaseSearchEngine):
        def __init__(self, **kwargs):
            super().__init__()
            self._is_available = False

        def _get_previews(self, query):
            return []

        def _get_full_content(self, items):
            return items

    with _patches(
        config_return={"mock_eng": _engine_config()},
        class_return=_MockEngine,
    ):
        with patch(
            "local_deep_research.web_search_engines.search_engine_factory.logger"
        ) as mock_logger:
            engine = get_search(
                "mock_eng", Mock(), settings_snapshot={"a": "b"}
            )
            assert engine is not None
            assert engine._is_available is False
            # Verify logger logged false for availability flag
            logged_avail = any(
                "Engine availability flag: False" in str(call)
                for call in mock_logger.info.call_args_list
            )
            assert logged_avail


def test_factory_logging_passes_settings_snapshot_to_is_available():
    """Factory logging passes settings_snapshot when calling engine.is_available."""
    from local_deep_research.web_search_engines.search_engine_base import (
        BaseSearchEngine,
    )
    from local_deep_research.web_search_engines.search_engine_factory import (
        get_search,
    )

    received_snapshots = []

    class _MockEngine(BaseSearchEngine):
        @classmethod
        def is_available(cls, settings_snapshot=None):
            received_snapshots.append(settings_snapshot)
            return True

        def _get_previews(self, query):
            return []

        def _get_full_content(self, items):
            return items

    snapshot = {"my_key": "my_val"}
    with _patches(
        config_return={"mock_eng": _engine_config()},
        class_return=_MockEngine,
    ):
        get_search("mock_eng", Mock(), settings_snapshot=snapshot)

    assert len(received_snapshots) > 0
    assert received_snapshots[-1] == snapshot


def test_factory_logging_swallows_type_error_from_legacy_fallback():
    """Availability diagnostics cannot break a successfully created engine."""
    from local_deep_research.web_search_engines.search_engine_base import (
        BaseSearchEngine,
    )
    from local_deep_research.web_search_engines.search_engine_factory import (
        get_search,
    )

    class _MockEngine(BaseSearchEngine):
        def is_available(self):
            raise TypeError("internal availability failure")

        def _get_previews(self, query):
            return []

        def _get_full_content(self, items):
            return items

    with (
        _patches(
            config_return={"mock_eng": _engine_config()},
            class_return=_MockEngine,
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.logger"
        ) as mock_logger,
    ):
        engine = get_search("mock_eng", Mock(), settings_snapshot={"a": "b"})

    assert isinstance(engine, _MockEngine)
    assert any(
        "Engine availability fallback failed" in str(call)
        for call in mock_logger.debug.call_args_list
    )