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.

23.1 KB
Raw Blame
"""Destructive library operations must distinguish URL-equivalent PDF versions."""

import hashlib
import uuid
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import Mock

import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import Session

from local_deep_research.constants import FILE_PATH_SENTINELS
from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import DownloadTracker
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentBlob,
    DocumentCollection,
    DocumentStatus,
    SourceType,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.research_library.services.library_service import (
    LibraryService,
)
from local_deep_research.research_library.services.pdf_storage_manager import (
    PDFStorageManager,
)
from local_deep_research.research_library.utils import (
    document_awaits_pdf,
    get_absolute_path_from_settings,
    get_legacy_url_hashes,
    get_url_hash,
)

LIBRARY_MODULE = "local_deep_research.research_library.services.library_service"
OLD_URL = "https://example.org/paper.pdf?utm_source=old"
NEW_URL = "https://example.org/paper.pdf"


@pytest.fixture
def library(tmp_path, monkeypatch):
    engine = create_engine("sqlite:///:memory:")

    @event.listens_for(engine, "connect")
    def enable_foreign_keys(connection, _):
        connection.execute("PRAGMA foreign_keys=ON")

    Base.metadata.create_all(engine)
    with Session(engine) as session:
        source = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Download",
        )
        collection = Collection(id=str(uuid.uuid4()), name="Library")
        research = ResearchHistory(
            id=str(uuid.uuid4()),
            query="PDF versions",
            mode="quick",
            status="completed",
            created_at="2026-01-01T00:00:00",
        )
        session.add_all([source, collection, research])
        session.commit()

        @contextmanager
        def scoped_session(*args, **kwargs):
            yield session

        settings = Mock()
        settings.get_setting.side_effect = lambda key, default=None: {
            "research_library.storage_path": str(tmp_path),
            "research_library.shared_library": False,
        }.get(key, default)
        monkeypatch.setattr(
            f"{LIBRARY_MODULE}.get_user_db_session", scoped_session
        )
        monkeypatch.setattr(
            f"{LIBRARY_MODULE}.get_settings_manager", lambda **kw: settings
        )
        monkeypatch.setattr(
            "local_deep_research.utilities.db_utils.get_settings_manager",
            lambda: settings,
        )
        # Exercise the real per-user path resolver. A colliding file in the
        # old shared root must stay untouched now that no legacy fallback
        # exists (#7268).
        root = get_absolute_path_from_settings(
            "", "identity_test", settings_manager=settings
        )
        root.mkdir(parents=True)
        purge = Mock()
        monkeypatch.setattr(
            "local_deep_research.research_library.deletion.services.document_deletion.DocumentDeletionService._purge_document_rag",
            purge,
        )

        def add_document(url, name, *, legacy=False, with_tracker=True):
            content = f"%PDF-1.4 {name}".encode()
            resource = ResearchResource(
                research_id=research.id,
                title=name,
                url=url,
                source_type="academic",
                created_at="2026-01-01T00:00:00",
            )
            session.add(resource)
            session.flush()
            doc = Document(
                id=str(uuid.uuid4()),
                source_type_id=source.id,
                resource_id=resource.id,
                research_id=research.id,
                document_hash=hashlib.sha256(content).hexdigest(),
                original_url=url,
                title=name,
                file_path=name,
                file_size=len(content),
                file_type="pdf",
                storage_mode="filesystem",
                status=DocumentStatus.COMPLETED,
                text_content=f"Extracted text from {name}",
            )
            session.add(doc)
            session.flush()
            session.add(
                DocumentCollection(
                    document_id=doc.id, collection_id=collection.id
                )
            )
            tracker = None
            if with_tracker:
                tracker = DownloadTracker(
                    url=url,
                    url_hash=get_legacy_url_hashes(url)[1]
                    if legacy
                    else get_url_hash(url),
                    first_resource_id=resource.id,
                    file_path=name,
                    file_hash=doc.document_hash,
                    is_downloaded=True,
                )
                session.add(tracker)
            session.commit()
            (root / name).write_bytes(content)
            return doc, tracker

        yield SimpleNamespace(
            session=session,
            service=LibraryService("identity_test"),
            root=root,
            shared_root=tmp_path,
            add=add_document,
            purge=purge,
        )
    engine.dispose()


