Rework sequences (engine, seqplay, envelope, API, web UI), add bridge support, add the host sim-controller and rig simulation tests, and update docs, presets and project tooling config. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
286 lines
12 KiB
Python
286 lines
12 KiB
Python
"""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
|
|
|
|
|
|
# ---------------------------------------------------------------- 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
|
|
|
|
|
|
# ------------------------------------------------------------------- animations
|
|
|
|
|
|
def test_animation_plays_once_alone(c, zone, clock):
|
|
a, = presets(c, 1)
|
|
aid = make(c, kind="animation", 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_animation_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 animation plays
|
|
sid = make(c, lanes=[[{"preset_id": a, "beats": 3}, {"preset_id": b, "beats": 3}]],
|
|
lanes_group_ids=[["pytest-g1"]])
|
|
aid = make(c, kind="animation", 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}/fire", json={}) # the playing sequence's 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 animation"
|
|
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_animation_step_fires_overlay(c, zone, clock):
|
|
a, b = presets(c, 2)
|
|
aid = make(c, kind="animation", loop=False, lanes=[[{"preset_id": b, "beats": 2}]])
|
|
sid = make(c, lanes=[[{"preset_id": a, "beats": 1}, {"animation_id": aid, "beats": 1, "length": "animation"}]])
|
|
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"] == 2, "the step lasts as long as the animation"
|
|
|
|
|
|
def test_overlay_stop_and_fire_errors(c, zone, clock):
|
|
a, = presets(c, 1)
|
|
aid = make(c, kind="animation", loop=True, lanes=[[{"preset_id": a, "beats": 1}]])
|
|
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
|