"""Sequences (main's model and server playback). Playback runs on the controller's beat clock: the tests switch mic timing off and set the clock to 200 BPM (0.3 s a beat).""" import time import pytest from helpers import find_id_by_field, new_id, unique BEAT_S = 0.3 @pytest.fixture def clock(c): """Beats from the clock at 200 BPM; mic timing is restored afterwards.""" was = c.get("/api/audio/status").json()["status"]["running"] c.post("/api/audio/stop") c.put("/settings", json={"audio_simulated_bpm": 200}) yield c.post("/sequences/stop") if was: c.post("/api/audio/start") @pytest.fixture def zone(c, profile): zid = new_id(c.post("/zones", json={"name": unique("pytest-zone"), "names": ["pytest-nobody"]})) yield zid c.delete(f"/zones/{zid}") def presets(c, n): ids = list(c.get("/presets").json()) assert len(ids) >= n return ids[:n] def test_crud(c, profile): name = unique("pytest-seq") resp = c.post("/sequences", json={"name": name, "steps": [{"preset_id": "1", "group_ids": []}]}) assert resp.status_code == 201 sid = new_id(resp) rec = resp.json()[sid] assert rec["profile_id"] == profile assert rec["lanes"] == [[{"preset_id": "1", "group_ids": []}]] # steps become lane 0 assert rec["advance_mode"] == "beats" assert find_id_by_field(c.get("/sequences").json(), "name", name) == sid resp = c.put(f"/sequences/{sid}", json={"step_duration_ms": 1234}) assert resp.status_code == 200 assert resp.json()["step_duration_ms"] == 1234 assert c.delete(f"/sequences/{sid}").status_code == 200 assert c.get(f"/sequences/{sid}").status_code == 404 def test_scoped_to_profile(c, profile): sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq")})) other = new_id(c.post("/profiles", json={"name": unique("pytest-other")})) c.post(f"/profiles/{other}/apply") assert sid not in c.get("/sequences").json() assert c.get(f"/sequences/{sid}").status_code == 404 c.post(f"/profiles/{profile}/apply") c.delete(f"/sequences/{sid}") c.delete(f"/profiles/{other}") def test_play_needs_zone(c, profile): sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq")})) assert c.post(f"/sequences/{sid}/play", json={}).status_code == 400 assert c.post(f"/sequences/{sid}/play", json={"zone_id": "999999"}).status_code == 400 def test_play_needs_steps(c, zone): sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq")})) resp = c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) assert resp.status_code == 400 assert "steps" in resp.json()["error"] def test_sync_phase_needs_playback(c): c.post("/sequences/stop") assert c.post("/sequences/sync-phase", json={"mode": "step"}).status_code == 409 def test_plays_on_beats_and_finishes(c, zone, clock): a, b = presets(c, 2) sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq"), "loop": False, "lanes": [[{"preset_id": a, "beats": 2}, {"preset_id": b, "beats": 1}]]})) resp = c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) assert resp.status_code == 200 assert resp.json()["ok"] is True and resp.json()["pending"] is False st = c.get("/sequences/status").json() assert st["active"] is True assert st["sequence_id"] == sid and st["zone_id"] == zone assert st["lane0_current_step"] == 1 and st["lane0_beats_per_step"] == 2 assert st["sequence_beats_per_pass"] == 3 seen = set() deadline = time.monotonic() + 10 * BEAT_S while time.monotonic() < deadline: st = c.get("/sequences/status").json() if not st["active"]: break seen.add(st["lane0_current_step"]) time.sleep(BEAT_S / 3) assert st["active"] is False, "sequence should end after its 3 beats" assert {1, 2} <= seen assert c.get("/api/audio/status").json()["status"]["beat_readout"] == "3/3" def test_loops_and_stops(c, zone, clock): a, = presets(c, 1) sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq"), "lanes": [[{"preset_id": a, "beats": 1}]]})) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) time.sleep(5 * BEAT_S) assert c.get("/sequences/status").json()["active"] is True # loop defaults on assert c.post("/sequences/sync-phase", json={"mode": "pass"}).json() == {"ok": True, "mode": "pass"} assert c.post("/sequences/stop").json() == {"ok": True} assert c.get("/sequences/status").json()["active"] is False def test_switch_waits_for_next_beat(c, zone, clock): a, b = presets(c, 2) s1 = new_id(c.post("/sequences", json={"name": unique("pytest-seq"), "lanes": [[{"preset_id": a, "beats": 1}]]})) s2 = new_id(c.post("/sequences", json={"name": unique("pytest-seq"), "lanes": [[{"preset_id": b, "beats": 1}]]})) c.post(f"/sequences/{s1}/play", json={"zone_id": zone}) resp = c.post(f"/sequences/{s2}/play", json={"zone_id": zone}) assert resp.json()["pending"] is True assert resp.json()["sequence_id"] == s2 time.sleep(3 * BEAT_S) assert c.get("/sequences/status").json()["sequence_id"] == s2 def test_editing_stops_playback(c, zone, clock): a, = presets(c, 1) sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq"), "lanes": [[{"preset_id": a, "beats": 1}]]})) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) c.put(f"/sequences/{sid}", json={"name": "edited"}) assert c.get("/sequences/status").json()["active"] is False