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.

24.8 KB
Raw Blame
"""LARGE complete-flow test: collection deletion SAFETY with REAL DB + REAL
on-disk FAISS files.

Drives the real production stack end to end:

  * ``LibraryRAGService.index_document`` (real chunk splitting + real
    ``VectorIndex``/``FaissVectorStore``) to build a genuine ``.faiss`` file
    under ``cache/rag_indices`` and real ``DocumentChunk``/``RAGIndex`` rows,
    exactly as ``tests/vector_stores/test_no_plaintext_production_path.py``
    does.
  * ``CollectionDeletionService.delete_collection`` (the real service under
    test) against that on-disk state.

Focus is the FLOW + rollback/orphan safety of ``delete_collection`` — NOT the
low-level ``unlink_files`` file-sweep contract, which is already covered by
``tests/research_library/deletion/utils/test_cascade_helper_sweep.py``.

Covered:
  (a) HAPPY PATH — index a real collection with 2 documents, delete it,
      confirm the ``.faiss`` file is gone, ``DocumentChunk`` rows are gone,
      and the ``RAGIndex`` row is gone.
  (b) DEFERRED-UNLINK ORDER — a forced mid-transaction failure (raised
      between the RAGIndex row-delete and the commit) rolls back the DB
      change AND leaves the ``.faiss`` file on disk untouched (no
      rollback-orphan where the row is restored but the file is already
      gone). Verified via a completely FRESH session/connection against a
      file-backed sqlite DB, not just the mutated in-process session.
  (c) POST-COMMIT UNLINK FAILURE SWALLOWED — the on-disk unlink is made to
      fail (permission-denied directory) AFTER a successful commit; the
      delete must still report success and the DB rows must still be gone.
  (d) ORPHANED-DOCUMENT CROSS-COLLECTION SAFETY — a document that lives in
      TWO collections: deleting one collection must leave the other
      collection's chunks/vectors, and a real semantic search against it,
      completely untouched.

Each destructive scenario is paired with a "control" run that behaves
normally, so the injected-failure assertions are proven to actually bite
(they are not vacuously true).
"""

import uuid
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock, patch

import numpy as np
import pytest
from langchain_core.embeddings import Embeddings
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.config.paths import get_cache_directory
from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentChunk,
    DocumentCollection,
    DocumentStatus,
    RAGIndex,
    SourceType,
)
from local_deep_research.research_library.deletion.services.collection_deletion import (
    CollectionDeletionService,
)
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
)

_RAG_MOD = "local_deep_research.research_library.services.library_rag_service"
_FACADE_MOD = "local_deep_research.vector_stores.facade"
_COLLECTION_DELETION_MOD = (
    "local_deep_research.research_library.deletion.services.collection_deletion"
)

USERNAME = "flowuser"


class _FakeEmbeddings(Embeddings):
    """Deterministic, network-free embeddings (mirrors the canonical harness
    in ``tests/vector_stores/test_no_plaintext_production_path.py``)."""

    def __init__(self, dim: int = 8):
        self.dim = dim

    def _vec(self, text: str):
        rng = np.random.default_rng(abs(hash(text)) % (2**32))
        return rng.random(self.dim).tolist()

    def embed_documents(self, texts):
        return [self._vec(t) for t in texts]

    def embed_query(self, text):
        return self._vec(text)


def _make_document(
    session, *, title: str, body: str, source_type_id: str
) -> str:
    import hashlib

    document_id = uuid.uuid4().hex
    session.add(
        Document(
            id=document_id,
            source_type_id=source_type_id,
            document_hash=hashlib.sha256(body.encode()).hexdigest(),
            title=title,
            text_content=body,
            original_url=f"http://example.com/{document_id}",
            file_size=len(body),
            file_type="txt",
            status=DocumentStatus.COMPLETED,
        )
    )
    return document_id


