fix(micromate): a write plan copies its steps -- nothing can be added after validation
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
49f8f49ac1
commit
cbda4b80df
2 files changed
+43
-9
No files matched your search
@@ -56,6 +56,8 @@ class WritePlan:
|
||||
steps: Tuple[WriteStep, ...]
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
# Copy: a caller's list must not be able to add a step after validation.
|
||||
object.__setattr__(self, "steps", tuple(self.steps))
|
||||
for s in self.steps:
|
||||
if s.sub in CROSS_SERIES_COLLISIONS:
|
||||
raise OpcodeRefused(
|
||||
@@ -117,6 +119,11 @@ def plan_setup_push(prefs: bytes, trigger: bytes, setup: bytes, name: str) -> Wr
|
||||
No field editing here (phase 5). Each block goes back with its length echo
|
||||
[0:3] zeroed; the unit recomputes the CRCs, so 0x68 and 0x71 drop the CRC
|
||||
slot and 0x82 sends it zeroed -- exactly THOR's frames.
|
||||
|
||||
The name goes out exactly as given. `MicromateClient.get_active_setup()`
|
||||
strips whitespace, so a setup name with leading or trailing spaces would
|
||||
not round-trip through it; phase 5 decides whether to take the unstripped
|
||||
name from the 0x41 read.
|
||||
"""
|
||||
_checked(prefs, _PREFS_LEN, "preferences (0x08)")
|
||||
_checked(trigger, _TRIGGER_LEN, "trigger (0x2E)")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Write plans: built from the unit's own reads, equal to THOR's frames byte for byte."""
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
@@ -80,15 +81,24 @@ def test_call_home_enable_then_disable_equal_thors_frames():
|
||||
assert off.frames() == [w2, confirms[1]]
|
||||
|
||||
|
||||
def test_call_home_flips_exactly_one_bit_and_echoes_everything_else():
|
||||
r1 = next(_content(e) for e in _session("call-home-enable-disable") if e["sub"] == 0x2C)
|
||||
for enabled in (True, False):
|
||||
data = plan_call_home_enabled(r1, enabled).steps[0].data
|
||||
diff = [i for i in range(len(r1)) if data[i] != r1[i]]
|
||||
assert set(diff) <= {5}
|
||||
assert data[5] & 0x01 == (1 if enabled else 0)
|
||||
assert data[5] & 0xFE == r1[5] & 0xFE
|
||||
assert data[118:120] == r1[118:120]
|
||||
def _call_home_reads():
|
||||
return [_content(e) for e in _session("call-home-enable-disable") if e["sub"] == 0x2C]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("read_idx", [0, 1])
|
||||
@pytest.mark.parametrize("enabled", [True, False])
|
||||
def test_call_home_flips_exactly_one_bit_and_echoes_everything_else(read_idx, enabled):
|
||||
reads = _call_home_reads()
|
||||
assert {reads[0][5], reads[1][5]} == {0x04, 0x05}
|
||||
r = reads[read_idx]
|
||||
data = plan_call_home_enabled(r, enabled).steps[0].data
|
||||
diff = [i for i in range(len(r)) if data[i] != r[i]]
|
||||
assert set(diff) <= {5}
|
||||
assert data[5] & 0x01 == (1 if enabled else 0)
|
||||
assert data[5] & 0xFE == r[5] & 0xFE
|
||||
if (r[5] & 1) == int(enabled):
|
||||
assert data[5] == r[5]
|
||||
assert data[118:120] == r[118:120]
|
||||
|
||||
|
||||
def test_call_home_refuses_an_unobserved_size():
|
||||
@@ -160,3 +170,20 @@ def test_describe_is_the_dry_run_shape():
|
||||
assert step == {"sub": "0x97", "what": "stop monitoring", "offset": "0xFFFF",
|
||||
"data_len": 0, "expect_ack": "0x68",
|
||||
"frame": "41021010009700ffff00000000000000000000a503"}
|
||||
|
||||
|
||||
def test_a_plan_copies_its_steps_so_a_later_mutation_cannot_change_it():
|
||||
lst = [WriteStep(0x96, 0xFFFF)]
|
||||
plan = WritePlan("start", lst)
|
||||
before = plan.frames()
|
||||
lst.append(WriteStep(0xA2, 0xFFFF))
|
||||
lst[0] = WriteStep(0x97, 0xFFFF)
|
||||
assert isinstance(plan.steps, tuple) and len(plan.steps) == 1
|
||||
assert plan.steps[0].sub == 0x96
|
||||
assert plan.frames() == before
|
||||
|
||||
|
||||
def test_steps_cannot_be_reassigned():
|
||||
plan = plan_stop_monitoring()
|
||||
with pytest.raises(dataclasses.FrozenInstanceError):
|
||||
plan.steps = ()
|
||||
Reference in new issue
Block a user