@pytest.mark.parametrize("missing", ["old.pdf", "new.pdf", None])
@pytest.mark.parametrize("has_text", [False, True])
def test_sync_checks_each_documents_own_file(library, missing, has_text):
    old, old_tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    new, new_tracker = library.add(NEW_URL, "new.pdf")
    if not has_text:
        old.text_content = new.text_content = None
        library.session.commit()
    ids = {old.id: "old.pdf", new.id: "new.pdf"}
    tracker_ids = {old_tracker.id: "old.pdf", new_tracker.id: "new.pdf"}
    if missing:
        (library.root / missing).unlink()

    stats = library.service.sync_library_with_filesystem()
    library.session.expunge_all()

    for doc_id, name in ids.items():
        surviving = library.session.get(Document, doc_id)
        # A missing PDF removes only a document that holds nothing else;
        # extracted text keeps the document, which then awaits its PDF.
        removed = name == missing and not has_text
        assert (surviving is None) == removed
        membership = (
            library.session.query(DocumentCollection)
            .filter_by(document_id=doc_id)
            .first()
        )
        assert (membership is None) == removed
        if surviving:
            assert surviving.text_content == (
                f"Extracted text from {name}" if has_text else None
            )
            assert document_awaits_pdf(surviving) == (name == missing)
    for tracker_id, name in tracker_ids.items():
        actual = library.session.get(DownloadTracker, tracker_id)
        assert actual.is_downloaded == (name != missing)
        assert actual.file_path == (None if name == missing else name)
    assert stats["files_found"] == (1 if missing else 2)
    assert stats["files_missing"] == stats["trackers_updated"] == bool(missing)
    assert [entry["path"] for entry in stats["missing_files"]] == (
        [missing] if missing else []
    )
    assert library.purge.call_count == bool(missing and not has_text)


def test_delete_only_unlinks_and_resets_requested_legacy_document(library):
    old, old_tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    new, new_tracker = library.add(NEW_URL, "new.pdf")
    old_id, new_id = old.id, new.id

    assert library.service.delete_document(old_id)
    library.session.expire_all()

    assert library.session.get(Document, old_id) is None
    assert (
        library.session.get(Document, new_id).text_content
        == "Extracted text from new.pdf"
    )
    assert not (library.root / "old.pdf").exists()
    assert (library.root / "new.pdf").read_bytes() == b"%PDF-1.4 new.pdf"
    assert old_tracker.file_path is None
    assert old_tracker.is_downloaded is False
    assert new_tracker.file_path == "new.pdf"
    assert new_tracker.is_downloaded is True
    library.purge.assert_called_once_with(old_id, [], full_delete=True)


@pytest.mark.parametrize("operation", ["sync", "delete"])
def test_document_path_wins_over_stale_matching_tracker_path(
    library, operation
):
    doc, tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    tracker.file_path = "unrelated.pdf"
    unrelated = library.root / "unrelated.pdf"
    unrelated.write_bytes(b"another saved document")
    doc_id = doc.id
    library.session.commit()

    if operation == "sync":
        unrelated.unlink()
        stats = library.service.sync_library_with_filesystem()
        library.session.expire_all()
        assert stats["files_found"] == 1
        assert library.session.get(Document, doc_id) is not None
        assert (library.root / "old.pdf").exists()
        assert tracker.is_downloaded is True
    else:
        assert library.service.delete_document(doc_id)
        assert not (library.root / "old.pdf").exists()
        assert unrelated.read_bytes() == b"another saved document"


@pytest.mark.parametrize("mode", ["database", "none", None])
def test_delete_uses_own_file_despite_stale_storage_mode(library, mode):
    old, old_tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    new, new_tracker = library.add(NEW_URL, "new.pdf")
    old.storage_mode = mode
    old_id, new_id = old.id, new.id
    library.session.commit()

    assert library.service.delete_document(old_id)
    library.session.expire_all()

    assert library.session.get(Document, old_id) is None
    assert not (library.root / "old.pdf").exists()
    assert old_tracker.is_downloaded is False
    assert library.session.get(Document, new_id) is not None
    assert (library.root / "new.pdf").read_bytes() == b"%PDF-1.4 new.pdf"
    assert new_tracker.is_downloaded is True


