Files
led-controller/test/api/test_devices.py
T
jimmyandClaude Opus 5.5 581b751904 feat: browser simulator served by the controller, desktop build, browser bridge
- led-simulator: the rig runs the C++ led-driver as WebAssembly (sim-driver/),
  one single-threaded worker per bar, no cross-origin isolation needed. Rooms
  (one JSON per room) with an on-page editor, and a pattern gallery. The
  Python server, Pyodide boot and ring buffer are removed.
- sim-controller sim_rig.c: serves /sim/ from an embedded pack, the page's
  WebSocket and the rooms/flash API. The desktop build embeds it too.
- bridge.c: a "browser" bridge kind that carries ESP-NOW frames to the page;
  devices announced through it are marked sim.
- UI: Simulator in the Menu, split view with resize and pop-out, swipe
  between controller and rig on phones; SIM/REAL badges in Devices.
- Fades: preset pushes and Wi-Fi driver bodies always carry fade and
  beat_time, so "off" no longer inherits an earlier fade.
- Also the sequences-v2 work in progress: desktop port, bridge firmware,
  serial bridge, Wi-Fi drivers, sequence timeline, audio and API changes.
- led-driver: ESP-NOW link, beat sync, control messages, sim hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:32:46 +13:00

178 lines
6.1 KiB
Python

import pytest
# Test MACs from v1; no real driver has them
ESPNOW_MAC = "aabbccddeeff"
# Wi-Fi drivers are keyed by their mDNS hostname (12 characters or fewer), not a MAC
WIFI_ID = "pytest-drv"
WIFI_ID2 = "pytest-drv2"
@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_ID}")
resp = c.post("/devices", json={"name": "pytest-wifi", "type": "led", "transport": "wifi",
"address": WIFI_ID})
assert resp.status_code == 201, resp.text
yield resp
c.delete(f"/devices/{WIFI_ID}")
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_ID]
assert rec["transport"] == "wifi"
assert rec["id"] == WIFI_ID
assert rec["address"] == f"{WIFI_ID}.local"
assert c.get(f"/devices/{WIFI_ID}").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": WIFI_ID2})
assert resp.status_code == 201
assert resp.json()[WIFI_ID2]["name"] == "pytest-wifi"
assert c.delete(f"/devices/{WIFI_ID2}").status_code == 200
def test_create_duplicate_mac(c, wifi_dev):
resp = c.post("/devices", json={"name": "pytest-wifi-dupmac", "transport": "wifi", "address": WIFI_ID})
assert resp.status_code == 409
def test_create_wifi_requires_hostname(c):
assert c.post("/devices", json={"name": "no-host-wifi", "transport": "wifi"}).status_code == 400
def test_create_wifi_rejects_ip_address(c):
resp = c.post("/devices", json={"name": "ip-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 dev in (ESPNOW_MAC, WIFI_ID):
resp = c.put(f"/devices/{dev}", 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}