"""TS-12 — asyncio cancellation propagates through the retry loop (not swallowed)."""

from __future__ import annotations

import asyncio

import httpx
import pytest
import respx

from mgf.http import AsyncHttpClient


@pytest.mark.asyncio
@respx.mock
async def test_cancellation_propagates() -> None:
    async def _never(request: httpx.Request) -> httpx.Response:
        await asyncio.sleep(30)  # blocks until cancelled
        return httpx.Response(200)

    respx.get("https://h/slow").mock(side_effect=_never)
    client = AsyncHttpClient(base_url="https://h")
    try:
        task = asyncio.ensure_future(client.get("/slow"))
        await asyncio.sleep(0.05)  # let the request enter its await
        task.cancel()
        with pytest.raises(asyncio.CancelledError):
            await task
    finally:
        await client.aclose()
