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.

4.7 KB
Raw Blame
"""Backup code paths must not write the user's real data directory under test.

Backup attempts persist only in an already-open encrypted user database and
otherwise stay in memory. The shared test guard still prevents an accidental
write to a developer's already-connected database when only backup service
path helpers were patched. Namespace/status reads must not create directories.

The "real" data directory here is a watched directory inside ``tmp_path``
reached through a patched ``platformdirs.user_data_dir`` with ``LDR_DATA_DIR``
unset, so a regression cannot touch a developer's actual files.
"""

import errno
import threading
from unittest.mock import MagicMock

import platformdirs
import pytest

from local_deep_research.database.backup import backup_service, backup_status
from local_deep_research.database.backup.backup_executor import BackupExecutor
from local_deep_research.database.backup.backup_service import BackupService


@pytest.fixture
def real_data_dir(tmp_path, monkeypatch):
    """A watched stand-in for the real platform data directory."""
    watched = tmp_path / "real-user-data"
    monkeypatch.delenv("LDR_DATA_DIR", raising=False)
    monkeypatch.setattr(
        platformdirs, "user_data_dir", lambda *args, **kwargs: str(watched)
    )
    return watched


@pytest.fixture
def backup_dir(tmp_path, monkeypatch, real_data_dir):
    """Point the path helpers at ``tmp_path`` the way the backup tests do.

    Only the helpers move; the data directory itself stays unpinned, which is
    exactly what reached the real auth database.
    """
    directory = tmp_path / "backups"
    directory.mkdir()
    monkeypatch.setattr(
        backup_service, "get_user_backup_directory", lambda username: directory
    )
    monkeypatch.setattr(
        backup_service,
        "get_encrypted_database_path",
        lambda: tmp_path / "databases",
    )
    return directory


@pytest.fixture
def service(backup_dir):
    return BackupService("alice", "not-a-real-password")


def _assert_nothing_written(watched):
    written = (
        sorted(str(path.relative_to(watched)) for path in watched.rglob("*"))
        if watched.exists()
        else []
    )
    assert not written, f"a backup path wrote outside tmp_path: {written}"


def test_failed_backup_leaves_the_real_data_dir_alone(service, real_data_dir):
    result = service.create_backup(force=True)

    assert result.error_code == backup_status.BackupError.SOURCE_MISSING
    _assert_nothing_written(real_data_dir)


def test_password_change_refresh_leaves_the_real_data_dir_alone(
    service, real_data_dir
):
    result = service.purge_and_refresh()

    assert result.error_code == backup_status.BackupError.SOURCE_MISSING
    _assert_nothing_written(real_data_dir)


def test_constructor_failure_leaves_the_real_data_dir_alone(
    monkeypatch, backup_dir, real_data_dir
):
    monkeypatch.setattr(
        backup_service,
        "get_user_backup_directory",
        MagicMock(side_effect=PermissionError(errno.EACCES, "private-path")),
    )

    with pytest.raises(PermissionError):
        BackupService("alice", "not-a-real-password")

    _assert_nothing_written(real_data_dir)


def test_background_backup_failure_leaves_the_real_data_dir_alone(
    monkeypatch, backup_dir, real_data_dir
):
    monkeypatch.setattr(
        backup_service,
        "get_user_backup_directory",
        MagicMock(side_effect=PermissionError(errno.EACCES, "private-path")),
    )
    executor = object.__new__(BackupExecutor)

    assert executor._run_backup("alice", "pw", 1, 7).success is False

    _assert_nothing_written(real_data_dir)


def test_scheduling_failure_leaves_the_real_data_dir_alone(real_data_dir):
    executor = object.__new__(BackupExecutor)
    executor._pending_backups = set()
    executor._pending_lock = threading.Lock()
    executor._executor = MagicMock()
    executor._executor.submit.side_effect = RuntimeError("executor closed")

    with pytest.raises(RuntimeError):
        executor.submit_backup("alice", "pw")

    _assert_nothing_written(real_data_dir)


def test_reading_the_status_leaves_the_real_data_dir_alone(real_data_dir):
    report = backup_status.get_backup_status("alice")

    assert report["last_attempt"] is None
    _assert_nothing_written(real_data_dir)


def test_a_pinned_data_dir_still_records_the_failure(
    service, tmp_path, monkeypatch
):
    """A pinned data root alone does not authorize a plaintext status store."""
    pinned = tmp_path / "pinned-data"
    monkeypatch.setenv("LDR_DATA_DIR", str(pinned))

    assert service.create_backup(force=True).success is False

    assert not list(pinned.rglob("*.db"))
    report = backup_status.get_backup_status("alice")
    assert report["status_available"] is False
    attempt = report["last_attempt"]
    assert attempt["error_code"] == "source_missing"