"""Server-sent events for audio/beat status.""" import asyncio import json import pytest @pytest.mark.asyncio async def test_initial_sse_line_includes_status(monkeypatch): from util import beat_status_broadcaster as bsb bsb.configure( loop=asyncio.get_running_loop(), status_builder=lambda: {"bpm_simulated": True, "beat_seq": 3}, ) line = await bsb.initial_sse_line() assert line.startswith("data: ") payload = json.loads(line[6:]) assert payload["type"] == "status" assert payload["status"]["beat_seq"] == 3 def test_audio_events_sse_first_chunk(server): c = server["client"] with c.stream("GET", "/api/audio/events") as resp: assert resp.status_code == 200 assert "text/event-stream" in resp.headers.get("content-type", "") chunk = next(resp.iter_bytes()) text = chunk.decode("utf-8") assert text.startswith("data: ") payload = json.loads(text.strip().removeprefix("data: ")) assert payload.get("type") == "status" assert "bpm_simulated" in payload.get("status", {})