"""A MiniMate that boots when its port opens is waited for, not given up on. BE9441 on a serial cable (bench, 2026-10-25) twice sent its boot banner ("Operating System") ~3 s after the port opened and ignored the POLL sent before it; the request then failed at the full receive timeout (15 s, or 120 s on the download path). After a mid-download reboot it answered 8.8 s after its banner. startup() now stops waiting on the moment it sees the banner and polls again until the unit answers (up to 20 s); with no banner, nothing changes. """ 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.client as mc # noqa: E402 import minimateplus.protocol as mp # noqa: E402 from minimate_replay import SessionReplay, fast_clock, recorded_replies # noqa: E402 BANNER = b"Operating System" @pytest.fixture(autouse=True) def _fast(monkeypatch): fast_clock(monkeypatch) class Booting(SessionReplay): """Banner instead of an answer to the first POLL; then `deaf` more POLL probes go unanswered (still booting); after that, the recorded session as normal.""" def __init__(self, replies, deaf=2, banner=True, banner_on_open=False, noise_after=0): super().__init__(replies) self.deaf, self.banner, self.polls = deaf, banner, 0 self.noise_after = noise_after # non-frame bytes sent after the banner if banner_on_open: # the banner lands inside the drain window self._buf, self.banner = BANNER, False def write(self, data): data = bytes(data) self.log = getattr(self, "log", []) + [data] if data[:2] == b"\x41\x02" and data[5] == 0x5B and data[8] == 0x00: # the POLL probe self.polls += 1 if self.polls == 1: if self.banner: self._buf += BANNER + b"." * self.noise_after return if self.polls <= 1 + self.deaf: return super().write(data) def connect(transport): client = mc.MiniMateClient(transport=transport) client.open() return client, client.connect() def test_a_unit_booting_on_connect_is_waited_for(): t = Booting(recorded_replies(), deaf=2) _client, info = connect(t) assert info.serial == "BE9441" assert t.polls == 4 # the lost one, two while booting, the answered one probes = [i for i, w in enumerate(t.log) if w[5:6] == b"\x5b" and w[8] == 0x00] assert all(t.log[i - 1] == b"\x41\x03" for i in probes) # every POLL follows a SESSION_RESET assert {w[5] for w in t.log if w[:2] == b"\x41\x02"} <= {0x5B, 0x15, 0x01, 0x1A, 0x08} # reads only def test_a_unit_that_boots_but_never_answers_says_so(): t = Booting(recorded_replies(), deaf=10_000) with pytest.raises(mp.TimeoutError, match="boot"): connect(t) def test_a_silent_unit_without_a_banner_fails_as_before(): t = Booting(recorded_replies(), deaf=10_000, banner=False) with pytest.raises(mp.TimeoutError) as e: connect(t) assert "boot" not in str(e.value) assert t.polls == 1 # no re-polling without the banner def test_startup_on_a_reused_protocol_ignores_leftover_bytes(): replies = recorded_replies() p = mp.MiniMateProtocol(SessionReplay(replies)) a5 = next(raw for sub, raw in replies if sub == 0xA5) p._parser.feed(a5[:20]) # half a frame left over from an earlier exchange assert p.startup().sub == 0xA4 def test_a_banner_followed_by_more_text_in_the_same_read_is_still_seen(): t = Booting(recorded_replies(), deaf=1, noise_after=200) assert connect(t)[1].serial == "BE9441" def test_a_banner_caught_while_draining_is_acted_on(monkeypatch): t_now = [0.0] def fine(): # fine enough that the 200 ms drain really reads t_now[0] += 0.01 return t_now[0] monkeypatch.setattr(mp.time, "monotonic", fine) t = Booting(recorded_replies(), deaf=1, banner_on_open=True) assert connect(t)[1].serial == "BE9441" assert t._buf == b"" # the drain consumed the banner