"""Tests for is_blocked_setting — security check for dangerous settings.

This function prevents users from modifying settings that could enable
dynamic code imports/execution. Zero prior test coverage.
"""

from local_deep_research.web.routes.settings_routes import is_blocked_setting


class TestIsBlockedSettingModulePaths:
    """Settings with 'module' in the key should be blocked."""

    def test_module_path_blocked(self):
        assert is_blocked_setting("search.custom_engine.module_path") is True

    def test_custom_module_blocked(self):
        assert is_blocked_setting("search.custom_module") is True

    def test_module_loader_blocked(self):
        assert is_blocked_setting("search.module_loader") is True

    def test_module_standalone_blocked(self):
        assert is_blocked_setting("module") is True


class TestIsBlockedSettingClassPaths:
    """Settings with 'class' in the key should be blocked."""

    def test_class_name_blocked(self):
        assert is_blocked_setting("search.engine.class_name") is True

    def test_handler_class_blocked(self):
        assert is_blocked_setting("handler_class") is True

    def test_class_loader_blocked(self):
        assert is_blocked_setting("class_loader") is True

    def test_class_standalone_blocked(self):
        assert is_blocked_setting("class") is True


class TestIsBlockedSettingCaseInsensitive:
    """Blocking should be case-insensitive."""

    def test_uppercase_module(self):
        assert is_blocked_setting("SEARCH.MODULE_PATH") is True

    def test_mixed_case_class(self):
        assert is_blocked_setting("Handler_Class") is True

    def test_uppercase_class(self):
        assert is_blocked_setting("CLASS_NAME") is True

    def test_mixed_case_module(self):
        assert is_blocked_setting("Custom_Module") is True


class TestIsBlockedSettingAllowedKeys:
    """Normal settings should NOT be blocked."""

    def test_temperature_allowed(self):
        assert is_blocked_setting("llm.temperature") is False

    def test_model_name_allowed(self):
        assert is_blocked_setting("llm.model_name") is False

    def test_search_engine_allowed(self):
        assert is_blocked_setting("search.engine") is False

    def test_report_format_allowed(self):
        assert is_blocked_setting("report.format") is False

    def test_app_theme_allowed(self):
        assert is_blocked_setting("app.theme") is False

    def test_max_results_allowed(self):
        assert is_blocked_setting("search.max_results") is False

    def test_output_language_allowed(self):
        assert is_blocked_setting("general.output_language") is False

    def test_empty_key_allowed(self):
        assert is_blocked_setting("") is False


class TestIsBlockedSettingEdgeCases:
    """Edge cases for the blocking logic."""

    def test_module_as_substring_blocked(self):
        # "module" appears as substring — should still block
        assert is_blocked_setting("my_module_path") is True

    def test_class_as_substring_blocked(self):
        assert is_blocked_setting("subclass_handler") is True

    def test_nested_key_with_module(self):
        assert is_blocked_setting("a.b.c.module.d") is True

    def test_path_pattern_blocked(self):
        # Check all patterns in BLOCKED_SETTING_PATTERNS
        assert is_blocked_setting("search.module_path") is True
        assert is_blocked_setting("search.class_name") is True
