#!/usr/bin/env python3 """Unit tests for the RTL UART delayed-startup tap (no Icarus).""" from __future__ import annotations import time from run_hthd_pty_rtl import UartTap, _clear_startup_complete, _crc16_ccitt_false def _motor(startup: bool) -> bytes: payload = bytearray(31) if startup: payload[0] = 0x47 else: payload[0] = 0x07 body = bytes([0x83, 31]) + bytes(payload) crc = _crc16_ccitt_false(body) return bytes([0xA5, 0x5A]) + body + bytes((crc & 0xFF, crc >> 8)) def test_hold_before_mark_clears_startup(): tap = UartTap(hold_s=10.0, max_hold_s=20.0) out = tap.rewrite_rtl_to_pty(_motor(True)) assert out[4] & 0x40 == 0, hex(out[4]) assert tap._cleared_motors == 1 def test_hold_until_first_motor_after_mark(): tap = UartTap(hold_s=0.05, max_hold_s=1.0) tap.mark_hthd_start() time.sleep(0.08) out = tap.rewrite_rtl_to_pty(_motor(True)) assert out[4] & 0x40 == 0, hex(out[4]) out2 = tap.rewrite_rtl_to_pty(_motor(True)) assert out2[4] & 0x40, hex(out2[4]) def test_clear_startup_preserves_length(): raw = _motor(True) cleared = _clear_startup_complete(raw) assert len(cleared) == len(raw) assert cleared[2] == 0x83 assert cleared[4] & 0x40 == 0 if __name__ == "__main__": test_hold_before_mark_clears_startup() test_hold_until_first_motor_after_mark() test_clear_startup_preserves_length() print("PASS")