@pytest.fixture
def flow_env(tmp_path, monkeypatch, mocker):
    """Real, file-backed sqlite DB + real on-disk cache tree.

    File-backed (not ``:memory:``) so a completely FRESH session (a new
    connection to the same file) can verify persisted state independent of
    the mutated in-process session's identity map — required for the
    rollback assertions in test (b) to be meaningful.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))

    db_path = tmp_path / "flow.db"
    engine = create_engine(f"sqlite:///{db_path}")
    Base.metadata.create_all(engine)
    SessionLocal = sessionmaker(bind=engine)
    session = SessionLocal()

    @contextmanager
    def _shared_session(*_a, **_k):
        yield session

    mocker.patch(f"{_RAG_MOD}.get_user_db_session", _shared_session)
    mocker.patch(f"{_FACADE_MOD}.get_user_db_session", _shared_session)
    mocker.patch(
        f"{_COLLECTION_DELETION_MOD}.get_user_db_session", _shared_session
    )

    fim = MagicMock()
    fim.verify_file.return_value = (True, None)
    fim.record_file.return_value = None
    mocker.patch(f"{_RAG_MOD}.FileIntegrityManager", return_value=fim)

    source_type = SourceType(
        id=uuid.uuid4().hex, name="document", display_name="Document"
    )
    session.add(source_type)
    session.commit()

    class Env:
        pass

    env = Env()
    env.tmp_path = tmp_path
    env.engine = engine
    env.SessionLocal = SessionLocal
    env.session = session
    env.source_type_id = source_type.id

    yield env

    session.close()
    engine.dispose()


def _index_two_documents(env, collection_id: str):
    """Index two real documents into ``collection_id`` via the real
    ``LibraryRAGService.index_document`` production path. Returns
    (document_id_1, document_id_2)."""
    emb_mgr = MagicMock()
    emb_mgr.embeddings = _FakeEmbeddings(dim=8)

    svc = LibraryRAGService(
        username=USERNAME,
        db_password="pw",  # gitleaks:allow
        embedding_model="fake-model",
        embedding_provider="sentence_transformers",
        embedding_manager=emb_mgr,
    )
    try:
        doc1 = _make_document(
            env.session,
            title="Doc One",
            body="The quick brown fox jumps over the lazy dog. " * 10,
            source_type_id=env.source_type_id,
        )
        doc2 = _make_document(
            env.session,
            title="Doc Two",
            body="A second unrelated document about mountain climbing. " * 10,
            source_type_id=env.source_type_id,
        )
        env.session.commit()

        r1 = svc.index_document(doc1, collection_id)
        r2 = svc.index_document(doc2, collection_id)
        assert r1["status"] == "success", r1
        assert r2["status"] == "success", r2
        assert r1["chunk_count"] > 0
        assert r2["chunk_count"] > 0
    finally:
        svc.close()

    return doc1, doc2


def _make_collection(env, *, name: str) -> str:
    collection_id = uuid.uuid4().hex
    env.session.add(Collection(id=collection_id, name=name))
    env.session.commit()
    return collection_id


def _rag_index_rows(session, collection_name: str):
    return (
        session.query(RAGIndex).filter_by(collection_name=collection_name).all()
    )


def _chunk_count(session, collection_name: str) -> int:
    return (
        session.query(DocumentChunk)
        .filter_by(collection_name=collection_name)
        .count()
    )


# --------------------------------------------------------------------------
# (a) Happy path
# --------------------------------------------------------------------------
class TestHappyPathDeletesEverything:
    def test_delete_collection_removes_faiss_file_and_db_rows(self, flow_env):
        env = flow_env
        collection_id = _make_collection(env, name="Happy Path Collection")
        collection_name = f"collection_{collection_id}"

        _index_two_documents(env, collection_id)

        # Preconditions: real state actually exists before deletion.
        rag_rows_before = _rag_index_rows(env.session, collection_name)
        assert len(rag_rows_before) == 1
        faiss_path = Path(rag_rows_before[0].index_path).with_suffix(".faiss")
        assert faiss_path.is_file()
        assert faiss_path.stat().st_size > 0
        assert _chunk_count(env.session, collection_name) > 0

        # Also confirm it lives under the real cache/rag_indices tree.
        rag_root = get_cache_directory() / "rag_indices"
        assert list(rag_root.rglob("*.faiss")), (
            "expected the real production indexing path to have written "
            "under cache/rag_indices"
        )

        service = CollectionDeletionService(username=USERNAME)
        result = service.delete_collection(
            collection_id, delete_orphaned_documents=False
        )

        assert result["deleted"] is True, result
        assert result["indices_deleted"] == 1
        assert result["chunks_deleted"] > 0

        # Observable on-disk state: the .faiss file is really gone.
        assert not faiss_path.is_file(), (
            "the .faiss index file should have been unlinked after a "
            "successful collection delete"
        )

        # Observable DB state: chunks and the RAGIndex row are really gone.
        assert _chunk_count(env.session, collection_name) == 0
        assert _rag_index_rows(env.session, collection_name) == []

        # And the collection row itself is gone.
        assert (
            env.session.query(Collection).filter_by(id=collection_id).first()
            is None
        )


# --------------------------------------------------------------------------
# (b) Deferred-unlink order: mid-transaction failure rolls back the DB and
#     leaves the file on disk (no restored-row-but-missing-file orphan).
# --------------------------------------------------------------------------
class TestDeferredUnlinkOrderOnRollback:
    def _inject_failure_on_collection_delete(self, session):
        """Monkeypatch this session's ``.delete`` to raise the FIRST time
        it is asked to delete a ``Collection`` row — i.e. AFTER the RAGIndex
        rows have already been staged for deletion (an earlier step in
        ``delete_collection``) but BEFORE ``session.commit()``."""
        original_delete = session.delete

        def _boom(instance):
            if isinstance(instance, Collection):
                raise RuntimeError("INJECTED_MID_TRANSACTION_FAILURE")
            return original_delete(instance)

        session.delete = _boom
        return original_delete

    def test_mid_transaction_failure_rolls_back_row_and_leaves_file(
        self, flow_env
    ):
        env = flow_env
        collection_id = _make_collection(env, name="Rollback Collection")
        collection_name = f"collection_{collection_id}"

        _index_two_documents(env, collection_id)

        rag_rows_before = _rag_index_rows(env.session, collection_name)
        assert len(rag_rows_before) == 1
        faiss_path = Path(rag_rows_before[0].index_path).with_suffix(".faiss")
        assert faiss_path.is_file()
        chunks_before = _chunk_count(env.session, collection_name)
        assert chunks_before > 0

        self._inject_failure_on_collection_delete(env.session)

        service = CollectionDeletionService(username=USERNAME)
        result = service.delete_collection(
            collection_id, delete_orphaned_documents=False
        )

        assert result["deleted"] is False
        assert "error" in result

        # The file must NOT have been touched: delete_collection's
        # post-commit unlink loop is unreachable once the except-branch's
        # early `return` fires.
        assert faiss_path.is_file(), (
            "the .faiss file was unlinked despite the transaction rolling "
            "back — a rollback-orphan (row restored, file already gone)"
        )

        # Verify the DB rollback via a COMPLETELY FRESH session/connection
        # against the file-backed DB — not the mutated in-process session —
        # so this is a real persisted-state check, not an ORM identity-map
        # artifact.
        fresh = env.SessionLocal()
        try:
            fresh_rows = (
                fresh.query(RAGIndex)
                .filter_by(collection_name=collection_name)
                .all()
            )
            assert len(fresh_rows) == 1, (
                "RAGIndex row was not rolled back after the injected "
                "mid-transaction failure"
            )
            assert (
                fresh.query(DocumentChunk)
                .filter_by(collection_name=collection_name)
                .count()
                == chunks_before
            ), "DocumentChunk rows were not rolled back"
            assert (
                fresh.query(Collection).filter_by(id=collection_id).first()
                is not None
            ), "Collection row was not rolled back"
        finally:
            fresh.close()

    def test_control_without_injected_failure_both_row_and_file_go(
        self, flow_env
    ):
        """Control: proves the assertions above actually bite. Without the
        injected failure, the SAME setup results in the row AND the file
        both being removed — so "row restored / file present" in the test
        above is a real signal of a bug, not something that always holds."""
        env = flow_env
        collection_id = _make_collection(env, name="Control Collection")
        collection_name = f"collection_{collection_id}"

        _index_two_documents(env, collection_id)
        rag_rows_before = _rag_index_rows(env.session, collection_name)
        faiss_path = Path(rag_rows_before[0].index_path).with_suffix(".faiss")
        assert faiss_path.is_file()

        # No failure injected this time.
        service = CollectionDeletionService(username=USERNAME)
        result = service.delete_collection(
            collection_id, delete_orphaned_documents=False
        )

        assert result["deleted"] is True, result
        assert not faiss_path.is_file()
        fresh = env.SessionLocal()
        try:
            assert (
                fresh.query(RAGIndex)
                .filter_by(collection_name=collection_name)
                .count()
                == 0
            )
        finally:
            fresh.close()


# --------------------------------------------------------------------------
# (c) Post-commit unlink failure is swallowed: a committed delete must not
#     be reported as failed just because a file could not be removed.
# --------------------------------------------------------------------------
class TestPostCommitUnlinkFailureSwallowed:
    def test_unlink_failure_after_commit_still_reports_success(self, flow_env):
        env = flow_env
        collection_id = _make_collection(env, name="Unlink Failure Collection")
        collection_name = f"collection_{collection_id}"

        _index_two_documents(env, collection_id)

        rag_rows_before = _rag_index_rows(env.session, collection_name)
        assert len(rag_rows_before) == 1
        faiss_path = Path(rag_rows_before[0].index_path).with_suffix(".faiss")
        assert faiss_path.is_file()

        service = CollectionDeletionService(username=USERNAME)

        # Force the post-commit on-disk unlink to fail the way a genuine
        # permission/IO error would — WITHOUT relying on directory mode bits.
        # CI runs as root, which bypasses a read-only (0o555) directory and
        # unlinks the file anyway, making the old chmod approach vacuous (and
        # red in CI). Patching Path.unlink to raise drives
        # CascadeHelper.delete_faiss_index_files into its best-effort
        # except-swallow branch with the file left on disk — exactly the
        # state a real unlink failure produces. Scoped to the delete call so
        # the real cleanup sweep at the end still runs normally.
        with patch(
            "pathlib.Path.unlink",
            side_effect=PermissionError("simulated read-only directory"),
        ):
            result = service.delete_collection(
                collection_id, delete_orphaned_documents=False
            )

        # The DB delete committed durably; a mere unlink hiccup must NOT flip
        # this into a reported failure.
        assert result["deleted"] is True, result
        assert result["indices_deleted"] == 1

        # The file-unlink genuinely failed and is still on disk (proves the
        # injected failure really fired, not a no-op).
        assert faiss_path.is_file(), (
            "expected the on-disk unlink to have been blocked (Path.unlink "
            "patched to raise) — if the file is gone the failure never "
            "actually fired and this test is vacuous"
        )

        # DB rows are gone regardless of the on-disk hiccup.
        fresh = env.SessionLocal()
        try:
            assert (
                fresh.query(RAGIndex)
                .filter_by(collection_name=collection_name)
                .count()
                == 0
            )
            assert (
                fresh.query(DocumentChunk)
                .filter_by(collection_name=collection_name)
                .count()
                == 0
            )
        finally:
            fresh.close()

        # Now that permissions are restored, a fresh delete_faiss_index_files
        # sweep would succeed — proving the earlier failure was a real,
        # transient permission issue and not some unrelated brokenness.
        from local_deep_research.research_library.deletion.utils.cascade_helper import (
            CascadeHelper,
        )

        assert (
            CascadeHelper.delete_faiss_index_files(
                str(faiss_path.with_suffix(""))
            )
            is True
        )
        assert not faiss_path.is_file()

    def test_control_writable_dir_unlink_succeeds(self, flow_env):
        """Control: proves the patched-unlink failure above is what causes
        the earlier "file survives" — with a real (unpatched) unlink the same
        delete removes the file, so "file survives" isn't just how this store
        always behaves."""
        env = flow_env
        collection_id = _make_collection(env, name="Writable Dir Collection")
        collection_name = f"collection_{collection_id}"

        _index_two_documents(env, collection_id)
        rag_rows_before = _rag_index_rows(env.session, collection_name)
        faiss_path = Path(rag_rows_before[0].index_path).with_suffix(".faiss")
        assert faiss_path.is_file()

        service = CollectionDeletionService(username=USERNAME)
        result = service.delete_collection(
            collection_id, delete_orphaned_documents=False
        )

        assert result["deleted"] is True
        assert not faiss_path.is_file()


# --------------------------------------------------------------------------
# (d) Orphaned-document cross-collection safety: a document shared by two
#     collections must keep its OTHER collection's chunks/vectors intact
#     when one collection is deleted.
# --------------------------------------------------------------------------
class TestCrossCollectionSafety:
    def test_deleting_one_collection_leaves_other_collections_intact(
        self, flow_env
    ):
        env = flow_env
        collection_a = _make_collection(env, name="Collection A")
        collection_b = _make_collection(env, name="Collection B")
        collection_name_a = f"collection_{collection_a}"
        collection_name_b = f"collection_{collection_b}"

        emb_mgr = MagicMock()
        emb_mgr.embeddings = _FakeEmbeddings(dim=8)
        svc = LibraryRAGService(
            username=USERNAME,
            db_password="pw",  # gitleaks:allow
            embedding_model="fake-model",
            embedding_provider="sentence_transformers",
            embedding_manager=emb_mgr,
        )
        try:
            shared_doc = _make_document(
                env.session,
                title="Shared Doc",
                body="This content is shared across two collections and must "
                "survive deletion of just one of them. " * 8,
                source_type_id=env.source_type_id,
            )
            only_in_b = _make_document(
                env.session,
                title="Only In B",
                body="This document lives only in collection B. " * 8,
                source_type_id=env.source_type_id,
            )
            env.session.commit()

            # Index the shared doc into BOTH collections, and a second doc
            # only into B (so B is not trivially empty after A is deleted).
            r_a = svc.index_document(shared_doc, collection_a)
            r_b1 = svc.index_document(shared_doc, collection_b)
            r_b2 = svc.index_document(only_in_b, collection_b)
            assert r_a["status"] == "success", r_a
            assert r_b1["status"] == "success", r_b1
            assert r_b2["status"] == "success", r_b2

            # B's shared search works BEFORE the deletion (sanity baseline).
            hits_before = svc.search(
                "shared across two collections", collection_b, top_k=5
            )
            assert hits_before
        finally:
            svc.close()

        rag_rows_a = _rag_index_rows(env.session, collection_name_a)
        rag_rows_b = _rag_index_rows(env.session, collection_name_b)
        assert len(rag_rows_a) == 1
        assert len(rag_rows_b) == 1
        faiss_a = Path(rag_rows_a[0].index_path).with_suffix(".faiss")
        faiss_b = Path(rag_rows_b[0].index_path).with_suffix(".faiss")
        assert faiss_a.is_file()
        assert faiss_b.is_file()

        chunks_b_before = _chunk_count(env.session, collection_name_b)
        assert chunks_b_before > 0

        # The shared document is NOT orphaned by deleting A (it remains
        # linked to B), so delete_orphaned_documents's value should not
        # matter here — use the default (True) to also prove that.
        service = CollectionDeletionService(username=USERNAME)
        result = service.delete_collection(collection_a)

        assert result["deleted"] is True, result
        # The shared doc must not have been treated as orphaned.
        assert result["orphaned_documents_deleted"] == 0

        # Collection A's own index/chunks are gone.
        assert not faiss_a.is_file()
        assert _chunk_count(env.session, collection_name_a) == 0
        assert _rag_index_rows(env.session, collection_name_a) == []

        # Collection B is COMPLETELY untouched: same file, same chunk count,
        # same RAGIndex row, and the document row itself still exists.
        assert faiss_b.is_file()
        assert faiss_b.stat().st_size > 0
        assert _chunk_count(env.session, collection_name_b) == chunks_b_before
        assert len(_rag_index_rows(env.session, collection_name_b)) == 1
        assert (
            env.session.query(Document).filter_by(id=shared_doc).first()
            is not None
        ), "the shared document itself must not have been hard-deleted"
        assert (
            env.session.query(DocumentCollection)
            .filter_by(document_id=shared_doc, collection_id=collection_b)
            .first()
            is not None
        )

        # Strongest proof: a REAL search against B's live store still finds
        # the shared document's content, rehydrated from the DB.
        emb_mgr2 = MagicMock()
        emb_mgr2.embeddings = _FakeEmbeddings(dim=8)
        svc2 = LibraryRAGService(
            username=USERNAME,
            db_password="pw",  # gitleaks:allow
            embedding_model="fake-model",
            embedding_provider="sentence_transformers",
            embedding_manager=emb_mgr2,
        )
        try:
            hits_after = svc2.search(
                "shared across two collections", collection_b, top_k=5
            )
            assert hits_after, (
                "collection B's search broke after deleting unrelated "
                "collection A"
            )
        finally:
            svc2.close()

        # And FaissVectorStore.load() on B's real on-disk file directly
        # shows its ids are still all live (no vectors silently dropped).
        from local_deep_research.vector_stores.implementations.segmented_faiss import (
            SegmentedFaissStore,
        )

        store_b = SegmentedFaissStore.load(
            faiss_b,
            dimension=8,
            index_type=rag_rows_b[0].index_type or "flat",
            metric=rag_rows_b[0].distance_metric or "cosine",
            normalize=(
                rag_rows_b[0].normalize_vectors
                if rag_rows_b[0].normalize_vectors is not None
                else True
            ),
        )
        live_ids = store_b.live_ids()
        db_ids = [
            row.id
            for row in env.session.query(DocumentChunk.id)
            .filter_by(collection_name=collection_name_b)
            .all()
        ]
        assert set(live_ids) == set(db_ids)
        assert len(live_ids) == chunks_b_before