Files
seismo-relay/tests/test_device_access_change_kind.py
serversdownandClaude Opus 5.5 1dd5bfa464 refactor(sfm): run_change takes a ChangeKind -- start/stop unchanged, call-home next (phase 4)
run_change named its change and its target state from a start/stop bool.  A
ChangeKind (action, want, became) now says what the change is called and which
state read back means it took; monitoring_change(start) gives exactly today's
names, call_home_change(enabled) the new ones.  An optional snapshot hook
records what the unit holds after the change in the result and the audit row
(never the verdict; one that raises is recorded as its error).

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

89 lines
3.2 KiB
Python

"""run_change is kind-agnostic (phase 4): a ChangeKind names the change and the
state that means it took, so call-home rides the audited path start/stop uses.
Fake callables only -- no device."""
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 sfm.device_access.change import ( # noqa: E402
ChangeKind, call_home_change, monitoring_change, run_change,
)
from sfm.device_access.models import UnitAddress # 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(kind, states, *, verify=(6.0, 2.0), snapshot=None, audit=None):
states = list(states)
audit = audit or Audit()
result = run_change(
kind=kind, serial="UM20147", series=4, dry_run=False, even_if_already=False,
audit=audit, address=ADDR, caller="test", sleep=lambda s: None,
verify_timeout_s=verify[0], verify_interval_s=verify[1],
read_serial=lambda: "UM20147",
read_state=lambda: states.pop(0) if len(states) > 1 else states[0],
describe_plan=lambda: {"steps": []}, send=lambda: {"subs": ["0x81"]},
snapshot=snapshot)
return result, audit
def test_monitoring_kinds_are_todays_names():
assert monitoring_change(True) == ChangeKind("start monitoring", "monitoring", "started")
assert monitoring_change(False) == ChangeKind("stop monitoring", "idle", "stopped")
def test_call_home_kinds():
assert call_home_change(True) == ChangeKind("enable call-home", "enabled", "enabled")
assert call_home_change(False) == ChangeKind("disable call-home", "disabled", "disabled")
def test_a_call_home_change_is_verified_by_its_own_state():
r, _ = change(call_home_change(False), ["enabled", "disabled"])
assert (r.outcome, r.verified, r.state_after) == ("sent", "ok", "disabled")
assert r.action == "disable call-home"
def test_a_call_home_read_back_that_did_not_change_is_a_mismatch():
r, _ = change(call_home_change(False), ["enabled"] * 4, verify=(3.0, 1.0))
assert (r.outcome, r.verified) == ("failed", "mismatch")
def test_the_snapshot_is_kept_in_the_result_and_the_audit():
r, audit = change(call_home_change(False), ["enabled", "disabled"], snapshot=lambda: {"after": "ab"})
assert r.detail["snapshot"] == {"after": "ab"}
((_, row),) = audit.rows.items()
assert row["states"]["snapshot"] == {"after": "ab"}
def test_a_failing_snapshot_does_not_change_the_outcome():
def broken():
raise RuntimeError("x")
r, _ = change(call_home_change(False), ["enabled", "disabled"], snapshot=broken)
assert (r.outcome, r.verified) == ("sent", "ok")
assert r.detail["snapshot"] == {"error": "x"}
def test_no_snapshot_leaves_the_result_as_before():
r, audit = change(monitoring_change(True), ["idle", "monitoring"])
assert "snapshot" not in r.detail
((_, row),) = audit.rows.items()
assert "snapshot" not in row["states"]