~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
"""Ported from ``tests/notes/test_post_review_bugfixes.py`` on main (deleted
by the FastAPI migration).
Regression guards for a batch of bugfixes (preview-key, reindex-flag,
synth-dedup, prune-bookends, delete-RAG-cleanup, wiki-link rename safety).
Each test asserts the specific invariant the fix restored; a revert of the
corresponding line flips the assertion.
Most of these are SERVICE-level (``NoteService`` / ``NoteAIService``), which
the migration left untouched -- so the assertions carry over unchanged. Only
the two route tests needed re-plumbing: Flask ``test_request_context`` ->
a direct call on the unwrapped FastAPI handler with a dummy ``Request``.
Successor audit
---------------
Superseded and NOT re-ported (3 of 19):
* ``test_prune_does_not_delete_bookend_rows`` ->
``tests/research_library/test_note_service_contracts.py::test_version_prune_caps_ordinary_rows_and_spares_bookends_and_siblings``.
* ``test_prune_counts_ordinary_versions_separately_from_bookends`` ->
``tests/notes/test_note_stress.py`` (same name, already ported).
* ``test_link_survives_target_rename_then_source_resave`` ->
``tests/notes/test_note_service.py::test_auto_suggested_link_survives_target_rename``
(the same Priority-2 cache, accept-link flavour).
Everything else below has no successor -- notably ``RagDocumentStatus`` and
``_mark_note_stale_for_reindex_in_session`` appear in NO branch test, and
``_trigger_note_auto_index`` appears in none either.
One trap worth naming: the branch's ``tests/notes/test_note_stress.py`` sets
``_capture_request_db_password = lambda username: None`` as a harness
convenience and asserts nothing about the submit, so
``TestDbPasswordGuard`` below is genuinely unpinned even though the same
monkeypatch exists on the branch.
"""
import asyncio
import inspect
import json as _json
import uuid
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi.responses import JSONResponse
from starlette.requests import Request
from local_deep_research.database.models import (
Collection,
Document,
DocumentChunk,
DocumentCollection,
NoteChangeType,
NoteVersion,
RAGIndex,
RagDocumentStatus,
)
from local_deep_research.database.models.library import EmbeddingProvider
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
from tests.notes.helpers import _generate_hash
USERNAME = "testuser"
RAG_FACTORY = (
"local_deep_research.research_library.services."
"rag_service_factory.get_rag_service"
)
# ---------------------------------------------------------------------------
# Route-call harness (only the two route tests need it)
# ---------------------------------------------------------------------------
def _handler(fn):
while hasattr(fn, "__wrapped__"):
fn = fn.__wrapped__
return fn
def _unpack(response):
if isinstance(response, JSONResponse):
return _json.loads(response.body), response.status_code
return response, 200
def _request(path="/", method="GET", query=""):
return Request(
{
"type": "http",
"method": method,
"path": path,
"query_string": query.encode(),
"headers": [],
"session": {"session_id": "sess-1"},
}
)
def _call(path, handler, *args, method="GET", json=None):
raw_path, _, query = path.partition("?")
fn = _handler(handler)
kwargs = {"username": USERNAME}
if "body" in inspect.signature(fn).parameters:
kwargs["body"] = json if json is not None else {}
if inspect.iscoroutinefunction(fn):
return _unpack(
asyncio.run(fn(_request(raw_path, method, query), *args, **kwargs))
)
return _unpack(fn(_request(raw_path, method, query), *args, **kwargs))
@pytest.fixture
def service_session(db_session, monkeypatch):
"""Point ``note_service``'s ``get_user_db_session`` at the test session."""
@contextmanager
def fake_session(username=None, password=None):
yield db_session
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service.get_user_db_session",
fake_session,
)
return db_session
@pytest.fixture
def route_session(db_session, monkeypatch):
"""Both import sites -- the route opens its own session in some paths."""
@contextmanager
def fake_session(username=None, password=None):
yield db_session
monkeypatch.setattr(
"local_deep_research.database.session_context.get_user_db_session",
fake_session,
)
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service.get_user_db_session",
fake_session,
)
return db_session
def _seed_notes_collection(session):
"""Seed the system Notes collection so ``create_note``'s
``_get_or_create_notes_collection`` doesn't have to invent one."""
collection = Collection(
id=str(uuid.uuid4()),
name="Notes",
description="default",
collection_type="notes",
)
session.add(collection)
session.commit()
return collection
def _seed_note_with_collection(session, note_source_type, indexed=True):
"""A note plus a Notes collection plus their (indexed) link row."""
note_id = str(uuid.uuid4())
collection_id = str(uuid.uuid4())
session.add(
Document(
id=note_id,
title="Original",
text_content="original content",
file_type="note",
file_size=16,
source_type_id=note_source_type.id,
document_hash=_generate_hash(f"{note_id}:original content"),
tags=["old"],
)
)
session.add(
Collection(
id=collection_id,
name="Notes",
description="default",
collection_type="notes",
)
)
session.add(
DocumentCollection(
document_id=note_id,
collection_id=collection_id,
indexed=indexed,
)
)
session.commit()
return note_id, collection_id
# ---------------------------------------------------------------------------
class TestPreviewRouteResponseShape:
"""The ``/synthesize/preview`` route used to return
``{"success": True, "preview": {...}}`` while the JS reads
``data.result``. Pre-fix the preview pane was always blank and the
``truncated_sources`` warning never fired. The fix renames the key to
``result``, matching the sibling synthesize-create route.
"""
def test_preview_route_returns_result_not_preview_key(self):
from local_deep_research.web.routers import notes as notes_routes
ai_result = {
"source_notes": [
{"id": "id-a", "title": "A"},
{"id": "id-b", "title": "B"},
],
"suggested_title": "synthesized",
"content": "merged",
"truncated_sources": False,
}
with patch.object(notes_routes, "NoteAIService") as mock_ai_cls:
mock_ai = MagicMock()
mock_ai.synthesize_notes = AsyncMock(return_value=ai_result)
mock_ai_cls.return_value = mock_ai
payload, _status = _call(
"/notes/api/notes/synthesize/preview",
notes_routes.preview_synthesis,
method="POST",
json={
"note_ids": ["id-a", "id-b"],
"synthesis_type": "merge",
},
)
assert payload["success"] is True
assert "result" in payload, (
"preview route must return data.result -- the JS reads that key. "
"Reverting the fix breaks the entire Preview pane."
)
assert payload["result"] == ai_result
assert "preview" not in payload, (
"the old `preview` key must not also be present -- the renamed "
"field is the canonical one."
)
class TestUpdateNoteResetsIndexedFlag:
"""``update_note`` must reset ``DocumentCollection.indexed=False`` on a
content or title change so the auto-index worker (which uses
``force_reindex=False``) actually re-embeds. Pre-fix semantic search
returned stale embeddings indefinitely after an edit.
"""
def test_content_change_marks_indexed_false(
self, service_session, note_source_type
):
note_id, collection_id = _seed_note_with_collection(
service_session, note_source_type
)
assert NoteService(username=USERNAME).update_note(
note_id, content="updated content"
)
link = (
service_session.query(DocumentCollection)
.filter_by(document_id=note_id, collection_id=collection_id)
.one()
)
assert link.indexed is False, (
"update_note must reset DocumentCollection.indexed=False on "
"content change -- without it the auto-index worker skips the "
"doc (force_reindex=False) and semantic search returns the "
"pre-edit embedding indefinitely."
)
def test_title_only_change_marks_indexed_false(
self, service_session, note_source_type
):
note_id, collection_id = _seed_note_with_collection(
service_session, note_source_type
)
assert NoteService(username=USERNAME).update_note(
note_id, title="Renamed"
)
link = (
service_session.query(DocumentCollection)
.filter_by(document_id=note_id, collection_id=collection_id)
.one()
)
# Title is embedded in FAISS chunk metadata; a rename without reset
# would leave the old title surfacing in search results.
assert link.indexed is False, (
"title-only changes must also trigger reindex -- the title "
"appears in chunk metadata returned by semantic search."
)
def test_tag_only_change_does_not_reset_indexed(
self, service_session, note_source_type
):
"""Tag changes do not affect embeddings, so the indexed flag stays
True. This pins the SCOPE of the fix -- we don't want every tag
toggle to trigger a costly reindex."""
note_id, collection_id = _seed_note_with_collection(
service_session, note_source_type
)
assert NoteService(username=USERNAME).update_note(note_id, tags=["new"])
link = (
service_session.query(DocumentCollection)
.filter_by(document_id=note_id, collection_id=collection_id)
.one()
)
assert link.indexed is True, (
"tag-only changes must not invalidate the FAISS index -- tags "
"aren't part of the embedding."
)
class TestEditConvergesBothIndexedStateSources:
"""An edit must invalidate BOTH indexed-state sources, not just the
legacy ``DocumentCollection.indexed`` flag.
``RagDocumentStatus`` row-existence is the canonical "indexed" marker the
RAG status route and ``get_rag_stats`` read; ``index_document`` writes it
AND ``DocumentCollection.indexed`` together. Pre-fix, update_note /
restore flipped only the flag and left the status row, so the status
report showed an edited note as still-indexed until (and unless) a
re-index actually ran. The fix deletes the status row in the same
transaction (``_mark_note_stale_for_reindex_in_session``).
Neither ``RagDocumentStatus`` nor ``_mark_note_stale_for_reindex*``
appears in any branch test.
"""
def _seed_indexed_note(self, session, note_source_type):
"""Seed a note that is fully 'indexed' in BOTH sources."""
note_id, collection_id = _seed_note_with_collection(
session, note_source_type
)
rag_index = RAGIndex(
collection_name=f"collection_{collection_id}",
embedding_model="all-MiniLM-L6-v2",
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
embedding_dimension=384,
index_path=f"/tmp/{collection_id}.faiss",
index_hash=_generate_hash(f"idx:{collection_id}"),
chunk_size=1000,
chunk_overlap=100,
)
session.add(rag_index)
session.flush() # need rag_index.id for the status FK
session.add(
RagDocumentStatus(
document_id=note_id,
collection_id=collection_id,
rag_index_id=rag_index.id,
chunk_count=3,
)
)
session.commit()
return note_id, collection_id
def _both_sources(self, session, note_id):
link = (
session.query(DocumentCollection)
.filter_by(document_id=note_id)
.one()
)
status_rows = (
session.query(RagDocumentStatus)
.filter_by(document_id=note_id)
.count()
)
return link.indexed, status_rows
def test_update_note_content_change_deletes_rag_document_status(
self, service_session, note_source_type
):
note_id, _ = self._seed_indexed_note(service_session, note_source_type)
# Precondition: both sources say "indexed".
assert self._both_sources(service_session, note_id) == (True, 1)
NoteService(USERNAME).update_note(note_id, content="new content")
service_session.expire_all()
indexed, status_rows = self._both_sources(service_session, note_id)
assert indexed is False
assert status_rows == 0, (
"update_note must delete the RagDocumentStatus row on a content "
"edit -- leaving it makes the RAG status route report a stale "
"note as still-indexed."
)
def test_restore_deletes_rag_document_status(
self, service_session, note_source_type
):
service = NoteService(USERNAME)
note_id, _ = self._seed_indexed_note(service_session, note_source_type)
# Create a prior version to restore to.
service.update_note(note_id, content="v2 content")
service_session.expire_all()
target = (
service_session.query(NoteVersion)
.filter_by(document_id=note_id)
.order_by(NoteVersion.created_at.asc())
.first()
)
# Re-index back to a clean state so the precondition holds again.
service_session.query(DocumentCollection).filter_by(
document_id=note_id
).update({DocumentCollection.indexed: True})
if (
service_session.query(RagDocumentStatus)
.filter_by(document_id=note_id)
.count()
== 0
):
rag_index = service_session.query(RAGIndex).first()
coll = (
service_session.query(DocumentCollection)
.filter_by(document_id=note_id)
.one()
)
service_session.add(
RagDocumentStatus(
document_id=note_id,
collection_id=coll.collection_id,
rag_index_id=rag_index.id,
chunk_count=3,
)
)
service_session.commit()
assert self._both_sources(service_session, note_id) == (True, 1)
ok, err = service.restore_with_bookends(note_id, target.id)
assert ok is True, err
service_session.expire_all()
indexed, status_rows = self._both_sources(service_session, note_id)
assert indexed is False
assert status_rows == 0, (
"restore must delete the RagDocumentStatus row so the status "
"report doesn't show the pre-restore state as indexed."
)
class TestIdenticalNotesContentHashScoping:
"""The version-dedup constraint ``UNIQUE(document_id, content_hash)`` is
scoped PER document_id, and ``Document.document_hash`` is salted with the
per-note uuid. Together these mean two distinct notes with byte-identical
title+content+tags both persist: their INITIAL version rows share an
identical content_hash but live under distinct document_ids, so there is
no cross-note UNIQUE collision and no globally-unique
``Document.document_hash`` collision.
Reverting the note_id salt in ``create_note``'s document_hash (hashing
``content`` alone) makes the second ``create_note`` IntegrityError;
widening the version constraint to a global content_hash makes the second
INITIAL snapshot collide. Either revert flips this test.
The branch's ``test_note_edge_cases.py::test_multiple_notes_same_title_allowed``
is a shadow test -- it hand-builds Documents with pre-distinct hashes and
never calls ``create_note``.
"""
def test_create_two_notes_with_identical_content_both_succeed(
self, service_session, note_source_type
):
_seed_notes_collection(service_session)
service = NoteService(username=USERNAME)
title = "Identical Title"
content = "byte-for-byte identical body"
tags = ["alpha", "beta"]
id_a = service.create_note(title=title, content=content, tags=tags)
# Pre-fix (document_hash hashing content alone) this second call
# would raise IntegrityError on Document.document_hash's UNIQUE.
id_b = service.create_note(title=title, content=content, tags=tags)
assert id_a != id_b, "two create_note calls must yield distinct ids"
docs = (
service_session.query(Document)
.filter(Document.id.in_([id_a, id_b]))
.all()
)
assert len(docs) == 2, (
"both notes must persist as separate Document rows despite "
"identical title/content/tags"
)
assert docs[0].document_hash != docs[1].document_hash, (
"document_hash must be salted per-note so identical content "
"doesn't violate the global UNIQUE(document_hash)."
)
v_a = (
service_session.query(NoteVersion).filter_by(document_id=id_a).all()
)
v_b = (
service_session.query(NoteVersion).filter_by(document_id=id_b).all()
)
assert len(v_a) == 1 and len(v_b) == 1
assert v_a[0].content_hash == v_b[0].content_hash, (
"identical title+content+tags must produce the same version "
"content_hash -- the dedup is keyed on (document_id, "
"content_hash), not content_hash alone."
)
assert v_a[0].document_id != v_b[0].document_id
class TestSynthesizeDeduplicatesNoteIds:
"""Duplicate ids in ``note_ids`` used to silently produce duplicate
``NoteSynthesisSource`` rows that hit ``uix_note_synthesis_source`` and
surfaced as an unhandled 500. The fix deduplicates note_ids inside
``synthesize_notes`` BEFORE the 2-5 count check.
"""
def _synthesize(self, note_ids):
from local_deep_research.research_library.notes.services.note_ai_service import (
NoteAIService,
)
svc = NoteAIService(username=USERNAME)
with patch.object(
svc, "_get_note_source_type_id", return_value="dummy-st"
):
with patch(
"local_deep_research.research_library.notes.services.note_ai_service.get_user_db_session"
) as mock_session_ctx:
# A session whose query() yields None for any id, so a list
# that CLEARS the count check falls through to the
# "couldn't find enough notes" branch instead.
fake_session = MagicMock()
fake_session.query.return_value.filter_by.return_value.first.return_value = None
mock_session_ctx.return_value.__enter__.return_value = (
fake_session
)
return asyncio.run(svc.synthesize_notes(note_ids, "merge"))
def test_duplicate_only_input_is_rejected_by_the_count_check(self):
"""``[X, X]`` must dedup to ONE id and be rejected by the 2-5 gate.
This is the discriminating case. ``[X, X, Y]`` cannot distinguish the
two implementations -- 2 and 3 are both inside [2, 5] -- so main's
version of this test would survive deleting the dedup (verified by
mutation here). Two entries collapsing to one is the only input where
dedup-before-count and count-before-dedup disagree on the outcome.
"""
same_id = str(uuid.uuid4())
result = self._synthesize([same_id, same_id])
assert result.get("success") is False
assert "2-5" in result.get("error", ""), (
"synthesize_notes must dedup BEFORE the 2-5 count check: "
"[X, X] is ONE distinct note and must be refused. Without the "
f"dedup it counts as 2 and slips through. Got: {result!r}"
)
def test_synthesize_deduplicates_input_ids(self):
"""``[X, X, Y]`` (2 distinct) still clears the count gate.
Positive control for the test above: the dedup must not reject a
list that merely CONTAINS duplicates.
"""
same_id = str(uuid.uuid4())
other_id = str(uuid.uuid4())
result = self._synthesize([same_id, same_id, other_id])
assert result.get("success") is False
assert "2-5" not in result.get("error", ""), (
"[X, X, Y] dedups to 2 distinct ids, which IS in range -- it "
"must reach the note-lookup branch, not the count refusal."
)
class TestPruneVersionsBookendCeiling:
"""The bookend-ceiling prune branch bounds the un-prunable bookend pool
to ``MAX_BOOKEND_VERSIONS``, deleting the OLDEST bookends. Without it,
repeated restores grow ``note_versions`` forever (each restore writes two
un-prunable bookend rows).
``tests/research_library/test_note_service_contracts.py::test_bookend_pool_has_its_own_independent_cap``
asserts the surviving COUNT only -- an inverted ordering that kept the
OLDEST bookends and deleted the newest still passes it. WHICH bookends
survive is what this test pins.
"""
@pytest.fixture
def patched_service(self, service_session):
return NoteService(username=USERNAME)
def test_prune_bounds_bookend_pool_to_max_bookend_versions_keeping_newest(
self, service_session, note_source_type, patched_service, monkeypatch
):
from local_deep_research.research_library.notes.services import (
note_service as note_service_mod,
)
# Patch the module-level constants the branch actually reads so the
# test is feasible without thousands of rows: a small bookend ceiling
# and a large per-note cap (so the FIRST/non-bookend FIFO branch never
# fires and we isolate the bookend-ceiling branch).
monkeypatch.setattr(note_service_mod, "MAX_BOOKEND_VERSIONS", 5)
monkeypatch.setattr(note_service_mod, "MAX_VERSIONS_PER_NOTE", 1000)
note_id = str(uuid.uuid4())
service_session.add(
Document(
id=note_id,
title="N",
text_content="x",
file_type="note",
file_size=1,
source_type_id=note_source_type.id,
document_hash=_generate_hash(note_id),
tags=[],
)
)
service_session.commit()
base = datetime.now(timezone.utc) - timedelta(days=2)
n_bookends = 5 + 3
bookend_records = [] # (i, id, created_at)
for i in range(n_bookends):
change_type = (
NoteChangeType.PRE_RESTORE.value
if i % 2 == 0
else NoteChangeType.RESTORE.value
)
created_at = base + timedelta(seconds=i)
v = NoteVersion(
id=str(uuid.uuid4()),
document_id=note_id,
title="N",
content=f"bookend {i}",
tags=[],
change_type=change_type,
content_hash=f"hash-{i}",
created_at=created_at,
)
service_session.add(v)
bookend_records.append((i, v.id, created_at))
# A couple of AUTO_SAVE rows to confirm they aren't miscounted as
# bookends by the ceiling branch.
for j in range(2):
service_session.add(
NoteVersion(
id=str(uuid.uuid4()),
document_id=note_id,
title="N",
content=f"auto {j}",
tags=[],
change_type=NoteChangeType.AUTO_SAVE.value,
content_hash=f"auto-{j}",
created_at=base + timedelta(seconds=100 + j),
)
)
service_session.commit()
patched_service._prune_versions_in_session(service_session, note_id)
service_session.commit()
bookend_types = (
NoteChangeType.PRE_RESTORE.value,
NoteChangeType.RESTORE.value,
)
surviving_bookends = (
service_session.query(NoteVersion)
.filter(NoteVersion.document_id == note_id)
.filter(NoteVersion.change_type.in_(bookend_types))
.order_by(NoteVersion.created_at.asc())
.all()
)
# Exactly MAX_BOOKEND_VERSIONS (=5) survive.
assert len(surviving_bookends) == 5, (
"the bookend-ceiling branch must bound the bookend pool to "
f"MAX_BOOKEND_VERSIONS; got {len(surviving_bookends)} surviving."
)
# The 5 NEWEST survive -- i.e. the 3 oldest were deleted. A desc()
# ordering / wrong tiebreak would delete the newest instead.
surviving_ids = {v.id for v in surviving_bookends}
by_time = sorted(bookend_records, key=lambda r: r[2])
newest_five_ids = {rec_id for (_i, rec_id, _ts) in by_time[-5:]}
assert surviving_ids == newest_five_ids, (
"the bookend-ceiling branch must keep the NEWEST bookends and "
"delete the oldest; an inverted ordering drops the wrong rows."
)
for _i, old_id, _ts in by_time[:3]:
assert old_id not in surviving_ids, (
f"oldest bookend {old_id} should have been pruned by the "
"bookend-ceiling branch."
)
class TestDeleteNoteRagCleanup:
"""``delete_note`` must drop RAG entries for EVERY indexed collection the
note belonged to -- the ghost-embedding invariant. The indexed-collection
set must be captured BEFORE ``session.delete()``; afterwards the
Document->DocumentCollection cascade has already destroyed those rows.
The branch pins only the all-succeed and never-constructed cases.
"""
def test_delete_note_removes_rag_entries_for_every_indexed_collection(
self, service_session, note_source_type
):
note_id = str(uuid.uuid4())
service_session.add(
Document(
id=note_id,
title="RAG note",
text_content="indexed content",
file_type="note",
file_size=15,
source_type_id=note_source_type.id,
document_hash=_generate_hash("rag_cleanup_note"),
tags=[],
)
)
coll_a = Collection(
id=str(uuid.uuid4()), name="A", collection_type="notes"
)
coll_b = Collection(
id=str(uuid.uuid4()), name="B", collection_type="custom"
)
coll_c = Collection(
id=str(uuid.uuid4()), name="C", collection_type="notes"
)
service_session.add_all([coll_a, coll_b, coll_c])
service_session.add_all(
[
DocumentCollection(
document_id=note_id, collection_id=coll_a.id, indexed=True
),
DocumentCollection(
document_id=note_id, collection_id=coll_b.id, indexed=True
),
DocumentCollection(
document_id=note_id, collection_id=coll_c.id, indexed=False
),
]
)
service_session.commit()
mock_rag = MagicMock()
mock_rag.purge_document_chunks = MagicMock()
with patch(RAG_FACTORY) as mock_factory:
mock_factory.return_value.__enter__.return_value = mock_rag
ok = NoteService(username=USERNAME).delete_note(note_id)
assert ok is True
# MUST be purge_document_chunks, NOT remove_document_from_rag: the
# Document (and its DocumentCollection join rows) are already
# cascade-deleted by the time cleanup runs, so the join-row lookup
# inside remove_document_from_rag would no-op and orphan the chunks
# (the ghost-embedding bug).
assert mock_rag.remove_document_from_rag.call_count == 0, (
"delete_note must NOT use remove_document_from_rag -- after the "
"cascade its DocumentCollection lookup no-ops, leaving orphaned "
"chunks. It must use purge_document_chunks."
)
assert mock_rag.purge_document_chunks.call_count == 2, (
"purge_document_chunks must be called once per INDEXED "
"collection (2), not for the indexed=False one. A call_count of "
"0 means the capture moved after session.delete()."
)
calls = mock_rag.purge_document_chunks.call_args_list
for c in calls:
assert c.args[0] == note_id
called_collection_ids = {c.args[1] for c in calls}
assert called_collection_ids == {coll_a.id, coll_b.id}
# FAISS vectors are purged too, once per indexed collection --
# replace-on-reindex can never fire again for a deleted id, so
# without this the deleted note's text kept surfacing in collection
# search. Vectors purge BEFORE chunk rows: the rows are the ownership
# evidence that protects chunks shared with other documents.
assert mock_rag.purge_document_vectors.call_count == 2
vector_ids = {
c.args[1] for c in mock_rag.purge_document_vectors.call_args_list
}
assert vector_ids == {coll_a.id, coll_b.id}
per_collection_order = [
call[0]
for call in mock_rag.method_calls
if call[0] in ("purge_document_vectors", "purge_document_chunks")
]
assert (
per_collection_order
== ["purge_document_vectors", "purge_document_chunks"] * 2
), "vectors must be purged before their ownership rows"
assert coll_c.id not in called_collection_ids, (
"the indexed=False collection must be excluded from RAG cleanup."
)
def test_delete_note_isolates_per_collection_purge_failure(
self, service_session, note_source_type
):
"""RAG cleanup is best-effort PER collection. A purge failure for one
indexed collection must NOT abort the purge of the others, and
delete_note must still return True (the DB delete already committed).
The branch covers all-succeed and all-raise; this pins the mixed case
the per-collection try/except exists for -- without it the first
failure skips the remaining collections and orphans their chunks."""
note_id = str(uuid.uuid4())
service_session.add(
Document(
id=note_id,
title="RAG note",
text_content="indexed content",
file_type="note",
file_size=15,
source_type_id=note_source_type.id,
document_hash=_generate_hash("rag_isolation_note"),
tags=[],
)
)
coll_a = Collection(
id=str(uuid.uuid4()), name="A", collection_type="notes"
)
coll_b = Collection(
id=str(uuid.uuid4()), name="B", collection_type="notes"
)
service_session.add_all([coll_a, coll_b])
service_session.add_all(
[
DocumentCollection(
document_id=note_id, collection_id=coll_a.id, indexed=True
),
DocumentCollection(
document_id=note_id, collection_id=coll_b.id, indexed=True
),
]
)
service_session.commit()
purged = []
def _purge(nid, collection_id):
# Raise on the FIRST collection; the loop must still reach the
# second.
purged.append(collection_id)
if len(purged) == 1:
raise RuntimeError(
"FAISS purge failed for the first collection"
)
mock_rag = MagicMock()
mock_rag.purge_document_chunks = MagicMock(side_effect=_purge)
with patch(RAG_FACTORY) as mock_factory:
mock_factory.return_value.__enter__.return_value = mock_rag
ok = NoteService(username=USERNAME).delete_note(note_id)
assert ok is True
assert mock_rag.purge_document_chunks.call_count == 2
assert set(purged) == {coll_a.id, coll_b.id}
assert (
service_session.query(Document).filter_by(id=note_id).first()
is None
)
def test_delete_note_purges_edit_window_collection_with_chunks(
self, service_session, note_source_type
):
"""Edit->reindex window: ``DocumentCollection.indexed`` is False
(cleared by the edit) but the note's pre-edit chunk rows/vectors
still exist. delete_note must STILL purge them -- the gate is
chunk-row existence, not the flag -- or they orphan."""
note_id = str(uuid.uuid4())
service_session.add(
Document(
id=note_id,
title="edited note",
text_content="stale content",
file_type="note",
file_size=13,
source_type_id=note_source_type.id,
document_hash=_generate_hash("edit_window_note"),
tags=[],
)
)
coll = Collection(
id=str(uuid.uuid4()), name="edited", collection_type="notes"
)
service_session.add(coll)
service_session.add(
DocumentCollection(
document_id=note_id, collection_id=coll.id, indexed=False
)
)
service_session.add(
DocumentChunk(
chunk_hash=_generate_hash("ew_chunk"),
source_type="document",
source_id=note_id,
collection_name=f"collection_{coll.id}",
chunk_text="stale",
chunk_index=0,
start_char=0,
end_char=5,
word_count=1,
embedding_id=str(uuid.uuid4()),
embedding_model="m",
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
embedding_dimension=2,
)
)
service_session.commit()
mock_rag = MagicMock()
with patch(RAG_FACTORY) as mock_factory:
mock_factory.return_value.__enter__.return_value = mock_rag
ok = NoteService(username=USERNAME).delete_note(note_id)
assert ok is True
# Despite indexed=False, the chunk-bearing collection is purged.
purged = {
c.args[1] for c in mock_rag.purge_document_chunks.call_args_list
}
assert coll.id in purged
class TestDbPasswordGuard:
"""When ``_capture_request_db_password`` returns None (rare -- the
request-context teardown races the worker submit), ``update_note`` must
NOT enqueue the change-summary worker. Pre-fix the worker was scheduled
anyway and silently failed to open the encrypted DB; users saw
"change_summary missing" with zero operator signal.
``tests/notes/test_note_stress.py`` monkeypatches the same capture to
``None`` but only as a harness convenience -- it never asserts the
submit did not happen, so the guard itself is unpinned there.
"""
def test_dbpw_none_skips_summary_submit(
self, service_session, note_source_type, monkeypatch
):
note_id = str(uuid.uuid4())
service_session.add(
Document(
id=note_id,
title="N",
text_content="original",
file_type="note",
file_size=8,
source_type_id=note_source_type.id,
document_hash=_generate_hash(f"{note_id}:original"),
tags=[],
)
)
service_session.commit()
# Force the capture to return None -- simulates the worker-submit
# timing race where the request context's password store has already
# been torn down.
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service._capture_request_db_password",
lambda username: None,
)
submit_calls = []
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service._submit_summary_task",
lambda fn, *args, **kwargs: submit_calls.append((fn, args, kwargs)),
)
# A content change is required to reach the summary-submit branch.
assert NoteService(username=USERNAME).update_note(
note_id, content="changed content"
)
assert submit_calls == [], (
"When dbpw is None, _submit_summary_task must NOT be called -- "
"the worker can't open the encrypted DB without the password and "
"would silently drop the summary."
)
def test_dbpw_present_does_submit_the_summary(
self, service_session, note_source_type, monkeypatch
):
"""Positive control: with a password the worker IS scheduled, so the
test above can't pass because the branch became unreachable."""
note_id = str(uuid.uuid4())
service_session.add(
Document(
id=note_id,
title="N",
text_content="original",
file_type="note",
file_size=8,
source_type_id=note_source_type.id,
document_hash=_generate_hash(f"{note_id}:original2"),
tags=[],
)
)
service_session.commit()
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service._capture_request_db_password",
lambda username: "a-password",
)
submit_calls = []
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service._submit_summary_task",
lambda fn, *args, **kwargs: submit_calls.append((fn, args, kwargs)),
)
assert NoteService(username=USERNAME).update_note(
note_id, content="changed content"
)
assert len(submit_calls) == 1, (
"with a db password the change-summary worker must be scheduled"
)
class TestUpdateNoteRouteResetsIndexedFlag:
"""Route-level companion to ``TestUpdateNoteResetsIndexedFlag``.
The service-level tests exercise ``NoteService.update_note`` directly;
this drives the real PUT handler to catch regressions where the ROUTE
layer (rather than the service) bypasses the reindex-flag reset -- e.g.
a change that calls a different update code path or mutates
DocumentCollection in the route itself.
"""
def test_put_route_resets_indexed_flag(
self, route_session, note_source_type
):
from local_deep_research.web.routers import notes as notes_routes
note_id, collection_id = _seed_note_with_collection(
route_session, note_source_type
)
# The route delegates to _trigger_note_auto_index which imports
# trigger_auto_index lazily; stub it so the test doesn't spin up
# background threads / the RAG service.
with patch.object(
notes_routes, "_trigger_note_auto_index", lambda *a, **kw: None
):
payload, status = _call(
f"/notes/api/notes/{note_id}",
notes_routes.update_note,
note_id,
method="PUT",
json={"content": "updated content"},
)
assert status == 200, payload
assert payload["success"] is True
link = (
route_session.query(DocumentCollection)
.filter_by(document_id=note_id, collection_id=collection_id)
.one()
)
assert link.indexed is False, (
"PUT /api/notes/<id> must result in DocumentCollection."
"indexed=False so the auto-index worker re-embeds."
)
class TestWikiLinkRenameSafety:
"""Regression guards for the 3-priority resolver in
``_parse_and_update_links_in_session``.
``tests/notes/test_note_service.py::test_auto_suggested_link_survives_target_rename``
already covers the Priority-2 cache HIT (rename). The two branches below
-- cache entry pointing at a DELETED document -- have no successor, and
they are the ones that decide whether a stale id can be resurrected.
"""
def test_link_to_deleted_target_does_not_resurrect(
self, service_session, note_source_type
):
"""The fallback must NOT resurrect links whose target was deleted (as
opposed to renamed). If target_document_id no longer exists, the link
is dropped -- the fallback only covers rename."""
from local_deep_research.database.models import NoteLink
_seed_notes_collection(service_session)
service = NoteService(username=USERNAME)
target_id = service.create_note(title="Doomed", content="target body")
source_id = service.create_note(
title="Source", content="See [[Doomed]] for details."
)
# Delete the target. Sanity: the NoteLink row is gone via CASCADE.
service.delete_note(target_id)
service_session.expire_all()
assert (
service_session.query(NoteLink)
.filter_by(source_document_id=source_id)
.count()
== 0
)
# Resave the source. Title lookup fails (no doc named "Doomed");
# the fallback should also fail (target_id no longer exists).
service.update_note(source_id, content="See [[Doomed]] anyway.")
service_session.expire_all()
assert (
service_session.query(NoteLink)
.filter_by(source_document_id=source_id)
.count()
== 0
), (
"Fallback resurrected a link to a deleted document. It must "
"verify the Document still exists before reusing the captured "
"target_document_id."
)
def test_link_retargets_to_surviving_same_titled_note_when_cached_target_deleted(
self, service_session, note_source_type
):
"""When the cached (Priority-2) target was deleted but ANOTHER note
with the same title survives, the resolver must fall through to
Priority-3 fresh title resolution and retarget to the survivor --
never resurrect the deleted id."""
from local_deep_research.database.models import NoteLink
_seed_notes_collection(service_session)
service = NoteService(username=USERNAME)
target1_id = service.create_note(title="Dup", content="first dup body")
source_id = service.create_note(
title="Source", content="See [[Dup]] here."
)
link = (
service_session.query(NoteLink)
.filter_by(source_document_id=source_id)
.one()
)
assert link.target_document_id == target1_id
# Second note ALSO titled "Dup" -- the survivor.
target2_id = service.create_note(
title="Dup", content="second dup body, the survivor"
)
# Delete the originally-linked target. The cached
# existing_link_targets entry now points at the deleted target1_id.
service.delete_note(target1_id)
# SQLite performed the NoteLink delete through ON DELETE CASCADE, so
# the ORM still considers the previously loaded ``link`` persistent.
# Expunge that stale identity before SQLite reuses its integer primary
# key for the replacement link below.
service_session.expunge(link)
service_session.expire_all()
service.update_note(
source_id, content="See [[Dup]] still here (edited)."
)
service_session.expire_all()
surviving = (
service_session.query(NoteLink)
.filter_by(source_document_id=source_id)
.all()
)
assert len(surviving) == 1, (
"link must not be dropped -- Priority-3 fresh title resolution "
f"should retarget to the survivor: {surviving!r}"
)
assert surviving[0].target_document_id == target2_id, (
"link must retarget to the surviving same-titled note via "
"Priority-3, not stay pinned to the deleted target."
)
assert surviving[0].target_document_id != target1_id, (
"the deleted target id must NOT be resurrected by a stale "
"Priority-2 cache hit."
)