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.

145 KB
Raw Blame
"""
Tests for the SearXNGSearchEngine class.

Tests cover:
- Initialization and configuration
- Safe search settings
- Rate limiting
- Search result parsing
- Preview generation
- Full content retrieval
- Error handling
"""

import requests

from unittest.mock import Mock, call, patch

import pytest

from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
)


class TestSafeSearchSetting:
    """Tests for SafeSearchSetting enum."""

    def test_safe_search_values(self):
        """SafeSearchSetting has correct values."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SafeSearchSetting,
        )

        assert SafeSearchSetting.OFF.value == 0
        assert SafeSearchSetting.MODERATE.value == 1
        assert SafeSearchSetting.STRICT.value == 2

    def test_safe_search_names(self):
        """SafeSearchSetting has correct names."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SafeSearchSetting,
        )

        assert SafeSearchSetting.OFF.name == "OFF"
        assert SafeSearchSetting.MODERATE.name == "MODERATE"
        assert SafeSearchSetting.STRICT.name == "STRICT"


class TestSearXNGSearchEngineInit:
    """Tests for SearXNGSearchEngine initialization."""

    def test_init_with_accessible_instance(self):
        """Initialize with accessible SearXNG instance."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                max_results=10,
            )

            assert engine._is_available is True
            assert engine.instance_url == "http://localhost:8080"
            assert engine.max_results == 10

    def test_init_with_inaccessible_instance(self):
        """Initialize with inaccessible SearXNG instance."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 500
            mock_response.raise_for_status.side_effect = requests.HTTPError(
                response=mock_response
            )
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert engine._is_available is False

    def test_init_with_connection_error(self):
        """Initialize handles connection errors gracefully."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )
        import requests

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_get.side_effect = requests.RequestException(
                "Connection refused"
            )

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert engine._is_available is False

    def test_init_strips_trailing_slash(self):
        """Initialize strips trailing slash from instance URL."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080/",
            )

            assert engine.instance_url == "http://localhost:8080"

    def test_init_with_custom_categories(self):
        """Initialize with custom categories."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                categories=["news", "science"],
            )

            assert engine.categories == ["news", "science"]

    def test_init_with_custom_engines(self):
        """Initialize with custom engines."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                engines=["google", "bing"],
            )

            assert engine.engines == ["google", "bing"]

    def test_init_default_categories(self):
        """Initialize uses default categories."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert engine.categories == ["general"]


class TestSafeSearchParsing:
    """Tests for safe search setting parsing."""

    def test_safe_search_string_name(self):
        """Parse safe search from string name."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
            SafeSearchSetting,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                safe_search="STRICT",
            )

            assert engine.safe_search == SafeSearchSetting.STRICT

    def test_safe_search_integer(self):
        """Parse safe search from integer."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
            SafeSearchSetting,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                safe_search=1,
            )

            assert engine.safe_search == SafeSearchSetting.MODERATE

    def test_safe_search_string_integer(self):
        """Parse safe search from string integer."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
            SafeSearchSetting,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                safe_search="2",
            )

            assert engine.safe_search == SafeSearchSetting.STRICT

    def test_safe_search_invalid_defaults_to_off(self):
        """Invalid safe search value defaults to OFF."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
            SafeSearchSetting,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                safe_search="INVALID_VALUE",
            )

            assert engine.safe_search == SafeSearchSetting.OFF


class TestIsValidSearchResult:
    """Tests for _is_valid_search_result method."""

    def test_valid_http_url(self):
        """Accept valid HTTP URL."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert engine._is_valid_search_result("http://example.com") is True

    def test_valid_https_url(self):
        """Accept valid HTTPS URL."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert engine._is_valid_search_result("https://example.com") is True

    def test_reject_relative_url(self):
        """Reject relative URL."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert (
                engine._is_valid_search_result("/stats?engine=google") is False
            )

    def test_reject_instance_url(self):
        """Reject URLs pointing to SearXNG instance itself."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert (
                engine._is_valid_search_result(
                    "http://localhost:8080/stats?engine=google"
                )
                is False
            )

    def test_reject_empty_url(self):
        """Reject empty URL."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            assert engine._is_valid_search_result("") is False
            assert engine._is_valid_search_result(None) is False


# Substrings identifying the two capacity-state lines the SearXNG rate
# limiter emits, kept here so a wording change touches one place.
SATURATION_WARNING = "not enough to bring the tracker back under"
RECOVERY_LOG = "evictable again"


def _track_urls(limiter, count=4, port_base=8080):
    """Put ``count`` URLs into the tracker through the public entry point.

    Returns them in creation order, so ``urls[0]`` is the oldest entry.
    """
    urls = [f"http://localhost:{port_base + i}" for i in range(count)]
    for url in urls:
        limiter.respect_rate_limit(url, 0.1)
    return urls


def _hold_every_lock(limiter, urls):
    """Acquire the tracked lock of every URL in ``urls``, in order.

    Returns the locks so the caller can release them. A test that releases
    some of them mid-body is expected to guard its own cleanup.
    """
    held = [limiter._url_state[url].lock for url in urls]
    for lock in held:
        assert lock.acquire(timeout=10)
    return held


def _saturate(limiter, count=4, port_base=8080):
    """Track ``count`` URLs and hold every one of their locks.

    The starting point of most of the capacity-state tests: the tracker sits
    at the capacity the test monkeypatched, with nothing evictable.
    """
    urls = _track_urls(limiter, count, port_base)
    return urls, _hold_every_lock(limiter, urls)


