Skip to content

~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.

33.6 KB
Raw Blame
"""Tests for Note CRUD operations at the database/model level."""

import uuid

import pytest

from local_deep_research.database.models import (
    Document,
    Collection,
    DocumentCollection,
    NoteLink,
    NoteResearch,
    NoteSynthesis,
    NoteSynthesisSource,
    NoteVersion,
    ResearchHistory,
)
from local_deep_research.research_library.notes.services.note_service import (
    NoteService,
)

from tests.notes.helpers import _generate_hash


def _create_note(
    session, source_type_id, title, content, tags=None, pinned=False
):
    """Helper to create a note document."""
    note_id = str(uuid.uuid4())
    note = Document(
        id=note_id,
        title=title,
        text_content=content,
        file_type="note",
        file_size=len(content),
        source_type_id=source_type_id,
        document_hash=_generate_hash(f"{note_id}:{content}"),
        tags=tags or [],
        favorite=pinned,
    )

    session.add(note)
    session.commit()
    return note


class TestNoteCRUDOperations:
    """Test suite for Note CRUD operations at database level."""

    @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

    # =========================================================================
    # Create Note Tests
    # =========================================================================

    def test_create_note_basic(self, db_session, note_source_type):
        """Test creating a basic note."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Test Note",
            "This is test content.",
        )

        assert note.id is not None
        assert len(note.id) == 36  # UUID format
        assert note.title == "Test Note"
        assert note.text_content == "This is test content."
        assert note.source_type_id == note_source_type.id

    def test_create_note_with_tags(self, db_session, note_source_type):
        """Test creating a note with tags."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Tagged Note",
            "Content with tags.",
            tags=["python", "testing", "notes"],
        )

        assert note.tags is not None
        assert "python" in note.tags
        assert len(note.tags) == 3

    def test_create_note_pinned(self, db_session, note_source_type):
        """Test creating a pinned note."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Pinned Note",
            "Important content.",
            pinned=True,
        )

        assert note.favorite is True

    def test_create_note_generates_hash(self, db_session, note_source_type):
        """Test that creating a note generates a document hash."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Hashed Note",
            "Content for hashing.",
        )

        assert note.document_hash is not None
        assert len(note.document_hash) == 64  # SHA-256 hex length

    def test_create_note_timestamps(self, db_session, note_source_type):
        """Test that creating a note sets timestamps."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Timestamped Note",
            "Content.",
        )

        assert note.created_at is not None
        assert note.updated_at is not None

    def test_create_multiple_notes(self, db_session, note_source_type):
        """Test creating multiple notes."""
        for i in range(5):
            _create_note(
                db_session,
                note_source_type.id,
                f"Note {i}",
                f"Content {i}",
            )

        count = (
            db_session.query(Document)
            .filter_by(source_type_id=note_source_type.id)
            .count()
        )
        assert count == 5

    # =========================================================================
    # Read Note Tests
    # =========================================================================

    def test_read_note_by_id(self, db_session, note_source_type):
        """Test reading a note by ID."""
        created = _create_note(
            db_session,
            note_source_type.id,
            "Readable Note",
            "Content.",
        )

        found = db_session.query(Document).filter_by(id=created.id).first()
        assert found is not None
        assert found.title == "Readable Note"

    def test_read_note_not_found(self, db_session, note_source_type):
        """Test reading a non-existent note."""
        found = (
            db_session.query(Document).filter_by(id="nonexistent-id").first()
        )
        assert found is None

    def test_read_note_with_tags(self, db_session, note_source_type):
        """Test reading a note includes tags."""
        created = _create_note(
            db_session,
            note_source_type.id,
            "Tagged Note",
            "Content.",
            tags=["tag1", "tag2"],
        )

        found = db_session.query(Document).filter_by(id=created.id).first()
        assert found.tags == ["tag1", "tag2"]

    # =========================================================================
    # Update Note Tests
    # =========================================================================

    def test_update_note_title(self, db_session, note_source_type):
        """Test updating a note's title."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Original Title",
            "Content.",
        )

        note.title = "Updated Title"
        db_session.commit()

        refreshed = db_session.query(Document).filter_by(id=note.id).first()
        assert refreshed.title == "Updated Title"

    def test_update_note_content(self, db_session, note_source_type):
        """Test updating a note's content."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Title",
            "Original content.",
        )

        note.text_content = "Updated content."
        db_session.commit()

        refreshed = db_session.query(Document).filter_by(id=note.id).first()
        assert refreshed.text_content == "Updated content."

    def test_update_note_tags(self, db_session, note_source_type):
        """Test updating a note's tags."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Title",
            "Content.",
            tags=["old_tag"],
        )

        note.tags = ["new_tag1", "new_tag2"]
        db_session.commit()

        refreshed = db_session.query(Document).filter_by(id=note.id).first()
        assert refreshed.tags == ["new_tag1", "new_tag2"]

    def test_update_note_pinned(self, db_session, note_source_type):
        """Test updating a note's pinned status."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Title",
            "Content.",
            pinned=False,
        )

        note.favorite = True
        db_session.commit()

        refreshed = db_session.query(Document).filter_by(id=note.id).first()
        assert refreshed.favorite is True

    # =========================================================================
    # Delete Note Tests
    # =========================================================================

    def test_delete_note(self, db_session, note_source_type):
        """Test deleting a note."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "To Delete",
            "Will be deleted.",
        )
        note_id = note.id

        db_session.delete(note)
        db_session.commit()

        found = db_session.query(Document).filter_by(id=note_id).first()
        assert found is None

    def test_delete_note_cascade_versions(self, db_session, note_source_type):
        """Test that deleting a note cascades to versions."""
        note = _create_note(
            db_session,
            note_source_type.id,
            "Title",
            "Content.",
        )

        # Create a version
        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Title",
            content="Content.",
            change_type="initial",
            content_hash="hash123",
        )
        db_session.add(version)
        db_session.commit()

        # Delete note
        db_session.delete(note)
        db_session.commit()

        # Version should be deleted too
        versions = (
            db_session.query(NoteVersion).filter_by(document_id=note.id).count()
        )
        assert versions == 0

    def test_delete_note_cascade_links(self, db_session, note_source_type):
        """Test that deleting a note cascades to links."""
        note1 = _create_note(
            db_session, note_source_type.id, "Note 1", "Content 1"
        )
        note2 = _create_note(
            db_session, note_source_type.id, "Note 2", "Links to [[Note 1]]"
        )

        # Create a link
        link = NoteLink(
            source_document_id=note2.id,
            target_document_id=note1.id,
            link_text="Note 1",
        )
        db_session.add(link)
        db_session.commit()

        # Delete source note
        db_session.delete(note2)
        db_session.commit()

        # Link should be deleted
        links = (
            db_session.query(NoteLink)
            .filter_by(source_document_id=note2.id)
            .count()
        )
        assert links == 0

    def test_delete_note_service_cascades_versions_links_research_and_nulls_synthesis(
        self, patched_session, note_source_type
    ):
        """Drive NoteService.delete_note (note_service.py:956 relies entirely
        on FK ondelete) and assert it fully cascades every dependent row type:
        versions, BOTH link directions, and note_research (which has zero
        service-path coverage today) — while a NoteSynthesis whose
        result_document_id pointed at the note survives via SET NULL.

        No DocumentCollection rows are seeded, so the LibraryRAGService
        cleanup branch is skipped (no RAG mocking needed). FK pragma is ON
        in conftest, so the DB-level ondelete is genuinely enforced.
        """
        from datetime import datetime, timezone

        target = _create_note(
            patched_session,
            note_source_type.id,
            "Target",
            "has [[Other]] link",
        )
        other = _create_note(
            patched_session, note_source_type.id, "Other", "other body"
        )

        # (1) a version under target.
        patched_session.add(
            NoteVersion(
                id=str(uuid.uuid4()),
                document_id=target.id,
                title="Target",
                content="has [[Other]] link",
                change_type="initial",
                content_hash=_generate_hash("target v1"),
            )
        )
        # (2) outgoing link (target -> Other) and incoming link (Other -> target).
        patched_session.add_all(
            [
                NoteLink(
                    source_document_id=target.id,
                    target_document_id=other.id,
                    link_text="Other",
                ),
                NoteLink(
                    source_document_id=other.id,
                    target_document_id=target.id,
                    link_text="Target",
                ),
            ]
        )
        # (3) research pin on target.
        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.add(
            NoteResearch(
                document_id=target.id,
                research_id=rid,
                display_order=0,
            )
        )
        # (4) a synthesis whose RESULT is target (assert SET NULL survival).
        synthesis = NoteSynthesis(
            result_document_id=target.id,
            synthesis_type="merge",
        )
        patched_session.add(synthesis)
        patched_session.commit()
        patched_session.refresh(synthesis)
        synthesis_id = synthesis.id
        patched_session.add(
            NoteSynthesisSource(
                synthesis_id=synthesis_id,
                source_document_id=other.id,
            )
        )
        patched_session.commit()

        # Action: drive the service delete.
        assert NoteService("testuser").delete_note(target.id) is True
        patched_session.expire_all()

        # CASCADE assertions.
        assert (
            patched_session.query(NoteVersion)
            .filter_by(document_id=target.id)
            .count()
            == 0
        )
        # BOTH link directions cleared.
        assert (
            patched_session.query(NoteLink)
            .filter_by(source_document_id=target.id)
            .count()
            == 0
        )
        assert (
            patched_session.query(NoteLink)
            .filter_by(target_document_id=target.id)
            .count()
            == 0
        )
        # note_research cascade — the genuinely-uncovered one.
        assert (
            patched_session.query(NoteResearch)
            .filter_by(document_id=target.id)
            .count()
            == 0
        )
        assert (
            patched_session.query(Document).filter_by(id=target.id).first()
            is None
        )

        # SET NULL survival via the service path: synthesis persists with a
        # nulled result FK.
        saved = (
            patched_session.query(NoteSynthesis)
            .filter_by(id=synthesis_id)
            .first()
        )
        assert saved is not None
        assert saved.result_document_id is None


