feat(micromate): build_request carries a data section -- reproduces all 251 THOR request frames

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-22 17:07:38 -04:00
1 parent 2a6acae259
commit 151ba1546f
2 files changed
+111 -2

No files matched your search

+91
View File
@@ -411,3 +411,94 @@ def test_builder_reproduces_every_captured_read_frame():
checked += 1
i = j + 1
assert checked == 218, f"expected 218 read frames, checked {checked}"
# ── the data section (writes) ─────────────────────────────────────────────────
from micromate.framing import request_sub, unstuff # noqa: E402
def _destuffed_payload(frame: bytes) -> bytes:
body = unstuff(frame[2:-1])
return body[:-1] # drop the checksum byte
def test_data_goes_after_the_params_inside_the_checksum():
data = bytes(range(0x20, 0x40))
f = build_request(0x7E, 0x007E, bytes(10), data)
payload = _destuffed_payload(f)
assert payload[:6] == bytes([0x10, 0x00, 0x7E, 0x00, 0x00, 0x7E])
assert payload[6:16] == bytes(10) and payload[16:] == data
assert unstuff(f[2:-1])[-1] == sum(payload) & 0xFF
@pytest.mark.parametrize("b", [0x02, 0x03, 0x04, 0x10])
def test_every_escape_byte_in_data_is_escaped_on_the_wire(b):
f = build_request(0x71, 0x082C, bytes(10), bytes([0x55, b, 0x55]))
assert bytes([0x55, DLE, b, 0x55]) in f
assert _destuffed_payload(f)[16:] == bytes([0x55, b, 0x55])
def test_a_literal_0x10_in_data_round_trips():
# Never captured in write data; params already prove `10 10` (SUB 0x5A).
data = b"\x10\x10\x00\x10"
assert _destuffed_payload(build_request(0x71, 0x082C, bytes(10), data))[16:] == data
def test_a_checksum_that_lands_on_an_escape_byte_is_escaped():
# Pick the last data byte so SUM8 of the payload is exactly 0x03.
head = bytes([0x10, 0x00, 0x7E, 0x00, 0x00, 0x7E]) + bytes(10) + b"\x01\x02"
last = (0x03 - sum(head)) & 0xFF
f = build_request(0x7E, 0x007E, bytes(10), b"\x01\x02" + bytes([last]))
assert f[-3:] == bytes([DLE, 0x03, ETX])
def test_without_data_the_frame_is_unchanged():
assert build_request(0x5B, 0x0030) == build_request(0x5B, 0x0030, bytes(10), b"")
def test_request_sub_reads_the_sub_back_even_when_stuffed():
assert request_sub(build_request(0x96, 0xFFFF)) == 0x96
assert request_sub(build_request(0x10, 0x0000)) == 0x10 # SUB itself escaped
assert request_sub(build_request(0x71, 0x082C, bytes(10), b"\x03" * 5)) == 0x71
@pytest.mark.parametrize("junk", [b"", b"\x41\x02\x03", b"\x02\x10\x10\x00\x96\x03", b"hello"])
def test_request_sub_refuses_anything_that_is_not_a_request_frame(junk):
with pytest.raises(ValueError):
request_sub(junk)
@pytest.mark.skipif(
not _CAPTURES.is_dir(),
reason="capture directory is gitignored; present only on a dev box",
)
def test_builder_reproduces_every_captured_request_frame_including_writes():
"""251/251: the 218 reads plus the 33 data-carrying writes."""
checked = 0
for path in sorted(_CAPTURES.rglob("raw_bw_*.bin")):
blob = path.read_bytes()
i = 0
while i < len(blob):
if not (blob[i] == ACK and i + 1 < len(blob) and blob[i + 1] == STX):
i += 1
continue
j = i + 2
body = bytearray()
while j < len(blob):
if blob[j] == DLE and j + 1 < len(blob):
body.append(blob[j + 1])
j += 2
continue
if blob[j] == ETX:
break
body.append(blob[j])
j += 1
payload = bytes(body[:-1])
sub = payload[2]
offset = (payload[4] << 8) | payload[5]
assert build_request(sub, offset, payload[6:16], payload[16:]) == blob[i:j + 1], (
f"{path.name} @0x{i:04x} SUB=0x{sub:02x}"
)
checked += 1
i = j + 1
assert checked == 251, f"expected 251 request frames, checked {checked}"