POST /devices/ping broadcasts an ESP-NOW PING_REQ, collects the drivers' PING_RSP for timeout_s and registers or renames the ones that answer. A reply also counts as the driver being heard, so group updates go to it again. POST /devices/groups sends every ESP-NOW driver its groups and reports the acks. The devices modal gets the Ping drivers and Update groups buttons from main, with per-driver ping dots. UI files are now served with Cache-Control: no-cache and the firmware build hash as ETag, so a browser picks up a new UI right after an update instead of a day later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
4.7 KiB
Python
149 lines
4.7 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)
|