| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #!/usr/bin/env python3
- """Wire-contract tests for the on-demand CT7428 temperature response."""
- from __future__ import annotations
- import pytest
- from tools import prod2u_p4_protocol as protocol
- GOLDEN_REQUEST = bytes.fromhex(
- "A55A020405000000000000000004280000009E0C"
- )
- GOLDEN_RESPONSE = bytes.fromhex(
- "A55A0204101B"
- "000102030405060708090A0B0C0D0E0F101112131415161718191A"
- "1FC9"
- )
- ALL_FF_RESPONSE = bytes.fromhex(
- "A55A0204101B"
- "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
- "1BC4"
- )
- def test_get_temperatures_request_matches_the_frozen_wire_frame() -> None:
- assert protocol.CommandId.GET_TEMPERATURES == 0x28
- assert protocol.build_get_temperatures() == GOLDEN_REQUEST
- decoded = protocol.decode_frame(GOLDEN_REQUEST)
- assert decoded.frame_type == protocol.FrameType.HOST_CMD
- assert decoded.payload == bytes.fromhex("28000000")
- def test_temperature_response_matches_the_frozen_35_byte_frame() -> None:
- values = bytes(range(27))
- response = protocol.build_temperature_response(values)
- assert response == GOLDEN_RESPONSE
- assert len(response) == 35
- assert response[:6] == bytes.fromhex("A55A0204101B")
- assert response[6:33] == values
- assert response[-2:] == protocol.crc16_ccitt(response[2:-2]).to_bytes(
- 2, "little"
- )
- def test_temperature_response_all_ff_frame_matches_the_frozen_crc() -> None:
- response = protocol.build_temperature_response(bytes([0xFF] * 27))
- assert response == ALL_FF_RESPONSE
- assert response[-2:] == bytes.fromhex("1BC4")
- def test_temperature_bus_error_ack_decodes_as_status_0x0a() -> None:
- wire = protocol.encode_frame(
- frame_type=protocol.FrameType.HOST_ACK,
- seq=0,
- timestamp_tick=0,
- payload=bytes.fromhex("00280A0000000000"),
- )
- ack = protocol.decode_host_ack(protocol.decode_frame(wire))
- assert ack.command == protocol.CommandId.GET_TEMPERATURES
- assert ack.status == protocol.StatusCode.TEMPERATURE_BUS_ERROR
- @pytest.mark.parametrize(
- "values",
- [
- bytes(26),
- bytes(28),
- [0] * 26 + [256],
- [0] * 26 + [-1],
- ],
- )
- def test_temperature_response_rejects_non_27_byte_payloads(values: object) -> None:
- with pytest.raises(ValueError):
- protocol.build_temperature_response(values)
|