"""Waking a MiniMate that is busy right after a start (BE9441, 2026-10-26). A start makes the unit run its sensor check (~30 s, Brian; ~40 s on BE11529, 2026-04-08) before 'Recording...'. Twice tonight a read-back sent 2 s after the start's ack -- a bare 0x1C, then SESSION_RESET + POLL -- got no answer, and with nothing re-sent the read waited out 120 s. wake() re-sends SESSION_RESET + POLL until the unit answers or the deadline passes. """ from __future__ import annotations import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import minimateplus.protocol as mp # noqa: E402 from minimate_replay import fast_clock # noqa: E402 from minimateplus.framing import POLL_DATA, POLL_PROBE, SESSION_RESET # noqa: E402 # BE9441's own POLL replies, recorded 2026-10-26 (probe ack, then the data reply) PROBE_ACK = bytes.fromhex("1002001010a400000030000000000000000000000000d403") def _poll_data_reply() -> bytes: from pathlib import Path from minimateplus.framing import S3FrameParser raw = (Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-histogram" / "unit_replies.bin").read_bytes() p, start = S3FrameParser(), 0 for i in range(len(raw)): for f in p.feed(raw[i:i + 1]): w = raw[start:i + 1] if f.sub == 0xA4 and len(f.data) > 20: return w[w.index(b"\x10\x02"):] start = i + 1 raise AssertionError("no POLL data reply in the fixture") class Busy: """Ignores the first `deaf` POLL probes (a sensor check), then answers.""" def __init__(self, deaf: int): self.deaf, self.sent, self._buf = deaf, [], b"" self.data_reply = _poll_data_reply() def write(self, data): data = bytes(data) self.sent.append(data) if data == POLL_PROBE: if self.deaf: self.deaf -= 1 else: self._buf += PROBE_ACK elif data == POLL_DATA: self._buf += self.data_reply def read(self, n): out, self._buf = self._buf[:n], self._buf[n:] return out @pytest.fixture(autouse=True) def _fast(monkeypatch): fast_clock(monkeypatch) def test_the_probe_ack_is_real(): from minimateplus.framing import S3FrameParser f = S3FrameParser().feed(PROBE_ACK) assert len(f) == 1 and f[0].sub == 0xA4 def test_a_busy_unit_is_asked_again_until_it_answers(): t = Busy(deaf=4) mp.MiniMateProtocol(t).wake(attempt_s=5.0, deadline_s=60.0) probes = [i for i, d in enumerate(t.sent) if d == POLL_PROBE] assert len(probes) == 5 # 4 ignored, the 5th answered assert all(t.sent[i - 1] == SESSION_RESET for i in probes) assert t.sent[-2:] == [SESSION_RESET, POLL_DATA] def test_a_unit_that_never_answers_ends_in_a_named_timeout(): t = Busy(deaf=10 ** 6) with pytest.raises(mp.TimeoutError, match="did not answer a POLL"): mp.MiniMateProtocol(t).wake(attempt_s=5.0, deadline_s=30.0) assert 5 <= t.sent.count(POLL_PROBE) <= 7 # every ~5 s for 30 s