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.9 KB
Raw Blame
"""Completion-marker edge tests for phase-2 legacy re-keying.

The tests cover branches where apparently completed work must remain retryable
because a legacy plaintext companion or a quarantine/finalization step failed.
"""

import threading
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock

from pytest_mock import MockerFixture

import local_deep_research.vector_stores.legacy_rekey as rekey


@contextmanager
def _session_context(session: MagicMock) -> Iterator[MagicMock]:
    yield session


def _one_index_harness(
    mocker: MockerFixture, tmp_path: Path, *, sidecar: bool = True
) -> tuple[Path, MagicMock]:
    faiss_path = tmp_path / "legacy.faiss"
    sidecar_path = tmp_path / "legacy.idmap.json"
    if sidecar:
        sidecar_path.write_text('{"0": "a"}', encoding="utf-8")
    rag_index = MagicMock()
    rag_index.id = 17
    rag_index.index_path = str(faiss_path)
    rag_index.collection_name = "collection_edge"
    rag_index.embedding_dimension = 2
    rag_index.index_type = "flat"
    rag_index.distance_metric = "l2"
    rag_index.normalize_vectors = False
    settings = MagicMock()
    settings.get_bool_setting.return_value = False
    session = MagicMock()
    session.query.return_value.all.return_value = [rag_index]
    mocker.patch.object(
        rekey,
        "get_user_db_session",
        # One fresh context manager per call: the orchestrator opens a
        # second session when persisting rekeyed config (legacy_rekey.py:146).
        side_effect=lambda *args, **kwargs: _session_context(session),
    )
    mocker.patch.object(rekey, "get_settings_manager", return_value=settings)
    mocker.patch(
        "local_deep_research.research_library.services.library_rag_service."
        "_get_faiss_write_lock",
        # Head returns a _TrackedRLock (an RLock wrapper); RLock() here
        # matches its reentrancy instead of a plain, non-reentrant Lock().
        return_value=threading.RLock(),
    )
    return faiss_path, settings


def test_already_migrated_index_with_unremovable_plaintext_withholds_marker(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path, settings = _one_index_harness(mocker, tmp_path, sidecar=False)
    faiss_path.write_bytes(b"migrated index")
    mocker.patch.object(rekey, "_is_idmap2", return_value=True)
    mocker.patch.object(rekey, "_purge_redundant_pkl", return_value=False)

    stats = rekey.rekey_user_indexes("alice", "password")

    assert stats["skipped_no_sidecar"] == 1
    settings.set_setting.assert_not_called()


def test_rekey_failure_with_successful_quarantine_counts_resolution(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path, settings = _one_index_harness(mocker, tmp_path)
    faiss_path.write_bytes(b"old index")
    mocker.patch.object(rekey, "_load_uuid_to_id", return_value={"a": 1})
    mocker.patch.object(
        rekey, "rekey_index_file", side_effect=ValueError("corrupt")
    )
    quarantine = mocker.patch.object(rekey, "_quarantine_and_reset")

    stats = rekey.rekey_user_indexes("alice", "password")

    assert stats["quarantined"] == 1
    quarantine.assert_called_once()
    settings.set_setting.assert_called_once_with(
        rekey.REKEY_MARKER_SETTING, True
    )


def test_rekeyed_index_with_unremovable_plaintext_remains_retryable(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path, settings = _one_index_harness(mocker, tmp_path)
    faiss_path.write_bytes(b"old index")
    mocker.patch.object(rekey, "_load_uuid_to_id", return_value={"a": 1})
    successful_rekey = mocker.patch.object(rekey, "rekey_index_file")
    mocker.patch.object(rekey, "_purge_redundant_pkl", return_value=False)
    mocker.patch.object(rekey, "FileIntegrityManager")

    stats = rekey.rekey_user_indexes("alice", "password")

    successful_rekey.assert_called_once()
    assert stats["rekeyed"] == 1
    settings.set_setting.assert_not_called()