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.

8.5 KB
Raw Blame
#!/usr/bin/env python3
"""
Test backwards compatibility for encrypted databases.

This test ensures that databases created with previous versions can still
be opened with the current version. This prevents breaking changes like
salt modifications from going undetected.

The full probe installs the latest published version, creates an encrypted
database outside the checkout, and opens it with the candidate. Once explicitly
enabled, setup failures must fail the probe rather than report a skipped success.

The only exception is KNOWN_UNINSTALLABLE_RELEASES: exact published versions
whose dependency metadata pip cannot resolve. A published package cannot be
fixed, so such a version is never chosen as the predecessor; the probe still
runs against the newest stable release that is not listed. Any other install
failure fails the probe (and the release gate that requires it). If a newly
published release turns out to be uninstallable, either yank it on PyPI (yanked
files are ignored) or add its exact version here, with the reason, in a
reviewed commit.
Run it manually with:
    RUN_SLOW_TESTS=true pytest tests/performance/database/test_backwards_compatibility.py
"""

import json
import os
import subprocess
import sys
import tempfile
from pathlib import Path
from urllib.request import urlopen

import pytest
from packaging.version import Version


# Exact published versions that can never be installed, mapped to the reason.
# Keep this narrow: list a version only after confirming its metadata is
# unresolvable, and never use it to hide a database compatibility failure.
KNOWN_UNINSTALLABLE_RELEASES = {
    # #3331: the PDM override requests>=2.33 leaked into the published
    # metadata and conflicts with arxiv~=2.4's requests~=2.32.0 pin.
    "1.4.0": "requests>=2.33 conflicts with arxiv~=2.4 (requests~=2.32.0)",
}

# Path to the script that creates a database using the installed package
# This is in a separate file for better IDE support (syntax highlighting, linting)
CREATE_DB_SCRIPT_PATH = (
    Path(__file__).parent / "scripts" / "create_compat_db.py"
)


class TestBackwardsCompatibility:
    """Test that current version can open databases from previous versions."""

    @pytest.fixture
    def temp_dir(self):
        """Create a temporary directory for test databases."""
        with tempfile.TemporaryDirectory() as tmpdir:
            yield Path(tmpdir)

    def get_previous_version(self) -> str:
        """Resolve the latest installable stable release without requiring pip.

        Yanked releases and the exact versions in KNOWN_UNINSTALLABLE_RELEASES
        are not candidates; everything else is.
        """
        with urlopen(
            "https://pypi.org/pypi/local-deep-research/json", timeout=30
        ) as response:
            payload = json.load(response)
        # The candidate is unreleased: the newest published stable package is
        # its predecessor, not the second entry returned by `pip index`.
        versions = [
            Version(version)
            for version, files in payload["releases"].items()
            if files and any(not file.get("yanked", False) for file in files)
        ]
        excluded = {Version(v) for v in KNOWN_UNINSTALLABLE_RELEASES}
        stable = [
            v
            for v in versions
            if not v.is_prerelease and not v.is_devrelease and v not in excluded
        ]
        if not stable:
            raise ValueError(
                "PyPI has no non-yanked stable release outside "
                "KNOWN_UNINSTALLABLE_RELEASES"
            )
        return str(max(stable))

    @pytest.mark.slow
    @pytest.mark.timeout(900)  # Includes installation and real database checks.
    @pytest.mark.skipif(
        os.environ.get("RUN_SLOW_TESTS") != "true",
        reason="Slow test - set RUN_SLOW_TESTS=true to run",
    )
    def test_open_database_from_previous_version(self, temp_dir, monkeypatch):
        """
        Test that we can open a database created with the previous PyPI version.

        This test:
        1. Installs the previous version in an isolated venv
        2. Creates a database using that version
        3. Verifies the current version can open it and read data
        """
        # Get previous version
        try:
            previous_version = self.get_previous_version()
        except Exception as e:
            pytest.fail(f"Could not get previous version: {e}")

        print(
            f"Testing encrypted database upgrade from PyPI {previous_version}"
        )
        # Do not let a checkout's PYTHONPATH replace the installed predecessor.
        child_env = os.environ.copy()
        child_env.pop("PYTHONPATH", None)

        # Create isolated venv for previous version
        venv_dir = temp_dir / "prev_venv"
        db_dir = temp_dir / "databases"
        db_dir.mkdir()

        # Create venv
        result = subprocess.run(
            [sys.executable, "-m", "venv", str(venv_dir)],
            capture_output=True,
            text=True,
            cwd=temp_dir,
            env=child_env,
            timeout=60,
        )
        if result.returncode != 0:
            pytest.fail(f"Could not create venv: {result.stderr}")

        # Get venv python
        if sys.platform == "win32":
            venv_python = venv_dir / "Scripts" / "python.exe"
        else:
            venv_python = venv_dir / "bin" / "python"

        # Install previous version (with timeout)
        # Package has many dependencies, needs sufficient time
        result = subprocess.run(
            [
                str(venv_python),
                "-m",
                "pip",
                "install",
                f"local-deep-research=={previous_version}",
                "--quiet",
            ],
            capture_output=True,
            text=True,
            cwd=temp_dir,
            env=child_env,
            timeout=600,  # 10 minute timeout - package has many dependencies
        )
        if result.returncode != 0:
            pytest.fail(f"Could not install previous version: {result.stderr}")

        # Create database with previous version
        username = "compat_test_user"
        password = "CompatTestPass123!"

        # Copy the script to temp dir (script is in separate file for IDE support)
        script_file = temp_dir / "create_db.py"
        script_file.write_text(CREATE_DB_SCRIPT_PATH.read_text())

        result = subprocess.run(
            [
                str(venv_python),
                str(script_file),
                str(db_dir),
                username,
                password,
            ],
            capture_output=True,
            text=True,
            cwd=temp_dir,
            env=child_env,
            timeout=60,
        )
        if result.returncode != 0:
            pytest.fail(
                f"Could not create database with previous version: {result.stderr}"
            )

        # Now test opening with current version
        monkeypatch.setattr(
            "local_deep_research.database.encrypted_db.get_data_directory",
            lambda: db_dir,
        )

        from local_deep_research.database.encrypted_db import (
            DatabaseManager,
        )
        from local_deep_research.database.models import UserSettings

        manager = DatabaseManager()
        assert manager.has_encryption, (
            "Candidate SQLCipher support is unavailable"
        )
        manager.data_dir = db_dir / "encrypted_databases"

        # Try to open the database
        engine = manager.open_user_database(username, password)
        assert engine is not None, (
            f"Failed to open database created with version {previous_version}. "
            "This likely means a breaking change was introduced (e.g., salt change)."
        )

        # Verify we can read the data
        try:
            with manager.get_session(username) as session:
                setting = (
                    session.query(UserSettings)
                    .filter_by(key="test.backwards_compat")
                    .first()
                )

                assert setting is not None, (
                    "Could not read data from previous version database"
                )
                assert setting.value["version"] == "previous"
                assert setting.value["test"] is True
                assert setting.value["package_version"] == previous_version
        finally:
            manager.close_user_database(username)


# Note: Salt stability tests have been moved to test_encryption_constants.py
# This file now only contains the slow PyPI backwards compatibility test.


if __name__ == "__main__":
    pytest.main([__file__, "-v"])