"""测试日志过滤器功能 - TDD RED阶段"""
import logging
import pytest

API_KEY = "AIzaSyDummyKey123456789abcdefghijklmnop"
SK_TEST_KEY = "sk_test_1234567890abcdefghijklmnopqrstuvwxyz1234567890"


class TestAPIKeyFilter:
    """测试APIKeyFilter过滤器"""

    def test_api_key_filtered_from_logs(self, caplog):
        """验证API Key从日志中过滤"""
        with caplog.at_level(logging.INFO):
            logging.info(f"Request to Gemini API with key={API_KEY}")

        assert API_KEY not in caplog.text, f"API Key should not appear in logs, but found: {API_KEY}"

    def test_api_key_filtered_from_url(self, caplog):
        """验证URL中的API Key从日志中过滤"""
        with caplog.at_level(logging.INFO):
            logging.info(f"Making request to https://api.gemini.com?key={API_KEY}&other=value")

        assert API_KEY not in caplog.text
        assert "key=***REDACTED***" in caplog.text

    def test_sk_test_key_filtered(self, caplog):
        """验证sk_test密钥格式也被过滤"""
        with caplog.at_level(logging.INFO):
            logging.info(f"Request with api_key={SK_TEST_KEY}")

        assert SK_TEST_KEY not in caplog.text

    def test_bearer_token_filtered(self, caplog):
        """验证Bearer token从日志中过滤"""
        with caplog.at_level(logging.INFO):
            logging.info("Request with Bearer token abcdefghijklmnopqrstuvwxyz")

        assert "Bearer abcdefghijklmnopqrstuvwxyz" not in caplog.text

    def test_authorization_header_filtered(self, caplog):
        """验证Authorization header从日志中过滤"""
        with caplog.at_level(logging.INFO):
            logging.info("Request with Authorization: secret_api_key_here_1234567890")

        assert "secret_api_key_here_1234567890" not in caplog.text

    def test_normal_log_preserved(self, caplog):
        """验证普通日志内容不受影响"""
        normal_message = "This is a normal log message without any secrets"

        with caplog.at_level(logging.INFO):
            logging.info(normal_message)

        assert normal_message in caplog.text


class TestAPIKeyFilterDirectly:
    """直接测试APIKeyFilter类"""

    def test_filter_class_exists(self):
        """验证APIKeyFilter类存在"""
        from app.middleware.logging_filter import APIKeyFilter
        assert APIKeyFilter is not None

    def test_filter_redacts_url_key_param(self):
        """验证过滤器直接处理URL中的key参数时正确脱敏"""
        from app.middleware.logging_filter import APIKeyFilter

        filter_instance = APIKeyFilter()

        record = logging.LogRecord(
            name="test",
            level=logging.INFO,
            pathname="",
            lineno=0,
            msg=f"Request to https://api.gemini.com?key={API_KEY}",
            args=(),
            exc_info=None
        )

        result = filter_instance.filter(record)

        assert result is True
        assert API_KEY not in record.msg
        assert "key=***REDACTED***" in record.msg

    def test_filter_preserves_normal_content(self):
        """验证过滤器保留正常日志内容"""
        from app.middleware.logging_filter import APIKeyFilter

        filter_instance = APIKeyFilter()

        record = logging.LogRecord(
            name="test",
            level=logging.INFO,
            pathname="",
            lineno=0,
            msg="Normal log message without secrets",
            args=(),
            exc_info=None
        )

        result = filter_instance.filter(record)

        assert result is True
        assert record.msg == "Normal log message without secrets"