class TestNoteListOperations:
    """Test suite for Note list/query operations."""

    def test_list_notes_empty(self, db_session, note_source_type):
        """Test listing notes when none exist."""
        notes = (
            db_session.query(Document)
            .filter_by(source_type_id=note_source_type.id)
            .all()
        )
        assert notes == []

    def test_list_notes_multiple(self, db_session, note_source_type):
        """Test listing multiple notes."""
        for i in range(3):
            _create_note(
                db_session, note_source_type.id, f"Note {i}", f"Content {i}"
            )

        notes = (
            db_session.query(Document)
            .filter_by(source_type_id=note_source_type.id)
            .all()
        )
        assert len(notes) == 3

    def test_list_notes_search_by_title(self, db_session, note_source_type):
        """Test listing notes with title search."""
        _create_note(
            db_session, note_source_type.id, "Python Tutorial", "Learn Python"
        )
        _create_note(
            db_session, note_source_type.id, "JavaScript Guide", "Learn JS"
        )
        _create_note(
            db_session,
            note_source_type.id,
            "Python Advanced",
            "Advanced Python",
        )

        notes = (
            db_session.query(Document)
            .filter(Document.source_type_id == note_source_type.id)
            .filter(Document.title.ilike("%Python%"))
            .all()
        )
        assert len(notes) == 2

    def test_list_notes_search_by_content(self, db_session, note_source_type):
        """Test listing notes with content search."""
        _create_note(
            db_session,
            note_source_type.id,
            "Note 1",
            "machine learning is great",
        )
        _create_note(
            db_session, note_source_type.id, "Note 2", "deep learning rocks"
        )
        _create_note(
            db_session, note_source_type.id, "Note 3", "no keywords here"
        )

        notes = (
            db_session.query(Document)
            .filter(Document.source_type_id == note_source_type.id)
            .filter(Document.text_content.ilike("%learning%"))
            .all()
        )
        assert len(notes) == 2

    def test_list_notes_pinned_only(self, db_session, note_source_type):
        """Test listing only pinned notes."""
        _create_note(
            db_session, note_source_type.id, "Pinned", "Content", pinned=True
        )
        _create_note(
            db_session,
            note_source_type.id,
            "Not Pinned 1",
            "Content",
            pinned=False,
        )
        _create_note(
            db_session,
            note_source_type.id,
            "Not Pinned 2",
            "Content",
            pinned=False,
        )

        notes = (
            db_session.query(Document)
            .filter(Document.source_type_id == note_source_type.id)
            .filter(Document.favorite.is_(True))
            .all()
        )
        assert len(notes) == 1
        assert notes[0].title == "Pinned"

    def test_list_notes_limit(self, db_session, note_source_type):
        """Test listing notes with limit."""
        for i in range(10):
            _create_note(
                db_session, note_source_type.id, f"Note {i}", f"Content {i}"
            )

        notes = (
            db_session.query(Document)
            .filter_by(source_type_id=note_source_type.id)
            .limit(5)
            .all()
        )
        assert len(notes) == 5

    def test_list_notes_offset(self, db_session, note_source_type):
        """Test listing notes with offset."""
        for i in range(10):
            _create_note(
                db_session, note_source_type.id, f"Note {i}", f"Content {i}"
            )

        notes = (
            db_session.query(Document)
            .filter_by(source_type_id=note_source_type.id)
            .offset(5)
            .limit(5)
            .all()
        )
        assert len(notes) == 5

    def test_list_notes_order_by_updated(self, db_session, note_source_type):
        """Test listing notes ordered by update time."""
        from datetime import datetime, timedelta, timezone

        created = []
        for i in range(3):
            note = _create_note(
                db_session, note_source_type.id, f"Note {i}", f"Content {i}"
            )
            created.append(note)

        # The ORM default for `updated_at` is server-side `CURRENT_TIMESTAMP`,
        # which on SQLite has second-level granularity — three inserts in
        # the same second collide and the DESC sort becomes non-deterministic
        # on fast CI runners (Python 3.14 in Docker hit this). Set explicit
        # staggered timestamps to make ordering observable.
        base = datetime.now(timezone.utc)
        for i, note in enumerate(created):
            note.updated_at = base + timedelta(seconds=i)
        db_session.commit()

        notes = (
            db_session.query(Document)
            .filter_by(source_type_id=note_source_type.id)
            .order_by(Document.updated_at.desc())
            .all()
        )
        assert len(notes) == 3
        # Most recently created should be first
        assert notes[0].title == "Note 2"

    def test_list_notes_pinned_first(self, db_session, note_source_type):
        """Test that pinned notes appear first when ordered."""
        _create_note(db_session, note_source_type.id, "Regular Note", "Content")
        _create_note(
            db_session,
            note_source_type.id,
            "Pinned Note",
            "Content",
            pinned=True,
        )

        notes = (
            db_session.query(Document)
            .filter_by(source_type_id=note_source_type.id)
            .order_by(Document.favorite.desc(), Document.updated_at.desc())
            .all()
        )
        assert notes[0].title == "Pinned Note"


