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.

2.7 KB
Raw Blame
"""Tests for FAISS vector removal on document removal.

These exercise ``LibraryRAGService.remove_documents_from_index`` (the new
primitive the Zotero removal path uses) without a real FAISS index or
embedding model: the RAGIndex existence check is stubbed via the session, so
these two cases (no ids / no current index) return before any vector store
is ever touched.
"""

from contextlib import contextmanager
from unittest.mock import MagicMock

from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
)


def _svc():
    # Build the service without running __init__ (no embeddings needed).
    svc = LibraryRAGService.__new__(LibraryRAGService)
    # The db_password setter propagates to these — set them first.
    svc.embedding_manager = None
    svc.integrity_manager = None
    svc.username = "u"
    svc.db_password = "pw"  # gitleaks:allow
    svc.rag_index_record = None
    return svc


def _patch_has_index(monkeypatch, exists: bool):
    @contextmanager
    def fake_session(*_a, **_k):
        sess = MagicMock()
        sess.query.return_value.filter_by.return_value.first.return_value = (
            object() if exists else None
        )
        yield sess

    monkeypatch.setattr(
        "local_deep_research.database.session_context.get_user_db_session",
        fake_session,
    )


def test_remove_documents_from_index_noop_without_index(monkeypatch):
    _patch_has_index(monkeypatch, False)
    svc = _svc()
    assert svc.remove_documents_from_index(["D1"], "COLL") == 0


def test_remove_documents_from_index_empty_ids(monkeypatch):
    # No DB hit, no index touched.
    svc = _svc()
    assert svc.remove_documents_from_index([], "COLL") == 0


def test_remove_document_from_rag_also_clears_vectors(monkeypatch):
    # The general UI removal path must clear FAISS vectors too, not just DB
    # chunks — it delegates to the new primitive.
    svc = LibraryRAGService.__new__(LibraryRAGService)
    svc.embedding_manager = MagicMock()
    svc.embedding_manager._delete_chunks_from_db.return_value = 3
    svc.integrity_manager = None
    svc.username = "u"
    svc._db_password = "pw"  # gitleaks:allow
    svc.remove_documents_from_index = MagicMock(return_value=2)

    @contextmanager
    def fake_session(*_a, **_k):
        yield MagicMock()  # DocumentCollection/Collection lookups -> truthy

    monkeypatch.setattr(
        "local_deep_research.research_library.services."
        "library_rag_service.get_user_db_session",
        fake_session,
    )

    result = svc.remove_document_from_rag("DOC1", "COLL1")
    assert result["status"] == "success"
    svc.embedding_manager._delete_chunks_from_db.assert_called_once()
    svc.remove_documents_from_index.assert_called_once_with(["DOC1"], "COLL1")