"""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(c, zone, clock): a, b = presets(c, 2) sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq"), "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() + 4 * BEAT_S while time.monotonic() < deadline: st = c.get("/sequences/status").json() seen.add(st["lane0_current_step"]) time.sleep(BEAT_S / 3) assert st["active"] is True, "a looping sequence keeps playing" assert {1, 2} <= seen # (A loop-off sequence is a one-shot: see test_play_routes_oneshot_over_preset) 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 # ---------------------------------------------------------------- multi-driver lanes # Made-up groups and drivers, so real drivers ignore every send TARGETS = [ {"name": "left", "group_ids": ["pytest-g1"]}, {"name": "mid", "group_ids": ["pytest-g2"]}, {"name": "right", "devices": ["02:00:00:00:00:01"]}, ] def make(c, **rec): rec.setdefault("name", unique("pytest-seq")) return new_id(c.post("/sequences", json=rec)) def wait_for(c, pred, beats=12): deadline = time.monotonic() + beats * BEAT_S st = None while time.monotonic() < deadline: st = c.get("/sequences/status").json() if pred(st): return st time.sleep(BEAT_S / 4) return st def test_targets_and_lane_opts_round_trip(c, profile): opts = [{"mode": "time", "offset": 1.5, "rate": 2, "targets": [2, 0], "chase": {"order": "bounce", "width": 1, "others": "off"}}] sid = make(c, targets=TARGETS, lane_opts=opts, lanes=[[{"preset_id": "1", "beats": 1, "targets": [0], "others": "off"}, {"rest": True, "beats": 1}]]) rec = c.get(f"/sequences/{sid}").json() assert rec["targets"] == TARGETS assert rec["lane_opts"] == opts assert rec["lanes"][0][1]["rest"] is True def test_old_sequence_status_unchanged(c, zone, clock): a, b = presets(c, 2) sid = make(c, lanes=[[{"preset_id": a, "beats": 2}, {"preset_id": b, "beats": 1}]], lanes_group_ids=[["pytest-g1"]]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) st = c.get("/sequences/status").json() assert st["num_lanes"] == 1 and st["lane0_beats_per_step"] == 2 and st["sequence_beats_per_pass"] == 3 assert st["lanes"][0]["mode"] == "beats" and st["lanes"][0]["steps"] == 2 def test_beat_and_time_lanes_together(c, zone, clock): a, b = presets(c, 2) sid = make(c, lanes=[[{"preset_id": a, "beats": 1}, {"preset_id": b, "beats": 1}], [{"preset_id": a, "seconds": 5}, {"preset_id": b, "seconds": 5}]], lane_opts=[{}, {"mode": "time"}]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) st = wait_for(c, lambda s: s["lanes"][0]["step"] == 1) assert st["lanes"][0]["step"] == 1, "the beat lane moves on the beat" assert st["lanes"][1]["mode"] == "time" and st["lanes"][1]["step"] == 0, "the time lane holds its 5 s" assert st["lanes"][1]["duration_ms"] == 5000 def test_offset_lane_starts_late(c, zone, clock): a, = presets(c, 1) sid = make(c, lanes=[[{"preset_id": a, "beats": 1}], [{"preset_id": a, "beats": 1}]], lane_opts=[{}, {"offset": 4}]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) st = c.get("/sequences/status").json() assert st["lanes"][0]["started"] is True and st["lanes"][1]["started"] is False st = wait_for(c, lambda s: s["lanes"][1]["started"]) assert st["lanes"][1]["started"] is True def test_rate_doubles_speed(c, zone, clock): a, b = presets(c, 2) sid = make(c, lanes=[[{"preset_id": a, "beats": 4}], [{"preset_id": a, "beats": 1}, {"preset_id": b, "beats": 1}]], lane_opts=[{}, {"rate": 2}]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) st = c.get("/sequences/status").json() assert st["lanes"][1]["beats_in_step"] == 0.5 def test_chase_moves_and_bounces(c, zone, clock): a, = presets(c, 1) sid = make(c, targets=TARGETS, lanes=[[{"preset_id": a, "beats": 1}]], lane_opts=[{"targets": [0, 1, 2], "chase": {"order": "bounce"}}]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) seen = [] deadline = time.monotonic() + 7 * BEAT_S while time.monotonic() < deadline: pos = c.get("/sequences/status").json()["lanes"][0]["pos"] if not seen or seen[-1] != pos: seen.append(pos) time.sleep(BEAT_S / 4) assert seen[:5] == [0, 1, 2, 1, 0][: len(seen[:5])] and len(seen) >= 4 # ------------------------------------------------------------------- one-shots # A sequence with loop off is a one-shot: it plays once over whatever runs in the zone, then the zone goes # back to its last looping preset or sequence. /fire plays one; /play routes one-shots the same way. def test_oneshot_plays_once_alone(c, zone, clock): a, = presets(c, 1) aid = make(c, loop=False, targets=TARGETS[:1], lanes=[[{"preset_id": a, "beats": 2}]], lane_opts=[{"targets": [0]}]) resp = c.post(f"/sequences/{aid}/fire", json={"zone_id": zone}) assert resp.status_code == 200 and resp.json()["wait"] == "now" st = c.get("/sequences/status").json() assert st["active"] is False and st["overlay"]["active"] is True and st["overlay"]["sequence_id"] == aid st = wait_for(c, lambda s: not s["overlay"]["active"]) assert st["overlay"]["active"] is False def test_oneshot_is_timed_in_seconds(c, zone, clock): # Whatever its advance_mode says, a one-shot counts seconds: 1.5 s, not 99 beats (~30 s at 200 BPM) a, = presets(c, 1) aid = make(c, loop=False, advance_mode="beats", targets=TARGETS[:1], lanes=[[{"preset_id": a, "seconds": 1.5, "beats": 99}]], lane_opts=[{"targets": [0]}]) assert c.post(f"/sequences/{aid}/fire", json={"zone_id": zone}).status_code == 200 st = wait_for(c, lambda s: s["overlay"]["active"]) assert st["overlay"]["lanes"][0]["mode"] == "time", "a one-shot's lanes are timed" t0 = time.monotonic() while time.monotonic() - t0 < 10 and c.get("/sequences/status").json()["overlay"]["active"]: time.sleep(0.1) elapsed = time.monotonic() - t0 assert c.get("/sequences/status").json()["overlay"]["active"] is False assert elapsed < 6, "the one-shot ends after its seconds" def test_play_routes_oneshot_over_preset(c, zone, clock): a, b = presets(c, 2) assert c.post(f"/zones/{zone}/play", json={"preset_id": a}).status_code == 200 # the looping preset aid = make(c, loop=False, lanes=[[{"preset_id": b, "beats": 2}]]) assert c.post(f"/sequences/{aid}/play", json={"zone_id": zone}).status_code == 200 st = c.get("/sequences/status").json() assert st["active"] is False, "a one-shot never becomes the zone's looping sequence" assert st["overlay"]["active"] is True and st["overlay"]["sequence_id"] == aid st = wait_for(c, lambda s: not s["overlay"]["active"]) assert st["overlay"]["active"] is False and st["active"] is False def test_oneshot_over_sequence_keeps_time(c, zone, clock): a, b = presets(c, 2) # A 6-beat pass, so the beat count can't wrap round while the one-shot plays sid = make(c, lanes=[[{"preset_id": a, "beats": 3}, {"preset_id": b, "beats": 3}]], lanes_group_ids=[["pytest-g1"]]) aid = make(c, loop=False, lanes=[[{"preset_id": b, "beats": 3}]], targets=[{"group_ids": ["pytest-g1"]}], lane_opts=[{"targets": [0]}]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) c.post(f"/sequences/{aid}/play", json={"zone_id": zone}) st = wait_for(c, lambda s: s["sequence_beat_at"] >= 2, beats=4) assert st["overlay"]["active"] is True and st["active"] is True and st["sequence_id"] == sid assert st["sequence_beat_at"] >= 2, "the sequence keeps counting under the one-shot" st = wait_for(c, lambda s: not s["overlay"]["active"]) assert st["overlay"]["active"] is False and st["active"] is True and st["sequence_id"] == sid def test_oneshot_clip_lasts_its_length(c, zone, clock): a, b = presets(c, 2) aid = make(c, loop=False, lanes=[[{"preset_id": b, "beats": 2}]]) sid = make(c, lanes=[[{"preset_id": a, "beats": 1}, {"animation_id": aid, "beats": 3}]]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) st = wait_for(c, lambda s: s["overlay"]["active"]) assert st["overlay"]["active"] is True and st["overlay"]["sequence_id"] == aid assert st["lanes"][0]["beats_in_step"] == 3, "the clip lasts as long as it is drawn" def test_looping_sequence_as_clip_plays_once(c, zone, clock): a, b = presets(c, 2) lid = make(c, lanes=[[{"preset_id": b, "beats": 1}]]) # loops on its own sid = make(c, lanes=[[{"preset_id": a, "beats": 1}, {"animation_id": lid, "beats": 6}]]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) st = wait_for(c, lambda s: s["overlay"]["active"]) assert st["overlay"]["active"] is True st = wait_for(c, lambda s: not s["overlay"]["active"], beats=4) assert st["overlay"]["active"] is False, "over the zone it plays once, not for ever" def test_legacy_animation_kind_plays_once(c, zone, clock): a, = presets(c, 1) aid = make(c, kind="animation", lanes=[[{"preset_id": a, "beats": 1}]]) rec = c.get(f"/sequences/{aid}").json() assert rec["loop"] is False and "kind" not in rec, "an older client's animation is a one-shot" c.post(f"/sequences/{aid}/play", json={"zone_id": zone}) st = c.get("/sequences/status").json() assert st["active"] is False and st["overlay"]["active"] is True def test_overlay_stop_and_fire_errors(c, zone, clock): a, = presets(c, 1) aid = make(c, loop=False, lanes=[[{"preset_id": a, "beats": 8}]]) assert c.post("/sequences/999999/fire", json={"zone_id": zone}).status_code == 404 c.post("/sequences/stop") assert c.post(f"/sequences/{aid}/fire", json={}).status_code == 400 # no zone, nothing playing c.post(f"/sequences/{aid}/fire", json={"zone_id": zone, "wait": "beat"}) st = wait_for(c, lambda s: s["overlay"]["active"], beats=3) assert st["overlay"]["active"] is True assert c.post("/sequences/overlay/stop").json() == {"ok": True} assert c.get("/sequences/status").json()["overlay"]["active"] is False def test_preset_takes_over_from_sequence(c, zone, clock): a, b = presets(c, 2) sid = make(c, lanes=[[{"preset_id": a, "beats": 2}]]) c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) assert c.get("/sequences/status").json()["active"] is True aid = make(c, loop=False, lanes=[[{"preset_id": a, "beats": 4}]]) c.post(f"/sequences/{aid}/play", json={"zone_id": zone}) # The UI has sent the preset to the drivers itself assert c.post(f"/zones/{zone}/play", json={"preset_id": b, "drivers": False}).status_code == 200 st = c.get("/sequences/status").json() assert st["active"] is False, "a looping preset replaces the zone's looping sequence" assert st["overlay"]["active"] is True, "the one-shot carries on, then comes back to the preset"