class TestRateLimiting:
    """Tests for rate limiting functionality."""

    @pytest.fixture(autouse=True)
    def auto_reset_rate_limiter(self):
        """Automatically reset rate limiter state before and after each test."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        _searxng_rate_limiter.reset_for_tests()
        yield
        _searxng_rate_limiter.reset_for_tests()

    def test_reset_for_tests_clears_global_state(self):
        """reset_for_tests directly empties tracked locks and last-request timestamps."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        _searxng_rate_limiter.respect_rate_limit("http://localhost:8080", 0.5)
        assert "http://localhost:8080" in _searxng_rate_limiter._url_state
        assert (
            _searxng_rate_limiter._url_state[
                "http://localhost:8080"
            ].last_request
            > 0
        )

        _searxng_rate_limiter.reset_for_tests()
        assert len(_searxng_rate_limiter._url_state) == 0

    def test_no_rate_limit_when_delay_is_zero_or_negative(self):
        """Delay of 0 or negative returns immediately without populating tracked state."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                delay_between_requests=0.0,
            )

            with patch("time.sleep") as mock_sleep:
                engine._respect_rate_limit()
                engine._respect_rate_limit()
                mock_sleep.assert_not_called()

        _searxng_rate_limiter.respect_rate_limit("http://localhost:8080", -1.0)
        assert len(_searxng_rate_limiter._url_state) == 0

    def test_rate_limit_shared_across_instances(self):
        """Two engines on the same URL share the rate-limit timestamp in global state."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        def _make_engine():
            with patch(
                "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
            ) as mock_get:
                mock_response = Mock()
                mock_response.status_code = 200
                mock_get.return_value = mock_response
                return SearXNGSearchEngine(
                    instance_url="http://localhost:8080",
                    delay_between_requests=0.5,
                )

        first = _make_engine()
        second = _make_engine()

        with patch("time.sleep") as mock_sleep:
            first._respect_rate_limit()
            initial_ts = _searxng_rate_limiter._url_state[
                "http://localhost:8080"
            ].last_request
            assert initial_ts > 0

            second._respect_rate_limit()
            # Second call should have slept the configured delay because
            # the limiter is shared across the two engine instances.
            mock_sleep.assert_called_once()
            sleep_arg = mock_sleep.call_args.args[0]
            assert 0 < sleep_arg <= 0.5

            second_ts = _searxng_rate_limiter._url_state[
                "http://localhost:8080"
            ].last_request
            assert second_ts >= initial_ts

    def test_rate_limit_isolated_per_instance_url(self):
        """Engines targeting different URLs do not block each other."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        def _make_engine(url):
            with patch(
                "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
            ) as mock_get:
                mock_response = Mock()
                mock_response.status_code = 200
                mock_get.return_value = mock_response
                return SearXNGSearchEngine(
                    instance_url=url,
                    delay_between_requests=0.5,
                )

        first = _make_engine("http://localhost:8080")
        second = _make_engine("http://localhost:8081")

        with patch("time.sleep") as mock_sleep:
            first._respect_rate_limit()
            second._respect_rate_limit()
            mock_sleep.assert_not_called()

    def test_rate_limit_url_normalization(self):
        """URLs with and without trailing slashes map to the same rate limit bucket."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        with patch("time.sleep") as mock_sleep:
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8080/", 0.5
            )
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8080", 0.5
            )
            mock_sleep.assert_called_once()

    def test_rate_limit_logging(self):
        """Sleeping logs an INFO message with the wait time."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        with (
            patch(
                "local_deep_research.web_search_engines.engines._searxng_rate_limiter.logger"
            ) as mock_logger,
            patch("time.sleep"),
        ):
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8080", 0.5
            )
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8080", 0.5
            )

            mock_logger.info.assert_called_once()
            log_msg = mock_logger.info.call_args.args[0]
            assert "SearXNG rate limiting: waiting" in log_msg

    def test_rate_limiter_evicts_old_entries_at_max_capacity(self, monkeypatch):
        """Evict oldest entries when MAX_TRACKED_URLS capacity is reached."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        for i in range(4):
            _searxng_rate_limiter.respect_rate_limit(
                f"http://localhost:808{i}", 0.1
            )

        assert len(_searxng_rate_limiter._url_state) == 4

        # Adding a 5th URL should trigger eviction of older entries
        _searxng_rate_limiter.respect_rate_limit("http://localhost:8084", 0.1)
        assert len(_searxng_rate_limiter._url_state) <= 3
        assert "http://localhost:8084" in _searxng_rate_limiter._url_state

    def test_eviction_skips_a_lock_another_thread_holds(self, monkeypatch):
        """The oldest entry is held, so eviction must move on to the next one."""
        import threading
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        for i in range(4):
            _searxng_rate_limiter.respect_rate_limit(
                f"http://localhost:808{i}", 0.1
            )

        held_url = "http://localhost:8080"
        held_lock = _searxng_rate_limiter._url_state[held_url].lock
        acquired = threading.Event()
        release = threading.Event()

        def holder():
            with held_lock:
                acquired.set()
                release.wait(10)

        thread = threading.Thread(target=holder)
        thread.start()
        try:
            assert acquired.wait(10)
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8084", 0.1
            )
            held_state = _searxng_rate_limiter._url_state.get(held_url)
            assert held_state is not None and held_state.lock is held_lock
            assert (
                "http://localhost:8081" not in _searxng_rate_limiter._url_state
            )
        finally:
            release.set()
            thread.join(10)

    def test_eviction_keeps_everything_when_all_locks_are_held(
        self, monkeypatch
    ):
        """With no free entry the tracker grows past capacity rather than
        dropping a lock that is in use."""
        import threading
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls = _track_urls(_searxng_rate_limiter)

        all_acquired = threading.Barrier(len(urls) + 1)
        release = threading.Event()

        def holder(url):
            with _searxng_rate_limiter._url_state[url].lock:
                try:
                    all_acquired.wait(10)
                except threading.BrokenBarrierError:
                    return
                release.wait(10)

        threads = [threading.Thread(target=holder, args=(url,)) for url in urls]
        for thread in threads:
            thread.start()
        try:
            all_acquired.wait(10)
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8084", 0.1
            )
            assert len(_searxng_rate_limiter._url_state) == 5
            for url in urls:
                assert url in _searxng_rate_limiter._url_state
        finally:
            release.set()
            for thread in threads:
                thread.join(10)

    def test_all_locks_held_warns_once_then_notes_recovery(
        self, monkeypatch, loguru_caplog
    ):
        """The all-held warning is a state transition, not a per-call log."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls, held = _saturate(_searxng_rate_limiter)
        try:
            with loguru_caplog.at_level("INFO"):
                for _ in range(3):
                    _searxng_rate_limiter._get_url_state(
                        "http://localhost:8084"
                    )
                assert loguru_caplog.text.count(SATURATION_WARNING) == 1
                assert RECOVERY_LOG not in loguru_caplog.text
        finally:
            for lock in held:
                lock.release()

        with loguru_caplog.at_level("INFO"):
            _searxng_rate_limiter._get_url_state(urls[0])
            assert RECOVERY_LOG in loguru_caplog.text
        assert _searxng_rate_limiter._all_held_warned_at is None

    def test_all_locks_held_warns_again_once_the_interval_elapses(
        self, monkeypatch, loguru_caplog
    ):
        """A condition that never clears must not go silent after one line."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
        monkeypatch.setattr(
            _searxng_rate_limiter, "ALL_HELD_REWARN_SECONDS", 0.0
        )

        urls, held = _saturate(_searxng_rate_limiter)
        try:
            with loguru_caplog.at_level("WARNING"):
                for _ in range(2):
                    _searxng_rate_limiter._get_url_state(
                        "http://localhost:8084"
                    )
                assert loguru_caplog.text.count(SATURATION_WARNING) == 2
        finally:
            for lock in held:
                lock.release()

    def test_transient_evictable_entry_is_not_a_recovery(
        self, monkeypatch, loguru_caplog
    ):
        """A lock between publication and acquisition must not clear the state.

        ``_get_url_state`` publishes a lock before its caller acquires it, so at
        capacity a single entry routinely reports ``locked() == False`` for the
        length of that window while every other lock is genuinely in use.
        Reading that one free slot as a recovery would clear the throttle and
        let the very next call warn again.

        The whole cycle runs on lookups of URLs that are already tracked, so it
        also pins the decision to every ``_get_url_state`` call rather than only
        to the calls that create a lock.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 20)
        assert _searxng_rate_limiter._recovery_headroom_slots() == 2

        urls, held = _saturate(_searxng_rate_limiter, 20, 9000)
        try:
            with loguru_caplog.at_level("INFO"):
                # At capacity with every lock in use. No new URL is involved:
                # a lookup of an already-tracked URL has to see this.
                _searxng_rate_limiter._get_url_state(urls[0])
                assert loguru_caplog.text.count(SATURATION_WARNING) == 1

                # That URL's holder finishes, and the next caller for it picks
                # the lock up again from _get_url_state. For the length of that
                # window the entry is evictable: one free slot out of twenty,
                # with nineteen locks still held. Not a recovery.
                held[0].release()
                reacquired = _searxng_rate_limiter._get_url_state(urls[0]).lock
                assert reacquired is held[0]
                assert RECOVERY_LOG not in loguru_caplog.text
                assert _searxng_rate_limiter._all_held_warned_at is not None

                # The window closes and the tracker is saturated again, which
                # must not read as a fresh episode either.
                assert reacquired.acquire(timeout=10)
                _searxng_rate_limiter._get_url_state(urls[1])
                assert RECOVERY_LOG not in loguru_caplog.text
                assert loguru_caplog.text.count(SATURATION_WARNING) == 1
            assert _searxng_rate_limiter._all_held_warned_at is not None
        finally:
            # held[0] is released and reacquired mid-test, so an assertion
            # failing inside that window would otherwise be masked by
            # ``RuntimeError: release unlocked lock`` from this cleanup.
            for lock in held:
                if lock.locked():
                    lock.release()

    def test_partial_recovery_below_the_threshold_stays_warned(
        self, monkeypatch, loguru_caplog
    ):
        """Two evictable entries at one over capacity give only one free slot.

        Recovery needs ``_recovery_headroom_slots()`` of headroom, so a
        margin that thin keeps the state warned -- and therefore keeps the
        re-warn interval, rather than the next new URL, in charge.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 20)
        assert _searxng_rate_limiter._recovery_headroom_slots() == 2

        urls, held = _saturate(_searxng_rate_limiter, 20, 9000)
        released = []
        try:
            with loguru_caplog.at_level("INFO"):
                _searxng_rate_limiter._get_url_state("http://localhost:9900")
                assert loguru_caplog.text.count(SATURATION_WARNING) == 1

                # One lock comes back; with 9900's still-unacquired lock that
                # is one slot of headroom out of twenty.
                held[0].release()
                released.append(held[0])
                _searxng_rate_limiter._get_url_state(urls[1])
                assert RECOVERY_LOG not in loguru_caplog.text
                assert _searxng_rate_limiter._all_held_warned_at is not None

                # Three more make the margin real.
                for lock in held[1:4]:
                    lock.release()
                    released.append(lock)
                _searxng_rate_limiter._get_url_state(urls[4])
                assert RECOVERY_LOG in loguru_caplog.text
                assert _searxng_rate_limiter._all_held_warned_at is None
        finally:
            for lock in held:
                if lock not in released:
                    lock.release()

    def test_second_exhaustion_episode_is_logged_within_the_interval(
        self, monkeypatch, loguru_caplog
    ):
        """A genuinely distinct episode is not swallowed by the re-warn timer.

        The interval only suppresses repeats of an episode that never cleared.
        Once the condition is observed to have cleared, the next exhaustion is
        a new event and must be reported immediately.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls = _track_urls(_searxng_rate_limiter)

        with loguru_caplog.at_level("INFO"):
            held = _hold_every_lock(_searxng_rate_limiter, urls)
            try:
                _searxng_rate_limiter._get_url_state("http://localhost:8084")
            finally:
                for lock in held:
                    lock.release()

            # The condition clears while traffic continues on tracked URLs.
            _searxng_rate_limiter._get_url_state(urls[0])
            assert _searxng_rate_limiter._all_held_warned_at is None

            # A second, unrelated exhaustion, well inside ALL_HELD_REWARN_SECONDS.
            episode_two = [
                _searxng_rate_limiter._url_state[url].lock
                for url in list(_searxng_rate_limiter._url_state)
            ]
            for lock in episode_two:
                assert lock.acquire(timeout=10)
            try:
                _searxng_rate_limiter._get_url_state("http://localhost:8085")
            finally:
                for lock in episode_two:
                    lock.release()

            assert loguru_caplog.text.count(SATURATION_WARNING) == 2

    def test_recovery_log_reports_counts_and_duration(
        self, monkeypatch, loguru_caplog
    ):
        """The recovery line carries the same kind of numbers as the warning."""
        import re

        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls, held = _saturate(_searxng_rate_limiter)
        try:
            _searxng_rate_limiter._get_url_state("http://localhost:8084")
        finally:
            for lock in held:
                lock.release()

        with loguru_caplog.at_level("INFO"):
            _searxng_rate_limiter._get_url_state(urls[0])

        assert re.search(
            r"5 of 5 tracked URL locks are evictable again after \d+\.\d+s",
            loguru_caplog.text,
        )

    def test_recovery_duration_spans_the_episode_not_the_last_rewarn(
        self, monkeypatch, loguru_caplog
    ):
        """held_for measures time at capacity, not time since the re-warn.

        ``_all_held_warned_at`` is rewritten on every re-warn so it can keep
        governing ``ALL_HELD_REWARN_SECONDS``. Deriving the duration from it
        would make a long episode punctuated by re-warns report roughly the
        re-warn interval instead of its real length.
        """
        import re
        import time
        from types import SimpleNamespace

        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
        monkeypatch.setattr(
            _searxng_rate_limiter, "ALL_HELD_REWARN_SECONDS", 0.0
        )

        clock = [1000.0]
        # Rebind the module's ``time`` name rather than setting ``monotonic``
        # on ``_searxng_rate_limiter.time`` -- that attribute *is* the stdlib
        # ``time`` module, so patching through it swaps the clock out from
        # under every other thread in the process for the test's duration.
        monkeypatch.setattr(
            _searxng_rate_limiter,
            "time",
            SimpleNamespace(monotonic=lambda: clock[0], sleep=time.sleep),
        )

        urls, held = _saturate(_searxng_rate_limiter)
        try:
            # Onset, then two re-warns 100s apart. The last re-warn lands at
            # t=1200, so a timer-derived duration would report ~0s.
            _searxng_rate_limiter._get_url_state("http://localhost:8084")
            clock[0] = 1100.0
            _searxng_rate_limiter._get_url_state("http://localhost:8085")
            clock[0] = 1200.0
            _searxng_rate_limiter._get_url_state("http://localhost:8086")
        finally:
            for lock in held:
                lock.release()

        clock[0] = 1300.0
        with loguru_caplog.at_level("INFO"):
            _searxng_rate_limiter._get_url_state(urls[0])

        match = re.search(
            r"evictable again after (\d+\.\d+)s", loguru_caplog.text
        )
        assert match, loguru_caplog.text
        assert float(match.group(1)) == 300.0

    def test_capacity_logs_are_emitted_outside_the_meta_lock(self, monkeypatch):
        """The capacity lines are written with ``_meta_lock`` released."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls = _track_urls(_searxng_rate_limiter)

        meta_lock_held_during_emit = []

        class _RecordingLogger:
            def _record(self, *args, **kwargs):
                meta_lock_held_during_emit.append(
                    _searxng_rate_limiter._meta_lock.locked()
                )

            warning = _record
            info = _record

        monkeypatch.setattr(_searxng_rate_limiter, "logger", _RecordingLogger())

        held = _hold_every_lock(_searxng_rate_limiter, urls)
        try:
            _searxng_rate_limiter._get_url_state("http://localhost:8084")
        finally:
            for lock in held:
                lock.release()
        _searxng_rate_limiter._get_url_state(urls[0])

        assert meta_lock_held_during_emit == [False, False]

    def test_zero_capacity_warns_only_about_locks_actually_in_use(
        self, monkeypatch, loguru_caplog
    ):
        """The warning never describes a table with nothing in use.

        ``headroom <= 0`` alone is not enough: at a capacity of zero the very
        first lookup satisfies it while the one lock it just created is free
        and unheld. The warning speaks about locks that are in use, so it
        fires only when at least one of them is.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 0)

        # An empty tracker has nothing to report, whatever the capacity says.
        with _searxng_rate_limiter._meta_lock:
            assert _searxng_rate_limiter._capacity_pressure_unlocked() is None

        with loguru_caplog.at_level("WARNING"):
            lock = _searxng_rate_limiter._get_url_state(
                "http://localhost:8080"
            ).lock

        assert "tracked URL locks" not in loguru_caplog.text

        # A lock that really is held is a different matter, and still warns.
        assert lock.acquire(timeout=10)
        try:
            with loguru_caplog.at_level("WARNING"):
                _searxng_rate_limiter._get_url_state("http://localhost:8080")
            assert (
                "only 0 of 1 tracked URL locks are evictable"
                in loguru_caplog.text
            )
        finally:
            lock.release()

    def test_eviction_window_leaves_no_orphaned_timestamp(self, monkeypatch):
        """A URL evicted before its timestamp is written must not leak a key.

        Each URL carries exactly one ``_UrlState`` (#5688), so eviction pops
        one entry from ``_url_state`` and a timestamp write for an
        already-evicted URL is an attribute write on the orphaned state the
        caller still holds -- it cannot recreate the dict entry eviction just
        removed.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        real_get_url_state = _searxng_rate_limiter._get_url_state

        def evicting_get_url_state(normalized_url):
            state = real_get_url_state(normalized_url)
            # Stand in for another thread evicting the entry in the window
            # between publication of the state and the caller acquiring its
            # lock.
            with _searxng_rate_limiter._meta_lock:
                _searxng_rate_limiter._url_state.pop(normalized_url, None)
            return state

        monkeypatch.setattr(
            _searxng_rate_limiter, "_get_url_state", evicting_get_url_state
        )

        for _ in range(5):
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8080", 0.1
            )

        assert _searxng_rate_limiter._url_state == {}

    def test_evicted_state_still_throttles_callers_that_already_hold_it(
        self, monkeypatch
    ):
        """Eviction drops the map entry, but existing callers share its clock.

        An identity guard on the final timestamp write would leave both
        callers seeing the initial zero timestamp and skipping their delay.
        Updating the retained state preserves their timing without restoring
        an evicted dictionary entry.
        """
        from types import SimpleNamespace

        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter as limiter,
        )

        url = "http://localhost:8080"
        first = limiter._get_url_state(url)
        second = limiter._get_url_state(url)
        assert first is second
        with limiter._meta_lock:
            limiter._url_state.pop(url)

        retained = [first, second]
        monkeypatch.setattr(
            limiter, "_get_url_state", lambda _: retained.pop(0)
        )
        now = [100.0]
        sleeps = []

        def advance(seconds):
            sleeps.append(seconds)
            now[0] += seconds

        monkeypatch.setattr(
            limiter,
            "time",
            SimpleNamespace(monotonic=lambda: now[0], sleep=advance),
        )
        limiter.respect_rate_limit(url, 0.5)
        limiter.respect_rate_limit(url, 0.5)

        assert sleeps == [0.5]
        assert first.last_request == 100.5
        assert limiter._url_state == {}

    def test_a_superseded_capacity_line_is_dropped_not_logged_late(
        self, monkeypatch, loguru_caplog
    ):
        """The last line an operator sees must agree with the live state.

        The decision runs under ``_meta_lock`` and the emission deliberately
        does not, so a thread descheduled in between can publish a recovery
        *after* a later thread has already published the warning that
        superseded it -- leaving "evictable again" as the operator's last word
        on a saturated tracker, with the next warning then suppressed for
        ``ALL_HELD_REWARN_SECONDS``.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls, held = _saturate(_searxng_rate_limiter)
        with loguru_caplog.at_level("INFO"):
            _searxng_rate_limiter._get_url_state("http://localhost:8084")
            for lock in held:
                lock.release()

            # Thread A decides recovery and is descheduled before emitting.
            with _searxng_rate_limiter._meta_lock:
                recovery = _searxng_rate_limiter._capacity_pressure_unlocked()
            assert recovery is not None and recovery[0] is False

            # Thread B decides the next warning and emits it first.
            everything = [
                state.lock
                for state in _searxng_rate_limiter._url_state.values()
            ]
            for lock in everything:
                assert lock.acquire(timeout=10)
            try:
                with _searxng_rate_limiter._meta_lock:
                    warning = (
                        _searxng_rate_limiter._capacity_pressure_unlocked()
                    )
                assert warning is not None and warning[0] is True

                _searxng_rate_limiter._emit_capacity_log(warning)
                # Thread A wakes up. Its record is stale and must not run.
                _searxng_rate_limiter._emit_capacity_log(recovery)
            finally:
                for lock in everything:
                    lock.release()

        assert RECOVERY_LOG not in loguru_caplog.text
        assert loguru_caplog.text.count(SATURATION_WARNING) == 2
        assert _searxng_rate_limiter._all_held_warned_at is not None

    def test_capacity_lines_still_emit_in_order_when_not_superseded(
        self, monkeypatch, loguru_caplog
    ):
        """Dropping stale records must not drop the ordinary ones."""
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls, held = _saturate(_searxng_rate_limiter)
        with loguru_caplog.at_level("INFO"):
            _searxng_rate_limiter._get_url_state("http://localhost:8084")
            for lock in held:
                lock.release()
            _searxng_rate_limiter._get_url_state(urls[0])

        assert loguru_caplog.text.count(SATURATION_WARNING) == 1
        assert loguru_caplog.text.count(RECOVERY_LOG) == 1
        assert loguru_caplog.text.index(SATURATION_WARNING) < (
            loguru_caplog.text.index(RECOVERY_LOG)
        )

    def test_small_capacity_still_has_a_hysteresis_band(
        self, monkeypatch, loguru_caplog
    ):
        """The band must not collapse onto the warn threshold.

        Below twenty entries the divisor alone supplies at most one slot.
        The two-slot floor keeps a briefly free lock from clearing the
        warning: one free slot out of four stays inside the recovery band.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
        assert _searxng_rate_limiter._recovery_headroom_slots() == 2

        urls, held = _saturate(_searxng_rate_limiter)
        released = []
        try:
            with loguru_caplog.at_level("INFO"):
                _searxng_rate_limiter._get_url_state(urls[0])
                assert loguru_caplog.text.count(SATURATION_WARNING) == 1

                # One slot back out of four: inside the band, still warned.
                held[0].release()
                released.append(held[0])
                _searxng_rate_limiter._get_url_state(urls[1])
                assert RECOVERY_LOG not in loguru_caplog.text
                assert _searxng_rate_limiter._all_held_warned_at is not None

                # Two is the threshold, so this one is a real recovery.
                held[1].release()
                released.append(held[1])
                _searxng_rate_limiter._get_url_state(urls[2])
                assert RECOVERY_LOG in loguru_caplog.text
                assert _searxng_rate_limiter._all_held_warned_at is None
        finally:
            for lock in held:
                if lock not in released:
                    lock.release()

    def test_small_capacity_state_does_not_flap_on_a_reused_lock(
        self, monkeypatch, loguru_caplog
    ):
        """With the real re-warn interval, a lock cycling in and out of use
        must not produce a warning per cycle.

        ``ALL_HELD_REWARN_SECONDS`` is deliberately left at its production
        value here: the only way a second warning can appear within it is a
        recovery having reset the state in between, which is exactly the flap
        the hysteresis band exists to stop.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls, held = _saturate(_searxng_rate_limiter)
        try:
            with loguru_caplog.at_level("INFO"):
                for index, lock in enumerate(held):
                    # Saturated, then one holder finishes and the next caller
                    # picks the same lock up again.
                    _searxng_rate_limiter._get_url_state(urls[index])
                    lock.release()
                    _searxng_rate_limiter._get_url_state(urls[index - 1])
                    assert lock.acquire(timeout=10)
        finally:
            for lock in held:
                lock.release()

        assert loguru_caplog.text.count(SATURATION_WARNING) == 1
        assert RECOVERY_LOG not in loguru_caplog.text

    def test_warning_reports_how_many_locks_are_evictable(
        self, monkeypatch, loguru_caplog
    ):
        """Zero headroom does not mean zero evictable entries.

        ``headroom <= 0`` is ``tracked - evictable >= MAX_TRACKED_URLS``, which
        admits a non-zero ``evictable`` whenever the tracker has grown past
        capacity. Claiming that all of them are in use would be false, so the
        line reports both counts and the limit it cannot get back under.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
        monkeypatch.setattr(
            _searxng_rate_limiter, "ALL_HELD_REWARN_SECONDS", 0.0
        )

        # Six tracked entries, every one of them held: the tracker is past
        # capacity because nothing could be reclaimed on the way there.
        urls = [f"http://localhost:808{i}" for i in range(6)]
        held = []
        for url in urls:
            lock = _searxng_rate_limiter._get_url_state(url).lock
            assert lock.acquire(timeout=10)
            held.append(lock)
        try:
            # Two callers finish. Two of six are now evictable -- evicting both
            # still leaves four tracked, which is not under a capacity of four.
            held[0].release()
            held[1].release()
            with loguru_caplog.at_level("WARNING"):
                _searxng_rate_limiter._get_url_state(urls[2])
        finally:
            for lock in held[2:]:
                lock.release()

        assert (
            "only 2 of 6 tracked URL locks are evictable, not enough to "
            "bring the tracker back under its capacity of 4"
        ) in loguru_caplog.text
        assert "all 6 tracked URL locks are in use" not in loguru_caplog.text

    def test_eviction_prefers_a_timestamped_lock_over_a_brand_new_one(
        self, monkeypatch
    ):
        """A lock with no timestamp yet is brand new, not the oldest.

        ``_get_url_state`` returns a freshly created ``_UrlState`` (lock
        unlocked, ``last_request == 0.0``) *before* its caller reaches
        ``respect_rate_limit``'s ``with state.lock:`` -- ``respect_rate_limit``
        writes a real timestamp once that caller passes its delay. This test
        reproduces exactly that window by calling ``_get_url_state`` directly
        and never acquiring the lock, the same window
        ``_evict_stale_locks_unlocked`` documents as leaving an entry
        evictable. ``_eviction_sort_key`` maps that ``0.0`` to ``+inf`` so it
        sorts last; naively sorting on the raw timestamp would put brand-new
        entries at the front of the victim list, evicting the newest entries
        before ones that actually went stale.

        (This is distinct from a request whose delay is still being applied:
        that path holds ``state.lock`` for the whole sleep, so it is already
        excluded from ``evictable`` by the lock-held guard regardless of this
        sort key.)
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        settled = [f"http://localhost:808{i}" for i in range(2)]
        for url in settled:
            _searxng_rate_limiter.respect_rate_limit(url, 0.1)

        # Brand new: the locks exist, their timestamps do not yet. The
        # tracker is now exactly at capacity, so the next new URL triggers
        # eviction.
        brand_new = [f"http://localhost:809{i}" for i in range(2)]
        for url in brand_new:
            _searxng_rate_limiter._get_url_state(url)
            assert _searxng_rate_limiter._url_state[url].last_request == 0.0

        assert len(_searxng_rate_limiter._url_state) == 4

        _searxng_rate_limiter._get_url_state("http://localhost:8099")

        for url in brand_new:
            assert url in _searxng_rate_limiter._url_state
        for url in settled:
            assert url not in _searxng_rate_limiter._url_state

    def test_capacity_scan_stops_once_the_answer_cannot_change(
        self, monkeypatch
    ):
        """The evictable count runs under ``_meta_lock``, so it is bounded.

        Below the warn threshold the only question is whether *any* lock is
        free, so the scan stops at the first one instead of walking every
        tracked URL on a hot path that already serializes every other lookup.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        class _CountingLock:
            def __init__(self):
                self.checks = 0

            def locked(self):
                self.checks += 1
                return False

        locks = {f"http://localhost:808{i}": _CountingLock() for i in range(4)}
        with _searxng_rate_limiter._meta_lock:
            _searxng_rate_limiter._url_state.update(
                {
                    url: _searxng_rate_limiter._UrlState(lock=lock)
                    for url, lock in locks.items()
                }
            )
            pending = _searxng_rate_limiter._capacity_pressure_unlocked()

        assert pending is None
        assert sum(lock.checks for lock in locks.values()) == 1

    def test_the_recovery_band_scales_with_the_shipped_capacity(
        self, monkeypatch
    ):
        """At the real ``MAX_TRACKED_URLS`` the band comes from the divisor.

        The two-slot floor is what every other test in this file measures,
        because they all shrink the capacity to 4 or 20. Below twenty entries
        the divisor contributes at most one slot, so a build that dropped it
        or weakened it would look identical there while the shipped tracker
        went from a hundred slots of hysteresis to two.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 1000)

        assert _searxng_rate_limiter._recovery_headroom_slots() == 100

    def test_recovery_stays_reachable_at_a_one_entry_capacity(
        self, monkeypatch, loguru_caplog
    ):
        """The band is capped at the capacity, so it can always be met.

        Headroom can never exceed ``MAX_TRACKED_URLS`` while anything is
        tracked, so an uncapped two-slot floor at a capacity of one would make
        recovery arithmetically impossible and pin the tracker as warned for
        the life of the process.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 1)
        assert _searxng_rate_limiter._recovery_headroom_slots() == 1

        urls, held = _saturate(_searxng_rate_limiter, 1)
        try:
            with loguru_caplog.at_level("INFO"):
                _searxng_rate_limiter._get_url_state(urls[0])
                assert loguru_caplog.text.count(SATURATION_WARNING) == 1

                held[0].release()
                _searxng_rate_limiter._get_url_state(urls[0])
                assert RECOVERY_LOG in loguru_caplog.text
                assert _searxng_rate_limiter._all_held_warned_at is None
        finally:
            for lock in held:
                if lock.locked():
                    lock.release()

    def test_the_rewarn_interval_is_five_minutes(self):
        """Pin the shipped value.

        Every test that exercises re-warning patches this to ``0.0`` to keep
        itself fast, so nothing else in the suite would notice the constant
        being changed.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        assert _searxng_rate_limiter.ALL_HELD_REWARN_SECONDS == 300.0

    def test_capacity_lines_are_emitted_under_the_emit_lock(self, monkeypatch):
        """Two threads emitting at once must not interleave their lines.

        The decision is made under ``_meta_lock`` and the emission happens
        after releasing it, so two threads can be inside
        ``_emit_capacity_log`` at the same time with records from different
        transitions. ``_emit_lock`` is what keeps one line from being written
        into the middle of another and what makes the generation check a
        read-modify-write rather than a race.

        Racing threads cannot be made to interleave on demand without sleeps,
        so the deterministic half of this test is the lock itself: a recording
        stand-in reports whether it was held at the moment each line was
        written, and the events it records must stay properly nested.
        """
        import threading

        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        events = []
        events_lock = threading.Lock()

        class _RecordingLock:
            """A real lock that notes when it is taken and released."""

            def __init__(self):
                self._lock = threading.Lock()
                self.held = False

            def acquire(self, *args, **kwargs):
                acquired = self._lock.acquire(*args, **kwargs)
                if acquired:
                    self.held = True
                    with events_lock:
                        events.append("acquire")
                return acquired

            def release(self):
                with events_lock:
                    events.append("release")
                self.held = False
                self._lock.release()

            def locked(self):
                return self._lock.locked()

            def __enter__(self):
                self.acquire()
                return self

            def __exit__(self, *exc_info):
                self.release()

        emit_lock = _RecordingLock()
        monkeypatch.setattr(_searxng_rate_limiter, "_emit_lock", emit_lock)

        class _RecordingLogger:
            def _record(self, message, *args):
                with events_lock:
                    events.append(
                        ("emit", emit_lock.held, message.format(*args))
                    )

            warning = _record
            info = _record

        monkeypatch.setattr(_searxng_rate_limiter, "logger", _RecordingLogger())

        # Two warnings from consecutive transitions. Whichever thread wins,
        # the newer record must be the one that lands.
        records = [
            (True, "first {}", (1,), 1),
            (True, "second {}", (2,), 2),
        ]
        start = threading.Barrier(len(records))

        def emit(record):
            start.wait(10)
            _searxng_rate_limiter._emit_capacity_log(record)

        threads = [
            threading.Thread(target=emit, args=(record,)) for record in records
        ]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join(10)
        for thread in threads:
            assert not thread.is_alive()

        emissions = [event for event in events if isinstance(event, tuple)]
        assert emissions, events
        # Held at the moment of the write. Dropping the ``with _emit_lock:``
        # leaves this False.
        assert all(held for _, held, _ in emissions), events
        # No line written twice, and none started while another was running.
        assert len(emissions) == len({text for _, _, text in emissions}), events
        depth = 0
        for event in events:
            if event == "acquire":
                depth += 1
            elif event == "release":
                depth -= 1
            else:
                assert depth == 1, events
            assert 0 <= depth <= 1, events
        assert depth == 0, events
        assert _searxng_rate_limiter._emitted_generation == 2

    def test_a_recovery_that_overtakes_its_own_warning_is_withheld(
        self, monkeypatch, loguru_caplog
    ):
        """The mirror image: a recovery must not be the first word either.

        Thread A decides the warning and is descheduled before emitting it,
        the holders finish, and thread B decides the recovery and emits
        first. Logging that recovery would print "evictable again" with
        nothing before it to recover from, and would stamp A's warning away
        as stale on the way past -- so an episode that really happened would
        leave no warning in the log at all. Withholding the recovery leaves
        the warning to stand on its own, which reads as an episode still
        open rather than one that never occurred.
        """
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)

        urls, held = _saturate(_searxng_rate_limiter)
        try:
            with loguru_caplog.at_level("INFO"):
                # Thread A decides the warning and does not get to emit it.
                with _searxng_rate_limiter._meta_lock:
                    warning = (
                        _searxng_rate_limiter._capacity_pressure_unlocked()
                    )
                assert warning is not None and warning[0] is True

                # Every holder finishes; thread B decides the recovery.
                for lock in held:
                    lock.release()
                with _searxng_rate_limiter._meta_lock:
                    recovery = (
                        _searxng_rate_limiter._capacity_pressure_unlocked()
                    )
                assert recovery is not None and recovery[0] is False
                assert recovery[3] == warning[3] + 1

                # B emits first. Nothing may be logged yet.
                _searxng_rate_limiter._emit_capacity_log(recovery)
                assert RECOVERY_LOG not in loguru_caplog.text
                assert SATURATION_WARNING not in loguru_caplog.text

                # A wakes up. Its warning is not stale and still goes out.
                _searxng_rate_limiter._emit_capacity_log(warning)
                assert loguru_caplog.text.count(SATURATION_WARNING) == 1
                assert RECOVERY_LOG not in loguru_caplog.text
        finally:
            for lock in held:
                if lock.locked():
                    lock.release()

    def test_rate_limit_multithreaded(self):
        """Multiple threads invoking respect_rate_limit concurrently are properly serialized."""
        import concurrent.futures
        import threading
        import time
        from local_deep_research.web_search_engines.engines import (
            _searxng_rate_limiter,
        )

        delay = 0.04
        num_threads = 5
        timestamps = []
        timestamps_lock = threading.Lock()

        def worker():
            t_start = time.monotonic()
            _searxng_rate_limiter.respect_rate_limit(
                "http://localhost:8080", delay
            )
            t_end = time.monotonic()
            with timestamps_lock:
                timestamps.append((t_start, t_end))

        start_time = time.monotonic()
        with concurrent.futures.ThreadPoolExecutor(
            max_workers=num_threads
        ) as executor:
            futures = [executor.submit(worker) for _ in range(num_threads)]
            concurrent.futures.wait(futures)
        total_time = time.monotonic() - start_time

        # Expected serialized duration for 5 calls is 4 * 0.04 = 0.16s.
        # Fully broken locking (concurrent sleep) completes in ~0.04s.
        # A 0.8x threshold (0.128s) reliably detects broken locking while
        # allowing margin for minor OS scheduling jitter.
        expected_serial_delay = (num_threads - 1) * delay
        assert total_time >= expected_serial_delay * 0.8, (
            f"Expected total_time >= {expected_serial_delay * 0.8:.3f}s, got {total_time:.3f}s"
        )

    def test_rate_limit_thread_safety(self):
        """Concurrent calls to _respect_rate_limit serialize requests and enforce delays correctly with a mocked clock."""
        import concurrent.futures
        import threading
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            delay = 0.5
            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                delay_between_requests=delay,
            )

            clock_lock = threading.Lock()
            simulated_time = 1000.0

            def mock_time():
                nonlocal simulated_time
                with clock_lock:
                    return simulated_time

            def mock_sleep(seconds):
                nonlocal simulated_time
                with clock_lock:
                    simulated_time += seconds

            completion_times = []

            def worker():
                engine._respect_rate_limit()
                with clock_lock:
                    completion_times.append(simulated_time)

            with (
                patch(
                    "local_deep_research.web_search_engines.engines._searxng_rate_limiter.time.monotonic",
                    side_effect=mock_time,
                ),
                patch(
                    "local_deep_research.web_search_engines.engines._searxng_rate_limiter.time.sleep",
                    side_effect=mock_sleep,
                ),
            ):
                with concurrent.futures.ThreadPoolExecutor(
                    max_workers=5
                ) as executor:
                    futures = [executor.submit(worker) for _ in range(10)]
                    for f in futures:
                        f.result()

            sorted_times = sorted(completion_times)
            assert len(sorted_times) == 10
            assert sorted_times[0] == 1000.0
            for prev_t, curr_t in zip(sorted_times[:-1], sorted_times[1:]):
                assert curr_t - prev_t == pytest.approx(delay)


class TestGetSearchResults:
    """Tests for _get_search_results method."""

    def test_returns_empty_when_unavailable(self):
        """Return empty list when engine is unavailable."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 500
            mock_response.raise_for_status.side_effect = requests.HTTPError(
                response=mock_response
            )
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            results = engine._search_previews("test query")

            assert results == []

    def test_parses_html_results(self):
        """Parse HTML search results."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        html_content = """
        <html>
        <body>
            <article class="result">
                <h3><a href="https://example.com/page1">Result 1</a></h3>
                <p class="content">This is the first result content.</p>
            </article>
            <article class="result">
                <h3><a href="https://example.com/page2">Result 2</a></h3>
                <p class="content">This is the second result content.</p>
            </article>
        </body>
        </html>
        """

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            # First call for availability check
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            # Second call for search
            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.text = html_content
            mock_response_search.cookies = {}

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            results = engine._get_search_results("test query", max_results=1)

            assert len(results) == 1
            assert results[0]["title"] == "Result 1"
            assert results[0]["url"] == "https://example.com/page1"
            assert mock_get.call_args_list[-1].kwargs["params"]["count"] == 1

    def test_html_parsing_skips_invalid_results_and_accumulates_up_to_limit(
        self,
    ):
        """HTML parser skips invalid/stats elements and continues accumulating valid results up to limit."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        html_content = """
        <html>
        <body>
            <article class="result">
                <h3><a href="http://localhost:8080/stats?engine=google">Internal Stats</a></h3>
                <p class="content">Stats page</p>
            </article>
            <article class="result">
                <h3><a href="https://example.com/page1">Result 1</a></h3>
                <p class="content">First valid result.</p>
            </article>
            <article class="result">
                <h3><a href="https://example.com/page2">Result 2</a></h3>
                <p class="content">Second valid result.</p>
            </article>
        </body>
        </html>
        """

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.text = html_content
            mock_response_search.cookies = {}

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            results = engine._get_search_results("test query", max_results=2)

            assert len(results) == 2
            assert results[0]["url"] == "https://example.com/page1"
            assert results[1]["url"] == "https://example.com/page2"

    def test_title_preserves_internal_whitespace(self):
        """Multi-word titles keep word boundaries (issue #4970).

        ``Tag.get_text(strip=True)`` collapses every internal whitespace
        run to nothing, so titles like ``Word One Word Two Word Three``
        render as ``WordOneWordTwoWordThree``. ``get_text(" ", strip=True)``
        replaces internal runs with a single space instead.
        """
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        # Real SearXNG output often splits a multi-word title across
        # several child nodes (e.g. spans holding separate words), so
        # the parse path concatenates their text. Leading/trailing
        # whitespace inside the anchor also exercises edge stripping.
        html_content = """
        <html>
        <body>
            <article class="result">
                <h3 class="result-title">
                    <a href="https://example.com/word-one">
                        <em>Word One</em> of
                        <em>Word Two Word Three</em>
                        at ExampleSite
                    </a>
                </h3>
                <p class="content">
                    <em>A</em> <em>multi-line</em>
                    <em>snippet</em> <em>with</em>
                    <em>extra</em> spaces.
                </p>
            </article>
        </body>
        </html>
        """

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.text = html_content
            mock_response_search.cookies = {}

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            results = engine._get_search_results("word one two three")

            assert len(results) == 1
            title = results[0]["title"]
            # The whitespace-collapsed bug would produce
            # "WordOneofWordTwoWordThreeatExampleSite" with no spaces.
            assert title == (
                "Word One of Word Two Word Three at ExampleSite"
            ), f"Title should keep internal spaces, got {title!r}"
            assert "  " not in title, (
                "Internal runs must collapse to a single space"
            )
            assert title == title.strip(), "Edges must be stripped"

            content = results[0]["content"]
            assert content.startswith("A multi-line"), content
            assert content.rstrip().endswith("extra spaces."), content
            # Word boundary across the newline must survive — the
            # old ``get_text(strip=True)`` joined "multi-line" with
            # "snippet" into "multi-linesnippet".
            assert "A multi-line snippet" in content, (
                f"Cross-fragment word boundary lost: {content!r}"
            )

    @pytest.mark.parametrize(
        "max_results_override, expected_count_param, expected_result_count",
        [
            (-5, 0, 0),
            (0, 0, 0),
            (1, 1, 1),
            (2, 2, 2),
            (5, 5, 2),
            (10, 10, 2),
        ],
    )
    def test_get_search_results_max_results_edge_cases(
        self,
        max_results_override,
        expected_count_param,
        expected_result_count,
    ):
        """Test _get_search_results with 0, negative, exact, and over-limit max_results."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        html_content = """
        <html>
        <body>
            <article class="result">
                <h3><a href="http://localhost:8080/stats?engine=google">Internal Stats</a></h3>
                <p class="content">Stats page</p>
            </article>
            <article class="result">
                <h3><a href="https://example.com/page1">Result 1</a></h3>
                <p class="content">First valid result.</p>
            </article>
            <article class="result">
                <h3><a href="https://example.com/page2">Result 2</a></h3>
                <p class="content">Second valid result.</p>
            </article>
        </body>
        </html>
        """

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.text = html_content
            mock_response_search.cookies = {}

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(instance_url="http://localhost:8080")

            results = engine._get_search_results(
                "test query", max_results=max_results_override
            )

            assert len(results) == expected_result_count
            assert (
                mock_get.call_args_list[-1].kwargs["params"]["count"]
                == expected_count_param
            )

    def test_engines_param_sent_to_searxng(self):
        """Engines list is forwarded to SearXNG as the ``engines`` query param."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        html_content = """
        <html>
        <body>
            <article class="result">
                <h3><a href="https://example.com/page1">Result 1</a></h3>
                <p class="content">First valid result.</p>
            </article>
        </body>
        </html>
        """

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.text = html_content
            mock_response_search.cookies = {}

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                engines=["kagi", "brave"],
            )

            engine._get_search_results("test query")

            search_params = mock_get.call_args_list[-1].kwargs["params"]
            assert search_params["engines"] == "kagi,brave"

    def test_no_engines_param_when_unset(self):
        """When engines is empty (default), the ``engines`` key is omitted."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        html_content = """
        <html>
        <body>
            <article class="result">
                <h3><a href="https://example.com/page1">Result 1</a></h3>
                <p class="content">First valid result.</p>
            </article>
        </body>
        </html>
        """

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.text = html_content
            mock_response_search.cookies = {}

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(instance_url="http://localhost:8080")

            engine._get_search_results("test query")

            search_params = mock_get.call_args_list[-1].kwargs["params"]
            assert "engines" not in search_params

    def test_engines_accepts_json_string_from_settings(self):
        """JSON-encoded string (as saved via the settings UI) is normalized to a list.

        ``ui_element: 'json'`` settings are stored as JSON text in the DB;
        ``_normalize_list`` must parse that form so the value reaches the
        SearXNG ``engines`` query param correctly via the factory.
        """
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                engines='["kagi", "brave", "wikipedia"]',
            )

            assert engine.engines == ["kagi", "brave", "wikipedia"]

    def test_single_quoted_engines_do_not_silently_filter_nothing(self):
        """Python-style quoting must not become four junk engine names.

        ``ui_element: 'json'`` means the field is parsed with ``json.loads``,
        which rejects single quotes. ``_normalize_list`` then falls through to
        its comma-separated branch, which splits the raw text on commas and
        keeps the brackets and quotes as part of each name -- so
        ``['kagi', 'brave']`` becomes ``["['kagi'", "'brave']"]`` and SearXNG
        matches none of them. The user believes they restricted the search and
        instead every engine is queried. ``_normalize_list`` now warns when
        the input was evidently meant to be JSON -- see
        ``test_json_shaped_input_warns_before_falling_back`` -- but the
        parsed value is deliberately unchanged.

        This pins the failure mode rather than the fix: the setting's own
        description used to show the single-quoted form, so this was the
        documented path. If ``_normalize_list`` is ever taught to accept the
        Python form, or to reject unparseable input loudly, this test should
        change deliberately -- not silently.
        """
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        # The safe_get patch is required, not decorative: __init__ performs an
        # availability probe, so an unpatched construction would reach the
        # network.
        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                engines="['kagi', 'brave']",
            )

            # Not a useful engine list -- every entry carries stray punctuation.
            assert engine.engines == ["['kagi'", "'brave']"]
            assert not any(name in ("kagi", "brave") for name in engine.engines)

    def test_documented_example_parses_to_real_engine_names(self):
        """The example in the setting description must actually work.

        Guards the description itself: this is the exact string the settings
        UI and CONFIGURATION.md tell the user to paste.
        """
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                engines='["kagi", "brave", "wikipedia", "arxiv"]',
            )

            assert engine.engines == ["kagi", "brave", "wikipedia", "arxiv"]

    def test_json_shaped_input_warns_before_falling_back(self):
        """A value that tried to be JSON and failed must not fail silently.

        The comma fallback turns ``['kagi', 'brave']`` into names carrying
        brackets and quotes, which no engine matches -- so the search is
        silently unrestricted. Warning is the whole point: without it the only
        symptom is results that look untouched.
        """
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.logger"
        ) as mock_logger:
            result = SearXNGSearchEngine._normalize_list(
                "['kagi', 'brave']", setting_name="SearXNG engines"
            )

        assert result == ["['kagi'", "'brave']"]
        assert mock_logger.warning.called
        message = mock_logger.warning.call_args.args[0]
        assert "not valid JSON" in message
        assert "SearXNG engines" in mock_logger.warning.call_args.args

    @pytest.mark.parametrize(
        "value",
        [
            "kagi,brave",  # genuine comma-separated input
            "kagi",  # a single bare name
            '["kagi", "brave"]',  # valid JSON
        ],
    )
    def test_valid_input_does_not_warn(self, value):
        """Only JSON-shaped failures warn.

        Comma-separated input is a supported form, not a mistake. Warning on
        it would train operators to ignore the message, which costs more than
        the warning is worth.
        """
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.logger"
        ) as mock_logger:
            SearXNGSearchEngine._normalize_list(
                value, setting_name="SearXNG engines"
            )

        assert not mock_logger.warning.called


class TestGetPreviews:
    """Tests for _get_previews method."""

    def test_returns_empty_when_unavailable(self):
        """Return empty list when engine is unavailable."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 500
            mock_response.raise_for_status.side_effect = requests.HTTPError(
                response=mock_response
            )
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            previews = engine._search_previews("test query")

            assert previews == []

    def test_formats_previews_correctly(self):
        """Format previews with correct fields."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            # Mock _get_search_results
            with patch.object(
                engine,
                "_get_search_results",
                return_value=[
                    {
                        "title": "Test Title",
                        "url": "https://example.com",
                        "content": "Test content",
                        "engine": "google",
                        "category": "general",
                    }
                ],
            ):
                previews = engine._search_previews("test query")

                assert len(previews) == 1
                assert previews[0]["title"] == "Test Title"
                assert previews[0]["link"] == "https://example.com"
                assert previews[0]["snippet"] == "Test content"
                assert previews[0]["engine"] == "google"


class TestGetFullContent:
    """Tests for _get_full_content method."""

    def test_returns_items_when_unavailable(self):
        """Return items as-is when engine is unavailable."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 500
            mock_response.raise_for_status.side_effect = requests.HTTPError(
                response=mock_response
            )
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            items = [{"title": "Test", "link": "https://example.com"}]
            result = engine._get_full_content(items)

            assert result == items


class TestRun:
    """Tests for run method."""

    def test_run_returns_empty_when_unavailable(self):
        """Run returns empty list when engine is unavailable."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 500
            mock_response.raise_for_status.side_effect = requests.HTTPError(
                response=mock_response
            )
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            results = engine.run("test query")

            assert results == []

    def test_run_handles_exceptions(self):
        """Run handles exceptions gracefully."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            with patch.object(
                engine, "_get_previews", side_effect=Exception("Search failed")
            ):
                results = engine.run("test query")

                assert results == []


