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.

11.0 KB
Raw Blame
"""
Behavioral tests for config/paths module.

Tests the real logic: LDR_DATA_DIR env var override, SHA-256 username hashing,
and structural invariants (all subdirs under data dir).
"""

import re

import pytest


class TestDataDirectoryEnvOverride:
    """Tests for LDR_DATA_DIR environment variable handling."""

    def test_custom_dir_from_env(self, tmp_path, monkeypatch):
        """LDR_DATA_DIR overrides the default platformdirs path."""
        from local_deep_research.config.paths import get_data_directory

        custom_dir = str(tmp_path / "custom_data")
        monkeypatch.setenv("LDR_DATA_DIR", custom_dir)
        assert get_data_directory() == tmp_path / "custom_data"

    def test_empty_env_var_uses_default(self, monkeypatch):
        """Empty LDR_DATA_DIR is falsy — falls through to platformdirs."""
        from local_deep_research.config.paths import get_data_directory

        monkeypatch.setenv("LDR_DATA_DIR", "")
        result = get_data_directory()
        # Empty string is falsy, so os.getenv returns "" but `if custom_path:`
        # is False — should use platformdirs default
        assert "local-deep-research" in str(result)

    def test_unset_env_var_uses_default(self, monkeypatch):
        """Without LDR_DATA_DIR, uses platformdirs default."""
        from local_deep_research.config.paths import get_data_directory

        monkeypatch.delenv("LDR_DATA_DIR", raising=False)
        result = get_data_directory()
        assert "local-deep-research" in str(result)

    def test_custom_data_directory_is_the_env_override(
        self, tmp_path, monkeypatch
    ):
        """With LDR_DATA_DIR set, the custom directory is the data directory."""
        from local_deep_research.config.paths import (
            get_custom_data_directory,
            get_data_directory,
        )

        monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "custom_data"))
        assert get_custom_data_directory() == tmp_path / "custom_data"
        assert get_custom_data_directory() == get_data_directory()

    @pytest.mark.parametrize("value", [None, ""])
    def test_custom_data_directory_is_none_when_unset_or_empty(
        self, monkeypatch, value
    ):
        """Unset and empty LDR_DATA_DIR both mean the platform default."""
        from local_deep_research.config.paths import get_custom_data_directory

        if value is None:
            monkeypatch.delenv("LDR_DATA_DIR", raising=False)
        else:
            monkeypatch.setenv("LDR_DATA_DIR", value)
        assert get_custom_data_directory() is None

    def test_bootstrap_data_dir_variable_moves_nothing(
        self, tmp_path, monkeypatch
    ):
        """Only LDR_DATA_DIR selects the directory; a lookalike is ignored."""
        from local_deep_research.config.paths import (
            get_custom_data_directory,
            get_data_directory,
        )

        monkeypatch.delenv("LDR_DATA_DIR", raising=False)
        baseline = get_data_directory()
        monkeypatch.setenv("LDR_BOOTSTRAP_DATA_DIR", str(tmp_path / "decoy"))

        assert get_data_directory() == baseline
        assert get_custom_data_directory() is None

    def test_custom_data_directory_rejects_a_relative_path(self, monkeypatch):
        """The helper keeps get_data_directory's validation, not its own."""
        from local_deep_research.config.paths import get_custom_data_directory

        monkeypatch.setenv("LDR_DATA_DIR", "relative/data")
        with pytest.raises(ValueError, match="absolute"):
            get_custom_data_directory()

    def test_custom_dir_propagates_to_all_subdirectories(
        self, tmp_path, monkeypatch
    ):
        """LDR_DATA_DIR affects every subdirectory function."""
        from local_deep_research.config.paths import (
            get_cache_directory,
            get_config_directory,
            get_data_directory,
            get_encrypted_database_path,
            get_library_directory,
            get_logs_directory,
            get_models_directory,
            get_research_outputs_directory,
        )

        custom_dir = str(tmp_path / "custom")
        monkeypatch.setenv("LDR_DATA_DIR", custom_dir)

        data_dir = get_data_directory()
        subdirs = [
            get_research_outputs_directory(),
            get_cache_directory(),
            get_logs_directory(),
            get_encrypted_database_path(),
            get_library_directory(),
            get_config_directory(),
            get_models_directory(),
        ]
        for subdir in subdirs:
            assert subdir.parent == data_dir, f"{subdir} not under {data_dir}"


