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.

5.2 KB
Raw Blame
"""Regression test for #4855: a document whose vector store persisted zero
embeddings must not be marked ``indexed=True``.

Invariant: a document flagged ``indexed=True`` has had its embeddings persisted
and is searchable. When the store fails to persist (its exception path),
``index_document`` must leave the row ``indexed=False`` so the background
reconciler — which only revisits ``indexed.is_(False)`` rows — picks it up and
re-embeds. Under the int-id vector store the persist runs ``store.apply()``
(durably mutating the FAISS file) BEFORE the DB commit, so the failure path
explicitly flags ``indexed=False`` and commits that flag
(``_flag_document_for_reindex``) rather than relying on an early return — which
is why this test asserts the row is flagged for retry, not that no commit at
all occurs.
"""

from unittest.mock import MagicMock, patch

from local_deep_research.database.models.library import RagDocumentStatus

_MOD = "local_deep_research.research_library.services.library_rag_service"


def _make_service(**overrides):
    """Instantiate LibraryRAGService with all heavy deps mocked out."""
    with (
        patch(f"{_MOD}.LocalEmbeddingManager") as _lem,
        patch(f"{_MOD}.get_user_db_session"),
        patch(f"{_MOD}.FileIntegrityManager"),
        patch(f"{_MOD}.get_text_splitter"),
    ):
        _lem.return_value.embeddings = MagicMock()
        from local_deep_research.research_library.services.library_rag_service import (
            LibraryRAGService,
        )

        defaults = dict(username="testuser", db_password="pw")
        defaults.update(overrides)
        return LibraryRAGService(**defaults)


def _ctx_yielding(session):
    """Context-manager mock that yields *session*."""
    ctx = MagicMock()
    ctx.__enter__ = MagicMock(return_value=session)
    ctx.__exit__ = MagicMock(return_value=False)
    return ctx


def _make_doc(text="Long text content for indexing test."):
    doc = MagicMock()
    doc.id = "doc-1"
    doc.text_content = text
    doc.title = "Test"
    doc.filename = None
    doc.original_url = "http://example.com/d"
    doc.authors = None
    doc.published_date = None
    doc.doi = None
    doc.arxiv_id = None
    doc.pmid = None
    doc.pmcid = None
    doc.extraction_method = None
    doc.word_count = None
    return doc


@patch(f"{_MOD}.ensure_in_collection")
@patch(f"{_MOD}.get_user_db_session")
def test_index_document_not_marked_indexed_when_store_persists_no_embeddings(
    mock_get_session, mock_ensure
):
    """A real chunk set is produced but the vector store fails to persist any
    embeddings. index_document must report an error and must never flip
    DocumentCollection.indexed to True — it flags the row indexed=False so the
    reconciler retries it."""
    svc = _make_service()

    session = MagicMock()
    # This unit fixture has no saved collection configuration.
    session.get.return_value = MagicMock(embedding_model=None)
    mock_get_session.return_value = _ctx_yielding(session)
    session.query.return_value.filter_by.return_value.first.return_value = (
        _make_doc()
    )
    # A first indexing failure has no persisted status row. The broad default
    # query double above represents the document, not a RagDocumentStatus.
    status_query = MagicMock()
    status_query.filter_by.return_value.first.return_value = None
    default_query = session.query.return_value
    session.query.side_effect = lambda model, *args: (
        status_query if model is RagDocumentStatus else default_query
    )
    mock_ensure.return_value = MagicMock(indexed=False, chunk_count=0)

    # The splitter produces a real (non-empty) chunk set...
    svc.text_splitter = MagicMock()
    svc.text_splitter.split_documents.return_value = [MagicMock()]

    svc.rag_index_record = MagicMock()
    svc.rag_index_record.index_path = "/tmp/idx-4855"
    svc.rag_index_record.id = 1

    # ...but the vector store fails to persist it: VectorIndex.index_prepared() raises
    # (its store.apply / embed failure path). index_document must catch that,
    # roll back, and flag the document indexed=False for retry — NOT mark it
    # indexed=True. Because the int-id store mutates the FAISS file durably
    # BEFORE the DB commit, the failure path commits that indexed=False flag
    # (see _flag_document_for_reindex).
    vindex = MagicMock()
    vindex.index.side_effect = RuntimeError("store persisted no embeddings")
    vindex.index_prepared.side_effect = RuntimeError(
        "store persisted no embeddings"
    )
    svc._get_vector_index = MagicMock(return_value=vindex)

    result = svc.index_document("doc-1", "coll-1")

    assert result["status"] == "error"

    indexed_updates = [
        c.args[0]
        for c in session.mock_calls
        if c[0].endswith(".update")
        and c.args
        and isinstance(c.args[0], dict)
        and "indexed" in c.args[0]
    ]
    # It flagged the document for retry ...
    assert indexed_updates, (
        "index_document neither indexed the document nor flagged it for retry "
        "after the store persisted no embeddings"
    )
    # ... and NEVER marked it indexed=True (the #4855 invariant).
    assert all(u.get("indexed") is False for u in indexed_updates), (
        "index_document set DocumentCollection.indexed=True despite the store "
        "persisting no embeddings"
    )