class TestResults:
    """Tests for results method."""

    def test_results_returns_empty_when_unavailable(self):
        """Results returns empty list when engine is unavailable."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 500
            mock_response.raise_for_status.side_effect = requests.HTTPError(
                response=mock_response
            )
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            results = engine.results("test query")

            assert results == []

    def test_results_formats_correctly(self):
        """Results formats output correctly."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            with patch.object(
                engine,
                "_get_search_results",
                return_value=[
                    {
                        "title": "Test",
                        "url": "https://example.com",
                        "content": "Content",
                    }
                ],
            ):
                results = engine.results("test query")

                assert len(results) == 1
                assert results[0]["title"] == "Test"
                assert results[0]["link"] == "https://example.com"
                assert results[0]["snippet"] == "Content"

    def test_results_with_custom_max_results(self):
        """Results respects custom max_results."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                max_results=10,
            )

            with patch.object(
                engine, "_get_search_results", return_value=[]
            ) as mock_search:
                engine.results("test query", max_results=5)

                mock_search.assert_called_once_with("test query", max_results=5)
                assert engine.max_results == 10

    def test_results_sequential_override_resets_to_default(self):
        """Sequential calls with and without max_results overrides use correct limits."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                max_results=10,
            )

            with patch.object(
                engine, "_get_search_results", return_value=[]
            ) as mock_search:
                engine.results("query 1", max_results=3)
                engine.results("query 2")

                assert mock_search.call_args_list == [
                    call("query 1", max_results=3),
                    call("query 2", max_results=None),
                ]
                assert engine.max_results == 10

    def test_results_concurrent_calls_do_not_cross_contaminate(self):
        """Concurrent calls with default max_results=None and overrides do not contaminate default worker limits."""
        import concurrent.futures
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_response.text = "<html></html>"
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                max_results=10,
            )

            recorded_calls = []
            original_get_search_results = engine._get_search_results

            def spy_get_search_results(query, max_results=None):
                resolved_limit = max(
                    0,
                    engine.max_results if max_results is None else max_results,
                )
                recorded_calls.append(
                    (query, max_results, resolved_limit, engine.max_results)
                )
                return original_get_search_results(
                    query, max_results=max_results
                )

            with patch.object(
                engine,
                "_get_search_results",
                side_effect=spy_get_search_results,
            ):
                with concurrent.futures.ThreadPoolExecutor(
                    max_workers=8
                ) as executor:
                    futures = []
                    for i in range(10):
                        futures.append(
                            executor.submit(
                                engine.results,
                                f"default_{i}",
                                max_results=None,
                            )
                        )
                        futures.append(
                            executor.submit(
                                engine.results,
                                f"override_{i}",
                                max_results=3 if i % 2 == 0 else 7,
                            )
                        )

                    for f in futures:
                        f.result()

                assert engine.max_results == 10

                default_calls = [
                    c for c in recorded_calls if c[0].startswith("default_")
                ]
                assert len(default_calls) == 10
                for query, param, resolved, engine_max in default_calls:
                    assert param is None
                    assert resolved == 10
                    assert engine_max == 10

                override_calls = [
                    c for c in recorded_calls if c[0].startswith("override_")
                ]
                assert len(override_calls) == 10
                for query, param, resolved, engine_max in override_calls:
                    assert param in (3, 7)
                    assert resolved == param
                    assert engine_max == 10


