"""main's /api/audio routes on the controller's microphone.""" import json import requests STATUS_KEYS = {"running", "bpm", "beat_readout", "bar_beat", "is_downbeat", "sequence", "sequence_pending", "audio_run", "audio_simulated_bpm", "bpm_simulated", "sequence_switch_wait"} def test_status(c): resp = c.get("/api/audio/status") assert resp.status_code == 200 st = resp.json()["status"] assert STATUS_KEYS <= set(st) assert st["sequence"]["active"] in (True, False) def test_start_stop(c): was = c.get("/api/audio/status").json()["status"]["running"] resp = c.post("/api/audio/stop") assert resp.json()["ok"] is True st = resp.json()["status"] assert st["running"] is False and st["bpm_simulated"] is True assert st["bpm"] == st["audio_simulated_bpm"] assert c.post("/api/audio/reset").status_code == 409 # nothing to reset while stopped assert c.post("/api/audio/start").json()["status"]["running"] is True assert c.post("/api/audio/reset").json()["ok"] is True assert c.post("/api/audio/anchor-bar").json()["ok"] is True if not was: c.post("/api/audio/stop") def test_devices(c): data = c.get("/api/audio/devices").json() assert isinstance(data["devices"], list) # The board's only input is the I2S mic, marked as the default. The desktop lists real input sources # (no default marker: the page's "System default input" entry stands for that). if data["devices"] and data["devices"][0]["id"] == "i2s": assert data["devices"][0]["is_default"] is True resp = c.put("/api/audio/device", json={"device_select": "i2s"}) assert resp.json()["audio_run"]["device_select"] == "i2s" def test_events_stream(base_url): # From main's test_audio_events_sse_first_chunk: the first event is a status snapshot with requests.get(base_url + "/api/audio/events", stream=True, timeout=10) as resp: assert resp.status_code == 200 assert resp.headers["Content-Type"].startswith("text/event-stream") for line in resp.iter_lines(decode_unicode=True): if line.startswith("data: "): event = json.loads(line[6:]) assert event["type"] == "status" assert STATUS_KEYS <= set(event["status"]) break def test_settings_switch_wait(c): assert c.put("/settings", json={"sequence_switch_wait": "downbeat"}).status_code == 200 assert c.get("/settings").json()["sequence_switch_wait"] == "downbeat" assert c.put("/settings", json={"sequence_switch_wait": "bar"}).status_code == 400 assert c.put("/settings", json={"audio_simulated_bpm": 999}).status_code == 200 assert c.get("/settings").json()["audio_simulated_bpm"] == 200 # clamped to the BPM range