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.

19.0 KB
Raw Blame
"""Tests for CascadeHelper utility."""

import shutil
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.constants import (
    FILE_PATH_SENTINELS,
)
from local_deep_research.research_library.deletion.utils.cascade_helper import (
    CascadeHelper,
)
from tests.security.path_max_fixtures import build_path_max_escape


class TestCascadeHelperDeleteDocumentChunks:
    """Tests for delete_document_chunks static method."""

    def test_deletes_chunks_for_document(self):
        """Should delete chunks for a specific document."""
        mock_session = MagicMock()
        mock_query = MagicMock()
        mock_session.query.return_value = mock_query
        mock_query.filter.return_value = mock_query
        mock_query.delete.return_value = 5

        count = CascadeHelper.delete_document_chunks(mock_session, "doc-123")

        assert count == 5
        mock_query.delete.assert_called_once()

    def test_filters_by_collection_when_provided(self):
        """Should filter by collection when specified."""
        mock_session = MagicMock()
        mock_query = MagicMock()
        mock_session.query.return_value = mock_query
        mock_query.filter.return_value = mock_query
        mock_query.delete.return_value = 3

        count = CascadeHelper.delete_document_chunks(
            mock_session, "doc-123", collection_name="collection_abc"
        )

        assert count == 3
        # Filter should be called multiple times (source_id, source_type, collection_name)
        assert mock_query.filter.call_count >= 2


class TestCascadeHelperDeleteCollectionChunks:
    """Tests for delete_collection_chunks static method."""

    def test_deletes_all_chunks_in_collection(self):
        """Should delete all chunks for a collection."""
        mock_session = MagicMock()
        mock_query = MagicMock()
        mock_session.query.return_value = mock_query
        mock_query.filter_by.return_value = mock_query
        mock_query.delete.return_value = 15

        count = CascadeHelper.delete_collection_chunks(
            mock_session, "collection_abc123"
        )

        assert count == 15


class TestCascadeHelperGetDocumentBlobSize:
    """Tests for get_document_blob_size static method."""

    def test_returns_blob_size(self):
        """Should return size of blob in bytes."""
        mock_session = MagicMock()
        mock_blob = MagicMock()
        mock_blob.pdf_binary = b"x" * 1024
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_blob

        size = CascadeHelper.get_document_blob_size(mock_session, "doc-123")

        assert size == 1024

    def test_returns_zero_when_no_blob(self):
        """Should return 0 when no blob exists."""
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = None

        size = CascadeHelper.get_document_blob_size(mock_session, "doc-123")

        assert size == 0

    def test_returns_zero_when_blob_is_empty(self):
        """Should return 0 when blob has no content."""
        mock_session = MagicMock()
        mock_blob = MagicMock()
        mock_blob.pdf_binary = None
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_blob

        size = CascadeHelper.get_document_blob_size(mock_session, "doc-123")

        assert size == 0


class TestCascadeHelperDeleteDocumentBlob:
    """Tests for delete_document_blob static method."""

    def test_deletes_blob_and_returns_size(self):
        """Should delete blob and return its size."""
        mock_session = MagicMock()
        mock_blob = MagicMock()
        mock_blob.pdf_binary = b"x" * 2048
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_blob

        size = CascadeHelper.delete_document_blob(mock_session, "doc-123")

        assert size == 2048
        mock_session.delete.assert_called_with(mock_blob)

    def test_returns_zero_when_no_blob(self):
        """Should return 0 when no blob exists."""
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = None

        size = CascadeHelper.delete_document_blob(mock_session, "doc-123")

        assert size == 0
        mock_session.delete.assert_not_called()


class TestCascadeHelperDeleteFilesystemFile:
    """Tests for delete_filesystem_file static method."""

    def test_deletes_existing_file(self, tmp_path):
        """Should delete file that exists."""
        test_file = tmp_path / "test.pdf"
        test_file.write_bytes(b"test content")

        result = CascadeHelper.delete_filesystem_file(str(test_file))

        assert result is True
        assert not test_file.exists()

    def test_returns_false_for_nonexistent_file(self, tmp_path):
        """Should return False for file that doesn't exist."""
        result = CascadeHelper.delete_filesystem_file(
            str(tmp_path / "nonexistent.pdf")
        )

        assert result is False

    def test_returns_false_for_none_path(self):
        """Should return False for None path."""
        result = CascadeHelper.delete_filesystem_file(None)

        assert result is False

    def test_skips_special_path_markers(self):
        """Should skip special path markers."""
        for marker in FILE_PATH_SENTINELS:
            result = CascadeHelper.delete_filesystem_file(marker)
            assert result is False


