feat: main's import/export, zones, audio API and web UI
Profiles, presets and sequences export and import as main's bundles. Zones carry group_ids, preset_group_ids, sequence_ids, content_kind and brightness; profiles seed rainbow as colour_cycle, and deleting a profile also deletes its sequences. Groups, zones and sequences written by older versions are migrated at boot. /api/audio (status, event stream, start/stop, reset, anchor-bar, devices) maps main's audio detector onto the mic's beat detector. Audio start/stop picks whether mic beats or the BPM clock time sequences; the clock's beats also go to the drivers while the mic is off. /ws now greets from a post-handshake callback (httpd does not call the handler for the upgrade). The web UI is main's, plus v2's outputs editor, list search and hostname/static IP settings; the bridge, Wi-Fi AP and led-tool parts are gone. API tests follow main's behaviour (119 pass on the board). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
04b56c6ef2
commit
bfeeb958df
44 files changed
+8709
-2338
No files matched your search
+2
-2
@@ -1,6 +1,6 @@
|
||||
# API tests
|
||||
|
||||
HTTP tests against a running controller, ported from the v1 Python app's `tests/test_endpoints_pytest.py` and `tests/test_endpoints.py`. They need `pytest` and `requests`.
|
||||
HTTP tests against a running controller, ported from the Python app's endpoint tests (`tests/test_endpoints_pytest.py` and `tests/test_endpoints.py`) on both the `animation-engine` and `main` lines, plus `main`'s bundle, audio, group and sequence tests where they work over HTTP. They need `pytest`, `requests` and `websocket-client`.
|
||||
|
||||
```bash
|
||||
make test-api HOST=10.1.1.180
|
||||
@@ -8,7 +8,7 @@ make test-api HOST=10.1.1.180
|
||||
|
||||
or `python3 -m pytest test/api --host <ip>` (or set `LED_HOST`). Without a host every test is skipped.
|
||||
|
||||
The tests create and delete profiles, presets, zones and so on. Before the run they save every collection (`GET /db/<name>`), and afterwards they write each one back (`PUT /db/<name>`). Zone play and preset send do go out over ESP-NOW. They use made-up driver names (`pytest-a`, `pytest-nobody`) so real drivers ignore them.
|
||||
The tests create and delete profiles, presets, zones and so on. The sequence tests switch mic timing off and run the beat clock at 200 BPM for a few seconds; mic timing is switched back on afterwards. Before the run they save every collection (`GET /db/<name>`), and afterwards they write each one back (`PUT /db/<name>`). Zone play and preset send do go out over ESP-NOW. They use made-up driver names (`pytest-a`, `pytest-nobody`) so real drivers ignore them.
|
||||
|
||||
## v1 features v2 does not have yet
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
"""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 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
|
||||
@@ -0,0 +1,82 @@
|
||||
"""Import/export bundles (main's tests/test_profile_bundle.py over HTTP)."""
|
||||
|
||||
import pytest
|
||||
|
||||
from helpers import new_id, unique
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def profile(c):
|
||||
pid = new_id(c.post("/profiles", json={"name": unique("pytest-profile")}))
|
||||
assert c.post(f"/profiles/{pid}/apply").status_code == 200
|
||||
yield pid
|
||||
c.delete(f"/profiles/{pid}")
|
||||
|
||||
|
||||
def test_profile_round_trip(c, profile):
|
||||
presets = c.get("/presets").json()
|
||||
a = next(iter(presets))
|
||||
sid = new_id(c.post("/sequences", json={"name": "pytest-seq", "lanes": [[{"preset_id": a, "beats": 2}]]}))
|
||||
zid = c.get(f"/profiles/{profile}").json()["zones"][0]
|
||||
c.put(f"/zones/{zid}", json={"sequence_ids": [sid]})
|
||||
c.put(f"/palettes/{c.get(f'/profiles/{profile}').json()['palette_id']}", json={"colors": ["#123456"]})
|
||||
|
||||
bundle = c.get(f"/profiles/{profile}/export").json()
|
||||
assert bundle["version"] == 1 and bundle["kind"] == "profile"
|
||||
assert bundle["palette"]["colors"] == ["#123456"]
|
||||
assert zid in bundle["zones"] and sid in bundle["sequences"]
|
||||
assert set(presets) == set(bundle["presets"])
|
||||
|
||||
resp = c.post("/profiles/import", json={"bundle": bundle, "name": "pytest-imported"})
|
||||
assert resp.status_code == 201
|
||||
new = resp.json()["id"]
|
||||
assert resp.json()[new]["name"] == "pytest-imported"
|
||||
assert c.get("/profiles/current").json()["id"] == new # applied by default
|
||||
imported = c.get("/presets").json()
|
||||
assert len(imported) == len(presets) and not set(imported) & set(presets)
|
||||
nz = c.get(f"/profiles/{new}").json()["zones"]
|
||||
assert len(nz) == 1 and nz[0] != zid
|
||||
zone = c.get(f"/zones/{nz[0]}").json()
|
||||
new_sid = zone["sequence_ids"][0]
|
||||
assert new_sid != sid
|
||||
seq = c.get(f"/sequences/{new_sid}").json()
|
||||
assert seq["lanes"][0][0]["preset_id"] in imported
|
||||
pal = c.get(f"/palettes/{c.get(f'/profiles/{new}').json()['palette_id']}").json()
|
||||
assert pal["colors"] == ["#123456"]
|
||||
c.delete(f"/profiles/{new}")
|
||||
|
||||
|
||||
def test_profile_import_rejects_other_bundles(c, profile):
|
||||
assert c.post("/profiles/import", json={"version": 2, "kind": "profile", "profile": {}}).status_code == 400
|
||||
assert c.post("/profiles/import", json={"version": 1, "kind": "preset", "profile": {}}).status_code == 400
|
||||
assert c.post("/profiles/import", json={"version": 1, "kind": "profile"}).status_code == 400
|
||||
|
||||
|
||||
def test_export_unknown_profile(c):
|
||||
assert c.get("/profiles/999999/export").status_code == 404
|
||||
|
||||
|
||||
def test_preset_round_trip(c, profile):
|
||||
a = next(iter(c.get("/presets").json()))
|
||||
bundle = c.get(f"/presets/{a}/export").json()
|
||||
assert bundle["kind"] == "preset"
|
||||
resp = c.post("/presets/import", json={"bundle": bundle})
|
||||
assert resp.status_code == 201
|
||||
new = new_id(resp)
|
||||
assert new != a
|
||||
assert resp.json()[new]["name"] == bundle["preset"]["name"]
|
||||
assert resp.json()[new]["profile_id"] == profile
|
||||
assert c.post("/presets/import", json={"kind": "sequence"}).status_code == 400
|
||||
|
||||
|
||||
def test_sequence_round_trip(c, profile):
|
||||
a = next(iter(c.get("/presets").json()))
|
||||
sid = new_id(c.post("/sequences", json={"name": "pytest-seq", "lanes": [[{"preset_id": a, "beats": 3}]]}))
|
||||
bundle = c.get(f"/sequences/{sid}/export").json()
|
||||
assert bundle["kind"] == "sequence" and a in bundle["presets"]
|
||||
resp = c.post("/sequences/import", json={"bundle": bundle})
|
||||
assert resp.status_code == 201
|
||||
new = new_id(resp)
|
||||
step = resp.json()[new]["lanes"][0][0]
|
||||
assert step["beats"] == 3 and step["preset_id"] != a
|
||||
assert step["preset_id"] in c.get("/presets").json()
|
||||
@@ -1,69 +1,6 @@
|
||||
"""Groups, sequences, scenes and palettes."""
|
||||
"""Scenes and palettes."""
|
||||
|
||||
from helpers import find_id_by_field, new_id, unique
|
||||
|
||||
|
||||
def test_groups(c):
|
||||
name = unique("pytest-group")
|
||||
assert c.post("/groups", json={"name": name}).status_code == 201
|
||||
gid = find_id_by_field(c.get("/groups").json(), "name", name)
|
||||
resp = c.get(f"/groups/{gid}")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["name"] == name
|
||||
resp = c.put(f"/groups/{gid}", json={"brightness": 10})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["brightness"] == 10
|
||||
assert c.delete(f"/groups/{gid}").status_code == 200
|
||||
|
||||
|
||||
def test_sequences_crud(c):
|
||||
name = unique("pytest-seq")
|
||||
resp = c.post("/sequences", json={"name": name, "steps": [{"preset_id": "1", "duration_s": 2}]})
|
||||
assert resp.status_code == 201
|
||||
sid = find_id_by_field(c.get("/sequences").json(), "name", name)
|
||||
resp = c.get(f"/sequences/{sid}")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["steps"] == [{"preset_id": "1", "duration_s": 2.0}]
|
||||
resp = c.put(f"/sequences/{sid}", json={"loop": False, "repeat_count": 2})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["loop"] is False and resp.json()["repeat_count"] == 2
|
||||
assert c.get("/sequences/status").status_code == 200
|
||||
assert c.delete(f"/sequences/{sid}").status_code == 200
|
||||
|
||||
|
||||
def test_sequence_step_needs_preset(c):
|
||||
sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq"), "steps": []}))
|
||||
assert c.put(f"/sequences/{sid}", json={"steps": [{"duration_s": 1}]}).status_code == 400
|
||||
c.delete(f"/sequences/{sid}")
|
||||
|
||||
|
||||
def test_sequence_without_zone_cannot_play(c):
|
||||
sid = new_id(c.post("/sequences", json={"name": unique("pytest-seq"),
|
||||
"steps": [{"preset_id": "1", "duration_s": 1}]}))
|
||||
assert c.post(f"/sequences/{sid}/play").status_code == 400
|
||||
c.delete(f"/sequences/{sid}")
|
||||
|
||||
|
||||
def test_sequence_play_and_stop(c):
|
||||
profile = new_id(c.post("/profiles", json={"name": unique("pytest-engine")}))
|
||||
c.post(f"/profiles/{profile}/apply")
|
||||
preset = new_id(c.post("/presets", json={"name": "pytest-on", "pattern": "on", "colors": ["#ff0000"]}))
|
||||
zone = new_id(c.post("/zones", json={"name": "pytest-engine-zone", "names": ["pytest-nobody"]}))
|
||||
c.post("/sequences", json={"name": "pytest-engine-seq", "zone_id": zone, "loop": False,
|
||||
"steps": [{"preset_id": preset, "duration_s": 0.5}]})
|
||||
sid = find_id_by_field(c.get("/sequences").json(), "name", "pytest-engine-seq")
|
||||
|
||||
resp = c.post(f"/sequences/{sid}/play")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["zone_id"] == zone
|
||||
assert c.get(f"/sequences/{sid}").json()["active"] is True
|
||||
resp = c.post(f"/sequences/{sid}/stop")
|
||||
assert resp.json()["stopped"] is True
|
||||
assert c.get(f"/sequences/{sid}").json()["active"] is False
|
||||
|
||||
c.delete(f"/sequences/{sid}")
|
||||
c.delete(f"/zones/{zone}")
|
||||
c.delete(f"/profiles/{profile}")
|
||||
from helpers import find_id_by_field, unique
|
||||
|
||||
|
||||
def test_scenes(c):
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
"""Device groups (main's controllers/group.py) and combined brightness."""
|
||||
|
||||
import pytest
|
||||
|
||||
from helpers import find_id_by_field, new_id, unique
|
||||
|
||||
MAC = "aabbccddee01" # made up: no driver answers it
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def profile(c):
|
||||
pid = new_id(c.post("/profiles", json={"name": unique("pytest-profile")}))
|
||||
assert c.post(f"/profiles/{pid}/apply").status_code == 200
|
||||
yield pid
|
||||
c.delete(f"/profiles/{pid}")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def device(c):
|
||||
c.delete(f"/devices/{MAC}")
|
||||
assert c.post("/devices", json={"name": "pytest-g", "mac": MAC, "output_brightness": 128}).status_code == 201
|
||||
yield MAC
|
||||
c.delete(f"/devices/{MAC}")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def group(c, device):
|
||||
resp = c.post("/groups", json={"name": unique("pytest-group"), "devices": ["AA:BB:CC:DD:EE:01"],
|
||||
"output_brightness": 128})
|
||||
assert resp.status_code == 201
|
||||
gid = str(resp.json()["id"])
|
||||
yield gid
|
||||
c.delete(f"/groups/{gid}")
|
||||
|
||||
|
||||
def test_crud(c):
|
||||
name = unique("pytest-group")
|
||||
resp = c.post("/groups", json={"name": name})
|
||||
assert resp.status_code == 201
|
||||
assert resp.json()["devices"] == []
|
||||
assert resp.json()["output_brightness"] == 255
|
||||
gid = find_id_by_field(c.get("/groups").json(), "name", name)
|
||||
resp = c.get(f"/groups/{gid}")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["name"] == name
|
||||
resp = c.put(f"/groups/{gid}", json={"brightness": 10})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["brightness"] == 10
|
||||
assert c.delete(f"/groups/{gid}").status_code == 200
|
||||
assert c.get(f"/groups/{gid}").status_code == 404
|
||||
|
||||
|
||||
def test_device_macs_normalized(c, group):
|
||||
assert c.get(f"/groups/{group}").json()["devices"] == [MAC]
|
||||
|
||||
|
||||
def test_profile_scoped_group(c, profile):
|
||||
resp = c.post("/groups", json={"name": unique("pytest-scoped"), "profile_scoped": True})
|
||||
gid = str(resp.json()["id"])
|
||||
assert resp.json()["profile_id"] == profile
|
||||
other = new_id(c.post("/profiles", json={"name": unique("pytest-other")}))
|
||||
c.post(f"/profiles/{other}/apply")
|
||||
assert gid not in c.get("/groups").json()
|
||||
assert c.get(f"/groups/{gid}").status_code == 404
|
||||
c.post(f"/profiles/{profile}/apply")
|
||||
assert gid in c.get("/groups").json()
|
||||
c.delete(f"/groups/{gid}")
|
||||
c.delete(f"/profiles/{other}")
|
||||
|
||||
|
||||
def test_resolve_brightness(c, group):
|
||||
# global 255 x group 128 x device 128 (x zone 255) -> round(255 * 128/255 * 128/255) = 64
|
||||
resp = c.post("/devices/resolve-brightness", json={"macs": [MAC], "zone_brightness": 255})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["values"] == {MAC: 64}
|
||||
|
||||
|
||||
def test_resolve_brightness_needs_list(c):
|
||||
assert c.post("/devices/resolve-brightness", json={"macs": "x"}).status_code == 400
|
||||
|
||||
|
||||
def test_group_brightness(c, group):
|
||||
resp = c.post(f"/groups/{group}/brightness")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["sent"] == 1
|
||||
|
||||
|
||||
def test_group_identify(c, group):
|
||||
resp = c.post(f"/groups/{group}/identify")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["sent"] == 1
|
||||
|
||||
|
||||
def test_group_identify_needs_devices(c):
|
||||
gid = str(c.post("/groups", json={"name": unique("pytest-empty")}).json()["id"])
|
||||
assert c.post(f"/groups/{gid}/identify").status_code == 400
|
||||
c.delete(f"/groups/{gid}")
|
||||
|
||||
|
||||
def test_group_driver_config(c, group):
|
||||
assert c.post(f"/groups/{group}/driver-config", json={}).status_code == 400
|
||||
resp = c.post(f"/groups/{group}/driver-config", json={"wifi_driver_num_leds": 60})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["sent"] == 1
|
||||
|
||||
|
||||
def test_device_brightness_and_config(c, device):
|
||||
resp = c.post(f"/devices/{device}/brightness", json={"zone_brightness": 255})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["brightness"] == 128
|
||||
assert c.post(f"/devices/{device}/driver-config", json={}).status_code == 400
|
||||
assert c.post(f"/devices/{device}/driver-config", json={"num_leds": 30}).status_code == 200
|
||||
|
||||
|
||||
def test_update_groups_push(c):
|
||||
resp = c.post("/devices/groups")
|
||||
espnow = [d for d in c.get("/devices").json().values() if d.get("transport") == "espnow"]
|
||||
if not espnow:
|
||||
assert resp.status_code == 400
|
||||
return
|
||||
data = resp.json()
|
||||
assert data["total"] == len(espnow)
|
||||
assert data["sent"] + data["failed"] == data["total"]
|
||||
@@ -2,7 +2,7 @@ import pytest
|
||||
|
||||
from helpers import new_id, unique
|
||||
|
||||
# Presets every new profile is seeded with (v1 controllers/profile.py)
|
||||
# Presets every new profile is seeded with (main's controllers/profile.py)
|
||||
SEEDED = {"on", "off", "rainbow", "Colour Cycle", "transition", "flicker", "flame", "twinkle"}
|
||||
|
||||
|
||||
@@ -81,7 +81,21 @@ def test_clone(c, profile):
|
||||
assert c.delete(f"/profiles/{clone}").status_code == 200
|
||||
|
||||
|
||||
def test_seeded_rainbow_uses_colour_cycle(c, profile):
|
||||
presets = c.get("/presets").json().values()
|
||||
assert next(p for p in presets if p["name"] == "rainbow")["pattern"] == "colour_cycle"
|
||||
|
||||
|
||||
def test_delete(c):
|
||||
pid = new_id(c.post("/profiles", json={"name": unique("pytest-profile")}))
|
||||
c.post(f"/profiles/{pid}/apply")
|
||||
prof = c.get(f"/profiles/{pid}").json()
|
||||
preset_ids = set(c.get("/presets").json())
|
||||
seq = new_id(c.post("/sequences", json={"name": "pytest-seq"}))
|
||||
assert c.delete(f"/profiles/{pid}").status_code == 200
|
||||
assert c.get(f"/profiles/{pid}").status_code == 404
|
||||
# its presets, sequences, zones and palette go with it, so a profile that reuses the id starts clean
|
||||
assert not preset_ids & set(c.get("/db/preset").json())
|
||||
assert not set(prof["zones"]) & set(c.get("/zones").json()["zones"])
|
||||
assert prof["palette_id"] not in c.get("/palettes").json()
|
||||
assert seq not in c.get("/db/sequence").json()
|
||||
@@ -26,3 +26,21 @@ def test_ws_rejects_plain_get(c):
|
||||
# A websocket endpoint refuses a request that is not an upgrade
|
||||
resp = c.get("/ws")
|
||||
assert resp.status_code in {400, 401, 403, 405, 426}
|
||||
|
||||
|
||||
def test_ws_forwards_messages(base_url):
|
||||
# From main's test_main_routes: the socket greets with the (empty) Wi-Fi snapshot and takes v1 messages
|
||||
import websocket
|
||||
|
||||
ws = websocket.create_connection(base_url.replace("http", "ws", 1) + "/ws", timeout=5)
|
||||
try:
|
||||
import json
|
||||
|
||||
snapshot = json.loads(ws.recv())
|
||||
assert snapshot["type"] == "device_tcp_snapshot"
|
||||
assert isinstance(snapshot["connected_ips"], list)
|
||||
ws.send('{"v":"1","select":["off"],"groups":["pytest-none"]}')
|
||||
ws.send("not json")
|
||||
assert "error" in json.loads(ws.recv())
|
||||
finally:
|
||||
ws.close()
|
||||
@@ -0,0 +1,145 @@
|
||||
"""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 profile(c):
|
||||
pid = new_id(c.post("/profiles", json={"name": unique("pytest-profile")}))
|
||||
assert c.post(f"/profiles/{pid}/apply").status_code == 200
|
||||
yield pid
|
||||
c.delete(f"/profiles/{pid}")
|
||||
|
||||
|
||||
@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
|
||||
@@ -21,3 +21,10 @@ def test_global_brightness(c):
|
||||
|
||||
def test_global_brightness_out_of_range(c):
|
||||
assert c.put("/settings/settings", json={"global_brightness": 300}).status_code == 400
|
||||
|
||||
|
||||
def test_put_settings_root(c):
|
||||
# main's UI writes PUT /settings
|
||||
assert c.put("/settings", json={"audio_beat_phase_ms": 40}).status_code == 200
|
||||
assert c.get("/settings").json()["audio_beat_phase_ms"] == 40
|
||||
assert c.put("/settings", json={"audio_beat_phase_ms": 900}).status_code == 400
|
||||
@@ -118,3 +118,21 @@ def test_play_and_stop(c, zone):
|
||||
resp = c.post(f"/zones/{zone}/stop")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["stopped"] is True
|
||||
|
||||
|
||||
def test_new_zone_has_main_fields(c, zone):
|
||||
rec = c.get(f"/zones/{zone}").json()
|
||||
assert rec["group_ids"] == [] and rec["preset_group_ids"] == {} and rec["sequence_ids"] == []
|
||||
assert rec["brightness"] == 255
|
||||
|
||||
|
||||
def test_zone_with_groups(c, profile):
|
||||
# main's test_zone_content_kind: presets and sequences coexist; group ids are kept as strings
|
||||
resp = c.post("/zones", json={"name": unique("pytest-zone"), "group_ids": [7], "content_kind": "sequences"})
|
||||
zid = new_id(resp)
|
||||
rec = resp.json()[zid]
|
||||
assert rec["group_ids"] == ["7"] and rec["content_kind"] == "sequences"
|
||||
c.put(f"/zones/{zid}", json={"presets": [["1"]], "sequence_ids": ["1"]})
|
||||
rec = c.get(f"/zones/{zid}").json()
|
||||
assert rec["presets"] == [["1"]] and rec["sequence_ids"] == ["1"]
|
||||
c.delete(f"/zones/{zid}")
|
||||
Reference in new issue
Block a user