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.

3.5 KB
Raw Blame
"""
Tests for get_user_database_filename() and get_data_directory() in paths.py.

Covers SHA-256 hash-based filename generation and LDR_DATA_DIR env var override.
"""

import hashlib
from pathlib import Path
from unittest.mock import patch


def _get_user_db_fn():
    from local_deep_research.config.paths import get_user_database_filename

    return get_user_database_filename


def _get_data_dir_fn():
    from local_deep_research.config.paths import get_data_directory

    return get_data_directory


class TestGetUserDatabaseFilename:
    """Tests for get_user_database_filename()."""

    def test_basic_username(self):
        expected_hash = hashlib.sha256("alice".encode()).hexdigest()[:16]
        result = _get_user_db_fn()("alice")
        assert result == f"ldr_user_{expected_hash}.db"

    def test_deterministic(self):
        fn = _get_user_db_fn()
        assert fn("alice") == fn("alice")

    def test_different_users_different_filenames(self):
        fn = _get_user_db_fn()
        assert fn("alice") != fn("bob")

    def test_case_sensitive(self):
        fn = _get_user_db_fn()
        assert fn("Alice") != fn("alice")

    def test_special_characters_safe(self):
        result = _get_user_db_fn()("[email protected]")
        assert "@" not in result
        assert result.startswith("ldr_user_")
        assert result.endswith(".db")

    def test_unicode_username(self):
        result = _get_user_db_fn()("用户")
        assert result.startswith("ldr_user_")
        assert result.endswith(".db")
        # Extract hash portion and verify it's 16-char hex
        hash_part = result[len("ldr_user_") : -len(".db")]
        assert len(hash_part) == 16
        assert all(c in "0123456789abcdef" for c in hash_part)


class TestGetDataDirectory:
    """Tests for get_data_directory()."""

    def test_ldr_data_dir_set(self, monkeypatch):
        monkeypatch.setenv("LDR_DATA_DIR", "/tmp/custom")
        result = _get_data_dir_fn()()
        assert result == Path("/tmp/custom")

    def test_ldr_data_dir_empty_falls_through(self, monkeypatch):
        monkeypatch.setenv("LDR_DATA_DIR", "")
        with patch(
            "local_deep_research.config.paths.platformdirs.user_data_dir",
            return_value="/fake/platformdirs/local-deep-research",
        ):
            result = _get_data_dir_fn()()
        assert "local-deep-research" in str(result)

    def test_ldr_data_dir_unset_uses_platformdirs(self, monkeypatch):
        monkeypatch.delenv("LDR_DATA_DIR", raising=False)
        with patch(
            "local_deep_research.config.paths.platformdirs.user_data_dir",
            return_value="/fake/platformdirs/local-deep-research",
        ):
            result = _get_data_dir_fn()()
        assert result == Path("/fake/platformdirs/local-deep-research")

    def test_returns_path_object(self, monkeypatch):
        monkeypatch.setenv("LDR_DATA_DIR", "/tmp/test")
        result = _get_data_dir_fn()()
        assert isinstance(result, Path)

    def test_platformdirs_path_contains_app_name(self, monkeypatch):
        monkeypatch.delenv("LDR_DATA_DIR", raising=False)
        with patch(
            "local_deep_research.config.paths.platformdirs.user_data_dir",
            return_value="/home/user/.local/share/local-deep-research",
        ):
            result = _get_data_dir_fn()()
        assert "local-deep-research" in str(result)

    def test_ldr_data_dir_with_spaces(self, monkeypatch):
        monkeypatch.setenv("LDR_DATA_DIR", "/tmp/my data")
        result = _get_data_dir_fn()()
        assert result == Path("/tmp/my data")