class TestCascadeHelperDeleteFilesystemFileSymlinkGuard:
    """Tests for the symlink / containment defense-in-depth guard on
    delete_filesystem_file (see #5481)."""

    def test_refuses_to_delete_when_path_itself_is_symlink(self, tmp_path):
        """A symlink at the target path must not be followed and unlinked."""
        real_target = tmp_path / "outside" / "secret.txt"
        real_target.parent.mkdir()
        real_target.write_bytes(b"do not delete me")

        library_root = tmp_path / "library"
        library_root.mkdir()
        symlink_path = library_root / "evil.pdf"
        symlink_path.symlink_to(real_target)

        result = CascadeHelper.delete_filesystem_file(str(symlink_path))

        assert result is False
        assert real_target.exists()
        # The symlink itself is also left in place -- we refuse to touch it.
        assert symlink_path.is_symlink()

    def test_refuses_escape_via_symlinked_ancestor_directory(self, tmp_path):
        """A symlinked ancestor directory must not let the delete escape
        the allowed_root, even though the leaf entry is a real file."""
        outside = tmp_path / "outside"
        outside.mkdir()
        secret = outside / "secret.txt"
        secret.write_bytes(b"do not delete me")

        library_root = tmp_path / "library"
        library_root.mkdir()
        # "subdir" is a symlink pointing OUTSIDE library_root.
        (library_root / "subdir").symlink_to(outside)

        escaping_path = library_root / "subdir" / "secret.txt"
        result = CascadeHelper.delete_filesystem_file(
            str(escaping_path), allowed_root=library_root
        )

        assert result is False
        assert secret.exists()

    def test_refuses_an_escape_behind_a_path_max_symlink_chain(self, tmp_path):
        """Non-strict resolution stops following symlinks once the resolved
        prefix outgrows PATH_MAX, so the re-check saw a path inside
        allowed_root while the unlink reached the file outside it."""
        library_root = tmp_path / "library"
        library_root.mkdir()
        outside = tmp_path / "outside"
        outside.mkdir()
        victim = outside / "victim.pdf"
        victim.write_bytes(b"do not delete me")
        link = build_path_max_escape(library_root, outside)

        result = CascadeHelper.delete_filesystem_file(
            str(library_root / link / "victim.pdf"), allowed_root=library_root
        )

        assert result is False
        assert victim.exists()

    def test_a_resolved_allowed_root_is_not_resolved_again(self, tmp_path):
        """With allowed_root_is_resolved=True the boundary is compared as
        given: a boundary directory swapped for a symlink after it was
        resolved cannot move the boundary along with the file (#5481)."""
        library_root = tmp_path / "library"
        pdfs = library_root / "pdfs"
        pdfs.mkdir(parents=True)
        (pdfs / "5.pdf").write_bytes(b"own file")
        victim_dir = tmp_path / "victim"
        victim_dir.mkdir()
        victim = victim_dir / "5.pdf"
        victim.write_bytes(b"do not delete me")
        pinned = pdfs.resolve()

        shutil.rmtree(pdfs)
        try:
            pdfs.symlink_to(victim_dir, target_is_directory=True)
        except (OSError, NotImplementedError):
            pytest.skip("Cannot create directory symlinks in this environment")

        result = CascadeHelper.delete_filesystem_file(
            str(pdfs / "5.pdf"),
            allowed_root=pinned,
            allowed_root_is_resolved=True,
        )

        assert result is False
        assert victim.read_bytes() == b"do not delete me"

    def test_allows_legitimate_delete_within_allowed_root(self, tmp_path):
        """A real file directly under allowed_root is still deletable."""
        library_root = tmp_path / "library"
        library_root.mkdir()
        real_file = library_root / "doc.pdf"
        real_file.write_bytes(b"content")

        result = CascadeHelper.delete_filesystem_file(
            str(real_file), allowed_root=library_root
        )

        assert result is True
        assert not real_file.exists()

    def test_allows_legitimate_delete_without_allowed_root(self, tmp_path):
        """Callers that don't pass allowed_root keep working (no containment
        boundary enforced, but the is_symlink() guard still applies)."""
        real_file = tmp_path / "doc.pdf"
        real_file.write_bytes(b"content")

        result = CascadeHelper.delete_filesystem_file(str(real_file))

        assert result is True
        assert not real_file.exists()


