from __future__ import annotations


async def _preflight(client, origin: str):
    return await client.options(
        "/state/summary",
        headers={
            "Origin": origin,
            "Access-Control-Request-Method": "GET",
        },
    )


async def test_dashboard_cors_allows_observable_fallback_ports(client):
    for origin in (
        "http://localhost:3000",
        "http://127.0.0.1:3000",
        "http://localhost:3001",
        "http://127.0.0.1:3001",
        "http://[::1]:3000",
    ):
        response = await _preflight(client, origin)

        assert response.status_code == 200
        assert response.headers["access-control-allow-origin"] == origin


async def test_dashboard_cors_still_rejects_non_dashboard_ports(client):
    response = await _preflight(client, "http://localhost:5173")

    assert response.status_code == 400
