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.

21.7 KB
Raw Blame
"""Edge case tests for Notes functionality."""

import uuid

import pytest

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

from tests.notes.helpers import _generate_hash


class TestNoteEdgeCases:
    """Test edge cases for Note model."""

    def test_note_with_very_long_title(self, db_session, note_source_type):
        """Test note with title at max length (500 chars)."""
        long_title = "A" * 500
        note = Document(
            id=str(uuid.uuid4()),
            title=long_title,
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("long_title_content"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert len(saved.title) == 500

    def test_note_with_very_long_content(self, db_session, note_source_type):
        """Test note with very long content."""
        long_content = "Content " * 10000  # ~80000 chars
        note = Document(
            id=str(uuid.uuid4()),
            title="Long Content Note",
            text_content=long_content,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash(long_content),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert len(saved.text_content) > 70000

    def test_note_with_unicode_content(self, db_session, note_source_type):
        """Test note with unicode/emoji content."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Unicode 世界 🌍 Мир",
            text_content="Content with émojis 🎉 and ünïcödé characters 日本語",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("unicode_content"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert "🌍" in saved.title
        assert "日本語" in saved.text_content

    def test_note_with_special_characters_in_tags(
        self, db_session, note_source_type
    ):
        """Test note with special characters in tags."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Special Tags",
            text_content="Content",
            tags=["c++", "c#", "node.js", "machine-learning", "AI/ML"],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("special_tags"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert "c++" in saved.tags
        assert "AI/ML" in saved.tags

    def test_note_with_empty_tags_list(self, db_session, note_source_type):
        """Test note with empty tags list."""
        note = Document(
            id=str(uuid.uuid4()),
            title="No Tags",
            text_content="Content",
            tags=[],
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("no_tags"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert saved.tags == []

    def test_note_with_null_tags(self, db_session, note_source_type):
        """Test note with null tags."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Null Tags",
            text_content="Content",
            tags=None,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("null_tags"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert saved.tags is None

    def test_note_with_many_tags(self, db_session, note_source_type):
        """Test note with many tags."""
        many_tags = [f"tag{i}" for i in range(100)]
        note = Document(
            id=str(uuid.uuid4()),
            title="Many Tags",
            text_content="Content",
            tags=many_tags,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("many_tags"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert len(saved.tags) == 100

    def test_note_with_markdown_content(self, db_session, note_source_type):
        """Test note with markdown formatting."""
        markdown_content = """
# Heading 1
## Heading 2

**Bold text** and *italic text*

- List item 1
- List item 2

```python
def hello():
    print("Hello, World!")
```

| Column 1 | Column 2 |
|----------|----------|
| Cell 1   | Cell 2   |

> Blockquote

[Link](https://example.com)
"""
        note = Document(
            id=str(uuid.uuid4()),
            title="Markdown Note",
            text_content=markdown_content,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash(markdown_content),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert "```python" in saved.text_content
        assert "# Heading 1" in saved.text_content

    def test_note_with_html_content(self, db_session, note_source_type):
        """Test note with HTML content (should be stored as-is)."""
        html_content = """
<h1>Title</h1>
<p>Paragraph with <strong>bold</strong> text.</p>
<script>alert('xss')</script>
"""
        note = Document(
            id=str(uuid.uuid4()),
            title="HTML Note",
            text_content=html_content,
            file_type="note",
            file_size=0,
            document_hash=_generate_hash(html_content),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).first()
        assert (
            "<script>" in saved.text_content
        )  # Stored as-is, sanitization is app's job

    def test_note_with_special_title(self, db_session, note_source_type):
        """Test note with special characters in title."""
        note = Document(
            id=str(uuid.uuid4()),
            title="2024 Research Report v2.0 - 日本語",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("special_title"),
            source_type_id=note_source_type.id,
        )
        db_session.add(note)
        db_session.commit()

        saved = db_session.query(Document).filter_by(title=note.title).first()
        assert saved is not None
        assert "2024" in saved.title
        assert "日本語" in saved.title

    def test_multiple_notes_same_title_allowed(
        self, db_session, note_source_type
    ):
        """Test that duplicate titles are allowed (no unique constraint)."""
        note1 = Document(
            id=str(uuid.uuid4()),
            title="Same Title",
            text_content="Content 1",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("note_one"),
            source_type_id=note_source_type.id,
        )
        note2 = Document(
            id=str(uuid.uuid4()),
            title="Same Title",
            text_content="Content 2",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("note_two"),
            source_type_id=note_source_type.id,
        )
        db_session.add_all([note1, note2])
        db_session.commit()

        notes = db_session.query(Document).filter_by(title="Same Title").all()
        assert len(notes) == 2


class TestNoteLinkEdgeCases:
    """Test edge cases for NoteLink model."""

    @pytest.fixture
    def setup_notes(self, db_session, note_source_type):
        """Set up test notes."""
        notes = [
            Document(
                id=str(uuid.uuid4()),
                title=f"Note {i}",
                text_content=f"Content {i}",
                file_type="note",
                file_size=0,
                document_hash=_generate_hash(f"note_{i}"),
                source_type_id=note_source_type.id,
            )
            for i in range(5)
        ]
        for note in notes:
            db_session.add(note)
        db_session.commit()
        return notes

    def test_note_with_many_outgoing_links(self, db_session, setup_notes):
        """Test note with many outgoing links."""
        source = setup_notes[0]

        for target in setup_notes[1:]:
            link = NoteLink(
                source_document_id=source.id,
                target_document_id=target.id,
                link_text=target.title,
            )
            db_session.add(link)

        db_session.commit()

        links = (
            db_session.query(NoteLink)
            .filter_by(source_document_id=source.id)
            .all()
        )
        assert len(links) == 4

    def test_note_with_many_incoming_links(self, db_session, setup_notes):
        """Test note with many incoming links (backlinks)."""
        target = setup_notes[0]

        for source in setup_notes[1:]:
            link = NoteLink(
                source_document_id=source.id,
                target_document_id=target.id,
                link_text=target.title,
            )
            db_session.add(link)

        db_session.commit()

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

    def test_link_text_with_special_characters(self, db_session, setup_notes):
        """Test link text with special characters."""
        link = NoteLink(
            source_document_id=setup_notes[0].id,
            target_document_id=setup_notes[1].id,
            link_text="Note: A Test! @#$%^&*()",
        )
        db_session.add(link)
        db_session.commit()

        saved = db_session.query(NoteLink).first()
        assert "@#$%^&*()" in saved.link_text

    def test_link_text_with_unicode(self, db_session, setup_notes):
        """Test link text with unicode characters."""
        link = NoteLink(
            source_document_id=setup_notes[0].id,
            target_document_id=setup_notes[1].id,
            link_text="機械学習 🤖 Машинное обучение",
        )
        db_session.add(link)
        db_session.commit()

        saved = db_session.query(NoteLink).first()
        assert "機械学習" in saved.link_text

    def test_circular_links(self, db_session, setup_notes):
        """Test circular links (A -> B -> C -> A)."""
        # A -> B
        link1 = NoteLink(
            source_document_id=setup_notes[0].id,
            target_document_id=setup_notes[1].id,
            link_text="B",
        )
        # B -> C
        link2 = NoteLink(
            source_document_id=setup_notes[1].id,
            target_document_id=setup_notes[2].id,
            link_text="C",
        )
        # C -> A (circular)
        link3 = NoteLink(
            source_document_id=setup_notes[2].id,
            target_document_id=setup_notes[0].id,
            link_text="A",
        )

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

        # All links should exist
        assert db_session.query(NoteLink).count() == 3

    def test_bidirectional_links(self, db_session, setup_notes):
        """Test bidirectional links (A <-> B)."""
        # A -> B
        link1 = NoteLink(
            source_document_id=setup_notes[0].id,
            target_document_id=setup_notes[1].id,
            link_text="B",
        )
        # B -> A
        link2 = NoteLink(
            source_document_id=setup_notes[1].id,
            target_document_id=setup_notes[0].id,
            link_text="A",
        )

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

        # Both links should exist
        assert db_session.query(NoteLink).count() == 2

    def test_delete_target_removes_incoming_links(
        self, db_session, setup_notes
    ):
        """Test that deleting a note removes incoming links."""
        source, target = setup_notes[0], setup_notes[1]

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

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

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


class TestNoteVersionEdgeCases:
    """Test edge cases for NoteVersion model."""

    @pytest.fixture
    def sample_note(self, db_session, note_source_type):
        """Create a sample note."""
        note = Document(
            id=str(uuid.uuid4()),
            title="Versioned Note",
            text_content="Initial 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()
        return note

    def test_many_versions(self, db_session, sample_note):
        """Test note with many versions."""
        for i in range(1, 101):
            version = NoteVersion(
                id=str(uuid.uuid4()),
                document_id=sample_note.id,
                title=f"Title v{i}",
                content=f"Content v{i}",
                change_type="manual_save",
                content_hash=f"hash{i}",
            )
            db_session.add(version)

        db_session.commit()

        versions = (
            db_session.query(NoteVersion)
            .filter_by(document_id=sample_note.id)
            .all()
        )
        assert len(versions) == 100

    def test_version_with_very_long_change_summary(
        self, db_session, sample_note
    ):
        """Test version with long change summary."""
        long_summary = "This change includes " + ("many updates " * 500)

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=sample_note.id,
            title="Title",
            content="Content",
            change_type="manual_save",
            change_summary=long_summary,
            content_hash="hash",
        )
        db_session.add(version)
        db_session.commit()

        saved = db_session.query(NoteVersion).first()
        assert len(saved.change_summary) > 5000

    def test_version_with_null_change_summary(self, db_session, sample_note):
        """Test version with null change summary."""
        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=sample_note.id,
            title="Title",
            content="Content",
            change_type="initial",
            change_summary=None,
            content_hash="hash",
        )
        db_session.add(version)
        db_session.commit()

        saved = db_session.query(NoteVersion).first()
        assert saved.change_summary is None

    def test_version_content_with_binary_like_data(
        self, db_session, sample_note
    ):
        """Test version content with binary-like string data."""
        # Simulate content that might look like binary
        binary_like = "".join(chr(i) for i in range(32, 127))

        version = NoteVersion(
            id=str(uuid.uuid4()),
            document_id=sample_note.id,
            title="Binary-like",
            content=binary_like,
            change_type="manual_save",
            content_hash="hash",
        )
        db_session.add(version)
        db_session.commit()

        saved = db_session.query(NoteVersion).first()
        assert saved.content == binary_like

    def test_version_ordering_by_created_at(self, db_session, sample_note):
        """Test that versions can be ordered by created_at."""
        version_ids = []
        for i in range(1, 6):
            vid = str(uuid.uuid4())
            version_ids.append(vid)
            version = NoteVersion(
                id=vid,
                document_id=sample_note.id,
                title=f"Title v{i}",
                content=f"Content v{i}",
                change_type="manual_save",
                content_hash=f"hash{i}",
            )
            db_session.add(version)

        db_session.commit()

        # Query with ordering
        versions = (
            db_session.query(NoteVersion)
            .filter_by(document_id=sample_note.id)
            .order_by(NoteVersion.created_at.desc())
            .all()
        )

        assert len(versions) == 5
        # All version IDs should be unique
        assert len(set(v.id for v in versions)) == 5


class TestNoteSynthesisEdgeCases:
    """Test edge cases for NoteSynthesis model."""

    def test_synthesis_without_result_note(self, db_session):
        """Test synthesis record without result note (preview mode)."""
        synthesis = NoteSynthesis(
            result_document_id=None,
            synthesis_type="merge",
        )
        db_session.add(synthesis)
        db_session.commit()

        saved = db_session.query(NoteSynthesis).first()
        assert saved.result_document_id is None
        assert saved.sources == []

    def test_synthesis_with_max_source_notes(
        self, db_session, note_source_type
    ):
        """Test synthesis with 5 source notes (maximum)."""
        result = Document(
            id=str(uuid.uuid4()),
            title="Merged",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("merged_result"),
            source_type_id=note_source_type.id,
        )
        source_docs = [
            Document(
                id=str(uuid.uuid4()),
                title=f"src {i}",
                text_content=f"c{i}",
                file_type="note",
                file_size=1,
                document_hash=_generate_hash(f"max_src_{i}"),
                source_type_id=note_source_type.id,
            )
            for i in range(5)
        ]
        db_session.add_all([result] + source_docs)
        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_synthesis_tracks_source_ordering(
        self, db_session, note_source_type
    ):
        """Test that source notes are all retained (no guarantee on order)."""
        result = Document(
            id=str(uuid.uuid4()),
            title="Result",
            text_content="Content",
            file_type="note",
            file_size=0,
            document_hash=_generate_hash("ordering_result"),
            source_type_id=note_source_type.id,
        )
        source_docs = [
            Document(
                id=str(uuid.uuid4()),
                title=label,
                text_content="x",
                file_type="note",
                file_size=1,
                document_hash=_generate_hash(f"ord_{label}"),
                source_type_id=note_source_type.id,
            )
            for label in ("first", "second", "third")
        ]
        db_session.add_all([result] + source_docs)
        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) == 3
        source_ids = {s.source_document_id for s in saved.sources}
        assert source_ids == {d.id for d in source_docs}

    def test_multiple_syntheses_from_same_sources(
        self, db_session, note_source_type
    ):
        """Test multiple syntheses from the same source notes."""
        source_docs = [
            Document(
                id=str(uuid.uuid4()),
                title=f"src {i}",
                text_content=f"c{i}",
                file_type="note",
                file_size=1,
                document_hash=_generate_hash(f"repeat_src_{i}"),
                source_type_id=note_source_type.id,
            )
            for i in range(2)
        ]
        db_session.add_all(source_docs)
        db_session.commit()

        for i in range(3):
            result = Document(
                id=str(uuid.uuid4()),
                title=f"Result {i}",
                text_content="Content",
                file_type="note",
                file_size=0,
                document_hash=_generate_hash(f"multi_result_{i}"),
                source_type_id=note_source_type.id,
            )
            db_session.add(result)
            db_session.commit()

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

        db_session.commit()

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