""" test_write_frames.py — the 3-11-26/170151 Blastware config write, read correctly. Frames 102–112 of raw_bw_20260311_170151.bin: 68, 73, 71×3, 72, 82, 83, 69, 74, 72. ⚠ REWRITTEN 2026-10-27 (#46). This file pinned a model of these frames that was wrong: "only BW_CMD is doubled", a "DLE-aware checksum", compliance chunks of 1,027 / 1,055 bytes at "offset 0x1004", single-block "offset = data[1] + 2". Every one of those read the frames with their escape bytes still in. Decoded as every Blastware request decodes (each 02/03/04/10 escaped, SUM8 of the payload), these frames are ordinary requests: a LENGTH at [4:6] equal to the data's length, the compliance image in 1,024-byte chunks addressed by params[2:4], and blocks of N + 2 bytes. tests/test_minimate_write_frames.py covers every write on record, tonight's 26 frames included. Skips when the capture is absent. """ from __future__ import annotations import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from minimateplus.framing import build_bw_write_frame, checksum CAPTURE_PATH = os.path.join( os.path.dirname(__file__), "fixtures", "captures", "3-11-26", "raw_bw_20260311_170151.bin", ) def _unstuff(b: bytes) -> bytes: out, k = bytearray(), 0 while k < len(b): if b[k] == 0x10 and k + 1 < len(b): out.append(b[k + 1]); k += 2 else: out.append(b[k]); k += 1 return bytes(out) def _frames(raw: bytes) -> list: out, i = [], 0 while i < len(raw): if raw[i:i + 2] == b"\x41\x03": i += 2 continue if raw[i:i + 2] != b"\x41\x02": i += 1 continue j = i + 2 while j < len(raw) and raw[j] != 0x03: j += 2 if raw[j] == 0x10 else 1 out.append(raw[i:j + 1]) i = j + 1 return out @pytest.fixture(scope="module") def writes() -> list: frames = _frames(open(CAPTURE_PATH, "rb").read()) w = [f for f in frames if 0x68 <= _unstuff(f[2:-1])[2] <= 0x83] assert [f"{_unstuff(f[2:-1])[2]:02X}" for f in w] == \ ["68", "73", "71", "71", "71", "72", "82", "83", "69", "74", "72"] return w def _fields(frame: bytes): p = _unstuff(frame[2:-1]) payload, chk = p[:-1], p[-1] return payload[2], int.from_bytes(payload[4:6], "big"), payload[6:16], payload[16:], chk, payload def test_every_frame_is_an_ordinary_request_with_a_sum8_checksum(writes): for f in writes: sub, length, params, data, chk, payload = _fields(f) assert chk == checksum(payload) assert build_bw_write_frame(sub, data, params=params) == f def test_the_length_field_is_the_datas_length(writes): for f in writes: sub, length, params, data, chk, _ = _fields(f) assert length == len(data) def test_single_blocks_are_their_header_plus_two(writes): for f in writes: sub, length, params, data, chk, _ = _fields(f) if sub in (0x68, 0x82, 0x69): assert len(data) == int.from_bytes(data[:2], "big") + 2 def test_the_compliance_image_goes_in_1024_byte_chunks_addressed_by_params(writes): chunks = [_fields(f) for f in writes if _fields(f)[0] == 0x71] assert [(length, params[2:4].hex()) for _, length, params, *_ in chunks] == \ [(0x400, "0000"), (0x400, "0400"), (0x2C, "0800")] image = b"".join(c[3] for c in chunks) assert len(image) == 2092 and int.from_bytes(image[:2], "big") == 2090