~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
"""Does the library pipeline actually *do* what it advertises?
Everything here runs over real HTTP against the assembled FastAPI app
(``TestClient``), through the same routes the browser uses: register ->
log in -> create a collection -> upload real bytes -> read the bytes back
-> search -> delete. No route function is called directly, no session is
mocked, and no assertion stops at "it returned 200": every one of them
looks at the resulting *state* through a second, independent endpoint.
Deliberately NOT repeated here:
* ``sanitize_filename`` over the wire and the multipart parser's edges --
``tests/web/test_multipart_upload_boundary.py``.
* Upload dedup / intra-batch duplicate statuses --
``tests/web/routers/test_collection_upload_dedup.py``.
* Row-level deletion cascade driven through the services --
``tests/research_library/test_deletion_cascade_contracts.py``. That
file asserts on rows with the app switched off; this one asserts on
what an HTTP client can still *see* after a delete, which is a
different question (an orphaned row is invisible to it; a resurrected
document is not).
* The RAG index lifecycle against a real FAISS store --
``tests/research_library/test_document_full_lifecycle.py``.
One known-and-filed defect is load-bearing context but is not
re-asserted: ``POST /library/api/collections/{id}/index/start`` accepting
a nonexistent collection (#5828). The bulk re-index trigger is no longer
one of them -- it is a POST, so the CSRF middleware challenges it like
any other mutation (pinned by
``tests/security/test_csrf_coverage.py``).
Every assertion is paired with a control that proves this harness can
observe the opposite outcome through the identical path -- the deleted
document is checked against a deliberately-seeded sibling that must
survive, the rejected upload against an accepted one, the un-cancellable
task against a cancellable one.
"""
import uuid
from datetime import UTC, datetime, timedelta
import pytest
from fastapi.testclient import TestClient
from tests.web.routers.test_rag_pdf_storage_manager_user_scoping import (
_minimal_pdf,
)
PASSWORD = "TestPassword123!" # noqa: S105
def _make_client(prefix: str) -> TestClient:
"""A logged-in client with a CSRF header, on a brand-new user.
Each fixture gets its OWN user: a single user's TestClient session
exhausts the per-user DB pool after roughly 60 sequential requests,
and these flows are request-heavy.
"""
from local_deep_research.web.fastapi_app import app
client = TestClient(app, raise_server_exceptions=False)
username = f"{prefix}_{uuid.uuid4().hex[:8]}"
def _csrf() -> str:
client.get("/auth/login")
resp = client.get("/auth/csrf-token")
if resp.status_code != 200:
return ""
return resp.json().get("csrf_token", "")
client.post(
"/auth/register",
data={
"username": username,
"password": PASSWORD,
"confirm_password": PASSWORD,
"acknowledge": "true",
"csrf_token": _csrf(),
},
follow_redirects=False,
)
login = client.post(
"/auth/login",
data={
"username": username,
"password": PASSWORD,
"csrf_token": _csrf(),
},
follow_redirects=False,
)
if login.status_code != 302:
pytest.fail(
f"auth bootstrap broken: login returned {login.status_code} "
f"(expected 302): {login.text[:300]}"
)
token_resp = client.get("/auth/csrf-token")
if token_resp.status_code == 200:
client.headers.update(
{"X-CSRFToken": token_resp.json().get("csrf_token", "")}
)
client.username = username
return client
def _new_collection(client, name: str) -> str:
"""Create a collection and return its id, insisting it really exists."""
resp = client.post("/library/api/collections", json={"name": name})
assert resp.status_code == 200, (
f"collection setup failed: {resp.status_code} {resp.text[:300]}"
)
return resp.json()["collection"]["id"]
def _upload(client, collection_id, filename, content, ctype=None, **form):
"""One file, one request. Refuses to let a 429 pose as a verdict.
``upload_to_collection`` carries ``@upload_rate_limit_user`` (10/min);
if the limiter ever starts firing in-process, a 429 would otherwise be
silently asserted against as the handler's answer.
"""
files = {"files": (filename, content, ctype or "application/octet-stream")}
resp = client.post(
f"/library/api/collections/{collection_id}/upload",
files=files,
data=form or None,
)
assert resp.status_code != 429, (
"upload rate limiter fired; the assertions below would be testing "
"the limiter, not the pipeline"
)
return resp
def _uploaded_id(resp):
"""The document id of a single-file upload that must have succeeded."""
body = resp.json()
assert resp.status_code == 200, f"{resp.status_code}: {resp.text[:300]}"
assert body["errors"] == [], body["errors"]
assert len(body["uploaded"]) == 1, body["uploaded"]
return body["uploaded"][0]["id"]
def _doc_ids(client, collection_id):
"""Document ids the collection-listing endpoint reports, as a set."""
resp = client.get(f"/library/api/collections/{collection_id}/documents")
assert resp.status_code == 200, resp.text[:300]
return {d["id"] for d in resp.json()["documents"]}
def _text_of(client, document_id):
return client.get(f"/library/api/document/{document_id}/text")
@pytest.fixture(scope="module")
def upload_client():
client = _make_client("libpipe_up")
yield client
client.post("/auth/logout", follow_redirects=False)
@pytest.fixture(scope="module")
def lifecycle_client():
client = _make_client("libpipe_life")
yield client
client.post("/auth/logout", follow_redirects=False)
@pytest.fixture(scope="module")
def task_client():
client = _make_client("libpipe_task")
yield client
client.post("/auth/logout", follow_redirects=False)
# ---------------------------------------------------------------------------
# Upload -> store -> retrieve: is what comes back what went in?
# ---------------------------------------------------------------------------
@pytest.mark.timeout(180)
class TestUploadStoreRetrieveFidelity:
"""A 200 from the upload route is a promise about later reads.
The route reports ``status: "uploaded"`` and a ``text_length``; the
only thing that makes those numbers mean anything is whether a
*later, separate* request hands the same bytes back.
"""
def test_plain_text_survives_the_round_trip_byte_for_byte(
self, upload_client
):
"""``.txt`` is stored verbatim -- no trimming, no re-encoding.
The control is the sibling upload below: a second document with
different bytes, read back through the identical endpoint, must
return *its own* content. Without it, "the text matched" would
pass just as happily for a handler that echoed a constant.
"""
collection = _new_collection(upload_client, "fidelity-txt")
body = (
"MARKER_ALPHA_9931 the quick brown fox\n trailing ws \n".encode()
)
sibling = "MARKER_BETA_2277 a wholly different document\n".encode()
doc_id = _uploaded_id(
_upload(upload_client, collection, "alpha.txt", body, "text/plain")
)
sib_id = _uploaded_id(
_upload(
upload_client, collection, "beta.txt", sibling, "text/plain"
)
)
assert doc_id != sib_id
resp = _text_of(upload_client, doc_id)
assert resp.status_code == 200, resp.text[:300]
assert resp.json()["text_content"] == body.decode(), (
"stored text is not the uploaded bytes: "
f"{resp.json()['text_content']!r}"
)
# Control: the same endpoint returns the sibling's own bytes.
sib_resp = _text_of(upload_client, sib_id)
assert sib_resp.status_code == 200, sib_resp.text[:300]
assert sib_resp.json()["text_content"] == sibling.decode()
# And the listing agrees on the size actually uploaded.
listing = upload_client.get(
f"/library/api/collections/{collection}/documents"
).json()["documents"]
sizes = {d["id"]: d["file_size"] for d in listing}
assert sizes[doc_id] == len(body), sizes
assert sizes[sib_id] == len(sibling), sizes
@pytest.mark.parametrize("file_count", [1, 2])
def test_upload_survives_another_writer_during_extraction(
self, upload_client, monkeypatch, file_count
):
"""A parser must not hold a stale WAL snapshot or an earlier file's lock."""
from sqlalchemy import text
from local_deep_research import document_loaders
from local_deep_research.database.encrypted_db import db_manager
collection_id = _new_collection(
upload_client, f"concurrent-extraction-{file_count}"
)
engine = db_manager.connections[upload_client.username]
with engine.connect() as connection:
assert (
connection.exec_driver_sql("PRAGMA journal_mode").scalar()
== "wal"
)
extract = document_loaders.extract_text_from_bytes
committed_during_extraction = []
def extract_with_an_independent_writer(content, extension, filename):
result = extract(content, extension, filename)
# This is a separate real SQLCipher connection, as used by
# post-login work and background jobs. No sleeps or mocked DB.
with engine.begin() as writer:
writer.exec_driver_sql("PRAGMA busy_timeout=1000")
writer.execute(
text(
"UPDATE collections SET description = :value WHERE id = :id"
),
{"value": filename, "id": collection_id},
)
committed_during_extraction.append(filename)
return result
monkeypatch.setattr(
document_loaders,
"extract_text_from_bytes",
extract_with_an_independent_writer,
)
filenames = [f"concurrent-{index}.txt" for index in range(file_count)]
contents = {name: f"{name}: {uuid.uuid4().hex}" for name in filenames}
response = upload_client.post(
f"/library/api/collections/{collection_id}/upload",
files=[
("files", (name, contents[name].encode(), "text/plain"))
for name in filenames
],
)
assert response.status_code == 200, response.text
body = response.json()
assert committed_during_extraction == filenames
assert body["errors"] == [], body
assert body["summary"] == {
"total": file_count,
"successful": file_count,
"failed": 0,
}
for uploaded in body["uploaded"]:
assert (
_text_of(upload_client, uploaded["id"]).json()["text_content"]
== contents[uploaded["filename"]]
)
assert len(body["uploaded"]) == file_count
def test_markdown_is_stripped_of_markup_before_storage(self, upload_client):
"""``.md`` does NOT round-trip: the loader flattens it to prose.
This is worth pinning rather than assuming: ``file_size`` is the
uploaded length while the searchable body is shorter, so anything
that greps the stored text for ``#`` or ``*`` finds nothing. The
control is the ``.txt`` test above -- identical path, identical
assertions, and there the bytes *are* preserved -- so this is a
property of the markdown loader, not of the storage layer.
"""
collection = _new_collection(upload_client, "fidelity-md")
source = b"# Heading One\n\nsome *emphasised* body MARKER_MD_77\n"
doc_id = _uploaded_id(
_upload(
upload_client, collection, "doc.md", source, "text/markdown"
)
)
stored = _text_of(upload_client, doc_id).json()["text_content"]
assert stored != source.decode(), (
"markdown now round-trips verbatim; this test's premise changed"
)
# The words survive -- the markup does not.
assert "MARKER_MD_77" in stored
assert "Heading One" in stored
assert "#" not in stored, stored
assert "*" not in stored, stored
# file_size stays the *uploaded* size even though the stored text
# is shorter, so the two numbers are not interchangeable.
listing = upload_client.get(
f"/library/api/collections/{collection}/documents"
).json()["documents"]
entry = next(d for d in listing if d["id"] == doc_id)
assert entry["file_size"] == len(source)
assert len(stored) < len(source)
def test_pdf_stored_in_the_database_is_served_back_unmodified(
self, upload_client
):
"""``pdf_storage=database`` must survive re-encryption and retrieval.
The bytes go through ``PDFStorageManager.save_pdf`` into an
encrypted ``DocumentBlob`` and come back out of a *different*
route, built by a *different* ``PDFStorageManager`` instance
(``view_pdf_page`` constructs its own from the global
``research_library.pdf_storage_mode`` setting, not from the
per-upload ``pdf_storage`` form value). If those two disagreed,
the upload would look successful and the file would be
unreachable -- which is exactly what the control below shows a
genuinely-absent PDF looks like.
"""
collection = _new_collection(upload_client, "fidelity-pdf")
pdf_bytes = _minimal_pdf("PDFMARKER_8080 hello")
doc_id = _uploaded_id(
_upload(
upload_client,
collection,
"stored.pdf",
pdf_bytes,
"application/pdf",
pdf_storage="database",
)
)
served = upload_client.get(f"/library/api/document/{doc_id}/pdf")
assert served.status_code == 200, served.text[:200]
assert served.headers["content-type"].startswith("application/pdf")
assert served.content == pdf_bytes, (
f"served {len(served.content)} bytes, uploaded {len(pdf_bytes)}"
)
# The extracted text is stored alongside the blob, not instead of it.
assert (
"PDFMARKER_8080"
in _text_of(upload_client, doc_id).json()["text_content"]
)
# Control: the same PDF uploaded text-only has no blob to serve,
# and the identical endpoint says so with a 404.
text_only_id = _uploaded_id(
_upload(
upload_client,
collection,
"textonly.pdf",
_minimal_pdf("TEXTONLY_909"),
"application/pdf",
pdf_storage="none",
)
)
assert (
upload_client.get(
f"/library/api/document/{text_only_id}/pdf"
).status_code
== 404
)
assert (
"TEXTONLY_909"
in _text_of(upload_client, text_only_id).json()["text_content"]
)
# ---------------------------------------------------------------------------
# Format handling: what the route trusts, and what it checks
# ---------------------------------------------------------------------------
@pytest.mark.timeout(180)
class TestFormatHandling:
"""The collection-upload route decides by *extension*, then by whether
a loader could produce text. Nothing sniffs the content.
Each rejection below is paired with an acceptance that differs in
exactly one respect, so "it was rejected" cannot be explained by the
request never having reached the handler.
"""
@pytest.fixture(scope="class")
def formats_collection(self, upload_client):
return _new_collection(upload_client, "formats")
def test_zero_byte_file_is_rejected_and_creates_no_document(
self, upload_client, formats_collection
):
"""An empty upload must not become a zero-content library entry."""
before = _doc_ids(upload_client, formats_collection)
resp = _upload(
upload_client, formats_collection, "empty.txt", b"", "text/plain"
)
assert resp.status_code == 200, resp.text[:300]
body = resp.json()
assert body["uploaded"] == [], body["uploaded"]
assert [e["filename"] for e in body["errors"]] == ["empty.txt"]
assert "Could not extract text" in body["errors"][0]["error"]
assert _doc_ids(upload_client, formats_collection) == before, (
"a rejected empty file still landed in the collection"
)
# Control: one byte more and the identical path stores a document.
one_byte_id = _uploaded_id(
_upload(
upload_client,
formats_collection,
"onebyte.txt",
b"x",
"text/plain",
)
)
assert one_byte_id in _doc_ids(upload_client, formats_collection)
assert _text_of(upload_client, one_byte_id).json()["text_content"] == (
"x"
)
def test_unsupported_extension_is_named_in_the_error(
self, upload_client, formats_collection
):
"""``.xyz`` and a bare extensionless name are both refused.
The extensionless case is the interesting one: ``Path("noext")``
has an empty suffix, so the error text degenerates to
"Unsupported format: " with nothing after the colon.
"""
before = _doc_ids(upload_client, formats_collection)
resp = _upload(
upload_client, formats_collection, "weird.xyz", b"hello world"
)
assert resp.json()["errors"][0]["error"] == "Unsupported format: .xyz"
resp = _upload(
upload_client, formats_collection, "noext", b"hello world"
)
assert resp.json()["errors"][0]["error"] == "Unsupported format: "
assert _doc_ids(upload_client, formats_collection) == before
def test_binary_bytes_under_a_txt_name_are_refused(
self, upload_client, formats_collection
):
"""Undecodable bytes fail extraction rather than storing mojibake."""
before = _doc_ids(upload_client, formats_collection)
resp = _upload(
upload_client,
formats_collection,
"bin.txt",
b"\x00\x01\x02\xff\xfe binary \x80",
"text/plain",
)
assert resp.json()["uploaded"] == []
assert "Could not extract text" in resp.json()["errors"][0]["error"]
assert _doc_ids(upload_client, formats_collection) == before
def test_windows_1252_text_under_a_txt_name_is_stored_as_written(
self, upload_client, formats_collection
):
"""Legacy "ANSI" text is read, where the bytes above are refused.
With chardet 7, LangChain's ``autodetect_encoding`` raised
``TypeError`` for every .txt that was not UTF-8, so this upload was
refused as well (#6532).
"""
text = "Grüße aus Köln, 5 €"
resp = _upload(
upload_client,
formats_collection,
"ansi.txt",
text.encode("cp1252"),
"text/plain",
)
doc_id = _uploaded_id(resp)
stored = _text_of(upload_client, doc_id).json()["text_content"]
assert stored == text
@pytest.mark.parametrize("encoding", ["utf-8-sig", "cp1252", "utf-16"])
def test_a_csv_upload_is_stored_in_the_encoding_it_was_written_in(
self, upload_client, formats_collection, encoding
):
"""Excel's "CSV UTF-8" (with a BOM), its "ANSI" CSV, and UTF-16.
Before #6532 was fixed, the Windows-1252 and UTF-16 files were
refused, and the UTF-8 byte-order mark was stored as part of the
first column name.
"""
rows = "Name,Stadt\nMüller,Köln\n"
resp = _upload(
upload_client,
formats_collection,
f"people-{encoding}.csv",
rows.encode(encoding),
"text/csv",
)
doc_id = _uploaded_id(resp)
stored = _text_of(upload_client, doc_id).json()["text_content"]
assert stored == "Name: Müller\nStadt: Köln"
def test_extension_wins_over_content_in_both_directions(
self, upload_client, formats_collection
):
"""A mislabelled file is judged by its name, not its magic bytes.
Both halves are the *same* file mislabelled two ways:
* PDF bytes named ``.txt`` -> accepted, and the raw PDF source
(``%PDF-1.4``, ``endobj``, the xref table) becomes the
document's searchable "text";
* plain text named ``.pdf`` -> rejected, because pypdf cannot
parse it.
The sibling upload route ``POST /api/upload/pdf`` *does* check
the ``%PDF`` signature ("File signature mismatch"); this one has
no content check at all, by design -- it accepts arbitrary
document types. Pinned because the consequence is concrete: a
renamed binary is indexed as garbage prose rather than refused.
"""
pdf_bytes = _minimal_pdf("MISLABELLED_314")
as_txt = _upload(
upload_client,
formats_collection,
"actually_a_pdf.txt",
pdf_bytes,
"text/plain",
)
doc_id = _uploaded_id(as_txt)
stored = _text_of(upload_client, doc_id).json()["text_content"]
assert stored == pdf_bytes.decode("latin-1"), (
"PDF bytes under a .txt name are stored as raw source text"
)
assert stored.startswith("%PDF-1.4")
listing = upload_client.get(
f"/library/api/collections/{formats_collection}/documents"
).json()["documents"]
entry = next(d for d in listing if d["id"] == doc_id)
assert entry["file_type"] == "txt", entry
# The mirror image: real text under a .pdf name is refused.
as_pdf = _upload(
upload_client,
formats_collection,
"actually_text.pdf",
b"this is plain text, not a pdf at all\n",
"application/pdf",
)
assert as_pdf.json()["uploaded"] == []
assert (
as_pdf.json()["errors"][0]["error"]
== "Could not extract text from pdf file"
)
def test_batch_where_every_file_failed_still_reports_success_true(
self, upload_client, formats_collection
):
"""200 + ``success: true`` for a batch that stored nothing.
The per-file ``errors`` array is the only signal; the top-level
flag and the HTTP status are both green. The control is the
sibling upload endpoint ``POST /api/upload/pdf``, which answers
the same "every file failed" situation with 400 -- so this is a
divergence between two upload routes in the same app, not a
house style.
"""
resp = _upload(
upload_client, formats_collection, "nope.xyz", b"content"
)
assert resp.status_code == 200
assert resp.json()["success"] is True
assert resp.json()["summary"] == {
"total": 1,
"successful": 0,
"failed": 1,
}
# Control: the sibling route, same all-files-failed situation.
sibling = upload_client.post(
"/api/upload/pdf",
files={"files": ("bad.pdf", b"not a pdf", "application/pdf")},
)
assert sibling.status_code == 400, sibling.text[:200]
assert sibling.json()["status"] == "error"
# ---------------------------------------------------------------------------
# ... and then can you find it again?
# ---------------------------------------------------------------------------
def _default_library_id(client) -> str:
"""Id of the built-in "Library" collection.
``GET /library/api/documents`` hard-scopes itself to this collection
(``LibraryService.get_documents`` falls back to
``get_default_library_id`` and the route never passes a
``collection_id``), so anything meant to be visible there has to be
uploaded into it.
"""
resp = client.get("/library/api/collections/list")
assert resp.status_code == 200, resp.text[:200]
for coll in resp.json()["collections"]:
if coll["name"] == "Library":
return coll["id"]
pytest.fail(f"no default Library collection: {resp.text[:300]}")
@pytest.mark.timeout(180)
class TestUploadedDocumentIsFindable:
"""Can a search find an uploaded document by the name it lists it under?
Two endpoints search documents and list each hit by name:
* ``GET /library/search/api/keyword`` -- the keyword leg of the Search
page ("Search everything") -- lists a hit as ``title or filename``.
* ``GET /library/api/documents?search=`` lists a document as
``document_title``: its title, else a linked resource's title, else
its filename. No page calls it with ``search``; the Library page's
search box filters the cards it has already rendered, in the browser.
No upload path sets ``Document.title``, so both list an upload under
its filename. The question is not whether they return 200 -- they
always do -- but whether they find a document that is demonstrably in
the scope they search by the name they list it under, and never by a
stored filename they do not list it under.
"""
@pytest.fixture(scope="class")
def seeded(self, upload_client):
"""Two uploaded documents; the second is given a real ``title``.
The title is written through the application's own
``get_user_db_session`` -- the same session factory the routes
use -- because no upload path sets ``Document.title``. It gives the
positive control a title to match, and makes the second document
one that is listed under its title while its stored filename
(``other_upload_5151.txt``) is not shown as its name.
"""
from local_deep_research.database.models.library import Document
from local_deep_research.database.session_context import (
get_user_db_session,
)
library_id = _default_library_id(upload_client)
untitled_id = _uploaded_id(
_upload(
upload_client,
library_id,
"FINDME_UNTITLED_5150.txt",
b"body text of the untitled document\n",
"text/plain",
)
)
titled_id = _uploaded_id(
_upload(
upload_client,
library_id,
"other_upload_5151.txt",
b"body text of the titled document\n",
"text/plain",
)
)
with get_user_db_session(upload_client.username) as session:
doc = session.query(Document).filter_by(id=titled_id).first()
assert doc is not None, "titled control document vanished"
doc.title = "FINDME_TITLED_5151"
session.commit()
return {"untitled": untitled_id, "titled": titled_id}
def _search(self, client, query):
resp = client.get("/library/api/documents", params={"search": query})
assert resp.status_code == 200, resp.text[:300]
return {d["id"] for d in resp.json()["documents"]}
def _keyword_hits(self, client, query):
"""``{id: listed title}`` from the Search page's keyword leg."""
resp = client.get(
"/library/search/api/keyword", params={"q": query, "limit": 50}
)
assert resp.status_code == 200, resp.text[:300]
return {r["id"]: r["title"] for r in resp.json()["results"]}
def test_search_matches_a_document_whose_title_column_is_set(
self, upload_client, seeded
):
"""Positive control: the filter works, on the column it reads.
A title match must retain the existing behavior when filename
search is added, and exclude the unrelated uploaded document.
"""
hits = self._search(upload_client, "FINDME_TITLED_5151")
assert seeded["titled"] in hits, hits
assert seeded["untitled"] not in hits, (
"the filter is not discriminating at all"
)
# ilike ignores the case of ASCII letters.
assert seeded["titled"] in self._search(
upload_client, "findme_titled_5151"
)
def test_both_documents_are_listed_when_no_search_is_given(
self, upload_client, seeded
):
"""Both uploaded documents really are in the searched scope.
Same endpoint, same collection, filter absent -- so a later
"search found nothing" cannot be explained by the document being
elsewhere, unfinished (``status != completed``) or paginated out.
"""
resp = upload_client.get("/library/api/documents")
assert resp.status_code == 200, resp.text[:300]
listed = {d["id"]: d for d in resp.json()["documents"]}
assert seeded["untitled"] in listed, listed.keys()
assert seeded["titled"] in listed, listed.keys()
# And the API lists the untitled document under its filename.
assert (
listed[seeded["untitled"]]["document_title"]
== "FINDME_UNTITLED_5150.txt"
)
def test_search_finds_an_uploaded_document_by_its_displayed_title(
self, upload_client, seeded
):
hits = self._search(upload_client, "FINDME_UNTITLED_5150")
assert seeded["untitled"] in hits, (
"the documents API cannot find an uploaded document by the "
f"title it lists it under; hits={hits}"
)
assert seeded["titled"] not in hits
assert seeded["untitled"] in self._search(
upload_client, "findme_untitled_5150"
)
# LIKE wildcards in the query stay literal: as wildcards, "%" and
# "_" would match the "_" and the "0" of FINDME_UNTITLED_5150.txt.
assert seeded["untitled"] not in self._search(
upload_client, "FINDME_UNTITLED%5150"
)
assert seeded["untitled"] not in self._search(
upload_client, "UNTITLED_515_.txt"
)
def test_search_skips_a_titled_documents_stored_filename(
self, upload_client, seeded
):
"""A titled document is listed under its title, not its filename.
The control proves the term is in the stored filename the API
reports for that document, so the miss is the filter declining a
name the document is not listed under, not the term being absent.
"""
resp = upload_client.get("/library/api/documents")
assert resp.status_code == 200, resp.text[:300]
titled = {d["id"]: d for d in resp.json()["documents"]}[
seeded["titled"]
]
assert titled["document_title"] == "FINDME_TITLED_5151"
assert titled["file_name"] == "other_upload_5151.txt"
assert seeded["titled"] not in self._search(
upload_client, "other_upload_5151"
)
def test_search_page_finds_an_upload_by_its_listed_filename(
self, upload_client, seeded
):
"""The Search page lists an upload under its filename.
Its body text does not contain the name, so only the filename can
produce the hit.
"""
hits = self._keyword_hits(upload_client, "FINDME_UNTITLED_5150")
assert hits.get(seeded["untitled"]) == "FINDME_UNTITLED_5150.txt", (
"the Search page cannot find an uploaded document by the name "
f"it lists it under; hits={hits}"
)
assert seeded["titled"] not in hits
assert seeded["untitled"] in self._keyword_hits(
upload_client, "findme_untitled_5150"
)
assert seeded["untitled"] not in self._keyword_hits(
upload_client, "FINDME_UNTITLED%5150"
)
assert seeded["untitled"] not in self._keyword_hits(
upload_client, "UNTITLED_515_.txt"
)
def test_search_page_skips_a_titled_documents_stored_filename(
self, upload_client, seeded
):
"""Control first: the titled document is found by, and listed
under, its title; then its stored filename must not match."""
hits = self._keyword_hits(upload_client, "FINDME_TITLED_5151")
assert hits.get(seeded["titled"]) == "FINDME_TITLED_5151", hits
assert seeded["titled"] not in self._keyword_hits(
upload_client, "other_upload_5151"
)
# ---------------------------------------------------------------------------
# Collection lifecycle: what a delete actually removes
# ---------------------------------------------------------------------------
SHARED_BYTES = b"SHARED_MARKER_4242 lives in two collections\n"
@pytest.mark.timeout(180)
class TestCollectionLifecycle:
"""Create -> add -> remove -> delete, judged by what is still readable.
Every deletion assertion is paired with a sibling that must survive
the same call: a delete that wiped everything would satisfy "the
target is gone" just as well as a correct one.
"""
def test_deleting_a_collection_deletes_orphans_and_spares_shared_docs(
self, lifecycle_client
):
coll_a = _new_collection(lifecycle_client, "cascade-A")
coll_b = _new_collection(lifecycle_client, "cascade-B")
orphan_id = _uploaded_id(
_upload(
lifecycle_client,
coll_a,
"orphan.txt",
b"ONLY_IN_A_5555 lonely document\n",
"text/plain",
)
)
shared_id = _uploaded_id(
_upload(
lifecycle_client,
coll_a,
"shared.txt",
SHARED_BYTES,
"text/plain",
)
)
# Same bytes into B: the route dedups by hash and links the SAME
# document into the second collection.
linked = _upload(
lifecycle_client, coll_b, "shared.txt", SHARED_BYTES, "text/plain"
).json()["uploaded"][0]
assert linked["id"] == shared_id, linked
assert linked["status"] == "added_to_collection", linked
# Precondition: both are readable, and B really holds the shared one.
assert _text_of(lifecycle_client, orphan_id).status_code == 200
assert _text_of(lifecycle_client, shared_id).status_code == 200
assert shared_id in _doc_ids(lifecycle_client, coll_b)
deleted = lifecycle_client.delete(f"/library/api/collections/{coll_a}")
assert deleted.status_code == 200, deleted.text[:300]
body = deleted.json()
assert body["documents_unlinked"] == 2, body
assert body["orphaned_documents_deleted"] == 1, body
# The orphan is really gone -- through the document endpoint...
assert _text_of(lifecycle_client, orphan_id).status_code == 404
# ...and the collection with it.
assert (
lifecycle_client.get(
f"/library/api/collections/{coll_a}/documents"
).status_code
== 404
)
# Control: the shared document survived the identical cascade and
# still returns its own bytes, still listed by its other collection.
survivor = _text_of(lifecycle_client, shared_id)
assert survivor.status_code == 200, survivor.text[:200]
assert survivor.json()["text_content"] == SHARED_BYTES.decode()
assert shared_id in _doc_ids(lifecycle_client, coll_b)
def test_removing_a_document_from_its_last_collection_deletes_it(
self, lifecycle_client
):
""" "Remove from collection" is a delete for a single-collection doc.
Both halves go through the identical route; the only difference
is whether a second collection still holds the document.
"""
coll_c = _new_collection(lifecycle_client, "unlink-C")
coll_d = _new_collection(lifecycle_client, "unlink-D")
solo_id = _uploaded_id(
_upload(
lifecycle_client,
coll_c,
"solo.txt",
b"SOLO_1_9k\n",
"text/plain",
)
)
dual_bytes = b"DUAL_1_9k content\n"
dual_id = _uploaded_id(
_upload(
lifecycle_client, coll_c, "dual.txt", dual_bytes, "text/plain"
)
)
_upload(lifecycle_client, coll_d, "dual.txt", dual_bytes, "text/plain")
assert {solo_id, dual_id} <= _doc_ids(lifecycle_client, coll_c)
removed_solo = lifecycle_client.request(
"DELETE", f"/library/api/collection/{coll_c}/document/{solo_id}"
)
assert removed_solo.status_code == 200, removed_solo.text[:300]
assert removed_solo.json()["document_deleted"] is True
removed_dual = lifecycle_client.request(
"DELETE", f"/library/api/collection/{coll_c}/document/{dual_id}"
)
assert removed_dual.status_code == 200, removed_dual.text[:300]
assert removed_dual.json()["document_deleted"] is False
assert _text_of(lifecycle_client, solo_id).status_code == 404
# Control: the doc that was in two collections is unlinked from C
# but still whole, still readable, still listed by D.
kept = _text_of(lifecycle_client, dual_id)
assert kept.status_code == 200, kept.text[:200]
assert kept.json()["text_content"] == dual_bytes.decode()
assert _doc_ids(lifecycle_client, coll_c) == set()
assert dual_id in _doc_ids(lifecycle_client, coll_d)
def test_system_collection_refuses_deletion_with_its_documents_intact(
self, lifecycle_client
):
"""The built-in Library is undeletable -- and nothing is lost trying.
Control: a user collection holding an equivalent document, hit
with the identical call, IS deleted and DOES take its orphan.
"""
library_id = _default_library_id(lifecycle_client)
protected_doc = _uploaded_id(
_upload(
lifecycle_client,
library_id,
"protected.txt",
b"PROTECTED_7 stays put\n",
"text/plain",
)
)
refused = lifecycle_client.delete(
f"/library/api/collections/{library_id}"
)
assert refused.status_code == 409, refused.text[:300]
assert refused.json()["collection_type"] == "default_library"
assert refused.json()["deleted"] is False
assert _text_of(lifecycle_client, protected_doc).status_code == 200
assert protected_doc in _doc_ids(lifecycle_client, library_id)
control_coll = _new_collection(lifecycle_client, "deletable-control")
control_doc = _uploaded_id(
_upload(
lifecycle_client,
control_coll,
"control.txt",
b"CONTROL_7 goes away\n",
"text/plain",
)
)
allowed = lifecycle_client.delete(
f"/library/api/collections/{control_coll}"
)
assert allowed.status_code == 200, allowed.text[:300]
assert allowed.json()["orphaned_documents_deleted"] == 1
assert _text_of(lifecycle_client, control_doc).status_code == 404
# ...and the protected one is still standing after all of it.
assert _text_of(lifecycle_client, protected_doc).status_code == 200
def test_upload_to_a_nonexistent_collection_stores_nothing(
self, lifecycle_client
):
"""404 before any Document row is created.
Worth stating explicitly next to #5828 (index/start happily
accepts a nonexistent collection id and spawns a worker): the
upload route does not share that flaw -- it checks first. The
control is the same bytes into a real collection, which do land.
"""
ghost = str(uuid.uuid4())
resp = _upload(
lifecycle_client,
ghost,
"ghost.txt",
b"GHOST_777 data\n",
"text/plain",
)
assert resp.status_code == 404, resp.text[:300]
assert resp.json()["error"] == "Collection not found"
real = _new_collection(lifecycle_client, "ghost-control")
landed = _uploaded_id(
_upload(
lifecycle_client,
real,
"ghost.txt",
b"GHOST_777 data\n",
"text/plain",
)
)
assert (
_text_of(lifecycle_client, landed).json()["text_content"]
== "GHOST_777 data\n"
)
# ---------------------------------------------------------------------------
# Semantic search over a collection nobody has indexed yet
# ---------------------------------------------------------------------------
@pytest.mark.timeout(120)
class TestCollectionSemanticSearch:
def test_unindexed_collection_reports_success_and_no_results(
self, task_client
):
"""A never-indexed collection answers exactly like an empty one.
The document is provably in the collection and its text provably
contains the query string (both checked below through other
endpoints), yet the search returns ``success: true`` with an
empty ``results`` list and nothing that would let a caller tell
"this collection has no vector index" from "no document
matched". Pinned as the current contract: the endpoint's error
paths *do* work (a nonexistent collection 404s, a blank query
400s), so the silence here is specific to the unindexed case.
"""
collection = _new_collection(task_client, "unindexed-search")
doc_id = _uploaded_id(
_upload(
task_client,
collection,
"needle.txt",
b"NEEDLE_60422 in the haystack\n",
"text/plain",
)
)
# State proof: the corpus really does contain the query string.
assert doc_id in _doc_ids(task_client, collection)
assert (
"NEEDLE_60422"
in _text_of(task_client, doc_id).json()["text_content"]
)
resp = task_client.post(
f"/library/api/collections/{collection}/search",
json={"query": "NEEDLE_60422", "limit": 5},
)
assert resp.status_code == 200, resp.text[:300]
body = resp.json()
assert body["success"] is True, body
assert body["results"] == [], body
assert "index" not in resp.text.lower(), (
"the response now mentions indexing; this pin is stale"
)
# Controls: the same endpoint does report other failures.
ghost = task_client.post(
f"/library/api/collections/{uuid.uuid4()}/search",
json={"query": "NEEDLE_60422"},
)
assert ghost.status_code == 404, ghost.text[:200]
blank = task_client.post(
f"/library/api/collections/{collection}/search",
json={"query": " "},
)
assert blank.status_code == 400, blank.text[:200]
assert blank.json()["error"] == "Query is required"
# ---------------------------------------------------------------------------
# Indexing task status: is "processing" ever a lie?
# ---------------------------------------------------------------------------
def _seed_indexing_task(client, collection_id, status, age):
"""Insert a ``TaskMetadata`` row exactly as ``index/start`` would.
Written through the application's own ``get_user_db_session`` and
its own model -- the same rows the route creates -- because the only
other way to obtain a *processing* task is to spawn the real indexing
worker, which downloads an embedding model. What is being tested is
what the status/cancel endpoints do with rows that already exist, so
the worker is beside the point (and a worker killed mid-run leaves
precisely this row behind).
"""
from local_deep_research.database.models import TaskMetadata
from local_deep_research.database.session_context import (
get_user_db_session,
)
task_id = str(uuid.uuid4())
when = datetime.now(UTC) - age
with get_user_db_session(client.username) as session:
session.add(
TaskMetadata(
task_id=task_id,
status=status,
task_type="indexing",
created_at=when,
started_at=when,
progress_current=0,
progress_total=0,
progress_message="Starting indexing...",
metadata_json={"collection_id": collection_id},
)
)
session.commit()
return task_id
@pytest.fixture
def fresh_client():
"""Factory for per-test users.
``cancel_indexing`` and ``start_background_index`` both scan *all* of
a user's indexing tasks, so a task seeded by one test would leak into
another's answer. A private user per test makes each one independent
of execution order (this suite is run under ``-p no:randomly``
locally but not necessarily in CI).
"""
made = []
def _factory(prefix):
client = _make_client(prefix)
made.append(client)
return client
yield _factory
for client in made:
client.post("/auth/logout", follow_redirects=False)
@pytest.mark.timeout(180)
class TestIndexingTaskStatus:
def test_a_task_abandoned_by_a_dead_worker_reports_processing_forever(
self, fresh_client
):
"""Nothing ages out a ``processing`` row, and it blocks restarts.
``cleanup_old_tasks`` reaps on ``completed_at``, which an
abandoned task never gets, so a worker killed by a restart leaves
a task that still claims to be running two days later -- and
``index/start`` refuses the collection with 409 for as long as it
stands. Recovery exists (``index/cancel``), but the status
endpoint gives the UI no way to tell a live job from a dead one:
the returned ``created_at`` is the only clue.
Control: a sibling collection whose seeded task is ``completed``
is reported as completed through the identical endpoint, so
"processing" here is a real per-collection lookup and not a
constant.
"""
client = fresh_client("libpipe_stale")
stuck_coll = _new_collection(client, "stuck")
done_coll = _new_collection(client, "done")
stuck_task = _seed_indexing_task(
client, stuck_coll, "processing", timedelta(days=2)
)
done_task = _seed_indexing_task(
client, done_coll, "completed", timedelta(days=2)
)
stuck = client.get(
f"/library/api/collections/{stuck_coll}/index/status"
)
assert stuck.status_code == 200, stuck.text[:200]
assert stuck.json()["task_id"] == stuck_task
assert stuck.json()["status"] == "processing", stuck.json()
assert stuck.json()["created_at"].startswith(
(datetime.now(UTC) - timedelta(days=2)).strftime("%Y-%m-%d")
), stuck.json()["created_at"]
# Control: same endpoint, sibling collection, terminal status.
done = client.get(f"/library/api/collections/{done_coll}/index/status")
assert done.json()["task_id"] == done_task
assert done.json()["status"] == "completed", done.json()
# The stale row wedges the collection: no new index can start.
blocked = client.post(
f"/library/api/collections/{stuck_coll}/index/start", json={}
)
assert blocked.status_code == 409, blocked.text[:300]
assert blocked.json()["task_id"] == stuck_task, (
"the 409 is caused by some other task than the one seeded"
)
assert (
blocked.json()["error"]
== "Indexing is already in progress for this collection"
)
def test_cancel_ends_the_task_when_it_is_the_only_one_running(
self, fresh_client
):
"""Positive control for the xfail below: cancel does work.
One processing task, cancelled through the real endpoint, and
the status endpoint agrees afterwards. Whatever the next test
shows, it is not that cancellation is unimplemented.
"""
client = fresh_client("libpipe_cancel1")
collection = _new_collection(client, "cancel-solo")
task_id = _seed_indexing_task(
client, collection, "processing", timedelta(minutes=5)
)
resp = client.post(
f"/library/api/collections/{collection}/index/cancel"
)
assert resp.status_code == 200, resp.text[:300]
assert resp.json()["task_id"] == task_id
after = client.get(
f"/library/api/collections/{collection}/index/status"
)
assert after.json()["status"] == "cancelled", after.json()
def test_cancel_is_scoped_to_the_requested_collection(self, fresh_client):
client = fresh_client("libpipe_cancel2")
first_coll = _new_collection(client, "cancel-first")
second_coll = _new_collection(client, "cancel-second")
_seed_indexing_task(
client, first_coll, "processing", timedelta(minutes=10)
)
second_task = _seed_indexing_task(
client, second_coll, "processing", timedelta(minutes=5)
)
# Both are genuinely in flight as far as the API is concerned.
for coll in (first_coll, second_coll):
status = client.get(f"/library/api/collections/{coll}/index/status")
assert status.json()["status"] == "processing", status.json()
# Cancel the second one first: it is the one whose task is not the
# arbitrary row .first() returns.
second_cancel = client.post(
f"/library/api/collections/{second_coll}/index/cancel"
)
first_cancel = client.post(
f"/library/api/collections/{first_coll}/index/cancel"
)
assert second_cancel.status_code == 200, (
"cancelling the second collection while another collection is "
f"indexing: {second_cancel.status_code} "
f"{second_cancel.text[:200]}; its task {second_task} is still "
f"{client.get(f'/library/api/collections/{second_coll}/index/status').json()['status']}"
)
assert first_cancel.status_code == 200, first_cancel.text[:200]
for coll in (first_coll, second_coll):
status = client.get(f"/library/api/collections/{coll}/index/status")
assert status.json()["status"] == "cancelled", status.json()