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.3 KB
Raw Blame
"""Tests for Note database models (notes are Documents with source_type='note')."""

import uuid

import pytest
from sqlalchemy.exc import IntegrityError

from local_deep_research.database.models import (
    Collection,
    Document,
    DocumentCollection,
    NoteLink,
    NoteSynthesis,
    NoteSynthesisSource,
    NoteVersion,
)

from tests.notes.helpers import _generate_hash


class TestNoteModels:
    """Test suite for Note-related models."""

    # =========================================================================
    # Note (Document with source_type='note') Model Tests
    # =========================================================================

    def test_note_creation(self, db_session, note_source_type):
        """Test creating a Note (Document with source_type='note')."""
        content = "This is test content"
        note = Document(
            id=str(uuid.uuid4()),
            title="Test Note",
            text_content=content,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash(content),
            source_type_id=note_source_type.id,
            tags=["test", "example"],
            favorite=False,
        )

        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert saved is not None
        assert saved.title == "Test Note"
        assert saved.text_content == "This is test content"
        assert saved.file_type == "note"
        assert saved.tags == ["test", "example"]
        assert saved.favorite is False

    def test_note_favorite_status(self, db_session, note_source_type):
        """Test note favorite (pinned) status."""
        content = "Important content"
        note = Document(
            id=str(uuid.uuid4()),
            title="Favorite Note",
            text_content=content,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash(content),
            source_type_id=note_source_type.id,
            favorite=True,
        )

        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert saved.favorite is True

        # Update favorite status
        saved.favorite = False
        db_session.commit()
        assert saved.favorite is False

    # =========================================================================
    # NoteLink Model Tests
    # =========================================================================

    def test_note_link_creation(self, db_session, note_source_type):
        """Test creating a NoteLink record for wiki-style [[links]]."""
        # Create two notes
        source_content = "Content with [[Target Note]] link"
        target_content = "Target content"
        source_note = Document(
            id=str(uuid.uuid4()),
            title="Source Note",
            text_content=source_content,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash(source_content),
            source_type_id=note_source_type.id,
        )
        target_note = Document(
            id=str(uuid.uuid4()),
            title="Target Note",
            text_content=target_content,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash(target_content),
            source_type_id=note_source_type.id,
        )

        db_session.add_all([source_note, target_note])
        db_session.commit()

        # Create link
        link = NoteLink(
            source_document_id=source_note.id,
            target_document_id=target_note.id,
            link_text="Target Note",
            auto_suggested=False,
        )

        db_session.add(link)
        db_session.commit()

        saved = db_session.query(NoteLink).first()
        assert saved is not None
        assert saved.source_document_id == source_note.id
        assert saved.target_document_id == target_note.id
        assert saved.link_text == "Target Note"
        assert saved.auto_suggested is False

    def test_note_link_auto_suggested(self, db_session, note_source_type):
        """Test NoteLink with auto_suggested flag."""
        source = Document(
            id=str(uuid.uuid4()),
            title="Source",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_1"),
            source_type_id=note_source_type.id,
        )
        target = Document(
            id=str(uuid.uuid4()),
            title="Target",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_2"),
            source_type_id=note_source_type.id,
        )

        db_session.add_all([source, target])
        db_session.commit()

        link = NoteLink(
            source_document_id=source.id,
            target_document_id=target.id,
            link_text="Target",
            auto_suggested=True,
        )

        db_session.add(link)
        db_session.commit()

        saved = db_session.query(NoteLink).first()
        assert saved.auto_suggested is True

    def test_note_link_unique_constraint(self, db_session, note_source_type):
        """Test that duplicate links are prevented by unique constraint."""
        source = Document(
            id=str(uuid.uuid4()),
            title="Source",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_3"),
            source_type_id=note_source_type.id,
        )
        target = Document(
            id=str(uuid.uuid4()),
            title="Target",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_4"),
            source_type_id=note_source_type.id,
        )

        db_session.add_all([source, target])
        db_session.commit()

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

        # Try to add duplicate
        link2 = NoteLink(
            source_document_id=source.id,
            target_document_id=target.id,
            link_text="Target Again",
        )
        db_session.add(link2)

        with pytest.raises(Exception):  # IntegrityError
            db_session.commit()

    def test_note_synthesis_source_unique_constraint(
        self, db_session, note_source_type
    ):
        """uix_note_synthesis_source makes (synthesis_id,
        source_document_id) unique — the same source note can't be recorded
        twice for one synthesis. A different source under the same synthesis is
        allowed (positive control)."""
        src = Document(
            id=str(uuid.uuid4()),
            title="Src",
            text_content="c",
            file_type="note",
            file_size=1,
            document_hash=_generate_hash("synthsrc"),
            source_type_id=note_source_type.id,
        )
        src2 = Document(
            id=str(uuid.uuid4()),
            title="Src2",
            text_content="c",
            file_type="note",
            file_size=1,
            document_hash=_generate_hash("synthsrc2"),
            source_type_id=note_source_type.id,
        )
        db_session.add_all([src, src2])
        db_session.commit()

        synthesis = NoteSynthesis(synthesis_type="merge")
        db_session.add(synthesis)
        db_session.commit()

        db_session.add(
            NoteSynthesisSource(
                synthesis_id=synthesis.id, source_document_id=src.id
            )
        )
        db_session.commit()

        # Duplicate (same synthesis + same source) → IntegrityError.
        db_session.add(
            NoteSynthesisSource(
                synthesis_id=synthesis.id, source_document_id=src.id
            )
        )
        with pytest.raises(Exception):  # uix_note_synthesis_source
            db_session.commit()
        db_session.rollback()

        # A different source under the same synthesis commits fine.
        db_session.add(
            NoteSynthesisSource(
                synthesis_id=synthesis.id, source_document_id=src2.id
            )
        )
        db_session.commit()
        assert (
            db_session.query(NoteSynthesisSource)
            .filter_by(synthesis_id=synthesis.id)
            .count()
            == 2
        )

    def test_note_link_relationship_outgoing(
        self, db_session, note_source_type
    ):
        """Test outgoing links relationship from Document."""
        source = Document(
            id=str(uuid.uuid4()),
            title="Source",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_5"),
            source_type_id=note_source_type.id,
        )
        target1 = Document(
            id=str(uuid.uuid4()),
            title="Target 1",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_6"),
            source_type_id=note_source_type.id,
        )
        target2 = Document(
            id=str(uuid.uuid4()),
            title="Target 2",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_7"),
            source_type_id=note_source_type.id,
        )

        db_session.add_all([source, target1, target2])
        db_session.commit()

        link1 = NoteLink(
            source_document_id=source.id,
            target_document_id=target1.id,
            link_text="Target 1",
        )
        link2 = NoteLink(
            source_document_id=source.id,
            target_document_id=target2.id,
            link_text="Target 2",
        )

        db_session.add_all([link1, link2])
        db_session.commit()

        # Refresh to load relationships
        db_session.refresh(source)
        assert len(source.outgoing_note_links) == 2

    def test_note_link_relationship_incoming(
        self, db_session, note_source_type
    ):
        """Test incoming links (backlinks) relationship from Document."""
        source1 = Document(
            id=str(uuid.uuid4()),
            title="Source 1",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_8"),
            source_type_id=note_source_type.id,
        )
        source2 = Document(
            id=str(uuid.uuid4()),
            title="Source 2",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_9"),
            source_type_id=note_source_type.id,
        )
        target = Document(
            id=str(uuid.uuid4()),
            title="Target",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_10"),
            source_type_id=note_source_type.id,
        )

        db_session.add_all([source1, source2, target])
        db_session.commit()

        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()

        db_session.refresh(target)
        assert len(target.incoming_note_links) == 2

    def test_note_link_cascade_delete(self, db_session, note_source_type):
        """Test that links are deleted when source note is deleted."""
        source = Document(
            id=str(uuid.uuid4()),
            title="Source",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_11"),
            source_type_id=note_source_type.id,
        )
        target = Document(
            id=str(uuid.uuid4()),
            title="Target",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_12"),
            source_type_id=note_source_type.id,
        )

        db_session.add_all([source, target])
        db_session.commit()

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

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

        # Link should be deleted
        remaining = db_session.query(NoteLink).count()
        assert remaining == 0

    # =========================================================================
    # NoteVersion Model Tests
    # =========================================================================

    def test_note_version_creation(self, db_session, note_source_type):
        """Test creating a NoteVersion record."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Versioned Note",
            text_content="Initial content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Initial content"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Versioned Note",
            content="Initial content",
            tags=["test"],
            change_type="initial",
            change_summary="Initial version",
            content_hash="abc123",
        )

        db_session.add(version)
        db_session.commit()

        saved = db_session.query(NoteVersion).first()
        assert saved is not None
        assert saved.document_id == note.id
        assert saved.id is not None
        assert saved.title == "Versioned Note"
        assert saved.content == "Initial content"
        assert saved.change_type == "initial"
        assert saved.content_hash == "abc123"

    def test_note_version_change_types(self, db_session, note_source_type):
        """Test different change types for NoteVersion."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Note",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_13"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        change_types = [
            "initial",
            "auto_save",
            "manual_save",
            "restore",
            "pre_restore",
        ]

        for i, change_type in enumerate(change_types, 1):
            version = NoteVersion(
                id=str(uuid.uuid4()),
                document_id=note.id,
                title="Note",
                content=f"Content v{i}",
                change_type=change_type,
                content_hash=f"hash{i}",
            )
            db_session.add(version)

        db_session.commit()

        versions = db_session.query(NoteVersion).all()
        assert len(versions) == len(change_types)

    def test_invalid_change_type_rejected_by_check_constraint(
        self, db_session, note_source_type
    ):
        """A change_type outside the enum is rejected at the DB level by
        ck_note_change_type (note.py:120-123). conftest enables PRAGMA
        foreign_keys=ON and SQLite enforces CHECK by default, so this is
        runtime enforcement — the text-inspection tests
        (test_change_type_check_constraint_present, TestEnumCheckParity)
        can't catch a syntactically-present-but-ineffective constraint.
        """
        note = Document(
            id=str(uuid.uuid4()),
            title="Note",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_invalid_ct"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Note",
            content="x",
            change_type="bogus_type",
            content_hash="hashbad",
        )
        db_session.add(version)

        with pytest.raises(Exception):  # IntegrityError (ck_note_change_type)
            db_session.commit()

        db_session.rollback()
        # The bad row must never have landed.
        assert db_session.query(NoteVersion).count() == 0

    def test_note_version_unique_id(self, db_session, note_source_type):
        """Test that each NoteVersion has a unique UUID primary key."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Note",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_14"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        version_id = str(uuid.uuid4())
        v1 = NoteVersion(
            id=version_id,
            document_id=note.id,
            title="Note",
            content="Content",
            change_type="initial",
            content_hash="hash1",
        )
        db_session.add(v1)
        db_session.commit()

        # The duplicate row must reach the database so its PRIMARY KEY
        # constraint is what rejects it. Remove the original object from the
        # identity map first; otherwise SQLAlchemy warns about the deliberate
        # in-session identity conflict before issuing the INSERT.
        db_session.expunge(v1)
        v1_dup = NoteVersion(
            id=version_id,
            document_id=note.id,
            title="Note",
            content="Different",
            change_type="manual_save",
            content_hash="hash2",
        )
        db_session.add(v1_dup)

        with pytest.raises(IntegrityError):
            db_session.commit()

    def test_note_version_relationship(self, db_session, note_source_type):
        """Test Document.note_versions relationship."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Note",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_15"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        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=f"hash{i}",
            )
            db_session.add(version)

        db_session.commit()
        db_session.refresh(note)

        assert len(note.note_versions) == 3

    def test_note_version_cascade_delete(self, db_session, note_source_type):
        """Test that versions are deleted when note is deleted."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Note",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_16"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Note",
            content="Content",
            change_type="initial",
            content_hash="hash",
        )
        db_session.add(version)
        db_session.commit()

        db_session.delete(note)
        db_session.commit()

        remaining = db_session.query(NoteVersion).count()
        assert remaining == 0

    def test_note_version_change_summary(self, db_session, note_source_type):
        """Test storing LLM-generated change summaries."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Note",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_17"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Note",
            content="Content",
            change_type="manual_save",
            change_summary="Added a new section about quantum mechanics",
            content_hash="hash",
        )

        db_session.add(version)
        db_session.commit()

        saved = db_session.query(NoteVersion).first()
        assert (
            saved.change_summary
            == "Added a new section about quantum mechanics"
        )

    # =========================================================================
    # NoteSynthesis Model Tests
    # =========================================================================

    def test_note_synthesis_creation(self, db_session, note_source_type):
        """Test creating a NoteSynthesis record."""
        # Create source notes
        note1 = Document(
            id=str(uuid.uuid4()),
            title="Note 1",
            text_content="Content 1",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content 1"),
            source_type_id=note_source_type.id,
        )
        note2 = Document(
            id=str(uuid.uuid4()),
            title="Note 2",
            text_content="Content 2",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content 2"),
            source_type_id=note_source_type.id,
        )
        result_note = Document(
            id=str(uuid.uuid4()),
            title="Merged Note",
            text_content="Merged content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Merged content"),
            source_type_id=note_source_type.id,
        )

        db_session.add_all([note1, note2, result_note])
        db_session.commit()

        synthesis = NoteSynthesis(
            result_document_id=result_note.id,
            synthesis_type="merge",
        )
        synthesis.sources = [
            NoteSynthesisSource(source_document_id=note1.id),
            NoteSynthesisSource(source_document_id=note2.id),
        ]

        db_session.add(synthesis)
        db_session.commit()

        saved = db_session.query(NoteSynthesis).first()
        assert saved is not None
        assert saved.result_document_id == result_note.id
        assert [s.source_document_id for s in saved.sources] == [
            note1.id,
            note2.id,
        ]
        assert saved.synthesis_type == "merge"

    def test_note_synthesis_types(self, db_session, note_source_type):
        """Test different synthesis types."""
        for i, synthesis_type in enumerate(["merge", "summarize", "compare"]):
            result_note = Document(
                id=str(uuid.uuid4()),
                title=f"Result {synthesis_type}",
                text_content=f"Content {synthesis_type}",
                file_type="note",
                file_size=0,
                document_hash=_generate_hash(f"synthesis_types_{i}"),
                source_type_id=note_source_type.id,
            )
            db_session.add(result_note)
            db_session.commit()

            synthesis = NoteSynthesis(
                result_document_id=result_note.id,
                synthesis_type=synthesis_type,
            )
            db_session.add(synthesis)

        db_session.commit()

        syntheses = db_session.query(NoteSynthesis).all()
        assert len(syntheses) == 3

    def test_note_synthesis_multiple_sources(
        self, db_session, note_source_type
    ):
        """Test synthesis with multiple source notes (2-5)."""
        source_docs = [
            Document(
                id=str(uuid.uuid4()),
                title=f"Source {i}",
                text_content=f"c{i}",
                file_type="note",
                file_size=1,
                document_hash=_generate_hash(f"multi_source_{i}"),
                source_type_id=note_source_type.id,
            )
            for i in range(5)
        ]

        result = Document(
            id=str(uuid.uuid4()),
            title="Result",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_19"),
            source_type_id=note_source_type.id,
        )
        db_session.add_all(source_docs + [result])
        db_session.commit()

        synthesis = NoteSynthesis(
            result_document_id=result.id,
            synthesis_type="merge",
        )
        synthesis.sources = [
            NoteSynthesisSource(source_document_id=doc.id)
            for doc in source_docs
        ]

        db_session.add(synthesis)
        db_session.commit()

        saved = db_session.query(NoteSynthesis).first()
        assert len(saved.sources) == 5

    def test_note_synthesis_relationship(self, db_session, note_source_type):
        """Test NoteSynthesis.result_document relationship."""
        result = Document(
            id=str(uuid.uuid4()),
            title="Result",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_20"),
            source_type_id=note_source_type.id,
        )
        db_session.add(result)
        db_session.commit()

        synthesis = NoteSynthesis(
            result_document_id=result.id,
            synthesis_type="merge",
        )

        db_session.add(synthesis)
        db_session.commit()
        db_session.refresh(synthesis)

        assert synthesis.result_document is not None
        assert synthesis.result_document.title == "Result"

    def test_note_synthesis_set_null_on_delete(
        self, db_session, note_source_type
    ):
        """Test that result_document_id is set to NULL when result note is deleted."""
        result = Document(
            id=str(uuid.uuid4()),
            title="Result",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_21"),
            source_type_id=note_source_type.id,
        )
        db_session.add(result)
        db_session.commit()

        synthesis = NoteSynthesis(
            result_document_id=result.id,
            synthesis_type="merge",
        )

        db_session.add(synthesis)
        db_session.commit()

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

        # Synthesis should still exist but with null result_document_id
        saved = db_session.query(NoteSynthesis).first()
        assert saved is not None
        assert saved.result_document_id is None

    def test_delete_source_note_cascades_synthesis_source_but_keeps_synthesis_with_null_result(
        self, db_session, note_source_type
    ):
        """Deleting a SOURCE note CASCADE-deletes its NoteSynthesisSource
        junction row (note.py:363 ondelete='CASCADE') but must NOT touch the
        parent NoteSynthesis or its result FK. Demonstrates the SET-NULL vs
        CASCADE asymmetry on a single synthesis record: deleting the RESULT
        note nulls result_document_id (note.py:293) while the row persists.

        PRAGMA foreign_keys=ON in conftest makes the DB-level ondelete
        genuinely enforced. The sibling test_note_synthesis_set_null_on_delete
        only deletes the RESULT note, leaving the CASCADE side unverified.
        """
        result = Document(
            id=str(uuid.uuid4()),
            title="Result",
            text_content="Result content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("cascade_synth_result"),
            source_type_id=note_source_type.id,
        )
        source = Document(
            id=str(uuid.uuid4()),
            title="Source",
            text_content="Source content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("cascade_synth_source"),
            source_type_id=note_source_type.id,
        )
        db_session.add_all([result, source])
        db_session.commit()

        synthesis = NoteSynthesis(
            result_document_id=result.id,
            synthesis_type="merge",
        )
        db_session.add(synthesis)
        db_session.commit()
        db_session.refresh(synthesis)
        synthesis_id = synthesis.id

        src_link = NoteSynthesisSource(
            synthesis_id=synthesis_id,
            source_document_id=source.id,
        )
        db_session.add(src_link)
        db_session.commit()

        # Delete the SOURCE note.
        db_session.delete(source)
        db_session.commit()

        # (1) CASCADE: the junction row is gone (source_document_id uses
        # ondelete='CASCADE'; a flip to SET NULL/RESTRICT would fail here —
        # SET NULL would even error on the nullable=False column).
        assert db_session.query(NoteSynthesisSource).count() == 0, (
            "deleting a source note must CASCADE-delete its "
            "NoteSynthesisSource junction row (note.py:363)."
        )

        # (2) the parent synthesis (and its result FK) is untouched.
        saved = (
            db_session.query(NoteSynthesis).filter_by(id=synthesis_id).first()
        )
        assert saved is not None
        assert saved.result_document_id == result.id, (
            "deleting a SOURCE must not touch the synthesis or its result FK."
        )

        # Now delete the RESULT note: SET NULL on result FK, row persists.
        db_session.delete(result)
        db_session.commit()
        db_session.refresh(saved)
        assert saved.result_document_id is None, (
            "deleting the RESULT note must SET NULL the result FK "
            "(note.py:293), not delete the synthesis."
        )
        assert (
            db_session.query(NoteSynthesis).filter_by(id=synthesis_id).first()
            is not None
        )

    # =========================================================================
    # DocumentCollection Model Tests (replaces NoteCollection)
    # =========================================================================

    def test_document_collection_relationship(
        self, db_session, note_source_type
    ):
        """Test note-collection many-to-many relationship."""
        # Create collection
        collection = Collection(
            id=str(uuid.uuid4()),
            name="Test Collection",
            collection_type="notes",
        )
        db_session.add(collection)
        db_session.commit()

        # Create note
        note = Document(
            id=str(uuid.uuid4()),
            title="Test Note",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Content_22"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        # Link them
        doc_coll = DocumentCollection(
            document_id=note.id,
            collection_id=collection.id,
        )
        db_session.add(doc_coll)
        db_session.commit()

        # Verify relationship
        db_session.refresh(note)
        assert len(note.collections) == 1
        assert note.collections[0].collection_id == collection.id

    # =========================================================================
    # Comprehensive Relationship Tests
    # =========================================================================

    def test_note_full_relationships(self, db_session, note_source_type):
        """Test a note with all relationships populated."""
        # Create a note
        note = Document(
            id=str(uuid.uuid4()),
            title="Complete Note",
            text_content="This has all relationships",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("This has all relationships"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        # Add a version
        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Complete Note",
            content="This has all relationships",
            change_type="initial",
            content_hash="hash",
        )
        db_session.add(version)

        # Add an outgoing link
        target = Document(
            id=str(uuid.uuid4()),
            title="Target",
            text_content="Target",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("Target"),
            source_type_id=note_source_type.id,
        )
        db_session.add(target)
        db_session.commit()

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

        db_session.commit()
        db_session.refresh(note)

        # Verify all relationships
        assert len(note.note_versions) == 1
        assert len(note.outgoing_note_links) == 1