"""Tests for UART v2 protocol serialization in switch_pico_uart.""" import struct import pytest from switch_pico_bridge.switch_pico_uart import ( SwitchReport, IMUSample, SwitchDpad, UART_HEADER, UART_PROTOCOL_VERSION, ACCEL_LSB_PER_G, GYRO_LSB_PER_RAD_S, MS2_PER_G, compute_checksum, ) def test_v2_frame_with_imu_samples(): """V2 frame with 3 IMU samples should be 48 bytes with correct layout.""" r = SwitchReport( buttons=0, imu_samples=[ IMUSample(100, -200, 4096, 50, -50, 0), IMUSample(101, -201, 4097, 51, -51, 1), IMUSample(102, -202, 4098, 52, -52, 2), ], ) data = r.to_bytes() assert len(data) == 48, f"Expected 48 bytes, got {len(data)}" assert data[0] == UART_HEADER # 0xAA assert data[1] == UART_PROTOCOL_VERSION # 0x02 assert data[2] == 44 # payload_len assert data[10] == 3 # imu_count # Verify checksum assert data[-1] == compute_checksum(data[:-1]) # Verify first sample accel_x (int16 LE at byte 11) ax0 = struct.unpack_from("3 IMU samples should cap at 3.""" samples = [IMUSample(i, 0, 0, 0, 0, 0) for i in range(5)] r = SwitchReport(imu_samples=samples) data = r.to_bytes() assert len(data) == 48 # 3 samples, not 5 assert data[10] == 3 assert data[2] == 44 # payload_len for 3 samples