~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
"""Port of the deleted Flask suite ``tests/web/routes/test_unified_search_routes.py``.
The originals drove the Flask blueprint through ``test_request_context`` +
unwrapped handlers. The handlers now live in
``src/local_deep_research/web/routers/unified_search.py`` and are driven here
through ``TestClient`` with ``require_auth`` overridden — the pattern already
established by ``test_unified_search_keyword_fallback.py`` and
``tests/security/test_pagination_bounds.py``.
What is NOT re-ported here, because a branch successor already pins it and was
proven to go red under a mutation of the guard:
* the five ``display_title = title or filename or "Untitled"`` cases and the
``Document.filename`` projection ->
``test_unified_search_keyword_fallback.py``.
* ``?limit=notanint`` -> 400 and the 1-char minimum status code ->
``test_unified_search_router.py``. The 1-char case's *message* ("at least")
is not pinned there, so it is re-ported below.
* the full-precision sort within one collection ->
``test_unified_search_ranking_precision.py``. Its cross-collection
counterpart and the cross-collection *dedup* tie-break are NOT covered
there, so both are re-ported below.
Everything else in the original file had no successor at all.
Rate-limit note: the unified-search routes carry a 60/minute shared bucket
keyed by ``_user_key``, which falls back to the client IP when no real session
cookie is present (which is the case under ``dependency_overrides``). Each
test therefore sends a unique ``X-Forwarded-For`` so the module cannot starve
its own bucket — ``rate_limit._is_trusted_peer`` documents this as the
supported test escape hatch.
"""
import itertools
from contextlib import contextmanager
from datetime import datetime
from unittest.mock import MagicMock, patch
import pytest
from fastapi.testclient import TestClient
from local_deep_research.web.routers import unified_search
UPDATED = datetime(2026, 7, 1, 12, 0, 0)
_ip_counter = itertools.count(1)
@pytest.fixture
def search_client(app):
"""TestClient for the real app with ``require_auth`` short-circuited."""
from local_deep_research.web.dependencies.auth import require_auth
app.dependency_overrides[require_auth] = lambda: "testuser"
client = TestClient(app, raise_server_exceptions=False)
# Unique source IP per test -> its own rate-limit bucket.
n = next(_ip_counter)
client.headers["X-Forwarded-For"] = f"10.44.{n // 250}.{n % 250 + 1}"
try:
yield client
finally:
app.dependency_overrides.pop(require_auth, None)
def _session_cm(sessions):
"""Context-manager factory yielding one mock session per ``with`` use."""
iterator = iter(sessions)
@contextmanager
def fake_session(username=None, password=None, **kwargs):
yield next(iterator)
return fake_session
# ---------------------------------------------------------------------------
# Keyword leg
# ---------------------------------------------------------------------------
class TestKeywordSearch:
def _call(self, client, rows, query_string="q=alpha"):
session_mock = MagicMock()
(
session_mock.query.return_value.join.return_value.filter.return_value.order_by.return_value.limit.return_value.all
).return_value = rows
with patch(
"local_deep_research.database.session_context.get_user_db_session",
_session_cm([session_mock]),
):
response = client.get(f"/library/search/api/keyword?{query_string}")
return response.json(), response.status_code, session_mock
def test_happy_path_payload_shape_and_url_computation(self, search_client):
# Row shape mirrors the SELECT: (id, title, filename, preview,
# source_type, updated_at, research_id, instr_match_pos). Final
# element = instr() match position (0 = title-only match ->
# head-anchored preview, no ellipsis).
rows = [
("n1", "My note", None, "note preview", "note", UPDATED, None, 0),
(
"r1",
"Run report",
None,
"report preview",
"research_report",
UPDATED,
"res-9",
0,
),
(
"r2",
"Orphan report",
None,
"p",
"research_report",
UPDATED,
None,
0,
),
("u1", "Uploaded PDF", None, "p", "user_upload", UPDATED, None, 0),
]
payload, status, _ = self._call(search_client, rows)
assert status == 200, payload
assert payload["success"] is True
assert payload["count"] == 4
results = payload["results"]
assert results[0] == {
"id": "n1",
"title": "My note",
"content_preview": "note preview",
"source_type": "note",
"updated_at": UPDATED.isoformat(),
"research_id": None,
"url": "/notes/n1",
}
# research_report WITH a research_id links to the results page ...
assert results[1]["url"] == "/results/res-9"
# ... without one it falls back to the library document page.
assert results[2]["url"] == "/library/document/r2"
# Everything else links to the library document page.
assert results[3]["url"] == "/library/document/u1"
def test_deep_content_match_preview_is_marked_as_mid_document(
self, search_client
):
# When the match sits past the preview window's context lead-in,
# the SQL slices a window around it and the route marks the cut
# with a leading ellipsis so the card doesn't read as the
# document's opening text.
deep_pos = unified_search.PREVIEW_CONTEXT + 500
rows = [
(
"d1",
"Doc",
None,
"text around the match",
"note",
UPDATED,
None,
deep_pos,
),
# Match inside the lead-in zone -> head preview, no marker.
("d2", "Doc2", None, "head text", "note", UPDATED, None, 10),
]
payload, status, _ = self._call(search_client, rows)
assert status == 200, payload
assert payload["results"][0]["content_preview"] == (
"…text around the match"
)
assert payload["results"][1]["content_preview"] == "head text"
def test_single_char_query_message_names_the_minimum(self, search_client):
# Mirrors the page JS's 2-char minimum: a 1-char query is a full
# content scan for a near-meaningless match set.
# (test_unified_search_router.py pins the 400 status; only the
# message is re-ported here.)
payload, status, _ = self._call(search_client, [], query_string="q=x")
assert status == 400
assert payload["success"] is False
assert "at least" in payload["error"]
def test_query_over_cap_maps_to_400(self, search_client):
long_q = "x" * (unified_search.MAX_SEARCH_LEN + 1)
payload, status, _ = self._call(
search_client, [], query_string=f"q={long_q}"
)
assert status == 400, payload
assert payload["success"] is False
assert "maximum length" in payload["error"]
def test_empty_query_maps_to_400(self, search_client):
payload, status, _ = self._call(search_client, [], query_string="q=")
assert status == 400, payload
assert payload["success"] is False
class TestKeywordStatusFilter:
"""Regression: the keyword leg filters ``Document.status == completed``
like every other library search, so failed/in-progress downloads don't
surface. Output-invisible through the response (the mocked session
returns whatever rows the test hands it), so it is verified structurally
by compiling the filter clause the handler actually built."""
def test_status_completed_filter_applied(self, search_client):
session_mock = MagicMock()
captured = {}
def capture_filter(*args):
captured["clauses"] = args
m = MagicMock()
m.order_by.return_value.limit.return_value.all.return_value = []
return m
session_mock.query.return_value.join.return_value.filter = (
capture_filter
)
with patch(
"local_deep_research.database.session_context.get_user_db_session",
_session_cm([session_mock]),
):
response = search_client.get("/library/search/api/keyword?q=alpha")
assert response.status_code == 200, response.text[:300]
# Compile with literal binds so the bound value (invisible in a bare
# str()) is part of what's asserted -- a bare `str(clause)` renders
# the bind as a "?" placeholder, so this would stay green even if the
# production filter were flipped to `Document.status != "completed"`
# or used a different literal.
assert "clauses" in captured, (
"the handler never reached .filter(...) -- the test asserts "
"nothing as written"
)
rendered = " ".join(
str(c.compile(compile_kwargs={"literal_binds": True}))
for c in captured["clauses"]
)
assert "documents.status = 'completed'" in rendered, rendered
# Reject the inverted comparison explicitly: "!=" is not a substring
# of "=" alone, but guard it directly in case the equality literal
# above is ever loosened to a substring match.
assert "documents.status != 'completed'" not in rendered, rendered
# ---------------------------------------------------------------------------
# Semantic leg
# ---------------------------------------------------------------------------
class TestSemanticSearch:
def _call(
self,
client,
collections,
engine_results=None,
engine_side_effect=None,
doc_rows=None,
indexed_rows=None,
query_string="q=alpha",
):
"""Drive the semantic handler with mocked sessions + engine.
``collections`` seeds the collection lookup as
(id, name, collection_type) tuples; ``indexed_rows`` seeds the
current-RAGIndex name lookup as 1-tuples of 'collection_<id>';
``doc_rows`` seeds the Document enrichment query (id,
source_type_name, research_id tuples).
"""
collections_session = MagicMock()
# Two lookups on the first session: all collections (plain .all())
# and the current RAG index names (.filter().all()).
collections_session.query.return_value.all.return_value = collections
(
collections_session.query.return_value.filter.return_value.all
).return_value = indexed_rows or []
enrich_session = MagicMock()
(
enrich_session.query.return_value.join.return_value.filter.return_value.all
).return_value = doc_rows or []
engine = MagicMock()
if engine_side_effect is not None:
engine.search.side_effect = engine_side_effect
else:
engine.search.return_value = engine_results or []
with (
patch(
"local_deep_research.database.session_context.get_user_db_session",
_session_cm([collections_session, enrich_session]),
),
patch(
"local_deep_research.web_search_engines.engines."
"search_engine_collection.CollectionSearchEngine",
return_value=engine,
) as engine_cls,
):
response = client.get(
f"/library/search/api/semantic?{query_string}"
)
return (
(response.json(), response.status_code),
engine_cls,
engine,
)
def test_no_system_collections_returns_empty_without_engine(
self, search_client
):
(payload, status), engine_cls, _ = self._call(
search_client, collections=[]
)
assert status == 200, payload
assert payload == {
"success": True,
"query": "alpha",
"results": [],
"count": 0,
}
# SKIPPED, not created -- and no engine was even constructed.
engine_cls.assert_not_called()
def test_searches_only_existing_collections(self, search_client):
# Only two of the three system collections exist (e.g. the notes
# collection is lazily created and this user has no notes yet).
collections = [
("lib-1", "Library", "default_library"),
("hist-1", "Research History", "research_history"),
]
(payload, status), engine_cls, engine = self._call(
search_client, collections=collections
)
assert status == 200, payload
assert payload["success"] is True
assert engine_cls.call_count == 2
constructed_ids = [
call.kwargs["collection_id"] for call in engine_cls.call_args_list
]
assert constructed_ids == ["lib-1", "hist-1"]
assert engine.search.call_count == 2
def test_indexed_user_collection_is_searched_unindexed_is_not(
self, search_client
):
# The keyword leg covers every document, so the semantic leg must
# cover any collection the user actually indexed -- pre-fix, a PDF
# living only in an indexed user collection was keyword-searchable
# but never AI-searchable ("AI Only" returned nothing, silently).
collections = [
("lib-1", "Library", "default_library"),
("uc-1", "My PDFs", "user_collection"),
("uc-2", "Unindexed", "user_collection"),
]
(payload, status), engine_cls, _ = self._call(
search_client,
collections=collections,
indexed_rows=[("collection_uc-1",)],
)
assert status == 200, payload
constructed_ids = [
call.kwargs["collection_id"] for call in engine_cls.call_args_list
]
# System collection always; indexed user collection joins it; the
# unindexed one is skipped (its engine could only return []).
assert constructed_ids == ["lib-1", "uc-1"]
def test_merges_hits_by_similarity_and_computes_urls(self, search_client):
collections = [("lib-1", "Library", "default_library")]
engine_results = [
{
"relevance_score": 0.91,
"title": "A note",
"snippet": "note snippet",
"metadata": {"document_id": "n1"},
},
# Duplicate document (second chunk, lower score) -- deduped.
{
"relevance_score": 0.72,
"title": "A note",
"snippet": "other chunk",
"metadata": {"document_id": "n1"},
},
{
"relevance_score": 0.55,
"title": "A report",
"snippet": "report snippet",
"metadata": {"source_id": "r1"},
},
# Below min_similarity (default 0.25) -- dropped.
{
"relevance_score": 0.10,
"title": "Noise",
"snippet": "",
"metadata": {"document_id": "x1"},
},
]
# x1 IS present in the Document lookup on purpose: the Flask
# original omitted it, so the "below min_similarity is dropped"
# leg passed vacuously (x1 was discarded as a FAISS orphan whether
# or not the threshold ran). Listing it here makes deleting the
# `if score < min_similarity: continue` guard actually go red.
doc_rows = [
("n1", "note", None),
("r1", "research_report", "res-9"),
("x1", "user_upload", None),
]
(payload, status), _, _ = self._call(
search_client,
collections=collections,
engine_results=engine_results,
doc_rows=doc_rows,
)
assert status == 200, payload
results = payload["results"]
assert [r["id"] for r in results] == ["n1", "r1"]
assert results[0] == {
"id": "n1",
"title": "A note",
"content_preview": "note snippet",
"similarity": 0.91,
"source_type": "note",
"url": "/notes/n1",
}
assert results[1]["url"] == "/results/res-9"
assert results[1]["source_type"] == "research_report"
def test_sorts_by_full_precision_before_rounding_response(
self, search_client
):
# Cross-collection variant: the two scores arrive from DIFFERENT
# engines (one call each), so the merge -- not a within-collection
# sort -- is what has to preserve full precision.
collections = [
("lib-1", "Library", "default_library"),
("notes-1", "Notes", "notes"),
]
lower_score = {
"relevance_score": 0.90041,
"title": "Lower score",
"snippet": "first collection",
"metadata": {"document_id": "lower"},
}
higher_score = {
"relevance_score": 0.90049,
"title": "Higher score",
"snippet": "second collection",
"metadata": {"document_id": "higher"},
}
(payload, status), _, _ = self._call(
search_client,
collections=collections,
engine_side_effect=[[lower_score], [higher_score]],
doc_rows=[("lower", "note", None), ("higher", "note", None)],
)
assert status == 200, payload
assert [r["id"] for r in payload["results"]] == ["higher", "lower"]
assert [r["similarity"] for r in payload["results"]] == [0.9, 0.9]
def test_dedup_keeps_full_precision_best_hit_across_collections(
self, search_client
):
collections = [
("lib-1", "Library", "default_library"),
("notes-1", "Notes", "notes"),
]
lower_score = {
"relevance_score": 0.90041,
"title": "Lower-scoring duplicate",
"snippet": "first collection",
"metadata": {"document_id": "shared"},
}
higher_score = {
"relevance_score": 0.90049,
"title": "Higher-scoring duplicate",
"snippet": "second collection",
"metadata": {"document_id": "shared"},
}
(payload, status), _, _ = self._call(
search_client,
collections=collections,
engine_side_effect=[[lower_score], [higher_score]],
doc_rows=[("shared", "note", None)],
)
assert status == 200, payload
assert payload["results"] == [
{
"id": "shared",
"title": "Higher-scoring duplicate",
"content_preview": "second collection",
"similarity": 0.9,
"source_type": "note",
"url": "/notes/shared",
}
]
def test_faiss_orphan_hits_are_dropped(self, search_client):
# A hit whose Document row is gone (deleted doc, lingering vectors)
# must not surface as a dead link.
collections = [("lib-1", "Library", "default_library")]
engine_results = [
{
"relevance_score": 0.9,
"title": "Ghost",
"snippet": "s",
"metadata": {"document_id": "gone-1"},
}
]
(payload, status), _, _ = self._call(
search_client,
collections=collections,
engine_results=engine_results,
doc_rows=[],
)
assert status == 200, payload
assert payload["results"] == []
def test_engine_failure_propagates_as_generic_500(self, search_client):
(payload, status), _, _ = self._call(
search_client,
collections=[("lib-1", "Library", "default_library")],
engine_side_effect=RuntimeError(
"FAISS index corrupt: /secret/path"
),
)
assert status == 500, payload
assert payload["success"] is False
# handle_api_error returns a generic message -- no internal details.
assert "internal error" in payload["error"].lower()
assert "FAISS" not in payload["error"]
def test_query_over_cap_maps_to_400(self, search_client):
long_q = "x" * (unified_search.MAX_SEARCH_LEN + 1)
(payload, status), engine_cls, _ = self._call(
search_client,
collections=[("lib-1", "Library", "default_library")],
query_string=f"q={long_q}",
)
assert status == 400, payload
assert payload["success"] is False
engine_cls.assert_not_called()
@pytest.mark.parametrize("bad", ["nan", "inf", "-inf"])
def test_non_finite_min_similarity_maps_to_400(self, search_client, bad):
# float("nan") does not raise, and max(0.0, min(nan, 1.0)) silently
# collapses to the most-permissive 0.0 threshold -- so a bad
# min_similarity must be rejected outright, not quietly widened.
(payload, status), engine_cls, _ = self._call(
search_client,
collections=[("lib-1", "Library", "default_library")],
query_string=f"q=alpha&min_similarity={bad}",
)
assert status == 400, (bad, payload)
assert payload["success"] is False
engine_cls.assert_not_called()
class TestSemanticBackfill:
"""Regression: orphan hits inside the top-``limit`` must be backfilled
from lower-ranked valid hits, not silently shrink the result set."""
# Deliberately NOT inheriting from TestSemanticSearch (the Flask original
# did, which silently re-ran the whole parent class); only the driver is
# reused.
_call = TestSemanticSearch._call
def test_orphan_in_top_limit_is_backfilled(self, search_client):
collections = [("lib-1", "Library", "default_library")]
# limit=2; the two highest-scoring docs are orphans (absent from
# doc_rows), a third valid doc scores lower. Pre-fix the response
# returned 0-1 results; it must now return the 2 valid ones.
engine_results = [
{
"relevance_score": 0.95,
"title": "Ghost A",
"snippet": "a",
"metadata": {"document_id": "ghost-a"},
},
{
"relevance_score": 0.90,
"title": "Ghost B",
"snippet": "b",
"metadata": {"document_id": "ghost-b"},
},
{
"relevance_score": 0.80,
"title": "Real C",
"snippet": "c",
"metadata": {"document_id": "real-c"},
},
{
"relevance_score": 0.70,
"title": "Real D",
"snippet": "d",
"metadata": {"document_id": "real-d"},
},
]
(payload, status), _, _ = self._call(
search_client,
collections=collections,
engine_results=engine_results,
doc_rows=[("real-c", "note", None), ("real-d", "note", None)],
query_string="q=alpha&limit=2",
)
assert status == 200, payload
ids = [r["id"] for r in payload["results"]]
assert ids == ["real-c", "real-d"] # backfilled past the ghosts
assert payload["count"] == 2