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.

23.4 KB
Raw Blame
"""Provider errors must survive engines that return [] instead of raising."""

from unittest.mock import Mock, patch

import pytest
import requests

from local_deep_research.advanced_search_system.tools.search_availability import (
    run_search,
)
from local_deep_research.web_search_engines.engine_availability import (
    SearchEngineAvailability,
)
from local_deep_research.web_search_engines.engines.search_engine_searxng import (
    SearXNGSearchEngine,
)
from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
    SemanticScholarSearchEngine,
)
from local_deep_research.web_search_engines.engines.search_engine_serper import (
    SerperSearchEngine,
)


SEARX_GET = "local_deep_research.web_search_engines.engines.search_engine_searxng.safe_get"


def response(status=200, body=b""):
    result = requests.Response()
    result.status_code = status
    result._content = body
    return result


def searxng(**kwargs):
    return SearXNGSearchEngine(
        instance_url="http://localhost:8080",
        settings_snapshot={},
        programmatic_mode=True,
        delay_between_requests=0,
        **kwargs,
    )


@pytest.mark.parametrize("full_content", [False, True])
def test_blocked_localhost_is_initialized_only_once(monkeypatch, full_content):
    from local_deep_research.web_search_engines.engines.full_search import (
        FullSearchResults,
    )

    for env in (
        "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS",
        "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST",
        "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL",
    ):
        monkeypatch.delenv(env, raising=False)

    def make_engine():
        engine = searxng()
        if full_content:
            return FullSearchResults(llm=None, web_search=engine)
        return engine

    factory = Mock(side_effect=make_engine)
    on_change = Mock()
    tracker = SearchEngineAvailability(on_change)
    with (
        patch(
            SEARX_GET, side_effect=ValueError("URL failed security validation")
        ) as get,
        patch(
            "local_deep_research.web_search_engines.engines.search_engine_searxng.logger"
        ) as engine_logger,
    ):
        first = run_search("searxng", "q1", factory, tracker)
        second = run_search("searxng", "q2", factory, tracker)
    get.assert_called_once()
    factory.assert_called_once()
    engine_logger.warning.assert_called_once()
    engine_logger.error.assert_not_called()
    engine_logger.exception.assert_not_called()
    assert "private/loopback" in first
    assert "rest of this research" in second
    assert tracker.disabled()["searxng"].retry_at is None
    on_change.assert_called_once()
    event = on_change.call_args.args[2]
    assert event["type"] == "warning"
    assert event["status"] == "disabled"
    assert event["retry_at"] is None
    assert SearchEngineAvailability().state("searxng") is None


@pytest.mark.parametrize("during_initialization", [True, False])
def test_searxng_http_429_preserves_retry_after(during_initialization):
    limited = response(429)
    limited.headers["Retry-After"] = "720"
    replies = (
        [limited]
        if during_initialization
        else [response(), response(), limited]
    )
    with patch(SEARX_GET, side_effect=replies):
        engine = searxng()
        if not during_initialization:
            assert engine.run("query") == []
    assert engine.last_search_failure.cooldown_seconds == 720
    assert engine.last_search_failure.reason == "Rate limit reached"


@pytest.mark.parametrize(
    "body,result_count",
    [
        (b'{"results": []}', 0),
        # SearXNG routinely lists suspended backends; the JSON does not say
        # which backends were queried, so this is an empty search, not a
        # failure of every backend.
        (
            b'{"results": [], "unresponsive_engines": [["google", "rate limited"]]}',
            0,
        ),
        (
            b'{"results": [{"url": "https://example.org", "title": "Result"}], "unresponsive_engines": [["google", "rate limited"]]}',
            1,
        ),
    ],
)
def test_searxng_json_unresponsive_backends_do_not_disable_engine(
    body, result_count
):
    with patch(
        SEARX_GET, side_effect=[response(), response(), response(body=body)]
    ):
        engine = searxng(result_format="json")
        results = engine.run("query")
    assert len(results) == result_count
    assert engine.last_search_failure is None


def test_searxng_html_unresponsive_backend_is_an_empty_search():
    html = b'<article class="result"><h3><a href="/stats?engine=google">Backend failed</a></h3></article>'
    with patch(
        SEARX_GET, side_effect=[response(), response(), response(body=html)]
    ):
        engine = searxng()
        assert engine.run("query") == []
    assert engine.last_search_failure is None


