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.

5.2 KB
Raw Blame
"""
Tests for _get_min_kdf_iterations() in database/sqlcipher_utils.py

Tests cover:
- Production mode (no test env vars) returns 100K iterations
- PYTEST_CURRENT_TEST env var triggers test mode (1 iteration)
- LDR_TEST_MODE env var triggers test mode (1 iteration)
- Both env vars set → still returns 1
- Constants have correct values
"""

import pytest


class TestGetMinKdfIterations:
    """Tests for _get_min_kdf_iterations()."""

    def test_production_mode_returns_100k(self, monkeypatch):
        """No test env vars → production iterations (100_000)."""
        monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)
        monkeypatch.delenv("LDR_TEST_MODE", raising=False)

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 100_000

    def test_pytest_current_test_triggers_test_mode(self, monkeypatch):
        """PYTEST_CURRENT_TEST set → test iterations (1)."""
        monkeypatch.setenv("PYTEST_CURRENT_TEST", "tests/test_foo.py::test_bar")
        monkeypatch.delenv("LDR_TEST_MODE", raising=False)

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 1

    def test_ldr_test_mode_triggers_test_mode(self, monkeypatch):
        """LDR_TEST_MODE set → test iterations (1)."""
        monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)
        monkeypatch.setenv("LDR_TEST_MODE", "1")

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 1

    def test_both_env_vars_set(self, monkeypatch):
        """Both env vars set → still returns test iterations."""
        monkeypatch.setenv("PYTEST_CURRENT_TEST", "tests/test_foo.py::test_bar")
        monkeypatch.setenv("LDR_TEST_MODE", "1")

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 1

    def test_empty_pytest_current_test_is_production(self, monkeypatch):
        """Empty string for PYTEST_CURRENT_TEST is falsy → production mode."""
        monkeypatch.setenv("PYTEST_CURRENT_TEST", "")
        monkeypatch.delenv("LDR_TEST_MODE", raising=False)

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 100_000

    def test_empty_ldr_test_mode_is_production(self, monkeypatch):
        """Empty string for LDR_TEST_MODE is falsy → production mode."""
        monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)
        monkeypatch.setenv("LDR_TEST_MODE", "")

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 100_000

    @pytest.mark.parametrize(
        "falsey", ["0", "false", "False", "no", "off", "banana"]
    )
    def test_falsey_ldr_test_mode_is_production(self, monkeypatch, falsey):
        """LDR_TEST_MODE is parsed as a boolean: explicit falsey values (and
        unrecognised strings like 'banana') must NOT relax the floor. A bare
        truthiness check would treat any non-empty string as enabled and
        silently weaken encryption.
        """
        # Load-bearing: _get_min_kdf_iterations() also relaxes the floor when
        # PYTEST_CURRENT_TEST is present (which it always is under pytest), so
        # we must clear it to isolate the LDR_TEST_MODE behaviour under test.
        monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)
        monkeypatch.setenv("LDR_TEST_MODE", falsey)

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 100_000

    @pytest.mark.parametrize("truthy", ["true", "TRUE", "yes", "on", "enabled"])
    def test_extended_truthy_ldr_test_mode_relaxes(self, monkeypatch, truthy):
        """LDR_TEST_MODE accepts the full boolean truthy set (to_bool), not
        just '1'/'true' — locks that behavior so a narrower parser would be
        caught.
        """
        # Clear PYTEST_CURRENT_TEST so the relaxation we observe is attributable
        # to LDR_TEST_MODE, not pytest's own presence (see falsey test above).
        monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)
        monkeypatch.setenv("LDR_TEST_MODE", truthy)

        from local_deep_research.database.sqlcipher_utils import (
            _get_min_kdf_iterations,
        )

        assert _get_min_kdf_iterations() == 1


class TestKdfConstants:
    """Tests for KDF iteration constants."""

    def test_production_constant_is_100k(self):
        """MIN_KDF_ITERATIONS_PRODUCTION should be 100_000."""
        from local_deep_research.database.sqlcipher_utils import (
            MIN_KDF_ITERATIONS_PRODUCTION,
        )

        assert MIN_KDF_ITERATIONS_PRODUCTION == 100_000

    def test_testing_constant_is_1(self):
        """MIN_KDF_ITERATIONS_TESTING should be 1."""
        from local_deep_research.database.sqlcipher_utils import (
            MIN_KDF_ITERATIONS_TESTING,
        )

        assert MIN_KDF_ITERATIONS_TESTING == 1