class TestCascadeHelperDeleteFaissIndexFiles:
    """Tests for delete_faiss_index_files static method."""

    def test_deletes_faiss_and_pkl_files(self, tmp_path):
        """Should delete both .faiss and .pkl files."""
        base_path = tmp_path / "index"
        faiss_file = base_path.with_suffix(".faiss")
        pkl_file = base_path.with_suffix(".pkl")

        faiss_file.write_bytes(b"faiss content")
        pkl_file.write_bytes(b"pkl content")

        result = CascadeHelper.delete_faiss_index_files(str(base_path))

        assert result is True
        assert not faiss_file.exists()
        assert not pkl_file.exists()

    def test_returns_false_for_none_path(self):
        """Should return False for None path."""
        result = CascadeHelper.delete_faiss_index_files(None)

        assert result is False

    def test_returns_false_when_no_files_exist(self, tmp_path):
        """Should return False when no files to delete."""
        result = CascadeHelper.delete_faiss_index_files(
            str(tmp_path / "nonexistent_index")
        )

        assert result is False

    def test_deletes_within_allowed_root(self, tmp_path):
        """A real index family directly under allowed_root is deletable."""
        rag_root = tmp_path / "rag_indices"
        rag_root.mkdir()
        base_path = rag_root / "index"
        faiss_file = base_path.with_suffix(".faiss")
        faiss_file.write_bytes(b"faiss content")

        result = CascadeHelper.delete_faiss_index_files(
            str(base_path), allowed_root=rag_root
        )

        assert result is True
        assert not faiss_file.exists()

    def test_refuses_escape_via_symlinked_ancestor_directory(self, tmp_path):
        """A symlinked ancestor directory must not let the delete escape
        allowed_root."""
        outside = tmp_path / "outside"
        outside.mkdir()
        secret_faiss = outside / "index.faiss"
        secret_faiss.write_bytes(b"do not delete me")

        rag_root = tmp_path / "rag_indices"
        rag_root.mkdir()
        (rag_root / "user_hash").symlink_to(outside)

        escaping_path = rag_root / "user_hash" / "index"
        result = CascadeHelper.delete_faiss_index_files(
            str(escaping_path), allowed_root=rag_root
        )

        assert result is False
        assert secret_faiss.exists()

    def test_refuses_an_escape_behind_a_path_max_symlink_chain(self, tmp_path):
        """Same PATH_MAX chain as the filesystem-file case, for an index."""
        rag_root = tmp_path / "rag_indices"
        rag_root.mkdir()
        outside = tmp_path / "outside"
        outside.mkdir()
        secret_faiss = outside / "index.faiss"
        secret_faiss.write_bytes(b"do not delete me")
        link = build_path_max_escape(rag_root, outside)

        result = CascadeHelper.delete_faiss_index_files(
            str(rag_root / link / "index"), allowed_root=rag_root
        )

        assert result is False
        assert secret_faiss.exists()

    def test_refuses_to_delete_symlinked_faiss_file(self, tmp_path):
        """A symlinked .faiss file itself must not be unlinked/followed."""
        outside = tmp_path / "outside"
        outside.mkdir()
        real_target = outside / "real.faiss"
        real_target.write_bytes(b"do not delete me")

        base_path = tmp_path / "index"
        symlinked_faiss = base_path.with_suffix(".faiss")
        symlinked_faiss.symlink_to(real_target)

        result = CascadeHelper.delete_faiss_index_files(str(base_path))

        assert result is False
        assert real_target.exists()
        assert symlinked_faiss.is_symlink()


