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>
174 lines
6.0 KiB
Python
174 lines
6.0 KiB
Python
import pytest
|
|
|
|
# Test MACs from v1; no real driver has them
|
|
ESPNOW_MAC = "aabbccddeeff"
|
|
WIFI_MAC = "102030405060"
|
|
WIFI_MAC2 = "102030405061"
|
|
|
|
|
|
@pytest.fixture
|
|
def espnow_dev(c):
|
|
c.delete(f"/devices/{ESPNOW_MAC}")
|
|
resp = c.post("/devices", json={"name": "pytest-dev", "address": "aa:bb:cc:dd:ee:ff"})
|
|
assert resp.status_code == 201, resp.text
|
|
yield resp
|
|
c.delete(f"/devices/{ESPNOW_MAC}")
|
|
|
|
|
|
@pytest.fixture
|
|
def wifi_dev(c):
|
|
c.delete(f"/devices/{WIFI_MAC}")
|
|
resp = c.post("/devices", json={"name": "pytest-wifi", "type": "led", "transport": "wifi",
|
|
"address": "192.168.50.10", "mac": WIFI_MAC})
|
|
assert resp.status_code == 201, resp.text
|
|
yield resp
|
|
c.delete(f"/devices/{WIFI_MAC}")
|
|
|
|
|
|
def test_list(c):
|
|
resp = c.get("/devices")
|
|
assert resp.status_code == 200
|
|
assert isinstance(resp.json(), dict)
|
|
|
|
|
|
def test_create_requires_name(c):
|
|
assert c.post("/devices", json={}).status_code == 400
|
|
|
|
|
|
def test_create_espnow_keyed_by_mac(espnow_dev):
|
|
dev = espnow_dev.json()
|
|
assert list(dev) == [ESPNOW_MAC]
|
|
rec = dev[ESPNOW_MAC]
|
|
assert rec["name"] == "pytest-dev"
|
|
assert rec["id"] == ESPNOW_MAC
|
|
assert rec["type"] == "led"
|
|
assert rec["transport"] == "espnow"
|
|
assert rec["address"] == ESPNOW_MAC
|
|
|
|
|
|
def test_get_espnow_has_no_connection_state(c, espnow_dev):
|
|
resp = c.get(f"/devices/{ESPNOW_MAC}")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["name"] == "pytest-dev"
|
|
assert resp.json().get("connected") is None
|
|
assert c.get("/devices").json()[ESPNOW_MAC].get("connected") is None
|
|
|
|
|
|
def test_create_wifi(c, wifi_dev):
|
|
rec = wifi_dev.json()[WIFI_MAC]
|
|
assert rec["transport"] == "wifi"
|
|
assert rec["address"] == "192.168.50.10"
|
|
assert c.get(f"/devices/{WIFI_MAC}").json().get("connected") is False
|
|
|
|
|
|
def test_create_second_wifi_same_name(c, wifi_dev):
|
|
resp = c.post("/devices", json={"name": "pytest-wifi", "transport": "wifi",
|
|
"address": "192.168.50.11", "mac": WIFI_MAC2})
|
|
assert resp.status_code == 201
|
|
assert resp.json()[WIFI_MAC2]["name"] == "pytest-wifi"
|
|
assert c.delete(f"/devices/{WIFI_MAC2}").status_code == 200
|
|
|
|
|
|
def test_create_duplicate_mac(c, wifi_dev):
|
|
resp = c.post("/devices", json={"name": "pytest-wifi-dupmac", "transport": "wifi",
|
|
"address": "192.168.50.99", "mac": WIFI_MAC})
|
|
assert resp.status_code == 409
|
|
|
|
|
|
def test_create_wifi_requires_mac(c):
|
|
resp = c.post("/devices", json={"name": "no-mac-wifi", "transport": "wifi", "address": "192.168.50.12"})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_create_rejects_unknown_transport(c):
|
|
assert c.post("/devices", json={"name": "bad-tr", "transport": "serial"}).status_code == 400
|
|
|
|
|
|
def test_rename_blank(c, espnow_dev):
|
|
assert c.put(f"/devices/{ESPNOW_MAC}", json={"name": " "}).status_code == 400
|
|
|
|
|
|
def test_rename(c, espnow_dev, wifi_dev):
|
|
for mac in (ESPNOW_MAC, WIFI_MAC):
|
|
resp = c.put(f"/devices/{mac}", json={"name": "renamed"})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["name"] == "renamed"
|
|
|
|
|
|
def test_cannot_change_mac(c, espnow_dev):
|
|
assert c.put(f"/devices/{ESPNOW_MAC}", json={"mac": "001122334455"}).status_code == 400
|
|
|
|
|
|
def test_identify(c, espnow_dev):
|
|
resp = c.post(f"/devices/{ESPNOW_MAC}/identify")
|
|
assert resp.status_code == 200
|
|
assert resp.json().get("message")
|
|
|
|
|
|
def test_identify_unknown(c):
|
|
assert c.post("/devices/000000000000/identify").status_code == 404
|
|
|
|
|
|
def test_status(c):
|
|
resp = c.get("/devices/status")
|
|
assert resp.status_code == 200
|
|
assert isinstance(resp.json(), dict)
|
|
|
|
|
|
def test_delete(c, espnow_dev):
|
|
assert c.delete(f"/devices/{ESPNOW_MAC}").status_code == 200
|
|
assert c.get(f"/devices/{ESPNOW_MAC}").status_code == 404
|
|
|
|
|
|
def test_ping(c):
|
|
resp = c.post("/devices/ping", json={"timeout_s": 0.5})
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["ok"] is True
|
|
assert data["timeout_s"] == 0.5
|
|
assert isinstance(data["responses"], dict)
|
|
for mac, info in data["responses"].items():
|
|
assert info["mac"] == mac and "name" in info and info["rtt_ms"] >= 0
|
|
assert mac in c.get("/devices").json() # repliers are registered
|
|
|
|
|
|
def test_ping_rejects_bad_timeout(c):
|
|
assert c.post("/devices/ping", json={"timeout_s": "soon"}).status_code == 400
|
|
|
|
|
|
def test_update_groups(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"]
|
|
assert resp.status_code == (200 if data["failed"] == 0 else 503)
|
|
|
|
|
|
def test_layout_saves_and_clears_placement(c, espnow_dev):
|
|
resp = c.post("/devices/layout", json={"placements": {"aa:bb:cc:dd:ee:ff": {"x0": 100, "y0": 200, "x1": 300, "y1": 200}}})
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json()["updated"] == 1 and resp.json()["sent"] >= 1 # this driver is placed, so it is sent
|
|
dev = c.get(f"/devices/{ESPNOW_MAC}").json()
|
|
assert dev["placement"] == {"x0": 100, "y0": 200, "x1": 300, "y1": 200}
|
|
resp = c.post("/devices/layout", json={"placements": {ESPNOW_MAC: None}})
|
|
assert resp.json()["updated"] == 1
|
|
assert "placement" not in c.get(f"/devices/{ESPNOW_MAC}").json()
|
|
|
|
|
|
def test_layout_needs_placements(c):
|
|
assert c.post("/devices/layout", json={}).status_code == 400
|
|
# Unknown devices and bad placements are skipped
|
|
resp = c.post("/devices/layout", json={"placements": {"010203040506": {"x0": 1, "y0": 1, "x1": 2, "y1": 2},
|
|
ESPNOW_MAC: {"x0": "a"}}})
|
|
assert resp.status_code == 200 and resp.json()["updated"] == 0
|
|
|
|
|
|
def test_layout_keeps_rig_center(c, espnow_dev):
|
|
resp = c.post("/devices/layout", json={"placements": {}, "center": {"x": 500, "y": 350}})
|
|
assert resp.status_code == 200
|
|
assert c.get("/settings").json()["rig_center"] == {"x": 500, "y": 350}
|