import pytest from bus_timing import (status_packet_size, sync_cycle, sync_read_packet, sync_write_packet, wire_time_us) def test_sync_read_matches_reference_bytes() -> None: # Test vector from rustypot's protocol v1 unit tests. assert sync_read_packet([11, 12], 30, 2) == bytes( [0xFF, 0xFF, 0xFE, 0x06, 0x82, 0x1E, 0x02, 0x0B, 0x0C, 0x42]) def test_sync_write_matches_reference_bytes() -> None: assert sync_write_packet([11, 12], 30, [[0x00, 0x00], [0x0A, 0x14]]) == bytes( [0xFF, 0xFF, 0xFE, 0x0A, 0x83, 0x1E, 0x02, 0x0B, 0x00, 0x00, 0x0C, 0x0A, 0x14, 0x1F]) def test_six_servo_cycle() -> None: c = sync_cycle(6, data_len=2) assert c["write_bytes"] == 8 + 6 * 3 == 26 assert c["read_request_bytes"] == 8 + 6 == 14 assert c["read_reply_bytes"] == 6 * status_packet_size(2) == 48 assert c["total_us"] == pytest.approx(880.0) def test_wire_time_and_return_delay() -> None: assert wire_time_us(100) == pytest.approx(1000.0) assert wire_time_us(100, baud=500_000) == pytest.approx(2000.0) assert sync_cycle(6, return_delay_us=500.0)["total_us"] == pytest.approx(880.0 + 3000.0) def test_mismatched_data_rejected() -> None: with pytest.raises(ValueError): sync_write_packet([1, 2], 42, [[0, 0], [0]])