"""A MiniMate's settings read, assembled into the logical 2,090-byte block (phase 5). Measured 2026-11-03 on BE9441's 10-26 recording (12 reads): unescape each 1A data frame and take data[11:]; every frame but the last starts with one `00` that is not content -- drop it. The result is the block Blastware writes back: the read after each of its two writes equals the image it had just written. While monitoring, the unit answers with its device-info block instead. """ from __future__ import annotations import os import sys from pathlib import Path import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import minimateplus.protocol as mp # noqa: E402 from minimateplus.framing import S3FrameParser # noqa: E402 from minimateplus.protocol import ( # noqa: E402 COMPLIANCE_CONTENT_LEN, assemble_compliance_content, is_compliance_content, ) from test_minimate_write_frames import WRITES, _payload # noqa: E402 FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-histogram" def _reads() -> list: """Each recorded 1A read as its data frames' `data`, in order (a probe reply, 11 bytes of data, starts each read).""" reads, cur = [], None for f in S3FrameParser().feed((FIX / "unit_replies.bin").read_bytes()): if f.sub != 0xE5: continue if len(f.data) == 11: cur = [] reads.append(cur) else: cur.append(bytes(f.data)) return reads READS = _reads() IMAGES = [b"".join(_payload(f)[16:] for f in WRITES[k:k + 13] if _payload(f)[2] == 0x71) for k in (0, 13)] def test_the_recording_holds_twelve_reads_in_both_patterns(): assert len(READS) == 12 assert sorted({len(r) for r in READS}) == [3, 5] # SFM's 1024+1024+42, Blastware's 4x512+42 def test_reads_assemble_to_2090_bytes(): assert COMPLIANCE_CONTENT_LEN == 2090 assert all(len(assemble_compliance_content(r)) == 2090 for r in READS) def test_a_read_after_a_blastware_write_equals_the_write(): assert assemble_compliance_content(READS[1]) == IMAGES[0][:2090] assert assemble_compliance_content(READS[2]) == IMAGES[1][:2090] def test_a_monitoring_units_answer_is_not_settings(): # reads 3-7 were taken while BE9441 was monitoring: they start 0e 'BE9441' assert [is_compliance_content(assemble_compliance_content(r)) for r in READS[3:8]] == [False] * 5 assert all(is_compliance_content(assemble_compliance_content(r)) for r in READS[:3] + READS[8:]) def _s3_reply(sub: int, data: bytes) -> bytes: """A unit reply on the wire: DLE STX, the payload with every 0x10 doubled, ETX. (`data` here holds no 02/03/04, so no escape is needed.)""" body = bytes([0x00, 0x10, sub, 0x00, 0x00]) + data body += bytes([sum(body) & 0xFF]) return b"\x10\x02" + body.replace(b"\x10", b"\x10\x10") + b"\x03" class _ProbeAndOneFrame: """Answers the probe and frame B, then goes silent: frame C never arrives.""" def __init__(self): self.sent = [] self._replies = [_s3_reply(0xE5, bytes(11)), _s3_reply(0xE5, bytes(11) + bytes(1025))] self._buf = b"" def write(self, data): self.sent.append(bytes(data)) if self._replies: self._buf += self._replies.pop(0) def read(self, n): out, self._buf = self._buf[:n], self._buf[n:] return out def test_read_compliance_content_refuses_a_missing_frame(): t = _ProbeAndOneFrame() proto = mp.MiniMateProtocol(t, recv_timeout=0.2) with pytest.raises(mp.ProtocolError): proto.read_compliance_content() assert len(t.sent) == 3 # probe, B, C -- and no D after C went unanswered # Bench 2026-11-04 (BE9441, live): frame B of a 1A read came back one byte long, # ending in a lone 0x10 -- the kept escape of a checksum byte that was itself # escaped (02 / 03 / 04: about one frame in 85); the parser strips only the byte # after it. The 12 recorded reads never hit it. The read knows how much it asked # for, so it takes exactly that; a leftover other than that one 0x10 is kept, and # the block then fails is_compliance_content. def test_a_frame_with_an_escaped_checksums_stray_byte_is_trimmed_to_what_was_asked(): sfm_read = READS[8] # SFM's own 1024 + 1024 + 42 pattern good = assemble_compliance_content(sfm_read, lengths=(1024, 1024, 42)) assert good == assemble_compliance_content(sfm_read) and is_compliance_content(good) stray = [sfm_read[0] + b"\x10", sfm_read[1], sfm_read[2]] assert len(assemble_compliance_content(stray)) == 2091 # what the bench saw assert assemble_compliance_content(stray, lengths=(1024, 1024, 42)) == good def test_a_leftover_that_is_not_the_stray_escape_is_not_trimmed(): sfm_read = READS[8] odd = [sfm_read[0] + b"\x55", sfm_read[1], sfm_read[2]] assert not is_compliance_content(assemble_compliance_content(odd, lengths=(1024, 1024, 42))) # The same artifact on the waveform-data read a settings write echoes back: it asks # for 0xCA = 202 bytes; one extra trailing 0x10 is the escaped checksum's DLE. # (Unfixed, it fails closed: the write's own block check refuses 203 bytes.) class _Replies: def __init__(self, data): self._data = data self.sent = [] def __call__(self, frame): self.sent.append(frame) def _waveform_proto(data_reply: bytes): from types import SimpleNamespace p = mp.MiniMateProtocol(object()) p._send = _Replies(None) replies = [SimpleNamespace(data=bytes(11)), SimpleNamespace(data=bytes(11) + data_reply)] p._recv_one = lambda expected_sub=None, **kw: replies.pop(0) return p def test_the_waveform_read_drops_an_escaped_checksums_stray_byte(): block = (200).to_bytes(2, "big") + bytes(range(198)) + b"\x17\x17" # N = 200, plus its 2 tail bytes assert _waveform_proto(block).read_waveform_data_raw() == block assert _waveform_proto(block + b"\x10").read_waveform_data_raw() == block assert _waveform_proto(block + b"\x55").read_waveform_data_raw() == block + b"\x55" # anything else is kept