"""
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
"""


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


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
