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.

66.1 KB
Raw Blame
"""Integration tests for Notes functionality."""

import asyncio
import uuid
from unittest.mock import AsyncMock, MagicMock

import pytest

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

from tests.notes.helpers import _generate_hash


class TestLinkingWorkflow:
    """Integration tests for the complete linking workflow."""

    @pytest.fixture
    def setup_knowledge_base(self, db_session, note_source_type):
        """Set up a knowledge base with interconnected notes."""
        notes = {
            "ml": Document(
                id=str(uuid.uuid4()),
                title="Machine Learning",
                text_content="Machine learning is a subset of [[Artificial Intelligence]].",
                tags=["ai", "ml"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("ml_note"),
                source_type_id=note_source_type.id,
            ),
            "ai": Document(
                id=str(uuid.uuid4()),
                title="Artificial Intelligence",
                text_content="AI includes [[Machine Learning]] and [[Deep Learning]].",
                tags=["ai"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("ai_note"),
                source_type_id=note_source_type.id,
            ),
            "dl": Document(
                id=str(uuid.uuid4()),
                title="Deep Learning",
                text_content="Deep learning uses neural networks. Related to [[Machine Learning]].",
                tags=["ai", "ml", "neural-networks"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("dl_note"),
                source_type_id=note_source_type.id,
            ),
            "python": Document(
                id=str(uuid.uuid4()),
                title="Python",
                text_content="Python is used for [[Machine Learning]] and [[Deep Learning]].",
                tags=["programming"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("python_note"),
                source_type_id=note_source_type.id,
            ),
        }

        for note in notes.values():
            db_session.add(note)
        db_session.commit()

        return notes

    def test_parse_links_from_content(self, db_session, setup_knowledge_base):
        """Test parsing [[links]] from note content."""
        from local_deep_research.research_library.notes.services.note_service import (
            LINK_PATTERN,
        )

        notes = setup_knowledge_base

        # Parse links from AI note
        ai_content = notes["ai"].text_content
        links = LINK_PATTERN.findall(ai_content)

        assert len(links) == 2
        assert "Machine Learning" in links
        assert "Deep Learning" in links

    def test_resolve_links_workflow(self, db_session, setup_knowledge_base):
        """Test the complete link resolution workflow."""
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        notes = setup_knowledge_base
        service = NoteService("test_user")

        # Test exact match
        result = service._resolve_link_internal(db_session, "Machine Learning")
        assert result is not None
        assert result["id"] == notes["ml"].id

        # Test title match
        result = service._resolve_link_internal(db_session, "Deep Learning")
        assert result is not None
        assert result["id"] == notes["dl"].id

        # Test partial match
        result = service._resolve_link_internal(db_session, "Python")
        assert result is not None
        assert result["id"] == notes["python"].id

    def test_build_link_graph(self, db_session, setup_knowledge_base):
        """Test building a link graph from notes."""
        notes = setup_knowledge_base

        # Create links based on content
        # ML -> AI
        link1 = NoteLink(
            source_document_id=notes["ml"].id,
            target_document_id=notes["ai"].id,
            link_text="Artificial Intelligence",
        )
        # AI -> ML
        link2 = NoteLink(
            source_document_id=notes["ai"].id,
            target_document_id=notes["ml"].id,
            link_text="Machine Learning",
        )
        # AI -> DL
        link3 = NoteLink(
            source_document_id=notes["ai"].id,
            target_document_id=notes["dl"].id,
            link_text="Deep Learning",
        )
        # DL -> ML
        link4 = NoteLink(
            source_document_id=notes["dl"].id,
            target_document_id=notes["ml"].id,
            link_text="Machine Learning",
        )
        # Python -> ML
        link5 = NoteLink(
            source_document_id=notes["python"].id,
            target_document_id=notes["ml"].id,
            link_text="Machine Learning",
        )
        # Python -> DL
        link6 = NoteLink(
            source_document_id=notes["python"].id,
            target_document_id=notes["dl"].id,
            link_text="Deep Learning",
        )

        db_session.add_all([link1, link2, link3, link4, link5, link6])
        db_session.commit()

        # Verify graph structure
        # ML should have 3 backlinks (AI, DL, Python)
        ml_backlinks = (
            db_session.query(NoteLink)
            .filter_by(target_document_id=notes["ml"].id)
            .all()
        )
        assert len(ml_backlinks) == 3

        # AI should have 1 backlink (ML)
        ai_backlinks = (
            db_session.query(NoteLink)
            .filter_by(target_document_id=notes["ai"].id)
            .all()
        )
        assert len(ai_backlinks) == 1

        # DL should have 2 backlinks (AI, Python)
        dl_backlinks = (
            db_session.query(NoteLink)
            .filter_by(target_document_id=notes["dl"].id)
            .all()
        )
        assert len(dl_backlinks) == 2

    def test_update_links_on_content_change(
        self, db_session, setup_knowledge_base
    ):
        """Test that links can be updated when content changes."""
        notes = setup_knowledge_base

        # Initial link: Python -> ML
        link = NoteLink(
            source_document_id=notes["python"].id,
            target_document_id=notes["ml"].id,
            link_text="Machine Learning",
        )
        db_session.add(link)
        db_session.commit()

        # Verify initial state
        links = (
            db_session.query(NoteLink)
            .filter_by(source_document_id=notes["python"].id)
            .all()
        )
        assert len(links) == 1

        # Clear old links (simulate content change)
        db_session.query(NoteLink).filter_by(
            source_document_id=notes["python"].id
        ).delete()

        # Add new links (Python now only links to AI)
        new_link = NoteLink(
            source_document_id=notes["python"].id,
            target_document_id=notes["ai"].id,
            link_text="Artificial Intelligence",
        )
        db_session.add(new_link)
        db_session.commit()

        # Verify updated state
        links = (
            db_session.query(NoteLink)
            .filter_by(source_document_id=notes["python"].id)
            .all()
        )
        assert len(links) == 1
        assert links[0].target_document_id == notes["ai"].id


class TestVersioningWorkflow:
    """Integration tests for the versioning workflow."""

    def test_note_editing_creates_versions(self, db_session, note_source_type):
        """Test that editing a note creates version snapshots."""
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        # Create note
        note = Document(
            id=str(uuid.uuid4()),
            title="Initial Title",
            text_content="Initial content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("initial_edit"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

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

        # Edit note
        note.title = "Updated Title"
        note.text_content = "Updated content with more details"
        db_session.commit()

        # Create version for edit
        v2 = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Updated Title",
            content="Updated content with more details",
            change_type="manual_save",
            change_summary="Added more details",
            content_hash=NoteService._compute_content_hash(
                "Updated content with more details"
            ),
        )
        db_session.add(v2)
        db_session.commit()

        # Verify versions
        versions = (
            db_session.query(NoteVersion)
            .filter_by(document_id=note.id)
            .order_by(NoteVersion.created_at)
            .all()
        )
        assert len(versions) == 2
        assert versions[0].title == "Initial Title"
        assert versions[1].title == "Updated Title"

    def test_version_restore_workflow(self, db_session, note_source_type):
        """Test restoring a note to a previous version."""
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        # Create note
        note = Document(
            id=str(uuid.uuid4()),
            title="Version 3 Title",
            text_content="Version 3 content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("versioned_note"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        # Create version history
        version_ids = []
        for i in range(1, 4):
            vid = str(uuid.uuid4())
            version_ids.append(vid)
            version = NoteVersion(
                id=vid,
                document_id=note.id,
                title=f"Version {i} Title",
                content=f"Version {i} content",
                change_type="manual_save" if i > 1 else "initial",
                content_hash=NoteService._compute_content_hash(
                    f"Version {i} content"
                ),
            )
            db_session.add(version)

        db_session.commit()

        # Restore to version 1
        v1 = db_session.query(NoteVersion).filter_by(id=version_ids[0]).first()

        note.title = v1.title
        note.text_content = v1.content
        db_session.commit()

        # A raw-ORM restore snapshot with the SAME content as an existing
        # version is rejected by the UNIQUE (document_id, content_hash)
        # constraint. NoteService handles this deduplication gracefully;
        # here we just verify the constraint fires.
        from sqlalchemy.exc import IntegrityError

        restore_version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title=v1.title,
            content=v1.content,
            change_type="restore",
            change_summary="Restored to version 1",
            content_hash=NoteService._compute_content_hash(v1.content),
        )
        db_session.add(restore_version)
        with pytest.raises(IntegrityError):
            db_session.commit()
        db_session.rollback()

        # Verify restore
        db_session.refresh(note)
        assert note.title == "Version 1 Title"
        assert note.text_content == "Version 1 content"

        # Verify version history — 3 original versions, no duplicate for
        # the identical-content restore.
        versions = (
            db_session.query(NoteVersion).filter_by(document_id=note.id).all()
        )
        assert len(versions) == 3

    def test_content_hash_prevents_duplicate_auto_saves(
        self, db_session, note_source_type
    ):
        """Test that content hash prevents duplicate auto-saves."""
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note = Document(
            id=str(uuid.uuid4()),
            title="Auto-save Test",
            text_content="Same content every time",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("auto_save_test"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        content_hash = NoteService._compute_content_hash(
            "Same content every time"
        )

        # First auto-save
        v1 = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=note.id,
            title="Auto-save Test",
            content="Same content every time",
            change_type="auto_save",
            content_hash=content_hash,
        )
        db_session.add(v1)
        db_session.commit()

        # Check for duplicate before second auto-save
        existing = (
            db_session.query(NoteVersion)
            .filter_by(document_id=note.id, content_hash=content_hash)
            .first()
        )

        # Should find existing version
        assert existing is not None
        assert existing.id == v1.id


class TestSynthesisWorkflow:
    """Integration tests for note synthesis workflow."""

    @pytest.fixture
    def setup_research_notes(self, db_session, note_source_type):
        """Set up research notes for synthesis testing."""
        notes = [
            Document(
                id=str(uuid.uuid4()),
                title="Research Findings - Study A",
                text_content="Study A found that machine learning improves accuracy by 20%.",
                tags=["research", "ml"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("study_a"),
                source_type_id=note_source_type.id,
            ),
            Document(
                id=str(uuid.uuid4()),
                title="Research Findings - Study B",
                text_content="Study B showed deep learning outperforms traditional ML by 15%.",
                tags=["research", "dl"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("study_b"),
                source_type_id=note_source_type.id,
            ),
            Document(
                id=str(uuid.uuid4()),
                title="Research Findings - Study C",
                text_content="Study C concluded neural networks work best for image recognition.",
                tags=["research", "neural-networks"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("study_c"),
                source_type_id=note_source_type.id,
            ),
        ]

        for note in notes:
            db_session.add(note)
        db_session.commit()

        return notes

    def test_merge_synthesis_workflow(
        self, db_session, setup_research_notes, note_source_type
    ):
        """Test the complete merge synthesis workflow."""
        notes = setup_research_notes

        # Create merged note
        merged_content = """
# Merged Research Findings

## Study A
Machine learning improves accuracy by 20%.

## Study B
Deep learning outperforms traditional ML by 15%.

## Study C
Neural networks work best for image recognition.
"""
        merged_note = Document(
            id=str(uuid.uuid4()),
            title="Combined Research Findings",
            text_content=merged_content,
            tags=["research", "synthesis"],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("merged_note"),
            source_type_id=note_source_type.id,
        )
        db_session.add(merged_note)
        db_session.commit()

        # Create synthesis record
        synthesis = NoteSynthesis(
            result_document_id=merged_note.id,
            synthesis_type="merge",
        )
        synthesis.sources = [
            NoteSynthesisSource(source_document_id=n.id) for n in notes
        ]
        db_session.add(synthesis)
        db_session.commit()

        # Verify synthesis
        saved = db_session.query(NoteSynthesis).first()
        assert saved.synthesis_type == "merge"
        assert len(saved.sources) == 3
        assert saved.result_document.title == "Combined Research Findings"

    def test_compare_synthesis_workflow(
        self, db_session, setup_research_notes, note_source_type
    ):
        """Test the compare synthesis workflow."""
        notes = setup_research_notes[:2]  # Compare only first two

        comparison_content = """
# Comparison: Study A vs Study B

## Similarities
- Both studies focus on machine learning techniques

## Differences
- Study A: ML improves accuracy by 20%
- Study B: DL outperforms ML by 15%

## Conclusions
Deep learning shows better performance than traditional ML.
"""
        comparison_note = Document(
            id=str(uuid.uuid4()),
            title="Study A vs Study B Comparison",
            text_content=comparison_content,
            tags=["research", "comparison"],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("comparison_note"),
            source_type_id=note_source_type.id,
        )
        db_session.add(comparison_note)
        db_session.commit()

        synthesis = NoteSynthesis(
            result_document_id=comparison_note.id,
            synthesis_type="compare",
        )
        synthesis.sources = [
            NoteSynthesisSource(source_document_id=n.id) for n in notes
        ]
        db_session.add(synthesis)
        db_session.commit()

        saved = db_session.query(NoteSynthesis).first()
        assert saved.synthesis_type == "compare"
        assert len(saved.sources) == 2

    def test_synthesis_preserves_source_notes(
        self, db_session, setup_research_notes, note_source_type
    ):
        """Test that synthesis doesn't affect source notes."""
        notes = setup_research_notes
        original_contents = {n.id: n.text_content for n in notes}

        # Create synthesis
        merged_note = Document(
            id=str(uuid.uuid4()),
            title="Merged",
            text_content="Merged content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("preserves_merged"),
            source_type_id=note_source_type.id,
        )
        db_session.add(merged_note)
        db_session.commit()

        synthesis = NoteSynthesis(
            result_document_id=merged_note.id,
            synthesis_type="merge",
        )
        synthesis.sources = [
            NoteSynthesisSource(source_document_id=n.id) for n in notes
        ]
        db_session.add(synthesis)
        db_session.commit()

        # Verify source notes unchanged
        for note in notes:
            db_session.refresh(note)
            assert note.text_content == original_contents[note.id]


class TestSearchAndDiscovery:
    """Integration tests for search and discovery features."""

    @pytest.fixture
    def setup_searchable_notes(self, db_session, note_source_type):
        """Set up notes for search testing."""
        notes = [
            Document(
                id=str(uuid.uuid4()),
                title="Introduction to Machine Learning",
                text_content="This note covers the basics of ML including supervised learning.",
                tags=["ml", "basics", "tutorial"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("search_ml_intro"),
                source_type_id=note_source_type.id,
            ),
            Document(
                id=str(uuid.uuid4()),
                title="Advanced Machine Learning Techniques",
                text_content="Deep dive into advanced ML topics like ensemble methods.",
                tags=["ml", "advanced"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("search_ml_adv"),
                source_type_id=note_source_type.id,
            ),
            Document(
                id=str(uuid.uuid4()),
                title="Python for Data Science",
                text_content="How to use Python for data analysis and visualization.",
                tags=["python", "data-science"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("search_python"),
                source_type_id=note_source_type.id,
            ),
            Document(
                id=str(uuid.uuid4()),
                title="Neural Networks Explained",
                text_content="Understanding the architecture of neural networks.",
                tags=["ml", "neural-networks"],
                file_type="note",
                file_size=0,
                document_hash=_generate_hash("search_nn"),
                source_type_id=note_source_type.id,
            ),
        ]

        for note in notes:
            db_session.add(note)
        db_session.commit()

        return notes

    def test_search_by_title(self, db_session, setup_searchable_notes):
        """Test searching notes by title."""
        results = (
            db_session.query(Document)
            .filter(Document.title.ilike("%Machine Learning%"))
            .all()
        )

        assert len(results) == 2

    def test_search_by_content(self, db_session, setup_searchable_notes):
        """Test searching notes by content."""
        results = (
            db_session.query(Document)
            .filter(Document.text_content.ilike("%neural networks%"))
            .all()
        )

        # Should find "Neural Networks Explained"
        assert len(results) == 1
        assert "Neural Networks" in results[0].title

    def test_search_by_tag(self, db_session, setup_searchable_notes):
        """Test filtering notes by tag."""
        # SQLite doesn't support JSON array contains directly,
        # so we use a string-based approach
        results = (
            db_session.query(Document).filter(Document.tags.isnot(None)).all()
        )

        # Filter in Python for tags containing "ml"
        ml_notes = [n for n in results if n.tags and "ml" in n.tags]
        assert len(ml_notes) == 3

    def test_search_for_linking_autocomplete(
        self, db_session, setup_searchable_notes
    ):
        """Test search for [[link]] autocomplete."""
        query = "Machine"

        results = (
            db_session.query(Document)
            .filter(Document.title.ilike(f"%{query}%"))
            .order_by(Document.updated_at.desc())
            .limit(5)
            .all()
        )

        assert len(results) == 2
        assert all("Machine" in r.title for r in results)

    def test_exclude_current_note_from_search(
        self, db_session, setup_searchable_notes
    ):
        """Test excluding current note from search results."""
        notes = setup_searchable_notes
        current_note_id = notes[0].id

        results = (
            db_session.query(Document)
            .filter(Document.title.ilike("%Machine%"))
            .filter(Document.id != current_note_id)
            .all()
        )

        # Should find only the other ML note
        assert len(results) == 1
        assert results[0].id != current_note_id


class TestAIServiceIntegration:
    """Integration tests for AI service functionality."""

    def test_change_summary_workflow(self):
        """Test the change summary workflow with mock LLM."""
        from local_deep_research.research_library.notes.services.note_ai_service import (
            NoteAIService,
        )

        service = NoteAIService("test_user")

        # Test no change case
        result = asyncio.run(
            service.summarize_changes(
                "Original content here", "Original content here"
            )
        )
        assert result == "No changes"

        # Test with changes (mock LLM)
        mock_llm = MagicMock()
        mock_response = MagicMock()
        mock_response.content = "Added a new paragraph about testing."
        mock_llm.ainvoke = AsyncMock(return_value=mock_response)

        service._llm = mock_llm

        result = asyncio.run(
            service.summarize_changes(
                "Original content",
                "Original content with new paragraph about testing",
            )
        )

        assert "testing" in result.lower()

    def test_semantic_diff_workflow(self):
        """Test the semantic diff workflow."""
        from local_deep_research.research_library.notes.services.note_ai_service import (
            NoteAIService,
        )

        service = NoteAIService("test_user")

        # Test no change
        diff = asyncio.run(
            service.semantic_diff("Same content", "Same content")
        )
        assert diff["unchanged"] is True

        # Test with changes (mock LLM)
        mock_llm = MagicMock()
        mock_response = MagicMock()
        mock_response.content = """
        {
            "added": ["New section on testing"],
            "removed": [],
            "modified": ["Introduction updated"],
            "summary": "Added testing section and updated intro"
        }
        """
        mock_llm.ainvoke = AsyncMock(return_value=mock_response)

        service._llm = mock_llm

        diff = asyncio.run(
            service.semantic_diff("Old content", "New content with additions")
        )

        assert diff["unchanged"] is False
        assert "added" in diff


class TestRestoreWithBookends:
    """Regression guards for ``NoteService.restore_with_bookends``.

    Earlier the route orchestrated PRE_RESTORE + content update + RESTORE
    across three separate sessions. ``update_note`` also auto-created a
    MANUAL_SAVE snapshot, producing 3 NoteVersion rows per restore. This
    test pins the new behaviour: exactly 2 snapshots, in one transaction.
    """

    @pytest.fixture
    def patched_session(self, db_session, monkeypatch):
        """Yield the test db_session whenever NoteService opens a per-user one."""
        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_restore_inserts_pre_restore_marker_no_phantom_manual_save(
        self, patched_session, note_source_type
    ):
        """The restore flow inserts a PRE_RESTORE snapshot of the
        current state and applies the version content — atomically.

        Note on dedup semantics: the RESTORE snapshot that bookends the
        flow has the same content as the version being restored to (by
        definition), so the (document_id, content_hash) UNIQUE constraint
        causes ``_create_version_snapshot_in_session`` to return the
        existing version's id without inserting a new row. The PRE_RESTORE
        marker is what makes the restore visible in version history.

        This test guards two real properties:
          1. PRE_RESTORE row IS inserted (its content is the document's
             current state, which is unique unless we've been here before).
          2. NO new ``manual_save`` row is created — that was the old bug
             where ``update_note`` slipped an auto-snapshot between the
             bookends.
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="Current",
            text_content="current content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("restore_bookend_target"),
            source_type_id=note_source_type.id,
            tags=["a"],
        )
        patched_session.add(note)
        patched_session.commit()

        earlier_id = str(uuid.uuid4())
        earlier = NoteVersion(
            id=earlier_id,
            document_id=note_id,
            title="Earlier",
            content="earlier content",
            change_type="manual_save",
            content_hash=NoteService._compute_content_hash("earlier content"),
        )
        patched_session.add(earlier)
        patched_session.commit()

        manual_saves_before = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="manual_save")
            .count()
        )

        service = NoteService("test_user")
        success, error = service.restore_with_bookends(note_id, earlier_id)
        assert success is True, error

        # Document state is restored.
        patched_session.refresh(note)
        assert note.title == "Earlier"
        assert note.text_content == "earlier content"

        # PRE_RESTORE row exists — its content is the pre-restore document
        # state which was unique, so it lands as a fresh row.
        pre_restore_count = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="pre_restore")
            .count()
        )
        assert pre_restore_count == 1

        # No phantom manual_save: the old bug auto-snapshotted between
        # the bookends. The new code suppresses that.
        manual_saves_after = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="manual_save")
            .count()
        )
        assert manual_saves_after == manual_saves_before

    def test_restore_returns_note_not_found_for_missing_note(
        self, patched_session
    ):
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        service = NoteService("test_user")
        success, error = service.restore_with_bookends(
            str(uuid.uuid4()), str(uuid.uuid4())
        )
        assert success is False
        assert error == "note_not_found"

    def test_restore_returns_version_not_found_for_missing_version(
        self, patched_session, note_source_type
    ):
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="x",
            text_content="x",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("restore_no_version"),
            source_type_id=note_source_type.id,
        )
        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"

    def test_restore_round_trip_back_and_forth(
        self, patched_session, note_source_type
    ):
        """Restore v1 then v2 in sequence — both must take effect, and
        no phantom ``manual_save`` rows may appear between bookends.

        Regression guard: the old ``update_note``-driven restore path
        slipped a MANUAL_SAVE snapshot between PRE_RESTORE and RESTORE.
        After the bookends refactor, restoring shouldn't ever produce a
        manual_save row.
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="Current",
            text_content="current",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("restore_round_trip"),
            source_type_id=note_source_type.id,
            tags=["a"],
        )
        patched_session.add(note)
        patched_session.commit()

        v1_id = str(uuid.uuid4())
        v1 = NoteVersion(
            id=v1_id,
            document_id=note_id,
            title="Title One",
            content="one",
            change_type="manual_save",
            content_hash=NoteService._compute_content_hash("one"),
        )
        v2_id = str(uuid.uuid4())
        v2 = NoteVersion(
            id=v2_id,
            document_id=note_id,
            title="Title Two",
            content="two",
            change_type="manual_save",
            content_hash=NoteService._compute_content_hash("two"),
        )
        patched_session.add_all([v1, v2])
        patched_session.commit()

        manual_saves_before = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="manual_save")
            .count()
        )

        service = NoteService("test_user")

        # Restore to v1 → document becomes "one".
        success, error = service.restore_with_bookends(note_id, v1_id)
        assert success is True, error
        patched_session.refresh(note)
        assert note.text_content == "one"

        # Restore to v2 → document becomes "two".
        success, error = service.restore_with_bookends(note_id, v2_id)
        assert success is True, error
        patched_session.refresh(note)
        assert note.text_content == "two"

        # The bug we fixed: no fresh ``manual_save`` rows landed across
        # either restore. PRE_RESTORE / RESTORE rows are fine and may be
        # deduplicated by the (document_id, content_hash) UNIQUE
        # constraint depending on intermediate content.
        manual_saves_after = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="manual_save")
            .count()
        )
        assert manual_saves_after == manual_saves_before

    def test_restore_to_current_content_is_idempotent(
        self, patched_session, note_source_type
    ):
        """Restoring to a version whose content already matches the
        document is a no-op content-wise, but must still succeed and
        must not create a phantom ``manual_save`` row.

        Edge case: PRE_RESTORE snapshot's content equals the existing
        version's content, so the (document_id, content_hash) UNIQUE
        constraint dedups it — the snapshot helper must handle that
        gracefully instead of raising.
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note_id = str(uuid.uuid4())
        same_content = "identical"
        note = Document(
            id=note_id,
            title="Same",
            text_content=same_content,
            file_type="note",
            file_size=len(same_content),
            document_hash=_generate_hash("restore_idempotent"),
            source_type_id=note_source_type.id,
            tags=["a"],
        )
        patched_session.add(note)
        patched_session.commit()

        v1_id = str(uuid.uuid4())
        v1 = NoteVersion(
            id=v1_id,
            document_id=note_id,
            title="Same",
            content=same_content,
            tags=["a"],
            change_type="manual_save",
            content_hash=NoteService._compute_content_hash(same_content),
        )
        patched_session.add(v1)
        patched_session.commit()

        manual_saves_before = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="manual_save")
            .count()
        )

        service = NoteService("test_user")
        success, error = service.restore_with_bookends(note_id, v1_id)
        assert success is True, error

        # Document still has the same content (and v1's title/tags,
        # which here are also identical).
        patched_session.refresh(note)
        assert note.text_content == same_content

        # No phantom manual_save row — the no-op restore must not slip
        # one in.
        manual_saves_after = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="manual_save")
            .count()
        )
        assert manual_saves_after == manual_saves_before

    def test_pre_restore_row_stores_current_state_not_target_version_state(
        self, patched_session, note_source_type
    ):
        """The PRE_RESTORE bookend must snapshot the state being REPLACED
        (the document's *current* title/content/tags), not the state being
        restored TO (the target version's fields).

        Restoring conceptually means "save where we are, then jump back".
        The PRE_RESTORE row is the "save where we are" half — it's the only
        way a user can undo a restore. If it captured the target version's
        fields instead, the audit row would be a useless duplicate of the
        RESTORE row and the pre-restore state would be lost forever.

        Regression guard for note_service.py ~L1849-1857: the PRE_RESTORE
        ``_create_version_snapshot_in_session`` call passes
        ``document.title / document.text_content / document.tags``. If that
        were reverted to pass the target version's ``v_title / v_content /
        v_tags`` (a tempting copy-paste from the RESTORE call below it),
        the PRE_RESTORE row would read "Old Title"/"old content"/["old"]
        and these assertions would FAIL.
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        # Distinct current ("now") state vs target ("old") state so the two
        # are never confusable in the assertions below.
        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="Now Title",
            text_content="now content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("pre_restore_state_current"),
            source_type_id=note_source_type.id,
            tags=["now-a", "now-b"],
        )
        patched_session.add(note)
        patched_session.commit()

        target_id = str(uuid.uuid4())
        target = NoteVersion(
            id=target_id,
            document_id=note_id,
            title="Old Title",
            content="old content",
            tags=["old"],
            change_type="manual_save",
            content_hash=NoteService._compute_version_hash(
                "Old Title", "old content", ["old"]
            ),
        )
        patched_session.add(target)
        patched_session.commit()

        service = NoteService("test_user")
        success, error = service.restore_with_bookends(note_id, target_id)
        assert success is True, error

        pre_restore = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="pre_restore")
            .one()
        )

        # The PRE_RESTORE row captures the state we just left (V_now)...
        assert pre_restore.title == "Now Title"
        assert pre_restore.content == "now content"
        assert pre_restore.tags == ["now-a", "now-b"]

        # ...and NOT the target version's state (V_old). These are the
        # assertions that flip red if the snapshot args are swapped.
        assert pre_restore.title != "Old Title"
        assert pre_restore.content != "old content"

        # Sanity: the document itself did move to the target state.
        patched_session.refresh(note)
        assert note.title == "Old Title"
        assert note.text_content == "old content"

    def test_restore_rolls_back_pre_restore_row_when_restore_bookend_fails(
        self, patched_session, note_source_type
    ):
        """If the RESTORE bookend write blows up mid-flow, the WHOLE op must
        roll back: no orphan PRE_RESTORE row may persist and the document
        content must be untouched.

        ``restore_with_bookends`` flushes PRE_RESTORE, mutates the document,
        then writes the RESTORE bookend — all before the single
        ``session.commit()`` at the end. Atomicity comes from that ordering:
        a failure short-circuits to the ``except`` block (returning
        ``internal_error``) WITHOUT ever committing, so the flushed-but-
        uncommitted PRE_RESTORE row and document mutations are discarded
        when the (uncommitted) transaction is rolled back.

        Regression guard for note_service.py ~L1899/L1906: if someone made
        the PRE_RESTORE durable independently — e.g. inserting a
        ``session.commit()`` after the PRE_RESTORE snapshot — the orphan row
        would survive the rollback and ``pre_restore_count`` below would be
        1, failing this test. (Verified manually: committing PRE_RESTORE
        early leaves ``pre_restore_count == 1`` after rollback.)
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
            NoteChangeType,
        )

        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="Now Title",
            text_content="now content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("restore_rollback_doc"),
            source_type_id=note_source_type.id,
            tags=["now"],
        )
        patched_session.add(note)
        patched_session.commit()

        target_id = str(uuid.uuid4())
        target = NoteVersion(
            id=target_id,
            document_id=note_id,
            title="Old Title",
            content="old content",
            tags=["old"],
            change_type="manual_save",
            content_hash=NoteService._compute_version_hash(
                "Old Title", "old content", ["old"]
            ),
        )
        patched_session.add(target)
        patched_session.commit()

        service = NoteService("test_user")

        # Fail only the RESTORE bookend write, AFTER the PRE_RESTORE flush
        # and the document mutation have already happened in-session. This
        # is the worst case for atomicity: the most state is pending.
        real_snapshot = service._create_version_snapshot_in_session

        def fail_on_restore_bookend(
            session,
            nid,
            title,
            content,
            tags,
            change_type,
            change_summary=None,
        ):
            if change_type == NoteChangeType.RESTORE.value:
                raise RuntimeError("simulated RESTORE bookend write failure")
            return real_snapshot(
                session,
                nid,
                title,
                content,
                tags,
                change_type,
                change_summary,
            )

        service._create_version_snapshot_in_session = fail_on_restore_bookend

        success, error = service.restore_with_bookends(note_id, target_id)
        assert success is False
        assert error == "internal_error"

        # The flow never reached ``session.commit()``, so the pending
        # PRE_RESTORE row + document mutation are uncommitted. A real
        # per-user request session is discarded without commit on this
        # error path; roll back to reproduce that lifecycle, then prove
        # nothing leaked.
        patched_session.rollback()

        pre_restore_count = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="pre_restore")
            .count()
        )
        assert pre_restore_count == 0, "orphan PRE_RESTORE row survived"

        # Document is untouched — the failed restore did not partially apply.
        patched_session.refresh(note)
        assert note.title == "Now Title"
        assert note.text_content == "now content"

    def test_double_restore_to_same_version_writes_distinct_bookend_rows(
        self, patched_session, note_source_type
    ):
        """Restoring to the SAME version twice (with no edit in between) must
        produce two distinct PRE_RESTORE rows and two distinct RESTORE rows
        — four audit rows total, none deduped away.

        After the first restore the document already holds the target
        content, so the second restore's PRE_RESTORE state-hash AND its
        RESTORE state-hash both collide with rows the first restore wrote.
        The (document_id, content_hash) UNIQUE constraint + dedup pre-check
        would silently drop them — erasing the "user restored here again"
        signal — were it not for the per-row uuid4 salt mixed into the
        bookend hash.

        Regression guard for note_service.py ~L1649-1655: the salt branch
        for PRE_RESTORE / RESTORE change types guarantees a unique hash per
        row. Removing it (falling back to the plain
        ``_compute_version_hash``) makes the second restore dedup against
        the first — collapsing to 1 PRE_RESTORE / 0 RESTORE rows, failing
        the ``== 2`` assertions below. (Verified manually: with the salt
        removed this scenario yields pre_restore=1, restore=0.)
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="Now Title",
            text_content="now content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("double_restore_doc"),
            source_type_id=note_source_type.id,
            tags=["now"],
        )
        patched_session.add(note)
        patched_session.commit()

        target_id = str(uuid.uuid4())
        target = NoteVersion(
            id=target_id,
            document_id=note_id,
            title="Target Title",
            content="target content",
            tags=["target"],
            change_type="manual_save",
            content_hash=NoteService._compute_version_hash(
                "Target Title", "target content", ["target"]
            ),
        )
        patched_session.add(target)
        patched_session.commit()

        service = NoteService("test_user")

        # Restore to the same version twice in a row — no edit between, so
        # the second restore's bookend states are byte-identical to the
        # first's. Only the salt keeps them from colliding.
        success, error = service.restore_with_bookends(note_id, target_id)
        assert success is True, error
        success, error = service.restore_with_bookends(note_id, target_id)
        assert success is True, error

        pre_restore_count = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="pre_restore")
            .count()
        )
        restore_count = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="restore")
            .count()
        )
        assert pre_restore_count == 2, (
            "second PRE_RESTORE deduped — per-row salt missing"
        )
        assert restore_count == 2, "RESTORE rows deduped — per-row salt missing"

        # Each bookend row has a distinct content_hash (salt does its job).
        bookend_hashes = [
            row.content_hash
            for row in patched_session.query(NoteVersion)
            .filter(
                NoteVersion.document_id == note_id,
                NoteVersion.change_type.in_(["pre_restore", "restore"]),
            )
            .all()
        ]
        assert len(bookend_hashes) == len(set(bookend_hashes)) == 4

    def test_restore_with_bookends_rejects_version_from_another_note_same_user(
        self, patched_session, note_source_type
    ):
        """The version lookup is scoped to ``(document_id, id)`` — a version
        that belongs to a DIFFERENT note in the SAME DB must not be
        restorable onto note A.

        ``test_restore_returns_version_not_found_for_missing_version`` only
        proves a random UUID (that exists on no note) is rejected. This test
        is stronger: ``versionB_id`` is a REAL version row — it just lives
        under note B. Per-user DB isolation can't protect this (both notes
        are in one DB); only the ``document_id`` predicate in the lookup does.

        Regression guard for note_service.py ~L1836-1840: if the lookup is
        refactored to ``filter_by(id=version_id)`` alone (dropping the
        ``document_id=note_id`` predicate), restore would clobber note A's
        title/content/tags with note B's version and write PRE_RESTORE /
        RESTORE bookends onto note A. The assertions below flip red the
        moment that predicate is dropped.
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        noteA_id = str(uuid.uuid4())
        noteB_id = str(uuid.uuid4())

        noteA = Document(
            id=noteA_id,
            title="A current",
            text_content="A content",
            tags=["a"],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("xnote-A"),
            source_type_id=note_source_type.id,
        )
        noteB = Document(
            id=noteB_id,
            title="B current",
            text_content="B content",
            tags=["b"],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("xnote-B"),
            source_type_id=note_source_type.id,
        )

        # A real version row — but it belongs to note B, not note A.
        versionB_id = str(uuid.uuid4())
        versionB = NoteVersion(
            id=versionB_id,
            document_id=noteB_id,
            title="B old",
            content="B old content",
            change_type="manual_save",
            content_hash=NoteService._compute_content_hash("B old content"),
        )
        patched_session.add_all([noteA, noteB, versionB])
        patched_session.commit()

        service = NoteService("test_user")
        success, error = service.restore_with_bookends(noteA_id, versionB_id)

        # Cross-note restore is rejected as if the version didn't exist.
        assert success is False
        assert error == "version_not_found"

        # Note A is completely unchanged.
        patched_session.refresh(noteA)
        assert noteA.title == "A current"
        assert noteA.text_content == "A content"
        assert noteA.tags == ["a"]

        # And no bookend rows leaked onto note A.
        assert (
            patched_session.query(NoteVersion)
            .filter_by(document_id=noteA_id, change_type="pre_restore")
            .count()
            == 0
        )
        assert (
            patched_session.query(NoteVersion)
            .filter_by(document_id=noteA_id, change_type="restore")
            .count()
            == 0
        )

    def test_restore_with_bookends_replays_tags_onto_document(
        self, patched_session, note_source_type
    ):
        """A successful restore must replay the target version's tags onto
        the document — not just its title/content.

        Every other successful-restore test uses identical or unasserted
        tags between current and version, so a dropped tag assignment would
        pass them silently. Here the current tags and the version tags
        DIFFER, and tags are asserted on the post-restore document, so this
        is the test that catches a regression.

        Regression guard for note_service.py ~L1862 (``document.tags =
        v_tags``) and the bookend tag snapshots (~L1849-1857 PRE_RESTORE,
        ~L1872-1880 RESTORE): dropping the document tag replay or
        snapshotting the wrong tag list into either bookend flips the
        assertions below.
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="Current",
            text_content="current",
            tags=["current-tag-a", "current-tag-b"],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("restore_tags"),
            source_type_id=note_source_type.id,
        )
        patched_session.add(note)
        patched_session.commit()

        v_id = str(uuid.uuid4())
        version = NoteVersion(
            id=v_id,
            document_id=note_id,
            title="Old Title",
            content="old content",
            tags=["old-tag-x", "old-tag-y"],
            change_type="manual_save",
            content_hash=NoteService._compute_content_hash("old content"),
        )
        patched_session.add(version)
        patched_session.commit()

        service = NoteService("test_user")
        success, error = service.restore_with_bookends(note_id, v_id)
        assert success is True, error

        # The version's tags (and title/content) are replayed onto the
        # document. ``document.tags`` is the core, dedup-immune guard.
        patched_session.refresh(note)
        assert note.tags == ["old-tag-x", "old-tag-y"]
        assert note.text_content == "old content"
        assert note.title == "Old Title"

        # The PRE_RESTORE bookend snapshots the state we left (current tags).
        pre_restore = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="pre_restore")
            .first()
        )
        assert pre_restore is not None
        assert pre_restore.tags == ["current-tag-a", "current-tag-b"]

        # The RESTORE bookend (always inserted — salted hash) snapshots the
        # state we jumped to (version tags). If for any reason it were
        # deduped away, the document-tags + PRE_RESTORE assertions above
        # remain the load-bearing guards.
        restore_row = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="restore")
            .first()
        )
        if restore_row is not None:
            assert restore_row.tags == ["old-tag-x", "old-tag-y"]

    def test_restore_survives_post_commit_reparse_failure(
        self, patched_session, note_source_type, monkeypatch
    ):
        """Fix B (restore durability): a ``_parse_and_update_links`` failure
        AFTER the durable commit must still return ``(True, None)``, because
        the restore has already committed.

        ``restore_with_bookends`` commits PRE_RESTORE + content update +
        RESTORE, then re-parses [[wiki-links]] OUTSIDE that transaction. The
        reparse is non-critical: a failure there must NOT report
        ``(False, "internal_error")`` for a restore that already landed
        durably — that contradicts the method's contract and would make the
        UI show a failure for a change the user can see took effect.

        The fix wraps the post-commit reparse in its own try/except.

        Revert target: moving the reparse back inside the OUTER try (no inner
        try/except) lets the failure fall into the outer ``except``, returning
        ``(False, "internal_error")`` — the ``(True, None)`` assertion below
        flips red. (The document content + RESTORE bookend assertions stay
        green either way because the commit happened before the reparse, so
        they pin the "actually committed" half of the contract.)
        """
        from local_deep_research.research_library.notes.services.note_service import (
            NoteService,
        )

        note_id = str(uuid.uuid4())
        note = Document(
            id=note_id,
            title="Current",
            text_content="current content",
            tags=["current"],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("restore_reparse_fail"),
            source_type_id=note_source_type.id,
        )
        patched_session.add(note)
        patched_session.commit()

        target_id = str(uuid.uuid4())
        target = NoteVersion(
            id=target_id,
            document_id=note_id,
            title="Old Title",
            content="old content with [[a link]]",
            tags=["old"],
            change_type="manual_save",
            content_hash=NoteService._compute_version_hash(
                "Old Title", "old content with [[a link]]", ["old"]
            ),
        )
        patched_session.add(target)
        patched_session.commit()

        service = NoteService("test_user")

        # Make ONLY the post-commit reparse blow up. It runs after the
        # transaction's single commit, so the restore is already durable
        # by the time this raises.
        def boom_reparse(*args, **kwargs):
            raise RuntimeError("simulated post-commit reparse failure")

        monkeypatch.setattr(
            NoteService,
            "_parse_and_update_links",
            boom_reparse,
        )

        success, error = service.restore_with_bookends(note_id, target_id)

        # Contract: durable commit happened, so the reparse failure is
        # swallowed and the call reports success.
        assert success is True, (
            f"post-commit reparse failure leaked as a restore failure: "
            f"error={error!r}"
        )
        assert error is None

        # The restore actually committed: the document holds the restored
        # version's content...
        patched_session.refresh(note)
        assert note.text_content == "old content with [[a link]]"
        assert note.title == "Old Title"

        # ...and the RESTORE bookend row landed.
        restore_count = (
            patched_session.query(NoteVersion)
            .filter_by(document_id=note_id, change_type="restore")
            .count()
        )
        assert restore_count == 1, (
            "RESTORE bookend missing — restore did not commit before the "
            "reparse failure"
        )