class TestNoteCollectionOperations:
    """Test suite for Note collection operations."""

    @pytest.fixture
    def sample_collection(self, db_session):
        """Create a sample collection."""
        collection = Collection(
            id=str(uuid.uuid4()),
            name="Test Collection",
            description="A test collection",
            collection_type="user",
        )
        db_session.add(collection)
        db_session.commit()
        return collection

    def test_add_note_to_collection(
        self, db_session, note_source_type, sample_collection
    ):
        """Test adding a note to a collection."""
        note = _create_note(db_session, note_source_type.id, "Note", "Content")

        doc_coll = DocumentCollection(
            document_id=note.id,
            collection_id=sample_collection.id,
            indexed=False,
        )
        db_session.add(doc_coll)
        db_session.commit()

        # Verify
        found = (
            db_session.query(DocumentCollection)
            .filter_by(document_id=note.id, collection_id=sample_collection.id)
            .first()
        )
        assert found is not None

    def test_note_in_multiple_collections(self, db_session, note_source_type):
        """Test a note can be in multiple collections."""
        note = _create_note(db_session, note_source_type.id, "Note", "Content")

        collections = []
        for i in range(3):
            coll = Collection(
                id=str(uuid.uuid4()),
                name=f"Collection {i}",
                collection_type="user",
            )
            db_session.add(coll)
            collections.append(coll)
        db_session.commit()

        for coll in collections:
            doc_coll = DocumentCollection(
                document_id=note.id,
                collection_id=coll.id,
                indexed=False,
            )
            db_session.add(doc_coll)
        db_session.commit()

        count = (
            db_session.query(DocumentCollection)
            .filter_by(document_id=note.id)
            .count()
        )
        assert count == 3

    def test_remove_note_from_collection(
        self, db_session, note_source_type, sample_collection
    ):
        """Test removing a note from a collection."""
        note = _create_note(db_session, note_source_type.id, "Note", "Content")

        doc_coll = DocumentCollection(
            document_id=note.id,
            collection_id=sample_collection.id,
            indexed=False,
        )
        db_session.add(doc_coll)
        db_session.commit()

        # Remove
        db_session.delete(doc_coll)
        db_session.commit()

        found = (
            db_session.query(DocumentCollection)
            .filter_by(document_id=note.id, collection_id=sample_collection.id)
            .first()
        )
        assert found is None

    def test_get_notes_in_collection(
        self, db_session, note_source_type, sample_collection
    ):
        """Test getting all notes in a collection."""
        notes = []
        for i in range(3):
            note = _create_note(
                db_session, note_source_type.id, f"Note {i}", f"Content {i}"
            )
            notes.append(note)
            doc_coll = DocumentCollection(
                document_id=note.id,
                collection_id=sample_collection.id,
                indexed=False,
            )
            db_session.add(doc_coll)
        db_session.commit()

        # Query notes in collection
        notes_in_coll = (
            db_session.query(Document)
            .join(DocumentCollection)
            .filter(DocumentCollection.collection_id == sample_collection.id)
            .all()
        )
        assert len(notes_in_coll) == 3


