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.

8.1 KB
Raw Blame
"""Failure-path coverage for plaintext cleanup and low-level legacy re-keying."""

from pathlib import Path
from unittest.mock import MagicMock

import faiss
import pytest
from pytest_mock import MockerFixture

import local_deep_research.vector_stores.legacy_cleanup as cleanup


def test_find_legacy_docstores_returns_empty_for_absent_root(tmp_path):
    assert cleanup._find_legacy_docstores(tmp_path / "missing") == ([], [])


def test_find_legacy_docstores_records_walk_error(tmp_path, mocker):
    error = OSError("denied")
    error.filename = str(tmp_path / "blocked")

    def _walk(_root, *, onerror):
        onerror(error)
        return iter(())

    mocker.patch.object(cleanup.os, "walk", _walk)

    assert cleanup._find_legacy_docstores(tmp_path) == ([], [error.filename])


def test_delete_file_retries_read_only_file_after_chmod(mocker):
    path = MagicMock()
    path.unlink.side_effect = [PermissionError("readonly"), None]
    chmod = mocker.patch.object(cleanup.os, "chmod")

    cleanup._delete_file(path)

    chmod.assert_called_once_with(
        path, cleanup.stat.S_IWRITE | cleanup.stat.S_IREAD
    )
    assert path.unlink.call_count == 2


def test_delete_file_ignores_chmod_failure_before_final_retry(mocker):
    path = MagicMock()
    path.unlink.side_effect = [PermissionError("readonly"), None]
    mocker.patch.object(cleanup.os, "chmod", side_effect=OSError("unsupported"))

    cleanup._delete_file(path)

    assert path.unlink.call_count == 2


def test_write_sidecar_continues_when_directory_fsync_is_unsupported(
    tmp_path, mocker
):
    sidecar = tmp_path / "index.idmap.json"
    mocker.patch.object(
        cleanup.os, "fsync", side_effect=[None, OSError("unsupported")]
    )

    cleanup._write_sidecar_atomic(sidecar, {"0": "a" * 32})

    assert sidecar.read_text(encoding="utf-8") == '{"0": "' + "a" * 32 + '"}'