class TestTagValidation:
    """Backend tag validation — frontend cap is bypassable via direct API."""

    def test_validate_tags_accepts_none_and_empty(self):
        from local_deep_research.research_library.notes.services.note_service import (
            _validate_tags,
        )

        _validate_tags(None)  # no-op for "don't update tags"
        _validate_tags([])  # empty list is fine

    def test_validate_tags_rejects_non_list(self):
        from local_deep_research.research_library.notes.services.note_service import (
            _validate_tags,
        )

        with pytest.raises(ValueError, match="must be a list"):
            _validate_tags("not-a-list")  # type: ignore[arg-type]
        with pytest.raises(ValueError, match="must be a list"):
            _validate_tags({"k": "v"})  # type: ignore[arg-type]

    def test_validate_tags_rejects_non_string_entries(self):
        from local_deep_research.research_library.notes.services.note_service import (
            _validate_tags,
        )

        with pytest.raises(ValueError, match="must be a string"):
            _validate_tags(["ok", 42, "also-ok"])  # type: ignore[list-item]

    def test_validate_tags_rejects_too_long(self):
        from local_deep_research.research_library.notes.services.note_service import (
            MAX_TAG_LENGTH,
            _validate_tags,
        )

        # Boundary: exactly MAX_TAG_LENGTH passes, +1 fails.
        _validate_tags(["x" * MAX_TAG_LENGTH])
        with pytest.raises(ValueError, match="exceeds maximum length"):
            _validate_tags(["x" * (MAX_TAG_LENGTH + 1)])


