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.

5.1 KB
Raw Blame
"""Tests for the operator gate on unencrypted filesystem PDF storage.

The ``filesystem`` PDF storage mode writes library-downloaded third-party
PDFs as plaintext to disk. It is now an environment-only operator gate
(``research_library.allow_filesystem_pdf_storage`` /
``LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE``) and disabled by
default. These tests cover the runtime enforcement point:
``filesystem_pdf_storage_allowed`` and ``resolve_pdf_storage_mode``, plus the
consumption behavior (a coerced mode must not reach ``_save_to_filesystem``).
"""

from unittest.mock import MagicMock, patch

import pytest

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

ENV_VAR = "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE"


@pytest.fixture
def gate_off(monkeypatch):
    monkeypatch.delenv(ENV_VAR, raising=False)


@pytest.fixture
def gate_on(monkeypatch):
    monkeypatch.setenv(ENV_VAR, "true")


class TestFilesystemPdfStorageGate:
    def test_gate_off_by_default(self, gate_off):
        assert filesystem_pdf_storage_allowed() is False

    def test_gate_on_when_env_set(self, gate_on):
        assert filesystem_pdf_storage_allowed() is True

    @pytest.mark.parametrize("truthy", ["true", "1", "yes", "on", "enabled"])
    def test_gate_on_accepts_truthy_env_values(self, monkeypatch, truthy):
        monkeypatch.setenv(ENV_VAR, truthy)
        assert filesystem_pdf_storage_allowed() is True

    def test_resolve_coerces_filesystem_when_gate_off(self, gate_off):
        assert resolve_pdf_storage_mode("filesystem") == "database"

    @pytest.mark.parametrize(
        "value", ["FILESYSTEM", " filesystem ", "Filesystem"]
    )
    def test_resolve_coercion_is_case_and_padding_insensitive(
        self, gate_off, value
    ):
        assert resolve_pdf_storage_mode(value) == "database"

    def test_resolve_coercion_warns_once(
        self, gate_off, loguru_caplog, monkeypatch
    ):
        from local_deep_research.research_library.services import (
            pdf_storage_manager,
        )

        # monkeypatch restores the module global for whatever runs next.
        monkeypatch.setattr(pdf_storage_manager, "_COERCION_WARNED", False)
        with loguru_caplog.at_level("WARNING"):
            assert resolve_pdf_storage_mode("filesystem") == "database"
            assert resolve_pdf_storage_mode("filesystem") == "database"

        coercion_warnings = [
            r
            for r in loguru_caplog.records
            if "coercing pdf_storage_mode 'filesystem' -> 'database'"
            in r.message
        ]
        assert len(coercion_warnings) == 1

    def test_resolve_preserves_filesystem_when_gate_on(self, gate_on):
        assert resolve_pdf_storage_mode("filesystem") == "filesystem"

    @pytest.mark.parametrize("mode", ["database", "none", "", "unknown"])
    def test_resolve_passthrough_for_non_filesystem_modes(self, gate_off, mode):
        assert resolve_pdf_storage_mode(mode) == mode


class TestGateEnforcedAtConsumption:
    """A filesystem mode that survives to a PDFStorageManager must, once
    resolved through the gate, write to the encrypted database rather than
    plaintext on disk."""

    def test_gate_off_coerced_mode_never_writes_plaintext(
        self, gate_off, tmp_path, mock_pdf_content
    ):
        # Simulate a stored/env value of "filesystem" reaching the consumer.
        effective_mode = resolve_pdf_storage_mode("filesystem")
        assert effective_mode == "database"

        manager = PDFStorageManager(tmp_path, effective_mode)
        mock_doc = MagicMock()
        mock_doc.id = "doc-123"
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = None

        with patch.object(manager, "_save_to_filesystem") as save_to_fs:
            result, _ = manager.save_pdf(
                mock_pdf_content, mock_doc, mock_session, "test.pdf"
            )

        # No plaintext write happened; the encrypted DB path was taken.
        save_to_fs.assert_not_called()
        assert result == "database"
        assert mock_doc.storage_mode == "database"
        assert mock_session.add.called
        # No plaintext file was created under the library root.
        assert not list(tmp_path.rglob("*.pdf"))

    def test_gate_on_honors_filesystem_write(
        self, gate_on, tmp_path, mock_pdf_content
    ):
        effective_mode = resolve_pdf_storage_mode("filesystem")
        assert effective_mode == "filesystem"

        manager = PDFStorageManager(tmp_path, effective_mode)
        mock_doc = MagicMock()
        mock_doc.id = "doc-123"
        mock_session = MagicMock()

        with patch.object(
            manager,
            "_save_to_filesystem",
            return_value=tmp_path / "pdfs" / "test.pdf",
        ) as save_to_fs:
            result, _ = manager.save_pdf(
                mock_pdf_content, mock_doc, mock_session, "test.pdf"
            )

        save_to_fs.assert_called_once()
        assert mock_doc.storage_mode == "filesystem"
        assert result == "pdfs/test.pdf"