Files
seismo-relay/tests/test_device_access_s4_call_home.py
T
serversdownandClaude Opus 5.5 9e03b3f0ab fix(sfm): a Micromate in an unknown run state gets no call-home write, and a lost ack keeps its snapshot (phase-4 review)
- s4_change_call_home's read_state now returns an unknown(0xNN) run state as
  it is, so the change core refuses it (UnitStateUnknown, nothing sent), as
  start/stop already does; it checked only for calling_home.
- run_change records the snapshot on the lost-ack path too: after an ack is
  lost, what the unit now holds (and, on Series 4, whether [118:120] moved) is
  what tells whether the write landed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-02 17:23:59 -05:00

117 lines
4.5 KiB
Python

"""Series 4 call-home: read the enable bit and dial string, and change it through
the audited path (phase 4). The frames are micromate.writes' (equal to THOR's
for UM12947); [118:120] is unit-maintained and moves after any config write."""
from __future__ import annotations
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from device_access_fakes import FakeMicromateClient, um12947_call_home # noqa: E402
from micromate.opcodes import ENABLEABLE_WRITES # noqa: E402
from micromate.writes import plan_call_home_enabled # noqa: E402
from sfm.device_access.errors import CallHomeInProgress # noqa: E402
from sfm.device_access.models import UnitAddress # noqa: E402
from sfm.device_access.s4 import s4_call_home_state, s4_change_call_home # noqa: E402
ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None)
class Audit:
def __init__(self):
self.rows = {}
def begin(self, **f):
cid = f"c{len(self.rows) + 1}"
self.rows[cid] = dict(f, outcome="pending")
return cid
def finish(self, cid, **f):
self.rows[cid].update(f)
def change(fake, *, enabled, dry_run=False, even_if_already=False):
return s4_change_call_home(fake, "UM20147", enabled=enabled, dry_run=dry_run,
even_if_already=even_if_already, audit=Audit(), address=ADDR,
caller="test", sleep=lambda s: None,
verify_timeout_s=3.0, verify_interval_s=1.0)
def test_call_home_writes_are_now_enableable():
assert {0x7E, 0x7F} <= ENABLEABLE_WRITES
def test_s4_call_home_state_reads_the_enable_bit_and_dial_string():
st = s4_call_home_state(FakeMicromateClient(), "UM20147")
assert (st.serial, st.series, st.enabled, st.dial_string) == ("UM20147", 4, True, "RADIO RING")
assert st.detail["byte5"] == "0x05"
assert st.detail["block"] == um12947_call_home().hex()
assert s4_call_home_state(FakeMicromateClient(call_home=um12947_call_home(False)), "UM20147").enabled is False
def test_s4_dry_run_shows_thors_frames():
fake = FakeMicromateClient()
r = change(fake, enabled=False, dry_run=True)
assert r.outcome == "dry_run"
assert r.plan["steps"] == plan_call_home_enabled(um12947_call_home(), False).describe()["steps"]
assert fake.executed == []
def test_s4_disable_sends_7e_7f_and_verifies():
fake = FakeMicromateClient()
r = change(fake, enabled=False)
assert (r.outcome, r.verified, r.state_after, r.action) == ("sent", "ok", "disabled", "disable call-home")
assert [st.sub for st in fake.executed[0].steps] == [0x7E, 0x7F]
assert fake.call_home[5] == 0x04
def test_s4_unit_maintained_bytes_do_not_make_a_mismatch():
r = change(FakeMicromateClient(), enabled=False)
assert r.verified == "ok"
snap = r.detail["snapshot"]
assert snap["changed_offsets"] == [5, 118, 119]
assert snap["before"] == um12947_call_home().hex() and snap["after"] != snap["before"]
def test_s4_no_change_while_calling_home():
fake = FakeMicromateClient(run_states=[0x03])
with pytest.raises(CallHomeInProgress):
change(fake, enabled=False)
assert fake.executed == []
def test_s4_already_enabled_is_skipped():
fake = FakeMicromateClient()
r = change(fake, enabled=True)
assert r.outcome == "skipped" and fake.executed == []
def test_s4_even_if_already_sends_the_no_op_write():
fake = FakeMicromateClient()
r = change(fake, enabled=True, even_if_already=True)
assert len(fake.executed) == 1
assert any("changes nothing" in w for w in r.plan["warnings"])
def test_s4_unknown_run_state_sends_nothing():
# Final review #3: a run state never measured is refused, as start/stop does.
from sfm.device_access.errors import UnitStateUnknown
fake = FakeMicromateClient(run_states=[0x07])
with pytest.raises(UnitStateUnknown):
change(fake, enabled=False)
assert fake.executed == []
def test_a_lost_ack_still_records_what_the_unit_holds():
# Final review #4: the lost-ack path is where the operator most needs it.
import micromate.protocol as mm_proto
fake = FakeMicromateClient(execute_error=mm_proto.TimeoutError("lost"))
r = change(fake, enabled=False)
assert (r.outcome, r.sent) == ("failed", True)
snap = r.detail["snapshot"]
assert snap["changed_offsets"] == [] and snap["before"] == snap["after"] # the write did not land