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.

15.0 KB
Raw Blame
"""Caller-to-consumer contracts from #6651; only external I/O is replaced.

The real registry, settings readers, factory signature filter, constructors,
engine execution and HTML extraction remain in the path. See the companion
developer guide for precedence, intentional agent overrides and mutation checks.
"""

import json
import socket
from types import SimpleNamespace
from unittest.mock import Mock
from urllib.parse import urlsplit

import pytest
import requests
from langchain_core.language_models.fake import FakeListLLM

from local_deep_research.advanced_search_system.strategies import (
    langgraph_agent_strategy as agent,
)
from local_deep_research.mcp.server import _execute_search
from local_deep_research.research_library.downloaders import html
from local_deep_research.web_search_engines import search_engine_factory
from local_deep_research.web_search_engines.engines import (
    search_engine_mojeek,
    search_engine_scaleserp,
    search_engine_searxng,
    search_engine_serper,
    search_engine_tinyfish,
)
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
)


ARTICLE_URL = "https://93.184.216.34/article"
SNIPPET = "Short provider snippet."
ARTICLE_TEXT = (
    "Die Forschung untersucht die Wirkung von Licht auf Pflanzen. "
    "Die Ergebnisse zeigen, dass die Pflanzen mit Licht besser wachsen. "
    "Diese ausführlichen Beobachtungen stehen nur im vollständigen Artikel. "
) * 8
ARTICLE_HTML = (
    f"<html><body><article><p>{ARTICLE_TEXT}</p></article></body></html>"
)
pytestmark = pytest.mark.usefixtures("loguru_caplog")


def _response(url, *, payload=None, text=""):
    response = requests.Response()
    response.status_code = 200
    response.url = url
    response.encoding = "utf-8"
    response.headers["Content-Type"] = (
        "application/json"
        if payload is not None
        else "text/html; charset=utf-8"
    )
    response._content = (
        json.dumps(payload).encode() if payload is not None else text.encode()
    )
    return response


@pytest.fixture(params=["scalar", "envelope"])
def snapshot(request):
    def build(engine="searxng", **values):
        settings = {
            "policy.egress_scope": "public_only",
            "search.tool": engine,
            "search.engine.library.enabled": False,
            "search.snippets_only": True,
            "search.max_filtered_results": 2,
            "search.region": "gb",
            "search.safe_search": True,
            "search.search_language": "German",
            f"search.engine.web.{engine}.enabled": True,
            f"search.engine.web.{engine}.requires_api_key": engine != "searxng",
            f"search.engine.web.{engine}.api_key": "offline-contract-credential",
            f"search.engine.web.{engine}.supports_full_search": True,
            f"search.engine.web.{engine}.default_params.enable_llm_relevance_filter": False,
        }
        if engine == "searxng":
            settings.update(
                {
                    "search.engine.web.searxng.default_params.instance_url": "https://93.184.216.35",
                    "search.engine.web.searxng.default_params.result_format": "json",
                }
            )
        settings.update(values)
        if request.param == "envelope":
            return {key: {"value": value} for key, value in settings.items()}
        return settings

    return build