class TestCascadeHelperDeleteRagIndicesForCollection:
    """Tests for delete_rag_indices_for_collection static method."""

    def test_deletes_indices_and_files(self, tmp_path):
        """Should delete RAGIndex records and FAISS files."""
        mock_session = MagicMock()

        # unlink_files=True (the default) derives its containment root from
        # get_cache_directory() / "rag_indices" -- nest the fixture's files
        # under a "rag_indices" dir and point get_cache_directory at
        # tmp_path so that root resolves there.
        rag_indices_dir = tmp_path / "rag_indices"
        rag_indices_dir.mkdir()

        # Create mock index with file path
        mock_index = MagicMock()
        mock_index.index_path = str(rag_indices_dir / "index")
        mock_session.query.return_value.filter_by.return_value.all.return_value = [
            mock_index
        ]

        # Create the files
        faiss_file = (rag_indices_dir / "index").with_suffix(".faiss")
        faiss_file.write_bytes(b"faiss content")

        with patch(
            "local_deep_research.config.paths.get_cache_directory",
            return_value=tmp_path,
        ):
            result = CascadeHelper.delete_rag_indices_for_collection(
                mock_session, "collection_abc"
            )

        assert result["deleted_indices"] == 1
        assert result["deleted_files"] == 1
        assert not faiss_file.exists()
        mock_session.delete.assert_called_with(mock_index)

    def test_returns_zero_when_no_indices(self):
        """Should return 0 when no indices exist."""
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.all.return_value = []

        result = CascadeHelper.delete_rag_indices_for_collection(
            mock_session, "collection_abc"
        )

        assert result["deleted_indices"] == 0
        assert result["deleted_files"] == 0


class TestCascadeHelperUpdateDownloadTracker:
    """Tests for update_download_tracker static method."""

    def test_updates_tracker_when_found(self):
        """Should update tracker when document has URL."""
        mock_session = MagicMock()
        mock_doc = MagicMock()
        mock_doc.original_url = "https://example.com/paper.pdf"
        mock_doc.id = "document-1"
        mock_doc.document_hash = "content-hash"

        mock_tracker = MagicMock()
        mock_tracker.library_document_id = mock_doc.id
        mock_tracker.file_hash = mock_doc.document_hash
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_tracker

        # Patch where update_download_tracker's call chain reads it:
        # find_tracker_by_url -> get_url_hash_candidates resolves
        # get_url_hash from utilities.download_url module globals, not
        # from the research_library.utils re-export shim.
        with patch(
            "local_deep_research.utilities.download_url.get_url_hash",
            return_value="abc123",
        ):
            result = CascadeHelper.update_download_tracker(
                mock_session, mock_doc
            )

        assert result is True
        assert mock_tracker.is_downloaded is False

    def test_returns_false_when_no_url(self):
        """Should return False when document has no URL."""
        mock_session = MagicMock()
        mock_doc = MagicMock()
        mock_doc.original_url = None

        result = CascadeHelper.update_download_tracker(mock_session, mock_doc)

        assert result is False


class TestCascadeHelperCountDocumentInCollections:
    """Tests for count_document_in_collections static method."""

    def test_returns_collection_count(self):
        """Should return number of collections document is in."""
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.count.return_value = 3

        count = CascadeHelper.count_document_in_collections(
            mock_session, "doc-123"
        )

        assert count == 3


class TestCascadeHelperGetDocumentCollections:
    """Tests for get_document_collections static method."""

    def test_returns_collection_ids(self):
        """Should return list of collection IDs."""
        mock_session = MagicMock()

        mock_dc1 = MagicMock()
        mock_dc1.collection_id = "col-1"
        mock_dc2 = MagicMock()
        mock_dc2.collection_id = "col-2"

        mock_session.query.return_value.filter_by.return_value.all.return_value = [
            mock_dc1,
            mock_dc2,
        ]

        collections = CascadeHelper.get_document_collections(
            mock_session, "doc-123"
        )

        assert collections == ["col-1", "col-2"]


class TestCascadeHelperDeleteDocumentCompletely:
    """Tests for delete_document_completely static method."""

    def test_deletes_document_and_related_records(self):
        """Should delete document, blob, and collection links."""
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.delete.return_value = 1

        result = CascadeHelper.delete_document_completely(
            mock_session, "doc-123"
        )

        assert result is True
        # Should have called delete for blob, collection links, and document
        assert (
            mock_session.query.return_value.filter_by.return_value.delete.call_count
            >= 3
        )

    def test_returns_false_when_document_not_found(self):
        """Should return False when document doesn't exist."""
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.delete.return_value = 0

        result = CascadeHelper.delete_document_completely(
            mock_session, "nonexistent"
        )

        assert result is False