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.

16.9 KB
Raw Blame
"""Large end-to-end test: a single library Document's FULL RAG lifecycle
through REAL components — index -> reindex-with-edited-text -> delete ->
reindex-again — driving the production ``LibraryRAGService`` against a real
on-disk FAISS backend and a real (in-memory) encrypted-DB-shaped
SQLite session.

Mirrors the real-components harness style of
``tests/vector_stores/test_no_plaintext_production_path.py``: a deterministic
network-free ``_FakeEmbeddings``, a real ``Base.metadata.create_all`` sqlite
engine/session shared across every ``get_user_db_session`` call site,
``LDR_DATA_DIR`` pointed at ``tmp_path`` so the real ``cache/rag_indices/``
tree is used, and a stubbed ``FileIntegrityManager`` (integrity bookkeeping is
orthogonal to the lifecycle invariants under test here).

Each phase asserts on OBSERVABLE real state, never on a mock:
  1. index_document(...)                 -> search finds SECRET_A; .faiss exists
  2. index_document(..., force_reindex)   -> search finds SECRET_B, NOT SECRET_A;
                                              old vectors replaced, not duplicated
  3. remove_document_from_rag(...)        -> search finds nothing; zero
                                              DocumentChunk rows; store count/
                                              live_ids drop to 0; .faiss bytes
                                              contain neither secret
  4. index_document(...) again            -> searchable again (delete didn't
                                              corrupt the collection)
"""

import hashlib
import uuid
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock

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.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.services.library_rag_service import (
    LibraryRAGService,
)
from local_deep_research.vector_stores import get_vector_store_class

_RAG_MOD = "local_deep_research.research_library.services.library_rag_service"
_FACADE_MOD = "local_deep_research.vector_stores.facade"

SECRET_A = "ZZZ_LIFECYCLE_SECRET_A_9d1f3c8e"
SECRET_B = "ZZZ_LIFECYCLE_SECRET_B_7ab2e410"


class _FakeEmbeddings(Embeddings):
    """Deterministic, network-free embeddings — numeric vectors, so the text
    markers can never survive into them as raw bytes (keeps on-disk greps
    sound)."""

    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 _grep_all(root, needle: bytes):
    """Every on-disk file under ``root`` that contains ``needle`` (raw bytes)."""
    return [
        str(f)
        for f in root.rglob("*")
        if f.is_file() and needle in f.read_bytes()
    ]