class TestClassAttributes:
    """Tests for class attributes."""

    def test_is_public(self):
        """SearXNGSearchEngine is marked as public."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine.is_public is True

    def test_is_generic(self):
        """SearXNGSearchEngine is marked as generic."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine.is_generic is True


class TestGetSelfHostingInstructions:
    """Tests for get_self_hosting_instructions static method."""

    def test_returns_instructions(self):
        """Returns self-hosting instructions."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        instructions = SearXNGSearchEngine.get_self_hosting_instructions()

        assert "SearXNG Self-Hosting Instructions" in instructions
        assert "docker" in instructions.lower()
        assert "8080" in instructions


class TestInvoke:
    """Tests for invoke method."""

    def test_invoke_calls_run(self):
        """Invoke calls run method."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
            )

            with patch.object(
                engine, "run", return_value=[{"title": "Result"}]
            ) as mock_run:
                result = engine.invoke("test query")

                mock_run.assert_called_once_with("test query")
                assert result == [{"title": "Result"}]


class TestNormalizeList:
    """Tests for _normalize_list static method."""

    def test_none_returns_none(self):
        """None input returns None."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine._normalize_list(None) is None

    def test_list_returned_unchanged(self):
        """Already-parsed list is returned as-is."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        original = ["general", "news"]
        assert SearXNGSearchEngine._normalize_list(original) is original

    def test_json_array_string_parsed(self):
        """JSON array string is parsed into a list."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine._normalize_list('["general", "news"]') == [
            "general",
            "news",
        ]

    def test_json_array_with_crlf_parsed(self):
        """JSON array with \\r\\n (issue #1030 exact input) is parsed correctly."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        raw = '[\r\n  "general"\r\n]'
        assert SearXNGSearchEngine._normalize_list(raw) == ["general"]

    def test_comma_separated_string_parsed(self):
        """Comma-separated string is split into a list."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine._normalize_list(
            "general, news, science"
        ) == [
            "general",
            "news",
            "science",
        ]

    def test_empty_string_returns_none(self):
        """Empty string returns None."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine._normalize_list("") is None

    def test_whitespace_only_returns_none(self):
        """Whitespace-only string returns None."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine._normalize_list("   ") is None

    def test_bare_string_returns_single_element_list(self):
        """A bare string without JSON brackets or commas becomes a single-element list."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        assert SearXNGSearchEngine._normalize_list("general") == ["general"]


class TestCategoriesNormalization:
    """Tests that categories are normalized in __init__."""

    def test_string_categories_normalized_to_list(self):
        """String categories (issue #1030) are parsed into a proper list."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                categories='[\r\n  "general"\r\n]',
            )

            assert engine.categories == ["general"]

    def test_none_categories_default_to_general(self):
        """None categories default to ['general']."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                categories=None,
            )

            assert engine.categories == ["general"]

    def test_string_engines_normalized_to_list(self):
        """String engines are parsed into a proper list."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                engines='["google", "bing"]',
            )

            assert engine.engines == ["google", "bing"]


class TestResultFormat:
    """Tests for the opt-in JSON result_format (issue #5078)."""

    @staticmethod
    def _search_params(mock_get):
        """Return the ``params`` dict of the search request safe_get call."""
        for call_item in mock_get.call_args_list:
            if "params" in call_item.kwargs:
                return call_item.kwargs["params"]
        raise AssertionError("no safe_get call carried a params kwarg")

    def test_default_format_is_html(self):
        """Without an override the engine still requests format=html."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.text = "<html><body></body></html>"
            mock_response_search.cookies = {}

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(instance_url="http://localhost:8080")
            assert engine.result_format == "html"

            engine._get_search_results("test query")
            assert self._search_params(mock_get)["format"] == "html"

    def test_json_format_requests_json_and_parses_results(self):
        """result_format='json' requests format=json and parses the JSON body.

        On the HTML-only code path a JSON document is handed to
        BeautifulSoup, which finds no result elements, so this returns an
        empty list. The JSON path returns the two structured results.
        """
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        json_body = {
            "query": "test query",
            "results": [
                {
                    "url": "https://example.com/page1",
                    "title": "Result 1",
                    "content": "First result content.",
                    "engine": "duckduckgo",
                    "category": "general",
                },
                {
                    "url": "https://example.com/page2",
                    "title": "Result 2",
                    "content": "Second result content.",
                    "engine": "brave",
                    "category": "general",
                },
            ],
            "unresponsive_engines": [],
        }

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.cookies = {}
            mock_response_search.json.return_value = json_body

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                result_format="json",
            )
            assert engine.result_format == "json"

            results = engine._get_search_results("test query")

            assert self._search_params(mock_get)["format"] == "json"
            assert len(results) == 2
            assert results[0] == {
                "title": "Result 1",
                "url": "https://example.com/page1",
                "content": "First result content.",
                "engine": "duckduckgo",
                "category": "general",
            }
            assert results[1]["url"] == "https://example.com/page2"
            assert results[1]["engine"] == "brave"

    def test_json_format_filters_internal_urls(self):
        """The JSON path rejects the same internal/stats pages the HTML path does."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        json_body = {
            "results": [
                {
                    "url": "http://localhost:8080/stats?engine=google",
                    "title": "internal stats",
                    "content": "",
                },
                {
                    "url": "https://example.com/real",
                    "title": "Real Result",
                    "content": "kept",
                },
            ],
        }

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.cookies = {}
            mock_response_search.json.return_value = json_body

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                result_format="json",
            )

            results = engine._get_search_results("test query")

            assert len(results) == 1
            assert results[0]["url"] == "https://example.com/real"

    def test_json_format_respects_per_call_max_results(self):
        """The JSON format path passes and respects per-call max_results."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        json_body = {
            "results": [
                {
                    "url": "https://example.com/page1",
                    "title": "Result 1",
                    "content": "First content",
                },
                {
                    "url": "https://example.com/page2",
                    "title": "Result 2",
                    "content": "Second content",
                },
            ],
        }

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.cookies = {}
            mock_response_search.json.return_value = json_body

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                result_format="json",
                max_results=10,
            )

            results = engine._get_search_results("test query", max_results=1)

            assert self._search_params(mock_get)["count"] == 1
            assert len(results) == 1
            assert results[0]["url"] == "https://example.com/page1"
            assert engine.max_results == 10

    @pytest.mark.parametrize(
        "max_results_override, expected_count_param, expected_result_count",
        [
            (-5, 0, 0),
            (0, 0, 0),
            (1, 1, 1),
            (2, 2, 2),
            (5, 5, 2),
            (10, 10, 2),
        ],
    )
    def test_json_format_max_results_edge_cases(
        self,
        max_results_override,
        expected_count_param,
        expected_result_count,
    ):
        """Test JSON format path with 0, negative, exact, and over-limit max_results."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        json_body = {
            "results": [
                {
                    "url": "https://example.com/page1",
                    "title": "Result 1",
                    "content": "First content",
                },
                {
                    "url": "https://example.com/page2",
                    "title": "Result 2",
                    "content": "Second content",
                },
            ],
        }

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response_init = Mock()
            mock_response_init.status_code = 200
            mock_response_init.cookies = {}

            mock_response_search = Mock()
            mock_response_search.status_code = 200
            mock_response_search.cookies = {}
            mock_response_search.json.return_value = json_body

            mock_get.side_effect = [
                mock_response_init,
                mock_response_init,
                mock_response_search,
            ]

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                result_format="json",
            )

            results = engine._get_search_results(
                "test query", max_results=max_results_override
            )

            assert (
                self._search_params(mock_get)["count"] == expected_count_param
            )
            assert len(results) == expected_result_count

    def test_invalid_format_falls_back_to_html(self):
        """An unsupported result_format is coerced to 'html' with a warning."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"
        ) as mock_get:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_get.return_value = mock_response

            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                result_format="csv",
            )

            assert engine.result_format == "html"


SEARXNG_MODULE = (
    "local_deep_research.web_search_engines.engines.search_engine_searxng"
)
INSTANCE_URL_ENV = "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL"
GATE_ENV = "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS"
ALLOWLIST_ENV = "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST"
RESULT_FETCH_ENV = "LDR_SEARCH_ALLOW_PRIVATE_RESULT_FETCH"


class TestAllowPrivateIpsDerivation:
    """The instance URL's allow_private_ips flag is DERIVED, not hard-coded.

    A public-nature SearXNG pointed at an internal host is an SSRF vector; the
    engine must NOT be permitted to reach a private instance URL unless an
    operator explicitly opted in (env gate) or provisioned the URL themselves
    (env-lock). A user-editable egress scope never grants it.
    """

    def test_public_only_default_refuses_private(self):
        """With no operator opt-in and no env-locked instance URL, private
        egress to the instance URL is withheld by default (allow_private=False)."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:8080") is False
        )

    def test_empty_snapshot_refuses_private(self):
        """No operator opt-in / env-lock present -> refuses private (False)."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:8080") is False
        )

    def test_user_scope_never_grants_private(self):
        """A user-editable egress scope must NOT grant private egress for a
        static public engine. ``policy.egress_scope`` is a self-service
        dropdown (STRICT included), so relaxing on it would be a self-service
        SSRF bypass (scope=strict + internal instance URL). Only the operator
        env gate / env-lock may permit private egress -> stays False
        regardless of scope (the resolver no longer takes a scope input)."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:8080") is False
        )

    def test_operator_gate_allows_private(self, monkeypatch):
        """The env opt-in permits private egress even under PUBLIC_ONLY."""
        monkeypatch.setenv(GATE_ENV, "true")
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:8080") is True
        )

    def test_env_locked_instance_url_allows_private(self, monkeypatch):
        """An operator-provisioned (env-locked) instance URL is trusted, so
        Docker's private container URL keeps working under PUBLIC_ONLY."""
        monkeypatch.setenv(INSTANCE_URL_ENV, "http://searxng:8080")
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:8080") is True
        )

    def test_allowlisted_origin_allows_private(self, monkeypatch):
        """An instance URL whose exact origin is in the operator allowlist is
        permitted private egress without the blanket opt-in."""
        monkeypatch.setenv(ALLOWLIST_ENV, "http://localhost:8080")
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:8080") is True
        )

    def test_non_allowlisted_origin_refuses_private(self, monkeypatch):
        """The allowlist is exact-origin: a different port does not match."""
        monkeypatch.setenv(ALLOWLIST_ENV, "http://localhost:8080")
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:9090") is False
        )
        assert (
            _resolve_searxng_allow_private_ips("http://192.168.1.5:8080")
            is False
        )

    def test_schemeless_allowlist_entry_grants_nothing(self, monkeypatch):
        """The most likely operator mistake — omitting the scheme — must not
        grant private egress at runtime either."""
        monkeypatch.setenv(ALLOWLIST_ENV, "localhost:8080")
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://localhost:8080") is False
        )

    def test_listed_metadata_origin_still_never_fetched(self, monkeypatch):
        """Listing a cloud-metadata origin flips the runtime GATE to True,
        but the downstream SSRF validation still refuses the fetch — the
        engine ends up unavailable regardless. Pins the invariant that the
        allowlist can never license a metadata destination end to end."""
        monkeypatch.setenv(ALLOWLIST_ENV, "http://169.254.169.254:80")
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
            _resolve_searxng_allow_private_ips,
        )

        assert (
            _resolve_searxng_allow_private_ips("http://169.254.169.254") is True
        )
        # No safe_get mock: validate_url blocks the metadata IP before any
        # network I/O, so this is hermetic.
        engine = SearXNGSearchEngine(instance_url="http://169.254.169.254")
        assert engine._is_available is False
        # The remediation hint must NOT fire — the env flags would not help.
        assert engine._private_url_blocked_by_gate() is False

    def test_hint_silent_on_allowlisted_transport_failure(self, monkeypatch):
        """An allowlisted instance that is merely DOWN (transport error) must
        not surface the private-URL remediation hint — the gate is open; the
        problem is the instance, not the policy."""
        monkeypatch.setenv(ALLOWLIST_ENV, "http://127.0.0.1:8080")
        import requests
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with (
            patch(f"{SEARXNG_MODULE}.safe_get") as mock_get,
            patch(f"{SEARXNG_MODULE}.logger") as mock_logger,
        ):
            mock_get.side_effect = requests.RequestException(
                "connection refused"
            )
            engine = SearXNGSearchEngine(instance_url="http://127.0.0.1:8080")

        assert engine._is_available is False
        assert engine._allow_private_ips is True
        assert engine._private_url_blocked_by_gate() is False
        hints = [
            c.args[0]
            for c in (
                mock_logger.error.call_args_list
                + mock_logger.warning.call_args_list
            )
            if c.args
        ]
        assert not any(GATE_ENV in msg for msg in hints)


