Files
seismo-relay/tests/test_device_access_s3_events.py
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

138 lines
5.6 KiB
Python

"""Series-3 events: listed from headers, one downloaded, mapped into the shared shape."""
from __future__ import annotations
import datetime
import os
import struct
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from device_access_fakes import FakeMiniMateClient, s3_event # noqa: E402
from sfm.device_access.errors import ( # noqa: E402
EventNotFound, MalformedReply, StaleEvent, WrongUnit,
)
from sfm.device_access.models import UnitAddress # noqa: E402
from sfm.device_access.s3 import Series3Adapter # noqa: E402
TCP = UnitAddress(host="10.0.0.5", tcp_port=12345, port=None, baud=None)
FIRST = bytes.fromhex("01110000")
def adapter(*clients):
queue = list(clients)
seen = []
def factory(address, **kw):
seen.append(kw)
return queue.pop(0)
a = Series3Adapter(client_factory=factory)
a.seen = seen
return a
def test_listing_maps_each_header_into_the_shared_shape():
out = adapter(FakeMiniMateClient()).list_events(TCP, "BE18193")
assert (out.serial, out.series, len(out.events)) == ("BE18193", 3, 2)
e = out.events[1]
assert e.event_id == "BE18193:0111245a:20261002093015"
assert (e.index, e.timestamp) == (1, "2026-10-02T09:30:15")
# The 0C record does not say waveform or histogram (every layout reads "Waveform").
assert e.record_type is None and "not decoded yet" in e.detail["record_type_note"]
assert e.peaks_ips == {"tran": 0.05, "vert": 0.08, "long": 0.03}
assert e.peak_vector_sum_ips == 0.12 and e.detail["mic_psi"] == 0.0001
assert e.filename and e.filename.startswith("T193")
def test_listing_uses_the_header_walk_and_the_default_timeout():
# A header walk is small replies only; a lost one should cost 10 s, not 120 s.
fake = FakeMiniMateClient()
a = adapter(fake)
a.list_events(TCP, "BE18193")
assert "list_event_headers" in fake.calls and "download_event_by_key" not in fake.calls
assert a.seen == [{}]
assert fake.closed == 1
def test_downloading_uses_the_long_timeout():
a = adapter(FakeMiniMateClient())
a.download_event(TCP, "BE18193", FIRST, None)
assert a.seen == [{"timeout": 120.0}]
def test_listing_refuses_a_different_unit():
with pytest.raises(WrongUnit):
adapter(FakeMiniMateClient(serial="BE11529")).list_events(TCP, "BE18193")
def test_download_returns_the_event_its_frames_and_the_settings():
fake = FakeMiniMateClient()
dl = adapter(fake).download_event(TCP, "BE18193", FIRST, "20261001080000")
assert (dl.series, dl.event_id, dl.verified) == (3, "BE18193:01110000:20261001080000", True)
assert dl.event is fake.events[0] and dl.a5_frames is fake.events[0]._a5_frames
assert dl.a5_frames and all(f.sub == 0xA5 for f in dl.a5_frames)
assert dl.compliance is fake.compliance and dl.filename.startswith("T193")
assert dl.timestamp == datetime.datetime(2026, 10, 1, 8, 0, 0)
# #8: the download carries the Blastware file itself, for the file-import path.
from minimateplus.blastware_file import blastware_file_bytes
assert dl.bw_bytes and dl.bw_bytes == blastware_file_bytes(dl.event, dl.a5_frames)
def test_download_refuses_a_different_unit_before_downloading():
fake = FakeMiniMateClient(serial="BE11529")
with pytest.raises(WrongUnit):
adapter(fake).download_event(TCP, "BE18193", FIRST, None)
assert "download_event_by_key" not in fake.calls
def test_a_key_that_is_not_stored_is_404():
with pytest.raises(EventNotFound):
adapter(FakeMiniMateClient()).download_event(TCP, "BE18193", bytes.fromhex("0111ffff"), None)
def test_a_stale_id_is_refused_rather_than_downloading_another_event():
fake = FakeMiniMateClient()
with pytest.raises(StaleEvent, match="20261001080000"):
adapter(fake).download_event(TCP, "BE18193", FIRST, "20250101000000")
# The walk was told what to expect, so it compared after 0C and sent no 5A.
assert fake.download_requests == [(FIRST, "2025-01-01T00:00:00")]
assert fake.last_download is not None and fake.last_download._a5_frames is None
def test_an_id_without_a_timestamp_skips_the_check():
fake = FakeMiniMateClient()
dl = adapter(fake).download_event(TCP, "BE18193", FIRST, None)
assert dl.event_id == "BE18193:01110000:20261001080000"
assert fake.download_requests == [(FIRST, None)]
def test_a_target_whose_record_could_not_be_read_is_not_called_stale():
# A lost 0C reply leaves no timestamp to compare: that is a bad reply, not an erase.
ev = s3_event(0, "01110000", datetime.datetime(2026, 10, 1, 8, 0, 0))
ev.timestamp = None
fake = FakeMiniMateClient(events=[ev])
with pytest.raises(MalformedReply, match="0C"):
adapter(fake).download_event(TCP, "BE18193", FIRST, "20261001080000")
assert fake.last_download._a5_frames is None
def test_a_failed_5a_is_a_clear_error():
fake = FakeMiniMateClient(events=[s3_event(0, "01110000",
datetime.datetime(2026, 10, 1, 8, 0, 0), frames=False)])
with pytest.raises(MalformedReply, match="5A"):
adapter(fake).download_event(TCP, "BE18193", FIRST, None)
def test_an_assembly_fault_is_a_named_502_not_a_bare_500():
# Once the 64 KB page bug's signature; that is fixed (2026-10-26), and the
# mapping stays as a defence against any struct.error in assembly.
fake = FakeMiniMateClient()
def boom(key4, **kw):
raise struct.error("argument out of range")
fake.download_event_by_key = boom
with pytest.raises(MalformedReply, match="could not be assembled.*not saved"):
adapter(fake).download_event(TCP, "BE18193", FIRST, None)