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.

14.4 KB
Raw Blame
"""The pre-migration backup must not delete backups the user chose to keep.

``DatabaseManager._open_user_database_cold`` writes a forced backup before
running pending migrations. It used to build ``BackupService`` with the
library defaults (keep 1 backup, max 7 days), so the cleanup after that
backup deleted every older backup, ignoring the user's
``backup.max_count`` / ``backup.max_age_days``. These tests drive the real
open path with real SQLCipher files and check which backups survive.
"""

import os
import time
import uuid

import pytest

import local_deep_research.database.initialize as init_mod

pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")

DAY = 24 * 60 * 60


@pytest.fixture
def manager(tmp_path, monkeypatch):
    from local_deep_research.database.encrypted_db import DatabaseManager

    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    monkeypatch.delenv("LDR_BACKUP_MAX_COUNT", raising=False)
    monkeypatch.delenv("LDR_BACKUP_MAX_AGE_DAYS", raising=False)
    mgr = DatabaseManager()
    if not mgr.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode)")
    monkeypatch.setattr(mgr, "_run_phase2_rekey", lambda *a, **k: None)
    yield mgr
    mgr.close_all_databases()


def _plant_backups(username, ages_days):
    """Create fake backup files with the given ages; return their paths."""
    from local_deep_research.config.paths import get_user_backup_directory

    backup_dir = get_user_backup_directory(username)
    now = time.time()
    paths = []
    for i, age in enumerate(ages_days):
        path = backup_dir / f"ldr_backup_20200101_00000{i}.db"
        path.write_bytes(b"older backup")
        mtime = now - age * DAY
        os.utime(path, (mtime, mtime))
        paths.append(path)
    return backup_dir, paths


def _reopen_with_pending_migration(manager, monkeypatch, username, password):
    manager.close_user_database(username)
    monkeypatch.setattr(
        "local_deep_research.database.alembic_runner.needs_migration",
        lambda *a, **k: True,
    )
    monkeypatch.setattr(init_mod, "initialize_database", lambda *a, **k: None)
    assert manager.open_user_database(username, password) is not None


@pytest.mark.parametrize(
    ("max_count", "max_age_days", "ages", "kept", "pruned"),
    [
        # Age pruning: the 10-day backup is inside both limits and must
        # survive (it would not under the 7-day default or with the two
        # values swapped); the 40-day one is inside max_count=5 and is
        # removed only by max_age_days=30.
        pytest.param(5, 30, [1, 10, 40], [0, 1], [2], id="age"),
        # Count pruning: all planted backups are inside max_age_days; the
        # new backup plus the two newest fill max_count=3.
        pytest.param(3, 30, [1, 2, 4, 5], [0, 1], [2, 3], id="count"),
    ],
)
def test_premigration_backup_applies_user_retention_settings(
    manager, monkeypatch, max_count, max_age_days, ages, kept, pruned
):
    """The pre-migration backup prunes with the user's backup.max_count and
    backup.max_age_days, each passed through to BackupService."""
    from local_deep_research.settings.manager import SettingsManager

    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with manager.get_session(username) as session:
        sm = SettingsManager(session)
        assert sm.set_setting("backup.max_count", max_count)
        assert sm.set_setting("backup.max_age_days", max_age_days)
    assert engine is not None

    backup_dir, planted = _plant_backups(username, ages)
    _reopen_with_pending_migration(manager, monkeypatch, username, password)

    remaining = set(backup_dir.glob("ldr_backup_*.db"))
    new = remaining - set(planted)
    assert len(new) == 1, "the pre-migration backup was not written"
    for i in kept:
        assert planted[i] in remaining, (
            f"pre-migration backup deleted the {ages[i]}-day-old backup, "
            "which the user's retention settings keep"
        )
    for i in pruned:
        assert planted[i] not in remaining, (
            f"the {ages[i]}-day-old backup is outside the user's retention "
            "settings but was kept"
        )


def test_premigration_backup_survives_huge_max_age_days_override(
    manager, monkeypatch
):
    """An env override such as LDR_BACKUP_MAX_AGE_DAYS=1000000 ("keep
    forever") is above the setting's maximum, so the pre-migration backup
    does not trust it: the new backup is kept and nothing is pruned."""
    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-3!"  # noqa: S105
    manager.create_user_database(username, password)
    monkeypatch.setenv("LDR_BACKUP_MAX_AGE_DAYS", "1000000")

    backup_dir, planted = _plant_backups(username, [1, 20])
    _reopen_with_pending_migration(manager, monkeypatch, username, password)

    remaining = set(backup_dir.glob("ldr_backup_*.db"))
    assert len(remaining - set(planted)) == 1, (
        "the pre-migration backup was deleted after it was written"
    )
    assert set(planted) <= remaining