class TestAllowPrivateResultFetchDerivation:
    """The private RESULT-URL fetch grant is a separate, narrower opt-in:
    it needs the env-only ``LDR_SEARCH_ALLOW_PRIVATE_RESULT_FETCH`` flag AND
    the instance approval, never either one alone (issue #2477).

    The instance approval is satisfied by every env-locked (compose)
    deployment, and a public engine proxies the public web, so the instance
    grant alone must not extend to whatever private URLs the index returns.
    """

    @staticmethod
    def _resolve(url="http://localhost:8080"):
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            resolve_searxng_allow_private_result_fetch,
        )

        return resolve_searxng_allow_private_result_fetch(url)

    def test_default_refuses_private_result_fetch(self):
        assert self._resolve() is False

    def test_opt_in_alone_grants_nothing(self, monkeypatch):
        """Without any instance approval the engine may not even reach its
        own private instance; the result-fetch opt-in cannot open more."""
        monkeypatch.setenv(RESULT_FETCH_ENV, "true")
        assert self._resolve() is False

    def test_blanket_instance_gate_alone_grants_nothing(self, monkeypatch):
        monkeypatch.setenv(GATE_ENV, "true")
        assert self._resolve() is False

    def test_env_locked_instance_alone_grants_nothing(self, monkeypatch):
        """The bundled docker-compose case: an env-locked instance URL
        approves the instance itself, not private result URLs."""
        monkeypatch.setenv(INSTANCE_URL_ENV, "http://searxng:8080")
        assert self._resolve() is False

    def test_opt_in_with_blanket_instance_gate_grants(self, monkeypatch):
        monkeypatch.setenv(GATE_ENV, "true")
        monkeypatch.setenv(RESULT_FETCH_ENV, "true")
        assert self._resolve() is True

    def test_opt_in_with_env_locked_instance_grants(self, monkeypatch):
        monkeypatch.setenv(INSTANCE_URL_ENV, "http://searxng:8080")
        monkeypatch.setenv(RESULT_FETCH_ENV, "true")
        assert self._resolve() is True

    def test_opt_in_with_allowlisted_origin_grants_for_that_origin(
        self, monkeypatch
    ):
        monkeypatch.setenv(ALLOWLIST_ENV, "http://localhost:8080")
        monkeypatch.setenv(RESULT_FETCH_ENV, "true")
        assert self._resolve("http://localhost:8080") is True
        assert self._resolve("http://localhost:9090") is False

    def test_opt_in_false_value_refuses(self, monkeypatch):
        monkeypatch.setenv(GATE_ENV, "true")
        monkeypatch.setenv(RESULT_FETCH_ENV, "false")
        assert self._resolve() is False


