Files
seismo-relay/tests/test_micromate_schedule.py
T
serversdownandClaude Opus 5.5 d0335dbbbd feat(micromate): read the schedule file (read-only)
MicromateProtocol.read_file: 0x94 + path, then 0x48 pages to an
all-zero reply -- THOR's frames byte for byte (UM12947, 2026-09-24),
including params[3] = 1 on 0x94 and params[7] = 1 on the first 0x48.
micromate.schedule parses the 260-byte records (two- and five-entry
schedules from THOR's captures) and names the scheduled starts that
would load a setup other than the active one, ignoring case.  The
client gains the raw (unstripped) active-setup name and the prefs /
trigger / setup content reads a setup push sends back.

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

143 lines
5.4 KiB
Python

"""Reading a Micromate's schedule file, read-only (phase 5).
A Micromate's scheduled start loads the setup it names, so a settings change to
another setup would be undone at the next start: SFM reads the schedule to refuse
that case. Ground truth is THOR's 2026-09-24/25 captures of UM12947
(bridges/captures/9-24-26 - micromate2): its read of `schedule.dat` (0x94, then
0x48 pages) and its writes of a two- and a five-entry schedule (0x8E bodies).
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from micromate.client import MicromateClient # noqa: E402
from micromate.framing import MicromateFrameParser, unstuff # noqa: E402
from micromate.protocol import MicromateProtocol # noqa: E402
from micromate.schedule import ( # noqa: E402
SCHEDULE_PATH, ScheduleEntry, parse_schedule, schedule_conflicts,
)
from test_micromate_protocol import ScriptedTransport, frame # noqa: E402
from test_minimate_write_frames import _frames # noqa: E402
D = Path(__file__).resolve().parent.parent / "bridges" / "captures" / "9-24-26 - micromate2"
READ_S3 = D / "raw_s3_20260924_191835_read_scheduler.bin"
READ_BW = D / "raw_bw_20260924_191835_read_scheduler.bin"
ACTIONCHECK_BW = D / "raw_bw_20260925_005122_actioncheck.bin"
pytestmark = pytest.mark.skipif(not D.is_dir(), reason="bridges/captures is not on this machine")
def _payloads(path: Path) -> list:
return [unstuff(f[2:-1])[:-1] for f in _frames(path.read_bytes())]
def _written_schedule(path: Path) -> bytes:
"""The schedule body THOR wrote: the 0x8E chunks after the 0x8D that opens
schedule.dat, joined (a long body goes in 1,024-byte pieces)."""
body, current = b"", None
for p in _payloads(path):
if p[2] == 0x8D:
current = p[16:] == SCHEDULE_PATH.encode("ascii")
elif p[2] == 0x8E and current:
body += p[16:]
return body
def _read_replies() -> list:
"""The unit's replies to THOR's schedule read: 0x6B, then three 0xB7 pages."""
fr = MicromateFrameParser().feed(READ_S3.read_bytes())
i = next(k for k, f in enumerate(fr) if f.sub == 0x6B)
return fr[i:i + 4]
def test_the_read_capture_parses():
body = bytes(_read_replies()[2].data)[11:]
assert len(body) == 524
e = parse_schedule(body)
assert [(x.time, x.actions) for x in e] == [("07:30", ("start_with_setup",)), ("19:30", ("call_home",))]
assert len(e[0].setup) == 40 and e[1].setup == ""
assert e[0].day == 3 and e[0].action_bits == 2
def test_the_five_entry_schedule_parses():
e = parse_schedule(_written_schedule(ACTIONCHECK_BW))
assert [(x.time, x.actions, x.setup) for x in e] == [
("07:30", ("start_with_setup",), "TEST1.mmb"),
("08:00", ("stop",), ""),
("08:30", ("self_check",), ""),
("09:00", ("start_with_setup",), "test2.mmb"),
("19:30", ("call_home",), ""),
]
def test_conflicts_are_case_insensitive():
e = parse_schedule(_written_schedule(ACTIONCHECK_BW))
assert [x.setup for x in schedule_conflicts(e, "test1.MMB")] == ["test2.mmb"]
assert [x.setup for x in schedule_conflicts(e, " TEST2.mmb ")] == ["TEST1.mmb"]
def test_no_schedule_records_is_no_conflict():
assert parse_schedule(bytes(524)) == []
assert schedule_conflicts(parse_schedule(bytes(524)), "x.mmb") == []
def test_an_unknown_action_bit_is_named_not_dropped():
rec = bytearray(260)
rec[4], rec[5], rec[6] = 14, 3, 0x01 # 07:00, the setup-less start THOR never sends?
(e,) = parse_schedule(bytes(rec))
assert e.actions == ("unknown(0x01)",) and e.setup == ""
assert schedule_conflicts([e], "a.mmb") == [] # no setup named: nothing to undo the change
def test_read_file_sends_thors_frames_and_stops_at_zeros():
replies = [frame(f.sub, bytes(f.data), flags=f.flags, page=f.page_key) for f in _read_replies()]
t = ScriptedTransport(replies)
body = MicromateProtocol(t, recv_timeout=0.5).read_file(SCHEDULE_PATH)
thor = [f for f in _frames(READ_BW.read_bytes()) if unstuff(f[2:-1])[2] in (0x94, 0x48)]
assert t.written == thor[:4] # 0x94 + path, 0x48 (first page), 0x48, 0x48
assert body == bytes(_read_replies()[2].data)[11:]
class _Proto:
def __init__(self, data: bytes):
self.data = data
def read_active_setup_name(self):
return self.data
def read_event_index(self):
return self.data
def read_trigger_config(self):
return self.data
def read_compliance_config(self):
return self.data
def _client(data: bytes) -> MicromateClient:
c = MicromateClient.__new__(MicromateClient)
c._proto = _Proto(data)
return c
def test_get_active_setup_raw_keeps_spaces():
c = _client(bytes(11) + b" LOC 4 .mmb \x00junk")
assert c.get_active_setup_raw() == " LOC 4 .mmb "
assert c.get_active_setup() == "LOC 4 .mmb"
def test_block_content_getters_strip_the_prefix():
c = _client(bytes(range(11)) + b"content")
assert c.get_prefs_content() == c.get_trigger_content() == c.get_setup_content() == b"content"
def test_schedule_entry_is_a_value():
assert ScheduleEntry(0, "07:00", 3, 2, ("start_with_setup",), "a") == \
ScheduleEntry(0, "07:00", 3, 2, ("start_with_setup",), "a")