class TestUserDatabaseFilenameHashing:
    """Tests for get_user_database_filename SHA-256 hashing logic."""

    def test_deterministic_same_input(self):
        """Same username always produces the same filename."""
        from local_deep_research.config.paths import get_user_database_filename

        assert get_user_database_filename(
            "alice"
        ) == get_user_database_filename("alice")

    def test_different_users_different_filenames(self):
        """Different usernames produce different filenames."""
        from local_deep_research.config.paths import get_user_database_filename

        assert get_user_database_filename(
            "alice"
        ) != get_user_database_filename("bob")

    def test_case_sensitive(self):
        """'Alice' and 'alice' hash differently (SHA-256 is case-sensitive)."""
        from local_deep_research.config.paths import get_user_database_filename

        assert get_user_database_filename(
            "Alice"
        ) != get_user_database_filename("alice")

    def test_hash_is_16_hex_characters(self):
        """Hash portion is exactly 16 lowercase hex characters."""
        from local_deep_research.config.paths import get_user_database_filename

        result = get_user_database_filename("testuser")
        hash_part = result.removeprefix("ldr_user_").removesuffix(".db")
        assert len(hash_part) == 16
        assert re.fullmatch(r"[0-9a-f]+", hash_part), (
            f"Non-hex chars in hash: {hash_part}"
        )

    def test_special_characters_sanitized_by_hashing(self):
        """Path-traversal chars in username don't leak into filename."""
        from local_deep_research.config.paths import get_user_database_filename

        result = get_user_database_filename("[email protected]/../../etc/passwd")
        hash_part = result.removeprefix("ldr_user_").removesuffix(".db")
        assert re.fullmatch(r"[0-9a-f]+", hash_part)

    def test_unicode_username_hashed_via_utf8(self):
        """Unicode usernames are encoded as UTF-8 before hashing."""
        from local_deep_research.config.paths import get_user_database_filename

        result = get_user_database_filename("用户名")
        hash_part = result.removeprefix("ldr_user_").removesuffix(".db")
        assert len(hash_part) == 16

    def test_empty_username_raises(self):
        """Empty string must be rejected, not silently hashed.

        sha256("") is a well-known constant (starts with e3b0c442); if it
        were accepted, two different "no username" call sites would collide
        into one shared, deterministic database filename instead of failing
        loudly. Defense-in-depth at the hash chokepoint -- see #5481.
        """
        import pytest

        from local_deep_research.config.paths import get_user_database_filename

        with pytest.raises(ValueError):
            get_user_database_filename("")

    def test_none_username_raises(self):
        """None must also be rejected before it reaches .encode()."""
        import pytest

        from local_deep_research.config.paths import get_user_database_filename

        with pytest.raises(ValueError):
            get_user_database_filename(None)


class TestUserBackupDirectoryHashing:
    """Tests for the get_user_backup_directory empty-username guard.

    Mirrors get_user_database_filename: get_user_backup_directory is the
    other sha256(username) chokepoint, and without a guard an empty/None
    username collapses to the shared sha256("") directory -- see #5481.
    """

    def test_empty_username_raises(self):
        """Empty string must be rejected, not silently hashed into the
        shared sha256("") backup directory."""
        import pytest

        from local_deep_research.config.paths import get_user_backup_directory

        with pytest.raises(ValueError):
            get_user_backup_directory("")

    def test_none_username_raises(self):
        """None must also be rejected before it reaches .encode()."""
        import pytest

        from local_deep_research.config.paths import get_user_backup_directory

        with pytest.raises(ValueError):
            get_user_backup_directory(None)


class TestSubdirectoryStructure:
    """Tests for structural invariants across all subdirectory functions."""

    def test_all_subdirs_have_correct_names_and_parent(self):
        """Each subdirectory function returns the right name under data dir."""
        from local_deep_research.config.paths import (
            get_cache_directory,
            get_config_directory,
            get_data_directory,
            get_encrypted_database_path,
            get_library_directory,
            get_logs_directory,
            get_models_directory,
            get_research_outputs_directory,
        )

        data_dir = get_data_directory()
        expected = {
            "research_outputs": get_research_outputs_directory,
            "cache": get_cache_directory,
            "logs": get_logs_directory,
            "encrypted_databases": get_encrypted_database_path,
            "library": get_library_directory,
            "config": get_config_directory,
            "models": get_models_directory,
        }
        for name, func in expected.items():
            result = func()
            assert result.parent == data_dir, (
                f"{func.__name__} not under data dir"
            )
            assert result.name == name, f"{func.__name__} has wrong name"

    def test_no_subdirectory_name_collisions(self):
        """All 7 subdirectory functions use distinct names."""
        from local_deep_research.config.paths import (
            get_cache_directory,
            get_config_directory,
            get_encrypted_database_path,
            get_library_directory,
            get_logs_directory,
            get_models_directory,
            get_research_outputs_directory,
        )

        names = [
            get_research_outputs_directory().name,
            get_cache_directory().name,
            get_logs_directory().name,
            get_encrypted_database_path().name,
            get_library_directory().name,
            get_config_directory().name,
            get_models_directory().name,
        ]
        assert len(names) == len(set(names)), f"Duplicate names: {names}"

    def test_backward_compat_get_data_dir_returns_string(self):
        """get_data_dir() returns string version of get_data_directory()."""
        from local_deep_research.config.paths import (
            get_data_dir,
            get_data_directory,
        )

        assert get_data_dir() == str(get_data_directory())