class TestEngineForwardsDerivedAllowPrivateIps:
    """The engine forwards the DERIVED flag to safe_get (not a hard-coded True)."""

    def test_probe_uses_false_under_public_only(self):
        """Default PUBLIC_ONLY posture -> the availability probe is made with
        allow_private_ips=False."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(f"{SEARXNG_MODULE}.safe_get") as mock_get:
            mock_get.return_value = Mock(status_code=200)
            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                settings_snapshot={"search.tool": "searxng"},
            )

        assert engine._allow_private_ips is False
        assert mock_get.call_args.kwargs["allow_private_ips"] is False

    def test_probe_uses_true_with_operator_gate(self, monkeypatch):
        """Operator opt-in -> probe made with allow_private_ips=True."""
        monkeypatch.setenv(GATE_ENV, "true")
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(f"{SEARXNG_MODULE}.safe_get") as mock_get:
            mock_get.return_value = Mock(status_code=200)
            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                settings_snapshot={"search.tool": "searxng"},
            )

        assert engine._allow_private_ips is True
        assert mock_get.call_args.kwargs["allow_private_ips"] is True

    def test_search_forwards_derived_flag(self):
        """The search request path forwards the same derived flag."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(f"{SEARXNG_MODULE}.safe_get") as mock_get:
            mock_get.return_value = Mock(
                status_code=200, text="<html></html>", cookies=None
            )
            engine = SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                settings_snapshot={"search.tool": "searxng"},
            )
            engine._is_available = True
            engine.search_url = "http://localhost:8080/search"
            mock_get.reset_mock()
            engine._get_search_results("hello")

        # Every safe_get in the search path used the derived flag (False here).
        assert mock_get.call_count >= 1
        for c in mock_get.call_args_list:
            assert c.kwargs.get("allow_private_ips") is False