@pytest.fixture
def lifecycle_env(tmp_path, monkeypatch, mocker):
    """Real in-memory DB session shared across every ``get_user_db_session``
    call site the service/facade use, plus the real on-disk cache tree."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))

    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    @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)
    # ``remove_documents_from_index`` does its own local
    # ``from ...database.session_context import get_user_db_session`` inside
    # the method body, which re-binds the name from the SOURCE module rather
    # than using ``library_rag_service``'s own module-level import — patching
    # only the two aliases above leaves that call site hitting the real
    # (unpatched) session factory. Patch the source directly too so every call
    # site — regardless of which alias it resolved — sees the shared session.
    mocker.patch(
        "local_deep_research.database.session_context.get_user_db_session",
        _shared_session,
    )

    # Integrity records live in the DB, not the FAISS tree; stub the manager
    # so the test stays focused on the lifecycle invariants, mirroring
    # test_no_plaintext_production_path.py.
    fim = MagicMock()
    fim.verify_file.return_value = (True, None)
    fim.record_file.return_value = None
    mocker.patch(f"{_RAG_MOD}.FileIntegrityManager", return_value=fim)

    yield session

    session.close()
    engine.dispose()


@pytest.fixture
def seeded_document(lifecycle_env):
    """A real Collection + Document (+ DocumentCollection link) carrying
    SECRET_A in its text content."""
    session = lifecycle_env

    collection_id = uuid.uuid4().hex
    document_id = uuid.uuid4().hex
    source_type = SourceType(
        id=uuid.uuid4().hex, name="document", display_name="Document"
    )
    session.add(source_type)
    session.add(Collection(id=collection_id, name="Lifecycle collection"))

    body = f"This document's body mentions {SECRET_A} several times. " * 5
    session.add(
        Document(
            id=document_id,
            source_type_id=source_type.id,
            document_hash=hashlib.sha256(body.encode()).hexdigest(),
            title="Lifecycle document",
            text_content=body,
            original_url="http://example.com/lifecycle",
            file_size=len(body),
            file_type="txt",
            status=DocumentStatus.COMPLETED,
        )
    )
    session.commit()

    # Explicit DocumentCollection link (index_document would create it via
    # ensure_in_collection anyway, but the task seeds it explicitly so the
    # membership is unambiguous from the start).
    session.add(
        DocumentCollection(
            document_id=document_id, collection_id=collection_id, indexed=False
        )
    )
    session.commit()

    return {"collection_id": collection_id, "document_id": document_id}


@pytest.fixture
def rag_service(lifecycle_env):
    """Real LibraryRAGService; only the embedding backend is faked (no
    network/model load), mirroring the canonical harness."""
    emb_mgr = MagicMock()
    emb_mgr.embeddings = _FakeEmbeddings(dim=8)
    # ``remove_document_from_rag``'s DB-only backstop sweep
    # (``_delete_chunks_from_db``) is a real int-returning DB query in
    # production; in the normal path exercised here every row is already
    # gone (deleted by ``remove_documents_from_index``) by the time it
    # runs, so it finds nothing and returns 0. Left as a bare MagicMock it
    # would return a MagicMock instead of an int, which corrupts the
    # ``deleted_count = removed_vectors + ...`` arithmetic under test.
    emb_mgr._delete_chunks_from_db.return_value = 0

    svc = LibraryRAGService(
        username="lifecycleuser",
        db_password="pw",  # gitleaks:allow
        embedding_model="fake-model",
        embedding_provider="sentence_transformers",
        embedding_manager=emb_mgr,
    )
    yield svc
    svc.close()


def _load_store(faiss_path, rag_index_row):
    store_cls = get_vector_store_class()
    return store_cls.load(
        faiss_path,
        dimension=rag_index_row.embedding_dimension,
        index_type=rag_index_row.index_type or "flat",
        metric=rag_index_row.distance_metric or "cosine",
        normalize=(
            rag_index_row.normalize_vectors
            if rag_index_row.normalize_vectors is not None
            else True
        ),
    )


def _current_rag_index_row(session, collection_id):
    collection_name = f"collection_{collection_id}"
    return (
        session.query(RAGIndex)
        .filter_by(collection_name=collection_name, is_current=True)
        .first()
    )


def _chunk_ids_for_source(session, document_id):
    return [
        row.id
        for row in session.query(DocumentChunk.id).filter_by(
            source_type="document", source_id=document_id
        )
    ]


class TestDocumentFullLifecycle:
    def test_index_reindex_delete_reindex_round_trip(
        self, lifecycle_env, seeded_document, rag_service
    ):
        session = lifecycle_env
        svc = rag_service
        document_id = seeded_document["document_id"]
        collection_id = seeded_document["collection_id"]

        # ------------------------------------------------------------ #
        # Phase 1: initial index — SECRET_A is indexed and searchable.
        # ------------------------------------------------------------ #
        result = svc.index_document(document_id, collection_id)
        assert result["status"] == "success", result
        assert result["chunk_count"] > 0

        # One registered index may contain multiple immutable segment files.
        # Resolve its public path from the DB rather than counting every
        # native .faiss file beneath the storage directory.
        assert (
            session.query(RAGIndex)
            .filter_by(collection_name=f"collection_{collection_id}")
            .count()
            == 1
        )
        rag_index_row = _current_rag_index_row(session, collection_id)
        assert rag_index_row is not None
        faiss_path = Path(rag_index_row.index_path)
        assert faiss_path.is_file()
        assert faiss_path.stat().st_size > 0

        hits = svc.search(f"body mentions {SECRET_A}", collection_id, top_k=5)
        assert hits
        assert any(SECRET_A in h.text for h in hits)

        store = _load_store(faiss_path, rag_index_row)
        phase1_ids = set(_chunk_ids_for_source(session, document_id))
        assert phase1_ids, "no DocumentChunk rows were created by indexing"
        assert set(store.live_ids()) == phase1_ids
        assert store.count() == len(phase1_ids)

        # ------------------------------------------------------------ #
        # Phase 2: edit the text to SECRET_B and force-reindex. The old
        # SECRET_A vectors/rows must be REPLACED, not left as stale
        # duplicates alongside the new SECRET_B ones.
        # ------------------------------------------------------------ #
        document = session.query(Document).filter_by(id=document_id).first()
        new_body = f"This document's body now mentions {SECRET_B} instead. " * 5
        document.text_content = new_body
        document.document_hash = hashlib.sha256(new_body.encode()).hexdigest()
        session.commit()

        result2 = svc.index_document(
            document_id, collection_id, force_reindex=True
        )
        assert result2["status"] == "success", result2
        assert result2["chunk_count"] > 0

        hits_b = svc.search(
            f"body now mentions {SECRET_B}", collection_id, top_k=5
        )
        assert hits_b
        assert any(SECRET_B in h.text for h in hits_b)
        assert all(SECRET_A not in h.text for h in hits_b), (
            "stale SECRET_A content resurfaced after a force_reindex with "
            "edited text"
        )

        # A broad query that would have matched EITHER version's chunks must
        # not return any SECRET_A-bearing result either.
        hits_broad = svc.search("This document's body", collection_id, top_k=10)
        assert hits_broad
        assert all(SECRET_A not in h.text for h in hits_broad)
        assert any(SECRET_B in h.text for h in hits_broad)

        # DB-level: every remaining chunk row for this source carries the NEW
        # text only — the old rows were deleted, not merely out-ranked.
        remaining_rows = (
            session.query(DocumentChunk)
            .filter_by(source_type="document", source_id=document_id)
            .all()
        )
        assert remaining_rows
        assert all(SECRET_A not in (r.chunk_text or "") for r in remaining_rows)
        assert any(SECRET_B in (r.chunk_text or "") for r in remaining_rows)

        rag_index_row = _current_rag_index_row(session, collection_id)
        store2 = _load_store(faiss_path, rag_index_row)
        phase2_ids = set(_chunk_ids_for_source(session, document_id))
        assert phase2_ids, "no DocumentChunk rows after reindex"
        assert phase2_ids.isdisjoint(phase1_ids), (
            "reindex reused the OLD chunk ids instead of replacing them with "
            "fresh rows — the old-vs-new id sets should never overlap here"
        )
        assert set(store2.live_ids()) == phase2_ids
        assert store2.count() == len(phase2_ids)

        # The on-disk .faiss holds no plaintext of either secret (vectors are
        # pure floats — this is the structural guarantee, reasserted here).
        leaked = _grep_all(faiss_path.parent, SECRET_A.encode())
        leaked += _grep_all(faiss_path.parent, SECRET_B.encode())
        assert leaked == [], f"plaintext marker leaked to disk: {leaked}"

        # ------------------------------------------------------------ #
        # Phase 3: delete the document from RAG. Search must return
        # nothing, chunk rows must be gone, and the store must reflect it.
        # ------------------------------------------------------------ #
        del_result = svc.remove_document_from_rag(document_id, collection_id)
        assert del_result["status"] == "success", del_result
        # Regression guard for FIX A (commit daa5e2ebb): the reported
        # ``deleted_count`` must reflect the TRUE number of chunks removed —
        # this is the only real-FAISS path in the suite, so it validates the
        # count against ground truth (the actual phase-2 chunk id set) rather
        # than a mock's return value.
        assert del_result["deleted_count"] == len(phase2_ids), del_result

        hits_after_delete = svc.search(
            "This document's body", collection_id, top_k=10
        )
        assert hits_after_delete == []

        zero_rows = (
            session.query(DocumentChunk)
            .filter_by(source_type="document", source_id=document_id)
            .count()
        )
        assert zero_rows == 0

        rag_index_row = _current_rag_index_row(session, collection_id)
        store3 = _load_store(faiss_path, rag_index_row)
        assert store3.count() == 0
        assert store3.live_ids() == []

        leaked_after_delete = _grep_all(faiss_path.parent, SECRET_A.encode())
        leaked_after_delete += _grep_all(faiss_path.parent, SECRET_B.encode())
        assert leaked_after_delete == [], (
            f"plaintext marker leaked to disk after delete: "
            f"{leaked_after_delete}"
        )

        # The Document row itself must still exist (this is a RAG-removal,
        # not a document deletion) — a control for phase 4 below.
        doc_still_present = (
            session.query(Document).filter_by(id=document_id).first()
        )
        assert doc_still_present is not None
        assert SECRET_B in doc_still_present.text_content

        # ------------------------------------------------------------ #
        # Phase 4: re-index the still-present Document -> searchable again.
        # Proves the delete didn't corrupt the collection/store.
        # ------------------------------------------------------------ #
        result3 = svc.index_document(document_id, collection_id)
        assert result3["status"] == "success", result3
        assert result3["chunk_count"] > 0

        hits_final = svc.search(
            f"body now mentions {SECRET_B}", collection_id, top_k=5
        )
        assert hits_final
        assert any(SECRET_B in h.text for h in hits_final)

        rag_index_row = _current_rag_index_row(session, collection_id)
        store4 = _load_store(faiss_path, rag_index_row)
        phase4_ids = set(_chunk_ids_for_source(session, document_id))
        assert phase4_ids
        assert set(store4.live_ids()) == phase4_ids
        assert store4.count() == len(phase4_ids)

    def test_control_broad_query_would_have_matched_secret_a_before_reindex(
        self, lifecycle_env, seeded_document, rag_service
    ):
        """Sanity control for phase 2's "no SECRET_A" assertions above: prove
        the broad query genuinely WOULD surface SECRET_A content when it is
        still the indexed text — i.e. the "not present after reindex"
        assertions are not vacuously true because the query never matches
        anything.
        """
        svc = rag_service
        document_id = seeded_document["document_id"]
        collection_id = seeded_document["collection_id"]

        result = svc.index_document(document_id, collection_id)
        assert result["status"] == "success", result

        hits = svc.search("This document's body", collection_id, top_k=10)
        assert hits
        assert any(SECRET_A in h.text for h in hits), (
            "control failed: the broad query does not surface SECRET_A even "
            "before the reindex — the phase-2 'SECRET_A absent' assertions "
            "would be vacuous"
        )