@pytest.mark.parametrize(
    "mode,has_blob,keep_file",
    [
        ("database", True, True),
        ("database", False, False),
        ("none", False, True),
        ("filesystem", True, True),
        (None, True, True),
    ],
)
def test_delete_borrows_tracker_path_only_for_missing_legacy_storage(
    library, mode, has_blob, keep_file
):
    doc, tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    doc.storage_mode = mode
    doc.file_path = None
    doc_id = doc.id
    if has_blob:
        library.session.add(
            DocumentBlob(
                document_id=doc_id,
                pdf_binary=b"%PDF-1.4 old.pdf",
                blob_hash=doc.document_hash,
            )
        )
    library.session.commit()

    assert library.service.delete_document(doc_id)
    library.session.expire_all()

    assert library.session.get(Document, doc_id) is None
    assert library.session.get(DocumentBlob, doc_id) is None
    assert (library.root / "old.pdf").exists() == keep_file
    assert tracker.is_downloaded is False


@pytest.mark.parametrize("mode", ["filesystem", "database", "none"])
def test_sync_preserves_own_storage_without_a_tracker(library, mode):
    doc, _ = library.add(OLD_URL, "old.pdf", with_tracker=False)
    if mode == "database":
        doc.storage_mode = "database"
        doc.file_path = "database"
        library.session.add(
            DocumentBlob(
                document_id=doc.id,
                pdf_binary=b"%PDF-1.4 old.pdf",
                blob_hash=doc.document_hash,
            )
        )
    elif mode == "none":
        doc.storage_mode = "none"
        doc.file_path = None
    doc_id = doc.id
    library.session.commit()

    stats = library.service.sync_library_with_filesystem()
    library.session.expire_all()

    assert (
        library.session.get(Document, doc_id).text_content
        == "Extracted text from old.pdf"
    )
    assert stats["files_found"] == 1
    assert stats["files_missing"] == 0
    library.purge.assert_not_called()
    if mode == "database":
        assert (
            library.session.get(DocumentBlob, doc_id).pdf_binary
            == b"%PDF-1.4 old.pdf"
        )


@pytest.mark.parametrize("mode", ["database", None])
@pytest.mark.parametrize("sentinel", FILE_PATH_SENTINELS)
def test_sync_preserves_sentinels_independently_of_storage_mode(
    library, mode, sentinel
):
    doc, _ = library.add(OLD_URL, "old.pdf", with_tracker=False)
    (library.root / "old.pdf").unlink()
    doc.storage_mode = mode
    doc.file_path = sentinel
    doc_id = doc.id
    library.session.commit()

    stats = library.service.sync_library_with_filesystem()
    library.session.expire_all()

    assert (
        library.session.get(Document, doc_id).text_content
        == "Extracted text from old.pdf"
    )
    assert stats["files_found"] == 1
    assert stats["files_missing"] == 0
    library.purge.assert_not_called()


@pytest.mark.parametrize("own_path", [True, False])
def test_sync_checks_disk_when_database_label_has_no_blob(library, own_path):
    doc, tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    doc.storage_mode = "database"
    if not own_path:
        doc.file_path = None
    doc_id = doc.id
    library.session.commit()
    if own_path:
        # The production reader supports this metadata/storage mismatch.
        manager = PDFStorageManager(
            library.root, "database", username="identity_test"
        )
        assert manager.load_pdf(doc, library.session) == b"%PDF-1.4 old.pdf"

    stats = library.service.sync_library_with_filesystem()
    library.session.expire_all()

    assert (
        library.session.get(Document, doc_id).text_content
        == "Extracted text from old.pdf"
    )
    assert (
        library.session.query(DocumentCollection)
        .filter_by(document_id=doc_id)
        .count()
        == 1
    )
    assert tracker.is_downloaded is True
    assert stats["files_found"] == 1
    assert stats["files_missing"] == 0
    library.purge.assert_not_called()


