~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
"""
Comprehensive tests for the ArXiv search engine.
Tests initialization, search functionality, error handling, and sorting options.
"""
import pytest
from unittest.mock import Mock
from datetime import datetime
from local_deep_research.utilities.arxiv_api import (
ArxivSortCriterion,
ArxivSortOrder,
)
class TestArXivSearchEngineInit:
"""Tests for ArXiv search engine initialization."""
@pytest.fixture(autouse=True)
def mock_journal_filter(self, monkeypatch):
"""Mock JournalReputationFilter to avoid LLM dependency."""
monkeypatch.setattr(
"local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
Mock(return_value=None),
)
def test_init_default_parameters(self):
"""Test initialization with default parameters."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine()
# ArXiv forces minimum of 25 results
assert engine.max_results >= 25
assert engine.sort_by == "relevance"
assert engine.sort_order == "descending"
assert engine.include_full_text is False
assert engine.download_dir is None
assert engine.is_public is True
assert engine.is_scientific is True
assert engine.is_generic is False
def test_init_custom_parameters(self):
"""Test initialization with custom parameters."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine(
max_results=50,
sort_by="submittedDate",
sort_order="ascending",
include_full_text=True,
download_dir="/tmp/arxiv",
max_full_text=5,
)
assert engine.max_results == 50
assert engine.sort_by == "submittedDate"
assert engine.sort_order == "ascending"
assert engine.include_full_text is True
assert engine.download_dir == "/tmp/arxiv"
assert engine.max_full_text == 5
def test_sort_criteria_mapping(self):
"""Test that sort criteria are properly mapped."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine()
assert "relevance" in engine.sort_criteria
assert "lastUpdatedDate" in engine.sort_criteria
assert "submittedDate" in engine.sort_criteria
assert engine.sort_criteria["relevance"] == ArxivSortCriterion.RELEVANCE
class TestArXivSearchExecution:
"""Tests for ArXiv search execution."""
@pytest.fixture(autouse=True)
def mock_journal_filter(self, monkeypatch):
"""Mock JournalReputationFilter to avoid LLM dependency."""
monkeypatch.setattr(
"local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
Mock(return_value=None),
)
@pytest.fixture
def arxiv_engine(self):
"""Create an ArXiv engine instance."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
return ArXivSearchEngine(max_results=10)
def test_search_results_structure(self, arxiv_engine, monkeypatch):
"""Test that search results have correct structure."""
# Create mock arXiv result
mock_result = Mock()
mock_result.entry_id = "http://arxiv.org/abs/2301.12345v1"
mock_result.title = "Deep Learning for NLP"
mock_result.summary = "A comprehensive study of deep learning in NLP."
mock_result.authors = [Mock(name="John Doe"), Mock(name="Jane Smith")]
mock_result.published = datetime(2023, 1, 15)
mock_result.updated = datetime(2023, 2, 1)
mock_result.pdf_url = "http://arxiv.org/pdf/2301.12345v1"
mock_result.primary_category = "cs.CL"
mock_result.categories = ["cs.CL", "cs.AI"]
monkeypatch.setattr(
"local_deep_research.web_search_engines.engines.search_engine_arxiv.fetch_arxiv_results",
Mock(return_value=[mock_result]),
)
results = arxiv_engine._get_search_results("deep learning")
assert len(results) == 1
assert results[0].title == "Deep Learning for NLP"
def test_search_empty_results(self, arxiv_engine, monkeypatch):
"""Test ArXiv search with no results."""
monkeypatch.setattr(
"local_deep_research.web_search_engines.engines.search_engine_arxiv.fetch_arxiv_results",
Mock(return_value=[]),
)
results = arxiv_engine._search_previews("nonexistent query xyz")
assert results == []
class TestArXivErrorHandling:
"""Tests for ArXiv search error handling."""
@pytest.fixture(autouse=True)
def mock_journal_filter(self, monkeypatch):
"""Mock JournalReputationFilter to avoid LLM dependency."""
monkeypatch.setattr(
"local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
Mock(return_value=None),
)
@pytest.fixture
def arxiv_engine(self):
"""Create an ArXiv engine instance."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
return ArXivSearchEngine(max_results=10)
def test_search_handles_network_error(self, arxiv_engine, monkeypatch):
"""Test that network errors are handled gracefully by _get_previews."""
from urllib.error import URLError
monkeypatch.setattr(
"local_deep_research.web_search_engines.engines.search_engine_arxiv.fetch_arxiv_results",
Mock(side_effect=URLError("Network unreachable")),
)
# _get_previews handles exceptions and returns empty list
results = arxiv_engine._search_previews("test query")
assert results == []
def test_search_handles_fetch_exception(self, arxiv_engine, monkeypatch):
monkeypatch.setattr(
"local_deep_research.web_search_engines.engines.search_engine_arxiv.fetch_arxiv_results",
Mock(side_effect=RuntimeError("empty page")),
)
# _get_previews handles exceptions and returns empty list
results = arxiv_engine._search_previews("test query")
assert results == []
def test_rate_limit_error_message_is_scrubbed(
self, arxiv_engine, monkeypatch
):
"""Raised RateLimitError must not propagate raw exception secrets.
An upstream API can echo credentials back in an error body (e.g. a
Bearer token in a 429 response). The substring check on ``error_msg``
identifies the rate-limit shape, but the raised ``RateLimitError``
must carry the scrubbed ``safe_msg`` — otherwise tenacity / global
handlers logging the raised exception re-leak the secret.
"""
import traceback
from urllib.error import HTTPError
from local_deep_research.web_search_engines.rate_limiting import (
RateLimitError,
)
leaked = "Bearer sk-arxivleaked0987654321"
monkeypatch.setattr(
"local_deep_research.web_search_engines.engines.search_engine_arxiv.fetch_arxiv_results",
Mock(
side_effect=HTTPError(
"https://export.arxiv.org/api/query",
429,
f"Too Many Requests: {leaked}",
{},
None,
)
),
)
with pytest.raises(RateLimitError) as exc_info:
arxiv_engine._search_previews("test query")
msg = str(exc_info.value)
assert "sk-arxivleaked0987654321" not in msg
# A full traceback render must not re-leak the secret through the
# implicit __context__ chain (guards the `from None` on the raise:
# handlers like the thread excepthook format with chain=True).
rendered = "".join(traceback.format_exception(exc_info.value))
assert "sk-arxivleaked0987654321" not in rendered
class TestArXivSortingOptions:
"""Tests for ArXiv sorting functionality."""
@pytest.fixture(autouse=True)
def mock_journal_filter(self, monkeypatch):
"""Mock JournalReputationFilter to avoid LLM dependency."""
monkeypatch.setattr(
"local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
Mock(return_value=None),
)
def test_sort_by_relevance(self):
"""Test sorting by relevance."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine(sort_by="relevance")
assert (
engine.sort_criteria.get(engine.sort_by)
== ArxivSortCriterion.RELEVANCE
)
def test_sort_by_date(self):
"""Test sorting by submission date."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine(sort_by="submittedDate")
assert (
engine.sort_criteria.get(engine.sort_by)
== ArxivSortCriterion.SUBMITTED_DATE
)
def test_sort_order_ascending(self):
"""Test ascending sort order."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine(sort_order="ascending")
assert (
engine.sort_directions.get(engine.sort_order)
== ArxivSortOrder.ASCENDING
)
class TestArXivFullTextDownload:
"""Tests for ArXiv full text download functionality."""
@pytest.fixture(autouse=True)
def mock_journal_filter(self, monkeypatch):
"""Mock JournalReputationFilter to avoid LLM dependency."""
monkeypatch.setattr(
"local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
Mock(return_value=None),
)
def test_full_text_disabled_by_default(self):
"""Test that full text download is disabled by default."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine()
assert engine.include_full_text is False
def test_full_text_enabled_with_download_dir(self):
"""Test that full text can be enabled with download directory."""
from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
ArXivSearchEngine,
)
engine = ArXivSearchEngine(
include_full_text=True,
download_dir="/tmp/arxiv_papers",
max_full_text=3,
)
assert engine.include_full_text is True
assert engine.download_dir == "/tmp/arxiv_papers"
assert engine.max_full_text == 3