def test_unresolvable_instance_host_gets_a_finite_cooldown(monkeypatch):
    """A DNS failure fails SSRF validation but must not disable for the run."""
    import socket
    from types import SimpleNamespace

    from local_deep_research.security import ssrf_validator

    for env in (
        "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS",
        "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST",
        "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL",
    ):
        monkeypatch.delenv(env, raising=False)

    def unresolvable(*args, **kwargs):
        raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")

    # The real safe_get runs and refuses at validation, before any request.
    monkeypatch.setattr(ssrf_validator.socket, "getaddrinfo", unresolvable)
    clock = SimpleNamespace(now=0.0)
    tracker = SearchEngineAvailability(clock=lambda: clock.now)
    factory = Mock(
        side_effect=lambda: SearXNGSearchEngine(
            instance_url="https://searx.example.org",
            settings_snapshot={},
            programmatic_mode=True,
            delay_between_requests=0,
        )
    )
    first = run_search("searxng", "q", factory, tracker)
    assert "security validation" in first
    assert "rest of this research" not in first
    assert tracker.disabled()["searxng"].retry_at is not None
    clock.now = 300
    run_search("searxng", "q", factory, tracker)
    assert factory.call_count == 2


@pytest.mark.parametrize("outer", [True, False])
def test_wikipedia_non_json_response_is_classified_as_rate_limit(outer):
    from local_deep_research.web_search_engines.engines.search_engine_wikipedia import (
        WikipediaSearchEngine,
    )

    module = (
        "local_deep_research.web_search_engines.engines.search_engine_wikipedia"
    )
    decode_error = requests.exceptions.JSONDecodeError("Expecting value", "", 0)
    engine = WikipediaSearchEngine(settings_snapshot={}, llm=Mock())
    engine.rate_tracker = Mock(enabled=False)
    engine.rate_tracker.apply_rate_limit.return_value = 0.0
    with (
        patch(
            f"{module}.wikipedia.search",
            side_effect=decode_error if outer else None,
            return_value=["Title"],
        ),
        patch(f"{module}.wikipedia.summary", side_effect=decode_error),
    ):
        assert engine._search_previews("query") == []
    assert engine.last_search_failure.reason.startswith("Rate limit")
    assert engine.last_search_failure.cooldown_seconds == 300
    engine.close()


def test_semantic_scholar_mounted_retry_exhausted_on_429_is_a_rate_limit(
    monkeypatch,
):
    """Production path: the engine's mounted Retry turns 429s into RetryError.

    session.get is NOT patched: the engine's own SafeSession, HTTPAdapter
    and Retry(status_forcelist=[429, ...]) run; only DNS and urllib3's
    per-attempt request are stubbed, so nothing leaves the process.
    """
    import socket

    import urllib3
    from urllib3.connectionpool import HTTPConnectionPool

    def public_address(host, *args, **kwargs):
        return [
            (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))
        ]

    attempts = []

    def respond(self, conn, method, url, **kwargs):
        attempts.append(url)
        return urllib3.HTTPResponse(
            body=b"", status=429, headers={}, preload_content=False
        )

    from local_deep_research.security import dns_pinning

    # SSRF validation resolves via socket.getaddrinfo; connect-time pinning
    # uses the resolver dns_pinning captured at import. Stub both.
    monkeypatch.setattr(socket, "getaddrinfo", public_address)
    monkeypatch.setattr(dns_pinning, "_real_getaddrinfo", public_address)
    monkeypatch.setattr(HTTPConnectionPool, "_make_request", respond)
    engine = SemanticScholarSearchEngine(
        llm=Mock(), settings_snapshot={}, retry_backoff_factor=0
    )
    monkeypatch.setattr(engine, "_respect_rate_limit", lambda: None)
    engine.programmatic_mode = True
    engine.rate_tracker = Mock(enabled=False)
    engine.rate_tracker.apply_rate_limit.return_value = 0.0
    with patch.object(engine, "_get_previews", wraps=engine._direct_search):
        assert engine.run("query") == []
    assert len(attempts) == engine.max_retries + 1
    assert engine.last_search_failure.reason == "Rate limit reached"
    assert engine.last_search_failure.cooldown_seconds == 300
    engine.close()


