Files
seismo-relay/tests/test_5a_blastware_walk.py
T
serversdownandClaude Opus 5.5 f61fc3cc78 test(minimateplus): the walk rebuilds Blastware's own files for the page-crossing events
Brian's Blastware files from the recorded session (events 0, 1, 3, 7 -- three
cross a 64 KB page) as fixtures; SFM's walk, fed Blastware's recorded replies,
writes each one byte for byte.  Live the same day, SFM's own downloads of all 8
events were byte-identical to Blastware's (sha256), the 4 crossings included --
and events 2-5's shorter sample counts (20,490-21,011 vs 21,504) are in
Blastware's files too: the unit's own recording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-10-26 14:45:38 -04:00

168 lines
6.3 KiB
Python

"""SFM's MiniMate requests and 5A walk, against Blastware's own (recorded 2026-10-26).
Blastware downloaded all 8 of BE9441's events -- three of which cross a 64 KB page
-- while a bridge recorded both directions (fixture README). Every one of its 748
requests is the standard 16-byte payload with a SUM8 checksum and every 02 / 03 /
04 / 10 byte DLE-escaped; a 5A request carries a length (0x0200, or the remainder
for TERM) and a 4-byte linear address. The walk is the event's start, then
+0x200 at a time carrying into the page byte, then TERM at the next boundary for
exactly the remainder. SFM must send exactly those requests.
"""
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__))))
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import minimateplus.protocol as mp # noqa: E402
from minimate_replay import fast_clock # noqa: E402
from minimateplus.blastware_file import extract_body_bytes, write_blastware_file # noqa: E402
from minimateplus.models import Event # noqa: E402
from minimateplus.framing import S3FrameParser, build_5a_request, build_bw_frame, waveform_key_params # noqa: E402
from minimateplus.waveform_codec import STREAM_END_ID, decode_waveform_v2, walk_records # noqa: E402
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-blastware"
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 bw_requests() -> list:
"""Blastware's requests, each as wire bytes; the SESSION_RESETs dropped."""
raw, out, i = (FIX / "blastware_requests.bin").read_bytes(), [], 0
while i < len(raw):
if raw[i:i + 2] == b"\x41\x03":
i += 2
continue
j = i + 2
while raw[j] != 0x03: # the first ETX not escaped by a DLE
j += 2 if raw[j] == 0x10 else 1
out.append(raw[i:j + 1])
i = j + 1
return out
def logical(frame: bytes) -> bytes:
return _unstuff(frame[2:-1])[:-1] # the 16-byte payload, checksum dropped
def unit_a5_replies() -> list:
raw, out, start, p = (FIX / "unit_replies.bin").read_bytes(), [], 0, S3FrameParser()
for i in range(len(raw)):
for f in p.feed(raw[i:i + 1]):
if f.sub == 0xA5:
out.append(raw[start:i + 1])
start = i + 1
return out
REQS = bw_requests()
FIVE_A = [f for f in REQS if logical(f)[2] == 0x5A]
def addr_len(frame: bytes) -> tuple:
p = logical(frame)
return int.from_bytes(p[6:10], "big"), int.from_bytes(p[4:6], "big")
def test_the_recording_is_what_the_readme_says():
assert (len(REQS), len(FIVE_A)) == (748, 540)
def test_every_blastware_5a_request_is_rebuilt_byte_for_byte():
assert [build_5a_request(*addr_len(f)) for f in FIVE_A] == FIVE_A
def test_every_other_blastware_request_is_rebuilt_byte_for_byte():
others = [f for f in REQS if logical(f)[2] != 0x5A]
assert len(others) == 208
for f in others:
p = logical(f)
assert build_bw_frame(p[2], p[5], p[6:16]) == f
def test_a_standard_request_escapes_02_03_04_in_its_params_and_checksum():
frame = build_bw_frame(0x0A, 0x46, waveform_key_params(bytes.fromhex("01120203")))
assert b"\x10\x02\x10\x03" in frame # the key's 02 and 03
for low in range(256): # a checksum of 0x03
f = build_bw_frame(0x0A, 0x46, waveform_key_params(bytes([0x01, 0x11, 0x50, low])))
if sum(_unstuff(f[2:-1])[:-1]) & 0xFF == 0x03:
assert f.endswith(b"\x10\x03\x03")
break
else:
pytest.fail("no key gave a checksum of 0x03")
# -- the walk ------------------------------------------------------------------
class ByAddress:
"""Answers each 5A request with Blastware's recorded reply for the same
(address, length); records what SFM sent."""
def __init__(self):
self.replies = {addr_len(q): r for q, r in zip(FIVE_A, unit_a5_replies())}
self.sent, self._buf = [], b""
def write(self, data):
data = bytes(data)
self.sent.append(data)
self._buf += self.replies.get(addr_len(data), b"")
def read(self, n):
out, self._buf = self._buf[:n], self._buf[n:]
return out
def blastware_walk(start: int) -> list:
"""Blastware's requests for the event that starts at `start`."""
i = next(k for k, f in enumerate(FIVE_A) if addr_len(f) == (start, 0x200))
walk = [FIVE_A[i]]
while addr_len(walk[-1])[1] == 0x200: # until TERM (a short read)
i += 1
walk.append(FIVE_A[i])
return walk
@pytest.fixture(autouse=True)
def _fast(monkeypatch):
fast_clock(monkeypatch)
@pytest.mark.parametrize("key", ["01110000", "01118a2c", "0112a0b6", "0114b0b2"])
def test_the_walk_sends_exactly_blastwares_requests_and_the_event_is_whole(key):
t = ByAddress()
frames = mp.MiniMateProtocol(t).read_bulk_waveform_stream(
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True, strict=True)
want = blastware_walk(int(key, 16))
assert t.sent == want
assert len(frames) == len(want)
_s, body, _f = extract_body_bytes(frames)
recs = walk_records(body)
assert body[recs[-1]["end"] + 4] == STREAM_END_ID
dec = decode_waveform_v2(body)
assert len({len(dec[c]) for c in ("Tran", "Vert", "Long")}) == 1
@pytest.mark.parametrize("key,bw", [("01110000", "K441LTOF.ZI0"), ("01118a2c", "K441LTOF.ZQ0"),
("0112a0b6", "K441LTOG.000"), ("0114b0b2", "K441LTOG.0S0")])
def test_the_file_built_from_the_walk_is_blastwares_own(key, bw, tmp_path):
frames = mp.MiniMateProtocol(ByAddress()).read_bulk_waveform_stream(
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True, strict=True)
ev = Event(index=0)
ev.timestamp = None # the builder takes the footer from the TERM frame
path = tmp_path / bw
write_blastware_file(ev, frames, path)
assert path.read_bytes() == (FIX / "blastware" / bw).read_bytes()