from fastapi.testclient import TestClient

from backend.app.main import app

AUTH_CREDENTIALS = {"username": "demo", "password": "railgame123"}

client = TestClient(app)


def _authenticate() -> str:
    response = client.post("/api/auth/login", json=AUTH_CREDENTIALS)
    assert response.status_code == 200
    return response.json()["accessToken"]


def test_network_snapshot_requires_authentication() -> None:
    response = client.get("/api/network")
    assert response.status_code == 401


def test_network_snapshot_endpoint_returns_collections() -> None:
    token = _authenticate()
    response = client.get("/api/network", headers={"Authorization": f"Bearer {token}"})
    assert response.status_code == 200

    payload = response.json()
    assert set(payload.keys()) == {"stations", "tracks", "trains"}
    assert all(isinstance(payload[key], list) for key in payload)
    assert payload["stations"], "Expected sample station data"
    assert payload["trains"], "Expected sample train data"

    station = payload["stations"][0]
    assert "name" in station and "createdAt" in station