class TestNoteBacklinkOperations:
    """Test suite for Note backlink operations."""

    def test_get_backlinks_none(self, db_session, note_source_type):
        """Test getting backlinks when there are none."""
        note = _create_note(
            db_session, note_source_type.id, "Lonely Note", "No one links here"
        )

        backlinks = (
            db_session.query(NoteLink)
            .filter_by(target_document_id=note.id)
            .all()
        )
        assert backlinks == []

    def test_get_backlinks_single(self, db_session, note_source_type):
        """Test getting a single backlink."""
        target = _create_note(
            db_session, note_source_type.id, "Target Note", "I am linked to"
        )
        source = _create_note(
            db_session,
            note_source_type.id,
            "Source Note",
            "Check out [[Target Note]]",
        )

        link = NoteLink(
            source_document_id=source.id,
            target_document_id=target.id,
            link_text="Target Note",
        )
        db_session.add(link)
        db_session.commit()

        backlinks = (
            db_session.query(NoteLink)
            .filter_by(target_document_id=target.id)
            .all()
        )
        assert len(backlinks) == 1
        assert backlinks[0].source_document_id == source.id

    def test_get_backlinks_multiple(self, db_session, note_source_type):
        """Test getting multiple backlinks."""
        target = _create_note(
            db_session,
            note_source_type.id,
            "Popular Note",
            "Everyone links here",
        )

        for i in range(5):
            source = _create_note(
                db_session,
                note_source_type.id,
                f"Source {i}",
                "Links to [[Popular Note]]",
            )
            link = NoteLink(
                source_document_id=source.id,
                target_document_id=target.id,
                link_text="Popular Note",
            )
            db_session.add(link)

        db_session.commit()

        backlinks = (
            db_session.query(NoteLink)
            .filter_by(target_document_id=target.id)
            .all()
        )
        assert len(backlinks) == 5

    def test_get_backlinks_with_source_info(self, db_session, note_source_type):
        """Test getting backlinks with source note info."""
        target = _create_note(
            db_session, note_source_type.id, "Target", "Target content"
        )
        source = _create_note(
            db_session,
            note_source_type.id,
            "Source",
            "This mentions [[Target]]",
        )

        link = NoteLink(
            source_document_id=source.id,
            target_document_id=target.id,
            link_text="Target",
        )
        db_session.add(link)
        db_session.commit()

        # Query with join to get source note info
        backlinks = (
            db_session.query(NoteLink, Document)
            .join(Document, NoteLink.source_document_id == Document.id)
            .filter(NoteLink.target_document_id == target.id)
            .all()
        )
        assert len(backlinks) == 1
        link_obj, source_doc = backlinks[0]
        assert source_doc.title == "Source"


