feat(sfm): start/stop monitoring for Series 3 through the same safety layer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
78c3d568d5
commit
ce5d2cb0d6
5 files changed
+332
-106
No files matched your search
@@ -283,3 +283,134 @@ def test_a_malformed_serial_is_422_even_without_an_audit_sink():
|
||||
from sfm.device_access.errors import BadRequest
|
||||
with pytest.raises(BadRequest):
|
||||
access(FakeMicromateClient(), audit=None).start_monitoring(ADDR, "XX123")
|
||||
|
||||
|
||||
# ── Series 3 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
import minimateplus.protocol as mmp_proto # noqa: E402
|
||||
from device_access_fakes import FakeMiniMateClient # noqa: E402
|
||||
|
||||
TCP3 = UnitAddress(host="10.0.0.6", tcp_port=12345, port=None, baud=None)
|
||||
|
||||
|
||||
def access3(fake, audit="default"):
|
||||
seen = []
|
||||
|
||||
def factory(a, **kw):
|
||||
seen.append(kw)
|
||||
return fake
|
||||
da = DeviceAccess(s3_client_factory=factory, start_reaper=False,
|
||||
audit=Audit() if audit == "default" else audit,
|
||||
sleep=lambda s: None, verify_timeout_s=6.0, verify_interval_s=2.0)
|
||||
da.factory_calls = seen
|
||||
return da
|
||||
|
||||
|
||||
def test_series3_dry_run_returns_its_own_frame_and_sends_nothing():
|
||||
fake = FakeMiniMateClient(monitor_states=[False])
|
||||
r = access3(fake).start_monitoring(TCP3, "BE18193", dry_run=True)
|
||||
assert (r.series, r.outcome, r.state_before) == (3, "dry_run", "idle")
|
||||
assert r.plan["steps"][0]["sub"] == "0x96" and fake.writes == []
|
||||
assert r.plan["steps"][0]["expect_ack"] == "0x69" and r.plan["sends_nothing"] is True
|
||||
|
||||
|
||||
def test_series3_start_is_verified_by_monitor_status():
|
||||
fake = FakeMiniMateClient(monitor_states=[False, False, True])
|
||||
da = access3(fake)
|
||||
r = da.start_monitoring(TCP3, "BE18193")
|
||||
assert (r.outcome, r.verified, r.state_after) == ("sent", "ok", "monitoring")
|
||||
assert fake.writes == ["start_monitoring"]
|
||||
assert da._audit.rows[r.audit_id]["ack"] == {"subs": ["0x69"]}
|
||||
|
||||
|
||||
def test_series3_stop_acks_0x68():
|
||||
fake = FakeMiniMateClient(monitor_states=[True, False])
|
||||
da = access3(fake)
|
||||
r = da.stop_monitoring(TCP3, "BE18193")
|
||||
assert r.verified == "ok" and fake.writes == ["stop_monitoring"]
|
||||
assert da._audit.rows[r.audit_id]["ack"] == {"subs": ["0x68"]}
|
||||
|
||||
|
||||
def test_series3_wrong_unit_is_refused_before_writing():
|
||||
fake = FakeMiniMateClient(serial="BE11529")
|
||||
with pytest.raises(WrongUnit):
|
||||
access3(fake).stop_monitoring(TCP3, "BE18193")
|
||||
assert fake.writes == []
|
||||
|
||||
|
||||
def test_series3_a_lost_ack_is_never_retried_even_over_tcp():
|
||||
fake = FakeMiniMateClient(monitor_states=[False, True],
|
||||
write_error=mmp_proto.TimeoutError("no ack"))
|
||||
da = access3(fake)
|
||||
r = da.start_monitoring(TCP3, "BE18193")
|
||||
assert fake.writes == ["start_monitoring"] # one send, no cold-boot retry
|
||||
assert r.outcome == "failed" and r.state_after == "monitoring"
|
||||
|
||||
|
||||
def test_series3_already_in_state_is_skipped():
|
||||
fake = FakeMiniMateClient(monitor_states=[True])
|
||||
da = access3(fake)
|
||||
r = da.start_monitoring(TCP3, "BE18193")
|
||||
assert (r.sent, r.outcome) == (False, "skipped") and fake.writes == []
|
||||
assert da._audit.rows[r.audit_id]["outcome"] == "skipped"
|
||||
|
||||
|
||||
def test_series3_a_state_that_never_changes_is_a_mismatch():
|
||||
fake = FakeMiniMateClient(monitor_states=[False])
|
||||
da = access3(fake)
|
||||
r = da.start_monitoring(TCP3, "BE18193")
|
||||
assert (r.outcome, r.verified, r.state_after) == ("failed", "mismatch", "idle")
|
||||
assert da._audit.rows[r.audit_id]["verified"] == "mismatch"
|
||||
|
||||
|
||||
def test_series3_a_failing_read_back_is_audited_then_raised():
|
||||
fake = FakeMiniMateClient(monitor_states=[False])
|
||||
da = access3(fake)
|
||||
real = fake.get_monitor_status
|
||||
calls = []
|
||||
|
||||
def flaky():
|
||||
calls.append(1)
|
||||
if len(calls) > 1:
|
||||
raise mmp_proto.TimeoutError("quiet")
|
||||
return real()
|
||||
fake.get_monitor_status = flaky
|
||||
with pytest.raises(NoAnswer):
|
||||
da.start_monitoring(TCP3, "BE18193")
|
||||
row = next(iter(da._audit.rows.values()))
|
||||
assert (row["outcome"], row["verified"]) == ("sent", "not_checked")
|
||||
assert "reading the state back failed" in row["error"]
|
||||
assert fake.writes == ["start_monitoring"]
|
||||
|
||||
|
||||
def test_series3_audit_begin_failing_sends_nothing():
|
||||
class Broken(Audit):
|
||||
def begin(self, **f):
|
||||
raise RuntimeError("disk full")
|
||||
fake = FakeMiniMateClient(monitor_states=[False])
|
||||
with pytest.raises(AuditUnavailable):
|
||||
access3(fake, audit=Broken()).start_monitoring(TCP3, "BE18193")
|
||||
assert fake.writes == []
|
||||
|
||||
|
||||
def test_series3_lost_ack_with_unreadable_follow_up_raises_and_is_not_retried():
|
||||
fake = FakeMiniMateClient(monitor_states=[False], write_error=mmp_proto.TimeoutError("no ack"))
|
||||
da = access3(fake)
|
||||
real = fake.get_monitor_status
|
||||
n = []
|
||||
|
||||
def second_fails():
|
||||
n.append(1)
|
||||
if len(n) > 1:
|
||||
raise mmp_proto.TimeoutError("still quiet")
|
||||
return real()
|
||||
fake.get_monitor_status = second_fails
|
||||
with pytest.raises(NoAnswer):
|
||||
da.start_monitoring(TCP3, "BE18193")
|
||||
assert fake.writes == ["start_monitoring"]
|
||||
assert len(da.factory_calls) == 1 # no second TCP attempt
|
||||
row = next(iter(da._audit.rows.values()))
|
||||
assert row["outcome"] == "failed"
|
||||
|
||||
# The other Series-3 operations still retry once over TCP on a cold unit:
|
||||
# tests/test_device_access_s3.py::test_a_cold_unit_over_tcp_gets_one_retry.
|
||||
Reference in new issue
Block a user