class TestSynthesisSourceTypeFilter:
    """Synthesis must only accept Documents with source_type='note'.

    Previously, ``session.query(Document).filter_by(id=note_id)`` would
    return any Document type — uploaded PDFs, research results — even
    though the route is meant to operate over notes.
    """

    @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_ai_service.get_user_db_session",
            fake_session,
        )
        # `_get_note_source_type_id` looks up by name in the session.
        return db_session

    def test_synthesize_skips_non_note_documents(
        self, patched_session, note_source_type
    ):
        from local_deep_research.database.models import SourceType
        from local_deep_research.research_library.notes.services.note_ai_service import (
            NoteAIService,
        )

        # Two real notes
        note_a = Document(
            id=str(uuid.uuid4()),
            title="Note A",
            text_content="alpha",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("synth_note_a"),
            source_type_id=note_source_type.id,
        )
        note_b = Document(
            id=str(uuid.uuid4()),
            title="Note B",
            text_content="beta",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("synth_note_b"),
            source_type_id=note_source_type.id,
        )
        # One non-note: a research download with the same shape
        research_st = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research download",
            description="Document downloaded from a research run",
            icon="file-pdf",
        )
        patched_session.add(research_st)
        patched_session.commit()

        non_note = Document(
            id=str(uuid.uuid4()),
            title="Some PDF",
            text_content="gamma",
            file_type="pdf",
            file_size=0,
            document_hash=_generate_hash("synth_non_note"),
            source_type_id=research_st.id,
        )
        patched_session.add_all([note_a, note_b, non_note])
        patched_session.commit()

        service = NoteAIService("test_user")
        # Mock the LLM so the test doesn't need a real provider
        mock_llm = MagicMock()
        mock_response = MagicMock()
        mock_response.content = "merged content"
        mock_llm.ainvoke = AsyncMock(return_value=mock_response)
        service._llm = mock_llm

        result = asyncio.run(
            service.synthesize_notes(
                [note_a.id, non_note.id, note_b.id], synthesis_type="merge"
            )
        )

        # The synthesis should succeed using the two notes — and the
        # non-note should NOT appear in source_notes.
        assert result.get("success") is True, result.get("error")
        source_ids = {n["id"] for n in result["source_notes"]}
        assert source_ids == {note_a.id, note_b.id}
        assert non_note.id not in source_ids

    def test_synthesis_truncated_sources_false_for_short_notes(
        self, patched_session, note_source_type
    ):
        """Two notes both well under the 1000-char per-note cap — the
        ``truncated_sources`` flag must stay False so the UI doesn't
        falsely warn the user that content was clipped.
        """
        from local_deep_research.research_library.notes.services.note_ai_service import (
            NoteAIService,
        )

        note_a = Document(
            id=str(uuid.uuid4()),
            title="Short A",
            text_content="alpha content well under the cap",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("synth_short_a"),
            source_type_id=note_source_type.id,
        )
        note_b = Document(
            id=str(uuid.uuid4()),
            title="Short B",
            text_content="beta content also well under the cap",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("synth_short_b"),
            source_type_id=note_source_type.id,
        )
        patched_session.add_all([note_a, note_b])
        patched_session.commit()

        service = NoteAIService("test_user")
        mock_llm = MagicMock()
        mock_response = MagicMock()
        mock_response.content = "merged content"
        mock_llm.ainvoke = AsyncMock(return_value=mock_response)
        service._llm = mock_llm

        result = asyncio.run(
            service.synthesize_notes(
                [note_a.id, note_b.id], synthesis_type="merge"
            )
        )

        assert result.get("success") is True, result.get("error")
        assert result["truncated_sources"] is False

    def test_synthesis_truncated_sources_true_when_any_note_exceeds_cap(
        self, patched_session, note_source_type
    ):
        """One short note + one over the per-note cap must flip
        ``truncated_sources`` to True. Even a single over-cap source means the
        LLM saw clipped text — the flag is OR-across-sources, not AND.

        The per-note budget is ``SYNTHESIS_TOTAL_CONTENT_CHARS // len(notes)``
        (24000 // 2 = 12000 for the two notes here), so the long note must
        exceed 12000 chars, not the old flat 1000.
        """
        from local_deep_research.research_library.notes.services.note_ai_service import (
            NoteAIService,
        )

        note_short = Document(
            id=str(uuid.uuid4()),
            title="Short",
            text_content="brief content under the cap",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("synth_mixed_short"),
            source_type_id=note_source_type.id,
        )
        # Length 13000 — strictly above the per-note cap (24000 // 2 = 12000
        # for the two notes in this test).
        long_content = "x" * 13000
        note_long = Document(
            id=str(uuid.uuid4()),
            title="Long",
            text_content=long_content,
            file_type="note",
            file_size=len(long_content),
            document_hash=_generate_hash("synth_mixed_long"),
            source_type_id=note_source_type.id,
        )
        patched_session.add_all([note_short, note_long])
        patched_session.commit()

        service = NoteAIService("test_user")
        mock_llm = MagicMock()
        mock_response = MagicMock()
        mock_response.content = "merged content"
        mock_llm.ainvoke = AsyncMock(return_value=mock_response)
        service._llm = mock_llm

        result = asyncio.run(
            service.synthesize_notes(
                [note_short.id, note_long.id], synthesis_type="merge"
            )
        )

        assert result.get("success") is True, result.get("error")
        assert result["truncated_sources"] is True