Files
seismo-relay/tests/test_device_access_s3_verify.py
T
serversdownandClaude Opus 5.5 c32a9e7851 fix(sfm): a MiniMate download that decodes as nothing is refused, not stored as a histogram
Review of 36f4e5d (measured): with the metadata-page bug put back, 4 of 7
BE12599 non-first waveforms decoded to nothing, and the check stored them
unverified as 'a histogram' -- the worst form of the bug it was built for.
Now: no waveform decode must decode as a histogram (stored unverified) or it is
refused; a waveform's record chain must end on its END record (0x06), as the
file reader requires -- cut-short streams have none; the structure checks run
for every unit and only the peak comparison is limited to a unit confirmed in
normal range (geo_range == 0; an unread range is no longer assumed normal);
a clipped blast at full scale on both sides counts as a match.  Tests pin the
one-step tolerance, both ranges, the END record and the neither case.

No DB or schema change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-10-25 18:47:39 -04:00

143 lines
6.2 KiB
Python

"""A MiniMate download is checked against the unit's own peaks before it is stored.
Series 4 downloads were already checked (decoded PVS against the unit's).
Series 3's were not, so two bad files went into the store as successes at the
bench (2026-10-25). Now: if the waveform decodes, all three geophone channels
must be the same length and each channel's peak must match the unit's own 0C
peak to one 0.005 in/s step, or the download is refused and nothing is stored.
No waveform decode (a histogram -- 0 of ~430 fixture histograms decode as one)
or a sensitive-range unit (its scale is still open) is stored unverified.
"""
from __future__ import annotations
import os
import sys
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.blastware_file as bw # noqa: E402
from minimateplus.framing import S3FrameParser # noqa: E402
from minimate_replay import FIX # noqa: E402
import minimateplus.client as mc # noqa: E402
from minimate_replay import KEY, SessionReplay, fast_clock, recorded_replies # noqa: E402
from minimateplus.models import ComplianceConfig, Event, PeakValues # noqa: E402
from sfm.device_access.errors import MalformedReply # noqa: E402
from sfm.device_access.models import UnitAddress # noqa: E402
from sfm.device_access.s3 import Series3Adapter, verify_s3_download # noqa: E402
CABLE = UnitAddress(host=None, tcp_port=None, port="/dev/ttyUSB0", baud=38400)
COUNTS_PER_IPS = 3200 # 32000 counts = 10.000 in/s (normal range)
@pytest.fixture(autouse=True)
def _fast(monkeypatch):
fast_clock(monkeypatch)
def event(samples, peaks):
ev = Event(index=0)
ev.raw_samples = samples
ev.peak_values = PeakValues(tran=peaks[0], vert=peaks[1], long=peaks[2])
return ev
def flat(n, peak_ips):
return [0] * (n - 1) + [round(peak_ips * COUNTS_PER_IPS)]
NORMAL = ComplianceConfig(geo_range=0)
GOOD = bw.extract_body_bytes( # BE9441 event 1, whole
[f for f in S3FrameParser().feed(FIX.read_bytes()) if f.sub == 0xA5])[1]
_H = (FIX.parent.parent / "ts-fix" / "K441LKZU.C30H").read_bytes() # BE9441's own histogram
HISTOGRAM = _H[bw._WAVEFORM_HEADER_SIZE + 21:-26]
def test_matching_peaks_and_lengths_verify():
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.03, 0.095, 0.02))},
(0.02999996, 0.09499989, 0.01999998))
assert verify_s3_download(ev, NORMAL, body=GOOD)[0] is True
def test_a_peak_that_disagrees_is_refused():
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.01, 0.01, 0.005))},
(0.03, 0.095, 0.02))
with pytest.raises(MalformedReply, match="Tran peak is 0.0100.*not saved"):
verify_s3_download(ev, NORMAL, body=GOOD)
def test_unequal_channel_lengths_are_refused():
ev = event({"Tran": flat(100, 0.03), "Vert": flat(100, 0.095), "Long": flat(90, 0.02)},
(0.03, 0.095, 0.02))
with pytest.raises(MalformedReply, match="lengths.*not saved"):
verify_s3_download(ev, NORMAL, body=GOOD)
def test_a_histogram_is_stored_unverified():
ok, why = verify_s3_download(event(None, (0.03, 0.095, 0.02)), NORMAL, body=HISTOGRAM)
assert ok is None and "histogram" in why
def test_a_body_that_decodes_as_neither_is_refused():
with pytest.raises(MalformedReply, match="neither a waveform nor a histogram.*not saved"):
verify_s3_download(event(None, (0.03, 0.095, 0.02)), NORMAL, body=GOOD[:4000])
def test_a_waveform_without_its_end_record_is_refused():
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.03, 0.095, 0.02))},
(0.03, 0.095, 0.02))
with pytest.raises(MalformedReply, match="end record.*not saved"):
verify_s3_download(ev, NORMAL, body=GOOD[:20_000])
def test_a_sensitive_range_unit_is_structure_checked_but_not_peak_checked():
ev = event({c: flat(100, 0.5) for c in ("Tran", "Vert", "Long")}, (0.03, 0.095, 0.02))
assert verify_s3_download(ev, ComplianceConfig(geo_range=1), body=GOOD)[0] is None
short = event({"Tran": flat(100, 0.5), "Vert": flat(100, 0.5), "Long": flat(90, 0.5)},
(0.03, 0.095, 0.02))
with pytest.raises(MalformedReply, match="lengths"):
verify_s3_download(short, ComplianceConfig(geo_range=1), body=GOOD)
def test_an_unknown_range_is_not_peak_checked():
ev = event({c: flat(100, 0.5) for c in ("Tran", "Vert", "Long")}, (0.03, 0.095, 0.02))
assert verify_s3_download(ev, ComplianceConfig(), body=GOOD)[0] is None
def test_a_clipped_blast_is_not_refused():
ev = event({c: [0] * 99 + [32016] for c in ("Tran", "Vert", "Long")}, (10.0, 10.0, 10.0))
assert verify_s3_download(ev, NORMAL, body=GOOD)[0] is True
def test_the_tolerance_is_one_step():
near = event({c: flat(100, 0.095) for c in ("Tran", "Vert", "Long")}, (0.095 + 0.005,) * 3)
assert verify_s3_download(near, NORMAL, body=GOOD)[0] is True
far = event({c: flat(100, 0.095) for c in ("Tran", "Vert", "Long")}, (0.095 + 0.0055,) * 3)
with pytest.raises(MalformedReply):
verify_s3_download(far, NORMAL, body=GOOD)
def adapter():
def factory(address, **kw):
return mc.MiniMateClient(transport=SessionReplay(recorded_replies()))
return Series3Adapter(client_factory=factory)
def test_be9441_event_1_downloads_verified():
dl = adapter().download_event(CABLE, "BE9441", KEY, "20261025141049")
assert dl.verified is True
def test_the_check_refuses_the_file_the_metadata_page_bug_produced(monkeypatch):
real = bw._frame_body_bytes
calls = []
def old_rule(frame, skip): # the bug, back: frames 1 and 2 cut as metadata pages
calls.append(1)
return real(frame, skip + 1 if len(calls) in (2, 3) and skip == 12 else skip)
monkeypatch.setattr(bw, "_frame_body_bytes", old_rule)
with pytest.raises(MalformedReply, match="not saved"):
adapter().download_event(CABLE, "BE9441", KEY, "20261025141049")