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.

3.8 KB
Raw Blame
"""Regression tests for legacy_rekey._quarantine_and_reset's ``is_current`` guard.

Round-1 review changed ``rekey_user_indexes`` to re-key ALL ``RAGIndex`` rows
(not just ``is_current=True``): reverting the embedding model re-resolves a
search to a stale row by ``index_hash``, and an un-rekeyed old-format ``.faiss``
would hard-fail search. To keep a STALE row's re-key FAILURE from collaterally
wiping the healthy CURRENT index's ``indexed`` state (which would force a
needless full re-embed of the whole collection), ``_quarantine_and_reset`` now
resets the collection-wide ``DocumentCollection.indexed`` flag ONLY when the
quarantined row is ``is_current``.

These tests lock that guard so a future refactor cannot silently reintroduce the
"stale-row quarantine re-embeds the whole collection" regression. The function
that previously had zero behavioral coverage now has its core invariant pinned.
"""

import threading
from unittest.mock import MagicMock, patch

_MOD = "local_deep_research.vector_stores.legacy_rekey"


def _run_quarantine(tmp_path, *, is_current):
    """Invoke _quarantine_and_reset with a fully mocked user DB session.

    Returns (doc_collection_query, rag_status_query, idx) so callers can assert
    which DB mutations ran. The .faiss/.idmap paths are non-existent so the
    file-rename branches are skipped — the DB reset logic is what we're pinning.
    """
    from local_deep_research.vector_stores.legacy_rekey import (
        _quarantine_and_reset,
    )

    rag_index = MagicMock()
    rag_index.id = 7
    rag_index.collection_name = "collection_abc"

    # The freshly-queried idx carries the authoritative is_current the guard reads.
    idx = MagicMock()
    idx.id = 7
    idx.is_current = is_current

    q_ragindex = MagicMock()
    q_ragindex.filter_by.return_value.first.return_value = idx
    q_status = MagicMock()
    q_doccoll = MagicMock()

    # Route session.query(Model) by class name so the test is robust to import
    # path / class identity.
    routes = {
        "RAGIndex": q_ragindex,
        "RagDocumentStatus": q_status,
        "DocumentCollection": q_doccoll,
    }
    session = MagicMock()
    session.query.side_effect = lambda model: routes[model.__name__]

    ctx = MagicMock()
    ctx.__enter__ = MagicMock(return_value=session)
    ctx.__exit__ = MagicMock(return_value=False)

    faiss_path = tmp_path / "idx.faiss"  # does not exist -> rename skipped
    sidecar_path = tmp_path / "idx.idmap.json"

    with patch(f"{_MOD}.get_user_db_session", return_value=ctx):
        _quarantine_and_reset(
            "user", "pw", rag_index, faiss_path, sidecar_path, threading.Lock()
        )
    return q_doccoll, q_status, idx


def test_current_row_quarantine_resets_collection_indexed(tmp_path):
    """Quarantining the CURRENT index flips the collection's live indexed flag
    (so the collection rebuilds) and clears this row's RagDocumentStatus."""
    q_doccoll, q_status, idx = _run_quarantine(tmp_path, is_current=True)

    q_doccoll.filter_by.return_value.update.assert_called_once()
    payload = q_doccoll.filter_by.return_value.update.call_args[0][0]
    assert payload.get("indexed") is False

    q_status.filter_by.return_value.delete.assert_called_once()
    assert idx.chunk_count == 0
    assert idx.total_documents == 0


def test_stale_row_quarantine_leaves_collection_indexed_untouched(tmp_path):
    """Quarantining a STALE (is_current=False) row must NOT flip the collection's
    live indexed flag -- the current index is still healthy and serving searches
    -- but it still clears this stale row's own RagDocumentStatus."""
    q_doccoll, q_status, idx = _run_quarantine(tmp_path, is_current=False)

    q_doccoll.filter_by.return_value.update.assert_not_called()

    q_status.filter_by.return_value.delete.assert_called_once()
    assert idx.chunk_count == 0
    assert idx.total_documents == 0