From the whole-branch review of phase 5: - The preview's revision now covers the changes asked for, so a live change must be exactly what was previewed (any API caller, not only the page). It covers what the preview showed -- the settings block (and a Micromate's setup name) -- not the blocks a write only echoes, which are re-read at send and sent as read (a MiniMate's trigger block byte 5 differs by session, #46). - The units' limits hold on the block that would result: a range, rate or mode change alone can no longer leave a held trigger, alarm or record time over them. - Every MiniMate block read is logical, so push_config_raw no longer unescapes again (a job number of 4098, 10 02, was refused at Send); the preview runs the write's own block check, so a block it would refuse is shown as blocked. - A MiniMate's monitoring refusal says a stop makes it call in (spec). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
193 lines
8.2 KiB
Python
193 lines
8.2 KiB
Python
"""Changing a Micromate's settings by serial (phase 5): preview, one audited send,
|
|
read back. The push is micromate.writes.plan_setup_push (THOR's frames) under
|
|
the active setup's own name; the unit's schedule is read first, because a
|
|
scheduled start loads the setup it names and would undo a change to another."""
|
|
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 micromate.protocol as mm_proto # noqa: E402
|
|
from device_access_fakes import FakeMicromateClient, schedule_record # noqa: E402
|
|
from micromate.opcodes import ENABLEABLE_WRITES # noqa: E402
|
|
from micromate.setup_fields import field_values # noqa: E402
|
|
from sfm.device_access.errors import ( # noqa: E402
|
|
BadRequest, ChangeRefused, SettingsChanged, UnitStateUnknown,
|
|
)
|
|
from sfm.device_access.models import UnitAddress # noqa: E402
|
|
from sfm.device_access.s4 import s4_change_settings, s4_settings # noqa: E402
|
|
|
|
ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None)
|
|
PUSH_SUBS = [0xDA, 0x68, 0x73, 0x82, 0x83, 0x71, 0x72]
|
|
|
|
|
|
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, changes, *, revision=None, dry_run=False, audit=None):
|
|
return s4_change_settings(fake, "UM20147", changes=changes, revision=revision, dry_run=dry_run,
|
|
audit=audit if audit is not None else Audit(), address=ADDR,
|
|
caller="test", sleep=lambda s: None,
|
|
verify_timeout_s=2.0, verify_interval_s=1.0)
|
|
|
|
|
|
def live(fake, changes, audit=None):
|
|
rev = change(fake, changes, dry_run=True).plan["revision"]
|
|
return change(fake, changes, revision=rev, audit=audit)
|
|
|
|
|
|
def test_setup_push_subs_are_now_enableable():
|
|
assert set(PUSH_SUBS) <= ENABLEABLE_WRITES
|
|
|
|
|
|
def test_s4_settings_read_describes_the_editable_fields():
|
|
st = s4_settings(FakeMicromateClient(), "UM20147")
|
|
fields = {d["field"]: d for d in st.detail["editable"]}
|
|
assert fields["geo_trigger_ips"]["value"] == 0.5 and "geo_range" not in fields
|
|
|
|
|
|
def test_s4_dry_run_previews_old_to_new_and_sends_nothing():
|
|
fake = FakeMicromateClient()
|
|
r = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True)
|
|
assert r.outcome == "dry_run"
|
|
assert r.plan["fields"] == {"geo_trigger_ips": {"label": "Geophone trigger", "old": 0.5, "new": 0.3}}
|
|
assert len(r.plan["revision"]) == 16 and r.plan["blocked"] == []
|
|
assert [s["sub"] for s in r.plan["steps"]] == [f"0x{s:02X}" for s in PUSH_SUBS]
|
|
assert any("edit buffer" in w for w in r.plan["warnings"])
|
|
assert fake.executed == []
|
|
|
|
|
|
def test_s4_trigger_change_sends_the_push_and_verifies():
|
|
fake = FakeMicromateClient()
|
|
r = live(fake, {"geo_trigger_ips": 0.3})
|
|
assert (r.outcome, r.verified, r.state_after) == ("sent", "ok", "as asked")
|
|
assert [st.sub for st in fake.executed[0].steps] == PUSH_SUBS
|
|
assert field_values(fake.setup, 4)["geo_trigger_ips"] == 0.3
|
|
assert r.detail["snapshot"]["fields"]["geo_trigger_ips"] == {
|
|
"label": "Geophone trigger", "old": 0.5, "new": 0.3, "now": 0.3}
|
|
name_step = fake.executed[0].steps[0]
|
|
assert name_step.data.rstrip(b"\x00") == b"TEST1.mmb" # the active setup's own name
|
|
|
|
|
|
def test_s4_stale_revision_sends_nothing():
|
|
fake = FakeMicromateClient()
|
|
rev = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True).plan["revision"]
|
|
fake.setup = FakeMicromateClient(setup=None).setup[:40] + b"\x00\x07" + fake.setup[42:] # job number moved
|
|
audit = Audit()
|
|
with pytest.raises(SettingsChanged):
|
|
change(fake, {"geo_trigger_ips": 0.3}, revision=rev, audit=audit)
|
|
assert fake.executed == [] and audit.rows == {}
|
|
|
|
|
|
def test_s4_already_as_asked_is_skipped():
|
|
fake = FakeMicromateClient()
|
|
r = live(fake, {"geo_trigger_ips": 0.5})
|
|
assert r.outcome == "skipped" and fake.executed == []
|
|
|
|
|
|
@pytest.mark.parametrize("dry_run", [True, False])
|
|
def test_s4_monitoring_is_refused(dry_run):
|
|
fake = FakeMicromateClient(run_states=[0x02])
|
|
with pytest.raises(UnitStateUnknown) as e:
|
|
change(fake, {"geo_trigger_ips": 0.3}, revision="x", dry_run=dry_run)
|
|
assert "stop it first" in str(e.value) and "nothing sent" in str(e.value)
|
|
assert fake.executed == []
|
|
|
|
|
|
def test_s4_a_value_the_unit_does_not_allow_is_a_bad_request():
|
|
with pytest.raises(BadRequest):
|
|
change(FakeMicromateClient(), {"geo_trigger_ips": 12.0}, dry_run=True)
|
|
|
|
|
|
def test_s4_factory_setup_is_refused_live_and_shown_in_preview():
|
|
fake = FakeMicromateClient(active_name="factory.MMB")
|
|
r = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True)
|
|
assert any("factory setup" in b for b in r.plan["blocked"]) and r.plan["steps"] == []
|
|
with pytest.raises(ChangeRefused):
|
|
change(fake, {"geo_trigger_ips": 0.3}, revision=r.plan["revision"])
|
|
assert fake.executed == []
|
|
|
|
|
|
def test_s4_schedule_conflict_blocks():
|
|
fake = FakeMicromateClient(schedule_body=schedule_record(14, 0x02, "other.mmb") + bytes(264))
|
|
r = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True)
|
|
assert any("other.mmb" in b and "07:00" in b for b in r.plan["blocked"])
|
|
with pytest.raises(ChangeRefused) as e:
|
|
change(fake, {"geo_trigger_ips": 0.3}, revision=r.plan["revision"])
|
|
assert "nothing sent" in str(e.value) and fake.executed == []
|
|
|
|
|
|
def test_s4_schedule_on_the_active_setup_warns_only():
|
|
fake = FakeMicromateClient(schedule_body=schedule_record(14, 0x02, "test1.MMB")
|
|
+ schedule_record(37, 0x04) + bytes(4))
|
|
r = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True)
|
|
assert r.plan["blocked"] == [] and any("survives" in w and "07:00" in w for w in r.plan["warnings"])
|
|
assert live(fake, {"geo_trigger_ips": 0.3}).verified == "ok"
|
|
|
|
|
|
def test_an_unreadable_schedule_refuses():
|
|
fake = FakeMicromateClient(schedule_error=mm_proto.ProtocolError("no reply to 0x94"))
|
|
r = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True)
|
|
assert any("could not read this unit's schedule" in b for b in r.plan["blocked"])
|
|
with pytest.raises(ChangeRefused):
|
|
change(fake, {"geo_trigger_ips": 0.3}, revision=r.plan["revision"])
|
|
assert fake.executed == []
|
|
|
|
|
|
def test_no_schedule_records_is_no_conflict():
|
|
r = live(FakeMicromateClient(), {"geo_trigger_ips": 0.3})
|
|
assert r.verified == "ok"
|
|
|
|
|
|
def test_s4_a_lost_ack_records_what_the_unit_holds():
|
|
fake = FakeMicromateClient(execute_error=mm_proto.TimeoutError("lost"))
|
|
r = live(fake, {"geo_trigger_ips": 0.3})
|
|
assert (r.outcome, r.sent) == ("failed", True)
|
|
snap = r.detail["snapshot"]
|
|
assert snap["fields"]["geo_trigger_ips"]["now"] == 0.5 and snap["changed_offsets"] == []
|
|
|
|
|
|
def test_s4_unseen_block_size_is_blocked():
|
|
fake = FakeMicromateClient()
|
|
fake.prefs = fake.prefs + b"\x00"
|
|
r = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True)
|
|
assert any("has not seen a block like it" in b for b in r.plan["blocked"])
|
|
with pytest.raises(ChangeRefused):
|
|
change(fake, {"geo_trigger_ips": 0.3}, revision=r.plan["revision"])
|
|
assert fake.executed == []
|
|
|
|
|
|
# Final review #1: the revision covers the changes previewed, not only the unit.
|
|
def test_s4_a_revision_covers_the_changes_previewed():
|
|
fake = FakeMicromateClient()
|
|
rev = change(fake, {"job_number": 7}, dry_run=True).plan["revision"]
|
|
with pytest.raises(SettingsChanged):
|
|
change(fake, {"geo_trigger_ips": 0.3}, revision=rev)
|
|
assert fake.executed == []
|
|
|
|
|
|
# Ruling (final review, declined-to-judge): the blocks a push only echoes are
|
|
# re-read at send and sent as read, so they are not part of the revision.
|
|
def test_s4_an_echoed_block_moving_does_not_stale_the_preview():
|
|
fake = FakeMicromateClient()
|
|
rev = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True).plan["revision"]
|
|
from micromate.setup_block import with_crc
|
|
prefs = bytearray(fake.prefs)
|
|
prefs[31] ^= 0x01
|
|
fake.prefs = with_crc(bytes(prefs))
|
|
assert change(fake, {"geo_trigger_ips": 0.3}, revision=rev).verified == "ok"
|