~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Tests for the SerplySearchEngine class.
Tests cover:
- Initialization and configuration
- API key handling
- Request construction per search type
- Preview generation for web, news and scholar results
- Rate limit error handling
- Class attributes
"""
from unittest.mock import Mock, patch
import pytest
import requests
MODULE = "local_deep_research.web_search_engines.engines.search_engine_serply"
def _make_engine(**kwargs):
from local_deep_research.web_search_engines.engines.search_engine_serply import (
SerplySearchEngine,
)
kwargs.setdefault("api_key", "test-api-key")
return SerplySearchEngine(**kwargs)
def _mock_response(payload):
response = Mock()
response.status_code = 200
response.json.return_value = payload
response.raise_for_status = Mock()
return response
class TestSerplySearchEngineInit:
"""Tests for SerplySearchEngine initialization."""
def test_init_with_api_key(self):
"""Initialize with API key and defaults."""
engine = _make_engine()
assert engine.api_key == "test-api-key"
assert engine.max_results == 10
assert engine.search_type == "web"
assert engine.include_full_content is False
def test_init_invalid_search_type_falls_back_to_web(self):
"""An unsupported search_type falls back to 'web' and logs a
warning so a config typo does not silently change verticals."""
with patch(f"{MODULE}.logger") as mock_logger:
engine = _make_engine(search_type="images")
assert engine.search_type == "web"
mock_logger.warning.assert_called_once()
assert "images" in mock_logger.warning.call_args[0][0]
def test_init_valid_search_type_does_not_warn(self):
"""A supported search_type is accepted without a warning."""
with patch(f"{MODULE}.logger") as mock_logger:
engine = _make_engine(search_type="scholar")
assert engine.search_type == "scholar"
mock_logger.warning.assert_not_called()
def test_scholar_search_type_is_scientific(self):
"""Scholar mode joins the academic pipeline (DOI enrichment,
journal reputation) while web and news stay generic."""
assert _make_engine(search_type="scholar").is_scientific is True
assert _make_engine(search_type="web").is_scientific is False
assert _make_engine(search_type="news").is_scientific is False
def test_base_kwargs_are_forwarded(self):
"""Base-class parameters passed through **kwargs reach
BaseSearchEngine instead of being dropped."""
engine = _make_engine(programmatic_mode=True, include_full_content=True)
assert engine.programmatic_mode is True
assert engine.include_full_content is True
def test_init_without_api_key_raises(self):
"""Initialize without API key raises ValueError."""
from local_deep_research.web_search_engines.engines.search_engine_serply import (
SerplySearchEngine,
)
with pytest.raises(
ValueError, match="No valid API key found for Serply"
):
SerplySearchEngine()
def test_init_with_api_key_from_settings(self):
"""Initialize with API key from settings snapshot."""
from local_deep_research.web_search_engines.engines.search_engine_serply import (
SerplySearchEngine,
)
engine = SerplySearchEngine(
settings_snapshot={
"search.engine.web.serply.api_key": "settings-api-key"
}
)
assert engine.api_key == "settings-api-key"
class TestGetPreviews:
"""Tests for _get_previews across the three search types."""
def test_web_previews(self):
"""Web results map title/description/link to previews and the
request carries the API key header and capped num."""
payload = {
"results": [
{
"title": "Result 1",
"description": "First description",
"link": "https://example.com/1",
"position": "1",
},
{
"title": "Result 2",
"description": "Second description",
"link": "https://example.org/2",
"position": "2",
},
]
}
with patch(
f"{MODULE}.safe_get", return_value=_mock_response(payload)
) as mock_get:
engine = _make_engine(max_results=150)
previews = engine._search_previews("test query")
assert len(previews) == 2
assert previews[0]["title"] == "Result 1"
assert previews[0]["snippet"] == "First description"
assert previews[0]["link"] == "https://example.com/1"
assert previews[0]["displayed_link"] == "example.com"
assert previews[1]["position"] == 2
assert "_full_result" not in previews[0]
call_kwargs = mock_get.call_args[1]
assert mock_get.call_args[0][0] == "https://api.serply.io/v1/search/"
assert call_kwargs["params"] == {"q": "test query", "num": 100}
assert call_kwargs["headers"]["X-Api-Key"] == "test-api-key"
assert "User-Agent" in call_kwargs["headers"]
def test_news_previews_strip_html_and_cap_results(self):
"""News entries come from the /news/ endpoint; the HTML summary is
flattened to text and the list is capped since Serply ignores num."""
payload = {
"entries": [
{
"title": f"Story {i}",
"link": f"https://news.example.com/{i}",
"summary": (
'<a href="https://x">Story</a> '
'<font color="#6f6f6f">Some Source</font>'
),
"published": "Wed, 20 May 2026 07:00:00 GMT",
}
for i in range(5)
]
}
with patch(
f"{MODULE}.safe_get", return_value=_mock_response(payload)
) as mock_get:
engine = _make_engine(search_type="news", max_results=2)
previews = engine._search_previews("test query")
assert mock_get.call_args[0][0] == "https://api.serply.io/v1/news/"
assert len(previews) == 2
assert previews[0]["snippet"] == "Story Some Source"
assert previews[0]["date"] == "Wed, 20 May 2026 07:00:00 GMT"
def test_news_snippet_strips_entity_encoded_markup(self):
"""Entities are decoded before tags are stripped, so encoded markup
in a summary is removed rather than turned back into live tags."""
payload = {
"entries": [
{
"title": "Story",
"link": "https://news.example.com/1",
"summary": (
"Plain & simple <img src=x onerror=alert(1)>"
"<b>bold</b> text"
),
}
]
}
with patch(f"{MODULE}.safe_get", return_value=_mock_response(payload)):
engine = _make_engine(search_type="news")
previews = engine._search_previews("test query")
assert previews[0]["snippet"] == "Plain & simple bold text"
assert "<" not in previews[0]["snippet"]
def test_scholar_previews(self):
"""Scholar articles come from the /scholar/ endpoint and use the
author/venue description as the snippet."""
payload = {
"articles": [
{
"title": "A Survey",
"link": "https://arxiv.org/abs/1234.5678",
"description": "A Author, B Author - arXiv, 2023",
"extras": {"citations": {"count": 12}},
}
]
}
with patch(
f"{MODULE}.safe_get", return_value=_mock_response(payload)
) as mock_get:
engine = _make_engine(search_type="scholar")
previews = engine._search_previews("test query")
assert mock_get.call_args[0][0] == "https://api.serply.io/v1/scholar/"
assert previews[0]["title"] == "A Survey"
assert previews[0]["snippet"] == "A Author, B Author - arXiv, 2023"
assert "date" not in previews[0]
def test_empty_results(self):
"""A response without the expected list yields no previews."""
with patch(
f"{MODULE}.safe_get", return_value=_mock_response({"results": None})
):
engine = _make_engine()
assert engine._search_previews("test query") == []
def test_rate_limit_status_raises(self):
"""A 429 response raises RateLimitError."""
from local_deep_research.web_search_engines.rate_limiting import (
RateLimitError,
)
response = _mock_response({})
response.status_code = 429
response.raise_for_status.side_effect = requests.HTTPError(
response=response
)
with patch(f"{MODULE}.safe_get", return_value=response):
engine = _make_engine()
with pytest.raises(RateLimitError):
engine._search_previews("test query")
def test_request_exception_returns_empty(self):
"""A network error is logged and yields no previews."""
with patch(
f"{MODULE}.safe_get",
side_effect=requests.exceptions.RequestException("boom"),
):
engine = _make_engine()
assert engine._search_previews("test query") == []
def test_request_exception_with_rate_limit_status(self):
"""A RequestException carrying a 429 response raises RateLimitError."""
from local_deep_research.web_search_engines.rate_limiting import (
RateLimitError,
)
err = requests.exceptions.RequestException("boom")
err.response = Mock(status_code=429)
with patch(f"{MODULE}.safe_get", side_effect=err):
engine = _make_engine()
with pytest.raises(RateLimitError):
engine._search_previews("test query")
class TestClassAttributes:
"""Tests for class-level attributes."""
def test_egress_labels(self):
"""Serply is a public, generic engine with NON_SENSITIVE/EXPOSING
egress labels (enforced by
test_registered_engines_declare_consistent_labels)."""
from local_deep_research.web_search_engines.engines.search_engine_serply import (
SerplySearchEngine,
)
from local_deep_research.web_search_engines.search_engine_base import (
Exposure,
Sensitivity,
)
assert SerplySearchEngine.is_public is True
assert SerplySearchEngine.is_generic is True
assert (
SerplySearchEngine.egress_sensitivity is Sensitivity.NON_SENSITIVE
)
assert SerplySearchEngine.egress_exposure is Exposure.EXPOSING