ESPSomfy-RTS/test/e2e/test_messages.py
cjkas 10521cf78d test: start the e2e suite for socket protocol 2
A pytest suite run by hand against a device (test/e2e/README.md): hello, the schema and its hash, the sync with next and resync, replies to bad frames, feed subscriptions, the status event, every client syncing at once, a sixth client refused, a page load, and the socket under the device's security mode. It never transmits, changes settings or reboots. --record saves hello, the sync and the schema as fixtures.

PROTOCOL.md now describes the protocol as built.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 11:03:07 +02:00

60 lines
2.2 KiB
Python

"""What the device answers to client frames: errors and feed subscriptions."""
from proto_client import run_sync
def synced(connect, schema):
c = connect(schema=schema.hash)
run_sync(c)
return c
def reply_to(c, msg_id, timeout=5):
return c.recv_until(lambda f: f["t"] == 2 and f.get("id") == msg_id, timeout)[-1]
def error_name(schema, reply):
return schema.errors[reply["d"][schema.keys["code"]]]
def test_malformed_json_is_a_bad_request(connect, schema):
c = synced(connect, schema)
c.send_text("not json")
reply = c.recv_until(lambda f: f["t"] == 2)[-1]
assert error_name(schema, reply) == "badRequest"
def test_a_wrong_version_is_a_bad_request(connect, schema):
c = synced(connect, schema)
c.send_text('{"v":1,"t":3,"id":7}')
assert error_name(schema, reply_to(c, 7)) == "badRequest"
def test_an_event_type_from_a_client_is_unknown(connect, schema):
c = synced(connect, schema)
msg_id = c.send(schema.type_numbers["shadeState"], {}, with_id=True)
assert error_name(schema, reply_to(c, msg_id)) == "unknownOp"
def test_subscribing_to_a_feed_is_ok(connect, schema):
c = synced(connect, schema)
for feed in ("frames", "frequencyScan"):
msg_id = c.send(schema.type_numbers["subscribe"], {schema.keys["feed"]: schema.feed_numbers[feed]}, with_id=True)
reply = reply_to(c, msg_id)
assert reply["d"].get(schema.keys["ok"]) == 1
msg_id = c.send(schema.type_numbers["unsubscribe"], {schema.keys["feed"]: schema.feed_numbers[feed]}, with_id=True)
assert reply_to(c, msg_id)["d"].get(schema.keys["ok"]) == 1
def test_an_unknown_feed_is_a_bad_request(connect, schema):
c = synced(connect, schema)
msg_id = c.send(schema.type_numbers["subscribe"], {schema.keys["feed"]: 99}, with_id=True)
assert error_name(schema, reply_to(c, msg_id)) == "badRequest"
def test_memory_status_arrives_within_the_status_period(connect, schema):
"""The device sends its status every 30 s."""
c = synced(connect, schema)
f = c.recv_until(lambda f: f["t"] == schema.type_numbers["memory"], timeout=40)[-1]
memory = schema.expand(f["d"])
assert memory["freeHeap"] > 0
assert memory["uptime"] > 0