def test_semantic_scholar_retry_after_wait_is_bounded(monkeypatch):
    """A six-hour Retry-After must not park the agent's tool call for hours."""
    import socket

    import urllib3
    from urllib3.connectionpool import HTTPConnectionPool
    from urllib3.util import retry as urllib3_retry

    from local_deep_research.security import dns_pinning

    def public_address(host, *args, **kwargs):
        return [
            (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))
        ]

    def respond(self, conn, method, url, **kwargs):
        return urllib3.HTTPResponse(
            body=b"",
            status=429,
            headers={"Retry-After": "21600"},
            preload_content=False,
        )

    sleeps = []
    monkeypatch.setattr(socket, "getaddrinfo", public_address)
    monkeypatch.setattr(dns_pinning, "_real_getaddrinfo", public_address)
    monkeypatch.setattr(HTTPConnectionPool, "_make_request", respond)
    monkeypatch.setattr(urllib3_retry.time, "sleep", sleeps.append)
    engine = SemanticScholarSearchEngine(llm=Mock(), settings_snapshot={})
    monkeypatch.setattr(engine, "_respect_rate_limit", lambda: None)
    engine.programmatic_mode = True
    engine.rate_tracker = Mock(enabled=False)
    engine.rate_tracker.apply_rate_limit.return_value = 0.0
    with patch.object(engine, "_get_previews", wraps=engine._direct_search):
        assert engine.run("query") == []
    assert len(sleeps) == engine.max_retries
    assert max(sleeps) <= 30
    assert engine.last_search_failure.reason == "Rate limit reached"
    engine.close()


def test_semantic_scholar_swallowed_rate_limit_keeps_signal():
    engine = SemanticScholarSearchEngine(
        llm=Mock(), settings_snapshot={}, programmatic_mode=True
    )
    limited = response(429)
    limited.headers["Retry-After"] = "120"
    with (
        patch.object(engine, "_respect_rate_limit"),
        patch.object(engine.session, "get", return_value=limited),
    ):
        engine.programmatic_mode = True
        engine.rate_tracker = Mock(enabled=False)
        engine.rate_tracker.apply_rate_limit.return_value = 0.0
        with patch.object(engine, "_get_previews", wraps=engine._direct_search):
            assert engine.run("query") == []
    assert engine.last_search_failure.cooldown_seconds == 120
    engine.close()


@pytest.mark.parametrize(
    "api_key,run_long", [("invalid-key", True), (None, False)]
)
def test_semantic_scholar_bare_403_with_api_key_is_denied_credentials(
    api_key, run_long
):
    """S2 answers a bad x-api-key with 403 {"message":"Forbidden"} only."""
    engine = SemanticScholarSearchEngine(
        llm=Mock(),
        settings_snapshot={},
        programmatic_mode=True,
        api_key=api_key,
    )
    forbidden = response(403, b'{"message":"Forbidden"}')
    with (
        patch.object(engine, "_respect_rate_limit"),
        patch.object(engine.session, "get", return_value=forbidden),
    ):
        engine.programmatic_mode = True
        engine.rate_tracker = Mock(enabled=False)
        engine.rate_tracker.apply_rate_limit.return_value = 0.0
        with patch.object(engine, "_get_previews", wraps=engine._direct_search):
            assert engine.run("query") == []
    failure = engine.last_search_failure
    if run_long:
        assert failure.cooldown_seconds is None
        assert "check credentials" in failure.reason
    else:
        # Without a key the 403 cannot be about the key: a block.
        assert failure.cooldown_seconds == 300
    engine.close()


def test_bare_403_stays_a_block_for_engines_that_did_not_opt_in():
    engine = SerperSearchEngine(api_key="test-api-key", settings_snapshot={})
    assert engine.bare_403_means_bad_api_key is False
    engine._record_search_failure(
        requests.HTTPError(response=response(403, b'{"message":"Forbidden"}'))
    )
    assert engine.last_search_failure.cooldown_seconds == 300
    engine.close()


def test_serper_retry_after_survives_base_error_handling():
    engine = SerperSearchEngine(api_key="test-api-key", settings_snapshot={})
    engine.programmatic_mode = True
    engine.rate_tracker = Mock(enabled=False)
    limited = response(429)
    limited.headers["Retry-After"] = "240"
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_serper.safe_post",
        return_value=limited,
    ):
        assert engine.run("query") == []
    assert engine.last_search_failure.cooldown_seconds == 240
    engine.close()


