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.

7.4 KB
Raw Blame
"""Guardian snippet/full-content behavior through the real engine and factory."""

from unittest.mock import Mock, patch

import pytest

from local_deep_research.utilities.research_content_cache import (
    ResearchContentCache,
)
from local_deep_research.web_search_engines.engines.search_engine_guardian import (
    GuardianSearchEngine,
)
from local_deep_research.web_search_engines.search_engine_factory import (
    create_search_engine,
)


def _api_response(*articles):
    response = Mock()
    response.json.return_value = {"response": {"results": list(articles)}}
    return response


def _article(article_id, body):
    return {
        "id": article_id,
        "webTitle": f"Title {article_id}",
        "webUrl": f"https://www.theguardian.com/{article_id}",
        "webPublicationDate": "2026-10-01T12:00:00Z",
        "sectionName": "News",
        "fields": {
            "headline": f"Title {article_id}",
            "trailText": f"Preview {article_id}",
            "byline": "Reporter",
            # Return a body even when not requested to prove it cannot leak.
            "body": body,
        },
        "tags": [],
    }


@pytest.mark.parametrize(
    ("snapshot_value", "override", "snippets_only"),
    [
        (True, None, True),
        (False, None, False),
        (False, True, True),
        (True, False, False),
    ],
)
def test_factory_setting_controls_guardian_request_and_result(
    snapshot_value, override, snippets_only
):
    snapshot = {
        "search.tool": {"value": "guardian"},
        "search.snippets_only": {"value": snapshot_value},
        "search.engine.web.guardian.api_key": {"value": "test-key"},
    }
    config = {
        "guardian": {
            "module_path": ".engines.search_engine_guardian",
            "class_name": "GuardianSearchEngine",
            "requires_api_key": True,
            "default_params": {},
        }
    }
    kwargs = {"search_snippets_only": override} if override is not None else {}

    with patch(
        "local_deep_research.web_search_engines.search_engine_factory.search_config",
        return_value=config,
    ):
        engine = create_search_engine(
            "guardian",
            settings_snapshot=snapshot,
            programmatic_mode=True,
            adaptive_search=False,
            **kwargs,
        )

    assert isinstance(engine, GuardianSearchEngine)
    assert engine.search_snippets_only is snippets_only
    engine.rate_tracker.apply_rate_limit = Mock(return_value=0)

    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_guardian.safe_get",
        return_value=_api_response(_article("one", "Complete article")),
    ) as get:
        results = engine.run("climate policy")

    assert len(results) == 1
    assert results[0]["snippet"] == "Preview one"
    assert results[0]["source"] == "The Guardian"
    assert ("body" in get.call_args.kwargs["params"]["show-fields"]) is (
        not snippets_only
    )
    if snippets_only:
        assert "content" not in results[0]
        assert "full_content" not in results[0]
    else:
        assert results[0]["content"] == "Complete article"
        assert results[0]["full_content"] == "Complete article"
    assert not hasattr(engine, "_full_articles")


@pytest.mark.parametrize("snippets_only", [True, False])
def test_relevance_filter_keeps_selected_article_in_both_modes(snippets_only):
    engine = GuardianSearchEngine(
        api_key="test-key",
        llm=Mock(),
        max_filtered_results=1,
        adaptive_search=False,
        optimize_queries=False,
        search_snippets_only=snippets_only,
    )
    engine.rate_tracker.apply_rate_limit = Mock(return_value=0)

    with (
        patch(
            "local_deep_research.web_search_engines.engines.search_engine_guardian.safe_get",
            return_value=_api_response(
                _article("one", "Body one"), _article("two", "Body two")
            ),
        ),
        patch.object(
            engine,
            "_filter_for_relevance",
            side_effect=lambda previews, query: previews[1:],
        ) as filter_results,
    ):
        results = engine.run("climate policy")

    filter_results.assert_called_once()
    assert [item["id"] for item in results] == ["two"]
    assert ("full_content" in results[0]) is (not snippets_only)
    if not snippets_only:
        assert results[0]["full_content"] == "Body two"


@pytest.fixture
def cached_guardian_engine():
    engine = GuardianSearchEngine(
        api_key="test-key",
        adaptive_search=False,
        optimize_queries=False,
        search_snippets_only=False,
    )
    engine.content_cache = ResearchContentCache()
    engine.rate_tracker.apply_rate_limit = Mock(return_value=0)
    with patch(
        "local_deep_research.security.ssrf_validator.validate_url",
        return_value=True,
    ):
        yield engine


@pytest.mark.parametrize("cached_body", [None, "Previously fetched body"])
def test_snippets_never_restore_or_populate_shared_cache(
    cached_guardian_engine, cached_body
):
    engine = cached_guardian_engine
    engine.search_snippets_only = True
    url = "https://www.theguardian.com/one"
    if cached_body is not None:
        engine.content_cache.put(url, cached_body)

    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_guardian.safe_get",
        return_value=_api_response(_article("one", "Unrequested body")),
    ):
        results = engine.run("climate policy")

    assert len(results) == 1
    assert results[0]["snippet"] == "Preview one"
    assert "content" not in results[0]
    assert "full_content" not in results[0]
    assert "full_content_is_complete" not in results[0]
    cached = engine.content_cache.get(url)
    if cached_body is None:
        assert cached is None
    else:
        assert cached["content"] == cached_body


def test_full_content_reuses_shared_cache_after_local_cleanup(
    cached_guardian_engine,
):
    engine = cached_guardian_engine
    url = "https://www.theguardian.com/one"
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_guardian.safe_get",
        side_effect=[
            _api_response(_article("one", "Complete body")),
            _api_response(_article("one", "")),
        ],
    ):
        first = engine.run("climate policy")
        assert first[0]["full_content"] == "Complete body"
        assert not hasattr(engine, "_full_articles")
        assert engine.content_cache.get(url)["content"] == "Complete body"

        second = engine.run("related climate policy")

    assert second[0]["full_content"] == "Complete body"
    assert second[0]["full_content_is_complete"] is True
    assert not hasattr(engine, "_full_articles")


def test_missing_body_does_not_prevent_later_complete_fetch(
    cached_guardian_engine,
):
    engine = cached_guardian_engine
    url = "https://www.theguardian.com/one"
    with patch(
        "local_deep_research.web_search_engines.engines.search_engine_guardian.safe_get",
        side_effect=[
            _api_response(_article("one", "")),
            _api_response(_article("one", "Complete body")),
        ],
    ):
        first = engine.run("climate policy")
        assert first[0]["full_content_is_complete"] is False
        assert engine.content_cache.get(url) is None

        second = engine.run("related climate policy")

    assert second[0]["full_content"] == "Complete body"
    assert engine.content_cache.get(url)["content"] == "Complete body"