import unittest from tools.prod2u_simple_protocol import ( FrameType, ProtocolError, build_get_motor_status, build_get_temperature, build_set_all, build_temperature_data, crc16_ccitt_false, decode_frame, decode_motor_status_data, decode_temperature_data, encode_frame, mid_target, ) class ProtocolTests(unittest.TestCase): def test_get_temperature_matches_frozen_hex_vector(self): self.assertEqual(build_get_temperature().hex(" ").upper(), "A5 5A 02 00 6D 7B") def test_get_motor_status_golden(self): self.assertEqual(build_get_motor_status().hex().upper(), "A55A03005C48") def test_set_all_all_zero_full_open_golden(self): raw = bytes.fromhex("A55A010B00000000000000000000005873") frame = decode_frame(raw) self.assertEqual(frame.frame_type, FrameType.SET_ALL) self.assertEqual(len(frame.payload), 11) self.assertEqual(build_set_all([0] * 4, [0] * 4, 0, 0, 0).hex().upper(), raw.hex().upper()) def test_set_all_doors_50_percent_golden(self): raw = bytes.fromhex("A55A010B0A141E2800193264323232F65A") frame = decode_frame(raw) self.assertEqual(frame.payload, bytes((10, 20, 30, 40, 0, 25, 50, 100, 50, 50, 50))) self.assertEqual( build_set_all((10, 20, 30, 40), (0, 25, 50, 100), 50, 50, 50).hex().upper(), raw.hex().upper(), ) def test_crc_reference_matches_independent_implementation(self): body = bytes((0x01, 0x0B, 10, 20, 30, 40, 0, 25, 50, 100, 50, 50, 50)) self.assertEqual(crc16_ccitt_false(body), 0x5AF6) def test_legacy_14_byte_length_is_rejected_on_decode(self): legacy = bytes.fromhex("A55A010E00010203040506070A141E280102B2C5") with self.assertRaises(ProtocolError): decode_frame(legacy) def test_set_all_accepts_duty_and_percent_0_100(self): self.assertEqual(len(build_set_all([0] * 4, [0] * 4, 0, 0, 0),), 17) self.assertEqual(len(build_set_all([100] * 4, [100] * 4, 100, 100, 100)), 17) for bad in (101, -1): with self.assertRaises(ValueError): build_set_all([bad] + [0] * 3, [0] * 4, 0, 0, 0) with self.assertRaises(ValueError): build_set_all([0] * 4, [0] * 4, bad, 0, 0) def test_door_percent_range_not_jog_only(self): for percent in (0, 1, 50, 99, 100): frame = decode_frame(build_set_all([0] * 4, [0] * 4, percent, percent, percent)) self.assertEqual(frame.payload[-3:], bytes((percent, percent, percent))) for percent in (-1, 101, 255): with self.assertRaises(ValueError): build_set_all([0] * 4, [0] * 4, percent, 0, 0) def test_temperature_response_is_exactly_33_bytes(self): raw = build_temperature_data([0xFF] * 27) self.assertEqual(len(raw), 33) self.assertEqual(decode_temperature_data(decode_frame(raw)), (0xFF,) * 27) def test_motor_status_decode_31_bytes(self): # Global 0x47 = cal XYZ + startup; axes idle/openish zeros except travel 1000 payload = bytearray(31) payload[0] = 0x47 for base in (1, 11, 21): payload[base] = 2 # IDLE payload[base + 7] = 0xE8 payload[base + 8] = 0x03 # travel 1000 frame = encode_frame(FrameType.MOTOR_STATUS_DATA, bytes(payload)) self.assertEqual(len(frame), 37) status = decode_motor_status_data(decode_frame(frame)) self.assertTrue(status.startup_complete) self.assertEqual(int(status.global_status) & 0x07, 0b111) self.assertEqual(status.x.travel_steps, 1000) def test_mid_target_half_up(self): self.assertEqual(mid_target(100, 50), 50) self.assertEqual(mid_target(3, 50), 2) self.assertEqual(mid_target(2, 1), 1) def test_crc_and_length_errors_are_rejected(self): raw = bytearray(encode_frame(FrameType.GET_TEMPERATURE, b"")) raw[-1] ^= 1 with self.assertRaises(ProtocolError): decode_frame(bytes(raw)) with self.assertRaises(ProtocolError): decode_frame(bytes(raw[:-1])) def test_unknown_type_rejected(self): with self.assertRaises(ProtocolError): decode_frame(encode_frame(0x7F, b"")) if __name__ == "__main__": unittest.main()