@pytest.mark.parametrize("mode", ["filesystem", None])
def test_sync_checks_blob_when_filesystem_label_has_no_file(library, mode):
    doc, tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    doc.storage_mode = mode
    (library.root / "old.pdf").unlink()
    doc_id = doc.id
    library.session.add(
        DocumentBlob(
            document_id=doc_id,
            pdf_binary=b"%PDF-1.4 old.pdf",
            blob_hash=doc.document_hash,
        )
    )
    library.session.commit()

    stats = library.service.sync_library_with_filesystem()
    library.session.expire_all()

    assert (
        library.session.get(DocumentBlob, doc_id).pdf_binary
        == b"%PDF-1.4 old.pdf"
    )
    assert (
        library.session.get(Document, doc_id).text_content
        == "Extracted text from old.pdf"
    )
    assert tracker.is_downloaded is True
    assert stats["files_found"] == 1
    assert stats["files_missing"] == 0
    library.purge.assert_not_called()


@pytest.mark.parametrize("has_text", [False, True])
def test_sync_removes_document_only_when_no_content_remains(library, has_text):
    """Both declared backends are missing: no blob, and no file on disk."""
    doc, tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    doc.storage_mode = "database"
    if not has_text:
        doc.text_content = None
    (library.root / "old.pdf").unlink()
    doc_id = doc.id
    library.session.commit()

    stats = library.service.sync_library_with_filesystem()
    library.session.expire_all()

    retained = library.session.get(Document, doc_id)
    assert (retained is not None) == has_text
    assert tracker.is_downloaded is False
    assert stats["files_missing"] == stats["trackers_updated"] == 1
    if has_text:
        assert retained.text_content == "Extracted text from old.pdf"
        assert document_awaits_pdf(retained)
        library.purge.assert_not_called()
    else:
        library.purge.assert_called_once_with(doc_id, [], full_delete=True)


@pytest.mark.parametrize(
    "identity",
    ["hash", "link", "unknown", "conflicting_hash", "conflicting_link"],
)
@pytest.mark.parametrize("has_text", [False, True])
@pytest.mark.parametrize("missing_path", [None, ""])
def test_legacy_tracker_fallback_requires_unambiguous_document_identity(
    library, identity, has_text, missing_path
):
    old, old_tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    new, new_tracker = library.add(NEW_URL, "new.pdf")
    old_id, new_id = old.id, new.id
    # Pre-upgrade row with no stored Document path. A verified tracker can
    # identify its missing file; a URL alone cannot establish that identity.
    old.file_path = missing_path
    old.storage_mode = None
    if not has_text:
        old.text_content = None
    if identity == "link":
        old_tracker.file_hash = None
        old_tracker.library_document_id = old.id
    elif identity == "unknown":
        old_tracker.file_hash = None
    elif identity == "conflicting_hash":
        old_tracker.library_document_id = old.id
        old_tracker.file_hash = new.document_hash
    elif identity == "conflicting_link":
        old_tracker.library_document_id = new.id
    (library.root / "old.pdf").unlink()
    library.session.commit()

    stats = library.service.sync_library_with_filesystem()
    library.session.expire_all()

    identified = identity in ("hash", "link")
    retained = library.session.get(Document, old_id)
    assert (retained is None) == (identified and not has_text)
    if retained is not None:
        # Text keeps an identified document, which then awaits its PDF; an
        # unidentified one is left exactly as it was.
        assert document_awaits_pdf(retained) == identified
        assert (
            library.session.query(DocumentCollection)
            .filter_by(document_id=old_id)
            .count()
            == 1
        )
    assert old_tracker.is_downloaded == (not identified)
    assert library.session.get(Document, new_id) is not None
    assert new_tracker.is_downloaded is True
    assert new_tracker.file_path == "new.pdf"
    assert stats["files_missing"] == int(identified)


@pytest.mark.parametrize("mode", ["filesystem", None, "database"])
@pytest.mark.parametrize("tracker", ["absent", "other_version"])
def test_sync_judges_pathless_document_by_its_database_label(
    library, mode, tracker
):
    """Without a path or an identifying tracker only a database label is storage.

    Missing or ambiguous legacy metadata cannot prove which file is absent,
    so such a document is retained. A document labelled as stored in the
    database with no blob and nothing else is removed and reported under
    the fixed ``"database"`` label. A tracker of another PDF version is
    never reset for it.
    """
    doc, url_tracker = library.add(
        OLD_URL, "old.pdf", with_tracker=tracker == "other_version"
    )
    (library.root / "old.pdf").unlink()
    doc.file_path = None
    doc.storage_mode = mode
    doc.text_content = None
    if url_tracker is not None:
        url_tracker.file_hash = hashlib.sha256(b"another version").hexdigest()
    doc_id = doc.id
    library.session.commit()

    stats = library.service.sync_library_with_filesystem()
    library.session.expire_all()

    removed = mode == "database"
    assert (library.session.get(Document, doc_id) is None) == removed
    assert (stats["files_found"], stats["files_missing"]) == (
        (0, 1) if removed else (1, 0)
    )
    assert stats["missing_files"] == (
        [{"id": doc_id, "title": "old.pdf", "path": "database", "url": OLD_URL}]
        if removed
        else []
    )
    assert stats["trackers_updated"] == 0
    if url_tracker is not None:
        assert (url_tracker.is_downloaded, url_tracker.file_path) == (
            True,
            "old.pdf",
        )
    assert library.purge.call_count == int(removed)