class TestNoteVersionOperations:
    """Test suite for Note version operations."""

    def test_create_initial_version(self, db_session, note_source_type):
        """Test creating initial version for a note."""
        note = _create_note(
            db_session, note_source_type.id, "Title", "Initial content"
        )

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Title",
            content="Initial content",
            change_type="initial",
            content_hash=_generate_hash("Initial content"),
        )
        db_session.add(version)
        db_session.commit()

        found = (
            db_session.query(NoteVersion).filter_by(document_id=note.id).first()
        )
        assert found is not None
        assert found.id is not None
        assert found.change_type == "initial"

    def test_create_multiple_versions(self, db_session, note_source_type):
        """Test creating multiple versions."""
        note = _create_note(db_session, note_source_type.id, "Title", "Content")

        for i in range(1, 4):
            version = NoteVersion(
                id=str(uuid.uuid4()),
                document_id=note.id,
                title=f"Title v{i}",
                content=f"Content v{i}",
                change_type="initial" if i == 1 else "manual_save",
                content_hash=_generate_hash(f"Content v{i}"),
            )
            db_session.add(version)
        db_session.commit()

        versions = (
            db_session.query(NoteVersion)
            .filter_by(document_id=note.id)
            .order_by(NoteVersion.created_at)
            .all()
        )
        assert len(versions) == 3

    def test_get_latest_version(self, db_session, note_source_type):
        """Test getting the latest version of a note."""
        note = _create_note(db_session, note_source_type.id, "Title", "Content")

        for i in range(1, 4):
            version = NoteVersion(
                id=str(uuid.uuid4()),
                document_id=note.id,
                title=f"Title v{i}",
                content=f"Content v{i}",
                change_type="manual_save",
                content_hash=_generate_hash(f"Content v{i}"),
            )
            db_session.add(version)
        db_session.commit()

        latest = (
            db_session.query(NoteVersion)
            .filter_by(document_id=note.id)
            .order_by(NoteVersion.created_at.desc())
            .first()
        )
        assert latest is not None

    def test_version_with_tags(self, db_session, note_source_type):
        """Test version stores tags."""
        note = _create_note(db_session, note_source_type.id, "Title", "Content")

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Title",
            content="Content",
            tags=["tag1", "tag2", "tag3"],
            change_type="initial",
            content_hash=_generate_hash("Content"),
        )
        db_session.add(version)
        db_session.commit()

        found = (
            db_session.query(NoteVersion).filter_by(document_id=note.id).first()
        )
        assert found.tags == ["tag1", "tag2", "tag3"]

    def test_version_with_change_summary(self, db_session, note_source_type):
        """Test version stores change summary."""
        note = _create_note(db_session, note_source_type.id, "Title", "Content")

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Updated Title",
            content="Updated Content",
            change_type="manual_save",
            change_summary="Added new section about machine learning",
            content_hash=_generate_hash("Updated Content"),
        )
        db_session.add(version)
        db_session.commit()

        found = (
            db_session.query(NoteVersion).filter_by(document_id=note.id).first()
        )
        assert (
            found.change_summary == "Added new section about machine learning"
        )