class TestPrivateUrlBlockedHint:
    """The engine surfaces the operator opt-in when it disables itself solely
    because the instance URL is a gated private/loopback address (FIX 2)."""

    @staticmethod
    def _make_engine(instance_url):
        """Build an engine with a mocked (always-OK) probe, so
        ``_private_url_blocked_by_gate`` can be exercised in isolation."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with patch(f"{SEARXNG_MODULE}.safe_get") as mock_get:
            mock_get.return_value = Mock(status_code=200)
            return SearXNGSearchEngine(instance_url=instance_url)

    def test_loopback_url_is_flagged(self):
        """A loopback instance URL with the gate OFF is flagged."""
        engine = self._make_engine("http://127.0.0.1:8080")
        assert engine._allow_private_ips is False
        assert engine._private_url_blocked_by_gate() is True

    def test_public_url_is_not_flagged(self):
        """A genuinely-public instance URL is never flagged."""
        engine = self._make_engine("http://8.8.8.8:8080")
        assert engine._private_url_blocked_by_gate() is False

    def test_metadata_url_is_not_flagged(self):
        """Cloud-metadata addresses stay blocked even with the opt-in, so the
        hint (which points at that opt-in) must NOT be offered for them."""
        engine = self._make_engine("http://169.254.169.254:8080")
        assert engine._private_url_blocked_by_gate() is False

    def test_gate_on_is_not_flagged(self, monkeypatch):
        """With the operator opt-in ON, private egress is allowed, so there is
        nothing to remediate."""
        monkeypatch.setenv(GATE_ENV, "true")
        engine = self._make_engine("http://127.0.0.1:8080")
        assert engine._allow_private_ips is True
        assert engine._private_url_blocked_by_gate() is False

    @pytest.mark.parametrize(
        "instance_url",
        [
            "http://localhost:8080",
            "http://127.0.0.1:8080",
            "http://[::1]:8080",
            "http://192.168.1.10:8080",
        ],
    )
    def test_blocked_probe_warns_once_and_skips_searches(
        self, monkeypatch, instance_url
    ):
        """Unapproved local instances warn without a traceback or requests."""
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        for env in (GATE_ENV, ALLOWLIST_ENV, INSTANCE_URL_ENV):
            monkeypatch.delenv(env, raising=False)
        with (
            patch("requests.sessions.Session.send") as send,
            patch(f"{SEARXNG_MODULE}.logger") as mock_logger,
        ):
            engine = SearXNGSearchEngine(
                instance_url=instance_url,
                settings_snapshot={},
                programmatic_mode=True,
            )
            failure = engine.last_search_failure
            assert engine.run("first query") == []
            assert engine.run("second query") == []
            assert engine._search_previews("query") == []
            with pytest.raises(RuntimeError):
                engine._get_search_results("query")
            engine.close()

        assert engine._is_available is False
        assert failure is not None
        assert engine.last_search_failure == failure
        assert failure.cooldown_seconds is None
        send.assert_not_called()
        mock_logger.error.assert_not_called()
        mock_logger.exception.assert_not_called()
        mock_logger.warning.assert_called_once()
        warning = mock_logger.warning.call_args.args[0]
        assert "rest of this research" in warning
        assert GATE_ENV in warning
        assert ALLOWLIST_ENV in warning
        assert INSTANCE_URL_ENV in warning

    def test_public_probe_failure_has_no_remediation_error(self):
        """A transport failure against a public URL must NOT emit the private-URL
        remediation hint (precise detection, no spam)."""
        import requests
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        with (
            patch(f"{SEARXNG_MODULE}.safe_get") as mock_get,
            patch(f"{SEARXNG_MODULE}.logger") as mock_logger,
        ):
            mock_get.side_effect = requests.RequestException(
                "connection refused"
            )
            engine = SearXNGSearchEngine(instance_url="http://8.8.8.8:8080")

        assert engine._is_available is False
        mock_logger.exception.assert_called_once()
        hints = [
            c.args[0]
            for c in (
                mock_logger.error.call_args_list
                + mock_logger.warning.call_args_list
            )
            if c.args
        ]
        assert not any(GATE_ENV in msg for msg in hints), (
            f"unexpected private-URL hint on a public failure: {hints}"
        )


class TestFullSearchPrivateIpGate:
    """The factory's full-search wrapper, the only full-content path, gets
    the engine's resolved RESULT-URL fetch grant (instance approval AND the
    env-only ``LDR_SEARCH_ALLOW_PRIVATE_RESULT_FETCH`` opt-in) as its
    ``allow_private_ips``, never a hardcoded value and never the instance
    gate alone (issue #2477)."""

    FULL_SEARCH_MODULE = (
        "local_deep_research.web_search_engines.engines.full_search"
    )
    # What search_config() injects for searxng from the engine registry.
    WRAPPER_CONFIG = {
        "full_search_module": ".engines.full_search",
        "full_search_class": "FullSearchResults",
    }

    def _make_engine(self, monkeypatch, *, instance_gate, result_fetch):
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )

        for env, enabled in (
            (GATE_ENV, instance_gate),
            (RESULT_FETCH_ENV, result_fetch),
        ):
            if enabled:
                monkeypatch.setenv(env, "true")
            else:
                monkeypatch.delenv(env, raising=False)

        with patch(f"{SEARXNG_MODULE}.safe_get") as mock_get:
            mock_get.return_value = Mock(status_code=200)
            return SearXNGSearchEngine(
                instance_url="http://localhost:8080",
                llm=Mock(),
            )

    def _wrap(self, engine):
        """Build the wrapper the way ``create_search_engine`` does once full
        content is on: ``_create_full_search_wrapper`` with the registry's
        module/class for searxng and a run scope that permits private URLs
        through the downloader, including redirect hops."""
        from local_deep_research.web_search_engines.engines.full_search import (
            FullSearchResults,
        )
        from local_deep_research.web_search_engines.search_engine_factory import (
            _create_full_search_wrapper,
        )

        wrapper = _create_full_search_wrapper(
            "searxng",
            engine,
            dict(self.WRAPPER_CONFIG),
            engine.llm,
            {},
            egress_context=EgressContext(
                scope=EgressScope.UNPROTECTED,
                primary_engine="searxng",
                require_local_llm=False,
                require_local_embeddings=False,
            ),
        )
        assert isinstance(wrapper, FullSearchResults)
        assert wrapper.web_search is engine
        return wrapper

    def _run_full_search(self, wrapper, link):
        """Run the wrapper on one result URL with the REAL ``validate_url``;
        return the results and the mocked downloader."""
        wrapper.web_search = Mock()
        wrapper.web_search.invoke.return_value = [
            {"link": link, "title": "Result"},
        ]
        with (
            patch(f"{self.FULL_SEARCH_MODULE}.QUALITY_CHECK_DDG_URLS", False),
            patch(
                f"{self.FULL_SEARCH_MODULE}.batch_fetch_and_extract",
                return_value={link: "content"},
            ) as mock_batch,
        ):
            results = wrapper.run("test query")
        return results, mock_batch

    def test_gated_instance_with_opt_in_propagates_private_result_fetch(
        self, monkeypatch
    ):
        engine = self._make_engine(
            monkeypatch, instance_gate=True, result_fetch=True
        )
        assert engine._allow_private_ips is True
        assert engine.allow_private_result_fetch is True
        assert self._wrap(engine).allow_private_ips is True

    def test_env_locked_instance_without_opt_in_keeps_result_fetch_strict(
        self, monkeypatch
    ):
        """NEGATIVE (the bundled docker-compose case): an env-locked
        instance URL approves the instance itself but must NOT extend to
        fetching private result URLs without the explicit opt-in."""
        monkeypatch.setenv(INSTANCE_URL_ENV, "http://searxng:8080")
        engine = self._make_engine(
            monkeypatch, instance_gate=False, result_fetch=False
        )
        assert engine._allow_private_ips is True
        assert engine.allow_private_result_fetch is False
        assert self._wrap(engine).allow_private_ips is False

    def test_opt_in_without_instance_gate_grants_nothing(self, monkeypatch):
        """NEGATIVE: the opt-in alone (ungated / public instance) opens
        nothing."""
        engine = self._make_engine(
            monkeypatch, instance_gate=False, result_fetch=True
        )
        assert engine._allow_private_ips is False
        assert engine.allow_private_result_fetch is False
        assert self._wrap(engine).allow_private_ips is False

    def test_ungated_instance_does_not_get_private_ip_trust(self, monkeypatch):
        """NEGATIVE: a default/public (ungated) instance must NOT have its
        full-content fetches trusted with private IPs."""
        engine = self._make_engine(
            monkeypatch, instance_gate=False, result_fetch=False
        )
        assert engine.allow_private_result_fetch is False
        assert self._wrap(engine).allow_private_ips is False

    def test_engine_holds_the_grant_but_does_not_self_wrap(self, monkeypatch):
        """The engine only resolves the grant; it builds no ``full_search``
        of its own and ``_get_full_content`` stays a pass-through, so the
        factory wrapper is the sole fetcher the grant can reach."""
        engine = self._make_engine(
            monkeypatch, instance_gate=True, result_fetch=True
        )
        assert not hasattr(engine, "full_search")
        items = [{"title": "T", "link": "http://192.168.1.5/wiki"}]
        assert engine._get_full_content(items) is items

    def test_ungated_instance_full_search_blocks_private_url(self, monkeypatch):
        """NEGATIVE end-to-end: with the grant closed, the real
        ``validate_url`` still rejects a private result URL, so the
        downloader is never invoked for it."""
        engine = self._make_engine(
            monkeypatch, instance_gate=False, result_fetch=False
        )
        results, mock_batch = self._run_full_search(
            self._wrap(engine), "http://192.168.1.5/wiki"
        )
        mock_batch.assert_not_called()
        assert results[0]["full_content"] is None

    def test_env_locked_instance_full_search_blocks_private_url(
        self, monkeypatch
    ):
        """NEGATIVE end-to-end for the compose case: instance approved via
        the env-lock, no opt-in, so a private result URL is still rejected
        by the real ``validate_url``."""
        monkeypatch.setenv(INSTANCE_URL_ENV, "http://searxng:8080")
        engine = self._make_engine(
            monkeypatch, instance_gate=False, result_fetch=False
        )
        assert engine._allow_private_ips is True
        results, mock_batch = self._run_full_search(
            self._wrap(engine), "http://192.168.1.5/wiki"
        )
        mock_batch.assert_not_called()
        assert results[0]["full_content"] is None

    def test_granted_instance_full_search_fetches_private_url(
        self, monkeypatch
    ):
        """POSITIVE end-to-end against the real ``validate_url`` (the
        wrapper has an UNPROTECTED run scope): with both approvals an RFC1918 result
        URL reaches the downloader, with the link-local carve-out riding
        along."""
        engine = self._make_engine(
            monkeypatch, instance_gate=True, result_fetch=True
        )
        results, mock_batch = self._run_full_search(
            self._wrap(engine), "http://192.168.1.5/wiki"
        )
        mock_batch.assert_called_once()
        assert mock_batch.call_args.args[0] == ["http://192.168.1.5/wiki"]
        assert mock_batch.call_args.kwargs.get("allow_private_ips") is True
        assert mock_batch.call_args.kwargs.get("block_link_local") is True
        assert results[0]["full_content"] == "content"

    def test_gated_instance_still_blocks_cloud_metadata_url(self, monkeypatch):
        """Even with both approvals, the ``ALWAYS_BLOCKED_METADATA_IPS``
        literals stay hard-blocked."""
        engine = self._make_engine(
            monkeypatch, instance_gate=True, result_fetch=True
        )
        results, mock_batch = self._run_full_search(
            self._wrap(engine), "http://169.254.169.254/latest/meta-data"
        )
        mock_batch.assert_not_called()
        assert results[0]["full_content"] is None

    def test_gated_instance_still_blocks_link_local_url(self, monkeypatch):
        """Even with both approvals, the rest of 169.254.0.0/16 (provider
        metadata outside the always-blocked literals, e.g. 169.254.10.5)
        stays blocked via ``block_link_local=True``."""
        engine = self._make_engine(
            monkeypatch, instance_gate=True, result_fetch=True
        )
        results, mock_batch = self._run_full_search(
            self._wrap(engine), "http://169.254.10.5/latest/meta-data"
        )
        mock_batch.assert_not_called()
        assert results[0]["full_content"] is None

    def test_open_grant_logs_redacted_instance(
        self, monkeypatch, loguru_caplog
    ):
        """An open grant is an allow, so it leaves one INFO line naming the
        opt-in and the redacted instance origin."""
        with loguru_caplog.at_level("INFO"):
            self._make_engine(
                monkeypatch, instance_gate=True, result_fetch=True
            )
        assert RESULT_FETCH_ENV in loguru_caplog.text
        assert "http://localhost:8080" in loguru_caplog.text

    def test_closed_grant_logs_nothing(self, monkeypatch, loguru_caplog):
        """Instance approved but no opt-in: no grant line."""
        with loguru_caplog.at_level("INFO"):
            self._make_engine(
                monkeypatch, instance_gate=True, result_fetch=False
            )
        assert RESULT_FETCH_ENV not in loguru_caplog.text


