Files
seismo-relay/tests/test_micromate_opcodes.py
T
serversdownandClaude Opus 5.5 c082f72d62 feat(s4): change a Micromate's settings by serial, audited, schedule-checked
s4_change_settings runs the shared change core with a read-back verdict
for its state (as asked / unchanged / differs).  It reads the active
setup's name (unstripped), the preferences, trigger and setup blocks and
the schedule; patches the setup through micromate.setup_fields; sends
THOR's setup push (0xDA 68 73 82 83 71 72, now enableable) once; reads
the setup back.  Refused before sending: a monitoring unit, a value the
unit does not allow (422), a stale preview (SettingsChanged, 409), and --
shown as 'blocked' in the preview -- the factory setup, a schedule that
loads another setup, an unreadable schedule, or a block SFM has not seen
(ChangeRefused, 409).  s4_settings carries the editable fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-03 21:43:03 -05:00

139 lines
4.8 KiB
Python

"""The send-side gate: no write opcode reaches the wire unless allowed, and 0xA2 never."""
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 micromate.framing import build_request # noqa: E402
from micromate.opcodes import ( # noqa: E402
CROSS_SERIES_COLLISIONS, READ_SUBS, WRITE_SUBS, OpcodeRefused, check_transmit,
)
from micromate.writes import plan_start_monitoring # noqa: E402
from test_micromate_protocol import frame, proto # noqa: E402
# THOR's captured end-call-home frame. Same SUB as Series III's CONFIRM ERASE, not the
# same bytes (S3 carries offset 0x0000 and token 0xFE; this carries offset 0xFFFF).
A2_WIRE = bytes.fromhex("4102101000a200ffff00000000000000000000b003")
def test_no_read_command_is_classed_as_a_write():
reads = {v for k, v in vars(mm_proto).items() if k.startswith("SUB_")}
assert reads and not (reads & set(WRITE_SUBS))
def test_0xa2_is_a_collision_and_a_write():
assert 0xA2 in CROSS_SERIES_COLLISIONS and 0xA2 in WRITE_SUBS
def test_a_write_is_refused_unless_allowed():
with pytest.raises(OpcodeRefused, match="0x96"):
check_transmit(0x96)
check_transmit(0x96, frozenset({0x96}))
check_transmit(0x5B) # a read needs no permission
def test_0xa2_is_refused_even_when_allowed():
with pytest.raises(OpcodeRefused, match="ERASE"):
check_transmit(0xA2, frozenset({0xA2}))
def test_the_protocol_refuses_a_write_and_writes_nothing():
p, t = proto([])
with pytest.raises(OpcodeRefused):
p._exchange(0x96, offset=0xFFFF)
with pytest.raises(OpcodeRefused):
p._send(build_request(0x97, 0xFFFF)) # a direct _send is gated too
assert t.written == []
def test_the_protocol_refuses_the_captured_0xa2_frame_byte_for_byte():
p, t = proto([])
with pytest.raises(OpcodeRefused):
p._send(A2_WIRE, permit=frozenset({0xA2}))
assert t.written == []
def test_a_write_goes_out_only_through_execute_on_a_scripted_transport():
p, t = proto([frame(0xA4, bytes(48)), frame(0x69, bytes(11))])
p.poll()
t.written.clear()
p.execute(plan_start_monitoring())
assert t.written == [build_request(0x96, 0xFFFF)]
def test_reads_are_unaffected_by_the_gate():
p, t = proto([frame(0xA4, bytes(11))])
p.poll()
assert len(t.written) == 1
def test_a_frame_that_is_not_a_request_is_refused_not_sent():
p, t = proto([])
with pytest.raises(ValueError):
p._send(b"\x41\x03")
assert t.written == []
def test_every_protocol_read_sub_is_in_the_read_table():
consts = {k: v for k, v in vars(mm_proto).items() if k.startswith("SUB_")}
assert len(consts) == 21 # + 0x48 and 0x94, the file read (phase 5)
assert set(consts.values()) <= set(READ_SUBS)
assert {0x94, 0x48} <= set(READ_SUBS)
def test_read_and_write_tables_are_disjoint():
assert not (set(READ_SUBS) & set(WRITE_SUBS))
@pytest.mark.parametrize("sub", [0xA3, 0x69, 0x74, 0x98, 0xEE])
def test_an_unknown_opcode_is_refused_and_nothing_is_written(sub):
p, t = proto([])
with pytest.raises(OpcodeRefused, match="unknown opcode"):
p._send(build_request(sub, 0))
assert t.written == []
@pytest.mark.parametrize("sub", [0xEE, 0xA8, 0xAA]) # delete and its confirm stay above the ceiling
def test_a_permit_beyond_the_ceiling_is_refused_at_the_wire(sub):
p, t = proto([])
with pytest.raises(OpcodeRefused):
p._send(build_request(sub, 0), permit=frozenset({sub}))
assert t.written == []
def test_allowed_writes_is_retired():
with pytest.raises(TypeError):
proto([], allowed_writes=frozenset({0x96}))
def test_the_tables_are_read_only():
for table in (WRITE_SUBS, READ_SUBS, CROSS_SERIES_COLLISIONS):
with pytest.raises((TypeError, AttributeError)):
table.pop(next(iter(table)))
with pytest.raises(TypeError):
table[0x00] = "x"
@pytest.mark.parametrize("tail", [build_request(0x96, 0xFFFF), A2_WIRE])
def test_a_read_followed_by_a_write_in_one_send_is_refused(tail):
p, t = proto([])
with pytest.raises(ValueError):
p._send(build_request(0x5B, 0x0030) + tail, permit=frozenset({0x96}))
assert t.written == []
def test_a_bytearray_frame_is_checked_and_written_as_the_same_bytes():
p, t = proto([])
p._send(bytearray(build_request(0x5B, 0x0030)))
assert t.written == [build_request(0x5B, 0x0030)]
def test_an_uncatalogued_sub_is_refused_even_when_explicitly_allowed():
with pytest.raises(OpcodeRefused):
check_transmit(0xEE, frozenset({0xEE}))