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.

8.0 KB
Raw Blame
"""Tests for migration 0022_add_note_references + the NoteReference model.

The reference layer: note → (document | research) rows with optional
quote anchors, powering the document/research Notes panels and the
Word-review-style inline comments. The three-way parity between the
model CHECK, the migration CHECK, and behavior follows the house style
established by the note_syntheses enum CHECK in 0021.
"""

import uuid

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import (
    Document,
    NoteReference,
    ResearchHistory,
    SourceType,
)
from local_deep_research.database.models.base import Base


@pytest.fixture
def engine():
    eng = create_engine("sqlite:///:memory:")

    # FK enforcement matches the app (PRAGMA foreign_keys=ON everywhere).
    from sqlalchemy import event

    @event.listens_for(eng, "connect")
    def _fk_on(dbapi_conn, _):
        dbapi_conn.execute("PRAGMA foreign_keys=ON")

    Base.metadata.create_all(eng)
    try:
        yield eng
    finally:
        eng.dispose()


@pytest.fixture
def session(engine):
    s = sessionmaker(bind=engine)()
    yield s
    s.close()


def _source_type(session):
    st = session.query(SourceType).filter_by(name="note").first()
    if st is None:
        st = SourceType(id=str(uuid.uuid4()), name="note", display_name="Note")
        session.add(st)
        session.commit()
    return st


def _note(session, title="A note"):
    doc = Document(
        id=str(uuid.uuid4()),
        title=title,
        text_content="body",
        file_type="note",
        file_size=4,
        document_hash=uuid.uuid4().hex,
        source_type_id=_source_type(session).id,
    )
    session.add(doc)
    session.commit()
    return doc


def _research(session):
    rh = ResearchHistory(
        id=str(uuid.uuid4()),
        query="q",
        mode="quick",
        status="completed",
        created_at="2026-07-04T00:00:00+00:00",
    )
    session.add(rh)
    session.commit()
    return rh


class TestSingleTargetCheck:
    """Model CHECK: exactly one of the two targets."""

    def test_document_target_ok(self, session):
        note, target = _note(session), _note(session, "target doc")
        session.add(
            NoteReference(note_id=note.id, target_document_id=target.id)
        )
        session.commit()

    def test_research_target_ok(self, session):
        note, rh = _note(session), _research(session)
        session.add(
            NoteReference(
                note_id=note.id,
                target_research_id=rh.id,
                quote="a passage",
            )
        )
        session.commit()

    def test_no_target_rejected(self, session):
        from sqlalchemy.exc import IntegrityError

        session.add(NoteReference(note_id=_note(session).id))
        with pytest.raises(IntegrityError):
            session.commit()
        session.rollback()

    def test_both_targets_rejected(self, session):
        from sqlalchemy.exc import IntegrityError

        note, target, rh = (
            _note(session),
            _note(session, "t"),
            _research(session),
        )
        session.add(
            NoteReference(
                note_id=note.id,
                target_document_id=target.id,
                target_research_id=rh.id,
            )
        )
        with pytest.raises(IntegrityError):
            session.commit()
        session.rollback()


class TestCascades:
    def test_deleting_the_note_cascades_the_reference(self, session):
        note, target = _note(session), _note(session, "target")
        session.add(
            NoteReference(
                note_id=note.id, target_document_id=target.id, quote="q"
            )
        )
        session.commit()
        session.delete(note)
        session.commit()
        assert session.query(NoteReference).count() == 0

    def test_deleting_the_target_cascades_the_reference(self, session):
        note, rh = _note(session), _research(session)
        session.add(NoteReference(note_id=note.id, target_research_id=rh.id))
        session.commit()
        session.delete(rh)
        session.commit()
        assert session.query(NoteReference).count() == 0

    def test_deleting_the_target_document_cascades_the_reference(self, session):
        note, target = _note(session), _note(session, "target doc")
        session.add(
            NoteReference(note_id=note.id, target_document_id=target.id)
        )
        session.commit()
        session.delete(target)
        session.commit()
        assert session.query(NoteReference).count() == 0


class TestModelMigrationParity:
    """The migration's CHECK expression must match the model's — drift
    means fresh installs (migration) and tests (metadata.create_all)
    enforce different rules."""

    def test_check_constraint_parity(self):
        import re
        from pathlib import Path

        import local_deep_research.database.migrations.versions as versions

        migration = (
            Path(versions.__file__).parent / "0022_add_note_references.py"
        ).read_text()
        model_checks = [
            str(c.sqltext)
            for c in NoteReference.__table__.constraints
            if c.name == "ck_note_reference_single_target"
        ]
        assert model_checks, "model CHECK missing"
        normalized_model = re.sub(r"\s+", " ", model_checks[0]).strip()
        # The migration carries the same expression. Its source splits the
        # SQL across adjacent string literals, so drop the quote characters
        # before collapsing whitespace.
        migration_normalized = re.sub(r"\s+", " ", migration.replace('"', ""))
        assert (
            "(target_document_id IS NOT NULL) + (target_research_id IS NOT NULL) = 1"
            in migration_normalized
        )
        assert (
            normalized_model
            == "(target_document_id IS NOT NULL) + (target_research_id IS NOT NULL) = 1"
        )


class TestMigration0022HeadAlignment:
    """Head-alignment guard (moved from the 0019 test file per the repo
    convention of keeping this in the newest migration's test).

    The assertion tracks the latest migration file dynamically (instead of
    a hardcoded revision id), so it no longer needs to be relocated every
    time a new migration lands here. 0023 (drop uix_chunk_collection) and
    0024 (sqlite_autoincrement on document_chunks) advance the head past
    0022 without requiring any change to this test.
    """

    def test_head_revision_matches_latest_migration_file(self):
        from local_deep_research.database.alembic_runner import (
            get_head_revision,
            get_migrations_dir,
        )

        versions_dir = get_migrations_dir() / "versions"
        revision_ids = sorted(
            p.name[:4]
            for p in versions_dir.glob("*.py")
            if p.name[:4].isdigit()
        )
        assert revision_ids, "no migration files found in versions dir"
        assert get_head_revision() == revision_ids[-1]

    def test_single_head_no_branches(self):
        """The revision graph must never branch into multiple heads."""
        from alembic.config import Config
        from alembic.script import ScriptDirectory

        from local_deep_research.database.alembic_runner import (
            get_migrations_dir,
        )

        config = Config()
        config.set_main_option("script_location", str(get_migrations_dir()))
        script = ScriptDirectory.from_config(config)
        heads = script.get_heads()
        assert len(heads) == 1, f"expected a single head, found {heads}"

    def test_0022_chains_correctly_to_0021(self):
        from alembic.config import Config
        from alembic.script import ScriptDirectory

        from local_deep_research.database.alembic_runner import (
            get_migrations_dir,
        )

        config = Config()
        config.set_main_option("script_location", str(get_migrations_dir()))
        script = ScriptDirectory.from_config(config)
        assert script.get_revision("0022").down_revision == "0021"