def test_cleanup_failure_keeps_the_finalized_backup(manager, monkeypatch):
    """BackupService itself (the login backup path): if removing older
    backups raises, the backup that was just finalized is kept and the
    result is still a success."""
    from local_deep_research.database.backup.backup_service import (
        BackupService,
    )

    def _failing_cleanup(self, *args, **kwargs):
        raise RuntimeError("cleanup failed")

    monkeypatch.setattr(BackupService, "_cleanup_old_backups", _failing_cleanup)
    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-4!"  # noqa: S105
    manager.create_user_database(username, password)
    backup_dir, planted = _plant_backups(username, [1])

    result = BackupService(
        username=username,
        password=password,
        max_backups=5,
        max_age_days=7,
    ).create_backup(force=True)

    assert result.success, result.error
    assert result.backup_path is not None and result.backup_path.exists()
    assert planted[0].exists()


@pytest.mark.parametrize(
    "max_age_days",
    [740_000, 1_000_000, 999_999_999, float("inf"), float("nan")],
    ids=["740000", "1000000", "999999999", "inf", "nan"],
)
def test_out_of_range_max_age_days_still_applies_max_count(
    manager, max_age_days
):
    """BackupService itself (the login backup path): a max_age_days that
    cannot be turned into a date (e.g. LDR_BACKUP_MAX_AGE_DAYS=1000000,
    "keep forever") disables only the age limit. The count limit must
    still prune, and stale .tmp files must still be removed; otherwise
    every daily backup is kept until the disk fills."""
    from local_deep_research.database.backup.backup_service import (
        BackupService,
    )

    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-5!"  # noqa: S105
    manager.create_user_database(username, password)
    backup_dir, planted = _plant_backups(username, [1, 2, 3, 4])
    stale_tmp = backup_dir / "ldr_backup_20200101_000009.db.tmp"
    stale_tmp.write_bytes(b"interrupted backup")
    old = time.time() - 2 * 60 * 60
    os.utime(stale_tmp, (old, old))

    result = BackupService(
        username=username,
        password=password,
        max_backups=2,
        max_age_days=max_age_days,
    ).create_backup(force=True)

    assert result.success, result.error
    remaining = set(backup_dir.glob("ldr_backup_*.db"))
    assert result.backup_path in remaining
    assert remaining == {result.backup_path, planted[0]}, (
        "max_count=2 was not applied: expected the new backup and the "
        "newest planted one only"
    )
    assert not stale_tmp.exists(), "stale .tmp file was not removed"


@pytest.mark.parametrize(
    ("max_backups", "max_age_days", "kept"),
    [
        # max_age_days <= 0 ("keep forever"): no age limit, so every
        # planted backup is inside max_count=5 and is kept.
        pytest.param(5, 0, [0, 1, 2], id="age-0"),
        pytest.param(5, -1, [0, 1, 2], id="age-negative"),
        pytest.param(5, -30.5, [0, 1, 2], id="age-negative-float"),
        # max_count < 1 is treated as 1: the new backup is the one kept.
        pytest.param(0, 7, [], id="count-0"),
        pytest.param(-1, 7, [], id="count-negative"),
        pytest.param(0, 0, [], id="both-0"),
        pytest.param(-3, -3, [], id="both-negative"),
    ],
)
def test_non_positive_retention_never_deletes_the_new_backup(
    manager, max_backups, max_age_days, kept
):
    """BackupService itself (the login backup path passes the raw
    settings and env overrides): LDR_BACKUP_MAX_AGE_DAYS=0 or a negative
    value means no age limit, and LDR_BACKUP_MAX_COUNT=0 or a negative
    value is treated as 1. Neither may delete the backup just written."""
    from local_deep_research.database.backup.backup_service import (
        BackupService,
    )

    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-6!"  # noqa: S105
    manager.create_user_database(username, password)
    backup_dir, planted = _plant_backups(username, [1, 2, 3])

    result = BackupService(
        username=username,
        password=password,
        max_backups=max_backups,
        max_age_days=max_age_days,
    ).create_backup(force=True)

    assert result.success, result.error
    assert result.backup_path is not None and result.backup_path.exists(), (
        "the sign-in backup deleted the backup it had just written"
    )
    remaining = set(backup_dir.glob("ldr_backup_*.db"))
    assert remaining == {result.backup_path} | {planted[i] for i in kept}