@pytest.mark.parametrize(
    "module_name,class_name,method,args,request_name,error",
    [
        (
            "wikipedia",
            "WikipediaSearchEngine",
            "_get_previews",
            ("query",),
            "wikipedia.search",
            requests.HTTPError(response=response(429)),
        ),
        (
            "wikinews",
            "WikinewsSearchEngine",
            "_fetch_search_results",
            ("query", 0),
            "safe_get",
            requests.HTTPError(response=response(429)),
        ),
        (
            "wayback",
            "WaybackSearchEngine",
            "_get_wayback_snapshots",
            ("https://example.org",),
            "safe_get",
            requests.ConnectionError("offline"),
        ),
    ],
)
def test_other_swallowed_request_errors_preserve_health(
    module_name, class_name, method, args, request_name, error
):
    import importlib

    module_path = f"local_deep_research.web_search_engines.engines.search_engine_{module_name}"
    module = importlib.import_module(module_path)
    engine = getattr(module, class_name)(settings_snapshot={}, llm=Mock())
    engine.programmatic_mode = True
    engine.rate_tracker = Mock(enabled=False)
    engine.rate_tracker.apply_rate_limit.return_value = 0.0
    raw_method = getattr(engine, method)
    with patch(f"{module_path}.{request_name}", side_effect=error):
        with pytest.raises(type(error)):
            getattr(engine, method)(*args)
        with patch.object(
            engine, "_get_previews", side_effect=lambda query: raw_method(*args)
        ):
            assert engine.run("query") == []
    assert engine.last_search_failure is not None
    engine.close()


@pytest.mark.parametrize(
    "status,delay", [(429, 180), (401, None), (503, 180), (500, 180)]
)
def test_openalex_response_failure_survives_run(status, delay):
    from local_deep_research.web_search_engines.engines.search_engine_openalex import (
        OpenAlexSearchEngine,
    )

    engine = OpenAlexSearchEngine(
        llm=Mock(), settings_snapshot={}, api_key="test-key"
    )
    engine.programmatic_mode = True
    failed = response(status)
    failed.headers["Retry-After"] = "180"
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
        return_value=failed,
    ):
        assert engine.run("query") == []
    assert engine.last_search_failure.cooldown_seconds == delay
    engine.close()


@pytest.mark.parametrize("during_initialization", [True, False])
def test_full_content_wrapper_preserves_failure_and_prevents_recreation(
    during_initialization,
):
    from local_deep_research.web_search_engines.engines.full_search import (
        FullSearchResults,
    )

    limited = response(429)
    limited.headers["Retry-After"] = "420"
    replies = (
        [limited]
        if during_initialization
        else [response(), response(), limited]
    )
    factory = Mock(
        side_effect=lambda: FullSearchResults(llm=None, web_search=searxng())
    )
    tracker = SearchEngineAvailability()
    with patch(SEARX_GET, side_effect=replies):
        first = run_search("searxng", "one", factory, tracker)
        second = run_search("searxng", "two", factory, tracker)
    assert "Rate limit reached" in first
    assert "for 420s" in second
    factory.assert_called_once()


def test_semantic_scholar_successful_empty_fallback_clears_prior_failure():
    engine = SemanticScholarSearchEngine(
        llm=Mock(), settings_snapshot={}, programmatic_mode=True
    )
    engine.llm = None
    engine.rate_tracker = Mock(enabled=False)
    engine.rate_tracker.apply_rate_limit.return_value = 0.0
    with (
        patch.object(engine, "_respect_rate_limit"),
        patch.object(
            engine.session,
            "get",
            side_effect=[response(429), response(body=b'{"data": []}')],
        ) as request,
    ):
        # The unquoted fallback is a successful search with no matches.
        assert engine.run('"one"') == []
    assert request.call_count == 2
    assert engine.last_search_failure is None
    engine.close()


def test_pubmed_successful_empty_fallback_clears_prior_failure():
    from local_deep_research.web_search_engines.engines.search_engine_pubmed import (
        PubMedSearchEngine,
    )

    engine = PubMedSearchEngine(llm=Mock(), settings_snapshot={})
    engine.programmatic_mode = True
    engine.llm = None
    engine.rate_tracker = Mock(enabled=False)
    engine.rate_tracker.apply_rate_limit.return_value = 0.0
    with (
        patch.object(engine, "_get_result_count", return_value=0),
        patch(
            "local_deep_research.web_search_engines.engines.search_engine_pubmed.safe_get",
            side_effect=[
                requests.Timeout(),
                response(
                    body=b'{"esearchresult": {"idlist": [], "count": "0"}}'
                ),
            ],
        ) as request,
    ):
        # Removing the time filter succeeds after the first request failed.
        assert engine.run("one") == []
    assert request.call_count == 2
    assert engine.last_search_failure is None
    engine.close()


