Files
seismo-relay/tests/test_s3_settings_read.py
T
serversdownandClaude Opus 5.5 1bb15573fc feat(s3): assemble a settings read into the 2,090-byte block
Unescape each 1A data frame, take data[11:], drop the one non-content
byte that starts every frame but the last: 2,090 bytes headed by
N = 2090, Series IV's 0x1A layout.  Measured on BE9441's 10-26
recording (12 reads, both frame patterns); the read after each
Blastware write equals the image written.  read_compliance_content is
strict (a missing frame raises) and unjudged (a monitoring unit's
device-info answer comes back for the caller to recognise).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-03 21:28:04 -05:00

102 lines
3.7 KiB
Python

"""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