~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 NoteService linking and versioning methods."""
import re
import uuid
import pytest
from local_deep_research.database.models import (
Document,
DocumentCollection,
NoteLink,
NoteVersion,
)
from tests.notes.helpers import _generate_hash
def _make_note_doc(session, source_type_id, title, content, salt, tags=None):
"""Build, persist, and return a note ``Document``.
Shared by the test classes that need a real note row keyed by a
deterministic ``salt`` (which seeds the document hash). ``tags``
defaults to an empty list, matching how the service treats a missing
tags value (``document.tags or []``).
"""
doc = Document(
id=str(uuid.uuid4()),
title=title,
text_content=content,
file_type="note",
file_size=len(content),
document_hash=_generate_hash(salt),
source_type_id=source_type_id,
tags=tags or [],
)
session.add(doc)
session.commit()
return doc
def _create_snapshot(
service,
session,
note_id,
title,
content,
tags,
change_type="manual_save",
):
"""Seed a NoteVersion row through the production in-session helpers.
Test-side replacement for the removed standalone
``_create_version_snapshot`` wrapper (which had no production
callers): snapshot + prune + commit, exactly what ``update_note``
does inside its transaction.
"""
version_id, _created = service._create_version_snapshot_in_session(
session, note_id, title, content, tags, change_type, None
)
service._prune_versions_in_session(session, note_id)
session.commit()
return version_id
class TestNoteServiceMethods:
"""Test suite for NoteService static methods and utilities."""
# =========================================================================
# Slug Generation Tests
# =========================================================================
def test_generate_slug_simple(self):
"""Test slug generation from simple title."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
slug = NoteService._generate_slug("Hello World")
assert slug == "hello-world"
def test_generate_slug_special_chars(self):
"""Test slug generation removes special characters."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
slug = NoteService._generate_slug("Hello! World? #Test@123")
assert slug == "hello-world-test123"
def test_generate_slug_multiple_spaces(self):
"""Test slug generation handles multiple spaces."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
slug = NoteService._generate_slug("Hello World")
assert slug == "hello-world"
def test_generate_slug_leading_trailing(self):
"""Test slug generation removes leading/trailing hyphens."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
slug = NoteService._generate_slug(" Hello World ")
assert slug == "hello-world"
assert not slug.startswith("-")
assert not slug.endswith("-")
def test_generate_slug_long_title(self):
"""Test slug generation limits length to 500 chars."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
long_title = "a" * 600
slug = NoteService._generate_slug(long_title)
assert len(slug) <= 500
def test_generate_slug_unicode(self):
"""Slug generation folds Latin diacritics to ASCII (NFKD) and drops
scripts with no ASCII form. We intentionally do NOT ship a
transliterator (text-unidecode / python-slugify are copyleft/GPL and
the slug is a non-critical convenience field), so CJK etc. drop out
while the surrounding ASCII survives.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# 世界 has no NFKD ASCII form → dropped; the ASCII words survive.
slug = NoteService._generate_slug("Hello 世界 World")
assert slug == "hello-world"
def test_generate_slug_empty(self):
"""Empty title falls back to ``"note"`` (no longer returns ``""``).
See test_generate_slug_non_latin_titles_degrade_gracefully for the
rationale; downstream consumers need a non-empty identifier.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
slug = NoteService._generate_slug("")
assert slug == "note"
# Whitespace-only has no content to derive from either.
assert NoteService._generate_slug(" ") == "note"
def test_generate_slug_only_special_chars(self):
"""Special-char-only titles have content too, so they take the same
derived fallback as a CJK title rather than the shared constant.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
slug = NoteService._generate_slug("!@#$%^&*()")
assert re.fullmatch(r"note-[0-9a-f]{12}", slug)
assert slug != NoteService._generate_slug("?????")
# =========================================================================
# Content Hash Tests
# =========================================================================
def test_compute_content_hash(self):
"""Test content hash computation."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
content = "Hello World"
hash_value = NoteService._compute_content_hash(content)
# SHA-256 produces 64 hex characters
assert len(hash_value) == 64
assert all(c in "0123456789abcdef" for c in hash_value)
def test_content_hash_deterministic(self):
"""Test that content hash is deterministic."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
content = "Test Content"
hash1 = NoteService._compute_content_hash(content)
hash2 = NoteService._compute_content_hash(content)
assert hash1 == hash2
def test_content_hash_empty_string(self):
"""Test content hash for empty string."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
hash_value = NoteService._compute_content_hash("")
assert len(hash_value) == 64
def test_content_hash_whitespace_sensitivity(self):
"""Test that content hash is sensitive to whitespace."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
hash1 = NoteService._compute_content_hash("Hello World")
hash2 = NoteService._compute_content_hash("Hello World")
assert hash1 != hash2
# =========================================================================
# Link Pattern Tests
# =========================================================================
def test_link_pattern_simple(self):
"""Test simple wiki-style link detection."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "Check out [[My Note]] for more info."
matches = LINK_PATTERN.findall(content)
assert len(matches) == 1
assert matches[0] == "My Note"
def test_link_pattern_multiple(self):
"""Test multiple wiki-style links."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "See [[Note A]] and [[Note B]] and [[Note C]]."
matches = LINK_PATTERN.findall(content)
assert len(matches) == 3
assert "Note A" in matches
assert "Note B" in matches
assert "Note C" in matches
def test_link_pattern_with_special_chars(self):
"""Test link pattern with special characters in note title."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "See [[My Note: A Summary (2024)]]."
matches = LINK_PATTERN.findall(content)
assert len(matches) == 1
assert matches[0] == "My Note: A Summary (2024)"
def test_link_pattern_nested_brackets(self):
"""Test link pattern doesn't match nested brackets incorrectly."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "This is [[a [nested] link]] test."
matches = LINK_PATTERN.findall(content)
# Pattern r"\[\[([^\]]+)\]\]" won't match because [nested] contains ]
# This is expected behavior - nested brackets break the pattern
assert len(matches) == 0
def test_link_pattern_no_links(self):
"""Test content with no links."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "This is plain text with no links."
matches = LINK_PATTERN.findall(content)
assert len(matches) == 0
def test_link_pattern_incomplete_brackets(self):
"""Test incomplete bracket patterns aren't matched."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "This has [single brackets] and [[incomplete."
matches = LINK_PATTERN.findall(content)
assert len(matches) == 0
def test_link_pattern_empty_brackets(self):
"""Test empty brackets aren't matched."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "This has [[]] empty brackets."
matches = LINK_PATTERN.findall(content)
assert len(matches) == 0
def test_link_pattern_multiline(self):
"""Test link pattern works across lines."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = """Line 1 with [[Link A]].
Line 2 with [[Link B]].
Line 3 with [[Link C]]."""
matches = LINK_PATTERN.findall(content)
assert len(matches) == 3
def test_link_pattern_in_code_block(self):
"""Test link pattern within code blocks (should still match)."""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
)
content = "```\n[[Code Link]]\n```"
matches = LINK_PATTERN.findall(content)
# Pattern matches anywhere, filtering is app logic
assert len(matches) == 1
class TestNoteServiceLinkResolution:
"""Test suite for NoteService link resolution."""
@pytest.fixture
def setup_notes(self, db_session, note_source_type):
"""Create sample notes for link resolution tests."""
notes = [
Document(
id=str(uuid.uuid4()),
title="Machine Learning Basics",
text_content="Content about ML",
file_type="note",
file_size=0,
document_hash=_generate_hash("test_service_1"),
source_type_id=note_source_type.id,
),
Document(
id=str(uuid.uuid4()),
title="Deep Learning",
text_content="Content about DL",
file_type="note",
file_size=0,
document_hash=_generate_hash("test_service_2"),
source_type_id=note_source_type.id,
),
Document(
id=str(uuid.uuid4()),
title="Neural Networks",
text_content="Content about NN",
file_type="note",
file_size=0,
document_hash=_generate_hash("test_service_3"),
source_type_id=note_source_type.id,
),
]
for note in notes:
db_session.add(note)
db_session.commit()
return notes
def test_resolve_link_internal_exact_match(self, db_session, setup_notes):
"""Test resolving internal link with exact title match."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
result = service._resolve_link_internal(
db_session, "Machine Learning Basics"
)
assert result is not None
assert result["id"] == setup_notes[0].id
assert result["title"] == "Machine Learning Basics"
def test_resolve_link_internal_case_insensitive(
self, db_session, setup_notes
):
"""Test resolving internal link with case-insensitive match."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
result = service._resolve_link_internal(
db_session, "machine learning basics"
)
assert result is not None
assert result["id"] == setup_notes[0].id
def test_resolve_link_internal_partial_match(self, db_session, setup_notes):
"""Test resolving internal link with partial match."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
result = service._resolve_link_internal(db_session, "Deep")
# Should match "Deep Learning"
assert result is not None
assert result["id"] == setup_notes[1].id
def test_resolve_link_internal_not_found(self, db_session, setup_notes):
"""Test resolving internal link that doesn't exist."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
result = service._resolve_link_internal(db_session, "Nonexistent Note")
assert result is None
def test_resolve_link_internal_like_wildcards_are_literal(
self, db_session, setup_notes
):
"""a LIKE wildcard in the target ([[%]], [[_]]) must be matched
literally by the prefix strategy, not over-match every note. No note
title starts with a literal '%' / '_', so these resolve to None."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
assert service._resolve_link_internal(db_session, "%") is None
assert service._resolve_link_internal(db_session, "_") is None
# A single-char underscore must not match a 1-char prefix of any title.
assert service._resolve_link_internal(db_session, "Machine_") is None
def test_resolve_link_internal_breaks_ties_by_created_at(
self, db_session, note_source_type
):
"""Two notes with identical titles must resolve deterministically.
Without an ``order_by``, ``.first()`` is at the DB's discretion
and can flip between calls. The fix orders by ``created_at`` asc
(then id asc as a final tiebreaker) so the older note wins.
"""
from datetime import datetime, timedelta, timezone
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# Two notes with the same title, distinct created_at + ids
older_id = "00000000-0000-0000-0000-000000000001"
newer_id = "00000000-0000-0000-0000-000000000002"
base = datetime.now(timezone.utc) - timedelta(days=2)
older = Document(
id=older_id,
title="TODO",
text_content="older",
file_type="note",
file_size=0,
document_hash=_generate_hash("dup_older"),
source_type_id=note_source_type.id,
created_at=base,
)
newer = Document(
id=newer_id,
title="TODO",
text_content="newer",
file_type="note",
file_size=0,
document_hash=_generate_hash("dup_newer"),
source_type_id=note_source_type.id,
created_at=base + timedelta(hours=5),
)
db_session.add_all([newer, older]) # insert order != age order
db_session.commit()
service = NoteService("test_user")
result = service._resolve_link_internal(db_session, "TODO")
assert result is not None
assert result["id"] == older_id, (
"duplicate-title resolution should pick the older note"
)
def test_resolve_link_internal_non_ascii_title_exact_typed(
self, db_session, note_source_type
):
"""A title containing an uppercase non-ASCII letter must resolve
when the link text is typed exactly as the title — which is what
the ``[[...]]`` autocomplete inserts.
SQLite's built-in lower() folds ASCII only, while Python's
str.lower() folds full Unicode. Pre-fix the link text was folded
in Python and the title in SQL, so
``lower('Über Alles') = 'über alles'`` was false even for a
byte-identical link — the link was permanently dead. Folding both
sides in SQL restores the match (and keeps ASCII
case-insensitivity, third assertion)."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
doc = _make_note_doc(
db_session, note_source_type.id, "Über Alles", "x", "uml-1"
)
service = NoteService("test_user")
exact = service._resolve_link_internal(db_session, "Über Alles")
assert exact is not None and exact["id"] == doc.id
# ASCII letters still fold case-insensitively around the umlaut.
ascii_case = service._resolve_link_internal(db_session, "Über ALLES")
assert ascii_case is not None and ascii_case["id"] == doc.id
# Prefix strategy folds the same way.
prefix = service._resolve_link_internal(db_session, "Über All")
assert prefix is not None and prefix["id"] == doc.id
def test_parse_links_non_ascii_title_creates_link(
self, db_session, note_source_type
):
"""End-to-end: saving a note with ``[[Über Alles]]`` persists the
NoteLink (the frontend inserts the exact title, so this is the
autocomplete → save → resolve path that was dead pre-fix)."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
target = _make_note_doc(db_session, st, "Über Alles", "x", "uml-t")
source = _make_note_doc(db_session, st, "Src", "y", "uml-s")
svc = NoteService("test_user")
resolved = svc._parse_and_update_links_in_session(
db_session, source.id, "see [[Über Alles]]"
)
db_session.commit()
assert [r["target_id"] for r in resolved] == [target.id]
link = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.one()
)
assert link.target_document_id == target.id
def test_parse_links_repeated_text_does_not_evict_later_distinct_link(
self, db_session, note_source_type, monkeypatch
):
"""The MAX_LINKS_PER_NOTE cap counts DISTINCT link texts, not raw
matches: many repetitions of one ``[[A]]`` must not evict a later
distinct ``[[B]]``. Pre-fix the raw match list was sliced before
dedup, so the repeated text consumed the whole cap."""
from local_deep_research.research_library.notes.services import (
note_service as ns_module,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
monkeypatch.setattr(ns_module, "MAX_LINKS_PER_NOTE", 2)
st = note_source_type.id
a = _make_note_doc(db_session, st, "AAA", "x", "cap-a")
b = _make_note_doc(db_session, st, "BBB", "y", "cap-b")
source = _make_note_doc(db_session, st, "Cap Src", "z", "cap-s")
svc = NoteService("test_user")
content = " ".join(["[[AAA]]"] * 10) + " [[BBB]]"
svc._parse_and_update_links_in_session(db_session, source.id, content)
db_session.commit()
targets = {
link.target_document_id
for link in db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
}
assert targets == {a.id, b.id}
def test_parse_links_resave_keeps_existing_row(
self, db_session, note_source_type
):
"""Re-saving unchanged content must keep the existing NoteLink row
(same primary key) rather than delete-all + recreate: the row's
id/created_at carry provenance and the auto_suggested badge, and
an unchanged link set should write zero rows."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
_make_note_doc(db_session, st, "Alpha", "x", "diff-t")
source = _make_note_doc(db_session, st, "Diff Src", "y", "diff-s")
svc = NoteService("test_user")
svc._parse_and_update_links_in_session(
db_session, source.id, "see [[Alpha]]"
)
db_session.commit()
row = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.one()
)
first_row_id = row.id
svc._parse_and_update_links_in_session(
db_session, source.id, "edited body, same [[Alpha]] link"
)
db_session.commit()
row = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.one()
)
assert row.id == first_row_id
class TestNoteServiceVersionUtils:
"""Test suite for NoteService version utilities."""
def test_version_utility_methods(self):
"""Test version utility methods exist."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# Just test that the service can be instantiated
# Version increment logic is tested via integration tests
assert hasattr(NoteService, "_compute_content_hash")
class TestNoteLinkDatabase:
"""Test suite for NoteLink database operations."""
@pytest.fixture
def sample_notes(self, db_session, note_source_type):
"""Create sample notes for testing."""
notes = []
for i in range(3):
note = Document(
id=str(uuid.uuid4()),
title=f"Note {i}",
text_content=f"Content {i}",
file_type="note",
file_size=0,
document_hash=_generate_hash(f"link_db_test_{i}"),
source_type_id=note_source_type.id,
)
notes.append(note)
db_session.add(note)
db_session.commit()
return notes
def test_create_links(self, db_session, sample_notes):
"""Test creating links between notes."""
source, target1, target2 = sample_notes
link1 = NoteLink(
source_document_id=source.id,
target_document_id=target1.id,
link_text="Note 1",
)
link2 = NoteLink(
source_document_id=source.id,
target_document_id=target2.id,
link_text="Note 2",
)
db_session.add_all([link1, link2])
db_session.commit()
links = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert len(links) == 2
def test_delete_links(self, db_session, sample_notes):
"""Test deleting links."""
source, target, _ = sample_notes
link = NoteLink(
source_document_id=source.id,
target_document_id=target.id,
link_text="Target",
)
db_session.add(link)
db_session.commit()
db_session.delete(link)
db_session.commit()
remaining = db_session.query(NoteLink).count()
assert remaining == 0
def test_backlinks_query(self, db_session, sample_notes):
"""Test querying backlinks (incoming links)."""
source1, source2, target = sample_notes
link1 = NoteLink(
source_document_id=source1.id,
target_document_id=target.id,
link_text="Target",
)
link2 = NoteLink(
source_document_id=source2.id,
target_document_id=target.id,
link_text="Target",
)
db_session.add_all([link1, link2])
db_session.commit()
backlinks = (
db_session.query(NoteLink)
.filter_by(target_document_id=target.id)
.all()
)
assert len(backlinks) == 2
def test_no_self_links(self, db_session, sample_notes):
"""Self-links are rejected by the DB-level CHECK constraint."""
from sqlalchemy.exc import IntegrityError
note = sample_notes[0]
link = NoteLink(
source_document_id=note.id,
target_document_id=note.id,
link_text="Self",
)
db_session.add(link)
with pytest.raises(IntegrityError):
db_session.commit()
db_session.rollback()
assert db_session.query(NoteLink).count() == 0
class TestNoteVersionDatabase:
"""Test suite for NoteVersion database operations."""
@pytest.fixture
def sample_note(self, db_session, note_source_type):
"""Create a sample note for testing."""
note = Document(
id=str(uuid.uuid4()),
title="Versioned Note",
text_content="Initial content",
file_type="note",
file_size=0,
document_hash=_generate_hash("version_test"),
source_type_id=note_source_type.id,
)
db_session.add(note)
db_session.commit()
return note
def test_version_creation(self, db_session, sample_note):
"""Test creating note versions."""
version = NoteVersion(
id=str(uuid.uuid4()),
document_id=sample_note.id,
title="Versioned Note",
content="Initial content",
change_type="initial",
content_hash="hash1",
)
db_session.add(version)
db_session.commit()
saved = db_session.query(NoteVersion).first()
assert saved.id is not None
assert saved.change_type == "initial"
def test_version_ordering(self, db_session, sample_note):
"""Test version ordering by created_at."""
for i in range(1, 4):
version = NoteVersion(
id=str(uuid.uuid4()),
document_id=sample_note.id,
title=f"Title v{i}",
content=f"Content v{i}",
change_type="manual_save",
content_hash=f"hash{i}",
)
db_session.add(version)
db_session.commit()
versions = (
db_session.query(NoteVersion)
.filter_by(document_id=sample_note.id)
.order_by(NoteVersion.created_at.desc())
.all()
)
assert len(versions) == 3
def test_content_hash_deduplication(self, db_session, sample_note):
"""UNIQUE(document_id, content_hash) enforces dedup at the DB level."""
from sqlalchemy.exc import IntegrityError
v1 = NoteVersion(
id=str(uuid.uuid4()),
document_id=sample_note.id,
title="Title",
content="Same content",
change_type="initial",
content_hash="same_hash",
)
v2 = NoteVersion(
id=str(uuid.uuid4()),
document_id=sample_note.id,
title="Title",
content="Same content",
change_type="manual_save",
content_hash="same_hash",
)
db_session.add(v1)
db_session.commit()
db_session.add(v2)
with pytest.raises(IntegrityError):
db_session.commit()
db_session.rollback()
versions = db_session.query(NoteVersion).all()
assert len(versions) == 1
class TestNoteResearchServiceMethods:
"""Tests for new NoteResearch service methods (update + reorder).
These methods use get_user_db_session internally; we patch it to yield
the test db_session.
"""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
"""Patch NoteService's get_user_db_session to yield the test session."""
from contextlib import contextmanager
@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 setup_note_with_research(self, patched_session, note_source_type):
"""Create a note and 3 linked research rows."""
from local_deep_research.database.models import (
NoteResearch,
ResearchHistory,
)
note = Document(
id=str(uuid.uuid4()),
title="Test Note",
text_content="x",
file_type="note",
file_size=0,
document_hash=_generate_hash("test_research_svc"),
source_type_id=note_source_type.id,
)
patched_session.add(note)
patched_session.commit()
from datetime import datetime, timezone
research_ids = [str(uuid.uuid4()) for _ in range(3)]
now = datetime.now(timezone.utc)
for i, rid in enumerate(research_ids):
rh = ResearchHistory(
id=rid,
query=f"query {i}",
mode="quick",
status="completed",
created_at=now.isoformat(),
)
patched_session.add(rh)
patched_session.commit()
for i, rid in enumerate(research_ids):
nr = NoteResearch(
document_id=note.id,
research_id=rid,
display_order=i,
)
patched_session.add(nr)
patched_session.commit()
return note, research_ids
def test_update_note_research_toggles_collapsed(
self, patched_session, setup_note_with_research
):
from local_deep_research.database.models import NoteResearch
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note, research_ids = setup_note_with_research
service = NoteService("test_user")
ok = service.update_note_research(
note.id, research_ids[0], is_collapsed=True
)
assert ok is True
nr = (
patched_session.query(NoteResearch)
.filter_by(document_id=note.id, research_id=research_ids[0])
.first()
)
assert nr.is_collapsed is True
def test_update_note_research_missing_returns_false(
self, patched_session, setup_note_with_research
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note, _ = setup_note_with_research
service = NoteService("test_user")
ok = service.update_note_research(
note.id, "nonexistent-id", is_collapsed=True
)
assert ok is False
def test_reorder_note_research_writes_new_order(
self, patched_session, setup_note_with_research
):
from local_deep_research.database.models import NoteResearch
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note, research_ids = setup_note_with_research
service = NoteService("test_user")
# Reverse order
reversed_ids = list(reversed(research_ids))
ok = service.reorder_note_research(note.id, reversed_ids)
assert ok is True
rows = (
patched_session.query(NoteResearch)
.filter_by(document_id=note.id)
.all()
)
by_id = {r.research_id: r.display_order for r in rows}
for idx, rid in enumerate(reversed_ids):
assert by_id[rid] == idx
def test_reorder_rejects_wrong_id_set(
self, patched_session, setup_note_with_research
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note, research_ids = setup_note_with_research
service = NoteService("test_user")
# Missing one id
ok = service.reorder_note_research(note.id, research_ids[:2])
assert ok is False
def test_reorder_rejects_extra_id(
self, patched_session, setup_note_with_research
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note, research_ids = setup_note_with_research
service = NoteService("test_user")
# Adding an id that doesn't belong to this note
extra = research_ids + [str(uuid.uuid4())]
ok = service.reorder_note_research(note.id, extra)
assert ok is False
def test_update_note_research_no_fields_is_noop(
self, patched_session, setup_note_with_research
):
"""Passing is_collapsed=None should leave the row unchanged."""
from local_deep_research.database.models import NoteResearch
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note, research_ids = setup_note_with_research
service = NoteService("test_user")
ok = service.update_note_research(
note.id, research_ids[0], is_collapsed=None
)
assert ok is True
nr = (
patched_session.query(NoteResearch)
.filter_by(document_id=note.id, research_id=research_ids[0])
.first()
)
assert nr.is_collapsed is False
class TestNoteAcceptSuggestedLink:
"""Tests for NoteService.accept_suggested_link."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
"""Patch get_user_db_session to yield the test session."""
from contextlib import contextmanager
@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 two_notes(self, patched_session, note_source_type):
"""Create source + target notes."""
source = Document(
id=str(uuid.uuid4()),
title="Source Note",
text_content="Initial content.",
file_type="note",
file_size=0,
document_hash=_generate_hash("accept_src"),
source_type_id=note_source_type.id,
)
target = Document(
id=str(uuid.uuid4()),
title="Target Note",
text_content="Target body.",
file_type="note",
file_size=0,
document_hash=_generate_hash("accept_tgt"),
source_type_id=note_source_type.id,
)
patched_session.add_all([source, target])
patched_session.commit()
return source, target
def test_accept_inserts_wiki_link_into_content(
self, patched_session, two_notes
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes
service = NoteService("test_user")
result = service.accept_suggested_link(source.id, target.id)
assert result is not None
patched_session.refresh(source)
assert "[[Target Note]]" in source.text_content
def test_accept_creates_notelink_with_auto_suggested_true(
self, patched_session, two_notes
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes
service = NoteService("test_user")
service.accept_suggested_link(source.id, target.id)
link = (
patched_session.query(NoteLink)
.filter_by(
source_document_id=source.id, target_document_id=target.id
)
.first()
)
assert link is not None
assert link.auto_suggested is True
def test_accept_refuses_when_same_text_links_to_different_note(
self, patched_session, note_source_type
):
"""Duplicate-title guard: if the note already links this exact text
to a DIFFERENT note, accepting must refuse (return None) rather than
silently retarget — and thereby delete — the user's existing edge.
Bare [[title]] cannot express two targets for the same text."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source = Document(
id=str(uuid.uuid4()),
title="Src",
text_content="See [[Dup]] for details.",
file_type="note",
file_size=0,
document_hash=_generate_hash("dup_src"),
source_type_id=note_source_type.id,
)
t1 = Document(
id=str(uuid.uuid4()),
title="Dup",
text_content="first",
file_type="note",
file_size=0,
document_hash=_generate_hash("dup_t1"),
source_type_id=note_source_type.id,
)
t2 = Document(
id=str(uuid.uuid4()),
title="Dup",
text_content="second",
file_type="note",
file_size=0,
document_hash=_generate_hash("dup_t2"),
source_type_id=note_source_type.id,
)
patched_session.add_all([source, t1, t2])
patched_session.flush()
# The user's existing hand-typed [[Dup]] resolves to t1.
patched_session.add(
NoteLink(
source_document_id=source.id,
target_document_id=t1.id,
link_text="Dup",
)
)
patched_session.commit()
service = NoteService("test_user")
result = service.accept_suggested_link(source.id, t2.id)
assert result is None
# The user's edge to t1 must survive untouched...
assert (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id, target_document_id=t1.id)
.first()
is not None
)
# ...and no edge to t2 was created.
assert (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id, target_document_id=t2.id)
.first()
is None
)
def test_accept_collapses_newline_in_target_title(
self, patched_session, note_source_type
):
"""A target title containing a newline must be collapsed to a
single-line [[link]] the parser can actually match — otherwise the
appended '[[a\\nb]]' is a dead literal, no NoteLink is created, and
every retry appends more junk while reporting success."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source = Document(
id=str(uuid.uuid4()),
title="Src",
text_content="body",
file_type="note",
file_size=0,
document_hash=_generate_hash("nl_src"),
source_type_id=note_source_type.id,
)
target = Document(
id=str(uuid.uuid4()),
title="Research: line one\nline two",
text_content="tgt",
file_type="note",
file_size=0,
document_hash=_generate_hash("nl_tgt"),
source_type_id=note_source_type.id,
)
patched_session.add_all([source, target])
patched_session.commit()
service = NoteService("test_user")
result = service.accept_suggested_link(source.id, target.id)
assert result is not None
patched_session.refresh(source)
# The appended link is single-line and contains no newline.
assert "[[Research: line one line two]]" in source.text_content
assert "\n\nResearch" not in source.text_content
# And a real NoteLink edge was created (not a dead literal).
assert (
patched_session.query(NoteLink)
.filter_by(
source_document_id=source.id, target_document_id=target.id
)
.first()
is not None
)
def test_auto_suggested_link_survives_target_rename(
self, patched_session, two_notes
):
"""accept_suggested_link binds S->T to T's exact id. Renaming T
(so the appended [[Target Note]] text no longer title-resolves) and
re-saving S must keep exactly ONE S->T NoteLink with auto_suggested
still True — the rename-safety cache resolves the stale link text to
T's id rather than dropping the link or relabeling it."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes
service = NoteService("test_user")
assert service.accept_suggested_link(source.id, target.id) is not None
# Rename the target so its title no longer matches the [[Target Note]]
# text now embedded in the source.
assert service.update_note(target.id, title="Renamed Target")
# Re-save the source with its (now stale-title) content → reparse links.
patched_session.refresh(source)
assert service.update_note(source.id, content=source.text_content)
links = (
patched_session.query(NoteLink)
.filter_by(
source_document_id=source.id, target_document_id=target.id
)
.all()
)
assert len(links) == 1
assert links[0].auto_suggested is True
def test_accept_refuses_self_link(self, patched_session, two_notes):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, _ = two_notes
service = NoteService("test_user")
result = service.accept_suggested_link(source.id, source.id)
assert result is None
links = patched_session.query(NoteLink).all()
assert len(links) == 0
def test_accept_propagates_write_failure_instead_of_swallowing(
self, patched_session, two_notes, monkeypatch
):
"""a real write failure (e.g. update_note raising ValueError when
the appended link pushes the note past the content-size cap) must
propagate, not be collapsed into the same None the benign
preconditions return. Swallowing it made the route answer a
misleading 'Link could not be accepted' 404 for a 400-class failure.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes
service = NoteService("test_user")
def _raise(*args, **kwargs):
raise ValueError("Content exceeds maximum length")
monkeypatch.setattr(service, "update_note", _raise)
with pytest.raises(ValueError, match="maximum length"):
service.accept_suggested_link(source.id, target.id)
# The benign precondition (target not found, reached before any write)
# still returns None rather than raising.
assert service.accept_suggested_link(source.id, "nonexistent") is None
def test_accept_does_not_clobber_a_concurrent_edit(
self, patched_session, two_notes, monkeypatch
):
"""Lost-update guard: accept_suggested_link must append the link to the
note's CURRENT body — read inside update_note's own write transaction —
not to a snapshot read during the earlier precondition phase. A normal
save that lands between the two must survive.
Pre-fix, accept precomputed ``new_content`` from ``source.text_content``
read in a separate (already-closed) session and handed that stale
string to ``update_note``, which overwrote whatever had been committed
in the meantime. This test simulates that interleaving deterministically
by committing a concurrent edit at the moment ``update_note`` is
invoked; the appended link must land on the concurrent edit, not erase
it.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes # source body starts "Initial content."
service = NoteService("test_user")
orig_update = service.update_note
injected = {"done": False}
def update_with_concurrent_edit(note_id, *args, **kwargs):
# A normal save commits on the SAME note after the precondition
# read but before the append write.
if not injected["done"] and note_id == source.id:
injected["done"] = True
doc = (
patched_session.query(Document)
.filter_by(id=source.id)
.first()
)
doc.text_content = "Concurrently edited body."
patched_session.commit()
return orig_update(note_id, *args, **kwargs)
monkeypatch.setattr(service, "update_note", update_with_concurrent_edit)
result = service.accept_suggested_link(source.id, target.id)
assert result is not None
patched_session.refresh(source)
# The concurrent edit is preserved AND the link is appended to it.
assert "Concurrently edited body." in source.text_content
assert "[[Target Note]]" in source.text_content
def test_append_link_is_idempotent(self, patched_session, two_notes):
"""update_note's _append_link_title append is idempotent: if the exact
wiki-link is already in the body it's a no-op. Two concurrent accepts
of the same suggestion both pass accept_suggested_link's already_linked
precondition (read in a now-closed session) and reach update_note;
without this the second appends a DUPLICATE [[Target Note]] line
(the NoteLink table dedups, but the body text does not).
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, _target = two_notes
service = NoteService("test_user")
assert service.update_note(source.id, _append_link_title="Target Note")
# A second append of the same link (the concurrent-accept case) must
# NOT duplicate the wiki-link text.
service.update_note(source.id, _append_link_title="Target Note")
patched_session.refresh(source)
assert source.text_content.count("[[Target Note]]") == 1
def test_append_link_creates_notelink_when_text_already_present(
self, patched_session, two_notes
):
"""If the link TEXT is already in the body but no NoteLink row exists
(the user typed [[Target]] while it was unresolved, then the target was
created), accepting must still create/bind the NoteLink — the append is
skipped (no duplicate line) but the reparse still runs. Regression guard
for the idempotent-append fix, which pre-refinement short-circuited the
reparse too and made the accept a silent no-op.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
from local_deep_research.database.models import NoteLink
source, target = two_notes # target titled "Target Note"
service = NoteService("test_user")
# Put the link TEXT in the body WITHOUT creating a NoteLink (a
# typed-while-unresolved link): write content directly, no reparse.
source.text_content = "see [[Target Note]]"
patched_session.commit()
assert (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.count()
== 0
)
service.update_note(
source.id,
_append_link_title="Target Note",
_link_overrides={"target note": target.id},
)
patched_session.refresh(source)
# Text not duplicated...
assert source.text_content.count("[[Target Note]]") == 1
# ...and the NoteLink to the target WAS created (accept not a no-op).
links = (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert len(links) == 1
assert links[0].target_document_id == target.id
def test_accept_returns_none_for_missing_target(
self, patched_session, two_notes
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, _ = two_notes
service = NoteService("test_user")
result = service.accept_suggested_link(source.id, "nonexistent")
assert result is None
def test_auto_suggested_survives_content_resave(
self, patched_session, two_notes
):
"""re-saving the note content reparses links, which used to
recreate them with auto_suggested=False — silently dropping the
AI-suggested badge. The flag must persist across reparse."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes
service = NoteService("test_user")
service.accept_suggested_link(source.id, target.id)
patched_session.refresh(source)
# Resave with the [[Target Note]] link still present.
service.update_note(
source.id, content=source.text_content + "\n\nmore text"
)
link = (
patched_session.query(NoteLink)
.filter_by(
source_document_id=source.id, target_document_id=target.id
)
.first()
)
assert link is not None
assert link.auto_suggested is True
def test_accept_does_not_relabel_existing_hand_typed_link(
self, patched_session, two_notes
):
"""accepting a suggestion to a target the user already linked by
hand must not flip the user's link to auto_suggested or append a
duplicate [[Target]]."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes
service = NoteService("test_user")
# Hand-type the link first (auto_suggested stays False).
service.update_note(source.id, content="[[Target Note]] body")
result = service.accept_suggested_link(source.id, target.id)
assert result is not None
links = (
patched_session.query(NoteLink)
.filter_by(
source_document_id=source.id, target_document_id=target.id
)
.all()
)
assert len(links) == 1
assert links[0].auto_suggested is False # not relabeled
patched_session.refresh(source)
# No duplicate wiki-link appended.
assert source.text_content.count("[[Target Note]]") == 1
def _make_note(self, session, source_type_id, title, salt):
import time
doc = Document(
id=str(uuid.uuid4()),
title=title,
text_content=f"{title} body.",
file_type="note",
file_size=0,
document_hash=_generate_hash(salt),
source_type_id=source_type_id,
)
session.add(doc)
session.commit()
# Ensure a distinct created_at so the title-resolver tie-break
# (oldest wins) is deterministic — the OLDER duplicate is the WRONG
# target we must NOT link to.
time.sleep(0.01)
return doc
def test_accept_links_to_the_exact_target_on_duplicate_titles(
self, patched_session, note_source_type
):
"""with two notes sharing a title, accepting a suggestion for
the NEWER one must link to THAT note — not the oldest, which the
title resolver would otherwise pick."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source = self._make_note(
patched_session, note_source_type.id, "Source", "dup_src"
)
older = self._make_note(
patched_session, note_source_type.id, "Duplicate", "dup_old"
)
newer = self._make_note(
patched_session, note_source_type.id, "Duplicate", "dup_new"
)
service = NoteService("test_user")
service.accept_suggested_link(source.id, newer.id)
links = (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert len(links) == 1
# The link points to the EXACT accepted target, not the older one.
assert links[0].target_document_id == newer.id
assert links[0].target_document_id != older.id
assert links[0].auto_suggested is True
def test_accept_to_duplicate_title_survives_reparse(
self, patched_session, note_source_type
):
"""durability: the id-binding must survive a later content
resave — a fresh title resolve would otherwise retarget the link to
the oldest same-titled note (the finding's 'undone on next reparse')."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source = self._make_note(
patched_session, note_source_type.id, "Src2", "dup2_src"
)
older = self._make_note(
patched_session, note_source_type.id, "Shared", "dup2_old"
)
newer = self._make_note(
patched_session, note_source_type.id, "Shared", "dup2_new"
)
service = NoteService("test_user")
service.accept_suggested_link(source.id, newer.id)
patched_session.refresh(source)
# Edit the content (keeping the [[Shared]] link) and resave.
service.update_note(
source.id, content=source.text_content + "\n\nmore body"
)
links = (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert len(links) == 1
assert links[0].target_document_id == newer.id # still the newer one
assert links[0].target_document_id != older.id
assert links[0].auto_suggested is True
def test_accept_binds_exact_id_when_target_loses_every_tiebreak(
self, patched_session, note_source_type
):
"""revert detector: the duplicate-title tests above lean on
``time.sleep`` for created_at ordering, which can collapse to the
secondary ``Document.id.asc()`` tie-break (random uuid4 → the wrong
note only *sometimes* sorts first, so a revert of the
``_link_overrides`` binding could slip through). Here the decoy is
constructed to win BOTH resolver tie-breakers deterministically
(strictly older created_at AND lexicographically smaller id), so
without the override the title resolver is GUARANTEED to pick the
decoy — and this test fails."""
from datetime import datetime, timedelta, timezone
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
now = datetime.now(timezone.utc)
def make(title, salt, doc_id, created_at):
doc = Document(
id=doc_id,
title=title,
text_content=f"{title} body.",
file_type="note",
file_size=0,
document_hash=_generate_hash(salt),
source_type_id=note_source_type.id,
created_at=created_at,
)
patched_session.add(doc)
patched_session.commit()
return doc
source = make("Tie Source", "tie_src", str(uuid.uuid4()), now)
# Decoy wins tie-break level 1 (older created_at) AND level 2
# (smaller id) — the intended target loses both.
decoy = make(
"Tie Title",
"tie_decoy",
"00000000-0000-0000-0000-000000000001",
now - timedelta(days=1),
)
target = make(
"Tie Title",
"tie_target",
"ffffffff-ffff-ffff-ffff-ffffffffffff",
now,
)
service = NoteService("test_user")
result = service.accept_suggested_link(source.id, target.id)
assert result is not None
links = (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert len(links) == 1
# The override pins the link to the EXACT accepted id; a fallback
# to title resolution would deterministically yield the decoy.
assert links[0].target_document_id == target.id
assert links[0].target_document_id != decoy.id
assert links[0].auto_suggested is True
def test_accept_binds_exact_id_when_target_title_has_pipe(
self, patched_session, note_source_type
):
"""Regression: the wiki-link parser partitions link text on the
FIRST '|' and resolves only the pre-pipe part. Pre-fix,
accept_suggested_link kept the pipe in target_title, so the
override key ('react|redux') never matched the parser's lookup
key ('react') — the exact-id binding was silently bypassed and
title resolution picked the decoy note titled 'React'."""
from datetime import datetime, timedelta, timezone
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
now = datetime.now(timezone.utc)
source = self._make_note(
patched_session, note_source_type.id, "Pipe Src", "pipe_src"
)
# Decoy: titled exactly the pre-pipe portion, older AND smaller id,
# so a fallback to title resolution deterministically picks it.
decoy = Document(
id="00000000-0000-0000-0000-00000000000a",
title="React",
text_content="decoy body",
file_type="note",
file_size=0,
document_hash=_generate_hash("pipe_decoy"),
source_type_id=note_source_type.id,
created_at=now - timedelta(days=1),
)
target = Document(
id="ffffffff-ffff-ffff-ffff-fffffffffffa",
title="React|Redux",
text_content="target body",
file_type="note",
file_size=0,
document_hash=_generate_hash("pipe_target"),
source_type_id=note_source_type.id,
created_at=now,
)
patched_session.add_all([decoy, target])
patched_session.commit()
service = NoteService("test_user")
result = service.accept_suggested_link(source.id, target.id)
assert result is not None
links = (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert len(links) == 1
assert links[0].target_document_id == target.id
assert links[0].target_document_id != decoy.id
assert links[0].auto_suggested is True
def test_accept_refuses_degenerate_wiki_syntax_only_title(
self, patched_session, note_source_type
):
"""Regression: a target title that strips down to nothing
('|', '[[', ']]') used to append a dead '[[]]' literal into the
note body (the parser skips empty link text, so no link was ever
created). Now refused — returns None (the method's failure
contract) without persisting the artifact."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source = self._make_note(
patched_session, note_source_type.id, "Degenerate Src", "deg_src"
)
target = self._make_note(
patched_session, note_source_type.id, "[[|]]", "deg_tgt"
)
service = NoteService("test_user")
assert service.accept_suggested_link(source.id, target.id) is None
# Source content untouched — no '[[]]' artifact persisted.
patched_session.expire_all()
refreshed = (
patched_session.query(Document).filter_by(id=source.id).first()
)
assert "[[]]" not in (refreshed.text_content or "")
# And no link row was created.
assert (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.count()
== 0
)
def test_accept_links_correctly_when_target_title_has_brackets(
self, patched_session, note_source_type
):
"""Subsumed by the id-binding: a target whose title
contains brackets is appended with the brackets stripped, so a
title-only resolve would dead-link — the id override binds it anyway."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source = self._make_note(
patched_session, note_source_type.id, "Src3", "brk_src"
)
target = self._make_note(
patched_session, note_source_type.id, "My [Draft] Note", "brk_tgt"
)
service = NoteService("test_user")
service.accept_suggested_link(source.id, target.id)
link = (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.first()
)
assert link is not None
assert link.target_document_id == target.id
def test_add_to_collection_rejects_non_note(self, patched_session):
"""collection helpers must verify the id is a note."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
assert service.add_to_collection("nonexistent-id", "coll") is False
assert service.remove_from_collection("nonexistent-id", "coll") is False
def test_add_to_collection_unknown_collection_raises_lookup(
self, patched_session, sample_note
):
"""A real note + a bogus collection_id must raise LookupError (mapped
to 404 by the route) rather than tripping the FK constraint and
surfacing as a generic 500."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
with pytest.raises(LookupError):
service.add_to_collection(sample_note.id, "does-not-exist")
def test_outgoing_links_exposes_auto_suggested(
self, patched_session, two_notes
):
"""get_outgoing_links surfaces the auto_suggested flag so the UI
can badge AI-suggested links. An accepted suggestion is flagged
True."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
source, target = two_notes
service = NoteService("test_user")
service.accept_suggested_link(source.id, target.id)
outgoing = service.get_outgoing_links(source.id)
assert len(outgoing) == 1
assert "auto_suggested" in outgoing[0]
assert outgoing[0]["auto_suggested"] is True
# Regression: the payload must expose target_id (the stable
# FK the frontend keys its [[link]] -> id map on for rename-safe
# navigation). Without it that lookup was always empty.
assert outgoing[0]["target_id"] == target.id
assert outgoing[0]["id"] == target.id
class TestNoteChangeTypeEnum:
"""NoteChangeType enum behavior."""
def test_enum_values(self):
from local_deep_research.database.models import NoteChangeType
values = {e.value for e in NoteChangeType}
assert values == {
"initial",
"auto_save",
"manual_save",
"pre_restore",
"restore",
}
def test_change_type_persists_as_string(self, db_session, note_source_type):
"""Writing a NoteChangeType value survives round-trip as a string."""
from local_deep_research.database.models import (
NoteChangeType,
NoteVersion,
)
note = Document(
id=str(uuid.uuid4()),
title="ct-note",
text_content="content",
file_type="note",
file_size=0,
document_hash=_generate_hash("enum_persist"),
source_type_id=note_source_type.id,
)
db_session.add(note)
db_session.commit()
v = NoteVersion(
id=str(uuid.uuid4()),
document_id=note.id,
title="ct-note",
content="content",
change_type=NoteChangeType.MANUAL_SAVE.value,
content_hash="abc",
)
db_session.add(v)
db_session.commit()
saved = db_session.query(NoteVersion).filter_by(id=v.id).first()
assert saved.change_type in (
"manual_save",
NoteChangeType.MANUAL_SAVE,
)
class TestPostReviewRegressions:
"""Regression guards for the fixes shipped in PR #3277.
Each test pins a single fix; reverting any of those fixes should make
the corresponding test fail. Companion service-level invariants
(validator helper, missing-note/version error codes, synthesis
source-type filter) live in test_note_integration.py — these tests
target call sites that aren't covered there.
"""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
"""Yield the test db_session whenever NoteService opens a per-user one.
Also stubs ``NoteAIService.summarize_changes_sync`` so ``update_note``
doesn't try to spin up an LLM when the content changes.
Replaces the async summary executor with a synchronous shim so the
worker that fills ``NoteVersion.change_summary`` runs on the test
thread; the in-memory SQLite session isn't thread-safe and a
worker firing across test boundaries flakes the suite.
"""
from contextlib import contextmanager
@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,
)
# Avoid hitting an LLM during update_note's auto-summarize step.
from local_deep_research.research_library.notes.services.note_ai_service import (
NoteAIService,
)
# The inline summary worker calls the SYNC entry point
# (summarize_changes_sync) — it owns no event loop, so it must not
# touch the async core (#6293). Stub the method it actually calls.
def _stubbed_summary(self, old, new):
return "stubbed"
monkeypatch.setattr(
NoteAIService,
"summarize_changes_sync",
_stubbed_summary,
)
# The summary executor is already run inline by the package-wide
# autouse fixture _force_synchronous_summary_executor (conftest.py).
return db_session
def test_link_research_uses_max_plus_one_after_delete(
self, patched_session, note_source_type
):
"""Regression guard for P0-2: ``link_research_to_note`` must derive
the next ``display_order`` from ``MAX(display_order)+1``, not
``COUNT(*)``. After a middle-row deletion (mimicking CASCADE from
ResearchHistory removal), COUNT-based logic would collide with an
existing row's display_order.
"""
from datetime import datetime, timezone
from sqlalchemy import func
from local_deep_research.database.models import (
NoteResearch,
ResearchHistory,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Link Research",
text_content="x",
file_type="note",
file_size=0,
document_hash=_generate_hash("link_research_max"),
source_type_id=note_source_type.id,
)
patched_session.add(note)
patched_session.commit()
research_ids = [str(uuid.uuid4()) for _ in range(3)]
now = datetime.now(timezone.utc)
for i, rid in enumerate(research_ids):
patched_session.add(
ResearchHistory(
id=rid,
query=f"q{i}",
mode="quick",
status="completed",
created_at=now.isoformat(),
)
)
patched_session.commit()
for i, rid in enumerate(research_ids):
patched_session.add(
NoteResearch(
document_id=note_id,
research_id=rid,
display_order=i,
)
)
patched_session.commit()
# Delete the middle NoteResearch row directly, simulating what
# CASCADE-from-ResearchHistory-removal leaves behind.
middle = (
patched_session.query(NoteResearch)
.filter_by(document_id=note_id, research_id=research_ids[1])
.first()
)
patched_session.delete(middle)
patched_session.commit()
# After deletion: COUNT==2 but MAX==2. COUNT-based code would
# write display_order=2, colliding with research_ids[2].
new_research_id = str(uuid.uuid4())
patched_session.add(
ResearchHistory(
id=new_research_id,
query="q-new",
mode="quick",
status="completed",
created_at=now.isoformat(),
)
)
patched_session.commit()
service = NoteService("test_user")
service.link_research_to_note(note_id, new_research_id)
new_row = (
patched_session.query(NoteResearch)
.filter_by(document_id=note_id, research_id=new_research_id)
.first()
)
assert new_row is not None
assert new_row.display_order == 3
# And the MAX-derived result really is max+1 of the surviving rows.
current_max = (
patched_session.query(func.max(NoteResearch.display_order))
.filter_by(document_id=note_id)
.scalar()
)
assert current_max == 3
def test_link_research_rejects_non_note_document(
self, patched_session, note_source_type
):
"""Regression: link_research_to_note was the only mutating
method without an _is_note guard, so NoteResearch rows could be
attached to any Document (e.g. an uploaded PDF) — orphaned rows
the notes UI never renders."""
from datetime import datetime, timezone
from local_deep_research.database.models import (
ResearchHistory,
SourceType,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
pdf_source = SourceType(
id=str(uuid.uuid4()),
name="document",
display_name="Document",
)
patched_session.add(pdf_source)
pdf_id = str(uuid.uuid4())
patched_session.add(
Document(
id=pdf_id,
title="Some PDF",
text_content="pdf body",
file_type="pdf",
file_size=10,
document_hash=_generate_hash("link_research_pdf"),
source_type_id=pdf_source.id,
)
)
research_id = str(uuid.uuid4())
patched_session.add(
ResearchHistory(
id=research_id,
query="q",
mode="quick",
status="completed",
created_at=datetime.now(timezone.utc).isoformat(),
)
)
patched_session.commit()
service = NoteService("test_user")
with pytest.raises(ValueError, match="not found"):
service.link_research_to_note(pdf_id, research_id)
from local_deep_research.database.models import NoteResearch
assert (
patched_session.query(NoteResearch)
.filter_by(document_id=pdf_id)
.count()
== 0
)
def test_link_research_duplicate_raises_immediately(
self, patched_session, note_source_type, monkeypatch
):
"""Regression: linking the SAME research twice hits the
(document_id, research_id) UNIQUE constraint. SQLite reports the columns
(not the constraint name), so the old name-based discriminator misrouted
it into the display_order retry loop and burned all retries. It must now
surface immediately (single attempt) — proving the discriminator and the
rollback both work.
"""
from datetime import datetime, timezone
from sqlalchemy.exc import IntegrityError
from local_deep_research.database.models import ResearchHistory
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="Dup Link",
text_content="x",
file_type="note",
file_size=0,
document_hash=_generate_hash("dup_link_research"),
source_type_id=note_source_type.id,
)
)
research_id = str(uuid.uuid4())
patched_session.add(
ResearchHistory(
id=research_id,
query="q",
mode="quick",
status="completed",
created_at=datetime.now(timezone.utc).isoformat(),
)
)
patched_session.commit()
service = NoteService("test_user")
service.link_research_to_note(note_id, research_id)
# Count attempts via the rollback helper (called once per failed try).
calls = {"n": 0}
original = NoteService._rollback_quietly
def counting(session):
calls["n"] += 1
return original(session)
monkeypatch.setattr(
NoteService, "_rollback_quietly", staticmethod(counting)
)
with pytest.raises(IntegrityError):
service.link_research_to_note(note_id, research_id)
# Exactly one attempt — not the 5-retry storm the old code produced.
assert calls["n"] == 1
def test_reorder_rejects_duplicate_ids(
self, patched_session, note_source_type
):
"""a duplicate id collapses in a set, so a set-equality check
alone would accept ['A','B','A'] and assign A two positions. Duplicates
must be rejected."""
from datetime import datetime, timezone
from local_deep_research.database.models import ResearchHistory
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="Reorder Dup",
text_content="x",
file_type="note",
file_size=0,
document_hash=_generate_hash("reorder_dup"),
source_type_id=note_source_type.id,
)
)
rids = [str(uuid.uuid4()) for _ in range(2)]
now = datetime.now(timezone.utc)
for i, rid in enumerate(rids):
patched_session.add(
ResearchHistory(
id=rid,
query="q",
mode="quick",
status="completed",
created_at=now.isoformat(),
)
)
patched_session.commit()
service = NoteService("test_user")
for i, rid in enumerate(rids):
service.link_research_to_note(note_id, rid)
# ['A','B','A'] collapses to {A,B} under set-equality but is invalid.
assert (
service.reorder_note_research(note_id, [rids[0], rids[1], rids[0]])
is False
)
def test_create_note_rejects_invalid_tags(
self, patched_session, note_source_type
):
"""Regression guard for P0-3: ``create_note`` must call
``_validate_tags`` BEFORE any DB work. A bad ``tags`` value
should raise ValueError with no Document persisted.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
before_count = patched_session.query(Document).count()
with pytest.raises(ValueError, match="must be a list"):
service.create_note(title="t", content="c", tags="not-a-list") # type: ignore[arg-type]
with pytest.raises(ValueError, match="exceeds maximum length"):
service.create_note(title="t", content="c", tags=["x" * 51])
# No Documents created on rejection — validation runs pre-DB.
assert patched_session.query(Document).count() == before_count
def test_update_note_rejects_invalid_tags(
self, patched_session, note_source_type
):
"""Regression guard for P0-3: ``update_note`` must call
``_validate_tags`` BEFORE any DB write. A bad ``tags`` value
should raise ValueError without mutating the existing note.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
original_tags = ["keep", "me"]
note = Document(
id=note_id,
title="Tag Update",
text_content="content",
file_type="note",
file_size=0,
document_hash=_generate_hash("update_tag_reject"),
source_type_id=note_source_type.id,
tags=list(original_tags),
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
with pytest.raises(ValueError, match="must be a list"):
service.update_note(note_id, tags="not-a-list") # type: ignore[arg-type]
with pytest.raises(ValueError, match="exceeds maximum length"):
service.update_note(note_id, tags=["x" * 51])
# Tags weren't mutated by the failed calls.
patched_session.refresh(note)
assert note.tags == original_tags
def test_create_and_update_note_reject_too_many_tags(
self, patched_session, note_source_type
):
"""Count guard on tags: exactly MAX_TAGS_PER_NOTE passes, one more
raises 'too many tags' — on both create and update — and the
rejection is pre-DB so no row is created/mutated.
``match='too many tags'`` deliberately targets the count branch
(note_service.py:301-302), not the per-tag-length branch, so the
50-pass + 51-raise boundary fails on any weakening of that guard
(removal, ``>=``, or ``> MAX + 1``). Each tag is kept 1-4 chars so
the 50-char per-tag guard never fires.
"""
from local_deep_research.research_library.notes.services.note_service import (
MAX_TAGS_PER_NOTE,
NoteService,
)
service = NoteService("test_user")
exactly_50 = ["t%02d" % i for i in range(MAX_TAGS_PER_NOTE)]
too_many = ["t%03d" % i for i in range(MAX_TAGS_PER_NOTE + 1)]
assert len(exactly_50) == MAX_TAGS_PER_NOTE
assert len(too_many) == MAX_TAGS_PER_NOTE + 1
# --- create boundary -------------------------------------------------
before_count = patched_session.query(Document).count()
created_id = service.create_note(
title="ok50", content="c", tags=exactly_50
)
assert created_id is not None
assert patched_session.query(Document).count() == before_count + 1
created = (
patched_session.query(Document).filter_by(id=created_id).first()
)
assert len(created.tags) == MAX_TAGS_PER_NOTE
with pytest.raises(ValueError, match="too many tags"):
service.create_note(title="boom", content="c", tags=too_many)
# Nothing persisted on rejection — still just the 50-tag note.
assert patched_session.query(Document).count() == before_count + 1
# --- update boundary -------------------------------------------------
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Tag Count",
text_content="content",
file_type="note",
file_size=0,
document_hash=_generate_hash("tag-count-update"),
source_type_id=note_source_type.id,
tags=["orig"],
)
patched_session.add(note)
patched_session.commit()
assert service.update_note(note_id, tags=exactly_50) is True
patched_session.refresh(note)
assert len(note.tags) == MAX_TAGS_PER_NOTE
with pytest.raises(ValueError, match="too many tags"):
service.update_note(note_id, tags=too_many)
# Unchanged — still the 50-tag list, NOT 51 (guard runs before mutate).
patched_session.refresh(note)
assert len(note.tags) == MAX_TAGS_PER_NOTE
def test_create_note_rejects_oversize_content_before_persist(
self, patched_session, note_source_type, monkeypatch
):
"""``create_note`` must reject oversize content BEFORE building the
Document (note_service.py:564). Shrinks the cap via monkeypatch so
the 50 MB real limit doesn't have to be allocated.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service.NOTE_CONTENT_MAX_BYTES",
8,
)
service = NoteService("test_user")
with pytest.raises(ValueError, match="content exceeds maximum size"):
service.create_note(title="ok", content="x" * 50)
# _assert_content_size runs before the Document is built/added.
assert (
patched_session.query(Document).filter_by(file_type="note").count()
== 0
)
def test_update_note_rejects_oversize_content_leaves_note_unchanged(
self, patched_session, note_source_type, monkeypatch
):
"""``update_note`` must reject oversize content (note_service.py:762-763)
before any mutation: existing body/character_count/hash are untouched
and no version snapshot is written.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service.NOTE_CONTENT_MAX_BYTES",
8,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="ok",
text_content="small",
file_type="note",
file_size=5,
document_hash=_generate_hash("oversize-content-update"),
source_type_id=note_source_type.id,
character_count=5,
tags=[],
)
patched_session.add(note)
patched_session.commit()
original_hash = note.document_hash
service = NoteService("test_user")
with pytest.raises(ValueError, match="content exceeds maximum size"):
service.update_note(note_id, content="y" * 50)
patched_session.refresh(note)
assert note.text_content == "small"
assert note.character_count == 5
assert note.document_hash == original_hash
assert (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id)
.count()
== 0
)
def test_update_note_rejects_empty_string_title_and_preserves_existing(
self, patched_session, note_source_type
):
"""The dedicated empty-title guard (note_service.py:760-761) must
reject ``title=''`` on update without erasing the existing title or
writing a junk version snapshot. ``title=None`` (explicit "don't
update") must still be accepted.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Keep Me",
text_content="body",
file_type="note",
file_size=4,
document_hash=_generate_hash("empty-title-update"),
source_type_id=note_source_type.id,
tags=["t"],
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
with pytest.raises(ValueError, match="title must not be empty"):
service.update_note(note_id, title="")
# Existing title untouched; no version snapshot written (guard runs
# before the DB session opens).
patched_session.refresh(note)
assert note.title == "Keep Me"
assert note.text_content == "body"
assert (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id)
.count()
== 0
)
# Explicit None means "don't update the title" — must not raise.
assert service.update_note(note_id, title=None) is True
def test_core_crud_rejects_non_note_document(
self, patched_session, note_source_type
):
"""get_note / update_note / delete_note must reject a same-DB
non-note Document via their ``not self._is_note(...)`` clause
(note_service.py:699, 769, 944). Existing tests only hit the
not-found branch (cross-user / nonexistent id), never the same-DB
_is_note rejection — without this guard the notes API could
read/overwrite/delete an arbitrary uploaded PDF in the same DB.
"""
from local_deep_research.database.models import SourceType
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
pdf_source = SourceType(
id=str(uuid.uuid4()),
name="document",
display_name="Document",
)
patched_session.add(pdf_source)
pdf_id = str(uuid.uuid4())
patched_session.add(
Document(
id=pdf_id,
title="Some PDF",
text_content="pdf body",
file_type="pdf",
file_size=10,
document_hash=_generate_hash("crud_non_note"),
source_type_id=pdf_source.id,
)
)
patched_session.commit()
service = NoteService("test_user")
# (1) get_note rejects it.
assert service.get_note(pdf_id) is None
# (2) update_note refuses; the PDF is unmutated, no version snapshot.
assert (
service.update_note(pdf_id, content="HACKED", title="HACKED")
is False
)
patched_session.expire_all()
doc = patched_session.query(Document).filter_by(id=pdf_id).first()
assert doc.text_content == "pdf body"
assert doc.title == "Some PDF"
assert (
patched_session.query(NoteVersion)
.filter_by(document_id=pdf_id)
.count()
== 0
)
# (3) delete_note refuses; the row survives.
assert service.delete_note(pdf_id) is False
patched_session.expire_all()
assert (
patched_session.query(Document).filter_by(id=pdf_id).first()
is not None
)
def test_link_research_exhausts_retries_and_raises_integrity_error(
self, patched_session, note_source_type, monkeypatch
):
"""A PERSISTENT display_order collision must exhaust exactly
``_LINK_RESEARCH_MAX_RETRIES`` attempts and then re-raise the
IntegrityError from the give-up branch (note_service.py:1387-1395),
never falling through to the line-1410 RuntimeError sentinel.
We force every commit to raise an IntegrityError whose ``orig``
message contains the literal ``display_order`` token so it stays on
the retryable branch, and count attempts via the per-failure
``_rollback_quietly`` hook (same idiom as
test_link_research_duplicate_raises_immediately).
"""
from datetime import datetime, timezone
from sqlalchemy.exc import IntegrityError
from local_deep_research.database.models import ResearchHistory
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="Exhaust",
text_content="x",
file_type="note",
file_size=0,
document_hash=_generate_hash("exhaust_retries"),
source_type_id=note_source_type.id,
)
)
research_id = str(uuid.uuid4())
patched_session.add(
ResearchHistory(
id=research_id,
query="q",
mode="quick",
status="completed",
created_at=datetime.now(timezone.utc).isoformat(),
)
)
patched_session.commit()
# Make EVERY commit fail with a retryable display_order collision.
def _always_collide():
raise IntegrityError(
"stmt",
"params",
Exception(
"UNIQUE constraint failed: "
"note_research.document_id, note_research.display_order"
),
)
monkeypatch.setattr(patched_session, "commit", _always_collide)
# Count attempts: _rollback_quietly fires once per failed try.
calls = {"n": 0}
original = NoteService._rollback_quietly
def counting(session):
calls["n"] += 1
return original(session)
monkeypatch.setattr(
NoteService, "_rollback_quietly", staticmethod(counting)
)
service = NoteService("test_user")
# The give-up branch must re-raise IntegrityError (NOT RuntimeError).
with pytest.raises(IntegrityError):
service.link_research_to_note(note_id, research_id)
assert calls["n"] == NoteService._LINK_RESEARCH_MAX_RETRIES
def test_update_note_propagates_non_content_hash_integrity_error(
self, patched_session, note_source_type, monkeypatch
):
"""update_note's commit-time ``except IntegrityError`` branch
(note_service.py ~899-935) only RECOVERS the version-dedup race (whose
error mentions ``content_hash``). Any other IntegrityError — e.g. a
NoteLink uix_note_link violation from the link reparse — must still
surface. This pins the discriminator's propagate leg: dropping the
``if "content_hash" not in err: raise`` guard would swallow the error
and (wrongly) return True.
"""
from sqlalchemy.exc import IntegrityError
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="Propagate",
text_content="original",
file_type="note",
file_size=0,
document_hash=_generate_hash("propagate_non_content_hash"),
source_type_id=note_source_type.id,
)
)
patched_session.commit()
# Commit raises an IntegrityError that does NOT mention content_hash
# (a uix_note_link violation), so the recovery leg must NOT fire.
def _link_collision():
raise IntegrityError(
"stmt",
"params",
Exception(
"UNIQUE constraint failed: uix_note_link "
"(note_links.source_document_id, "
"note_links.target_document_id)"
),
)
monkeypatch.setattr(patched_session, "commit", _link_collision)
service = NoteService("test_user")
with pytest.raises(IntegrityError):
service.update_note(note_id, content="new content")
def test_update_note_recovers_on_concurrent_content_hash_unique_collision(
self, patched_session, note_source_type, monkeypatch
):
"""The mirror of the propagate leg: when commit raises an
IntegrityError whose message DOES contain ``content_hash`` (the
version-dedup UNIQUE race where a concurrent identical-content save
already persisted the same version), update_note must RECOVER —
rollback and return True, no exception. Pins the recovery leg of the
discriminator at note_service.py ~924-933.
"""
from sqlalchemy.exc import IntegrityError
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="Recover",
text_content="original",
file_type="note",
file_size=0,
document_hash=_generate_hash("recover_content_hash_race"),
source_type_id=note_source_type.id,
)
)
patched_session.commit()
# Commit raises a version-dedup collision (mentions content_hash), so
# the recovery leg fires and update_note returns True.
def _content_hash_collision():
raise IntegrityError(
"stmt",
"params",
Exception(
"UNIQUE constraint failed: uix_note_version_content "
"(note_versions.document_id, note_versions.content_hash)"
),
)
monkeypatch.setattr(patched_session, "commit", _content_hash_collision)
service = NoteService("test_user")
assert service.update_note(note_id, content="new content") is True
def test_update_note_recovers_on_flush_time_content_hash_collision(
self, patched_session, note_source_type, monkeypatch
):
"""The REAL race raises the version-dedup UNIQUE at the snapshot FLUSH
(inside _create_version_snapshot_in_session), before commit(). The
recovery try must wrap the snapshot creation, not just commit() —
otherwise a benign concurrent identical-content save 500s. Without the
widened try this raises instead of returning True.
"""
from sqlalchemy.exc import IntegrityError
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="Recover",
text_content="original",
file_type="note",
file_size=0,
document_hash=_generate_hash("recover_flush_content_hash"),
source_type_id=note_source_type.id,
)
)
patched_session.commit()
def _flush_collision(*args, **kwargs):
raise IntegrityError(
"stmt",
"params",
Exception(
"UNIQUE constraint failed: uix_note_version_content "
"(note_versions.document_id, note_versions.content_hash)"
),
)
monkeypatch.setattr(
NoteService,
"_create_version_snapshot_in_session",
_flush_collision,
)
service = NoteService("test_user")
assert service.update_note(note_id, content="new content") is True
def test_update_note_recovery_reapplies_pin_after_collision(
self, patched_session, note_source_type, monkeypatch
):
"""The version-dedup recovery rolls back the loser's whole
transaction, but ``favorite`` is not part of the version hash —
the concurrent winner never persisted it. Pre-fix the recovery
returned True with the pin silently dropped (HTTP 200, note never
pinned). It must re-apply favorite before reporting success."""
from sqlalchemy.exc import IntegrityError
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="Recover",
text_content="original",
file_type="note",
file_size=0,
document_hash=_generate_hash("recover_pin_reapply"),
source_type_id=note_source_type.id,
favorite=False,
)
)
patched_session.commit()
def _flush_collision(*args, **kwargs):
raise IntegrityError(
"stmt",
"params",
Exception(
"UNIQUE constraint failed: uix_note_version_content "
"(note_versions.document_id, note_versions.content_hash)"
),
)
monkeypatch.setattr(
NoteService,
"_create_version_snapshot_in_session",
_flush_collision,
)
service = NoteService("test_user")
assert (
service.update_note(note_id, content="new content", favorite=True)
is True
)
patched_session.expire_all()
doc = patched_session.query(Document).filter_by(id=note_id).one()
assert doc.favorite is True, (
"recovery reported success but dropped the pin"
)
def test_restore_with_bookends_handles_no_versions_gracefully(
self, patched_session, note_source_type
):
"""Regression guard for P0-4: ``restore_with_bookends`` must
return ``(False, "version_not_found")`` for a missing version
rather than raising — note row stays untouched.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="No Versions",
text_content="untouched",
file_type="note",
file_size=0,
document_hash=_generate_hash("restore_no_versions"),
source_type_id=note_source_type.id,
tags=["a"],
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
success, error = service.restore_with_bookends(
note_id, str(uuid.uuid4())
)
assert success is False
assert error == "version_not_found"
# Document untouched.
patched_session.refresh(note)
assert note.title == "No Versions"
assert note.text_content == "untouched"
assert note.tags == ["a"]
# And no NoteVersion rows materialized.
assert (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id)
.count()
== 0
)
def test_list_notes_returns_content_preview_not_full_body(
self, patched_session, note_source_type
):
"""Regression guard: list_notes must return ``content_preview``
(capped) instead of the full ``text_content`` so a page of notes
doesn't ship MB of markdown the UI throws away.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# 10 KB body — far larger than the 300-char preview cap.
big_body = "x" * 10_000
note = Document(
id=str(uuid.uuid4()),
title="Big",
text_content=big_body,
file_type="note",
file_size=len(big_body),
document_hash=_generate_hash("big-body"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
results = NoteService("test_user").list_notes(limit=10)
assert len(results) == 1
row = results[0]
# Full content NOT shipped in the list payload.
assert row["content"] is None
# Preview present, bounded.
assert row["content_preview"] is not None
assert (
len(row["content_preview"])
# +3 for the "..." marker _truncate_preview appends
<= NoteService.LIST_CONTENT_PREVIEW_CHARS + 3
)
def test_get_note_still_returns_full_content(
self, patched_session, note_source_type
):
"""Sanity: the detail-fetch path keeps the full body."""
note_id = str(uuid.uuid4())
big_body = "y" * 5_000
note = Document(
id=note_id,
title="Detail",
text_content=big_body,
file_type="note",
file_size=len(big_body),
document_hash=_generate_hash("detail-body"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
# Stub _is_note so get_note recognises our fixture document.
from local_deep_research.research_library.notes.services.note_service import (
NoteService as NS,
)
import unittest.mock
with unittest.mock.patch.object(NS, "_is_note", return_value=True):
row = NS("test_user").get_note(note_id)
assert row is not None
assert row["content"] == big_body
assert row["content_preview"] is None
def test_title_only_edit_creates_distinct_version_snapshot(
self, patched_session, note_source_type
):
"""Regression guard: editing only the title (content unchanged)
must still produce a new NoteVersion row.
Pre-fix, ``content_hash`` was SHA256(content) only, so a title-only
edit dedup'd to the existing version and the old title silently
won on restore.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Original",
text_content="unchanged body",
file_type="note",
file_size=len("unchanged body"),
document_hash=_generate_hash("title-only-edit"),
source_type_id=note_source_type.id,
tags=["a"],
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
# First save creates the baseline version snapshot.
v_initial = _create_snapshot(
service,
patched_session,
note_id=note_id,
title="Original",
content="unchanged body",
tags=["a"],
change_type="manual_save",
)
# Now change only the title; content + tags identical.
v_title_edit = _create_snapshot(
service,
patched_session,
note_id=note_id,
title="Renamed",
content="unchanged body",
tags=["a"],
change_type="manual_save",
)
assert v_title_edit != v_initial, (
"Title-only edit was deduped to the original version — "
"version history silently dropped the new title."
)
versions = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id)
.order_by(NoteVersion.created_at.asc())
.all()
)
assert len(versions) == 2
assert versions[0].title == "Original"
assert versions[1].title == "Renamed"
def test_tag_only_edit_creates_distinct_version_snapshot(
self, patched_session, note_source_type
):
"""Same guarantee for tag-only edits."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Same",
text_content="same body",
file_type="note",
file_size=len("same body"),
document_hash=_generate_hash("tag-only-edit"),
source_type_id=note_source_type.id,
tags=["a"],
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
v1 = _create_snapshot(
service,
patched_session,
note_id=note_id,
title="Same",
content="same body",
tags=["a"],
change_type="manual_save",
)
v2 = _create_snapshot(
service,
patched_session,
note_id=note_id,
title="Same",
content="same body",
tags=["a", "b"],
change_type="manual_save",
)
assert v2 != v1, (
"Tag-only edit was deduped — version history lost the tag change."
)
def test_update_note_atomic_snapshot_failure_rolls_back_content(
self, patched_session, note_source_type, monkeypatch
):
"""regression guard: if the version snapshot helper raises,
the document content change must roll back with it. Pre-fix the
document committed first in its own session; a snapshot failure
would leave a stranded content update with no MANUAL_SAVE row.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
original_body = "before edit"
note = Document(
id=note_id,
title="t",
text_content=original_body,
file_type="note",
file_size=len(original_body),
document_hash=_generate_hash("atomic-snapshot"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
# Seed a baseline manual_save so the next update is "content
# change" not "new note".
_create_snapshot(
NoteService("test_user"),
patched_session,
note_id=note_id,
title="t",
content=original_body,
tags=[],
change_type="manual_save",
)
# Force the in-session snapshot helper to raise.
def _boom(*args, **kwargs):
raise RuntimeError("simulated snapshot failure")
monkeypatch.setattr(
NoteService,
"_create_version_snapshot_in_session",
_boom,
)
# update_note must raise (the exception escaped the with block,
# which rolls back the document mutation).
service = NoteService("test_user")
try:
service.update_note(note_id, content="after edit")
except RuntimeError:
pass
except Exception:
# Other exceptions are also acceptable as long as the rollback
# happened.
pass
# Verify: content reverted, no second manual_save row.
patched_session.expire_all()
refreshed = (
patched_session.query(Document).filter_by(id=note_id).first()
)
assert refreshed.text_content == original_body, (
f"Content was not rolled back after snapshot failure: "
f"{refreshed.text_content!r}"
)
manual_saves = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id, change_type="manual_save")
.count()
)
assert manual_saves == 1, (
f"Expected only the baseline manual_save row to remain, "
f"got {manual_saves}"
)
def test_update_note_atomic_link_reparse_failure_rolls_back_content(
self, patched_session, note_source_type, monkeypatch
):
"""regression guard: if link reparse raises, the content
change must roll back. Pre-fix the content committed first, so
a link-parse failure left a saved note with stale outgoing links.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
original_body = "no links yet"
note = Document(
id=note_id,
title="t",
text_content=original_body,
file_type="note",
file_size=len(original_body),
document_hash=_generate_hash("atomic-link-reparse"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
def _boom(*args, **kwargs):
raise RuntimeError("simulated link reparse failure")
monkeypatch.setattr(
NoteService,
"_parse_and_update_links_in_session",
_boom,
)
try:
NoteService("test_user").update_note(
note_id, content="now with [[a link]]"
)
except Exception:
pass
patched_session.expire_all()
refreshed = (
patched_session.query(Document).filter_by(id=note_id).first()
)
assert refreshed.text_content == original_body, (
f"Content was not rolled back after link-reparse failure: "
f"{refreshed.text_content!r}"
)
# Also: no NoteLink rows for this note (the rollback wiped any
# partial-insert state).
from local_deep_research.database.models import NoteLink
assert (
patched_session.query(NoteLink)
.filter_by(source_document_id=note_id)
.count()
== 0
)
def test_link_pattern_does_not_capture_unbounded_text(self):
"""M11 regression guard: an unclosed ``[[`` must not capture
multi-MB of body. The negated class is bounded at
MAX_LINK_TEXT_LENGTH so a 5 MB run between ``[[`` and ``]]``
either fails to match or matches only the 500-char prefix
followed by another ``]]`` later in the doc.
"""
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
MAX_LINK_TEXT_LENGTH,
)
# 5 MB of innocuous text between [[ and ]] — pre-fix this captured
# the whole thing.
body = "[[" + ("a" * (5 * 1024 * 1024)) + "]] tail"
matches = LINK_PATTERN.findall(body)
for m in matches:
assert len(m) <= MAX_LINK_TEXT_LENGTH, (
f"LINK_PATTERN captured {len(m)} chars — bound is "
f"{MAX_LINK_TEXT_LENGTH}. Unbounded capture would DoS "
"LOWER(title) lookups on every save."
)
def test_resolve_link_caps_input(self, patched_session):
"""``resolve_link`` is a public API; even if a caller bypasses the
regex and hands a huge string, the service must truncate before
the SQL lookup.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
MAX_LINK_TEXT_LENGTH,
)
# Patch internal resolver to spy on what it received.
from unittest.mock import patch
with patch.object(
NoteService,
"_resolve_link_internal",
return_value=None,
) as spy:
NoteService("test_user").resolve_link("x" * 10_000)
assert spy.called
# Second positional arg is link_text.
received = spy.call_args.args[1]
assert len(received) <= MAX_LINK_TEXT_LENGTH
def test_create_note_rejects_oversize_title(
self, patched_session, note_source_type
):
"""M10 regression guard: a 50 MB title used to be accepted
end-to-end and interpolated into LLM prompts / LOWER(title)
lookups. Cap is now enforced at the service boundary.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
MAX_TITLE_LENGTH,
)
service = NoteService("test_user")
oversize = "x" * (MAX_TITLE_LENGTH + 1)
with pytest.raises(ValueError, match="title exceeds maximum length"):
service.create_note(title=oversize, content="body")
def test_update_note_rejects_oversize_title(
self, patched_session, note_source_type
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
MAX_TITLE_LENGTH,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="ok",
text_content="x",
file_type="note",
file_size=1,
document_hash=_generate_hash("oversize-title-update"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
oversize = "y" * (MAX_TITLE_LENGTH + 1)
with pytest.raises(ValueError, match="title exceeds maximum length"):
service.update_note(note_id, title=oversize)
# Title untouched.
patched_session.refresh(note)
assert note.title == "ok"
def test_update_note_accepts_max_length_title(
self, patched_session, note_source_type
):
"""Exact boundary: MAX_TITLE_LENGTH characters is allowed."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
MAX_TITLE_LENGTH,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="ok",
text_content="x",
file_type="note",
file_size=1,
document_hash=_generate_hash("title-boundary"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
edge = "z" * MAX_TITLE_LENGTH
assert NoteService("test_user").update_note(note_id, title=edge) is True
patched_session.refresh(note)
assert note.title == edge
def test_update_note_snapshot_lands_synchronously_without_summary(
self, patched_session, note_source_type
):
"""contract: the version snapshot row must exist immediately
after ``update_note`` returns. ``change_summary`` is filled by a
background worker; the user sees their save reflected in history
without waiting on the LLM.
Note: the test's autouse executor stub runs the worker inline so
``change_summary`` is populated by the time we observe the row —
but the row id is the same one ``update_note`` returned and the
snapshot row was already in the DB before the worker fired.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="t",
text_content="original body",
file_type="note",
file_size=len("original body"),
document_hash=_generate_hash("c1-sync-snapshot"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
# Seed a manual_save baseline so the next update is "content change"
# not "new note".
_create_snapshot(
NoteService("test_user"),
patched_session,
note_id=note_id,
title="t",
content="original body",
tags=[],
change_type="manual_save",
)
ok = NoteService("test_user").update_note(
note_id, content="edited body"
)
assert ok is True
versions = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id, change_type="manual_save")
.order_by(NoteVersion.created_at.asc())
.all()
)
# Snapshot landed: the edit produced a second row.
assert len(versions) == 2
edit_row = versions[1]
assert edit_row.content == "edited body"
def test_update_note_title_only_creates_version_via_full_path(
self, patched_session, note_source_type
):
"""End-to-end integration of C3 through update_note.
Pin the behaviour the prior unit tests only validated at the
``_create_version_snapshot`` boundary: a title-only edit going
through the public ``update_note`` API must produce a new
``NoteVersion`` row with the updated title.
Pre-fix this silently dedup'd to the existing snapshot and the
user's new title was unrecoverable via restore.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
body = "stable body text"
note = Document(
id=note_id,
title="Before",
text_content=body,
file_type="note",
file_size=len(body),
document_hash=_generate_hash("e2e-title-only"),
source_type_id=note_source_type.id,
tags=["t"],
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
# First save: create the baseline manual_save row.
_create_snapshot(
service,
patched_session,
note_id=note_id,
title="Before",
content=body,
tags=["t"],
change_type="manual_save",
)
# Title-only edit goes through the public update_note API.
ok = service.update_note(note_id, title="After")
assert ok is True
manual_saves = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id, change_type="manual_save")
.order_by(NoteVersion.created_at.asc())
.all()
)
# Two manual_save rows: one baseline, one for the title edit.
assert len(manual_saves) == 2, (
f"Expected 2 manual_save rows, got {len(manual_saves)}: "
f"{[(v.title, v.content[:20]) for v in manual_saves]}"
)
assert manual_saves[0].title == "Before"
assert manual_saves[1].title == "After"
# Body is unchanged.
assert manual_saves[1].content == body
def test_identical_state_still_dedups(
self, patched_session, note_source_type
):
"""Sanity: dedup still fires when title + content + tags are all the
same; we only want title/tag-only edits to bypass it."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Same",
text_content="same",
file_type="note",
file_size=4,
document_hash=_generate_hash("identical-dedup"),
source_type_id=note_source_type.id,
tags=["t"],
)
patched_session.add(note)
patched_session.commit()
service = NoteService("test_user")
v1 = _create_snapshot(
service,
patched_session,
note_id=note_id,
title="Same",
content="same",
tags=["t"],
change_type="manual_save",
)
v2 = _create_snapshot(
service,
patched_session,
note_id=note_id,
title="Same",
content="same",
tags=["t"],
change_type="manual_save",
)
assert v2 == v1, "Truly-identical state must dedup"
def test_list_notes_query_count_is_bounded(
self, patched_session, note_source_type
):
"""Regression guard against the list_notes N+1.
_document_to_note_dict used to issue 3 sub-queries per row
(collection_count, research_count, indexed_coll), which at the
default limit=100 produces 300+ statements per page. The batched
rewrite computes those three aggregates with one grouped query
each, so the total statement count must be O(1) in the number
of notes returned, not O(N).
"""
from sqlalchemy import event
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# Seed 25 notes — more than enough to expose any per-row cost.
for i in range(25):
note = Document(
id=str(uuid.uuid4()),
title=f"Note {i}",
text_content=f"Body {i}",
file_type="note",
file_size=len(f"Body {i}"),
document_hash=_generate_hash(f"note-n+1-{i}"),
source_type_id=note_source_type.id,
tags=[],
)
patched_session.add(note)
patched_session.commit()
statements: list[str] = []
engine = patched_session.get_bind()
def _before_cursor_execute(
conn, cursor, statement, parameters, context, executemany
):
statements.append(statement)
event.listen(engine, "before_cursor_execute", _before_cursor_execute)
try:
results = NoteService("test_user").list_notes(limit=100)
finally:
event.remove(
engine, "before_cursor_execute", _before_cursor_execute
)
assert len(results) == 25, (
"Expected list_notes to return all 25 seeded notes"
)
# 1 SELECT for documents + 3 grouped aggregates + (1 SELECT each)
# for source_type lookup. Loose upper bound: 10 statements, max.
# Pre-fix: ~76 (1 + 3 sub-queries * 25).
assert len(statements) <= 10, (
f"list_notes emitted {len(statements)} SQL statements for "
f"25 notes — likely a N+1 regression. Statements: {statements}"
)
def test_update_note_tag_only_save_creates_version_snapshot(
self, patched_session, note_source_type
):
"""Fix #4: a tags-only edit MUST create a NoteVersion row.
Pre-fix the snapshot trigger was ``content_changed or title_changed``,
so a save that only added/removed tags silently produced no
version. ``_compute_version_hash`` already includes tags, so the
dedup layer was prepared for tag-only edits — the trigger was the
gap. This test pins the trigger.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Untouched title",
text_content="Untouched body",
file_type="note",
file_size=14,
document_hash=_generate_hash("tag-only"),
source_type_id=note_source_type.id,
tags=["initial"],
)
patched_session.add(note)
patched_session.commit()
# Tags-only edit: no title or content arg supplied.
assert NoteService("test_user").update_note(
note_id, tags=["added-via-tag-only-save"]
)
versions = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id)
.all()
)
assert len(versions) == 1, (
"Tag-only save did not create a NoteVersion row — "
"update_note trigger is missing tags_changed."
)
assert versions[0].tags == ["added-via-tag-only-save"]
assert versions[0].title == "Untouched title"
def test_update_note_favorite_only_creates_no_version(
self, patched_session, note_source_type
):
"""a favorite-only toggle changes no content/title/tags, so the
snapshot trigger (content_changed or title_changed or tags_changed) is
False and NO NoteVersion row may be created — the mirror of the
tag-only test above. The route-level no-auto-index behaviour is covered
in test_notes_api.py; this pins the service path."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="Pin me",
text_content="body",
file_type="note",
file_size=4,
document_hash=_generate_hash("favorite-only"),
source_type_id=note_source_type.id,
tags=["t"],
favorite=False,
)
patched_session.add(note)
patched_session.commit()
assert NoteService("test_user").update_note(note_id, favorite=True)
patched_session.expire_all()
refreshed = (
patched_session.query(Document).filter_by(id=note_id).first()
)
assert refreshed.favorite is True
assert (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id)
.count()
== 0
), "a favorite-only toggle must not snapshot a NoteVersion"
def test_restore_with_bookends_writes_both_audit_rows(
self, patched_session, note_source_type
):
"""Fix #3: PRE_RESTORE and RESTORE audit rows must ALWAYS land,
even though their state hash matches an existing version row.
Pre-fix, the (document_id, content_hash) dedup pre-check in
``_create_version_snapshot_in_session`` returned the existing
version's id without inserting — silently dropping the bookend
and erasing the "user restored here" signal from history.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
from local_deep_research.database.models import NoteChangeType
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="v1-title",
text_content="v1-content",
file_type="note",
file_size=10,
document_hash=_generate_hash("restore-bookend"),
source_type_id=note_source_type.id,
tags=["v1"],
)
patched_session.add(note)
patched_session.commit()
svc = NoteService("test_user")
# Save v1 (initial), then save v2 (a real edit).
v1_id = _create_snapshot(
svc,
patched_session,
note_id=note_id,
title="v1-title",
content="v1-content",
tags=["v1"],
change_type=NoteChangeType.INITIAL.value,
)
assert svc.update_note(
note_id,
title="v2-title",
content="v2-content",
tags=["v2"],
)
# Restore to v1 — should write PRE_RESTORE (v2 state) + content
# update + RESTORE (v1 state). The RESTORE row's state hash equals
# v1_id's hash; pre-fix this collision dropped the audit row.
assert svc.restore_with_bookends(note_id, v1_id)
versions = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id)
.all()
)
change_types = sorted(v.change_type for v in versions)
# We expect at least: initial, manual_save (the v2 edit),
# pre_restore, restore. The dedup pre-check, if reinstated,
# would silently drop "restore" (and possibly "pre_restore").
assert NoteChangeType.PRE_RESTORE.value in change_types, (
"PRE_RESTORE audit row missing — dedup pre-check dropped it."
)
assert NoteChangeType.RESTORE.value in change_types, (
"RESTORE audit row missing — dedup pre-check dropped it."
)
def test_restore_reparses_links_inside_its_own_transaction(
self, patched_session, note_source_type, monkeypatch
):
"""restore_with_bookends must re-parse wiki-links via the IN-SESSION
path (atomic with the content write), not a separate post-commit
_parse_and_update_links session. The post-commit reparse raced a
concurrent update_note: that save wrote correct NoteLink rows for its
new content, then the stale post-restore reparse reverted them,
leaving the link graph inconsistent with Document.text_content.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
from local_deep_research.database.models import NoteLink
svc = NoteService("test_user")
# Target notes so the wiki-links resolve to real NoteLink rows.
svc.create_note(title="Alpha", content="a")
svc.create_note(title="Beta", content="b")
note_id = svc.create_note(title="src", content="see [[Alpha]]")
v_alpha = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id, content="see [[Alpha]]")
.first()
)
assert v_alpha is not None
# Move the live content (and its link graph) to [[Beta]].
assert svc.update_note(note_id, content="see [[Beta]]")
# A regression to the racy post-commit reparse would call the
# SEPARATE-session _parse_and_update_links; the fix uses the in-session
# variant, so this must never be called during restore.
called = []
monkeypatch.setattr(
NoteService,
"_parse_and_update_links",
lambda self, *a, **k: called.append(a),
)
assert svc.restore_with_bookends(note_id, v_alpha.id)
assert called == [], (
"restore used the racy separate-session _parse_and_update_links "
"instead of reparsing inside the restore transaction"
)
texts = {
lnk.link_text
for lnk in patched_session.query(NoteLink)
.filter_by(source_document_id=note_id)
.all()
}
assert texts == {"Alpha"}, (
f"restore did not reconcile the link graph to the restored "
f"content (got {texts})"
)
def test_create_note_initial_version_lands_atomically(
self, patched_session, note_source_type, monkeypatch
):
"""Fix #2: ``create_note`` must write the Document, the INITIAL
version snapshot, and any [[wiki-links]] in a single transaction.
Pre-fix the Document committed in its own session and the snapshot
+ link reparse ran in fresh sessions afterwards. A simulated
snapshot failure left a Document with no INITIAL audit row.
Inject a failure at the in-session snapshot helper and assert that
no Document is durably committed.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# Ensure the lazily-created notes collection has a row to attach
# to (the service creates one if missing).
svc = NoteService("test_user")
original_snapshot = svc._create_version_snapshot_in_session
call_count = {"n": 0}
def boom_on_snapshot(self_inner, session, *args, **kwargs):
call_count["n"] += 1
raise RuntimeError("simulated snapshot failure")
# Patch on the instance via class — we want the in-session helper
# raise to propagate out (no swallowing).
monkeypatch.setattr(
NoteService,
"_create_version_snapshot_in_session",
boom_on_snapshot,
)
with pytest.raises(RuntimeError, match="simulated snapshot failure"):
svc.create_note(
title="never-persisted",
content="never persisted",
tags=["never"],
)
# The whole transaction was rolled back: no Document, no snapshot.
assert call_count["n"] == 1
assert (
patched_session.query(Document)
.filter_by(title="never-persisted")
.first()
is None
), "create_note left a Document committed despite snapshot failure"
# Restore for hygiene (monkeypatch is autouse-scoped but be safe).
monkeypatch.setattr(
NoteService,
"_create_version_snapshot_in_session",
original_snapshot,
)
def test_validate_title_rejects_whitespace_only(self):
"""_validate_title now rejects whitespace-only titles so the slug
generator doesn't emit empty strings and the wiki-link resolver
can't match an invisible title via ``[[ ]]``.
"""
from local_deep_research.research_library.notes.services.note_service import (
_validate_title,
)
for blank in (" ", "\t", "\n", " \t\n "):
with pytest.raises(ValueError, match="blank or whitespace"):
_validate_title(blank)
# Still accept legitimate values.
_validate_title(None) # explicit None = "don't update"
_validate_title("") # empty falls through to route-layer guard
_validate_title("Real title")
_validate_title(" surrounded by spaces ") # non-blank content
def test_get_backlinks_propagates_db_errors(
self, patched_session, note_source_type, monkeypatch
):
"""Pre-fix get_backlinks swallowed every Exception and returned
``[]`` — a DB failure looked identical to "no backlinks." Now
the exception propagates so the route layer can return a 500
via handle_api_error.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
patched_session.add(
Document(
id=note_id,
title="A",
text_content="body",
file_type="note",
file_size=4,
document_hash=_generate_hash("backlink-err"),
source_type_id=note_source_type.id,
tags=[],
)
)
patched_session.commit()
# Inject a query failure by patching the session's query method.
def _boom(*args, **kwargs):
raise RuntimeError("simulated db failure")
monkeypatch.setattr(patched_session, "query", _boom)
with pytest.raises(RuntimeError, match="simulated db failure"):
NoteService("test_user").get_backlinks(note_id)
def test_generate_slug_non_latin_titles_degrade_gracefully(self):
"""_generate_slug uses stdlib NFKD (no copyleft transliterator): Latin
diacritics fold to ASCII, but scripts with no ASCII form (CJK,
Cyrillic, Arabic) and emoji drop out. A title that reduces to nothing
falls back to a title-derived ``note-<hex>`` rather than the bare
``"note"`` constant (#6389); ASCII around dropped runs still survives.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# CJK-only → nothing survives the ASCII fold → derived fallback
assert NoteService._generate_slug("中文笔记").startswith("note-")
# Cyrillic-only → same
assert NoteService._generate_slug("Привет").startswith("note-")
# Diacritics still collapse to ASCII (NFKD)
assert NoteService._generate_slug("café") == "cafe"
# Emoji-only → same
assert NoteService._generate_slug("🎉🚀").startswith("note-")
# Mixed: the dropped CJK leaves the ASCII half
assert NoteService._generate_slug("Hello 世界") == "hello"
# ASCII-only behaviour unchanged
assert NoteService._generate_slug("Hello World") == "hello-world"
def test_generate_slug_fallback_distinguishes_two_non_latin_titles(self):
"""The defect this replaces: the constant made every CJK note share
one slug, so the autocomplete payload could not tell two notes apart.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
assert NoteService._generate_slug(
"中文笔记"
) != NoteService._generate_slug("日本語")
assert NoteService._generate_slug(
"Привет"
) != NoteService._generate_slug("🎉🚀")
def test_generate_slug_fallback_answers_case_and_composition_as_ascii_does(
self,
):
"""The ASCII branch lowercases and NFKD-folds, so ``Hello`` and
``hello`` share a slug. The fallback has to agree: otherwise renaming a
note from ``Привет`` to ``привет``, or two clients sending the same
title in different Unicode compositions, silently mints a second slug
for a title that did not change.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
assert NoteService._generate_slug(
"Привет"
) == NoteService._generate_slug("привет")
# U+0439 precomposed vs U+0438 U+0306 decomposed: one rendered glyph.
assert NoteService._generate_slug(
"\u0439"
) == NoteService._generate_slug("\u0438\u0306")
def test_generate_slug_fallback_is_derived_from_content_not_length(self):
"""``!!!`` and ``???`` are the same length and different text. A
fallback keyed on anything but the content would collapse them, which
is the collision this replaces.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
assert NoteService._generate_slug("!!!") != NoteService._generate_slug(
"???"
)
def test_generate_slug_fallback_is_deterministic_and_ascii(self):
"""A slug goes into a URL and is recomputed per request, so the
fallback has to be stable across calls and ASCII-only — a random or
time-derived value would satisfy "distinguishable" and break both.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
first = NoteService._generate_slug("中文笔记")
second = NoteService._generate_slug("中文笔记")
assert first == second
assert first.isascii()
assert re.fullmatch(r"note-[0-9a-f]{12}", first)
def test_generate_slug_fallback_collapses_whitespace_like_the_ascii_branch(
self,
):
"""``" Hello World "`` and ``"Hello World"`` already give one slug.
The fallback follows that rule rather than hashing raw bytes, so
padding does not mint a second slug for one title.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
assert NoteService._generate_slug(" 中文 笔记 ") == (
NoteService._generate_slug("中文 笔记")
)
def test_generate_slug_fallback_survives_a_lone_surrogate(self):
"""A title round-tripped through a filesystem or a lenient client can
carry an unpaired surrogate, which plain UTF-8 encoding raises on.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
slug = NoteService._generate_slug("\ud800")
assert re.fullmatch(r"note-[0-9a-f]{12}", slug)
def test_summary_executor_drops_task_when_queue_full(self, monkeypatch):
"""When the change-summary worker queue exceeds the soft cap,
_submit_summary_task drops the task rather than letting the
queue grow unbounded. Pre-fix, ThreadPoolExecutor's internal
SimpleQueue had no maxsize — a script-driven save burst could
retain hundreds of MB of (old_content, new_content) pairs.
"""
from local_deep_research.research_library.notes.services import (
note_service,
)
# Build a fake executor whose work-queue reports qsize > cap.
class _FullQueue:
def qsize(self):
return note_service._SUMMARY_QUEUE_SOFT_CAP + 1
submitted = []
class _FakeExecutor:
_work_queue = _FullQueue()
def submit(self, fn, *args, **kwargs):
submitted.append((fn, args, kwargs))
monkeypatch.setattr(
note_service,
"_get_summary_executor",
lambda: _FakeExecutor(),
)
def _dummy_fn():
pass
note_service._submit_summary_task(_dummy_fn, "arg")
assert submitted == [], "task was submitted despite full queue"
def test_summary_executor_submits_thread_cleanup_wrapped_callable(
self, monkeypatch
):
"""Fix A (credential hygiene): ``_submit_summary_task`` must submit
a ``thread_cleanup``-WRAPPED callable to the executor, never the bare
worker fn.
The summary pool's threads are long-lived, so ``cleanup_dead_threads``
never sweeps them. Without the wrapper the plaintext SQLCipher
password the worker captured (and its thread-local DB session) would
sit in the pool thread's ``_thread_credentials`` dict for the whole
process lifetime — the #4182 credential-hygiene class. The fix wraps
the worker in ``thread_cleanup(fn)`` so the worker clears both when it
returns.
Revert target: ``executor.submit(fn, *args, **kwargs)`` (the bare fn)
makes the submitted object identical to ``fn`` — both the identity
assertion and the behavioural cleanup assertion below flip red.
"""
import threading
from local_deep_research.research_library.notes.services import (
note_service,
)
from local_deep_research.database import thread_local_session
# Capture the callable the executor receives (queue under the soft
# cap so the task isn't dropped by the backpressure guard).
captured = {}
class _CapturingExecutor:
class _Queue:
def qsize(self):
return 0
_work_queue = _Queue()
def submit(self, fn, *args, **kwargs):
captured["fn"] = fn
captured["args"] = args
captured["kwargs"] = kwargs
monkeypatch.setattr(
note_service,
"_get_summary_executor",
lambda: _CapturingExecutor(),
)
worker_ran = {"n": 0}
def real_worker(a, b):
worker_ran["n"] += 1
return (a, b)
note_service._submit_summary_task(real_worker, "x", b="y")
submitted = captured["fn"]
# 1. Identity: the submitted object is NOT the bare worker fn.
# ``thread_cleanup(fn)`` returns a distinct functools.wraps wrapper.
assert submitted is not real_worker, (
"executor received the bare worker fn — thread_cleanup wrapper "
"is missing (credential hygiene regression)"
)
# 2. The wrapper preserves __wrapped__ (functools.wraps), so we can
# prove it specifically wraps OUR worker, not some unrelated shim.
assert getattr(submitted, "__wrapped__", None) is real_worker, (
"submitted callable does not wrap the worker via thread_cleanup"
)
# 3. Behavioural proof: running the submitted callable on this thread
# clears the thread-local credential entry. Seed a fake credential
# for the current thread, then invoke the wrapped callable (which
# runs the worker inside _ThreadCleanup); on exit it must call
# cleanup_current_thread, removing the entry. The bare fn would
# leave the entry in place.
tid = threading.get_ident()
mgr = thread_local_session.thread_session_manager
with mgr._lock:
mgr._thread_credentials[tid] = ("test_user", "secret-password")
submitted(*captured["args"], **captured["kwargs"])
assert worker_ran["n"] == 1, "wrapped callable did not run the worker"
with mgr._lock:
assert tid not in mgr._thread_credentials, (
"thread credentials NOT cleared after the wrapped worker ran "
"— thread_cleanup wrapping is missing"
)
def test_create_version_snapshot_coerces_null_content_to_empty(
self, patched_session, note_source_type
):
"""Fix E (null coercion): ``_create_version_snapshot_in_session``
must coerce ``content=None`` to ``""`` (and ``tags=None`` to ``[]``)
before inserting.
``NoteVersion.content``/``tags`` are NOT NULL, but the source these
snapshot — ``Document.text_content`` — IS nullable, and the restore
bookend path passes document fields through uncoerced. Without the
coercion a NULL content reaches the flush and raises IntegrityError.
Revert target: removing ``content = content or ""`` /
``tags = tags or []`` at the top of the helper → IntegrityError /
NOT NULL violation on flush instead of the clean row asserted below.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note_id = str(uuid.uuid4())
note = Document(
id=note_id,
title="t",
text_content=None, # nullable on Document; mirrors the real case
file_type="note",
file_size=0,
document_hash=_generate_hash("null-coercion-snapshot"),
source_type_id=note_source_type.id,
tags=None,
)
patched_session.add(note)
patched_session.commit()
svc = NoteService("test_user")
# Call the in-session helper directly with content=None / tags=None.
# Pre-fix this flush raised IntegrityError on the NOT NULL columns.
version_id, created = svc._create_version_snapshot_in_session(
patched_session,
note_id=note_id,
title="t",
content=None,
tags=None,
change_type="manual_save",
)
patched_session.commit()
assert created is True
row = (
patched_session.query(NoteVersion).filter_by(id=version_id).first()
)
assert row is not None, "no NoteVersion row was created"
# The NOT NULL columns were coerced to their empty sentinels.
assert row.content == "", (
f"content was not coerced to empty string: {row.content!r}"
)
assert row.tags == [], (
f"tags was not coerced to empty list: {row.tags!r}"
)
class TestNoteIndexStatus:
"""NoteService.get_notes_index_status — Ask-your-notes pre-flight counts."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
from contextlib import contextmanager
@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
def test_empty_when_no_notes(self, patched_session, note_source_type):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
status = NoteService("test_user").get_notes_index_status()
assert status["note_count"] == 0
assert status["indexed_count"] == 0
# The Notes collection is created lazily, so an id is always returned.
assert status["collection_id"]
def test_counts_notes_and_indexed(self, patched_session, note_source_type):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
svc = NoteService("test_user")
svc.create_note(title="N1", content="body one")
status = svc.get_notes_index_status()
assert status["note_count"] == 1
# A freshly-created note isn't embedded yet.
assert status["indexed_count"] == 0
# Flip its Notes-collection membership to indexed.
row = (
patched_session.query(DocumentCollection)
.filter_by(collection_id=status["collection_id"])
.first()
)
assert row is not None
row.indexed = True
patched_session.commit()
assert svc.get_notes_index_status()["indexed_count"] == 1
class TestNoteUnlinkedMentions:
"""NoteService.get_unlinked_mentions — lexical mention discovery."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
from contextlib import contextmanager
@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
def _note(self, session, source_type_id, title, content, salt):
return _make_note_doc(session, source_type_id, title, content, salt)
def test_returns_unlinked_mentions_and_excludes_linked(
self, patched_session, note_source_type
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
target = self._note(
patched_session, st, "Photosynthesis Basics", "about plants", "t"
)
mentioner = self._note(
patched_session,
st,
"Plant Biology",
"See Photosynthesis Basics for the chemistry.",
"m",
)
linker = self._note(
patched_session,
st,
"Cell Energy",
"[[Photosynthesis Basics]] matters here.",
"l",
)
# `linker` already links to target → must be excluded even though it
# also contains the title text.
patched_session.add(
NoteLink(
source_document_id=linker.id,
target_document_id=target.id,
link_text="Photosynthesis Basics",
)
)
patched_session.commit()
mentions = NoteService("test_user").get_unlinked_mentions(target.id)
ids = {m["id"] for m in mentions}
assert mentioner.id in ids
assert linker.id not in ids # already linked
assert target.id not in ids # never self
def test_short_title_returns_empty(self, patched_session, note_source_type):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
tiny = self._note(patched_session, st, "Hi", "x", "tiny")
self._note(patched_session, st, "Other", "Hi there everyone", "o")
# "Hi" is below MIN_MENTION_TITLE_LEN → skipped (too generic).
assert NoteService("test_user").get_unlinked_mentions(tiny.id) == []
def test_unlinked_mentions_excludes_linked_in_sql_no_underreport(
self, patched_session, note_source_type
):
"""already-linked notes are excluded in SQL, not in Python after a
``limit * 3`` over-fetch. With limit=2 and SIX already-linked notes
inserted before two unlinked ones, the pre-fix over-fetch
(``.limit(6)`` with no ORDER BY) returned the six linked rows first and
the Python filter dropped them all — yielding zero mentions though two
existed. The fix excludes the linked rows in SQL and orders
deterministically, so both unlinked mentions are returned.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
target = self._note(
patched_session, st, "Quantum Mechanics", "physics", "qm-target"
)
# Six already-linked notes that ALSO mention the title, inserted FIRST
# (lower rowid → first in an unordered over-fetch window).
for i in range(6):
linker = self._note(
patched_session,
st,
f"Linker {i}",
"Quantum Mechanics is referenced here.",
f"qm-link-{i}",
)
patched_session.add(
NoteLink(
source_document_id=linker.id,
target_document_id=target.id,
link_text="Quantum Mechanics",
)
)
patched_session.commit()
# Two UNLINKED mentioners inserted AFTER the linked ones.
unlinked_a = self._note(
patched_session,
st,
"Unlinked A",
"Quantum Mechanics matters.",
"qm-a",
)
unlinked_b = self._note(
patched_session, st, "Unlinked B", "More Quantum Mechanics.", "qm-b"
)
service = NoteService("test_user")
mentions = service.get_unlinked_mentions(target.id, limit=2)
ids = {m["id"] for m in mentions}
assert ids == {unlinked_a.id, unlinked_b.id}
assert len(mentions) == 2
# Deterministic: a second identical call returns the same ordering.
again = service.get_unlinked_mentions(target.id, limit=2)
assert [m["id"] for m in again] == [m["id"] for m in mentions]
class TestReviewRound2ServiceFixes:
"""Regression guards for the round-2 review fixes in note_service.py.
Covers: dedup-absorbed snapshots must not feed the change-summary
worker, restore must reset DocumentCollection.indexed, empty wiki-link
targets must not resolve/persist, the version hash must not be
comma-injectable via tags, early type validation (content /
collection_ids), the MAX_RESEARCH_PER_NOTE cap on link creation, and
the Notes-collection unlink guard.
"""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
"""Yield the test db_session whenever NoteService opens a per-user
one; stub the LLM summary call and run the summary worker inline.
Also stubs ``_capture_request_db_password`` to return a value:
outside a Flask request context it returns None, which makes
``update_note`` skip the worker entirely — the dedup-vs-fresh-row
scheduling behaviour under test would be unreachable.
"""
from contextlib import contextmanager
@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,
)
from local_deep_research.research_library.notes.services.note_ai_service import (
NoteAIService,
)
# The inline summary worker calls the SYNC entry point
# (summarize_changes_sync) — it owns no event loop, so it must not
# touch the async core (#6293). Stub the method it actually calls.
def _stubbed_summary(self, old, new):
return "stubbed"
monkeypatch.setattr(
NoteAIService,
"summarize_changes_sync",
_stubbed_summary,
)
monkeypatch.setattr(
"local_deep_research.research_library.notes.services.note_service._capture_request_db_password",
lambda username: "test-pw",
)
# The summary executor is already run inline by the package-wide
# autouse fixture _force_synchronous_summary_executor (conftest.py).
return db_session
def _note(self, session, source_type_id, title, content, salt, tags=None):
return _make_note_doc(
session, source_type_id, title, content, salt, tags=tags
)
# -------------------------------------------------------------------
# dedup-absorbed snapshot id must not reach the worker
# -------------------------------------------------------------------
def test_dedup_absorbed_save_does_not_overwrite_historical_summary(
self, patched_session, note_source_type
):
"""Reverting a note to an exact prior state dedup-absorbs the
snapshot into the historical version row. Pre-fix, that row's id
was handed to the change-summary worker, which overwrote e.g.
"Initial version" with a description of the much later edit.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
svc = NoteService("test_user")
note_id = svc.create_note(title="t", content="content A")
# A → B: fresh MANUAL_SAVE row, worker (inline) fills "stubbed".
assert svc.update_note(note_id, content="content B")
# B → A: state hash matches the INITIAL snapshot → dedup-absorbed.
assert svc.update_note(note_id, content="content A")
patched_session.expire_all()
initial = (
patched_session.query(NoteVersion)
.filter_by(document_id=note_id, change_type="initial")
.one()
)
assert initial.change_summary == "Initial version", (
"Dedup-absorbed save fed the historical INITIAL row to the "
"change-summary worker, clobbering its summary."
)
def test_no_summary_task_scheduled_for_dedup_absorbed_snapshot(
self, patched_session, note_source_type, monkeypatch
):
"""Pin the scheduling fix itself (independent of the worker's
fill-only guard): a dedup-absorbed save must not submit a
summary task at all.
"""
from local_deep_research.research_library.notes.services import (
note_service as ns_module,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
svc = NoteService("test_user")
note_id = svc.create_note(title="t", content="content A")
submitted = []
monkeypatch.setattr(
ns_module,
"_submit_summary_task",
lambda fn, *a, **k: submitted.append(a),
)
assert svc.update_note(note_id, content="content B")
assert len(submitted) == 1 # fresh row → worker scheduled
assert svc.update_note(note_id, content="content A")
assert len(submitted) == 1, (
"Dedup-absorbed snapshot scheduled a change-summary task "
"for a historical version row."
)
def test_summary_task_scheduled_on_first_content_edit_of_blank_note(
self, patched_session, note_source_type, monkeypatch
):
"""A first-content edit of a previously blank/empty note must STILL
schedule the change-summary worker. Pre-fix the guard required
``old_content`` to be truthy, so this MANUAL_SAVE version's
change_summary stayed NULL forever — even though summarize_changes
handles the empty-old case cheaply (`if not old_content: return
"Note created"`).
"""
from local_deep_research.research_library.notes.services import (
note_service as ns_module,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# A note that currently has an EMPTY body.
note = self._note(
patched_session, note_source_type.id, "t", "", "blank-note"
)
svc = NoteService("test_user")
submitted = []
monkeypatch.setattr(
ns_module,
"_submit_summary_task",
lambda fn, *a, **k: submitted.append(a),
)
assert svc.update_note(note.id, content="now has real content")
assert len(submitted) == 1, (
"First-content edit of a blank note did not schedule the "
"change-summary worker; its version's change_summary would stay "
"NULL forever."
)
def test_summary_worker_refuses_to_overwrite_existing_summary(
self, patched_session, note_source_type
):
"""Belt-and-suspenders: even when handed a row that already has a
summary, the worker must not overwrite it.
"""
from local_deep_research.research_library.notes.services.note_service import (
_populate_change_summary_async,
)
note = self._note(
patched_session, note_source_type.id, "t", "c", "worker-guard"
)
version_id = str(uuid.uuid4())
patched_session.add(
NoteVersion(
id=version_id,
document_id=note.id,
title="t",
content="c",
change_type="initial",
change_summary="Initial version",
content_hash="guard-hash",
)
)
patched_session.commit()
_populate_change_summary_async(
"test_user", "test-pw", version_id, "old", "new"
)
patched_session.expire_all()
row = patched_session.query(NoteVersion).filter_by(id=version_id).one()
assert row.change_summary == "Initial version"
def test_summary_worker_populates_the_summary_without_an_event_loop(
self, db_session, note_source_type, monkeypatch
):
"""The change-summary worker must reach the LLM through the SYNC
path and never create an event loop of its own (#6293).
It runs on a plain ``ThreadPoolExecutor`` thread while the notes
routers await the async NoteAIService cores on uvicorn's request
loop. ``langchain_openai`` / ``langchain_anthropic`` cache ONE
``httpx.AsyncClient`` per process, keyed without any loop or thread
component, so a throwaway loop here ends up reusing a keep-alive
connection that belongs to the request loop: the read fails with
``RuntimeError: Event loop is closed`` AFTER the request has gone
out, the openai SDK's two default retries silently re-send it, and
the dead loop leaks an eventpoll FD.
Both halves are guarded. Creating a loop raises ``_LoopCreated``;
the stub LLM answers ``invoke`` but raises ``_AsyncPathUsed`` for
``ainvoke`` (or anything else the worker reaches for). Both derive
from ``BaseException`` so the worker's blanket ``except Exception``
cannot swallow the evidence and leave a bare "summary is NULL"
failure behind.
"""
import asyncio
from contextlib import contextmanager
from types import SimpleNamespace
from local_deep_research.research_library.notes.services.note_ai_service import (
NoteAIService,
)
from local_deep_research.research_library.notes.services.note_service import (
_populate_change_summary_async,
)
class _LoopCreated(BaseException):
pass
class _AsyncPathUsed(BaseException):
pass
def _no_new_loops(*args, **kwargs):
raise _LoopCreated(
"the change-summary worker created an event loop"
)
monkeypatch.setattr(asyncio, "run", _no_new_loops)
monkeypatch.setattr(asyncio, "new_event_loop", _no_new_loops)
@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,
)
seen = {}
class _SyncOnlyLLM:
"""Exposes ``invoke`` and ``close``; every other attribute is a
tripwire (``__getattr__`` only fires for names not found)."""
def invoke(self, prompt):
seen["prompt"] = prompt
return SimpleNamespace(content="Reworded the opening line.")
def close(self):
seen["closed"] = True
def __getattr__(self, name):
raise _AsyncPathUsed(
f"the change-summary worker reached for llm.{name}"
)
monkeypatch.setattr(
NoteAIService,
"_get_llm",
lambda self, temperature=None: _SyncOnlyLLM(),
)
note = _make_note_doc(
db_session, note_source_type.id, "t", "c", "loop-free"
)
version_id = str(uuid.uuid4())
db_session.add(
NoteVersion(
id=version_id,
document_id=note.id,
title="t",
content="c",
change_type="manual_save",
change_summary=None,
content_hash="loop-free-hash",
)
)
db_session.commit()
_populate_change_summary_async(
"test_user", "test-pw", version_id, "old body", "new body"
)
db_session.expire_all()
row = db_session.query(NoteVersion).filter_by(id=version_id).one()
assert row.change_summary == "Reworded the opening line.", (
"the sync change-summary path did not persist the LLM text"
)
# The shared prompt builder ran, and the shared teardown released
# the instance — the sync path is not a second implementation.
assert "old body" in seen["prompt"]
assert "new body" in seen["prompt"]
assert seen.get("closed") is True
# The sync twin must short-circuit identical content exactly like
# the async core's shared shortcut does, without touching the LLM
# (and therefore without bridging to the async core on a
# throwaway loop) — no separate no-op rule to drift out of sync.
ai_service = NoteAIService("test_user", dbpw="test-pw")
assert ai_service.summarize_changes_sync("x", "x") == "No changes"
# -------------------------------------------------------------------
# restore must reset DocumentCollection.indexed
# -------------------------------------------------------------------
def test_restore_resets_indexed_flag(
self, patched_session, note_source_type
):
"""restore_with_bookends changes content, so every
DocumentCollection row must flip indexed=False (mirroring
update_note) or the auto-index worker (force_reindex=False)
skips and semantic search serves pre-restore embeddings forever.
"""
from local_deep_research.database.models import Collection
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
svc = NoteService("test_user")
note = self._note(
patched_session, note_source_type.id, "t", "current", "restore-idx"
)
collection = Collection(
id=str(uuid.uuid4()), name="Notes", collection_type="notes"
)
patched_session.add(collection)
patched_session.add(
DocumentCollection(
document_id=note.id,
collection_id=collection.id,
indexed=True,
)
)
patched_session.commit()
version_id = _create_snapshot(
svc,
patched_session,
note_id=note.id,
title="t",
content="older content",
tags=[],
change_type="manual_save",
)
ok, err = svc.restore_with_bookends(note.id, version_id)
assert ok, f"restore failed: {err}"
patched_session.expire_all()
dc = (
patched_session.query(DocumentCollection)
.filter_by(document_id=note.id)
.one()
)
assert dc.indexed is False, (
"restore_with_bookends left DocumentCollection.indexed=True — "
"stale embeddings survive the restore."
)
# -------------------------------------------------------------------
# empty wiki-link target must not resolve or persist
# -------------------------------------------------------------------
def test_resolve_link_internal_empty_target_returns_none(
self, db_session, note_source_type
):
"""A whitespace-only or pipe-first target reduces to '' — pre-fix
startswith('') matched EVERY note and 'resolved' to the oldest.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# A victim note that the empty prefix would otherwise match.
self._note(
db_session, note_source_type.id, "Oldest Note", "x", "victim"
)
svc = NoteService("test_user")
for degenerate in (" ", "\t", "|click here", " |alias"):
assert svc._resolve_link_internal(db_session, degenerate) is None, (
f"degenerate link text {degenerate!r} resolved to a note"
)
def test_parse_links_empty_target_creates_no_phantom_link(
self, db_session, note_source_type
):
"""'[[ ]]' and '[[|click here]]' must create zero NoteLink rows."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
self._note(db_session, st, "Oldest Note", "x", "phantom-victim")
source = self._note(db_session, st, "Source", "y", "phantom-src")
svc = NoteService("test_user")
resolved = svc._parse_and_update_links_in_session(
db_session, source.id, "before [[ ]] middle [[|click here]] end"
)
db_session.commit()
assert resolved == []
links = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert links == [], (
"Empty wiki-link target persisted a phantom NoteLink"
)
# -------------------------------------------------------------------
# tag separator must not be injectable into the hash
# -------------------------------------------------------------------
def test_compute_version_hash_tag_separator_not_injectable(self):
"""['a,b'] and ['a','b'] are distinct tag states and must hash
differently (pre-fix the ',' join collided them, silently
dedup-absorbing a real tag-only edit from version history).
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
assert NoteService._compute_version_hash(
"t", "c", ["a,b"]
) != NoteService._compute_version_hash("t", "c", ["a", "b"])
# Order-insensitivity must survive the encoding change.
assert NoteService._compute_version_hash(
"t", "c", ["b", "a"]
) == NoteService._compute_version_hash("t", "c", ["a", "b"])
def test_compute_version_hash_nul_in_title_content_not_injectable(self):
"""title/content can each contain a literal NUL (a JSON "\\u0000"
survives request parsing and SQLite TEXT storage untouched), so two
genuinely different (title, content) pairs that differ only by where a
NUL sits must still hash differently. Pre-fix the NUL-joined payload
collided them, silently dedup-absorbing a real save out of version
history.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
h = NoteService._compute_version_hash
# Both would build the identical NUL-joined payload
# "MyTitle\x00Hello\x00World\x00[]" pre-fix.
assert h("MyTitle", "Hello\x00World", []) != h(
"MyTitle\x00Hello", "World", []
)
# A NUL genuinely in the content is still distinct from no NUL.
assert h("t", "a\x00b", []) != h("t", "ab", [])
def test_compute_version_hash_drops_non_string_tags(self):
"""the hash canonicalization filters non-string tags
(``isinstance(t, str)``), so a list with a stray int/None hashes
identically to the same list without them — while a string ``"123"``
is a real tag and must NOT collide with the dropped int ``123``.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
h = NoteService._compute_version_hash
# int/None are dropped → same hash as the string-only tag list.
assert h("t", "c", ["a", 123, None]) == h("t", "c", ["a"])
# A string "123" survives and is distinct from the dropped int 123.
assert h("t", "c", ["a", 123]) != h("t", "c", ["a", "123"])
# -------------------------------------------------------------------
# early type validation → ValueError (routes map to 400)
# -------------------------------------------------------------------
def test_assert_content_size_rejects_non_string(self):
from local_deep_research.research_library.notes.services.note_service import (
_assert_content_size,
)
for bad in (123, None, ["x"], {"x": 1}):
with pytest.raises(ValueError, match="content must be a string"):
_assert_content_size(bad)
_assert_content_size("fine") # strings still pass
def test_create_note_rejects_non_string_collection_id_elements(self):
"""An unhashable collection_ids element used to raise TypeError in
the de-dupe set comprehension → opaque 500 instead of 400.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
svc = NoteService("test_user")
with pytest.raises(
ValueError, match="each collection_id must be a string"
):
svc.create_note(title="t", content="c", collection_ids=[{"x": 1}])
def test_create_note_rejects_non_string_content(self):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
with pytest.raises(ValueError, match="content must be a string"):
NoteService("test_user").create_note(title="t", content=123)
# -------------------------------------------------------------------
# MAX_RESEARCH_PER_NOTE enforced on link creation
# -------------------------------------------------------------------
def test_link_research_enforces_cap(
self, patched_session, note_source_type, monkeypatch
):
"""The cap previously existed only on reorder; a note that grew
past it became permanently un-reorderable. Enforce it where rows
are created.
"""
from datetime import datetime, timezone
from local_deep_research.database.models import ResearchHistory
from local_deep_research.research_library.notes.services import (
note_service as ns_module,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
monkeypatch.setattr(ns_module, "MAX_RESEARCH_PER_NOTE", 2)
note = self._note(
patched_session, note_source_type.id, "t", "c", "research-cap"
)
research_ids = [str(uuid.uuid4()) for _ in range(3)]
now = datetime.now(timezone.utc)
for i, rid in enumerate(research_ids):
patched_session.add(
ResearchHistory(
id=rid,
query=f"q{i}",
mode="quick",
status="completed",
created_at=now.isoformat(),
)
)
patched_session.commit()
svc = NoteService("test_user")
svc.link_research_to_note(note.id, research_ids[0])
svc.link_research_to_note(note.id, research_ids[1])
with pytest.raises(ValueError, match="too many linked researches"):
svc.link_research_to_note(note.id, research_ids[2])
# -------------------------------------------------------------------
# never unlink a note from the Notes collection
# -------------------------------------------------------------------
def test_remove_from_collection_refuses_notes_system_collection(
self, patched_session, note_source_type
):
"""The Notes collection is the persistent home of every note; the
deletion services' orphan handling relies on it. Unlinking from
it must be refused.
"""
from local_deep_research.database.models import Collection
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note = self._note(
patched_session, note_source_type.id, "t", "c", "notes-guard"
)
notes_coll = Collection(
id=str(uuid.uuid4()), name="Notes", collection_type="notes"
)
other_coll = Collection(
id=str(uuid.uuid4()), name="Project X", collection_type="user"
)
patched_session.add_all([notes_coll, other_coll])
for coll in (notes_coll, other_coll):
patched_session.add(
DocumentCollection(
document_id=note.id,
collection_id=coll.id,
indexed=False,
)
)
patched_session.commit()
svc = NoteService("test_user")
with pytest.raises(
ValueError, match="Cannot remove a note from the Notes collection"
):
svc.remove_from_collection(note.id, notes_coll.id)
# Membership in the Notes collection is untouched.
assert (
patched_session.query(DocumentCollection)
.filter_by(document_id=note.id, collection_id=notes_coll.id)
.one()
is not None
)
# Removing from an ordinary collection still works.
assert svc.remove_from_collection(note.id, other_coll.id) is True
# -------------------------------------------------------------------
# Multi-mention wiki-link dedup (note_service.py:2022,2059-2061)
# -------------------------------------------------------------------
def test_duplicate_link_text_to_same_target_dedupes_to_one_note_link(
self, db_session, note_source_type
):
"""Three mentions of the same target ([[Alpha]], [[alpha]],
[[Alpha|see here]]) must collapse to a single NoteLink row.
Without the ``seen_targets`` dedup (note_service.py:2022,2059-2061)
the 2nd/3rd mentions each ``session.add`` a duplicate
(source, target) row, violating ``uix_note_link`` so the flush /
commit raises IntegrityError. Every existing len(links)==1 test uses
a SINGLE mention, so this multi-mention path is otherwise untested.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
target = self._note(
db_session, st, "Alpha", "target body", "dup-target"
)
source = self._note(db_session, st, "Source", "src body", "dup-src")
svc = NoteService("test_user")
resolved = svc._parse_and_update_links_in_session(
db_session,
source.id,
"see [[Alpha]] and again [[alpha]] plus [[Alpha|see here]]",
)
db_session.commit() # must NOT raise IntegrityError
links = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
assert len(links) == 1
assert links[0].target_document_id == target.id
assert len(resolved) == 1
# Re-parsing the same content keeps it at one link (no accumulation).
svc._parse_and_update_links_in_session(
db_session,
source.id,
"see [[Alpha]] and again [[alpha]] plus [[Alpha|see here]]",
)
db_session.commit()
assert (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.count()
== 1
)
def test_parse_links_skips_self_reference_resolved_by_title(
self, db_session, note_source_type
):
"""A note body containing [[<its own title>]] must create no
self-link, exercising the TITLE-resolution self-link path.
``_resolve_link_internal`` finds the note by exact title
(note_service.py:2146-2156); two layered guards keep it from
becoming a self-link: ``_valid_note_target`` returns None when
``doc_id == note_id`` (line 2012) and the persist filter requires
``target['id'] != note_id`` (line 2058). If BOTH regressed, a
NoteLink with source==target would flush and the DB CheckConstraint
(note.py:192-193) would raise IntegrityError on commit — losing the
user's edit (commit() below surfaces that as a test failure). The
title-resolution path is otherwise untested; distinct from the
accept-suggested path (test_accept_refuses_self_link) and the
direct-DB constraint test (test_no_self_links).
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
selfnote = self._note(
db_session, st, "My Self Note", "placeholder", "self-ref-src"
)
svc = NoteService("test_user")
resolved = svc._parse_and_update_links_in_session(
db_session,
selfnote.id,
"See [[My Self Note]] above and also [[my self note]] lowercase.",
)
db_session.commit() # IntegrityError here = a regressed self guard
assert resolved == []
assert (
db_session.query(NoteLink)
.filter_by(source_document_id=selfnote.id)
.count()
== 0
)
def test_parse_links_newline_inside_target_creates_no_link(
self, db_session, note_source_type
):
"""A ``[[...]]`` whose closing ``]]`` is on a later line is an
unclosed bracket, not a wiki-link, and must create no NoteLink —
even when a note happens to carry the (illegal) multi-line title.
``LINK_PATTERN`` excludes newlines (``[^\\]\\n]``) so parse agrees
with the frontend's ``WIKILINK_TARGET`` in formatting.js, which
also excludes ``\\n``. Pre-fix the backend class was ``[^\\]]``,
which captured across the newline and created a NoteLink the UI
would never render. Revert-detector: with the old ``[^\\]]`` class
the ``[[Bad\\nName]]`` construct resolves to the seeded target and
a NoteLink is created, flipping the count assertion to 1.
A normal single-line ``[[Single Line]]`` in the same body is the
positive control — the tightening must not break real links.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
# Target with an embedded newline in its title — only reachable by
# the (buggy) newline-spanning regex.
self._note(db_session, st, "Bad\nName", "x", "nl-target")
# Normal single-line target (positive control).
single = self._note(db_session, st, "Single Line", "y", "nl-single")
source = self._note(db_session, st, "NL Source", "z", "nl-source")
svc = NoteService("test_user")
resolved = svc._parse_and_update_links_in_session(
db_session,
source.id,
"Valid [[Single Line]] then unclosed [[Bad\nName]] across lines.",
)
db_session.commit()
links = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.all()
)
# Exactly one link — to the single-line target, never the
# newline-spanning one.
assert len(links) == 1
assert links[0].target_document_id == single.id
assert [r["target_id"] for r in resolved] == [single.id]
def test_parse_links_caps_notelink_rows_at_max_links_per_note(
self, patched_session, note_source_type, monkeypatch
):
"""``_parse_and_update_links_in_session`` must truncate the match
list to ``MAX_LINKS_PER_NOTE`` (note_service.py:1960-1968) so a note
with thousands of [[links]] doesn't resolve/insert every one on each
save. A target appearing only beyond the cap must NOT get a row.
"""
from local_deep_research.research_library.notes.services import (
note_service as ns_module,
)
from local_deep_research.research_library.notes.services.note_service import (
LINK_PATTERN,
NoteService,
)
monkeypatch.setattr(ns_module, "MAX_LINKS_PER_NOTE", 3)
st = note_source_type.id
source = self._note(patched_session, st, "Cap Source", "y", "cap-src")
targets = {
name: self._note(patched_session, st, name, "body", f"cap-{name}")
for name in ("T1", "T2", "T3", "T4")
}
# First three resolvable matches are T1/T2/T3; T4 sits past the cap,
# padded with many unresolvable ghosts so total matches >> 3.
ghosts = " ".join(f"[[ghost-{n}]]" for n in range(30))
content = f"[[T1]] [[T2]] [[T3]] {ghosts} [[T4]]"
assert len(LINK_PATTERN.findall(content)) > 3
svc = NoteService("test_user")
svc._parse_and_update_links_in_session(
patched_session, source.id, content
)
patched_session.commit()
assert (
patched_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.count()
== 3
)
# T4 appeared only beyond the cap → no row for it.
assert (
patched_session.query(NoteLink)
.filter_by(
source_document_id=source.id,
target_document_id=targets["T4"].id,
)
.first()
is None
)
# -------------------------------------------------------------------
# collection_ids: count cap + dedup + bogus-id skip (create_note)
# -------------------------------------------------------------------
def test_create_note_rejects_too_many_collection_ids(self):
"""Pre-session count cap (note_service.py:569-572): more than
``MAX_COLLECTIONS_PER_NOTE`` collection ids must raise before any DB
session opens, so no fixture is needed.
"""
from local_deep_research.research_library.notes.services.note_service import (
MAX_COLLECTIONS_PER_NOTE,
NoteService,
)
svc = NoteService("test_user")
collection_ids = [
str(uuid.uuid4()) for _ in range(MAX_COLLECTIONS_PER_NOTE + 1)
]
with pytest.raises(ValueError, match="too many collection_ids"):
svc.create_note(
title="t", content="c", collection_ids=collection_ids
)
def test_create_note_skips_bogus_collection_ids_and_dedupes_duplicates(
self, patched_session, note_source_type
):
"""create_note must de-dupe repeated collection ids (avoiding the
(document_id, collection_id) UNIQUE) and skip nonexistent ids
(avoiding the FK violation) — note_service.py:620,623-628. Either
regression flips create_note from returning an id to raising
IntegrityError.
"""
from local_deep_research.database.models import Collection
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
svc = NoteService("test_user")
real_coll_id = str(uuid.uuid4())
patched_session.add(
Collection(
id=real_coll_id,
name="Proj",
collection_type="user_collection",
)
)
patched_session.commit()
bogus_id = str(uuid.uuid4())
note_id = svc.create_note(
title="t",
content="c",
collection_ids=[real_coll_id, real_coll_id, bogus_id],
)
assert note_id is not None # no IntegrityError
rows = (
patched_session.query(DocumentCollection)
.filter_by(document_id=note_id)
.all()
)
coll_ids = {r.collection_id for r in rows}
# Exactly two memberships: the auto-created Notes collection + the one
# real (de-duped) collection. The bogus id is absent.
assert len(rows) == 2
assert real_coll_id in coll_ids
assert bogus_id not in coll_ids
assert (
patched_session.query(DocumentCollection)
.filter_by(collection_id=bogus_id)
.count()
== 0
)
# -------------------------------------------------------------------
# Strategy-2 (prefix) resolution tie-break by created_at
# -------------------------------------------------------------------
def test_resolve_link_internal_prefix_breaks_ties_by_created_at(
self, db_session, note_source_type
):
"""Two notes whose titles both start with 'Project' (and no note
titled exactly 'Project') force Strategy 2 (startswith prefix). Its
``order_by(created_at.asc(), id.asc())`` (note_service.py:2170) must
pick the OLDER note deterministically. The existing tie-break test
only exercises Strategy 1's order_by (exact match), so a regression
confined to the Strategy-2 order_by would pass the whole suite while
making prefix-resolved links retarget non-deterministically.
"""
from datetime import datetime, timedelta, timezone
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
alpha_id = "00000000-0000-0000-0000-000000000003"
beta_id = "00000000-0000-0000-0000-000000000004"
base = datetime.now(timezone.utc) - timedelta(days=2)
alpha = Document(
id=alpha_id,
title="Project Alpha",
text_content="older",
file_type="note",
file_size=0,
document_hash=_generate_hash("prefix_alpha"),
source_type_id=note_source_type.id,
created_at=base,
)
beta = Document(
id=beta_id,
title="Project Beta",
text_content="newer",
file_type="note",
file_size=0,
document_hash=_generate_hash("prefix_beta"),
source_type_id=note_source_type.id,
created_at=base + timedelta(hours=5),
)
db_session.add_all([beta, alpha]) # insert order != age order
db_session.commit()
service = NoteService("test_user")
result = service._resolve_link_internal(db_session, "Project")
assert result is not None
assert result["id"] == alpha_id, (
"prefix resolution should pick the older note"
)
assert result["title"] == "Project Alpha"
# -------------------------------------------------------------------
# delete_note RAG cleanup is best-effort (PR #4512)
# -------------------------------------------------------------------
def test_delete_note_commits_db_delete_even_when_rag_cleanup_raises(
self, patched_session, note_source_type, monkeypatch
):
"""When the note is indexed and the RAG chunk-purge raises,
``delete_note`` must still return True with the Document (and its
cascade-deleted NoteVersion rows) gone. Moving the cleanup out of
its try/except, or committing AFTER the RAG cleanup, would propagate
the FAISS error — the FAISS-outage bug class (PR #4512). (Cleanup
goes through ``purge_document_chunks``, not
``remove_document_from_rag``, since the join row is already gone.)
"""
from local_deep_research.database.models import Collection
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note = self._note(
patched_session,
note_source_type.id,
"Indexed",
"body",
"del-rag-fail",
)
collection = Collection(
id=str(uuid.uuid4()), name="Notes", collection_type="notes"
)
patched_session.add(collection)
patched_session.add(
DocumentCollection(
document_id=note.id,
collection_id=collection.id,
indexed=True,
)
)
patched_session.add(
NoteVersion(
id=str(uuid.uuid4()),
document_id=note.id,
title="Indexed",
content="body",
change_type="initial",
content_hash=_generate_hash("v-del-rag-fail"),
)
)
patched_session.commit()
calls = {"removed": 0, "vectors": 0}
class FakeRAG:
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def purge_document_vectors(self, note_id, collection_id):
calls["vectors"] += 1
return 0
def purge_document_chunks(self, note_id, collection_id):
calls["removed"] += 1
raise RuntimeError("FAISS down")
# delete_note now builds the service via the factory (per-collection
# embedding settings), so patch get_rag_service, not the class.
monkeypatch.setattr(
"local_deep_research.research_library.services.rag_service_factory.get_rag_service",
lambda *a, **k: FakeRAG(),
)
result = NoteService("test_user").delete_note(note.id)
assert result is True
assert calls["removed"] == 1 # the indexed+failing branch ran
assert calls["vectors"] == 1 # vectors purged before chunk rows
patched_session.expire_all()
assert (
patched_session.query(Document).filter_by(id=note.id).first()
is None
)
assert (
patched_session.query(NoteVersion)
.filter_by(document_id=note.id)
.count()
== 0
)
def test_non_indexed_note_delete_never_constructs_rag_service(
self, patched_session, note_source_type, monkeypatch
):
"""A note with no indexed DocumentCollection must short-circuit the
``if indexed_collection_ids:`` guard (note_service.py:963) so
LibraryRAGService is never imported/constructed — no wasteful FAISS
/ DB-password work and no crash surface for never-embedded notes.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note = self._note(
patched_session, note_source_type.id, "Bare", "body", "del-no-rag"
)
patched_session.commit()
constructed = []
def _fake_factory(*a, **k):
constructed.append(1)
raise AssertionError(
"get_rag_service must not be called for a non-indexed note"
)
monkeypatch.setattr(
"local_deep_research.research_library.services.rag_service_factory.get_rag_service",
_fake_factory,
)
result = NoteService("test_user").delete_note(note.id)
assert result is True
assert constructed == [] # guard short-circuited construction
patched_session.expire_all()
assert (
patched_session.query(Document).filter_by(id=note.id).first()
is None
)
def test_remove_from_collection_purges_rag_when_indexed(
self, patched_session, note_source_type, monkeypatch
):
"""Removing a note from a collection where it was indexed must purge
that collection's RAG vectors + chunk rows (vectors first), mirroring
delete_note. Without it the removed note stays searchable in the
collection forever, and — once the join row is gone — no later
delete_note can reach those entries either."""
from local_deep_research.database.models import Collection
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note = self._note(
patched_session, note_source_type.id, "N", "body", "rmc-idx"
)
collection = Collection(
id=str(uuid.uuid4()), name="Reading", collection_type="user"
)
patched_session.add(collection)
patched_session.add(
DocumentCollection(
document_id=note.id,
collection_id=collection.id,
indexed=True,
)
)
patched_session.commit()
order = []
class FakeRAG:
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def purge_document_vectors(self, note_id, collection_id):
order.append(("vectors", note_id, collection_id))
return 0
def purge_document_chunks(self, note_id, collection_id):
order.append(("chunks", note_id, collection_id))
return 0
monkeypatch.setattr(
"local_deep_research.research_library.services.rag_service_factory.get_rag_service",
lambda *a, **k: FakeRAG(),
)
result = NoteService("test_user").remove_from_collection(
note.id, collection.id
)
assert result is True
# Vectors purged before chunk rows, both scoped to (note, collection).
assert order == [
("vectors", note.id, collection.id),
("chunks", note.id, collection.id),
]
patched_session.expire_all()
assert (
patched_session.query(DocumentCollection)
.filter_by(document_id=note.id, collection_id=collection.id)
.first()
is None
)
def test_remove_from_collection_skips_rag_when_not_indexed(
self, patched_session, note_source_type, monkeypatch
):
"""A note that was never indexed in the collection must not construct
a RAG service on removal (no wasteful FAISS/DB-password work)."""
from local_deep_research.database.models import Collection
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
note = self._note(
patched_session, note_source_type.id, "N2", "body", "rmc-noidx"
)
collection = Collection(
id=str(uuid.uuid4()), name="Reading2", collection_type="user"
)
patched_session.add(collection)
patched_session.add(
DocumentCollection(
document_id=note.id,
collection_id=collection.id,
indexed=False,
)
)
patched_session.commit()
def _fake_factory(*a, **k):
raise AssertionError(
"get_rag_service must not be called when the note was not "
"indexed in the collection"
)
monkeypatch.setattr(
"local_deep_research.research_library.services.rag_service_factory.get_rag_service",
_fake_factory,
)
assert (
NoteService("test_user").remove_from_collection(
note.id, collection.id
)
is True
)
# -------------------------------------------------------------------
# get_unlinked_mentions escapes LIKE wildcards in the title
# -------------------------------------------------------------------
def test_get_unlinked_mentions_escapes_like_wildcards_in_title(
self, patched_session, note_source_type
):
"""``get_unlinked_mentions`` must escape LIKE wildcards in the note
title via ``_escape_like`` + ``escape='\\\\'`` (note_service.py:2333,
2346) — an independent call site from _resolve_link_internal. A '%'
or '_' in the title must match literally, not as a wildcard, or the
unlinked-mentions panel floods with false positives (leaking content
previews of unrelated notes).
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
# '%' wildcard case.
target = self._note(
patched_session, st, "50% Off Coupons", "about deals", "wc-target"
)
# Would match if '%' were a wildcard ('50<anything>Off Coupons'),
# but must NOT match the escaped literal.
decoy = self._note(
patched_session,
st,
"Unrelated",
"50 something Off Coupons here",
"wc-decoy",
)
# Contains the literal title → genuine mention.
real = self._note(
patched_session,
st,
"Sales Page",
"See 50% Off Coupons for the list.",
"wc-real",
)
mentions = NoteService("test_user").get_unlinked_mentions(target.id)
ids = {m["id"] for m in mentions}
assert real.id in ids # literal substring matched
assert decoy.id not in ids # '%' escaped to a literal, not a wildcard
assert target.id not in ids # never self
# -------------------------------------------------------------------
# _get_note_in_session: the centralized fetch+_is_note guard
# (half-copied as a bug on the collection / research helpers)
# -------------------------------------------------------------------
def test_collection_guard_half_copy_regression(
self, patched_session, note_source_type
):
"""add_to_collection / remove_from_collection / link_research_to_note
must refuse a real same-DB NON-note Document.
This pins the ``_is_note`` half of the guard that
``_get_note_in_session`` centralizes — the exact check the
collection helpers ORIGINALLY omitted (the half-copy bug), which let
a non-note Document (e.g. an uploaded PDF) acquire note-collection /
note-research rows. The existing
``test_add_to_collection_rejects_non_note`` only hits the *not-found*
branch (a nonexistent id); this hits the *same-DB non-note* branch,
and adds ``link_research_to_note`` so all three guarded call sites
are covered against a revert of the centralization.
"""
from datetime import datetime, timezone
from local_deep_research.database.models import (
DocumentCollection,
NoteResearch,
ResearchHistory,
SourceType,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# A real Document that is NOT a note (different source_type).
pdf_source = SourceType(
id=str(uuid.uuid4()),
name="document",
display_name="Document",
)
patched_session.add(pdf_source)
pdf_id = str(uuid.uuid4())
patched_session.add(
Document(
id=pdf_id,
title="Some PDF",
text_content="pdf body",
file_type="pdf",
file_size=10,
document_hash=_generate_hash("guard_half_copy_pdf"),
source_type_id=pdf_source.id,
)
)
research_id = str(uuid.uuid4())
patched_session.add(
ResearchHistory(
id=research_id,
query="q",
mode="quick",
status="completed",
created_at=datetime.now(timezone.utc).isoformat(),
)
)
patched_session.commit()
svc = NoteService("test_user")
# (1) add_to_collection refuses the non-note.
assert svc.add_to_collection(pdf_id, "any-collection") is False
# (2) remove_from_collection refuses the non-note.
assert svc.remove_from_collection(pdf_id, "any-collection") is False
# (3) link_research_to_note refuses the non-note (raises, matching the
# guarded site's bail tail).
with pytest.raises(ValueError, match="not found"):
svc.link_research_to_note(pdf_id, research_id)
# No note-side rows attached to the PDF.
assert (
patched_session.query(DocumentCollection)
.filter_by(document_id=pdf_id)
.count()
== 0
)
assert (
patched_session.query(NoteResearch)
.filter_by(document_id=pdf_id)
.count()
== 0
)
# -------------------------------------------------------------------
# _truncate_preview boundary: exactly preview_len → no ellipsis;
# preview_len + 1 → truncated to preview_len + "..."
# -------------------------------------------------------------------
def test_get_backlinks_preview_boundary(
self, patched_session, note_source_type, monkeypatch
):
"""A backlink source body of exactly ``preview_len`` chars is
returned WITHOUT an ellipsis; one char longer is truncated to
``preview_len`` + "...". Pins the SUBSTR(+1) sentinel logic the
``_truncate_preview`` helper centralizes.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
# Shrink the cap so the test body stays tiny.
preview_len = 10
monkeypatch.setattr(
NoteService, "LINK_CONTENT_PREVIEW_CHARS", preview_len
)
st = note_source_type.id
target = self._note(patched_session, st, "Target", "tbody", "bl-tgt")
# Source 1: body exactly preview_len chars → no ellipsis.
exact_src = self._note(
patched_session, st, "Exact", "a" * preview_len, "bl-exact"
)
# Source 2: body preview_len + 1 chars → truncated + ellipsis.
over_src = self._note(
patched_session, st, "Over", "b" * (preview_len + 1), "bl-over"
)
for src in (exact_src, over_src):
patched_session.add(
NoteLink(
source_document_id=src.id,
target_document_id=target.id,
link_text="Target",
)
)
patched_session.commit()
backlinks = NoteService("test_user").get_backlinks(target.id)
previews = {b["id"]: b["content_preview"] for b in backlinks}
assert previews[exact_src.id] == "a" * preview_len # no ellipsis
assert previews[over_src.id] == "b" * preview_len + "..."
def test_get_outgoing_links_preview_boundary(
self, patched_session, note_source_type, monkeypatch
):
"""Outgoing-link target body of exactly ``preview_len`` chars → no
ellipsis; one longer → truncated to ``preview_len`` + "...".
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
preview_len = 10
monkeypatch.setattr(
NoteService, "LINK_CONTENT_PREVIEW_CHARS", preview_len
)
st = note_source_type.id
source = self._note(patched_session, st, "Src", "sbody", "ol-src")
exact_tgt = self._note(
patched_session, st, "Exact", "a" * preview_len, "ol-exact"
)
over_tgt = self._note(
patched_session, st, "Over", "b" * (preview_len + 1), "ol-over"
)
for tgt in (exact_tgt, over_tgt):
patched_session.add(
NoteLink(
source_document_id=source.id,
target_document_id=tgt.id,
link_text=tgt.title,
)
)
patched_session.commit()
outgoing = NoteService("test_user").get_outgoing_links(source.id)
previews = {o["id"]: o["content_preview"] for o in outgoing}
assert previews[exact_tgt.id] == "a" * preview_len # no ellipsis
assert previews[over_tgt.id] == "b" * preview_len + "..."
def test_get_unlinked_mentions_preview_boundary(
self, patched_session, note_source_type, monkeypatch
):
"""Unlinked-mention body of exactly ``preview_len`` chars → no
ellipsis; one longer → truncated to ``preview_len`` + "...".
Both mentioning bodies must still contain the target title so the
ILIKE filter matches; the title is short enough to fit inside
``preview_len`` (after which the boundary char does the work).
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
preview_len = 10
monkeypatch.setattr(
NoteService, "LINK_CONTENT_PREVIEW_CHARS", preview_len
)
st = note_source_type.id
# Title "Foo" (>= MIN_MENTION_TITLE_LEN) and 3 chars so it fits.
target = self._note(patched_session, st, "Foo", "target body", "um-tgt")
# Mentioner with body exactly preview_len chars containing "Foo".
exact_body = "Foo" + "x" * (preview_len - 3)
assert len(exact_body) == preview_len
exact_m = self._note(patched_session, st, "M-exact", exact_body, "um-e")
# Mentioner with body preview_len + 1 chars containing "Foo".
over_body = "Foo" + "y" * (preview_len + 1 - 3)
assert len(over_body) == preview_len + 1
over_m = self._note(patched_session, st, "M-over", over_body, "um-o")
patched_session.commit()
mentions = NoteService("test_user").get_unlinked_mentions(target.id)
previews = {m["id"]: m["content_preview"] for m in mentions}
assert previews[exact_m.id] == exact_body # exactly preview_len → none
assert previews[over_m.id] == over_body[:preview_len] + "..."
# -------------------------------------------------------------------
# Aliased wiki-link stores the TARGET (pre-pipe) text, not the alias
# -------------------------------------------------------------------
def test_aliased_wiki_link_stores_target_text_not_alias(
self, db_session, note_source_type
):
"""``[[Target|Alias]]`` must store the TARGET (pre-pipe) text in
``NoteLink.link_text`` — not the full aliased string, and not the
display alias.
The display half is presentation-only; resolution, the rename-safety
cache key, and the frontend linkMap all key off the target, so the
stored ``link_text`` must be the pre-pipe portion. Existing
multi-mention tests use aliased syntax but only assert dedup/target_id
— none pins the stored ``link_text`` value, so this path is otherwise
untested.
"""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
st = note_source_type.id
target = self._note(
db_session, st, "Target Note", "target body", "alias-tgt"
)
source = self._note(db_session, st, "Source", "src body", "alias-src")
svc = NoteService("test_user")
resolved = svc._parse_and_update_links_in_session(
db_session, source.id, "see [[Target Note|click here]] now"
)
db_session.commit()
link = (
db_session.query(NoteLink)
.filter_by(source_document_id=source.id)
.one()
)
assert link.target_document_id == target.id
# Stored link_text is the TARGET, not the alias and not "Target|Alias".
assert link.link_text == "Target Note"
assert "|" not in link.link_text
assert link.link_text != "click here"
# The resolved payload mirrors the stored target text.
assert resolved[0]["link_text"] == "Target Note"
class TestCountNotesFilters:
"""count_notes shares list_notes' filter builder; verify its filters
directly (count is the pagination building block the list endpoint uses)."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
from contextlib import contextmanager
@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
def test_count_honors_search_and_pinned_filters(
self, patched_session, note_source_type
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
_make_note_doc(
patched_session,
note_source_type.id,
"Alpha One",
"a1 body",
"cnt-a1",
)
_make_note_doc(
patched_session,
note_source_type.id,
"Alpha Two",
"a2 body",
"cnt-a2",
)
pinned = _make_note_doc(
patched_session, note_source_type.id, "Beta", "b body", "cnt-b"
)
pinned.favorite = True
patched_session.commit()
assert service.count_notes() == 3
assert service.count_notes(search="Alpha") == 2 # title match
assert service.count_notes(pinned_only=True) == 1
assert service.count_notes(search="nope") == 0
def test_get_collection_id_happy_and_error_paths(
self, patched_session, note_source_type, monkeypatch
):
"""get_notes_collection_id returns the (created) Notes collection id,
and resolves a failure in the get-or-create to None — the wrapper's
defensive guard — rather than raising."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
assert service.get_notes_collection_id() # a real id
def _boom(self, session):
raise RuntimeError("boom")
monkeypatch.setattr(
NoteService, "_get_or_create_notes_collection", _boom
)
assert service.get_notes_collection_id() is None
class TestLinkLookupFailurePropagation:
"""resolve_link and search_notes_for_linking must propagate
infrastructure failures instead of collapsing them into the same
None / [] their benign not-found paths return — the routes map those
to a 404 / an empty 200, which would present a DB outage as the note
not existing (same rationale as accept_suggested_link's C3 fix and
note_ai_service's semantic_search)."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
from contextlib import contextmanager
@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
def test_resolve_link_propagates_infrastructure_failure(
self, patched_session, note_source_type, monkeypatch
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
def _boom(self, session, link_text):
raise RuntimeError("db gone")
monkeypatch.setattr(NoteService, "_resolve_link_internal", _boom)
with pytest.raises(RuntimeError, match="db gone"):
service.resolve_link("Some Note")
def test_resolve_link_still_returns_none_for_unresolvable(
self, patched_session, note_source_type
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
assert service.resolve_link("No Such Note Anywhere") is None
def test_search_notes_for_linking_propagates_infrastructure_failure(
self, patched_session, note_source_type, monkeypatch
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
def _boom(self, session):
raise RuntimeError("db gone")
monkeypatch.setattr(NoteService, "_get_note_source_type_id", _boom)
with pytest.raises(RuntimeError, match="db gone"):
service.search_notes_for_linking("query")
def test_search_notes_for_linking_still_returns_empty_for_no_match(
self, patched_session, note_source_type
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
assert service.search_notes_for_linking("zzz-no-match") == []
class TestNotesCollectionInitDurability:
"""V1a/V1b: the Notes-collection get-or-create must COMMIT inside the
per-user init lock (library_init pattern). Flush-only left the row
(a) invisible to concurrent sessions until after the lock was
released — a duplicate-collection race — and (b) subject to the
request-teardown rollback on read-only routes like ask-context,
which returned a collection UUID that never persisted."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
from contextlib import contextmanager
@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
def test_collection_survives_post_request_rollback(
self, patched_session, note_source_type
):
"""Simulates the app_factory teardown: a read-only route resolves
the collection id, then the shared session is rolled back. The
returned id must reference a row that still exists."""
from local_deep_research.database.models import Collection
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
collection_id = service.get_notes_collection_id()
assert collection_id
# The teardown_appcontext handler rolls back the shared session.
patched_session.rollback()
row = patched_session.get(Collection, collection_id)
assert row is not None, (
"Notes collection was discarded by the post-request rollback — "
"the get-or-create must commit, not just flush"
)
assert row.collection_type == "notes"
def test_get_or_create_is_idempotent_and_stable(
self, patched_session, note_source_type
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
first = service.get_notes_collection_id()
second = service.get_notes_collection_id()
assert first == second
class TestResearchNotesReverseLookup:
"""get_notes_for_research / create_note_for_research: the research →
notes direction that powers the results-page Notes panel. Pre-fix the
linkage was one-way — NoteResearch existed but nothing surfaced a
run's notes from the run itself."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
from contextlib import contextmanager
@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 research_row(self, patched_session):
from datetime import datetime, timezone
from local_deep_research.database.models import ResearchHistory
rid = str(uuid.uuid4())
patched_session.add(
ResearchHistory(
id=rid,
query="What is chunk dedup?",
mode="quick",
status="completed",
created_at=datetime.now(timezone.utc).isoformat(),
)
)
patched_session.commit()
return rid
def test_unknown_research_returns_none(self, patched_session):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
assert NoteService("test_user").get_notes_for_research("nope") is None
def test_lists_only_linked_notes_with_previews(
self, patched_session, note_source_type, research_row
):
from local_deep_research.database.models import NoteResearch
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
linked = _make_note_doc(
patched_session,
note_source_type.id,
"Linked note",
"x" * 400,
"rev-linked",
)
_make_note_doc(
patched_session,
note_source_type.id,
"Unlinked note",
"other body",
"rev-unlinked",
)
patched_session.add(
NoteResearch(
document_id=linked.id,
research_id=research_row,
display_order=0,
)
)
patched_session.commit()
notes = NoteService("test_user").get_notes_for_research(research_row)
assert [n["title"] for n in notes] == ["Linked note"]
# Preview is capped + marked (via the shared _truncate_preview, which
# appends "..."), not the full 400-char body.
assert notes[0]["content_preview"].endswith("...")
assert len(notes[0]["content_preview"]) <= 303
assert notes[0]["id"] == linked.id
assert notes[0]["linked_at"] is not None
def test_create_note_for_research_defaults_and_links(
self, patched_session, note_source_type, research_row
):
from local_deep_research.database.models import Document, NoteResearch
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
note_id = service.create_note_for_research(research_row)
note = patched_session.query(Document).filter_by(id=note_id).one()
assert note.title == "Notes on: What is chunk dedup?"
assert f"/results/{research_row}" in note.text_content
link = (
patched_session.query(NoteResearch)
.filter_by(document_id=note_id, research_id=research_row)
.one()
)
assert link is not None
def test_create_note_for_research_unknown_research_raises_lookup(
self, patched_session, note_source_type
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
with pytest.raises(LookupError):
NoteService("test_user").create_note_for_research("nope")
def test_create_note_for_research_cleans_up_on_link_failure(
self, patched_session, note_source_type, research_row, monkeypatch
):
"""All-or-nothing: if the link step fails, the just-created note
must not remain as an orphan the caller never learns about."""
from local_deep_research.database.models import Document
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
def _boom(self, note_id, research_id):
raise RuntimeError("link exploded")
monkeypatch.setattr(NoteService, "link_research_to_note", _boom)
with pytest.raises(RuntimeError, match="link exploded"):
service.create_note_for_research(research_row)
leftovers = (
patched_session.query(Document)
.filter(Document.title.like("Notes on:%"))
.count()
)
assert leftovers == 0, "orphan note left behind after link failure"
class TestNoteReferencesService:
"""NoteReference-backed methods: document-linked notes and quote
anchors for both target kinds (the reference layer)."""
@pytest.fixture
def patched_session(self, db_session, monkeypatch):
from contextlib import contextmanager
@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 library_doc(self, patched_session):
from local_deep_research.database.models import Document, SourceType
st = SourceType(
id=str(uuid.uuid4()), name="user_upload", display_name="Upload"
)
patched_session.add(st)
doc = Document(
id=str(uuid.uuid4()),
title="A Paper",
text_content="paper body " * 50,
file_type="pdf",
file_size=10,
document_hash=_generate_hash("ref-doc"),
source_type_id=st.id,
)
patched_session.add(doc)
patched_session.commit()
return doc
def test_create_note_for_document_links_and_defaults(
self, patched_session, note_source_type, library_doc
):
from local_deep_research.database.models import (
Document,
NoteReference,
)
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
note_id = service.create_note_for_document(library_doc.id)
note = patched_session.query(Document).filter_by(id=note_id).one()
assert note.title == "Notes on: A Paper"
ref = (
patched_session.query(NoteReference)
.filter_by(note_id=note_id, target_document_id=library_doc.id)
.one()
)
assert ref.quote is None # plain link, not an annotation
notes = service.get_notes_for_document(library_doc.id)
assert [n["id"] for n in notes] == [note_id]
def test_create_note_for_document_rejects_note_targets(
self, patched_session, note_source_type, library_doc
):
"""Annotating a NOTE is unsupported — mutable content means quote
anchors would drift on every edit."""
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
target_note = _make_note_doc(
patched_session, note_source_type.id, "T", "b", "ref-note-t"
)
with pytest.raises(ValueError, match="cannot be annotated"):
service.create_note_for_document(target_note.id)
def test_annotation_roundtrip_for_document_target(
self, patched_session, note_source_type, library_doc
):
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
service = NoteService("test_user")
note_id = service.create_note_for_document(
library_doc.id,
title="Comment: check this",
content="check this\n\n> paper body\n",
quote="paper body",
prefix="pre",
suffix="post",
)
anns = service.get_annotations_for_target(document_id=library_doc.id)
assert len(anns) == 1
assert anns[0]["note_id"] == note_id
assert anns[0]["quote"] == "paper body"
assert anns[0]["note_title"] == "Comment: check this"
assert service.has_annotation(note_id, document_id=library_doc.id)
# Deleting the note cascades the anchor.
service.delete_note(note_id)
assert (
service.get_annotations_for_target(document_id=library_doc.id) == []
)
def test_research_annotation_via_create_note_for_research(
self, patched_session, note_source_type
):
from datetime import datetime, timezone
from local_deep_research.database.models import ResearchHistory
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
rid = str(uuid.uuid4())
patched_session.add(
ResearchHistory(
id=rid,
query="q",
mode="quick",
status="completed",
created_at=datetime.now(timezone.utc).isoformat(),
)
)
patched_session.commit()
service = NoteService("test_user")
note_id = service.create_note_for_research(
rid,
title="Comment: hm",
content="hm\n\n> a passage\n",
quote="a passage",
)
anns = service.get_annotations_for_target(research_id=rid)
assert [a["note_id"] for a in anns] == [note_id]
assert service.has_annotation(note_id, research_id=rid)
# Anchor-less linked notes are NOT annotations.
plain = service.create_note_for_research(rid)
assert not service.has_annotation(plain, research_id=rid)
class TestMarkdownLabelEscaping:
"""escape_markdown_link_label: untrusted text (research queries,
externally-sourced document titles) must not break out of a
[label](url) provenance link."""
def test_escapes_link_structural_metacharacters(self):
from local_deep_research.research_library.notes.services.note_service import (
escape_markdown_link_label,
)
out = escape_markdown_link_label("evil](https://attacker/x)")
assert "\\]" in out and "\\(" in out and "\\)" in out
# No un-escaped `](` remains, so the label can't be terminated early
# to re-target the link.
assert "](" not in out
def test_neutralizes_newlines_and_backslashes(self):
from local_deep_research.research_library.notes.services.note_service import (
escape_markdown_link_label,
)
out = escape_markdown_link_label("a\nb\\c")
assert "\n" not in out
assert out.startswith("a b") or "a b" in out
assert "\\\\" in out # backslash doubled
def test_create_note_for_document_escapes_title_in_provenance(
self, db_session, note_source_type, monkeypatch
):
from contextlib import contextmanager
from local_deep_research.database.models import Document, SourceType
from local_deep_research.research_library.notes.services.note_service import (
NoteService,
)
@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,
)
st = SourceType(
id=str(uuid.uuid4()), name="user_upload", display_name="Upload"
)
db_session.add(st)
evil = "Paper](https://attacker.example/x"
doc = Document(
id=str(uuid.uuid4()),
title=evil,
text_content="body",
file_type="pdf",
file_size=4,
document_hash=_generate_hash("evil-title"),
source_type_id=st.id,
)
db_session.add(doc)
db_session.commit()
note_id = NoteService("test_user").create_note_for_document(doc.id)
note = db_session.query(Document).filter_by(id=note_id).one()
# The provenance link's label is escaped, so the attacker URL can't
# become the link target.
assert "](https://attacker.example" not in note.text_content
assert f"/library/document/{doc.id}" in note.text_content