Files
seismo-relay/tests/test_device_access_s3.py
serversdownandClaude Opus 5.5 66bb29b9d4 feat(s3): change a MiniMate's settings by serial on the shared codec
Series3Adapter.settings reads the 1A block assembled into its 2,090
logical bytes and decodes it with micromate.setup_fields (a monitoring
unit's device-info answer is refused with 'stop monitoring first').
change_settings runs the shared change core: read the four settings
blocks, patch the settings block's fields, send Blastware's sequence
through push_config_raw with the event index, trigger and waveform blocks
as read, re-read.  Refused before sending: monitoring, a value the unit
does not allow, a stale preview.  push_config_raw's pre-check now raises
ImageRefused, so a refused image is audited as nothing sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-03 21:46:50 -05:00

88 lines
3.5 KiB
Python

"""The Series-3 adapter: connect-per-request, mapped into the shared models."""
from __future__ import annotations
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import minimateplus.protocol as mmp_proto # noqa: E402
from device_access_fakes import FakeMiniMateClient # noqa: E402
from sfm.device_access.errors import ( # noqa: E402
ConnectFailed, MalformedReply, NoAnswer, UnitStateUnknown, 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)
SERIAL_LINK = UnitAddress(host=None, tcp_port=None, port="/dev/ttyUSB0", baud=None)
def adapter(*clients):
queue = list(clients)
return Series3Adapter(client_factory=lambda address: queue.pop(0))
def test_identify_maps_device_info():
fake = FakeMiniMateClient()
info = adapter(fake).identify(TCP, "BE18193")
assert (info.serial, info.series, info.model, info.firmware) == ("BE18193", 3, "MiniMate Plus", "S338.17")
assert info.active_setup == "BRIDGE-A"
assert info.monitoring is None # connect() does not read it on Series 3
assert fake.closed == 1
def test_a_different_unit_answering_is_refused():
fake = FakeMiniMateClient(serial="BE11529")
with pytest.raises(WrongUnit, match="BE18193.*BE11529"):
adapter(fake).identify(TCP, "BE18193")
assert fake.closed == 1
def test_status_maps_monitor_status():
fake = FakeMiniMateClient(monitoring=True)
st = adapter(fake).status(TCP, "BE18193")
assert (st.series, st.monitoring, st.battery_volts) == (3, True, 6.8)
assert (st.memory_total_bytes, st.memory_free_bytes) == (983026, 900000)
assert st.event_count is None and st.device_time is None
assert st.detail == {"serial_verified": False}
assert fake.calls == ["poll", "get_monitor_status"]
def test_settings_reads_the_logical_block():
# Phase 5: the settings come from the 1A read assembled into its 2,090 bytes and
# decoded by micromate.setup_fields (tests/test_device_access_s3_settings.py has
# the rest); the fake's block is BE9441's of 2026-10-26.
fake = FakeMiniMateClient()
s = adapter(fake).settings(TCP, "BE18193")
assert (s.record_mode, s.sample_rate, s.setup_name) == ("Histogram", 4096, "Standard Recording Setup.set")
assert fake.calls.index("connect") < fake.calls.index("read_compliance_content")
def test_settings_of_a_monitoring_unit_is_refused_and_not_retried():
# One fake only: a retry would ask the factory for a second and fail.
with pytest.raises(UnitStateUnknown, match="settings"):
adapter(FakeMiniMateClient(monitoring_settings=True)).settings(TCP, "BE18193")
def test_a_cold_unit_over_tcp_gets_one_retry():
first = FakeMiniMateClient(fail=[mmp_proto.TimeoutError("cold")])
second = FakeMiniMateClient()
info = adapter(first, second).identify(TCP, "BE18193")
assert info.serial == "BE18193"
assert first.closed == 1 and second.closed == 1
def test_a_serial_link_is_not_retried():
fake = FakeMiniMateClient(fail=[mmp_proto.TimeoutError("quiet")])
with pytest.raises(NoAnswer):
adapter(fake).identify(SERIAL_LINK, "BE18193")
def test_a_failed_connect_is_connect_failed_and_not_retried():
fake = FakeMiniMateClient(open_error=ConnectionRefusedError("refused"))
with pytest.raises(ConnectFailed, match="could not connect"):
adapter(fake).identify(TCP, "BE18193")