class TestFullSearchPrivateIpGateFactoryPath:
    """End to end through ``get_search``: with snippets-only off the factory
    wraps the real engine in ``FullSearchResults`` and the grant travels
    with it (issue #2477, on the path #6492 made the only one)."""

    def _get_search(self, monkeypatch, *, instance_gate, result_fetch):
        from local_deep_research.web_search_engines.engines.full_search import (
            FullSearchResults,
        )
        from local_deep_research.web_search_engines.engines.search_engine_searxng import (
            SearXNGSearchEngine,
        )
        from local_deep_research.web_search_engines.search_engine_factory import (
            get_search,
        )

        for env, enabled in (
            (GATE_ENV, instance_gate),
            (RESULT_FETCH_ENV, result_fetch),
        ):
            if enabled:
                monkeypatch.setenv(env, "true")
            else:
                monkeypatch.delenv(env, raising=False)

        # The same snapshot shape as the factory's own wrapper end-to-end
        # tests: search_config() registers an engine only when the snapshot
        # carries a key for it.
        settings_snapshot = {
            "search.tool": {"value": "searxng"},
            "search.engine.web.searxng.supports_full_search": {
                "value": True,
                "ui_element": "checkbox",
            },
        }
        with patch(f"{SEARXNG_MODULE}.safe_get") as mock_get:
            mock_get.return_value = Mock(status_code=200)
            wrapper = get_search(
                search_tool="searxng",
                llm_instance=Mock(),
                search_snippets_only=False,
                settings_snapshot=settings_snapshot,
                programmatic_mode=True,
            )
        assert isinstance(wrapper, FullSearchResults)
        assert isinstance(wrapper.web_search, SearXNGSearchEngine)
        return wrapper

    def test_gated_instance_with_opt_in_reaches_the_wrapper(self, monkeypatch):
        wrapper = self._get_search(
            monkeypatch, instance_gate=True, result_fetch=True
        )
        assert wrapper.web_search.allow_private_result_fetch is True
        assert wrapper.allow_private_ips is True

    def test_env_locked_instance_without_opt_in_stays_strict(self, monkeypatch):
        """NEGATIVE (compose case) on the production path."""
        monkeypatch.setenv(INSTANCE_URL_ENV, "http://searxng:8080")
        wrapper = self._get_search(
            monkeypatch, instance_gate=False, result_fetch=False
        )
        assert wrapper.web_search._allow_private_ips is True
        assert wrapper.allow_private_ips is False

    def test_wrapper_uses_the_run_egress_context(self, monkeypatch):
        """Default ADAPTIVE resolves SearXNG to PUBLIC_ONLY, so its private
        result-fetch grant cannot fetch a private result URL for this run."""
        wrapper = self._get_search(
            monkeypatch, instance_gate=True, result_fetch=True
        )
        assert wrapper.egress_context.scope is EgressScope.PUBLIC_ONLY
        assert wrapper._effective_allow_private_ips() is False

        private_url = "http://192.168.1.5/wiki"
        wrapper.web_search = Mock()
        wrapper.web_search.invoke.return_value = [
            {"link": private_url, "title": "Private result"}
        ]
        with (
            patch(
                "local_deep_research.web_search_engines.engines.full_search.QUALITY_CHECK_DDG_URLS",
                False,
            ),
            patch(
                "local_deep_research.web_search_engines.engines.full_search.batch_fetch_and_extract"
            ) as mock_batch,
        ):
            results = wrapper.run("test query")
        mock_batch.assert_not_called()
        assert results[0]["full_content"] is None