def test_extract_map_treats_sidecar_write_error_as_reindex_fallback(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    pkl_path = tmp_path / "legacy.pkl"
    pkl_path.write_bytes(b"placeholder")
    mocker.patch.object(
        cleanup, "load_index_to_docstore_id", return_value={0: "a" * 32}
    )
    mocker.patch.object(
        cleanup, "_write_sidecar_atomic", side_effect=OSError("full")
    )

    assert cleanup._extract_map(pkl_path) is False


def test_migration_reports_delete_failure_and_remaining_plaintext(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    pkl_path = tmp_path / "legacy.pkl"
    pkl_path.write_bytes(b"plaintext")
    mocker.patch.object(cleanup, "_rag_cache_root", return_value=tmp_path)
    mocker.patch.object(cleanup, "_extract_map", return_value=True)
    mocker.patch.object(cleanup, "_delete_file", side_effect=OSError("locked"))

    result = cleanup.migrate_legacy_docstores()

    assert result["found"] == 1
    assert result["deleted"] == 0
    assert result["remaining"] == 1


def test_migration_survives_root_chmod_failure(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    pkl_path = tmp_path / "legacy.pkl"
    pkl_path.write_bytes(b"plaintext")
    mocker.patch.object(cleanup, "_rag_cache_root", return_value=tmp_path)
    chmod = mocker.patch.object(
        Path, "chmod", side_effect=OSError("unsupported")
    )
    mocker.patch.object(cleanup, "_extract_map", return_value=False)
    error = mocker.patch.object(cleanup.logger, "error")
    exception = mocker.patch.object(cleanup.logger, "exception")

    result = cleanup.migrate_legacy_docstores()

    # Positive control: the root-hardening chmod was actually attempted (with
    # the process bound as `self`, so the mock only observes the mode arg —
    # see cleanup._rag_cache_root and the `root.chmod(0o700)` call it feeds).
    # Without it, deleting the hardening block at legacy_cleanup.py:377-381
    # would leave this test green for the wrong reason.
    chmod.assert_called_once_with(0o700)
    assert result["deleted"] == 1
    assert not pkl_path.exists()
    assert result["hardening_errors"] == 1
    # All plaintext WAS removed here, so the plaintext-migration summary must
    # not fire; the only ERROR is the dedicated hardening line, logged via
    # logger.exception() so the traceback is attached.
    messages = [
        str(call.args[0])
        for call in error.call_args_list + exception.call_args_list
    ]
    assert any("could not be restricted to 0o700" in m for m in messages)
    assert not any("did NOT complete cleanly" in m for m in messages)


def test_migration_reports_root_chmod_failure_on_clean_tree(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    mocker.patch.object(cleanup, "_rag_cache_root", return_value=tmp_path)
    chmod = mocker.patch.object(
        Path, "chmod", side_effect=OSError("unsupported")
    )
    error = mocker.patch.object(cleanup.logger, "error")
    exception = mocker.patch.object(cleanup.logger, "exception")

    result = cleanup.migrate_legacy_docstores()

    # Only the root-hardening chmod runs on a clean tree; pin it so a later
    # chmod elsewhere can't satisfy this test for the wrong reason.
    chmod.assert_called_once_with(0o700)
    assert result["found"] == 0
    assert result["scan_errors"] == 0
    assert result["hardening_errors"] == 1
    # Severity must not depend on whether legacy files happened to exist:
    # the hardening failure is an ERROR even on a clean tree, on its own
    # line, and never phrased as a plaintext-migration failure.
    messages = [
        str(call.args[0])
        for call in error.call_args_list + exception.call_args_list
    ]
    assert any("could not be restricted to 0o700" in m for m in messages)
    assert not any("did NOT complete cleanly" in m for m in messages)


def test_migration_reports_no_hardening_error_when_chmod_succeeds(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    mocker.patch.object(cleanup, "_rag_cache_root", return_value=tmp_path)

    result = cleanup.migrate_legacy_docstores()

    assert result["hardening_errors"] == 0


def test_rekey_rejects_dimension_mismatch_before_reconstruction(
    tmp_path: Path,
) -> None:
    faiss_path = tmp_path / "legacy.faiss"
    sidecar = tmp_path / "legacy.idmap.json"
    faiss.write_index(faiss.IndexFlatL2(2), str(faiss_path))
    sidecar.write_text("{}", encoding="utf-8")

    with pytest.raises(ValueError, match="dim 2 != expected 3"):
        cleanup.rekey_index_file(
            faiss_path,
            sidecar,
            {},
            dimension=3,
            index_type="flat",
            metric="l2",
            normalize=False,
        )


def test_rekey_empty_index_and_failed_readback_are_detected(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path = tmp_path / "legacy.faiss"
    sidecar = tmp_path / "legacy.idmap.json"
    sidecar.write_text("{}", encoding="utf-8")
    old = faiss.IndexFlatL2(2)
    mocker.patch.object(cleanup, "read_index", return_value=old)
    store = MagicMock()
    verify = MagicMock()
    verify.live_ids.return_value = {99}
    verify.count.return_value = 1
    store_class = MagicMock()
    store_class.create.return_value = store
    store_class.load.return_value = verify
    mocker.patch(
        "local_deep_research.vector_stores.implementations.faiss_store."
        "FaissVectorStore",
        store_class,
    )

    with pytest.raises(RuntimeError, match="read-back verify failed"):
        cleanup.rekey_index_file(
            faiss_path,
            sidecar,
            {},
            dimension=2,
            index_type="flat",
            metric="l2",
            normalize=False,
        )

    assert store.add.call_count == 0


def test_missing_sidecar_reraises_when_concurrent_probe_is_not_migrated(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path = tmp_path / "legacy.faiss"
    faiss_path.write_bytes(b"placeholder")
    old = faiss.IndexFlatL2(2)
    mocker.patch.object(
        cleanup, "read_index", side_effect=[old, OSError("partial")]
    )

    with pytest.raises(FileNotFoundError):
        cleanup.rekey_index_file(
            faiss_path,
            tmp_path / "missing.idmap.json",
            {},
            dimension=2,
            index_type="flat",
            metric="l2",
            normalize=False,
        )