import unittest from tools.prod2u_simple_protocol import ( FrameType, ProtocolError, build_get_temperature, build_set_all, build_temperature_data, crc16_ccitt_false, decode_frame, decode_temperature_data, encode_frame, ) 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_set_all_all_zero_golden_vector(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_mixed_golden_vector(self): raw = bytes.fromhex("A55A010B0A141E280019326401020087D5") frame = decode_frame(raw) self.assertEqual(frame.frame_type, FrameType.SET_ALL) self.assertEqual(frame.payload, bytes((10, 20, 30, 40, 0, 25, 50, 100, 1, 2, 0))) self.assertEqual( build_set_all((10, 20, 30, 40), (0, 25, 50, 100), 1, 2, 0).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, 1, 2, 0)) self.assertEqual(crc16_ccitt_false(body), 0xD587) 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_boundaries_and_rejects_out_of_range_values(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, 2, 2, 2)), 17) for values in ([101] + [0] * 3, [-1] + [0] * 3): with self.subTest(values=values[0]): with self.assertRaises(ValueError): build_set_all(values, [0] * 4, 0, 0, 0) for values in ([101] + [0] * 3, [-1] + [0] * 3): with self.subTest(values=values[0]): with self.assertRaises(ValueError): build_set_all([0] * 4, values, 0, 0, 0) def test_door_commands_are_exactly_zero_one_or_two(self): for command in (0, 1, 2): with self.subTest(command=command): frame = decode_frame(build_set_all([0] * 4, [0] * 4, command, command, command)) self.assertEqual(frame.payload[-3:], bytes((command, command, command))) for command in (-1, 3): with self.subTest(command=command): with self.assertRaises(ValueError): build_set_all([0] * 4, [0] * 4, command, 0, 0) def test_temperature_response_is_exactly_33_bytes(self): raw = build_temperature_data([0xFF] * 27) self.assertEqual(len(raw), 33) self.assertEqual(raw.hex(" ").upper(), "A5 5A 82 1B " + "FF " * 26 + "FF A7 45") self.assertEqual(decode_temperature_data(decode_frame(raw)), (0xFF,) * 27) 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_and_invalid_direction_are_rejected(self): with self.assertRaises(ProtocolError): decode_frame(encode_frame(0x7F, b"")) with self.assertRaises(ValueError): build_set_all([0] * 4, [0] * 4, 3, 0, 0) if __name__ == "__main__": unittest.main()