| 1 |
"""Tests for aioacaia.parser covering each message-parsing path.""" |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from aioacaia.exceptions import ( |
| 6 |
AcaiaMessageError, |
| 7 |
AcaiaMessageTooLong, |
| 8 |
AcaiaMessageTooShort, |
| 9 |
) |
| 10 |
from aioacaia.messages import ( |
| 11 |
ButtonMessage, |
| 12 |
ButtonType, |
| 13 |
Settings, |
| 14 |
TimerMessage, |
| 15 |
WeightMessage, |
| 16 |
) |
| 17 |
from aioacaia.parser import decode |
| 18 |
from aioacaia.scale import AcaiaScale |
| 19 |
from tests.fixtures import messages as m |
| 20 |
|
| 21 |
|
| 22 |
def _decode_message(raw: bytes): |
| 23 |
"""Decode a single, complete message and assert nothing remains.""" |
| 24 |
msg, remaining = decode(bytearray(raw)) |
| 25 |
assert remaining == b"" |
| 26 |
return msg |
| 27 |
|
| 28 |
|
| 29 |
def _assert_close(actual, expected): |
| 30 |
"""Assert equality, tolerating float rounding, and handling None.""" |
| 31 |
if expected is None: |
| 32 |
assert actual is None |
| 33 |
else: |
| 34 |
assert actual == pytest.approx(expected) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize( |
| 38 |
("raw", "expected"), |
| 39 |
[ |
| 40 |
(m.WEIGHT, 175.9), |
| 41 |
(m.WEIGHT_NEGATIVE, -175.9), |
| 42 |
(m.WEIGHT_UNIT_100, 17.59), |
| 43 |
(m.WEIGHT_UNIT_1000, 1.759), |
| 44 |
(m.WEIGHT_UNIT_10000, 0.1759), |
| 45 |
], |
| 46 |
) |
| 47 |
def test_decode_weight(raw, expected): |
| 48 |
"""Weight messages decode to the expected gram value.""" |
| 49 |
msg = _decode_message(raw) |
| 50 |
assert isinstance(msg, WeightMessage) |
| 51 |
assert msg.weight == pytest.approx(expected) |
| 52 |
|
| 53 |
|
| 54 |
def test_decode_weight_invalid_unit_raises(): |
| 55 |
"""An out-of-range unit byte raises a message error.""" |
| 56 |
with pytest.raises(AcaiaMessageError, match="Unknown weight unit"): |
| 57 |
decode(bytearray(m.WEIGHT_BAD_UNIT)) |
| 58 |
|
| 59 |
|
| 60 |
def test_decode_timer(): |
| 61 |
"""Timer messages decode to seconds.""" |
| 62 |
msg = _decode_message(m.TIMER) |
| 63 |
assert isinstance(msg, TimerMessage) |
| 64 |
assert msg.time == pytest.approx(90.5) |
| 65 |
|
| 66 |
|
| 67 |
def test_decode_heartbeat_weight(): |
| 68 |
"""A heartbeat wrapping a weight decodes to a WeightMessage.""" |
| 69 |
msg = _decode_message(m.HEARTBEAT_WEIGHT) |
| 70 |
assert isinstance(msg, WeightMessage) |
| 71 |
assert msg.weight == pytest.approx(175.9) |
| 72 |
|
| 73 |
|
| 74 |
def test_decode_heartbeat_time(): |
| 75 |
"""A heartbeat wrapping a timer decodes to a TimerMessage.""" |
| 76 |
msg = _decode_message(m.HEARTBEAT_TIME) |
| 77 |
assert isinstance(msg, TimerMessage) |
| 78 |
assert msg.time == pytest.approx(90.5) |
| 79 |
|
| 80 |
|
| 81 |
def test_decode_heartbeat_uses_first_record(): |
| 82 |
"""A heartbeat is decoded by its first wrapped record, not a later one.""" |
| 83 |
msg = _decode_message(m.HEARTBEAT_WEIGHT_THEN_TIME) |
| 84 |
assert isinstance(msg, WeightMessage) |
| 85 |
assert msg.weight == pytest.approx(175.9) |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.parametrize( |
| 89 |
("raw", "button", "timer_running", "expected_time", "expected_weight"), |
| 90 |
[ |
| 91 |
(m.BUTTON_TARE, ButtonType.TARE, None, None, 175.9), |
| 92 |
(m.BUTTON_START_WEIGHT, ButtonType.START, True, None, 175.9), |
| 93 |
(m.BUTTON_START, ButtonType.START, True, None, None), |
| 94 |
(m.BUTTON_STOP_TIME_WEIGHT, ButtonType.STOP, False, 90.5, 175.9), |
| 95 |
(m.BUTTON_STOP_TIME, ButtonType.STOP, False, 90.5, None), |
| 96 |
(m.BUTTON_STOP, ButtonType.STOP, False, None, None), |
| 97 |
(m.BUTTON_RESET_TIME_WEIGHT, ButtonType.RESET, None, 90.5, 175.9), |
| 98 |
(m.BUTTON_RESET_TIME, ButtonType.RESET, None, 90.5, None), |
| 99 |
(m.BUTTON_RESET, ButtonType.RESET, None, None, None), |
| 100 |
(m.BUTTON_STOP_WEIGHT, ButtonType.STOP, False, None, 175.9), |
| 101 |
(m.BUTTON_RESET_WEIGHT, ButtonType.RESET, None, None, 175.9), |
| 102 |
(m.BUTTON_UNKNOWN, ButtonType.UNKNOWN, None, None, None), |
| 103 |
], |
| 104 |
) |
| 105 |
def test_decode_button(raw, button, timer_running, expected_time, expected_weight): |
| 106 |
"""Button messages decode to the right event, timer state, time and weight.""" |
| 107 |
msg = _decode_message(raw) |
| 108 |
assert isinstance(msg, ButtonMessage) |
| 109 |
assert msg.button is button |
| 110 |
assert msg.timer_running is timer_running |
| 111 |
_assert_close(msg.time, expected_time) |
| 112 |
_assert_close(msg.weight, expected_weight) |
| 113 |
|
| 114 |
|
| 115 |
def test_decode_unknown_type_raises(): |
| 116 |
"""An unknown message type raises AcaiaMessageError.""" |
| 117 |
with pytest.raises(AcaiaMessageError): |
| 118 |
decode(bytearray(m.UNKNOWN_TYPE)) |
| 119 |
|
| 120 |
|
| 121 |
def test_decode_invalid_checksum_raises(): |
| 122 |
"""A frame with a mismatched checksum is rejected.""" |
| 123 |
with pytest.raises(AcaiaMessageError, match="checksum"): |
| 124 |
decode(bytearray(m.BAD_CHECKSUM)) |
| 125 |
|
| 126 |
|
| 127 |
def test_decode_short_weight_payload_raises_message_error(): |
| 128 |
"""A valid frame with an undersized weight payload is rejected cleanly.""" |
| 129 |
with pytest.raises(AcaiaMessageError, match="Weight payload is too short"): |
| 130 |
decode(bytearray(m.SHORT_WEIGHT)) |
| 131 |
|
| 132 |
|
| 133 |
@pytest.mark.parametrize( |
| 134 |
("raw", "units"), |
| 135 |
[ |
| 136 |
(m.SETTINGS_GRAMS, "grams"), |
| 137 |
(m.SETTINGS_OUNCES, "ounces"), |
| 138 |
], |
| 139 |
) |
| 140 |
def test_decode_settings(raw, units): |
| 141 |
"""Settings messages decode battery, units, auto-off and beep.""" |
| 142 |
settings, remaining = decode(bytearray(raw)) |
| 143 |
assert isinstance(settings, Settings) |
| 144 |
assert remaining == b"" |
| 145 |
assert settings.units == units |
| 146 |
assert settings.battery == 93 |
| 147 |
assert settings.auto_off == 5 |
| 148 |
assert settings.beep_on is True |
| 149 |
|
| 150 |
|
| 151 |
def test_decode_too_short_raises(): |
| 152 |
"""A header-only message raises AcaiaMessageTooShort with the received bytes.""" |
| 153 |
with pytest.raises(AcaiaMessageTooShort) as exc: |
| 154 |
decode(bytearray(m.HEADER_ONLY)) |
| 155 |
assert exc.value.bytes_recvd == bytearray(m.HEADER_ONLY) |
| 156 |
|
| 157 |
|
| 158 |
def test_decode_too_long_raises(): |
| 159 |
"""A length byte past the buffer raises AcaiaMessageTooLong.""" |
| 160 |
with pytest.raises(AcaiaMessageTooLong): |
| 161 |
decode(bytearray(m.TOO_LONG)) |
| 162 |
|
| 163 |
|
| 164 |
def test_decode_unknown_command_returns_none(): |
| 165 |
"""An unknown command yields no message but consumes the bytes.""" |
| 166 |
msg, remaining = decode(bytearray(m.UNKNOWN_COMMAND)) |
| 167 |
assert msg is None |
| 168 |
assert remaining == b"" |
| 169 |
|
| 170 |
|
| 171 |
def test_decode_skips_leading_garbage(): |
| 172 |
"""Bytes before the header are ignored.""" |
| 173 |
msg = _decode_message(m.LEADING_GARBAGE) |
| 174 |
assert isinstance(msg, WeightMessage) |
| 175 |
assert msg.weight == pytest.approx(175.9) |
| 176 |
|
| 177 |
|
| 178 |
def test_decode_returns_remaining_bytes(): |
| 179 |
"""Trailing bytes after a message are returned for the next decode.""" |
| 180 |
first, remaining = decode(bytearray(m.TWO_MESSAGES)) |
| 181 |
assert isinstance(first, WeightMessage) |
| 182 |
assert remaining == bytearray(m.SETTINGS_GRAMS) |
| 183 |
second, tail = decode(remaining) |
| 184 |
assert isinstance(second, Settings) |
| 185 |
assert tail == b"" |
| 186 |
|
| 187 |
|
| 188 |
def test_decode_split_message_reassembly(): |
| 189 |
"""A message split across two chunks decodes once reassembled.""" |
| 190 |
with pytest.raises(AcaiaMessageTooShort) as exc: |
| 191 |
decode(bytearray(m.HEADER_ONLY)) |
| 192 |
assert exc.value.bytes_recvd == bytearray(m.HEADER_ONLY) |
| 193 |
|
| 194 |
msg, remaining = decode(bytearray(m.HEADER_ONLY + m.SPLIT_REMAINDER)) |
| 195 |
assert isinstance(msg, WeightMessage) |
| 196 |
assert msg.weight == pytest.approx(175.9) |
| 197 |
assert remaining == b"" |
| 198 |
|
| 199 |
|
| 200 |
async def test_scale_reassembles_split_notifications(): |
| 201 |
"""The scale buffers a header-only chunk and decodes it with the next.""" |
| 202 |
scale = AcaiaScale("aa:bb:cc:dd:ee:ff") |
| 203 |
|
| 204 |
await scale.on_bluetooth_data_received(None, bytearray(m.HEADER_ONLY)) |
| 205 |
assert scale.weight is None |
| 206 |
|
| 207 |
await scale.on_bluetooth_data_received(None, bytearray(m.SPLIT_REMAINDER)) |
| 208 |
assert scale.weight == pytest.approx(175.9) |
| 209 |
|
| 210 |
|
| 211 |
@pytest.mark.parametrize("split_at", range(1, len(m.WEIGHT))) |
| 212 |
async def test_scale_reassembles_notifications_split_at_any_byte(split_at): |
| 213 |
"""The scale retains partial headers and partial message bodies.""" |
| 214 |
scale = AcaiaScale("aa:bb:cc:dd:ee:ff") |
| 215 |
|
| 216 |
await scale.on_bluetooth_data_received(None, bytearray(m.WEIGHT[:split_at])) |
| 217 |
assert scale.weight is None |
| 218 |
|
| 219 |
await scale.on_bluetooth_data_received(None, bytearray(m.WEIGHT[split_at:])) |
| 220 |
assert scale.weight == pytest.approx(175.9) |
| 221 |
|
| 222 |
|
| 223 |
async def test_scale_processes_all_messages_in_one_notification(): |
| 224 |
"""Coalesced frames are all applied instead of dropping the trailing frame.""" |
| 225 |
scale = AcaiaScale("aa:bb:cc:dd:ee:ff") |
| 226 |
|
| 227 |
await scale.on_bluetooth_data_received(None, bytearray(m.TWO_MESSAGES)) |
| 228 |
|
| 229 |
assert scale.weight == pytest.approx(175.9) |
| 230 |
assert scale.device_state is not None |
| 231 |
assert scale.device_state.battery_level == 93 |
| 232 |
|
| 233 |
|
| 234 |
async def test_scale_recovers_from_incomplete_frame_before_valid_frame(): |
| 235 |
"""A corrupt length does not pin valid data behind the incomplete frame.""" |
| 236 |
scale = AcaiaScale("aa:bb:cc:dd:ee:ff") |
| 237 |
|
| 238 |
await scale.on_bluetooth_data_received(None, bytearray(m.TOO_LONG + m.WEIGHT)) |
| 239 |
|
| 240 |
assert scale.weight == pytest.approx(175.9) |
| 241 |
assert scale._last_short_msg is None |
| 242 |
|
| 243 |
|
| 244 |
async def test_scale_does_not_resync_to_header_inside_fragmented_payload(): |
| 245 |
"""Header bytes inside an incomplete payload remain part of that frame.""" |
| 246 |
scale = AcaiaScale("aa:bb:cc:dd:ee:ff") |
| 247 |
raw = m.WEIGHT_WITH_EMBEDDED_HEADER |
| 248 |
|
| 249 |
await scale.on_bluetooth_data_received(None, bytearray(raw[:7])) |
| 250 |
await scale.on_bluetooth_data_received(None, bytearray(raw[7:])) |
| 251 |
|
| 252 |
assert scale.weight == pytest.approx(5681.5) |
| 253 |
|
| 254 |
|
| 255 |
@pytest.mark.parametrize( |
| 256 |
("raw", "button", "timer_running", "expected_time", "expected_weight"), |
| 257 |
[ |
| 258 |
# --- Original Pearl (PEARL-244684) --- |
| 259 |
# A stop reporting both weight and the elapsed time behind it. Decoding |
| 260 |
# the weight record as a time turned 55.1 g into "2342 s" — plausible |
| 261 |
# enough to go unnoticed, and no exception to catch it. |
| 262 |
(m.REAL_STOP_WEIGHT_TIME, ButtonType.STOP, False, 9.7, 55.1), |
| 263 |
# Presses arriving behind a 0b record, which the scale does send. |
| 264 |
(m.REAL_START_BEHIND_TAG_0B, ButtonType.START, True, None, 54.9), |
| 265 |
(m.REAL_RESET_BEHIND_TAG_0B, ButtonType.RESET, None, None, 0.0), |
| 266 |
# --- 2021+ new-style scale --- |
| 267 |
# Start and reset are a bare key code (no records, no tag 0b); stop |
| 268 |
# carries a weight then a time record. Stop time cross-checked against |
| 269 |
# the wall clock (10.8 s decoded vs 10.6 s measured). |
| 270 |
(m.REAL_2021_START, ButtonType.START, True, None, None), |
| 271 |
(m.REAL_2021_STOP_WEIGHT_TIME, ButtonType.STOP, False, 10.8, 0.0), |
| 272 |
(m.REAL_2021_RESET, ButtonType.RESET, None, None, None), |
| 273 |
], |
| 274 |
) |
| 275 |
def test_decode_button_real_captures( |
| 276 |
raw, button, timer_running, expected_time, expected_weight |
| 277 |
): |
| 278 |
"""Frames captured verbatim from real scales (Pearl and 2021+) decode correctly.""" |
| 279 |
msg = _decode_message(raw) |
| 280 |
assert isinstance(msg, ButtonMessage) |
| 281 |
assert msg.button is button |
| 282 |
assert msg.timer_running is timer_running |
| 283 |
_assert_close(msg.time, expected_time) |
| 284 |
_assert_close(msg.weight, expected_weight) |
| 285 |
|
| 286 |
|
| 287 |
def test_decode_heartbeat_wrapped_button(): |
| 288 |
"""A button nested in a heartbeat is a press, not an undecodable payload.""" |
| 289 |
msg = _decode_message(m.REAL_HEARTBEAT_WRAPPED_START) |
| 290 |
assert isinstance(msg, ButtonMessage) |
| 291 |
assert msg.button is ButtonType.START |
| 292 |
assert msg.timer_running is True |