@pytest.fixture
def offline_http(monkeypatch, tmp_path):
    """Replace HTTP/DNS, retaining real engine parsing and page extraction."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "data"))
    state = SimpleNamespace(requests=[], pages=[], extraction_languages=[])

    def dns(host, port, *args, **kwargs):
        return [
            (
                socket.AF_INET,
                socket.SOCK_STREAM,
                socket.IPPROTO_TCP,
                "",
                ("93.184.216.34", port or 443),
            )
        ]

    monkeypatch.setattr(socket, "getaddrinfo", dns)

    def provider(url, **kwargs):
        state.requests.append((url, kwargs))
        item = {
            "title": "Light and plants",
            "url": ARTICLE_URL,
            "link": ARTICLE_URL,
            "snippet": SNIPPET,
            "content": SNIPPET,
            "desc": SNIPPET,
        }
        items = [
            {
                **item,
                "url": f"{ARTICLE_URL}?result={i}",
                "link": f"{ARTICLE_URL}?result={i}",
            }
            for i in range(3)
        ]
        return _response(
            url,
            payload={
                "results": items,
                "organic": items,
                "organic_results": items,
                "response": {
                    "status": "OK",
                    "results": items,
                },
            },
        )

    for module, name in [
        (search_engine_searxng, "safe_get"),
        (search_engine_mojeek, "safe_get"),
        (search_engine_scaleserp, "safe_get"),
        (search_engine_serper, "safe_post"),
        (search_engine_tinyfish, "safe_get"),
    ]:
        monkeypatch.setattr(module, name, provider)

    def transport(self, request, **kwargs):
        assert urlsplit(request.url).path == "/article", request.url
        state.pages.append(request.url)
        return _response(request.url, text=ARTICLE_HTML)

    monkeypatch.setattr(requests.adapters.HTTPAdapter, "send", transport)
    extract = html.extract_content_with_metadata

    def observe_extraction(source, **kwargs):
        state.extraction_languages.append(kwargs["language"])
        return extract(source, **kwargs)

    monkeypatch.setattr(
        html, "extract_content_with_metadata", observe_extraction
    )
    return state


def _run(settings, **overrides):
    engine = search_engine_factory.create_search_engine(
        "searxng",
        settings_snapshot=settings,
        programmatic_mode=True,
        **overrides,
    )
    assert engine is not None
    try:
        return engine.run("light and plants")
    finally:
        engine.close()


@pytest.mark.parametrize(
    ("setting", "full"),
    [
        (True, False),
        (False, True),
        ("false", True),
        ("maybe", False),
        ("", False),
        (123, False),
        (None, False),
    ],
)
def test_snapshot_selects_real_searxng_execution(
    snapshot, offline_http, setting, full
):
    settings = snapshot(**{"search.snippets_only": setting})
    results = _run(settings)

    assert results and results[0]["snippet"] == SNIPPET
    request = next(
        kwargs["params"]
        for _, kwargs in offline_http.requests
        if "params" in kwargs
    )
    assert request["language"] == "de"
    assert request["safesearch"] == 1
    if full:
        assert ARTICLE_TEXT[:100] in results[0]["full_content"]
        assert offline_http.pages
        assert offline_http.extraction_languages == ["German"] * len(
            offline_http.pages
        )
    else:
        assert "full_content" not in results[0]
        assert offline_http.pages == []
        assert offline_http.extraction_languages == []


def test_missing_snippet_setting_uses_safe_engine_default(
    snapshot, offline_http
):
    settings = snapshot()
    del settings["search.snippets_only"]
    results = _run(settings)
    assert results[0]["snippet"] == SNIPPET
    assert "full_content" not in results[0]
    assert offline_http.pages == []


@pytest.mark.parametrize("snippets_only", [False, True])
@pytest.mark.parametrize("caller", ["factory", "get_search"])
def test_explicit_mode_overrides_opposite_snapshot(
    snapshot, offline_http, snippets_only, caller
):
    settings = snapshot(**{"search.snippets_only": not snippets_only})
    if caller == "factory":
        results = _run(settings, search_snippets_only=snippets_only)
    else:
        engine = search_engine_factory.get_search(
            "searxng",
            None,
            search_snippets_only=snippets_only,
            settings_snapshot=settings,
            programmatic_mode=True,
        )
        assert engine is not None
        try:
            results = engine.run("light and plants")
        finally:
            engine.close()
    assert bool(results[0].get("full_content")) is not snippets_only
    assert bool(offline_http.pages) is not snippets_only


@pytest.mark.parametrize(
    ("provider", "expected"),
    [
        ("mojeek", {"rb": "gb", "lb": "de", "safe": 1}),
        ("serper", {"gl": "gb", "hl": "de"}),
        ("tinyfish", {"location": "GB", "language": "de"}),
        # ScaleSerp's location is a place name, not an ISO country code.
        ("scaleserp", {"location": "United States", "hl": "de", "safe": "on"}),
    ],
)
def test_real_provider_requests_use_their_own_alias_formats(
    snapshot, offline_http, provider, expected
):
    engine = search_engine_factory.create_search_engine(
        provider, settings_snapshot=snapshot(provider), programmatic_mode=True
    )
    assert engine is not None
    try:
        results = engine.run("light and plants")
    finally:
        engine.close()
    assert results[0]["snippet"] == SNIPPET
    _, kwargs = offline_http.requests[-1]
    sent = kwargs.get("params", kwargs.get("json"))
    assert {key: sent[key] for key in expected} == expected
    assert offline_http.pages == []


def test_engine_defaults_and_explicit_aliases_have_documented_precedence(
    snapshot, offline_http
):
    settings = snapshot(
        **{
            "search.engine.web.searxng.default_params.language": "fr",
            "search.engine.web.searxng.default_params.safe_search": "STRICT",
        }
    )
    _run(settings)
    sent = offline_http.requests[-1][1]["params"]
    assert (sent["language"], sent["safesearch"]) == ("fr", 2)

    _run(settings, language="es", safe_search="OFF")
    sent = offline_http.requests[-1][1]["params"]
    assert (sent["language"], sent["safesearch"]) == ("es", 0)


def test_kwargs_base_constructor_keeps_snippet_flag_without_duplicates(
    snapshot, offline_http, monkeypatch
):
    calls = []
    original = BaseSearchEngine.__init__

    def observe(self, *args, **kwargs):
        calls.append(dict(kwargs))
        original(self, *args, **kwargs)

    monkeypatch.setattr(BaseSearchEngine, "__init__", observe)
    engine = search_engine_factory.create_search_engine(
        "mojeek",
        settings_snapshot=snapshot("mojeek", **{"search.snippets_only": False}),
        programmatic_mode=True,
        use_full_search=False,
    )
    assert isinstance(engine, search_engine_mojeek.MojeekSearchEngine)
    try:
        assert calls[0]["search_snippets_only"] is False
        assert engine.run("light and plants")[0]["snippet"] == SNIPPET
    finally:
        engine.close()


@pytest.mark.parametrize("limit", [1, 2])
def test_snapshot_filter_limit_reaches_real_relevance_filter(
    snapshot, offline_http, limit
):
    settings = snapshot(
        **{
            "search.max_filtered_results": str(limit),
            "search.engine.web.searxng.default_params.enable_llm_relevance_filter": True,
        }
    )
    engine = search_engine_factory.create_search_engine(
        "searxng",
        llm=FakeListLLM(responses=["0, 1, 2"]),
        settings_snapshot=settings,
        programmatic_mode=True,
    )
    assert engine is not None
    try:
        assert len(engine.run("light and plants")) == limit
    finally:
        engine.close()


@pytest.mark.parametrize(
    ("setting", "overrides", "expected"),
    [
        (None, {}, 1),
        ("not-a-number", {}, 1),
        (2, {"max_filtered_results": 3}, 3),
        (2, {}, 2),
    ],
)
def test_filter_limit_missing_malformed_and_explicit_precedence(
    snapshot, offline_http, setting, overrides, expected
):
    settings = snapshot(
        **{
            "search.max_filtered_results": setting,
            "search.engine.web.searxng.default_params.max_filtered_results": 1,
            "search.engine.web.searxng.default_params.enable_llm_relevance_filter": True,
        }
    )
    if setting is None:
        del settings["search.max_filtered_results"]
    engine = search_engine_factory.create_search_engine(
        "searxng",
        llm=FakeListLLM(responses=["0, 1, 2"]),
        settings_snapshot=settings,
        programmatic_mode=True,
        **overrides,
    )
    assert engine is not None
    try:
        assert len(engine.run("light and plants")) == expected
    finally:
        engine.close()


@pytest.mark.parametrize("region", ["all", "wt-wt"])
def test_non_country_region_does_not_become_tinyfish_location(
    snapshot, offline_http, region
):
    engine = search_engine_factory.create_search_engine(
        "tinyfish",
        settings_snapshot=snapshot("tinyfish", **{"search.region": region}),
        programmatic_mode=True,
    )
    assert engine is not None
    try:
        assert engine.run("light and plants")
    finally:
        engine.close()
    assert offline_http.requests[-1][1]["params"]["location"] == "US"


def test_region_overrides_engine_default_but_explicit_argument_wins(
    snapshot, offline_http
):
    settings = snapshot(
        "mojeek", **{"search.engine.web.mojeek.default_params.region": "fr"}
    )
    for overrides, expected in [({}, "gb"), ({"region": "es"}, "es")]:
        engine = search_engine_factory.create_search_engine(
            "mojeek",
            settings_snapshot=settings,
            programmatic_mode=True,
            **overrides,
        )
        assert engine is not None
        try:
            assert engine.run("light and plants")
        finally:
            engine.close()
        assert offline_http.requests[-1][1]["params"]["rb"] == expected


@pytest.mark.parametrize("snippets_only", [True, False])
def test_mcp_snapshot_controls_real_factory_and_page_extraction(
    snapshot, offline_http, snippets_only
):
    result = _execute_search(
        "light and plants",
        "searxng",
        3,
        snapshot(**{"search.snippets_only": snippets_only}),
    )
    assert result["status"] == "success"
    assert result["result_count"] == 3
    assert bool(result["results"][0].get("full_content")) is not snippets_only
    assert bool(offline_http.pages) is not snippets_only


@pytest.mark.parametrize("kind", ["web", "specialized"])
def test_agent_explicit_snippet_override_survives_real_execution(
    snapshot, offline_http, kind
):
    settings = snapshot(**{"search.snippets_only": False})
    collector = agent.SearchResultsCollector()
    if kind == "web":
        tool = agent._make_web_search_tool(
            search_engine_name="searxng",
            model=Mock(),
            settings_snapshot=settings,
            collector=collector,
            programmatic_mode=True,
        )
    else:
        tool = agent._make_specialized_search_tool(
            engine_name="searxng",
            description="Offline contract search",
            model=Mock(),
            settings_snapshot=settings,
            collector=collector,
            programmatic_mode=True,
        )
    output = tool.invoke({"query": "light and plants"})
    assert SNIPPET in output
    assert collector.results
    assert all("full_content" not in item for item in collector.results)
    assert offline_http.pages == []