class TestWikiLinkParsing:
    """Test suite for wiki-style link parsing."""

    def test_parse_simple_link(self):
        """Test parsing a simple wiki link."""
        from local_deep_research.research_library.notes.services.note_service import (
            LINK_PATTERN,
        )

        content = "Check out [[My Note]] for more."
        matches = LINK_PATTERN.findall(content)
        assert matches == ["My Note"]

    def test_parse_multiple_links(self):
        """Test parsing multiple wiki links."""
        from local_deep_research.research_library.notes.services.note_service import (
            LINK_PATTERN,
        )

        content = "See [[Note A]], [[Note B]], and [[Note C]]."
        matches = LINK_PATTERN.findall(content)
        assert len(matches) == 3
        assert set(matches) == {"Note A", "Note B", "Note C"}

    def test_parse_link_with_special_chars(self):
        """Test parsing link with special characters."""
        from local_deep_research.research_library.notes.services.note_service import (
            LINK_PATTERN,
        )

        content = "See [[Note: A Summary (2024)]]."
        matches = LINK_PATTERN.findall(content)
        assert matches == ["Note: A Summary (2024)"]

    def test_parse_no_links(self):
        """Test parsing content with no links."""
        from local_deep_research.research_library.notes.services.note_service import (
            LINK_PATTERN,
        )

        content = "This is plain text with no wiki links."
        matches = LINK_PATTERN.findall(content)
        assert matches == []

    def test_parse_empty_brackets(self):
        """Test that empty brackets are not 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 matches == []

    def test_parse_single_brackets_not_matched(self):
        """Test that single brackets are not matched."""
        from local_deep_research.research_library.notes.services.note_service import (
            LINK_PATTERN,
        )

        content = "This has [single brackets] only."
        matches = LINK_PATTERN.findall(content)
        assert matches == []

    def test_parse_links_in_markdown(self):
        """Test parsing links in markdown content."""
        from local_deep_research.research_library.notes.services.note_service import (
            LINK_PATTERN,
        )

        content = """
# Heading

This is a paragraph with a [[Link Here]].

## Another Heading

More text with [[Another Link]].
"""
        matches = LINK_PATTERN.findall(content)
        assert len(matches) == 2
        assert "Link Here" in matches
        assert "Another Link" in matches