~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 keyword-leg title fallback in the unified-search router.
Ported from the now-deleted Flask suite
(``tests/web/routes/test_unified_search_routes.py::TestKeywordSearch``) for
main commit 15229b65a "fix(library-search): fall back to Document.filename
in keyword leg title (#5208)".
The fix itself survived the FastAPI migration unchanged —
``routers/unified_search.py::keyword_search`` already projects
``Document.filename`` alongside ``Document.title`` and computes
``display_title = title or filename or "Untitled"`` (see the function's
docstring and inline comments). Only the Flask test harness (a hand-rolled
``_handler``/``_unpack`` shim over a Flask blueprint) was removed with the
Flask app; this file re-covers the same behavior against the FastAPI route
directly via ``TestClient``, mocking ``get_user_db_session`` the same way
``test_benchmark_export_metadata.py`` mocks it for a sibling FastAPI router
whose handler also does a local ``from ...database.session_context import
get_user_db_session`` import.
"""
from contextlib import contextmanager
from datetime import datetime, UTC
from unittest.mock import MagicMock, patch
import pytest
from fastapi.testclient import TestClient
def _make_app():
"""Return the real FastAPI app with ``require_auth`` overridden so the
route body executes as ``testuser`` without a real login/DB."""
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.dependencies.auth import require_auth
app.dependency_overrides[require_auth] = lambda: "testuser"
return app
@contextmanager
def _patch_db(rows):
"""Patch ``get_user_db_session`` (imported locally inside
``keyword_search`` from ``database.session_context``) so the query
chain's ``.all()`` returns ``rows``. Yields the mock session so a test
can inspect ``session.query.call_args`` if needed."""
mock_db = MagicMock()
chain = MagicMock()
chain.join.return_value = chain
chain.filter.return_value = chain
chain.order_by.return_value = chain
chain.limit.return_value = chain
chain.all.return_value = rows
mock_db.query.return_value = chain
@contextmanager
def _session_ctx(username, *args, **kwargs):
yield mock_db
with patch(
"local_deep_research.database.session_context.get_user_db_session",
side_effect=_session_ctx,
):
yield mock_db
@pytest.fixture(autouse=True)
def _clear_overrides():
"""Ensure the ``require_auth`` override added by ``_make_app`` doesn't
leak into other tests sharing the module-level FastAPI app."""
yield
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.dependencies.auth import require_auth
app.dependency_overrides.pop(require_auth, None)
def _client(app):
return TestClient(app, raise_server_exceptions=False)
def _row(
doc_id,
title,
filename,
source_type="user_upload",
updated_at=None,
research_id=None,
pos=0,
):
"""Build one row matching the SELECT projected by keyword_search:
(id, title, filename, preview, source_type, updated_at, research_id,
instr_match_pos)."""
return (
doc_id,
title,
filename,
"preview text",
source_type,
updated_at or datetime(2026, 7, 1, 12, 0, 0, tzinfo=UTC),
research_id,
pos,
)
class TestKeywordSearchTitleFallback:
def test_title_null_falls_back_to_filename(self):
# Uploaded documents have title=NULL and filename=<book name>. The
# keyword leg must surface the filename (the same string the
# semantic leg shows) instead of the generic 'Untitled' so a user
# can identify the document. Regression for the bug where the
# keyword leg did `title or 'Untitled'`, ignoring filename
# entirely, producing "Untitled" for every uploaded .txt / .pdf.
app = _make_app()
rows = [
_row(
"u1",
None,
"10_Lessons_from_Hindu_History_in_10_Episodes.txt",
)
]
with _patch_db(rows):
resp = _client(app).get("/library/search/api/keyword?q=alpha")
assert resp.status_code == 200
body = resp.json()
assert body["results"][0]["title"] == (
"10_Lessons_from_Hindu_History_in_10_Episodes.txt"
)
def test_title_empty_string_falls_back_to_filename(self):
# An empty string is falsy in Python; the `or` chain must still
# skip it and use filename.
app = _make_app()
rows = [_row("u1", "", "book.txt")]
with _patch_db(rows):
resp = _client(app).get("/library/search/api/keyword?q=alpha")
assert resp.status_code == 200
assert resp.json()["results"][0]["title"] == "book.txt"
def test_title_set_wins_over_filename(self):
# When Document.title is populated (e.g. research downloads,
# notes), it must take precedence — filename is only a fallback.
app = _make_app()
rows = [
_row(
"d1",
"Real Title",
"should_not_appear.txt",
source_type="research_report",
research_id="res-1",
)
]
with _patch_db(rows):
resp = _client(app).get("/library/search/api/keyword?q=alpha")
assert resp.status_code == 200
assert resp.json()["results"][0]["title"] == "Real Title"
def test_title_and_filename_both_null_falls_back_to_untitled(self):
# Defensive: a document with no title and no filename must still
# return a string for the client. This is the ONLY case where
# 'Untitled' should appear.
app = _make_app()
rows = [_row("x1", None, None)]
with _patch_db(rows):
resp = _client(app).get("/library/search/api/keyword?q=alpha")
assert resp.status_code == 200
assert resp.json()["results"][0]["title"] == "Untitled"
def test_mixed_title_states_in_one_response(self):
# A single keyword response containing a mix of title=set,
# title=NULL+filename=set, and title=set rows. The fallback must
# apply per-row, not blanket over the response.
app = _make_app()
rows = [
_row(
"r1",
"Research Title",
None,
source_type="research_report",
research_id="res-1",
),
_row("u1", None, "book_one.txt"),
_row("u2", None, "book_two.txt"),
_row("n1", "Note title", None, source_type="note"),
]
with _patch_db(rows):
resp = _client(app).get("/library/search/api/keyword?q=alpha")
assert resp.status_code == 200
titles = [r["title"] for r in resp.json()["results"]]
assert titles == [
"Research Title",
"book_one.txt",
"book_two.txt",
"Note title",
]
def test_query_projects_document_filename(self):
# Assert the ORM projection explicitly includes Document.filename
# (and Document.title), i.e. the query fetches the column needed
# for the fallback.
from local_deep_research.database.models.library import Document
app = _make_app()
with _patch_db([]) as mock_db:
resp = _client(app).get("/library/search/api/keyword?q=alpha")
assert resp.status_code == 200
query_args = mock_db.query.call_args[0]
assert Document.filename in query_args
assert Document.title in query_args