~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
"""Cross-engine SUCCESS-path contracts for the search-engine adapter layer.
Scope (PR #3299, Flask -> FastAPI port). The sibling file
``test_adapter_failure_contracts.py`` pins what happens when a backend
*fails*. This file pins the opposite half, which nothing covered: given a
**realistic** upstream response, does each engine that the UI offers
actually produce results that survive all the way into a report?
Every engine here is driven with its HTTP client (or vendored SDK) stubbed
out with a payload shaped like the real upstream response -- reconstructed
from the adapter's own parsing code and, where they existed, from the
captured fixtures in ``tests/mock_fixtures.py``. **No test in this file
makes an outbound network request**; an autouse fixture wedges the bottom
of ``requests`` shut so an unstubbed seam fails loudly instead of dialling
out.
What this file pins that the per-engine unit tests do not:
1. **The whole registry, not a favourite subset.** Every entry in
``ENGINE_REGISTRY`` must resolve to an importable ``BaseSearchEngine``
subclass, and must be constructible without a live backend. Two
entries are not (``elasticsearch``, ``google_pse``), one more
constructs into a permanently empty engine (``searxng``), and one is
unreachable through the factory at all (``guardian``) --
each pinned below.
2. **"Usable", not "a list".** For each engine the first preview is
asserted field by field against the stubbed upstream document: the
``title``/``link``/``snippet`` a human would recognise, not just a
truthy container. ``snippet`` matters because
``BaseCitationHandler._create_documents`` reads
``full_content`` -> ``snippet`` for the text the synthesis LLM sees; an
engine with an empty snippet cites a blank document.
3. **Nothing is dropped downstream.** The previews are fed to the real
consumers -- ``extract_links_from_search_results``,
``format_links_to_markdown`` and
``CitationFormatter.apply_inline_hyperlinks`` -- and every result must
come out the far end as a numbered, hyperlinked source.
4. **Two silent-drop defects** in that downstream chain, proved by
consequence rather than restated: a non-``str`` ``index`` discards the
whole result with only a log line, and ``RetrieverSearchEngine`` emits
``url`` without ``link`` so every LangChain-retriever result is dropped
on the non-LangGraph strategies.
Tests whose docstring starts with ``DEFECT:`` pin behaviour that is
currently wrong. They assert the broken behaviour on purpose so the file
stays green; each one names what a fix should change.
"""
import importlib
import json
from datetime import UTC, datetime, timedelta
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
import requests
from local_deep_research.security.module_whitelist import get_safe_module_class
from local_deep_research.text_optimization.citation_formatter import (
CitationFormatter,
CitationMode,
)
from local_deep_research.utilities.search_utilities import (
extract_links_from_search_results,
format_links_to_markdown,
)
from local_deep_research.web_search_engines.engine_registry import (
ENGINE_REGISTRY,
)
from local_deep_research.web_search_engines.engines import (
search_engine_arxiv as arxiv_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_brave as brave_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_collection as collection_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_elasticsearch as es_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_exa as exa_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_federal_register as federal_register_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_github as github_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_google_pse as pse_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_guardian as guardian_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_gutenberg as gutenberg_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_library as library_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_mojeek as mojeek_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_nasa_ads as nasa_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_openalex as openalex_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_openlibrary as openlibrary_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_paperless as paperless_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_pubchem as pubchem_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_pubmed as pubmed_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_retriever as retriever_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_scaleserp as scaleserp_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_searxng as searxng_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_semantic_scholar as s2_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_serpapi as serpapi_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_serper as serper_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_serply as serply_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_sofya as sofya_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_stackexchange as stackexchange_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_tavily as tavily_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_tinyfish as tinyfish_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_wayback as wayback_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_wikinews as wikinews_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_wikipedia as wikipedia_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_zenodo as zenodo_engine,
)
from local_deep_research.web_search_engines.search_engine_base import (
BaseSearchEngine,
)
# Two words, both lower-case and free of regex metacharacters, so the
# engines that require every query term to appear in the document
# (Wikinews) can be satisfied by a realistic stub.
QUERY = "transformer architecture"
API_KEY = "test-key-abcd1234"
# The five engines that build a ``JournalReputationFilter`` in their
# constructor. With no ``llm`` argument the filter reaches for one of its
# own, which drags in the whole LLM/egress-policy stack -- irrelevant to
# the preview path (the filter runs inside ``run()``, not
# ``_get_previews``), so it is switched off through the same per-engine
# setting the UI exposes.
_JOURNAL_FILTER_ENGINES = (
"arxiv",
"nasa_ads",
"openalex",
"pubmed",
"semantic_scholar",
)
# Minimal snapshot: non-empty so ``get_setting_from_snapshot`` resolves
# defaults from the snapshot path instead of reaching for a database, and
# carrying the ``_username`` the library/collection engines read.
SNAPSHOT = {
"_username": "success_path_user",
**{
f"search.engine.web.{name}.journal_reputation.enabled": False
for name in _JOURNAL_FILTER_ENGINES
},
}
# --------------------------------------------------------------------------
# Stubs
# --------------------------------------------------------------------------
class StubResponse:
"""Stand-in for ``requests.Response`` for every stubbed adapter."""
def __init__(self, status_code=200, text="", payload=None, headers=None):
self.status_code = status_code
self.text = text
self.cookies = {}
self.headers = headers if headers is not None else {}
self._payload = payload
def json(self):
if self._payload is None:
raise ValueError("no JSON payload configured on this stub")
return self._payload
def raise_for_status(self):
if self.status_code >= 400:
raise requests.HTTPError(f"{self.status_code} Error")
def const_response(**kwargs):
"""A ``safe_get``/``safe_post`` replacement returning one response."""
def _call(*_args, **_kwargs):
return StubResponse(**kwargs)
return _call
def routed_response(routes, default=None):
"""Dispatch on a substring of the request URL.
``routes`` maps a URL fragment to the ``StubResponse`` kwargs for
requests whose URL contains it. Multi-call adapters (PubMed's
esearch/esummary/efetch, PubChem's autocomplete/cid/property/
description) need this to look like one coherent upstream session
rather than the same blob four times.
"""
def _call(url, *_args, **_kwargs):
for fragment, kwargs in routes.items():
if fragment in url:
return StubResponse(**kwargs)
if default is not None:
return StubResponse(**default)
raise AssertionError(f"unstubbed upstream URL: {url}")
return _call
class _NetworkBlocked(AssertionError):
"""Raised if any test in this file would dial out for real."""
@pytest.fixture(autouse=True)
def no_outbound_network():
"""Wedge the bottom of ``requests`` shut.
Every adapter here routes through ``requests`` (directly, via
``safe_requests``, or via a vendored SDK). Patching the transport
adapter -- rather than each engine's seam -- means a stub I got wrong
surfaces as a loud failure instead of a live call to a public API.
"""
def _blocked(*_args, **_kwargs):
raise _NetworkBlocked("test attempted a real outbound HTTP request")
with patch.object(requests.adapters.HTTPAdapter, "send", _blocked):
yield
@pytest.fixture
def inert_tracker():
"""A rate tracker that never sleeps and never touches a database."""
tracker = MagicMock()
tracker.enabled = False
tracker.apply_rate_limit.return_value = 0.0
tracker.get_wait_time.return_value = 0.0
tracker.record_outcome.return_value = None
target = (
"local_deep_research.web_search_engines.search_engine_base.get_tracker"
)
with patch(target, return_value=tracker):
yield tracker
def _wire(engine, tracker):
"""Common post-construction wiring for a network-free engine."""
engine.rate_tracker = tracker
return engine
# --------------------------------------------------------------------------
# Realistic upstream payloads
#
# Field names and nesting come from the adapter's own parsing code (which
# is the in-repo record of the real API shape); values are drawn from a
# single coherent document per engine so an assertion on the preview can
# name the exact string the upstream returned.
# --------------------------------------------------------------------------
ARXIV_PAPER = SimpleNamespace(
entry_id="http://arxiv.org/abs/1706.03762v5",
title="Attention Is All You Need",
summary="We propose a new simple network architecture, the Transformer.",
authors=[
SimpleNamespace(name="Ashish Vaswani"),
SimpleNamespace(name="Noam Shazeer"),
],
published=datetime(2017, 6, 12, tzinfo=UTC),
journal_ref=None,
pdf_url="http://arxiv.org/pdf/1706.03762v5",
categories=["cs.CL"],
comment=None,
doi=None,
updated=None,
primary_category="cs.CL",
)
BRAVE_PAYLOAD = [
{
"title": "Transformer (deep learning architecture)",
"link": "https://en.wikipedia.org/wiki/Transformer_(deep_learning)",
"snippet": "A transformer is a deep learning architecture.",
}
]
ELASTIC_PAYLOAD = {
"hits": {
"hits": [
{
"_id": "doc-42",
"_index": "documents",
"_score": 7.5,
"_source": {
"title": "Internal Transformer Notes",
"url": "https://intranet.example.org/docs/42",
"content": "Long form notes about transformer models.",
},
"highlight": {
"content": ["notes about <em>transformer</em> models"]
},
}
]
}
}
EXA_PAYLOAD = {
"requestId": "req-1",
"results": [
{
"id": "https://arxiv.org/abs/1706.03762",
"title": "Attention Is All You Need",
"url": "https://arxiv.org/abs/1706.03762",
"publishedDate": "2017-06-12T00:00:00.000Z",
"author": "Ashish Vaswani",
"score": 0.19,
"text": "The dominant sequence transduction models...",
"highlights": ["the Transformer, based solely on attention"],
"summary": "Introduces the Transformer architecture.",
}
],
}
FEDERAL_REGISTER_PAYLOAD = {
"count": 1,
"results": [
{
"document_number": "2024-00001",
"title": "Transformer Safety Standards",
"html_url": (
"https://www.federalregister.gov/documents/2024/01/01/"
"2024-00001/transformer-safety-standards"
),
"abstract": "New transformer safety standards for the electrical grid.",
"type": "Rule",
"agencies": [{"name": "Department of Energy"}],
"publication_date": "2024-01-01",
"pdf_url": (
"https://www.federalregister.gov/documents/2024/01/01/"
"2024-00001.pdf"
),
}
],
}
GITHUB_REPO_PAYLOAD = {
"total_count": 1,
"incomplete_results": False,
"items": [
{
"id": 155220641,
"full_name": "huggingface/transformers",
"html_url": "https://github.com/huggingface/transformers",
"description": "State-of-the-art Machine Learning.",
"stargazers_count": 132000,
"forks_count": 26000,
"language": "Python",
"updated_at": "2024-05-01T12:00:00Z",
"created_at": "2018-10-29T13:56:00Z",
"topics": ["nlp", "transformers"],
"owner": {"login": "huggingface"},
"fork": False,
}
],
}
# GitHub returns ``"description": null`` for the many repositories that
# have none, and ``"body": null`` for issues opened with an empty body.
GITHUB_REPO_NULL_DESCRIPTION = {
"total_count": 1,
"items": [
{
"id": 1,
"full_name": "octocat/hello-world",
"html_url": "https://github.com/octocat/hello-world",
"description": None,
"owner": {"login": "octocat"},
}
],
}
GITHUB_ISSUE_NULL_BODY = {
"total_count": 1,
"items": [
{
"number": 7,
"title": "Crash on startup",
"html_url": "https://github.com/octocat/hello-world/issues/7",
"body": None,
"state": "open",
"user": {"login": "octocat"},
"comments": 0,
}
],
}
GOOGLE_PSE_PAYLOAD = {
"items": [
{
"title": "Transformer architecture explained",
"link": "https://example.com/transformers",
"snippet": "The transformer architecture uses self-attention.",
"pagemap": {"metatags": [{"og:description": "Extended"}]},
}
]
}
GUARDIAN_PAYLOAD = {
"response": {
"status": "ok",
"results": [
{
"id": "technology/2024/jan/01/transformers",
"webUrl": "https://www.theguardian.com/technology/transformers",
"webTitle": "How transformers took over AI",
"webPublicationDate": "2024-01-01T09:00:00Z",
"sectionName": "Technology",
"fields": {
"headline": "How transformers took over AI",
"trailText": "The architecture behind modern AI.",
"byline": "A Reporter",
"body": "<p>Full article body.</p>",
},
"tags": [
{"type": "keyword", "webTitle": "Artificial intelligence"}
],
}
],
}
}
GUTENBERG_PAYLOAD = {
"count": 1,
"results": [
{
"id": 1342,
"title": "Pride and Prejudice",
"authors": [
{"name": "Austen, Jane", "birth_year": 1775, "death_year": 1817}
],
"subjects": ["England -- Fiction"],
"bookshelves": ["Best Books Ever Listings"],
"languages": ["en"],
"copyright": False,
"download_count": 100000,
"summaries": ["A classic novel of manners."],
"formats": {
"text/html": "https://www.gutenberg.org/ebooks/1342.html",
"image/jpeg": "https://www.gutenberg.org/cache/1342.jpg",
},
}
],
}
MOJEEK_PAYLOAD = {
"response": {
"status": "OK",
"results": [
{
"title": "Transformer architecture",
"url": "https://www.mojeek-result.example/transformers",
"desc": "An overview of the transformer architecture.",
"cats": "",
}
],
}
}
NASA_ADS_PAYLOAD = {
"response": {
"numFound": 1,
"docs": [
{
"bibcode": "2017arXiv170603762V",
"title": ["Attention Is All You Need"],
"abstract": "We propose the Transformer architecture.",
"year": "2017",
"pubdate": "2017-06-00",
"pub": "arXiv e-prints",
"bibstem": ["arXiv"],
"author": ["Vaswani, Ashish", "Shazeer, Noam"],
"citation_count": 100000,
"doi": ["10.48550/arXiv.1706.03762"],
"keyword": ["Computer Science - Computation and Language"],
}
],
}
}
OPENALEX_PAYLOAD = {
"meta": {"count": 1},
"results": [
{
"id": "https://openalex.org/W2963403868",
"display_name": "Attention Is All You Need",
"publication_year": 2017,
"publication_date": "2017-06-12",
"doi": "https://doi.org/10.48550/arxiv.1706.03762",
"primary_location": {
"source": {
"id": "https://openalex.org/S4306400194",
"display_name": "arXiv (Cornell University)",
"type": "repository",
"issn_l": None,
}
},
"authorships": [
{
"author": {
"id": "https://openalex.org/A1",
"display_name": "Ashish Vaswani",
},
"institutions": [
{
"id": "https://openalex.org/I1",
"ror": "https://ror.org/00f54p054",
"display_name": "Google",
}
],
}
],
"cited_by_count": 100000,
"open_access": {"is_oa": True},
"best_oa_location": {"pdf_url": "https://arxiv.org/pdf/1706.03762"},
"abstract_inverted_index": {
"The": [0],
"Transformer": [1],
"architecture": [2],
},
}
],
}
OPENLIBRARY_PAYLOAD = {
"num_found": 1,
"docs": [
{
"key": "/works/OL45804W",
"title": "Fantastic Mr Fox",
"author_name": ["Roald Dahl"],
"first_publish_year": 1970,
"publisher": ["Puffin"],
"subject": ["Foxes", "Children's fiction"],
"isbn": ["0140328726"],
"cover_i": 6498519,
"has_fulltext": True,
"ebook_access": "borrowable",
"ia": ["fantasticmrfox0000dahl"],
}
],
}
PAPERLESS_PAYLOAD = {
"count": 1,
"results": [
{
"id": 7,
"title": "Transformer maintenance report",
"content": "Full OCR text of the transformer maintenance report.",
"correspondent_name": "Acme Utilities",
"document_type_name": "Report",
"created": "2024-03-02T00:00:00Z",
"modified": "2024-03-03T00:00:00Z",
"archive_serial_number": 12,
"tags_list": ["utilities"],
"__search_hit__": {
"score": 4.5,
"rank": 0,
"highlights": (
'annual <span class="match">transformer</span> inspection'
),
},
}
],
}
PUBCHEM_ROUTES = {
"/autocomplete/compound/": {
"payload": {"dictionary_terms": {"compound": ["caffeine"]}}
},
"/cids/JSON": {"payload": {"IdentifierList": {"CID": [2519]}}},
"/property/": {
"payload": {
"PropertyTable": {
"Properties": [
{
"CID": 2519,
"MolecularFormula": "C8H10N4O2",
"MolecularWeight": "194.19",
"IUPACName": "1,3,7-trimethylpurine-2,6-dione",
"CanonicalSMILES": "CN1C=NC2=C1C(=O)N(C)C(=O)N2C",
"InChIKey": "RYYVLZVUVIJVGH-UHFFFAOYSA-N",
"XLogP": -0.1,
"HBondDonorCount": 0,
"HBondAcceptorCount": 6,
}
]
}
}
},
"/description/JSON": {
"payload": {
"InformationList": {
"Information": [
{
"CID": 2519,
"Description": "Caffeine is a methylxanthine alkaloid.",
}
]
}
}
},
"/synonyms/JSON": {
"payload": {
"InformationList": {
"Information": [{"CID": 2519, "Synonym": ["caffeine"]}]
}
}
},
}
PUBMED_PMID = "28905360"
PUBMED_ROUTES = {
"esearch.fcgi": {
"payload": {"esearchresult": {"idlist": [PUBMED_PMID], "count": "1"}}
},
"esummary.fcgi": {
"payload": {
"result": {
"uids": [PUBMED_PMID],
PUBMED_PMID: {
"uid": PUBMED_PMID,
"title": "Deep learning in medical imaging",
"pubdate": "2017 Dec",
"epubdate": "",
"source": "Radiology",
"authors": [{"name": "Chartrand G"}],
"lastauthor": "Chartrand G",
"fulljournalname": "Radiology",
"volume": "285",
"issue": "3",
"pages": "700-712",
"issn": "0033-8419",
"essn": "1527-1315",
"pubtype": ["Journal Article"],
"recordstatus": "PubMed - indexed for MEDLINE",
"lang": ["eng"],
"pmcrefcount": 10,
"articleids": [
{"idtype": "pubmed", "value": PUBMED_PMID},
{"idtype": "doi", "value": "10.1148/radiol.2017170077"},
],
},
}
}
},
"efetch.fcgi": {
"text": (
"<PubmedArticleSet><PubmedArticle><MedlineCitation>"
f"<PMID>{PUBMED_PMID}</PMID><Article><Abstract>"
"<AbstractText>Deep learning applied to radiology.</AbstractText>"
"</Abstract></Article></MedlineCitation></PubmedArticle>"
"</PubmedArticleSet>"
)
},
}
SCALESERP_PAYLOAD = {
"request_info": {"success": True, "cached": False},
"organic_results": [
{
"position": 1,
"title": "Transformer architecture guide",
"link": "https://scaleserp-result.example/guide",
"snippet": "A guide to the transformer architecture.",
}
],
}
SEARXNG_PAYLOAD = {
"query": QUERY,
"number_of_results": 1,
"results": [
{
"url": "https://searxng-result.example/transformers",
"title": "Transformer architecture",
"content": "Self-attention replaced recurrence.",
"engine": "duckduckgo",
"category": "general",
}
],
"unresponsive_engines": [],
}
SEMANTIC_SCHOLAR_PAYLOAD = {
"total": 1,
"data": [
{
"paperId": "204e3073870fae3d05bcbc2f6a8e263d9b72e776",
"title": "Attention Is All You Need",
"abstract": "We propose the Transformer, a model architecture.",
"year": 2017,
"venue": "NeurIPS",
"publicationVenue": {"name": "NeurIPS", "issn": "1049-5258"},
"authors": [{"name": "Ashish Vaswani"}],
"externalIds": {"DOI": "10.48550/arXiv.1706.03762"},
"url": (
"https://www.semanticscholar.org/paper/"
"204e3073870fae3d05bcbc2f6a8e263d9b72e776"
),
"tldr": {"text": "Transformers beat RNNs."},
}
],
}
SERPAPI_PAYLOAD = {
"organic_results": [
{
"position": 1,
"title": "Transformer architecture overview",
"link": "https://serpapi-result.example/overview",
"snippet": "Overview of the transformer architecture.",
"displayed_link": "serpapi-result.example",
}
]
}
SERPER_PAYLOAD = {
"organic": [
{
"title": "Transformer architecture — Serper",
"link": "https://serper-result.example/transformers",
"snippet": "Serper's snippet for the transformer architecture.",
"position": 1,
}
]
}
# ``/v1/search/`` returns Google web results under ``results``; each carries
# the page description (not ``snippet``) and a ``published`` date only for
# dated pages.
SERPLY_PAYLOAD = {
"results": [
{
"title": "Transformer architecture - Serply",
"link": "https://serply-result.example/transformers",
"description": "Serply's description of the transformer.",
"realPosition": 1,
}
]
}
# ``/v1/scholar/`` returns Google Scholar articles under ``articles``; the
# description is the author/venue/year line, and the DOI-bearing link is
# what the scientific pipeline's enrichment step keys on.
SERPLY_SCHOLAR_PAYLOAD = {
"articles": [
{
"title": "Attention Is All You Need",
"link": "https://doi.org/10.48550/arXiv.1706.03762",
"description": "A Vaswani, N Shazeer - NeurIPS, 2017",
"extras": {"citations": {"count": 100000}},
}
]
}
SOFYA_PAYLOAD = {
"results": [
{
"url": "https://sofya-result.example/transformers",
"title": "Transformer architecture — Sofya",
"description": "Sofya SERP description.",
"content": "Extracted page content about transformers.",
"published_date": "2024-02-02",
}
]
}
STACKEXCHANGE_PAYLOAD = {
"quota_remaining": 290,
"items": [
{
"question_id": 64517424,
"title": "How does the transformer architecture work?",
"link": "https://stackoverflow.com/questions/64517424",
"body": "<p>I am trying to understand self-attention.</p>",
"owner": {
"display_name": "curious",
"link": "https://stackoverflow.com/users/1",
"reputation": 4321,
},
"score": 12,
"view_count": 9000,
"answer_count": 2,
"is_answered": True,
"accepted_answer_id": 64517500,
"tags": ["machine-learning", "transformer"],
"creation_date": 1603000000,
"last_activity_date": 1603100000,
}
],
}
TAVILY_PAYLOAD = {
"results": [
{
"title": "Transformer architecture — Tavily",
"url": "https://tavily-result.example/transformers",
"content": "Tavily's snippet for the transformer architecture.",
"raw_content": "Full page text.",
"score": 0.93,
}
]
}
TINYFISH_PAYLOAD = {
"results": [
{
"url": "https://tinyfish-result.example/transformers",
"title": "Transformer architecture — TinyFish",
"snippet": "TinyFish snippet for the transformer architecture.",
"site_name": "tinyfish-result.example",
"position": 1,
}
]
}
# CDX returns a header row followed by one row per capture.
WAYBACK_URL = "https://example.com/page?version=1"
WAYBACK_CDX_PAYLOAD = [
["timestamp", "original", "statuscode", "mimetype"],
["20200101120000", WAYBACK_URL, "200", "text/html"],
]
WIKINEWS_TIMESTAMP = (datetime.now(UTC) - timedelta(days=3)).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
WIKINEWS_SEARCH_PAYLOAD = {
"query": {
"search": [
{
"pageid": 4321,
"title": "New transformer architecture unveiled",
"snippet": (
'A new <span class="searchmatch">transformer</span> '
"architecture was unveiled."
),
"timestamp": WIKINEWS_TIMESTAMP,
}
]
}
}
WIKINEWS_CONTENT_PAYLOAD = {
"query": {
"pages": {
"4321": {
"pageid": 4321,
"extract": (
"Researchers unveiled a new transformer architecture "
"at a conference."
),
"revisions": [{"timestamp": WIKINEWS_TIMESTAMP}],
}
}
}
}
ZENODO_PAYLOAD = {
"hits": {
"total": 1,
"hits": [
{
"id": 1234567,
"metadata": {
"title": "Transformer benchmark dataset",
"description": "<p>A dataset of transformer runs.</p>",
"doi": "10.5281/zenodo.1234567",
"publication_date": "2024-01-15",
"resource_type": {"type": "dataset", "title": "Dataset"},
"access_right": "open",
"keywords": ["transformer"],
"license": {"id": "cc-by-4.0"},
"creators": [{"name": "Doe, Jane"}],
},
"links": {
"self_html": "https://zenodo.org/records/1234567",
"doi": "https://doi.org/10.5281/zenodo.1234567",
},
}
],
}
}
# --------------------------------------------------------------------------
# Per-engine drivers
#
# Each driver constructs the real engine, installs a stub at the engine's
# own client seam, and returns the previews ``BaseSearchEngine.run`` would
# hand to the strategy layer (``search.snippets_only`` defaults to true, so
# previews *are* the results on the shipped configuration).
# --------------------------------------------------------------------------
def _drive_arxiv(tracker):
engine = _wire(
arxiv_engine.ArXivSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
# ArXivSearchEngine is routed through the typed
# ``utilities.arxiv_api.fetch_arxiv_results`` boundary rather than the
# vendored ``arxiv`` SDK directly, so that is the seam to stub.
with patch.object(
arxiv_engine, "fetch_arxiv_results", return_value=[ARXIV_PAPER]
):
return engine._get_previews(QUERY)
def _drive_brave(tracker):
engine = _wire(
brave_engine.BraveSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
# ``BraveSearch.run`` returns the results as a JSON *string*.
engine.engine = SimpleNamespace(run=lambda q: json.dumps(BRAVE_PAYLOAD))
return engine._get_previews(QUERY)
def _drive_elasticsearch(tracker):
with patch.object(es_engine, "Elasticsearch") as client_cls:
client = client_cls.return_value
client.info.return_value = {
"cluster_name": "test",
"version": {"number": "8.13.0"},
}
client.search.return_value = ELASTIC_PAYLOAD
engine = _wire(
es_engine.ElasticsearchSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
return engine._get_previews(QUERY)
def _drive_exa(tracker):
engine = _wire(
exa_engine.ExaSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
exa_engine, "safe_post", const_response(payload=EXA_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_federal_register(tracker):
engine = _wire(
federal_register_engine.FederalRegisterSearchEngine(
max_results=3,
llm=None,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
federal_register_engine,
"safe_get",
const_response(payload=FEDERAL_REGISTER_PAYLOAD),
):
return engine._get_previews(QUERY)
def _github_engine(tracker, search_type="repositories"):
return _wire(
github_engine.GitHubSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
search_type=search_type,
settings_snapshot=SNAPSHOT,
),
tracker,
)
def _drive_github(tracker):
engine = _github_engine(tracker)
with patch.object(
github_engine,
"safe_get",
const_response(
payload=GITHUB_REPO_PAYLOAD,
headers={"X-RateLimit-Remaining": "4999"},
),
):
return engine._get_previews(QUERY)
def _drive_google_pse(tracker):
stub = const_response(payload=GOOGLE_PSE_PAYLOAD)
# ``GooglePSESearchEngine.__init__`` calls ``_validate_connection()``,
# a live query against the Google API, so the stub has to be in place
# for construction as well as for the search itself.
with patch.object(pse_engine, "safe_get", stub):
engine = _wire(
pse_engine.GooglePSESearchEngine(
max_results=1,
llm=None,
api_key=API_KEY,
search_engine_id="cse-id",
settings_snapshot=SNAPSHOT,
),
tracker,
)
return engine._get_previews(QUERY)
def _drive_guardian(tracker):
engine = _wire(
guardian_engine.GuardianSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
adaptive_search=False,
optimize_queries=False,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
guardian_engine, "safe_get", const_response(payload=GUARDIAN_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_gutenberg(tracker):
engine = _wire(
gutenberg_engine.GutenbergSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with patch.object(
gutenberg_engine, "safe_get", const_response(payload=GUTENBERG_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_mojeek(tracker):
engine = _wire(
mojeek_engine.MojeekSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
mojeek_engine, "safe_get", const_response(payload=MOJEEK_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_nasa_ads(tracker):
engine = _wire(
nasa_engine.NasaAdsSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
nasa_engine, "safe_get", const_response(payload=NASA_ADS_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_openalex(tracker):
engine = _wire(
openalex_engine.OpenAlexSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with patch.object(
openalex_engine, "safe_get", const_response(payload=OPENALEX_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_openlibrary(tracker):
engine = _wire(
openlibrary_engine.OpenLibrarySearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with patch.object(
openlibrary_engine,
"safe_get",
const_response(payload=OPENLIBRARY_PAYLOAD),
):
return engine._get_previews(QUERY)
def _drive_paperless(tracker):
engine = _wire(
paperless_engine.PaperlessSearchEngine(
max_results=3,
llm=None,
api_url="http://paperless.invalid:8000",
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
paperless_engine, "safe_get", const_response(payload=PAPERLESS_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_pubchem(tracker):
engine = _wire(
pubchem_engine.PubChemSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with patch.object(
pubchem_engine, "safe_get", routed_response(PUBCHEM_ROUTES)
):
return engine._get_previews("caffeine")
def _drive_pubmed(tracker):
engine = _wire(
pubmed_engine.PubMedSearchEngine(
max_results=3,
llm=None,
optimize_queries=False,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
pubmed_engine, "safe_get", routed_response(PUBMED_ROUTES)
):
return engine._get_previews(QUERY)
def _drive_scaleserp(tracker):
engine = _wire(
scaleserp_engine.ScaleSerpSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
scaleserp_engine, "safe_get", const_response(payload=SCALESERP_PAYLOAD)
):
return engine._get_previews(QUERY)
def _build_searxng(tracker):
"""SearXNG's ``__init__`` probes the instance, so stub it to construct."""
with patch.object(
searxng_engine, "safe_get", const_response(text="<html/>")
):
engine = searxng_engine.SearXNGSearchEngine(
instance_url="http://searx.invalid:8080/",
result_format="json",
max_results=5,
llm=None,
settings_snapshot=SNAPSHOT,
)
return _wire(engine, tracker)
def _drive_searxng(tracker):
engine = _build_searxng(tracker)
assert engine._is_available
with patch.object(
searxng_engine, "safe_get", const_response(payload=SEARXNG_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_semantic_scholar(tracker):
engine = _wire(
s2_engine.SemanticScholarSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
engine.session = SimpleNamespace(
get=lambda *a, **kw: StubResponse(payload=SEMANTIC_SCHOLAR_PAYLOAD),
post=lambda *a, **kw: StubResponse(payload=SEMANTIC_SCHOLAR_PAYLOAD),
close=lambda: None,
)
return engine._get_previews(QUERY)
def _drive_serpapi(tracker):
engine = _wire(
serpapi_engine.SerpAPISearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
engine.engine = SimpleNamespace(results=lambda q: SERPAPI_PAYLOAD)
return engine._get_previews(QUERY)
def _drive_serper(tracker):
engine = _wire(
serper_engine.SerperSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
serper_engine, "safe_post", const_response(payload=SERPER_PAYLOAD)
):
return engine._get_previews(QUERY)
def _build_serply(tracker, search_type="web"):
return _wire(
serply_engine.SerplySearchEngine(
max_results=3,
search_type=search_type,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
def _drive_serply(tracker):
engine = _build_serply(tracker)
with patch.object(
serply_engine, "safe_get", const_response(payload=SERPLY_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_webz(tracker):
from local_deep_research.settings.manager import SettingsManager
from local_deep_research.web_search_engines.search_engine_factory import (
create_search_engine,
)
from local_deep_research.web_search_engines.engines import (
search_engine_webz as webz_engine,
)
snapshot = {
key: value
for key, value in SettingsManager(
db_session=None
).default_settings.items()
if key.startswith("search.engine.web.webz.")
}
snapshot.update(
{
"search.engine.web.webz.api_key": API_KEY,
"search.engine.library.enabled": False,
"policy.egress_scope": "public_only",
"search.tool": "webz",
}
)
engine = create_search_engine(
"webz", settings_snapshot=snapshot, max_results=3
)
assert isinstance(engine, webz_engine.WebzSearchEngine)
engine = _wire(engine, tracker)
# Official response-format row within the documented results[] envelope.
payload = {
"results": [
{
"score": 7.1,
"article": {
"article_id": "32445ab27737dcdad9119b67fb1d379908a41ecf",
"url": "https://www.example.com/edition/",
"title": "Article Title",
"published_at": "2026-08-27T07:07:00.000+03:00",
"summary": "Article's extractive summary",
},
"chunk": {"text": "Relevant chunk matching the query."},
"metadata": {
"domain": "example.com",
"source_type": "local_news",
},
}
]
}
with patch.object(
webz_engine, "safe_post", const_response(payload=payload)
):
previews = engine._get_previews(QUERY)
assert previews[0]["date"] == "2026-08-27T07:07:00.000+03:00"
assert previews[0]["source"] == "example.com"
return previews
def _drive_serply_scholar(tracker):
engine = _build_serply(tracker, search_type="scholar")
with patch.object(
serply_engine,
"safe_get",
const_response(payload=SERPLY_SCHOLAR_PAYLOAD),
):
return engine._get_previews(QUERY)
def _drive_sofya(tracker):
engine = _wire(
sofya_engine.SofyaSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
sofya_engine, "safe_post", const_response(payload=SOFYA_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_stackexchange(tracker):
engine = _wire(
stackexchange_engine.StackExchangeSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with patch.object(
stackexchange_engine,
"safe_get",
const_response(payload=STACKEXCHANGE_PAYLOAD),
):
return engine._get_previews(QUERY)
def _drive_tavily(tracker):
engine = _wire(
tavily_engine.TavilySearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
tavily_engine, "safe_post", const_response(payload=TAVILY_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_tinyfish(tracker):
engine = _wire(
tinyfish_engine.TinyFishSearchEngine(
max_results=3,
llm=None,
api_key=API_KEY,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(
tinyfish_engine, "safe_get", const_response(payload=TINYFISH_PAYLOAD)
):
return engine._get_previews(QUERY)
def _drive_wayback(tracker):
engine = _wire(
wayback_engine.WaybackSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with patch.object(
wayback_engine,
"safe_get",
side_effect=const_response(payload=WAYBACK_CDX_PAYLOAD),
) as archive_request:
previews = engine._get_previews(WAYBACK_URL)
archive_request.assert_called_once()
assert archive_request.call_args.args[0] == engine.cdx_api
assert archive_request.call_args.kwargs["params"]["url"] == WAYBACK_URL
return previews
def _wikinews_router(url, *_args, **kwargs):
params = kwargs.get("params") or {}
if params.get("list") == "search":
return StubResponse(payload=WIKINEWS_SEARCH_PAYLOAD)
return StubResponse(payload=WIKINEWS_CONTENT_PAYLOAD)
def _drive_wikinews(tracker):
engine = _wire(
wikinews_engine.WikinewsSearchEngine(
max_results=1,
llm=None,
adaptive_search=False,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with patch.object(wikinews_engine, "safe_get", _wikinews_router):
return engine._get_previews(QUERY)
def _drive_wikipedia(tracker):
with patch.object(wikipedia_engine.wikipedia, "set_lang"):
engine = _wire(
wikipedia_engine.WikipediaSearchEngine(
max_results=1, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with (
patch.object(
wikipedia_engine.wikipedia,
"search",
return_value=["Transformer (deep learning architecture)"],
),
patch.object(
wikipedia_engine.wikipedia,
"summary",
return_value=(
"A transformer is a deep learning architecture based on "
"the multi-head attention mechanism."
),
),
):
return engine._get_previews(QUERY)
def _drive_zenodo(tracker):
engine = _wire(
zenodo_engine.ZenodoSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
with patch.object(
zenodo_engine, "safe_get", const_response(payload=ZENODO_PAYLOAD)
):
return engine._get_previews(QUERY)
# --- local engines: no network at all -------------------------------------
def _fake_rag_hit():
"""One ``SearchResult`` as ``LibraryRAGService.search`` returns them."""
return SimpleNamespace(
chunk_id=1,
source_id="41",
document_title="Attention Is All You Need (local copy)",
text="The Transformer architecture dispenses with recurrence.",
distance=0.8,
metric="cosine",
metadata={"document_id": "41", "chunk_index": 3},
)
def _fake_rag_service(module):
"""Context-manager stub for ``LibraryRAGService``.
Both ``search_engine_library`` and ``search_engine_collection`` gate on
``has_indexed_documents``, which this unconfigured MagicMock attribute
already answers truthily. ``get_rag_stats`` is therefore dead
configuration for ``_drive_library`` and ``_drive_collection`` alike; the
stub is kept as-is rather than split per engine.
"""
service = MagicMock()
service.get_rag_stats.return_value = {"indexed_documents": 5}
service.search.return_value = [_fake_rag_hit()]
cm = MagicMock()
cm.__enter__.return_value = service
cm.__exit__.return_value = False
return patch.object(module, "LibraryRAGService", return_value=cm)
def _fake_rag_index_session(module):
"""Context-manager stub for ``get_user_db_session``."""
rag_index = SimpleNamespace(
embedding_model="all-MiniLM-L6-v2",
embedding_model_type=SimpleNamespace(value="sentence_transformers"),
chunk_size=1000,
chunk_overlap=200,
splitter_type="recursive",
text_separators=None,
normalize_vectors=True,
distance_metric="cosine",
index_type="flat",
)
session = MagicMock()
session.query.return_value.filter_by.return_value.first.return_value = (
rag_index
)
cm = MagicMock()
cm.__enter__.return_value = session
cm.__exit__.return_value = False
return patch.object(module, "get_user_db_session", return_value=cm)
def _drive_library(tracker):
engine = _wire(
library_engine.LibraryRAGSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
tracker,
)
library_service = MagicMock()
library_service.get_all_collections.return_value = [
{"id": "col-1", "name": "Papers"}
]
with (
patch.object(
library_engine, "LibraryService", return_value=library_service
),
_fake_rag_index_session(library_engine),
_fake_rag_service(library_engine),
):
return engine._get_previews(QUERY)
def _drive_collection(tracker):
with _fake_rag_index_session(collection_engine):
engine = _wire(
collection_engine.CollectionSearchEngine(
collection_id="col-1",
collection_name="Papers",
max_results=3,
llm=None,
settings_snapshot=SNAPSHOT,
),
tracker,
)
with (
_fake_rag_index_session(collection_engine),
_fake_rag_service(collection_engine),
):
return engine._get_previews(QUERY)
def _drive_retriever(tracker):
from langchain_core.documents import Document
doc = Document(
page_content="The Transformer architecture uses self-attention.",
metadata={
"title": "Team wiki: transformers",
"source": "https://wiki.example.org/transformers",
},
)
retriever = SimpleNamespace(invoke=lambda q: [doc])
engine = _wire(
retriever_engine.RetrieverSearchEngine(
retriever=retriever, name="team_wiki", max_results=3
),
tracker,
)
return engine._get_previews(QUERY)
# --------------------------------------------------------------------------
# The case table
# --------------------------------------------------------------------------
class Case:
"""One engine, its driver, and what a usable first result looks like."""
def __init__(self, name, drive, title, link, snippet_contains):
self.name = name
self.drive = drive
self.title = title
self.link = link
self.snippet_contains = snippet_contains
CASES = [
Case(
"arxiv",
_drive_arxiv,
"Attention Is All You Need",
"http://arxiv.org/abs/1706.03762v5",
"Transformer",
),
Case(
"brave",
_drive_brave,
"Transformer (deep learning architecture)",
"https://en.wikipedia.org/wiki/Transformer_(deep_learning)",
"deep learning architecture",
),
Case(
"elasticsearch",
_drive_elasticsearch,
"Internal Transformer Notes",
"https://intranet.example.org/docs/42",
"transformer",
),
Case(
"exa",
_drive_exa,
"Attention Is All You Need",
"https://arxiv.org/abs/1706.03762",
"based solely on attention",
),
Case(
"federal_register",
_drive_federal_register,
"Transformer Safety Standards",
(
"https://www.federalregister.gov/documents/2024/01/01/"
"2024-00001/transformer-safety-standards"
),
"transformer safety standards",
),
Case(
"github",
_drive_github,
"huggingface/transformers",
"https://github.com/huggingface/transformers",
"State-of-the-art",
),
Case(
"google_pse",
_drive_google_pse,
"Transformer architecture explained",
"https://example.com/transformers",
"self-attention",
),
Case(
"guardian",
_drive_guardian,
"How transformers took over AI",
"https://www.theguardian.com/technology/transformers",
"architecture behind modern AI",
),
Case(
"gutenberg",
_drive_gutenberg,
"Pride and Prejudice",
"https://www.gutenberg.org/ebooks/1342",
"classic novel of manners",
),
Case(
"mojeek",
_drive_mojeek,
"Transformer architecture",
"https://www.mojeek-result.example/transformers",
"overview of the transformer",
),
Case(
"nasa_ads",
_drive_nasa_ads,
"Attention Is All You Need",
"https://doi.org/10.48550/arXiv.1706.03762",
"Transformer architecture",
),
Case(
"openalex",
_drive_openalex,
"Attention Is All You Need",
"https://doi.org/10.48550/arxiv.1706.03762",
"Transformer architecture",
),
Case(
"openlibrary",
_drive_openlibrary,
"Fantastic Mr Fox",
"https://openlibrary.org/works/OL45804W",
"Roald Dahl",
),
Case(
"paperless",
_drive_paperless,
"Acme Utilities. Transformer maintenance report (Report) 2024",
"http://paperless.invalid:8000/documents/7/details",
"transformer",
),
Case(
"pubchem",
_drive_pubchem,
"caffeine",
"https://pubchem.ncbi.nlm.nih.gov/compound/2519",
"C8H10N4O2",
),
Case(
"pubmed",
_drive_pubmed,
"Deep learning in medical imaging",
f"https://pubmed.ncbi.nlm.nih.gov/{PUBMED_PMID}/",
"Deep learning applied to radiology.",
),
Case(
"scaleserp",
_drive_scaleserp,
"Transformer architecture guide",
"https://scaleserp-result.example/guide",
"guide to the transformer",
),
Case(
"searxng",
_drive_searxng,
"Transformer architecture",
"https://searxng-result.example/transformers",
"Self-attention replaced recurrence",
),
Case(
"semantic_scholar",
_drive_semantic_scholar,
"Attention Is All You Need",
(
"https://www.semanticscholar.org/paper/"
"204e3073870fae3d05bcbc2f6a8e263d9b72e776"
),
"model architecture",
),
Case(
"serpapi",
_drive_serpapi,
"Transformer architecture overview",
"https://serpapi-result.example/overview",
"Overview of the transformer",
),
Case(
"serper",
_drive_serper,
"Transformer architecture — Serper",
"https://serper-result.example/transformers",
"Serper's snippet",
),
Case(
"webz",
_drive_webz,
"Article Title",
"https://www.example.com/edition/",
"Relevant chunk matching the query.",
),
Case(
"serply",
_drive_serply,
"Transformer architecture - Serply",
"https://serply-result.example/transformers",
"Serply's description",
),
Case(
"sofya",
_drive_sofya,
"Transformer architecture — Sofya",
"https://sofya-result.example/transformers",
"Sofya SERP description",
),
Case(
"stackexchange",
_drive_stackexchange,
"How does the transformer architecture work?",
"https://stackoverflow.com/questions/64517424",
"self-attention",
),
Case(
"tavily",
_drive_tavily,
"Transformer architecture — Tavily",
"https://tavily-result.example/transformers",
"Tavily's snippet",
),
Case(
"tinyfish",
_drive_tinyfish,
"Transformer architecture — TinyFish",
"https://tinyfish-result.example/transformers",
"TinyFish snippet",
),
Case(
"wayback",
_drive_wayback,
f"Archive of {WAYBACK_URL} (2020-01-01 12:00:00)",
f"https://web.archive.org/web/20200101120000/{WAYBACK_URL}",
"Archived version",
),
Case(
"wikinews",
_drive_wikinews,
"New transformer architecture unveiled",
"https://en.wikinews.org/?curid=4321",
"transformer",
),
Case(
"wikipedia",
_drive_wikipedia,
"Transformer (deep learning architecture)",
(
"https://en.wikipedia.org/wiki/"
"Transformer_(deep_learning_architecture)"
),
"multi-head attention",
),
Case(
"zenodo",
_drive_zenodo,
"Transformer benchmark dataset",
"https://zenodo.org/records/1234567",
"dataset of transformer runs",
),
Case(
"library",
_drive_library,
"Attention Is All You Need (local copy)",
"/library/document/41/chunks?document_chunk_id=1#chunk-3",
"dispenses with recurrence",
),
Case(
"collection",
_drive_collection,
"Attention Is All You Need (local copy)",
"/library/document/41/chunks?document_chunk_id=1#chunk-3",
"dispenses with recurrence",
),
]
CASES_BY_NAME = {case.name: case for case in CASES}
# --------------------------------------------------------------------------
# 1. Registry: every offered engine must resolve and be constructible
# --------------------------------------------------------------------------
# Engines whose implementation module/class is in ENGINE_REGISTRY but which
# no settings file defines. ``search_config()`` builds its engine dict from
# ``search.engine.web.*`` settings and only *decorates* the entries it finds
# with the registry's module/class, so an engine with no settings never
# enters the dict and ``create_search_engine`` fails closed on it with
# "Unknown search engine". This entry is therefore unreachable from the UI,
# the API and the LangGraph tool list alike.
REGISTRY_ENGINES_WITHOUT_SETTINGS = {"guardian"}
def _shipped_engine_names():
"""Engine names that the packaged default settings actually define."""
defaults_dir = (
Path(importlib.import_module("local_deep_research").__file__).parent
/ "defaults"
)
names = set()
for path in defaults_dir.rglob("*.json"):
try:
data = json.loads(path.read_text())
except (ValueError, OSError):
continue
if not isinstance(data, dict):
continue
for key in data:
if isinstance(key, str) and key.startswith("search.engine.web."):
parts = key.split(".")
if len(parts) > 4:
names.add(parts[3])
return names
class TestRegistryIsWiredUp:
@pytest.mark.parametrize("name", sorted(ENGINE_REGISTRY))
def test_entry_resolves_to_a_search_engine_class(self, name):
"""Every registry entry names a real ``BaseSearchEngine`` subclass."""
entry = ENGINE_REGISTRY[name]
cls = get_safe_module_class(entry.module_path, entry.class_name)
assert issubclass(cls, BaseSearchEngine)
assert cls.__name__ == entry.class_name
@pytest.mark.parametrize("name", sorted(ENGINE_REGISTRY))
def test_entry_implements_the_preview_phase(self, name):
"""``_get_previews`` is the abstract method ``run()`` drives."""
entry = ENGINE_REGISTRY[name]
cls = get_safe_module_class(entry.module_path, entry.class_name)
assert cls._get_previews is not BaseSearchEngine._get_previews, (
f"{name} does not override _get_previews"
)
@pytest.mark.parametrize(
"name",
sorted(set(ENGINE_REGISTRY) - REGISTRY_ENGINES_WITHOUT_SETTINGS),
)
def test_engine_with_settings_has_a_success_path_case(self, name):
"""Guard: every UI-offered engine is exercised below.
Without this, adding an engine to the registry silently adds an
untested one -- the exact gap this file exists to close.
"""
assert name in CASES_BY_NAME, (
f"engine '{name}' is offered in the UI but has no success-path "
"case in this file"
)
def test_guardian_is_unreachable_through_the_factory(self):
"""DEFECT: a registry entry no settings file defines.
``search_config()`` is settings-derived: it reads
``search.engine.web.*`` and only *decorates* the names it finds
with the registry's module/class. An engine with no shipped
settings therefore never enters the dict, and
``create_search_engine`` fails closed on it with "Unknown search
engine". ``guardian`` is a dead registry row --
unreachable from the UI, the API and the agent's tool list. A fix
either ships settings for them or drops the rows.
"""
shipped = _shipped_engine_names()
# Sanity: the scan actually found the shipped catalogue.
assert {"wikipedia", "pubmed", "searxng"} <= shipped
assert set(ENGINE_REGISTRY) - shipped == (
REGISTRY_ENGINES_WITHOUT_SETTINGS
)
class TestEnginesThatCannotBeConstructed:
"""Registry rows whose class cannot be built without a live backend."""
def test_elasticsearch_raises_when_the_cluster_is_down(self, inert_tracker):
"""Elasticsearch probes the cluster inside ``__init__``.
The probe raises ``ConnectionError``, which the factory swallows
into a ``None`` engine. That is deliberate (there is an
``is_available`` TCP pre-probe to keep it out of the tool list),
but it does mean the class is unconstructible without a live
cluster -- pinned so a future refactor cannot make it silent.
"""
with patch.object(es_engine, "Elasticsearch") as client_cls:
client_cls.return_value.info.side_effect = OSError("refused")
with pytest.raises(ConnectionError, match="Could not connect"):
es_engine.ElasticsearchSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
)
def test_google_pse_spends_a_live_api_call_to_be_constructed(self):
"""DEFECT: Google PSE probes the billed API inside ``__init__``.
``_validate_connection()`` issues a real ``customsearch/v1``
request and re-raises whatever it hits, so the engine cannot be
constructed while the network (or the quota) is unavailable --
the factory turns that into a ``None`` engine. Google PSE has a
100 queries/day free tier, and the LangGraph agent builds a fresh
engine per tool call, so the probe also spends quota that returns
no results to the user. Constructors should not do I/O; the
existing ``is_available`` hook is the place for a liveness check.
"""
with patch.object(
pse_engine,
"safe_get",
side_effect=requests.ConnectionError("refused"),
):
with pytest.raises(requests.RequestException):
pse_engine.GooglePSESearchEngine(
max_results=1,
llm=None,
api_key=API_KEY,
search_engine_id="cse-id",
settings_snapshot=SNAPSHOT,
# Keep the probe's own retry back-off out of the
# suite's wall clock; it defaults to 3 attempts with
# exponential sleeps.
max_retries=1,
retry_delay=0,
)
def test_searxng_unreachable_instance_preserves_failure(
self, inert_tracker
):
"""An unreachable instance keeps its failure alongside an empty public result."""
with patch.object(
searxng_engine,
"safe_get",
side_effect=requests.ConnectionError("refused"),
):
engine = searxng_engine.SearXNGSearchEngine(
max_results=5, llm=None, settings_snapshot=SNAPSHOT
)
engine.rate_tracker = inert_tracker
assert engine._is_available is False
assert engine.last_search_failure is not None
# No search_url attribute is ever set on the unavailable path.
assert not hasattr(engine, "search_url")
assert engine.run(QUERY) == []
# --------------------------------------------------------------------------
# 2. Success path: realistic upstream response -> usable normalised results
# --------------------------------------------------------------------------
@pytest.mark.parametrize("case", CASES, ids=[c.name for c in CASES])
class TestEngineSuccessPath:
def test_returns_normalised_results(self, case, inert_tracker):
"""A realistic upstream document becomes a usable preview.
``search.snippets_only`` ships as true, so on the default
configuration these previews *are* what reaches the citation
handler -- ``_get_full_content`` is never called.
"""
previews = case.drive(inert_tracker)
assert previews, (
f"{case.name} returned NO results for a realistic upstream response"
)
first = previews[0]
assert first.get("title") == case.title
assert first.get("link") == case.link
def test_snippet_is_text_the_synthesis_llm_can_read(
self, case, inert_tracker
):
"""``BaseCitationHandler._create_documents`` reads this field.
It takes ``full_content`` then ``snippet`` as the document body.
A missing or non-string value means the LLM is handed a blank
source under a real citation number.
"""
first = case.drive(inert_tracker)[0]
body = first.get("full_content", first.get("snippet", ""))
assert isinstance(body, str), (
f"{case.name} produced a non-str document body: {body!r}"
)
assert body.strip(), f"{case.name} produced an empty document body"
assert case.snippet_contains in body
def test_results_survive_link_extraction(self, case, inert_tracker):
"""Nothing is silently discarded by the real link extractor.
``extract_links_from_search_results`` swallows per-result
exceptions with only a log line, so a shape it dislikes vanishes
without any signal at all.
"""
previews = case.drive(inert_tracker)
links = extract_links_from_search_results(previews)
assert len(links) == len(previews), (
f"{case.name}: {len(previews) - len(links)} of {len(previews)} "
"results were dropped by extract_links_from_search_results"
)
assert links[0]["url"] == case.link
assert links[0]["title"] == case.title
def test_results_reach_the_report_as_numbered_hyperlinks(
self, case, inert_tracker
):
"""End of the chain: a Sources block and a clickable ``[1]``.
Indices are assigned the way ``_create_documents`` assigns them
(``str(i + 1)``), then both real renderers are run.
"""
links = extract_links_from_search_results(case.drive(inert_tracker))
for i, link in enumerate(links, start=1):
link["index"] = str(i)
sources_markdown = format_links_to_markdown(links)
assert sources_markdown.strip(), (
f"{case.name} produced an empty Sources block"
)
assert case.title.split(" (")[0][:20] in sources_markdown
formatter = CitationFormatter(mode=CitationMode.NUMBER_HYPERLINKS)
rendered = formatter.apply_inline_hyperlinks(
"The evidence is clear [1].", links
)
assert f"[[1]]({case.link})" in rendered, (
f"{case.name}: citation [1] was not hyperlinked; got {rendered!r}"
)
class TestSerplyScholarMode:
"""Serply's ``scholar`` vertical is the one case in the table above
that changes pipeline mid-class: ``is_scientific`` is set per instance,
so the same engine routes Scholar articles through the DOI enrichment
step and the journal reputation filter while web and news stay generic.
"""
def test_scholar_previews_are_usable(self, inert_tracker):
previews = _drive_serply_scholar(inert_tracker)
assert previews
first = previews[0]
assert first["title"] == "Attention Is All You Need"
assert first["link"] == "https://doi.org/10.48550/arXiv.1706.03762"
assert "NeurIPS, 2017" in first["snippet"]
links = extract_links_from_search_results(previews)
assert len(links) == len(previews)
assert links[0]["url"] == first["link"]
def test_scholar_results_reach_the_scientific_pipeline(
self, inert_tracker, monkeypatch
):
"""``run()`` hands Scholar previews to the DOI enrichment step.
Web mode must not: the enrichment call costs a network round trip
and only pays off for results that carry a DOI.
"""
seen = []
def fake_enrich(results, email=None, api_key=None, **kwargs):
seen.append([dict(r) for r in results])
return results
monkeypatch.setattr(
"local_deep_research.utilities.openalex_enrichment."
"enrich_results_with_source_ids",
fake_enrich,
)
scholar = _build_serply(inert_tracker, search_type="scholar")
scholar.programmatic_mode = True
assert scholar.is_scientific is True
with patch.object(
serply_engine,
"safe_get",
const_response(payload=SERPLY_SCHOLAR_PAYLOAD),
):
results = scholar.run(QUERY)
assert len(seen) == 1, "scholar results never reached enrichment"
assert seen[0][0]["link"] == (
"https://doi.org/10.48550/arXiv.1706.03762"
)
assert results and results[0]["title"] == "Attention Is All You Need"
web = _build_serply(inert_tracker)
web.programmatic_mode = True
assert web.is_scientific is False
with patch.object(
serply_engine, "safe_get", const_response(payload=SERPLY_PAYLOAD)
):
web.run(QUERY)
assert len(seen) == 1, "web results must skip DOI enrichment"
# --------------------------------------------------------------------------
# 3. Silent-drop defects in the downstream chain
# --------------------------------------------------------------------------
class TestDownstreamSilentDrops:
def test_non_str_index_discards_the_entire_result(self):
"""DEFECT: an int ``index`` deletes the whole source.
``extract_links_from_search_results`` calls ``index.strip()``
unconditionally, inside a bare ``except Exception: continue``.
A non-``str`` index therefore does not merely lose the index --
it discards title, URL, DOI and all, leaving only a log line.
Note the sibling renderer ``format_links_to_markdown`` already
coerces with ``str(i)``, so the two disagree; the strip() should
be ``str(index).strip()``.
"""
good = {
"title": "Kept",
"link": "https://example.org/kept",
"index": "1",
}
broken = dict(good, title="Lost", link="https://example.org/lost")
broken["index"] = 2 # int, as strategy enumeration would produce
links = extract_links_from_search_results([good, broken])
assert len(links) == 1
assert links[0]["url"] == "https://example.org/kept"
# The renderer that *does* coerce proves the int index is
# otherwise perfectly usable -- the drop is gratuitous.
assert "https://example.org/lost" in format_links_to_markdown(
[dict(broken)]
)
def test_retriever_results_are_dropped_for_want_of_a_link_key(
self, inert_tracker
):
"""DEFECT: ``RetrieverSearchEngine`` emits ``url``, not ``link``.
``extract_links_from_search_results`` reads ``result["link"]``
only, so every LangChain-retriever result is discarded from the
Sources block. The LangGraph strategy happens to paper over this
(``_add_tracked_result`` copies ``url`` to ``link``), but
``source_based``, ``focused_iteration`` and
``topic_organization`` extend ``all_links_of_system`` with the
raw engine dicts and then call this function -- so on those
strategies a registered retriever contributes zero citations
while still feeding text to the LLM. Compare
``search_engine_paperless.py``, which sets both keys and comments
that ``link`` exists "for compatibility with search utilities".
"""
previews = _drive_retriever(inert_tracker)
assert previews, "retriever returned no results"
assert previews[0]["url"] == "https://wiki.example.org/transformers"
assert "link" not in previews[0]
assert extract_links_from_search_results(previews) == []
def test_github_null_description_yields_a_non_str_snippet(
self, inert_tracker
):
"""DEFECT: ``"description": null`` becomes ``snippet: None``.
GitHub returns an explicit JSON null for repositories with no
description -- a large share of search hits. ``repo.get(
"description", "No description provided")`` only fires the
default when the *key* is absent, so the placeholder never
applies and ``snippet`` is ``None``. Downstream,
``_create_documents`` passes that straight into
``Document(page_content=...)``. Should be
``repo.get("description") or "No description provided"``.
"""
engine = _github_engine(inert_tracker)
with patch.object(
github_engine,
"safe_get",
const_response(
payload=GITHUB_REPO_NULL_DESCRIPTION,
headers={"X-RateLimit-Remaining": "4999"},
),
):
previews = engine._get_previews(QUERY)
assert len(previews) == 1
assert previews[0]["snippet"] is None
def test_github_issue_with_null_body_loses_the_whole_batch(
self, inert_tracker
):
"""DEFECT: one body-less issue kills the entire issues search.
``_format_issue_preview`` evaluates
``len(issue.get("body", ""))`` -- ``len(None)`` on the very
common body-less issue. The formatting loop in ``_get_previews``
has no per-item guard, so the TypeError escapes and
``BaseSearchEngine.run`` converts the whole search into an empty
list. Same failure family as the malformed-PubMed-article batch
loss, in a different engine.
"""
engine = _github_engine(inert_tracker, search_type="issues")
# GitHubSearchEngine does not accept programmatic_mode, so switch
# it after construction: run() would otherwise write a metrics row.
engine._configure_programmatic_mode(True)
engine.rate_tracker = inert_tracker
stub = const_response(
payload=GITHUB_ISSUE_NULL_BODY,
headers={"X-RateLimit-Remaining": "4999"},
)
with patch.object(github_engine, "safe_get", stub):
with pytest.raises(TypeError):
engine._get_previews(QUERY)
with patch.object(github_engine, "safe_get", stub):
# Through run(), the failure is laundered into "no results".
assert engine.run(QUERY) == []
def test_wayback_returns_nothing_for_a_plain_research_question(
self, inert_tracker
):
"""Wayback only looks up supplied URLs; topics need discovery first.
The same adapter and archive payload produce usable results for
explicit URLs, while plain topics make no archive request.
"""
engine = _wire(
wayback_engine.WaybackSearchEngine(
max_results=3, llm=None, settings_snapshot=SNAPSHOT
),
inert_tracker,
)
with patch.object(
wayback_engine,
"safe_get",
const_response(payload=WAYBACK_CDX_PAYLOAD),
):
assert engine._get_previews(QUERY) == []
# ... while the same engine and the same stub work fine when the
# query happens to be a URL, so the stub is not the reason.
assert _drive_wayback(inert_tracker)