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.

54.0 KB
Raw Blame
"""
Tests for search_engines_config module.

Tests the configuration loading and processing for search engines:
- _get_setting() - settings retrieval with fallbacks
- _extract_per_engine_config() - nested config extraction
- search_config() - full search engine configuration
- default_search_engine() - default engine retrieval
- local_search_engines() - local engine listing
"""

import pytest
from unittest.mock import MagicMock, patch

from local_deep_research.web_search_engines.search_engines_config import (
    _extract_per_engine_config,
    _get_setting,
    _resolve_api_key,
    default_search_engine,
    get_available_engines,
    search_config,
)


class TestGetSetting:
    """Tests for _get_setting function."""

    def test_returns_value_from_snapshot(self):
        """Should return value from settings snapshot when available."""
        with patch(
            "local_deep_research.web_search_engines.search_engines_config.get_setting_from_snapshot",
            return_value="snapshot_value",
        ):
            result = _get_setting(
                "test.key",
                "default_value",
                settings_snapshot={"test.key": "snapshot_value"},
            )
            assert result == "snapshot_value"

    def test_returns_value_from_db_session(self):
        """Should return value from db session when snapshot not available."""
        mock_session = MagicMock()
        mock_settings_manager = MagicMock()
        mock_settings_manager.get_setting.return_value = "db_value"

        with patch(
            "local_deep_research.web_search_engines.search_engines_config.get_settings_manager",
            return_value=mock_settings_manager,
        ):
            result = _get_setting(
                "test.key",
                "default_value",
                db_session=mock_session,
            )
            assert result == "db_value"

    def test_returns_default_when_no_source_available(self):
        """Should return default value when no settings source available."""
        result = _get_setting("test.key", "default_value")
        assert result == "default_value"

    def test_prefers_snapshot_over_db_session(self):
        """Should prefer settings snapshot over database session."""
        mock_session = MagicMock()
        mock_settings_manager = MagicMock()
        mock_settings_manager.get_setting.return_value = "db_value"

        with (
            patch(
                "local_deep_research.web_search_engines.search_engines_config.get_setting_from_snapshot",
                return_value="snapshot_value",
            ),
            patch(
                "local_deep_research.web_search_engines.search_engines_config.get_settings_manager",
                return_value=mock_settings_manager,
            ),
        ):
            result = _get_setting(
                "test.key",
                "default_value",
                db_session=mock_session,
                settings_snapshot={"test.key": "snapshot_value"},
            )
            assert result == "snapshot_value"

    def test_handles_snapshot_exception(self):
        """Should fall back to db_session when snapshot raises exception."""
        mock_session = MagicMock()
        mock_settings_manager = MagicMock()
        mock_settings_manager.get_setting.return_value = "db_value"

        with (
            patch(
                "local_deep_research.web_search_engines.search_engines_config.get_setting_from_snapshot",
                side_effect=Exception("Snapshot error"),
            ),
            patch(
                "local_deep_research.web_search_engines.search_engines_config.get_settings_manager",
                return_value=mock_settings_manager,
            ),
        ):
            result = _get_setting(
                "test.key",
                "default_value",
                db_session=mock_session,
                settings_snapshot={"test.key": "value"},
            )
            assert result == "db_value"

    def test_handles_db_session_exception(self):
        """Should return default when db_session raises exception."""
        mock_session = MagicMock()

        with patch(
            "local_deep_research.web_search_engines.search_engines_config.get_settings_manager",
            side_effect=Exception("DB error"),
        ):
            result = _get_setting(
                "test.key",
                "default_value",
                db_session=mock_session,
            )
            assert result == "default_value"

    def test_passes_username_to_settings_manager(self):
        """Should pass username to settings manager."""
        mock_session = MagicMock()
        mock_settings_manager = MagicMock()
        mock_settings_manager.get_setting.return_value = "value"

        with patch(
            "local_deep_research.web_search_engines.search_engines_config.get_settings_manager",
            return_value=mock_settings_manager,
        ) as mock_get_sm:
            _get_setting(
                "test.key",
                "default",
                db_session=mock_session,
                username="testuser",
            )
            mock_get_sm.assert_called_once_with(mock_session, "testuser")


class TestExtractPerEngineConfig:
    """Tests for _extract_per_engine_config function."""

    def test_extracts_simple_flat_config(self):
        """Should return flat config as-is for non-dotted keys."""
        raw_config = {"key1": "value1", "key2": "value2"}
        result = _extract_per_engine_config(raw_config)
        assert result == {"key1": "value1", "key2": "value2"}

    def test_extracts_single_level_nested_config(self):
        """Should convert single dotted keys to nested dict."""
        raw_config = {
            "engine1.param1": "value1",
            "engine1.param2": "value2",
        }
        result = _extract_per_engine_config(raw_config)
        assert "engine1" in result
        assert result["engine1"]["param1"] == "value1"
        assert result["engine1"]["param2"] == "value2"

    def test_extracts_multiple_engines(self):
        """Should extract configs for multiple engines."""
        raw_config = {
            "duckduckgo.api_key": "key1",
            "google.api_key": "key2",
            "google.cx": "cx123",
        }
        result = _extract_per_engine_config(raw_config)
        assert result["duckduckgo"]["api_key"] == "key1"
        assert result["google"]["api_key"] == "key2"
        assert result["google"]["cx"] == "cx123"

    def test_extracts_deeply_nested_config(self):
        """Should recursively extract deeply nested configs."""
        raw_config = {
            "engine.nested.param1": "value1",
            "engine.nested.param2": "value2",
        }
        result = _extract_per_engine_config(raw_config)
        assert result["engine"]["nested"]["param1"] == "value1"
        assert result["engine"]["nested"]["param2"] == "value2"

    def test_handles_empty_config(self):
        """Should return empty dict for empty input."""
        result = _extract_per_engine_config({})
        assert result == {}

    def test_mixes_flat_and_nested(self):
        """Should handle mix of flat and nested keys."""
        raw_config = {
            "simple_key": "simple_value",
            "engine.param": "nested_value",
        }
        result = _extract_per_engine_config(raw_config)
        assert result["simple_key"] == "simple_value"
        assert result["engine"]["param"] == "nested_value"


