Files
seismo-relay/tests/test_minimate_body_no_metadata.py
T
serversdownandClaude Opus 5.5 465bc704dd test+docs: SFM's MiniMate downloads are byte-identical to Blastware's; the 64 KB bug narrowed
Brian's Blastware downloads of BE9441's 20 events give ground truth.  SFM's
re-run downloads of events 0, 1, 3, 4 and 10 are byte-identical to Blastware's
own files; imported first through the watcher route, the same events download
as already_stored (no duplicate rows -- the unit's 0C and Blastware's file agree
on serial + timestamp).  Fixtures: Blastware's P00/Q10 (now asserted equal to
SFM's builds) and QZ0/T80, the two page-crossing events, as byte-exact targets.

The 64 KB page bug, narrowed by a read-only test walk: carrying the page byte
addresses the first chunk in page 0x12 (answered), but the next request carries
a raw 0x03 inside its params (counter 0x032c) and gets no reply.  Pages after
0x11 have such counters; 0x11 does not (event 1 skips 0x0000-0x05FF).  Needs a
Blastware wire capture of event 2 or 19 (recorded in CLAUDE.md, the Series-III
reference and the spec).  No code change.

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

126 lines
5.9 KiB
Python

"""A non-first MiniMate event's 5A stream has no metadata pages.
The event-1 walk fetches the two global metadata pages (0x1002, 0x1004) as A5
frames 1 and 2, which carry a 6-byte inner header (skip 13). A non-first event's
walk fetches none, so its frames 1 and 2 are ordinary sample chunks (skip 12).
The body builder skipped 13 on frames 1 and 2 unconditionally: every non-first
event lost two bytes and decoded truncated. Found on BE9441 at the bench
(2026-10-25); fixtures are its raw replies (see the fixture README).
"""
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__))))
from minimateplus.blastware_file import extract_body_bytes, write_blastware_file # noqa: E402
from minimateplus.event_file_io import read_blastware_file # noqa: E402
from minimateplus.framing import S3FrameParser # noqa: E402
from minimateplus.models import Event # noqa: E402
from minimateplus.waveform_codec import decode_waveform_v2 # noqa: E402
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-25-26"
SAMPLES = 21_504 # 5 s record + 0.25 s pre-trigger, at 4096 sps
UNIT_PEAKS = { # the unit's own 0C peaks, in/s
"event0": {"Tran": 0.020, "Vert": 0.060, "Long": 0.015},
"event1": {"Tran": 0.030, "Vert": 0.095, "Long": 0.020},
}
FILES = {"event0": "BE9441_event0_01110000_rx.bin", "event1": "BE9441_event1_0111646a_rx.bin"}
def a5_frames(which):
return [f for f in S3FrameParser().feed((FIX / FILES[which]).read_bytes()) if f.sub == 0xA5]
@pytest.mark.parametrize("which", ["event0", "event1"])
def test_the_body_decodes_whole_and_matches_the_units_own_peaks(which):
_strt, body, _footer = extract_body_bytes(a5_frames(which))
dec = decode_waveform_v2(body)
for ch, peak in UNIT_PEAKS[which].items():
assert len(dec[ch]) == SAMPLES, ch
assert max(abs(x) for x in dec[ch]) * 0.005 == pytest.approx(peak, abs=1e-9), ch
def test_metadata_pages_are_recognised_by_their_echoed_addresses():
first, other = a5_frames("event0"), a5_frames("event1")
assert (bytes(first[1].data[4:6]), bytes(first[2].data[4:6])) == (b"\x10\x02", b"\x10\x04")
assert bytes(other[1].data[4:6]) not in (b"\x10\x02", b"\x10\x04")
@pytest.mark.parametrize("which", ["event0", "event1"])
def test_the_written_file_reads_back_whole(which, tmp_path):
ev = Event(index=0)
ev.timestamp = None # the builder takes the footer from the TERM frame
path = tmp_path / f"{which}.P00"
write_blastware_file(ev, a5_frames(which), path)
pv = read_blastware_file(path).peak_values
got = {"Tran": pv.tran, "Vert": pv.vert, "Long": pv.long}
assert got == pytest.approx(UNIT_PEAKS[which], abs=1e-6)
def test_a_sample_chunk_at_0x1002_is_not_mistaken_for_a_header_byte():
"""Every A5 header is 12 bytes once inner DLE pairs (10 02/03/04) count as one;
the TERM's is 11. A sample chunk echoing address 0x1002 carries 13 raw header
bytes, like a metadata page -- the general rule handles both."""
frames = a5_frames("event1")
_s, want, _f = extract_body_bytes(frames)
one = frames[1]
hdr, payload = bytes(one.data[:12]), bytes(one.data[12:])
assert hdr[4:6] != b"\x10\x02"
moved = type(one)(**{**vars(one), "data": hdr[:4] + b"\x10\x02" + b"\x00" + hdr[6:] + payload})
_s, got, _f = extract_body_bytes([frames[0], moved] + frames[2:])
assert got == want
GOLDEN = { # sha256 of STRT + body + footer, and of the written file
# Both written files are byte-identical to Blastware's own downloads of the same
# events (fixtures/minimate-bench-10-25-26/blastware/, compared below).
# event 0's file is byte-identical to the one SFM stored at the bench before any fix
"event0": ("29beabfe46a858587b8cdc6ee3f484473c07f96485e404f68c42c6aa1e298aa7",
"589073a096d4f867c5ab8c697c82c4b635c7757fcfdd618b460f3b647c32ed7f"),
# event 1's is the bench's 25,810 bytes plus the 2 the metadata-page bug cut
"event1": ("25026e6f27b0e9e9508ceb49dcb0c44df688ee4f71c05b91826b088d4de68031",
"e74c664daaf22d5ae4a7b2464bf6d7aca432f2a10e00fd20c8f6b176999e71b3"),
}
@pytest.mark.parametrize("which", ["event0", "event1"])
def test_the_body_and_file_are_byte_exact(which, tmp_path):
"""Pins every header skip, the TERM's included: a TERM cut one byte off loses
a byte after the END record, which no decode and no peak check can see."""
import hashlib
frames = a5_frames(which)
strt, body, footer = extract_body_bytes(frames)
ev = Event(index=0)
ev.timestamp = None
path = tmp_path / "x.P00"
write_blastware_file(ev, frames, path)
assert hashlib.sha256(strt + body + footer).hexdigest() == GOLDEN[which][0]
assert hashlib.sha256(path.read_bytes()).hexdigest() == GOLDEN[which][1]
def test_a_term_at_0x1002_is_not_mistaken_for_a_header_byte():
"""The TERM's header (11 stripped bytes: one byte, its 4-byte address, zeros)
takes 12 raw bytes when the address it echoes carries an inner DLE pair."""
frames = a5_frames("event1")
_s, want, _f = extract_body_bytes(frames)
term = frames[-1]
d = bytes(term.data)
assert d[1:3] == b"\x01\x11" and d[3:5] != b"\x10\x02"
moved = type(term)(**{**vars(term), "data": d[:3] + b"\x10\x02" + b"\x00" + d[5:]})
_s, got, _f = extract_body_bytes(frames[:-1] + [moved])
assert got == want
@pytest.mark.parametrize("which,bw", [("event0", "K441LTMK.P00"), ("event1", "K441LTMK.Q10")])
def test_the_written_file_is_blastwares_own(which, bw, tmp_path):
ev = Event(index=0)
ev.timestamp = None
path = tmp_path / "x"
write_blastware_file(ev, a5_frames(which), path)
assert path.read_bytes() == (FIX / "blastware" / bw).read_bytes()