import pytest
from httpx import ASGITransport, AsyncClient

from app.main import app


@pytest.fixture
def anyio_backend():
    return "asyncio"


@pytest.mark.anyio
async def test_home_page():
    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://testserver"
    ) as client:
        response = await client.get("/")

    assert response.status_code == 200
    assert "See how fast that was?" in response.text
    assert "{{ project_name_html }}" in response.text


@pytest.mark.anyio
async def test_htmx_asset():
    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://testserver"
    ) as client:
        response = await client.get("/assets/htmx.min.js")

    assert response.status_code == 200
    assert "htmx" in response.text
    assert "{{ htmx_version }}" in response.text


@pytest.mark.anyio
async def test_status_api():
    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://testserver"
    ) as client:
        response = await client.get("/api/status")

    assert response.status_code == 200
    assert response.json()["app"] == {{ project_name_literal }}


@pytest.mark.anyio
async def test_status_card():
    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://testserver"
    ) as client:
        response = await client.get("/status")

    assert response.status_code == 200
    assert (
        response.text
        == "<p>{{ project_name_html }} is running. HTML updates and JSON APIs are ready.</p>"
    )


@pytest.mark.anyio
async def test_brief_card():
    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://testserver"
    ) as client:
        response = await client.get("/brief")

    assert response.status_code == 200
    assert (
        response.text
        == "<p>{{ project_name_html }} is ready. Replace this with your first real workflow.</p>"
    )


@pytest.mark.anyio
async def test_validation_error_uses_htmx_4_error_swap_model():
    async with AsyncClient(
        transport=ASGITransport(app=app), base_url="http://testserver"
    ) as client:
        response = await client.get("/validation-error")

    assert response.status_code == 422
    assert response.text == (
        "<p>Validation response rendered from a 422 status. "
        "htmx 4 can swap error HTML.</p>"
    )
