Files
seismo-relay/tests/test_5a_blastware_walk_histogram.py
T
serversdownandClaude Opus 5.5 9492e49400 fix(sfm): store a MiniMate download through the Blastware-file import path, as production does (#8)
A live Series-3 download was stored from the live 5A decode (store.save): a
histogram got no samples and nothing to plot (bench 2026-10-29).  The download
now carries the assembled Blastware file (DownloadedEvent.bw_bytes, built by the
new blastware_file_bytes -- byte-identical to Blastware's own K441LTON.900 and
K441LTOK.R10), and persist_download stores it through save_imported_bw, the
path the watcher uses for Blastware's files, the way Series 4 already uses
save_imported_idf.  The event is decoded from the file: histogram samples,
setup-block sample rate and record time (#44/#45).  source.kind is 'sfm-live'.

The file path does not yet read three things right (measured on BE9441): a
histogram's vector sum (0.503 where Blastware and the unit say 0.385 -- the
decode checker's open finding), the mic peak (0), and a sensitive range.  So the
unit's own 0C channel peaks, mic and vector sum are overlaid, as Series 4 does,
except an impossible vector sum (below the largest channel; BE9441's 0C read
-0.0, bench #4), and the unit's range is passed through for the stored samples.

Store consequence: new live MiniMate downloads only; existing rows and files are
untouched; no backfill, no migration.  The record type still reads 'Waveform'
for a live histogram until #41 (record type from STRT[18]) lands from the decode
checker's branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-10-29 23:27:44 -04:00

136 lines
4.6 KiB
Python

"""SFM's 5A walk past the old 128-chunk cap, against Blastware (BE9441, 2026-10-26).
Event 2 of tests/fixtures/minimate-bench-10-26-26-histogram/ is a 71,088-byte
histogram: 139 requests, crossing two 64 KB pages. Before the walk was sized from
STRT, a caller's max_chunks=128 cut it short. Fed the unit's recorded replies,
SFM's walk must send exactly Blastware's requests and rebuild Blastware's file.
"""
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 blastware_file_bytes, write_blastware_file # noqa: E402
from minimateplus.framing import S3FrameParser # noqa: E402
from minimateplus.models import Event # noqa: E402
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-histogram"
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 _requests() -> list:
raw, out, i = (FIX / "requests.bin").read_bytes(), [], 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 raw[j] != 0x03:
j += 2 if raw[j] == 0x10 else 1
out.append(raw[i:j + 1])
i = j + 1
return out
def _addr_len(frame: bytes) -> tuple:
p = _unstuff(frame[2:-1])
return int.from_bytes(p[6:10], "big"), int.from_bytes(p[4:6], "big")
def _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
FIVE_A = [f for f in _requests() if _unstuff(f[2:-1])[2] == 0x5A]
REPLIES = {}
for _q, _r in zip(FIVE_A, _a5_replies()):
REPLIES.setdefault(_addr_len(_q), _r)
class ByAddress:
def __init__(self):
self.sent, self._buf = [], b""
def write(self, data):
data = bytes(data)
self.sent.append(data)
self._buf += 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 last complete walk for the event at `start` (it also cancelled one)."""
starts = [k for k, f in enumerate(FIVE_A) if _addr_len(f) == (start, 0x200)]
for i in reversed(starts):
walk = [FIVE_A[i]]
while i + 1 < len(FIVE_A) and _addr_len(walk[-1])[1] == 0x200:
i += 1
walk.append(FIVE_A[i])
if _addr_len(walk[-1])[1] != 0x200:
return walk
raise AssertionError("no complete walk")
@pytest.fixture(autouse=True)
def _fast(monkeypatch):
fast_clock(monkeypatch)
def test_the_recording_pairs_up():
assert len(FIVE_A) == len(_a5_replies()) == 722
@pytest.mark.parametrize("key,bw,n", [("0111f210", "K441LTON.900", 139),
("011106a0", "K441LTOK.R10", 118)])
def test_the_walk_is_blastwares_and_rebuilds_its_file(key, bw, n, tmp_path):
t = ByAddress()
frames = mp.MiniMateProtocol(t).read_bulk_waveform_stream(
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True,
strict=True, max_chunks=128)
want = _blastware_walk(int(key, 16))
assert len(want) == n and t.sent == want
ev = Event(index=0)
ev.timestamp = None
write_blastware_file(ev, frames, tmp_path / bw)
assert (tmp_path / bw).read_bytes() == (FIX / "blastware" / bw).read_bytes()
@pytest.mark.parametrize("key,bw", [("0111f210", "K441LTON.900"), ("011106a0", "K441LTOK.R10")])
def test_blastware_file_bytes_is_blastwares_file(key, bw):
# What a live MiniMate download hands to the store (#8): the file itself, as bytes.
frames = mp.MiniMateProtocol(ByAddress()).read_bulk_waveform_stream(
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True,
strict=True, max_chunks=128)
ev = Event(index=0)
ev.timestamp = None
assert blastware_file_bytes(ev, frames) == (FIX / "blastware" / bw).read_bytes()