"""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 + 1 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)