def test_new_backup_kept_when_an_older_file_has_a_later_mtime(manager):
    """The new backup always takes the first slot of the count limit, even
    when another backup file's mtime is later (clock skew, a restored or
    copied file), so max_count=1 prunes the other file, not the new one."""
    from local_deep_research.database.backup.backup_service import (
        BackupService,
    )

    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-7!"  # noqa: S105
    manager.create_user_database(username, password)
    backup_dir, planted = _plant_backups(username, [-1])

    result = BackupService(
        username=username,
        password=password,
        max_backups=1,
        max_age_days=7,
    ).create_backup(force=True)

    assert result.success, result.error
    assert result.backup_path is not None and result.backup_path.exists(), (
        "cleanup deleted the backup it had just written"
    )
    assert set(backup_dir.glob("ldr_backup_*.db")) == {result.backup_path}


def test_nan_max_backups_means_no_count_limit(manager):
    """A max_count that is NaN cannot be compared with a count, so, like an
    unusable max_age_days, it turns off only the count limit; the age
    limit still prunes."""
    from local_deep_research.database.backup.backup_service import (
        BackupService,
    )

    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-8!"  # noqa: S105
    manager.create_user_database(username, password)
    backup_dir, planted = _plant_backups(username, [1, 2, 3, 20])

    result = BackupService(
        username=username,
        password=password,
        max_backups=float("nan"),
        max_age_days=7,
    ).create_backup(force=True)

    assert result.success, result.error
    remaining = set(backup_dir.glob("ldr_backup_*.db"))
    assert remaining == {result.backup_path, *planted[:3]}


def test_premigration_backup_keeps_all_when_settings_unreadable(
    manager, monkeypatch
):
    """Without a readable settings table the retention limits are unknown,
    so the pre-migration backup deletes nothing."""
    monkeypatch.setattr(init_mod, "initialize_database", lambda *a, **k: None)
    monkeypatch.setattr(
        "local_deep_research.database.alembic_runner.needs_migration",
        lambda *a, **k: False,
    )
    username = f"premig_{uuid.uuid4().hex[:8]}"
    password = "PreMigration-Pw-2!"  # noqa: S105
    manager.create_user_database(username, password)

    backup_dir, planted = _plant_backups(username, [1, 2, 20])
    _reopen_with_pending_migration(manager, monkeypatch, username, password)

    remaining = set(backup_dir.glob("ldr_backup_*.db"))
    assert set(planted) <= remaining, (
        "pre-migration backup deleted backups although the user's "
        "retention settings could not be read"
    )
    assert len(remaining - set(planted)) == 1


@pytest.mark.parametrize(
    ("values", "expected"),
    [
        ({"backup.max_count": 4, "backup.max_age_days": 30}, (4, 30)),
        ({"backup.max_count": 4.0, "backup.max_age_days": 30.0}, (4, 30)),
        ({"backup.max_count": 0, "backup.max_age_days": 30}, None),
        ({"backup.max_count": 4, "backup.max_age_days": None}, None),
        ({"backup.max_count": "4", "backup.max_age_days": 30}, None),
        ({"backup.max_count": True, "backup.max_age_days": 30}, None),
        ({"backup.max_count": 30, "backup.max_age_days": 90}, (30, 90)),
        ({"backup.max_count": 31, "backup.max_age_days": 30}, None),
        ({"backup.max_count": 4, "backup.max_age_days": 91}, None),
        ({"backup.max_count": 4, "backup.max_age_days": 1_000_000}, None),
    ],
)
def test_read_backup_retention_rejects_unusable_values(
    monkeypatch, values, expected
):
    from local_deep_research.database import encrypted_db
    from local_deep_research.settings.manager import SettingsManager

    monkeypatch.setattr(
        SettingsManager,
        "get_setting",
        lambda self, key, default=None, check_env=True: values[key],
    )
    engine = encrypted_db.create_engine("sqlite://")
    try:
        assert encrypted_db._read_backup_retention(engine) == expected
    finally:
        engine.dispose()


def test_retention_limits_match_default_settings():
    """The helper's ceilings are the settings' own max_value."""
    import json
    from pathlib import Path

    from local_deep_research.database import encrypted_db

    defaults_path = (
        Path(encrypted_db.__file__).resolve().parent.parent
        / "defaults"
        / "default_settings.json"
    )
    defaults = json.loads(defaults_path.read_text(encoding="utf-8"))
    assert (
        defaults["backup.max_count"]["max_value"]
        == encrypted_db._BACKUP_MAX_COUNT_LIMIT
    )
    assert (
        defaults["backup.max_age_days"]["max_value"]
        == encrypted_db._BACKUP_MAX_AGE_DAYS_LIMIT
    )