import pytest

from request_decorator_handlers.core.models import ValidationData
from request_decorator_handlers.validation.response import Validator


class DummyResponse:
    """Простая заглушка HTTP ответа для тестов Cloudflare."""

    def __init__(self, text: str, headers: dict | None = None):
        self.text = text
        self.headers = headers or {}


def test_cloudflare_detector_flags_block_page():
    response = DummyResponse(
        text=(
            "Attention Required! | Cloudflare\n"
            "Checking your browser before accessing example.com.\n"
            "DDoS protection by Cloudflare. Ray ID: 12345"
        ),
        headers={"Server": "cloudflare"},
    )
    validator = Validator.cloudflare_blocked()
    valid = ValidationData()

    validator(response, valid)

    assert valid.has_errors()
    assert valid.ERRORS[0].key == "CLOUDFLARE"


def test_cloudflare_detector_ignores_regular_content():
    response = DummyResponse(
        text="Welcome to example.com! We use Cloudflare analytics.",
        headers={"Server": "nginx"},
    )
    validator = Validator.cloudflare_blocked()
    valid = ValidationData()

    validator(response, valid)

    assert not valid.has_errors()