def test_delete_never_unlinks_legacy_shared_root(library):
    doc, tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    (library.root / "old.pdf").unlink()
    shared_file = library.shared_root / "old.pdf"
    shared_file.write_bytes(b"another user's colliding legacy file")

    assert library.service.delete_document(doc.id)

    assert shared_file.read_bytes() == b"another user's colliding legacy file"
    assert tracker.is_downloaded is False


@pytest.mark.parametrize("operation", ["delete", "remove_from_collection"])
@pytest.mark.parametrize("identity", ["hash", "link", "conflicting_hash"])
def test_primary_deletion_resets_only_matching_version_tracker(
    library, monkeypatch, operation, identity
):
    """Both deletion entry points must preserve a newer canonical tracker."""
    from local_deep_research.research_library.deletion.services import (
        document_deletion,
    )

    old, old_tracker = library.add(OLD_URL, "old.pdf", legacy=True)
    new, new_tracker = library.add(NEW_URL, "new.pdf")
    old_id, new_id = old.id, new.id
    collection_id = old.collections[0].collection_id
    if identity == "link":
        old_tracker.file_hash = None
        old_tracker.library_document_id = old.id
    elif identity == "conflicting_hash":
        old_tracker.library_document_id = old.id
        old_tracker.file_hash = new.document_hash
    library.session.commit()

    @contextmanager
    def scoped_session(*args, **kwargs):
        yield library.session

    monkeypatch.setattr(
        document_deletion, "get_user_db_session", scoped_session
    )
    service = document_deletion.DocumentDeletionService("identity_test")
    if operation == "delete":
        result = service.delete_document(old_id)
        assert result["deleted"] is True
    else:
        result = service.remove_from_collection(old_id, collection_id)
        assert result["unlinked"] is True
        assert result["document_deleted"] is True

    library.session.expire_all()
    assert library.session.get(Document, old_id) is None
    assert library.session.get(Document, new_id) is not None
    assert (library.root / "new.pdf").read_bytes() == b"%PDF-1.4 new.pdf"
    assert new_tracker.is_downloaded is True
    assert new_tracker.file_path == "new.pdf"
    matched = identity != "conflicting_hash"
    assert old_tracker.is_downloaded is (not matched)
    assert old_tracker.file_path == (None if matched else "old.pdf")


@pytest.mark.parametrize("sentinel", [*FILE_PATH_SENTINELS, "database"])
def test_delete_document_never_unlinks_a_file_named_like_a_sentinel(
    library, sentinel
):
    """A file literally named like a sentinel is the user's, not ours to unlink.

    ``delete_document`` skips the unlink when the stored path is a sentinel (or
    ``"database"``), so a user's file with one of those names in the library
    root must survive the delete. Dropping that exclusion would unlink it.
    """
    doc, tracker = library.add(OLD_URL, sentinel, legacy=True)
    doc_id = doc.id

    # A real user file whose name collides with the stored sentinel path.
    real_file = library.root / sentinel
    real_file.write_bytes(
        b"a real file that happens to be named like a sentinel"
    )

    assert library.service.delete_document(doc_id)
    library.session.expire_all()

    # The row and its tracker are reset...
    assert library.session.get(Document, doc_id) is None
    assert tracker.file_path is None
    assert tracker.is_downloaded is False
    # ...but the colliding file is still the user's.
    assert real_file.exists()
    assert real_file.read_bytes() == (
        b"a real file that happens to be named like a sentinel"
    )