@pytest.mark.parametrize("status,delay", [(429, 480), (500, 480)])
def test_pubmed_required_summaries_failure_survives_base_run(status, delay):
    from local_deep_research.web_search_engines.engines.search_engine_pubmed import (
        PubMedSearchEngine,
    )

    engine = PubMedSearchEngine(llm=Mock(), settings_snapshot={})
    engine.programmatic_mode = True
    engine.rate_tracker = Mock(enabled=False)
    engine.rate_tracker.apply_rate_limit.return_value = 0.0
    failed = response(status)
    failed.headers["Retry-After"] = "480"
    with (
        patch.object(engine, "_optimize_query_for_pubmed", return_value="q"),
        patch.object(
            engine, "_adaptive_search", return_value=(["123"], "test")
        ),
        patch.object(engine, "_get_article_abstracts", return_value={}),
        patch(
            "local_deep_research.web_search_engines.engines.search_engine_pubmed.safe_get",
            return_value=failed,
        ),
    ):
        assert engine.run("query") == []
    assert engine.last_search_failure.cooldown_seconds == delay
    engine.close()


def test_arxiv_translated_rate_limit_preserves_retry_after():
    from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
        ArXivSearchEngine,
    )

    engine = ArXivSearchEngine(llm=Mock(), settings_snapshot={})
    engine.programmatic_mode = True
    engine.rate_tracker = Mock(enabled=False)
    failed = response(429)
    failed.headers["Retry-After"] = "540"
    with patch.object(
        engine,
        "_get_search_results",
        side_effect=requests.HTTPError("429 rate limited", response=failed),
    ):
        assert engine.run("query") == []
    assert engine.last_search_failure.cooldown_seconds == 540
    engine.close()


@pytest.mark.parametrize("closest_only", [True, False])
def test_wayback_rate_limit_preserves_retry_after(closest_only):
    from local_deep_research.web_search_engines.engines.search_engine_wayback import (
        WaybackSearchEngine,
    )

    engine = WaybackSearchEngine(
        llm=Mock(), settings_snapshot={}, closest_only=closest_only
    )
    engine.programmatic_mode = True
    engine.rate_tracker = Mock(enabled=False)
    failed = response(429)
    failed.headers["Retry-After"] = "600"
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_wayback.safe_get",
        return_value=failed,
    ):
        assert engine.run("https://example.org") == []
    assert engine.last_search_failure.cooldown_seconds == 600
    engine.close()


def test_wikipedia_summary_request_failure_is_not_a_successful_empty_search():
    from local_deep_research.web_search_engines.engines.search_engine_wikipedia import (
        WikipediaSearchEngine,
    )

    engine = WikipediaSearchEngine(settings_snapshot={})
    engine.programmatic_mode = True
    with (
        patch(
            "local_deep_research.web_search_engines.engines.search_engine_wikipedia.wikipedia.search",
            return_value=["Title"],
        ),
        patch(
            "local_deep_research.web_search_engines.engines.search_engine_wikipedia.wikipedia.summary",
            side_effect=requests.Timeout(),
        ),
    ):
        assert engine.run("query") == []
    assert "timed out" in engine.last_search_failure.reason
    engine.close()


def test_serper_invalid_payload_is_not_a_successful_empty_search():
    engine = SerperSearchEngine(api_key="test-key", settings_snapshot={})
    engine.programmatic_mode = True
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_serper.safe_post",
        return_value=response(body=b"[]"),
    ):
        assert engine.run("query") == []
    assert engine.last_search_failure is not None
    engine.close()


def test_openalex_bad_query_does_not_mark_the_engine_down():
    from local_deep_research.web_search_engines.engines.search_engine_openalex import (
        OpenAlexSearchEngine,
    )

    engine = OpenAlexSearchEngine(
        llm=Mock(), settings_snapshot={}, api_key="test-key"
    )
    engine.programmatic_mode = True
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
        return_value=response(400),
    ):
        assert engine.run("query with (bad syntax") == []
    assert engine.last_search_failure is None
    engine.close()


def test_wayback_non_json_reply_for_one_url_does_not_mark_the_engine_down():
    from local_deep_research.web_search_engines.engines.search_engine_wayback import (
        WaybackSearchEngine,
    )

    engine = WaybackSearchEngine(llm=Mock(), settings_snapshot={})
    blocked = response(403, body=b"<html>Blocked Site Error</html>")
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_wayback.safe_get",
        return_value=blocked,
    ):
        assert engine._get_wayback_snapshots("https://blocked.example") == []
    assert engine.last_search_failure is None
    engine.close()