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.

6.1 KB
Raw Blame
"""Tests for PDFStorageManager._generate_filename() and _infer_storage_mode() edge cases.

Fills gaps NOT covered by test_pdf_storage_manager_edge_cases.py.
"""

from unittest.mock import Mock

import pytest

from local_deep_research.research_library.services.pdf_storage_manager import (
    PDFStorageManager,
)


@pytest.fixture()
def manager(tmp_path):
    """Create a PDFStorageManager rooted in a pytest-managed temp directory."""
    return PDFStorageManager(
        library_root=tmp_path,
        storage_mode="database",
        max_pdf_size_mb=100,
    )


# ---------------------------------------------------------------------------
# _generate_filename edge cases
# ---------------------------------------------------------------------------


class TestGenerateFilenameEdgeCases:
    """Edge cases not covered by test_pdf_storage_manager_edge_cases.py."""

    def test_arxiv_host_without_a_paper_is_not_named_arxiv(self, manager):
        """An arXiv URL that names no paper is routed as a generic page, so
        claiming `arxiv_` provenance in the filename asserted something the
        pipeline disagreed with (#6413)."""
        result = manager._generate_filename(
            "https://arxiv.org/some/page", 42, "fallback.pdf"
        )
        assert result == "fallback.pdf"

    def test_a_listing_url_is_not_named_arxiv(self, manager):
        """The URL from the issue: on the host, no identifier."""
        result = manager._generate_filename(
            "https://arxiv.org/list/cs.AI/recent", None, "fallback.pdf"
        )
        assert result == "fallback.pdf"

    def test_old_style_identifier_is_filename_safe(self, manager):
        """The shared parser accepts `cs.AI/0701001v2`, which the previous
        `\\d{4}\\.\\d{4,5}` regex silently did not. Its slash is a path
        separator, so it must not reach the filename."""
        result = manager._generate_filename(
            "https://arxiv.org/abs/cs.AI/0701001v2", 7, "fallback.pdf"
        )
        assert result == "arxiv_cs.AI_0701001.pdf"
        assert "/" not in result

    def test_the_version_does_not_reach_the_filename(self, manager):
        """`extract_arxiv_id` keeps the version, the stored name never has.
        Both versions of one paper are one library resource, so v1 and v2 must
        not land in the store as two differently named files."""
        v1 = manager._generate_filename(
            "https://arxiv.org/pdf/2401.12345v1", 7, "fallback.pdf"
        )
        v2 = manager._generate_filename(
            "https://arxiv.org/pdf/2401.12345v2", 7, "fallback.pdf"
        )
        assert v1 == v2 == "arxiv_2401.12345.pdf"

    def test_a_paper_url_still_carries_its_identifier(self, manager):
        result = manager._generate_filename(
            "https://arxiv.org/pdf/2301.00001.pdf", 7, "fallback.pdf"
        )
        assert result == "arxiv_2301.00001.pdf"

    def test_pubmed_url_without_pmc_id(self, manager):
        """PMC URL without PMC ID → fallback with timestamp."""
        result = manager._generate_filename(
            "https://ncbi.nlm.nih.gov/pmc/articles/", 99, "fallback.pdf"
        )
        assert result.startswith("pubmed_")
        assert "99" in result

    def test_pubmed_url_resource_id_none(self, manager):
        """PMC URL without PMC ID and resource_id=None → 'unknown'."""
        result = manager._generate_filename(
            "https://ncbi.nlm.nih.gov/pmc/articles/", None, "fallback.pdf"
        )
        assert "unknown" in result
        assert result.startswith("pubmed_")

    def test_url_with_no_hostname(self, manager):
        """URL with empty hostname → returns fallback."""
        result = manager._generate_filename(
            "file:///local/path.pdf", 1, "fallback.pdf"
        )
        # file:// has empty hostname → not arxiv/pmc → fallback
        assert result == "fallback.pdf"

    def test_arxiv_subdomain_without_valid_id(self, manager):
        """export.arxiv.org with no valid arXiv ID → the generic fallback."""
        result = manager._generate_filename(
            "https://export.arxiv.org/noarxivid", 5, "fallback.pdf"
        )
        assert result == "fallback.pdf"

    def test_ncbi_non_pmc_path_returns_fallback(self, manager):
        """ncbi.nlm.nih.gov but not /pmc path → returns fallback."""
        result = manager._generate_filename(
            "https://ncbi.nlm.nih.gov/pubmed/12345", 1, "fallback.pdf"
        )
        # path doesn't contain "/pmc" → not matched → fallback
        assert result == "fallback.pdf"


# ---------------------------------------------------------------------------
# _infer_storage_mode edge cases
# ---------------------------------------------------------------------------


class TestInferStorageModeEdgeCases:
    """Edge cases not covered by test_pdf_storage_manager_edge_cases.py."""

    def test_document_no_blob_no_path_returns_none(self, manager):
        doc = Mock()
        doc.blob = None
        doc.file_path = None
        assert manager._infer_storage_mode(doc) == "none"

    def test_document_without_blob_attribute(self, manager):
        """Document that doesn't have blob attribute (no hasattr)."""
        doc = Mock(spec=[])  # No attributes at all
        doc.file_path = "pdfs/paper.pdf"
        # hasattr(doc, "blob") is False → skips blob check → checks file_path
        assert manager._infer_storage_mode(doc) == "filesystem"

    def test_document_without_blob_attr_no_path(self, manager):
        """Document without blob attribute AND no file_path."""
        doc = Mock(spec=[])
        doc.file_path = None
        assert manager._infer_storage_mode(doc) == "none"

    def test_document_empty_file_path_returns_none(self, manager):
        """Empty string file_path is falsy → returns 'none'."""
        doc = Mock()
        doc.blob = None
        doc.file_path = ""
        assert manager._infer_storage_mode(doc) == "none"

    def test_blob_takes_priority_over_file_path(self, manager):
        """When both blob and file_path exist, blob wins → 'database'."""
        doc = Mock()
        doc.blob = Mock()  # truthy
        doc.file_path = "pdfs/paper.pdf"
        assert manager._infer_storage_mode(doc) == "database"