From 83d1a98c1c9954a018bfae10b10db4335775cea1 Mon Sep 17 00:00:00 2001 From: serversdown Date: Wed, 21 Oct 2026 16:52:43 -0400 Subject: [PATCH] feat(micromate): fetch_event returns the bytes with the checked decode get_event() now delegates to fetch_event(), which returns (blob, result). The same peak-vector-sum self-check applies, so a DecodeMismatch still stops the bytes from being stored. get_event() keeps its signature and behaviour. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- micromate/client.py | 13 ++++++++- tests/test_micromate_events.py | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/micromate/client.py b/micromate/client.py index b73f2f5..c29efe2 100644 --- a/micromate/client.py +++ b/micromate/client.py @@ -550,6 +550,17 @@ class MicromateClient: Histograms are not verified: `samples` is empty for them. """ + return self.fetch_event(ref, verify=verify, tolerance=tolerance, + chunk_size=chunk_size)[1] + + def fetch_event(self, ref: MicromateEventRef, *, verify: bool = True, + tolerance: float = 0.01, chunk_size: Optional[int] = None): + """`get_event()`, returning the raw bytes too: `(blob, result)`. + + For a caller that stores the file AND wants the self-check: the bytes go + to the waveform store unchanged, and a DecodeMismatch stops them being + stored at all. + """ if not ref.record_type: raise ValueError( f"event {ref.key_hex}: record_type is unknown, so the codec " @@ -580,7 +591,7 @@ class MicromateClient: ) if err is not None: log.debug("event %s: PVS agrees to %+.4f%%", ref.uid, 100 * err) - return result + return blob, result @staticmethod def decode_error(ref: MicromateEventRef, result) -> Optional[float]: diff --git a/tests/test_micromate_events.py b/tests/test_micromate_events.py index 883ed26..550c411 100644 --- a/tests/test_micromate_events.py +++ b/tests/test_micromate_events.py @@ -343,3 +343,52 @@ def test_a_corrupted_stored_peak_is_caught_by_the_self_check(): mm.get_event(ref, chunk_size=1024) return pytest.fail("no waveform event found in the capture") + + +# ── fetch_event: the bytes and the checked decode together ─────────────────── + +import math # noqa: E402 + +UM20147_W = Path(__file__).parent / "fixtures" / "thor-idf" / "UM20147_20250531135901.IDFW" + + +def _our_pvs(result): + from micromate.idf_file import geo_count_to_ips + ch = {k.lower(): v for k, v in result.samples.items()} + t, v, l = ch["tran"], ch["vert"], ch["long"] + return max(math.sqrt(geo_count_to_ips(t[i]) ** 2 + geo_count_to_ips(v[i]) ** 2 + + geo_count_to_ips(l[i]) ** 2) + for i in range(min(len(t), len(v), len(l)))) + + +def _fixture_client(monkeypatch, blob): + mm, _t = client([]) + monkeypatch.setattr(mm, "download_event", lambda ref, chunk_size=None: blob) + return mm + + +def _w_ref(pvs): + return MicromateEventRef( + index=0, key=bytes.fromhex("055d4a81"), size=UM20147_W.stat().st_size, + serial="UM20147", record_type="waveform", + timestamp=datetime.datetime(2025, 5, 31, 13, 59, 1), peak_vector_sum_ips=pvs) + + +def test_fetch_event_returns_the_downloaded_bytes_with_the_decode(monkeypatch): + blob = UM20147_W.read_bytes() + mm = _fixture_client(monkeypatch, blob) + probe = mm.fetch_event(_w_ref(None), verify=False)[1] + got, result = mm.fetch_event(_w_ref(_our_pvs(probe))) + assert got == blob and result.samples + + +def test_fetch_event_refuses_a_decode_that_disagrees_with_the_unit(monkeypatch): + mm = _fixture_client(monkeypatch, UM20147_W.read_bytes()) + probe = mm.fetch_event(_w_ref(None), verify=False)[1] + with pytest.raises(DecodeMismatch): + mm.fetch_event(_w_ref(_our_pvs(probe) * 1.05)) + + +def test_get_event_is_fetch_event_without_the_bytes(monkeypatch): + mm = _fixture_client(monkeypatch, UM20147_W.read_bytes()) + assert mm.get_event(_w_ref(None), verify=False).samples