class TestSearchConfig:
    """Tests for search_config function."""

    @patch(
        "local_deep_research.web_search_engines.retriever_registry.retriever_registry"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_returns_dict_of_search_engines(
        self, mock_get_setting, mock_registry
    ):
        """Should return dict containing search engine configs."""
        mock_get_setting.return_value = {}
        mock_registry.list_registered.return_value = []

        result = search_config()
        assert isinstance(result, dict)

    @patch(
        "local_deep_research.web_search_engines.retriever_registry.retriever_registry"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_excludes_removed_meta_engines(
        self, mock_get_setting, mock_registry
    ):
        """Should not include the removed 'auto'/'meta'/'parallel' engines."""
        mock_get_setting.return_value = {}
        mock_registry.list_registered.return_value = []

        result = search_config()
        assert "auto" not in result
        assert "meta" not in result
        assert "parallel" not in result
        assert "parallel_scientific" not in result

    @patch(
        "local_deep_research.web_search_engines.retriever_registry.retriever_registry"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_includes_registered_retrievers(
        self, mock_get_setting, mock_registry
    ):
        """Should include registered retrievers as search engines."""
        mock_get_setting.return_value = {}
        mock_registry.list_registered.return_value = ["custom_retriever"]

        result = search_config()
        assert "custom_retriever" in result
        assert result["custom_retriever"]["is_retriever"] is True
        assert (
            result["custom_retriever"]["class_name"] == "RetrieverSearchEngine"
        )

    @patch(
        "local_deep_research.web_search_engines.retriever_registry.retriever_registry"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_adds_library_search_engine_when_enabled(
        self, mock_get_setting, mock_registry
    ):
        """Should add library search engine when enabled."""
        mock_registry.list_registered.return_value = []

        def get_setting_side_effect(key, default, **kwargs):
            if key == "search.engine.library.enabled":
                return True
            return default

        mock_get_setting.side_effect = get_setting_side_effect

        result = search_config()
        assert "library" in result
        assert result["library"]["class_name"] == "LibraryRAGSearchEngine"

    @patch(
        "local_deep_research.web_search_engines.retriever_registry.retriever_registry"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_skips_library_when_disabled(self, mock_get_setting, mock_registry):
        """Should skip library search engine when disabled."""
        mock_registry.list_registered.return_value = []

        def get_setting_side_effect(key, default, **kwargs):
            if key == "search.engine.library.enabled":
                return False
            return default

        mock_get_setting.side_effect = get_setting_side_effect

        result = search_config()
        assert "library" not in result

    @patch(
        "local_deep_research.web_search_engines.retriever_registry.retriever_registry"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_public_collection_config_is_also_local(
        self, mock_get_setting, mock_registry
    ):
        mock_get_setting.side_effect = lambda key, default, **_kwargs: (
            True if key == "search.engine.library.enabled" else default
        )
        mock_registry.list_registered.return_value = []
        collection = MagicMock(
            id="public",
            name="Public Papers",
            description="",
            is_public=True,
            agent_enabled=True,
        )
        session = MagicMock()
        session.query.return_value.all.return_value = [collection]
        session_context = MagicMock()
        session_context.__enter__.return_value = session
        session_context.__exit__.return_value = False

        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
        )

        with patch(
            "local_deep_research.database.session_context.get_user_db_session",
            return_value=session_context,
        ):
            result = search_config(settings_snapshot={"_username": "alice"})

        assert result["collection_public"]["is_public"] is True
        assert result["collection_public"]["is_local"] is True


class TestDefaultSearchEngine:
    """Tests for default_search_engine function."""

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_returns_configured_default(self, mock_get_setting):
        """Should return configured default search engine."""
        mock_get_setting.return_value = "google"

        result = default_search_engine()
        assert result == "google"

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_returns_wikipedia_as_default(self, mock_get_setting):
        """Should return 'wikipedia' as default when not configured."""
        mock_get_setting.return_value = "wikipedia"

        result = default_search_engine()
        assert result == "wikipedia"

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_uses_correct_setting_key(self, mock_get_setting):
        """Should query the correct setting key."""
        mock_get_setting.return_value = "duckduckgo"

        default_search_engine()
        mock_get_setting.assert_called_once()
        call_args = mock_get_setting.call_args
        assert call_args[0][0] == "search.engine.DEFAULT_SEARCH_ENGINE"
        assert call_args[0][1] == "wikipedia"

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_passes_db_session(self, mock_get_setting):
        """Should pass db_session to _get_setting."""
        mock_get_setting.return_value = "searxng"
        mock_session = MagicMock()

        default_search_engine(db_session=mock_session)
        call_kwargs = mock_get_setting.call_args[1]
        assert call_kwargs["db_session"] is mock_session

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_passes_settings_snapshot(self, mock_get_setting):
        """Should pass settings_snapshot to _get_setting."""
        mock_get_setting.return_value = "brave"
        snapshot = {"search.engine.DEFAULT_SEARCH_ENGINE": "brave"}

        default_search_engine(settings_snapshot=snapshot)
        call_kwargs = mock_get_setting.call_args[1]
        assert call_kwargs["settings_snapshot"] is snapshot


class TestListEligibleEngineConfigsAvailability:
    """Tests for runtime availability filtering in list_eligible_engine_configs."""

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._engine_class_is_available"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config.search_config"
    )
    def test_excludes_unavailable_engines(
        self, mock_search_config, mock_is_avail
    ):
        from local_deep_research.web_search_engines.search_engines_config import (
            list_eligible_engine_configs,
        )

        mock_search_config.return_value = {
            "engine_a": {
                "module_path": "a.b",
                "class_name": "ClassA",
                "requires_api_key": False,
            },
            "engine_b": {
                "module_path": "a.b",
                "class_name": "ClassB",
                "requires_api_key": False,
            },
        }

        def side_effect(name, config, snapshot):
            return name == "engine_a"

        mock_is_avail.side_effect = side_effect

        snapshot = {"some": "setting"}
        res = list_eligible_engine_configs(settings_snapshot=snapshot)

        assert "engine_a" in res
        assert "engine_b" not in res

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._engine_class_is_available"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config.search_config"
    )
    def test_pre_probe_filtering_agent_disabled(
        self, mock_search_config, mock_is_avail
    ):
        """Disabled engines for agent are skipped before running runtime availability probe."""
        from local_deep_research.web_search_engines.search_engines_config import (
            list_eligible_engine_configs,
        )

        mock_search_config.return_value = {
            "disabled_engine": {
                "agent_enabled": False,
                "requires_api_key": False,
            },
            "enabled_engine": {
                "agent_enabled": True,
                "requires_api_key": False,
            },
        }
        mock_is_avail.return_value = True

        res = list_eligible_engine_configs(
            settings_snapshot={"some": "setting"}, check_agent_enabled=True
        )

        assert "enabled_engine" in res
        assert "disabled_engine" not in res
        # Availability probe should only have been called for the enabled engine
        assert mock_is_avail.call_count == 1
        assert mock_is_avail.call_args[0][0] == "enabled_engine"

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._engine_class_is_available"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config.search_config"
    )
    def test_pre_probe_filtering_auto_search_disabled(
        self, mock_search_config, mock_is_avail
    ):
        """Engines disabled for auto search are skipped before running runtime availability probe."""
        from local_deep_research.web_search_engines.search_engines_config import (
            list_eligible_engine_configs,
        )

        mock_search_config.return_value = {
            "auto_disabled": {
                "requires_api_key": False,
            },
            "auto_enabled": {
                "requires_api_key": False,
            },
        }
        mock_is_avail.return_value = True

        snapshot = {
            "search.engine.web.auto_disabled.use_in_auto_search": False,
            "search.engine.web.auto_enabled.use_in_auto_search": True,
        }
        res = list_eligible_engine_configs(
            settings_snapshot=snapshot, check_auto_search=True
        )

        assert "auto_enabled" in res
        assert "auto_disabled" not in res
        assert mock_is_avail.call_count == 1
        assert mock_is_avail.call_args[0][0] == "auto_enabled"

    @patch(
        "local_deep_research.web_search_engines.search_engines_config._engine_class_is_available"
    )
    @patch(
        "local_deep_research.web_search_engines.search_engines_config.search_config"
    )
    def test_strict_egress_retains_primary_engine(
        self, mock_search_config, mock_is_avail
    ):
        """Under STRICT egress scope, the primary engine passes egress filter and is retained."""
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )
        from local_deep_research.web_search_engines.search_engines_config import (
            list_eligible_engine_configs,
        )

        mock_search_config.return_value = {
            "searxng": {
                "requires_api_key": False,
                "module_path": "local_deep_research.web_search_engines.engines.search_engine_searxng",
                "class_name": "SearXNGSearchEngine",
            },
            "arxiv": {
                "requires_api_key": False,
                "module_path": "local_deep_research.web_search_engines.engines.search_engine_arxiv",
                "class_name": "ArxivSearchEngine",
            },
        }
        mock_is_avail.return_value = True

        egress_ctx = EgressContext(
            scope=EgressScope.STRICT,
            primary_engine="searxng",
            require_local_llm=False,
            require_local_embeddings=False,
        )
        snapshot = {"some": "setting"}
        res = list_eligible_engine_configs(
            settings_snapshot=snapshot, egress_context=egress_ctx
        )

        assert "searxng" in res
        assert "arxiv" not in res


MODULE = "local_deep_research.web_search_engines.search_engines_config"


class TestModuleResolveApiKey:
    """Tests for the module-level ``_resolve_api_key()`` in search_engines_config.

    This is the production filter path called by ``get_available_engines()``:
    it decides whether an engine shows up in auto-search. Previously had
    zero direct test coverage — these tests would have caught the silent
    substring-overreach regression shipped by #3065 and inherited by #4715.
    """

    def _resolve(self, engine_name, engine_config, settings_snapshot):
        return _resolve_api_key(engine_name, engine_config, settings_snapshot)

    def test_returns_key_from_snapshot_dict_value(self):
        snapshot = {
            "search.engine.web.brave.api_key": {"value": "real-key-123"}
        }
        assert self._resolve("brave", {}, snapshot) == "real-key-123"

    def test_returns_key_from_snapshot_plain_string(self):
        snapshot = {"search.engine.web.brave.api_key": "real-key-456"}
        assert self._resolve("brave", {}, snapshot) == "real-key-456"

    def test_falls_back_to_engine_config_api_key(self):
        snapshot = {}
        config = {"api_key": "config-key-789"}
        assert self._resolve("brave", config, snapshot) == "config-key-789"

    def test_snapshot_takes_priority_over_config(self):
        snapshot = {
            "search.engine.web.brave.api_key": {"value": "snapshot-key"}
        }
        config = {"api_key": "config-key"}
        assert self._resolve("brave", config, snapshot) == "snapshot-key"

    def test_returns_none_when_no_key(self):
        assert self._resolve("brave", {}, {}) is None

    def test_returns_none_when_snapshot_value_is_empty(self):
        snapshot = {"search.engine.web.brave.api_key": ""}
        assert self._resolve("brave", {}, snapshot) is None

    def test_returns_none_when_snapshot_value_is_none(self):
        snapshot = {"search.engine.web.brave.api_key": None}
        assert self._resolve("brave", {}, snapshot) is None

    def test_real_key_with_api_key_substring_passes(self):
        """Regression: substring-overreach previously rejected real keys
        ending in ``_API_KEY`` — they must pass the filter. The string
        below is chosen so the OLD ``endswith("_API_KEY")`` check would
        have rejected it; the new exact-match check passes it through."""
        snapshot = {
            "search.engine.web.brave.api_key": "sk-real-secret_API_KEY",
        }
        assert self._resolve("brave", {}, snapshot) == "sk-real-secret_API_KEY"

    def test_real_key_starting_with_your_passes(self):
        snapshot = {"search.engine.web.brave.api_key": "YOUR8xk29fReal"}
        assert self._resolve("brave", {}, snapshot) == "YOUR8xk29fReal"

    def test_env_var_name_paste_passes_filter(self):
        """If a user pastes an env-var NAME (``BRAVE_API_KEY``) instead of
        its value, the filter no longer silently drops the engine — it
        passes through and the engine fails at API-call time with a clear
        auth error. That's the preferred failure mode."""
        snapshot = {"search.engine.web.brave.api_key": "BRAVE_API_KEY"}
        assert self._resolve("brave", {}, snapshot) == "BRAVE_API_KEY"


class TestGetAvailableEngines:
    """Tests for ``get_available_engines()`` — the shared filter used by
    the langgraph-agent tool builder. Locks in the contract: snapshot
    required, exclude_engines respected, use_api_key_services honoured,
    requires_api_key engines gated on a resolvable key.
    """

    def _get(self, **kwargs):
        return get_available_engines(**kwargs)

    def test_returns_empty_dict_without_snapshot(self):
        assert self._get() == {}
        assert self._get(settings_snapshot=None) == {}
        assert self._get(settings_snapshot={}) == {}

    @patch(f"{MODULE}.get_setting_from_snapshot")
    @patch(f"{MODULE}.search_config")
    def test_excludes_engines_not_enabled_for_auto_search(
        self, mock_sc, mock_gss
    ):
        """Engines with ``use_in_auto_search = False`` are filtered out."""
        mock_sc.return_value = {
            "searxng": {"requires_api_key": False},
            "brave": {"requires_api_key": False},
        }

        def gss_side_effect(key, default, **kwargs):
            return key == "search.engine.web.searxng.use_in_auto_search"

        mock_gss.side_effect = gss_side_effect

        result = self._get(settings_snapshot={"_some": "data"})
        assert "searxng" in result
        assert "brave" not in result

    @patch(f"{MODULE}._resolve_api_key", return_value=None)
    @patch(f"{MODULE}.get_setting_from_snapshot", return_value=True)
    @patch(f"{MODULE}.search_config")
    def test_excludes_engine_with_missing_api_key(
        self, mock_sc, mock_gss, mock_resolve
    ):
        mock_sc.return_value = {"brave": {"requires_api_key": True}}

        result = self._get(settings_snapshot={"_some": "data"})
        assert "brave" not in result

    @patch(f"{MODULE}._resolve_api_key", return_value="real-key")
    @patch(f"{MODULE}.get_setting_from_snapshot", return_value=True)
    @patch(f"{MODULE}.search_config")
    def test_includes_engine_with_valid_api_key(
        self, mock_sc, mock_gss, mock_resolve
    ):
        mock_sc.return_value = {"brave": {"requires_api_key": True}}

        result = self._get(settings_snapshot={"_some": "data"})
        assert "brave" in result

    @patch(f"{MODULE}.get_setting_from_snapshot", return_value=True)
    @patch(f"{MODULE}.search_config")
    def test_excludes_api_key_engines_when_use_api_key_services_false(
        self, mock_sc, mock_gss
    ):
        mock_sc.return_value = {
            "searxng": {"requires_api_key": False},
            "brave": {"requires_api_key": True},
        }

        result = self._get(
            settings_snapshot={"_some": "data"},
            use_api_key_services=False,
        )
        assert "searxng" in result
        assert "brave" not in result

    @patch(f"{MODULE}.get_setting_from_snapshot", return_value=True)
    @patch(f"{MODULE}.search_config")
    def test_respects_exclude_engines_parameter(self, mock_sc, mock_gss):
        mock_sc.return_value = {
            "searxng": {"requires_api_key": False},
            "wikipedia": {"requires_api_key": False},
        }

        result = self._get(
            settings_snapshot={"_some": "data"},
            exclude_engines={"wikipedia"},
        )
        assert "searxng" in result
        assert "wikipedia" not in result


class TestCollectionEnginesCachingAndFallback:
    """Tests for collection search engines caching, invalidation, and stale fallback."""

    def setup_method(self):
        from local_deep_research.web_search_engines.search_engines_config import (
            invalidate_collection_engines_cache,
        )

        invalidate_collection_engines_cache()

    def teardown_method(self):
        from local_deep_research.web_search_engines.search_engines_config import (
            invalidate_collection_engines_cache,
        )

        invalidate_collection_engines_cache()

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_collection_engines_cached_across_calls(
        self, mock_get_setting, mock_get_session
    ):
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_collection = MagicMock()
        mock_collection.id = "col1"
        mock_collection.name = "History Collection"
        mock_collection.description = "History papers"
        mock_collection.is_public = True
        mock_collection.agent_enabled = True

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = [mock_collection]
        mock_get_session.return_value.__enter__.return_value = mock_session

        # Call 1: should query DB
        cfg1 = search_config(username="test_researcher")
        assert "collection_col1" in cfg1
        assert (
            cfg1["collection_col1"]["display_name"]
            == "History Collection (Collection)"
        )
        assert mock_get_session.call_count == 1

        # Call 2: within TTL, should reuse cache without querying DB
        cfg2 = search_config(username="test_researcher")
        assert "collection_col1" in cfg2
        assert mock_get_session.call_count == 1  # Still 1!

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_invalidate_cache_single_user(
        self, mock_get_setting, mock_get_session
    ):
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
            invalidate_collection_engines_cache,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = []
        mock_get_session.return_value.__enter__.return_value = mock_session

        # Populate user1 and user2
        search_config(username="user1")
        search_config(username="user2")
        assert mock_get_session.call_count == 2

        # Invalidate user1 only
        invalidate_collection_engines_cache(username="user1")

        # user2 should still be cached
        search_config(username="user2")
        assert mock_get_session.call_count == 2

        # user1 should re-query
        search_config(username="user1")
        assert mock_get_session.call_count == 3

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_invalidate_cache_all_users(
        self, mock_get_setting, mock_get_session
    ):
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
            invalidate_collection_engines_cache,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = []
        mock_get_session.return_value.__enter__.return_value = mock_session

        search_config(username="user1")
        search_config(username="user2")
        assert mock_get_session.call_count == 2

        # Clear entire cache
        invalidate_collection_engines_cache()

        search_config(username="user1")
        assert mock_get_session.call_count == 3
        search_config(username="user2")
        assert mock_get_session.call_count == 4

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_invalidation_during_refresh_aborts_store(
        self, mock_get_setting, mock_get_session
    ):
        """A cache-miss reader whose DB read straddles an invalidation must
        not store its pre-change snapshot over that invalidation (#6609).

        The creator's commit lands between the reader's DB read and its
        cache store (simulated by invalidating inside the read itself).
        The reader still returns fresh data for THIS call, but its
        snapshot must not be cached: the next read re-queries instead of
        being served a stale engine list for a full TTL.
        """
        from local_deep_research.web_search_engines.search_engines_config import (
            invalidate_collection_engines_cache,
            search_config,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_collection = MagicMock()
        mock_collection.id = "col_race"
        mock_collection.name = "Raced Collection"
        mock_collection.description = None
        mock_collection.is_public = False
        mock_collection.agent_enabled = True

        mock_session = MagicMock()

        def read_that_triggers_invalidation():
            # The invalidation lands AFTER the reader's DB read started
            # but BEFORE the reader reaches its store.
            invalidate_collection_engines_cache("racer")
            return [mock_collection]

        mock_session.query.return_value.all.side_effect = (
            read_that_triggers_invalidation
        )
        mock_get_session.return_value.__enter__.return_value = mock_session

        cfg1 = search_config(username="racer")
        assert "collection_col_race" in cfg1

        # The store must have aborted: the next read re-queries the DB
        # rather than being served the invalidated snapshot.
        cfg2 = search_config(username="racer")
        assert mock_get_session.call_count == 2
        assert "collection_col_race" in cfg2

    @pytest.mark.parametrize(
        ("before_read", "during_read"),
        [
            # A user never invalidated before: logout in another tab.
            pytest.param([], ["evict"], id="never-invalidated-then-evicted"),
            # Such a user again, hit by a global (no-arg) invalidation.
            pytest.param(
                [], ["invalidate_all"], id="never-invalidated-then-global"
            ),
            # ABA: the reader captured the generation after N earlier
            # invalidations; a logout evicts, then N new invalidations.
            pytest.param(["invalidate"], ["evict", "invalidate"], id="aba-n1"),
            pytest.param(
                ["invalidate"] * 3,
                ["evict"] + ["invalidate"] * 3,
                id="aba-n3",
            ),
        ],
    )
    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_refresh_straddling_an_eviction_never_stores(
        self, mock_get_setting, mock_get_session, before_read, during_read
    ):
        """An eviction or invalidation landing between a cache-miss
        reader's lookup and its store must abort that store (#6609).

        Pins the failure class of a generation that can repeat: the value
        the reader captured must never come back. Eviction used to drop a
        per-user counter that then read back as 0. A logout followed by N
        new invalidations restored a captured N (ABA), and the captured 0
        of a never-invalidated user did not change at all. Either way the
        stale store landed: the snapshot was cached over the logout and
        the new collections, and a research started within the TTL was
        served the stale engine list.
        """
        import uuid

        from local_deep_research.web_search_engines import (
            search_engines_config as sec,
        )

        # Unique per case: no generation history from other tests.
        username = f"straddle_{uuid.uuid4().hex[:8]}"
        ops = {
            "invalidate": lambda: sec.invalidate_collection_engines_cache(
                username
            ),
            "invalidate_all": sec.invalidate_collection_engines_cache,
            "evict": lambda: sec.evict_collection_engines_cache(username),
        }

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        def collection(collection_id):
            row = MagicMock()
            row.id = collection_id
            row.name = collection_id
            row.description = None
            row.is_public = False
            row.agent_enabled = True
            return row

        reads = []

        def read_collections():
            reads.append("read")
            if len(reads) == 1:
                # Another tab acts between this miss's lookup and its
                # store; the snapshot returned predates what it wrote.
                for op in during_read:
                    ops[op]()
                return [collection("col_old")]
            return [collection("col_old"), collection("col_new")]

        mock_session = MagicMock()
        mock_session.query.return_value.all.side_effect = read_collections
        mock_get_session.return_value.__enter__.return_value = mock_session

        for op in before_read:
            ops[op]()

        sec.search_config(username=username)

        with sec._COLLECTION_CACHE_LOCK:
            assert username not in sec._COLLECTION_ENGINES_CACHE, (
                "the straddling miss stored its pre-change snapshot"
            )
        cfg = sec.search_config(username=username)
        assert len(reads) == 2  # re-queried, not served the stale snapshot
        assert "collection_col_new" in cfg

    def test_every_cache_write_advances_a_never_reused_generation(self):
        """Every invalidation (per-user or global) and every eviction,
        including one for a user with no state at all, moves the generation
        strictly forward. A value a reader captured can therefore never
        recur, which is the property the store-time check relies on."""
        from local_deep_research.web_search_engines import (
            search_engines_config as sec,
        )

        writes = [
            lambda: sec.invalidate_collection_engines_cache("gen_user"),
            lambda: sec.evict_collection_engines_cache("gen_user"),
            lambda: sec.invalidate_collection_engines_cache("gen_user"),
            sec.invalidate_collection_engines_cache,
            lambda: sec.evict_collection_engines_cache("gen_never_seen"),
        ]
        seen = [sec._COLLECTION_CACHE_GENERATION]
        for write in writes:
            write()
            seen.append(sec._COLLECTION_CACHE_GENERATION)

        assert all(a < b for a, b in zip(seen, seen[1:])), seen

    def test_evict_drops_only_that_users_entry(self):
        """Eviction (session teardown) removes the user's cache entry and
        leaves other users' entries alone. A falsy username is a no-op
        rather than the global clear ``invalidate`` would perform."""
        import time

        from local_deep_research.web_search_engines import (
            search_engines_config as sec,
        )

        with sec._COLLECTION_CACHE_LOCK:
            sec._COLLECTION_ENGINES_CACHE["leaver"] = (time.monotonic(), {})
            sec._COLLECTION_ENGINES_CACHE["stayer"] = (time.monotonic(), {})

        sec.evict_collection_engines_cache("leaver")
        sec.evict_collection_engines_cache("")

        with sec._COLLECTION_CACHE_LOCK:
            assert "leaver" not in sec._COLLECTION_ENGINES_CACHE
            assert "stayer" in sec._COLLECTION_ENGINES_CACHE

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_db_error_falls_back_to_stale_cache(
        self, mock_get_setting, mock_get_session
    ):
        import time
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
            _COLLECTION_ENGINES_CACHE,
            _COLLECTION_CACHE_LOCK,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_collection = MagicMock()
        mock_collection.id = "col_stale"
        mock_collection.name = "Stale Collection"
        mock_collection.description = "Stale papers"
        mock_collection.is_public = False
        mock_collection.agent_enabled = True

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = [mock_collection]
        mock_get_session.return_value.__enter__.return_value = mock_session

        # 1. Warm the cache
        cfg1 = search_config(username="researcher_x")
        assert "collection_col_stale" in cfg1

        # 2. Simulate cache expiration by winding back the timestamp
        with _COLLECTION_CACHE_LOCK:
            old_data = _COLLECTION_ENGINES_CACHE["researcher_x"][1]
            _COLLECTION_ENGINES_CACHE["researcher_x"] = (
                time.monotonic() - 100.0,
                old_data,
            )

        # 3. Simulate DB failure (e.g. QueuePool timeout)
        mock_get_session.side_effect = TimeoutError(
            "QueuePool connection timed out"
        )

        # 4. search_config should catch error and fall back to stale cache!
        cfg2 = search_config(username="researcher_x")
        assert "collection_col_stale" in cfg2
        assert (
            cfg2["collection_col_stale"]["display_name"]
            == "Stale Collection (Collection)"
        )

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_db_error_cold_cache_does_not_crash(
        self, mock_get_setting, mock_get_session
    ):
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )
        mock_get_session.side_effect = RuntimeError("Database unreachable")

        # Must not raise, returns other configured engines
        cfg = search_config(username="cold_user")
        assert isinstance(cfg, dict)
        assert not any(k.startswith("collection_") for k in cfg)

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_copy_on_read_prevents_cache_corruption(
        self, mock_get_setting, mock_get_session
    ):
        """Mutating a returned engine dict must not corrupt cached data for subsequent calls."""
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_collection = MagicMock()
        mock_collection.id = "col_mutate"
        mock_collection.name = "Original Name"
        mock_collection.description = "Original Description"
        mock_collection.is_public = False
        mock_collection.agent_enabled = True

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = [mock_collection]
        mock_get_session.return_value.__enter__.return_value = mock_session

        # First read
        cfg1 = search_config(username="user_mut")
        assert (
            cfg1["collection_col_mutate"]["display_name"]
            == "Original Name (Collection)"
        )

        # Mutate returned dictionary and nested default_params
        cfg1["collection_col_mutate"]["display_name"] = "Corrupted Name"
        cfg1["collection_col_mutate"]["default_params"]["collection_name"] = (
            "Corrupted Name"
        )

        # Second read from cache must remain pristine
        cfg2 = search_config(username="user_mut")
        assert (
            cfg2["collection_col_mutate"]["display_name"]
            == "Original Name (Collection)"
        )
        assert (
            cfg2["collection_col_mutate"]["default_params"]["collection_name"]
            == "Original Name"
        )

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_stale_cache_age_cap_rejects_expired_stale_entries(
        self, mock_get_setting, mock_get_session
    ):
        """Stale cached engines older than _COLLECTION_CACHE_MAX_STALE must not be served."""
        import time
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
            _COLLECTION_ENGINES_CACHE,
            _COLLECTION_CACHE_LOCK,
            _COLLECTION_CACHE_MAX_STALE,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_collection = MagicMock()
        mock_collection.id = "col_expired_stale"
        mock_collection.name = "Old Stale Collection"
        mock_collection.description = "Old stale papers"
        mock_collection.is_public = False
        mock_collection.agent_enabled = True

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = [mock_collection]
        mock_get_session.return_value.__enter__.return_value = mock_session

        # 1. Warm cache
        cfg1 = search_config(username="user_stale_expired")
        assert "collection_col_expired_stale" in cfg1

        # 2. Wind back cache timestamp beyond _COLLECTION_CACHE_MAX_STALE
        with _COLLECTION_CACHE_LOCK:
            old_data = _COLLECTION_ENGINES_CACHE["user_stale_expired"][1]
            _COLLECTION_ENGINES_CACHE["user_stale_expired"] = (
                time.monotonic() - (_COLLECTION_CACHE_MAX_STALE + 10.0),
                old_data,
            )

        # 3. Simulate DB failure
        mock_get_session.side_effect = TimeoutError(
            "Database connection timed out"
        )

        # 4. Outage re-queries DB, fails, and stale cache is expired -> must NOT serve stale collection
        cfg2 = search_config(username="user_stale_expired")
        assert "collection_col_expired_stale" not in cfg2

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_unbounded_growth_bounded_by_lru_cache(
        self, mock_get_setting, mock_get_session
    ):
        """Cache must be bounded by LRU eviction when users exceed _COLLECTION_CACHE_MAXSIZE."""
        from local_deep_research.web_search_engines.search_engines_config import (
            search_config,
            _COLLECTION_ENGINES_CACHE,
            _COLLECTION_CACHE_LOCK,
            _COLLECTION_CACHE_MAXSIZE,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = []
        mock_get_session.return_value.__enter__.return_value = mock_session

        # Fill cache up to maxsize + 5 users
        total_users = _COLLECTION_CACHE_MAXSIZE + 5
        for i in range(total_users):
            search_config(username=f"user_{i}")

        with _COLLECTION_CACHE_LOCK:
            assert len(_COLLECTION_ENGINES_CACHE) <= _COLLECTION_CACHE_MAXSIZE
            # Earliest users (e.g. user_0) must have been evicted by LRU
            assert "user_0" not in _COLLECTION_ENGINES_CACHE
            assert f"user_{total_users - 1}" in _COLLECTION_ENGINES_CACHE

    @patch("local_deep_research.database.session_context.get_user_db_session")
    @patch(
        "local_deep_research.web_search_engines.search_engines_config._get_setting"
    )
    def test_flip_is_public_cache_expiry_and_runtime_containment(
        self, mock_get_setting, mock_get_session
    ):
        """Flip is_public True->False: before cache expiry, listing still shows engine
        while runtime search() fails closed at the DB backstop; once expired, engine
        is dropped from list_eligible_engine_configs and runtime search() still fails closed."""
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
            PolicyDeniedError,
        )
        from local_deep_research.web_search_engines.search_engines_config import (
            list_eligible_engine_configs,
            invalidate_collection_engines_cache,
        )
        from local_deep_research.web_search_engines.engines.search_engine_collection import (
            CollectionSearchEngine,
        )

        mock_get_setting.side_effect = lambda key, default=None, **kw: (
            True
            if "library.enabled" in key
            else ({} if "search.engine.web" in key else default)
        )

        mock_collection = MagicMock()
        mock_collection.id = "col_egress"
        mock_collection.name = "Public Research"
        mock_collection.description = "Open access docs"
        mock_collection.is_public = True
        mock_collection.agent_enabled = True
        mock_collection.embedding_model = "test-embed"
        mock_collection.embedding_model_type = None

        mock_session = MagicMock()
        mock_session.query.return_value.all.return_value = [mock_collection]
        mock_session.query.return_value.filter.return_value.first.return_value = mock_collection
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_collection
        mock_get_session.return_value.__enter__.return_value = mock_session

        snapshot = {
            "_username": "sec_user",
            "policy.egress_scope": {"value": "public_only"},
            "search.tool": {"value": "wikipedia"},
        }
        egress_ctx = EgressContext(
            scope=EgressScope.PUBLIC_ONLY,
            primary_engine="wikipedia",
            require_local_llm=False,
            require_local_embeddings=False,
            username="sec_user",
        )

        # 1. Initially is_public is True: collection is listed as eligible under PUBLIC_ONLY
        eligible_configs = list_eligible_engine_configs(
            settings_snapshot=snapshot, egress_context=egress_ctx
        )
        assert "collection_col_egress" in eligible_configs

        # 2. In DB, flip collection visibility to private: is_public = False
        mock_collection.is_public = False

        # 3. Runtime egress backstop: CollectionSearchEngine.search() executes fresh DB check
        # and fails closed immediately with PolicyDeniedError (even while cache is still warm)
        engine = CollectionSearchEngine(
            collection_id="col_egress",
            collection_name="Public Research",
            settings_snapshot=snapshot,
        )
        with pytest.raises(PolicyDeniedError):
            engine.search("query")

        # 4. Expire/invalidate collection cache
        invalidate_collection_engines_cache("sec_user")

        # 5. After cache expiration: list_eligible_engine_configs re-reads DB and drops the engine
        updated_configs = list_eligible_engine_configs(
            settings_snapshot=snapshot, egress_context=egress_ctx
        )
        assert "collection_col_egress" not in updated_configs

        # 6. Runtime backstop continues to fail closed
        with pytest.raises(PolicyDeniedError):
            engine.search("query")

    @patch(
        "local_deep_research.web_search_engines.search_engines_config.invalidate_collection_engines_cache"
    )
    @patch("local_deep_research.database.session_context.get_user_db_session")
    def test_create_and_update_routes_invalidate_cache(
        self, mock_get_session, mock_invalidate
    ):
        """Creating and updating collections in rag routes must invalidate collection engines cache."""
        from local_deep_research.web.routers.rag import (
            _create_collection_sync,
            _update_collection_sync,
        )

        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = None
        mock_get_session.return_value.__enter__.return_value = mock_session

        # Create collection
        create_res = _create_collection_sync(
            {"name": "New Collection", "description": "Desc"},
            username="user_routes",
        )
        assert create_res["success"] is True
        mock_invalidate.assert_called_with("user_routes")

        mock_invalidate.reset_mock()

        # Update collection
        existing_col = MagicMock()
        existing_col.id = "col_up"
        existing_col.name = "Old Name"
        existing_col.description = "Old Desc"
        existing_col.created_at = None
        existing_col.collection_type = "user_uploads"
        existing_col.is_public = False
        existing_col.agent_enabled = True
        mock_session.query.return_value.filter_by.return_value.first.return_value = existing_col
        mock_session.query.return_value.filter.return_value.first.return_value = None

        update_res = _update_collection_sync(
            {"is_public": True},
            collection_id="col_up",
            username="user_routes",
        )
        assert update_res["success"] is True
        mock_invalidate.assert_called_with("user_routes")

    @patch(
        "local_deep_research.web_search_engines.search_engines_config.invalidate_collection_engines_cache"
    )
    @patch(
        "local_deep_research.web.routers.library_delete.CollectionDeletionService"
    )
    def test_delete_route_invalidates_cache(
        self, mock_service_cls, mock_invalidate
    ):
        """Deleting a collection via the delete route must invalidate collection engines cache."""
        from local_deep_research.web.routers.library_delete import (
            delete_collection,
        )

        mock_service = MagicMock()
        mock_service.delete_collection.return_value = {"deleted": True}
        mock_service_cls.return_value = mock_service

        mock_request = MagicMock()
        res = delete_collection(
            request=mock_request,
            collection_id="col_del",
            username="user_del",
        )
        assert res["success"] is True
        mock_invalidate.assert_called_with("user_del")