"""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"])