"""
Tests for get_blocked_settings_error() in web/routes/settings_routes.py

Tests cover:
- Response structure (status, message, errors list)
- Single and multiple blocked keys
- Empty list input
- Error message consistency
- Special characters and dotted key names
"""

from local_deep_research.web.routes.settings_routes import (
    get_blocked_settings_error,
)


class TestGetBlockedSettingsError:
    """Tests for the security error response formatter."""

    def test_response_has_required_keys(self):
        """Response must have status, message, and errors."""
        result = get_blocked_settings_error(["some_key"])
        assert "status" in result
        assert "message" in result
        assert "errors" in result

    def test_status_is_error(self):
        """Status field must be 'error'."""
        result = get_blocked_settings_error(["key"])
        assert result["status"] == "error"

    def test_message_mentions_security(self):
        """Message should reference security violation."""
        result = get_blocked_settings_error(["key"])
        assert "security" in result["message"].lower()

    def test_single_key(self):
        """Single blocked key produces one error entry."""
        result = get_blocked_settings_error(["api_key"])
        assert len(result["errors"]) == 1
        assert result["errors"][0]["key"] == "api_key"
        assert result["errors"][0]["name"] == "api_key"

    def test_multiple_keys(self):
        """Multiple blocked keys produce matching error entries."""
        keys = ["api_key", "secret_token", "password"]
        result = get_blocked_settings_error(keys)
        assert len(result["errors"]) == 3
        error_keys = [e["key"] for e in result["errors"]]
        assert error_keys == keys

    def test_empty_list(self):
        """Empty list produces empty errors array."""
        result = get_blocked_settings_error([])
        assert result["errors"] == []
        assert result["status"] == "error"

    def test_error_entry_has_key_name_error(self):
        """Each error entry has key, name, and error fields."""
        result = get_blocked_settings_error(["test_key"])
        entry = result["errors"][0]
        assert "key" in entry
        assert "name" in entry
        assert "error" in entry

    def test_error_message_mentions_security_reason(self):
        """Error message in each entry mentions security."""
        result = get_blocked_settings_error(["key"])
        assert "security" in result["errors"][0]["error"].lower()

    def test_special_characters_in_key(self):
        """Keys with special characters are preserved as-is."""
        key = "config.api-key_v2"
        result = get_blocked_settings_error([key])
        assert result["errors"][0]["key"] == key

    def test_dotted_key_names(self):
        """Dotted keys (e.g., search.engine.timeout) are preserved."""
        key = "search.engine.timeout"
        result = get_blocked_settings_error([key])
        assert result["errors"][0]["key"] == key
        assert result["errors"][0]["name"] == key
