Skip to content

Commit 211429b

Browse files
authored
chore: clean up tests from previous pr (#687)
1 parent 7d113db commit 211429b

File tree

4 files changed

+64
-95
lines changed

4 files changed

+64
-95
lines changed

tests/devices/test_local_channel.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import json
5+
import logging
56
from collections.abc import Generator
67
from unittest.mock import AsyncMock, Mock, patch
78

@@ -149,10 +150,11 @@ async def test_successful_command_response(local_channel: LocalChannel, mock_loo
149150

150151
async def test_message_decode_error(local_channel: LocalChannel, caplog: pytest.LogCaptureFixture) -> None:
151152
"""Test handling of message decode errors."""
152-
local_channel._data_received(b"invalid_payload")
153-
await asyncio.sleep(0.01) # yield
153+
with caplog.at_level(logging.WARNING):
154+
local_channel._data_received(b"invalid_payload")
155+
await asyncio.sleep(0.01) # yield
154156

155-
warning_records = [record for record in caplog.records if record.levelname == "WARNING"]
157+
warning_records = caplog.records
156158
assert len(warning_records) == 1
157159
assert "Failed to decode message" in warning_records[0].message
158160

tests/devices/test_local_decoder_padding.py

Lines changed: 0 additions & 89 deletions
This file was deleted.

tests/devices/test_mqtt_channel.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ async def test_message_decode_error(
155155
callback = Mock()
156156
unsub = await mqtt_channel.subscribe(callback)
157157

158-
mqtt_message_handler(b"invalid_payload")
159-
await asyncio.sleep(0.01) # yield
158+
with caplog.at_level(logging.WARNING):
159+
mqtt_message_handler(b"invalid_payload")
160+
await asyncio.sleep(0.01) # yield
160161

161-
warning_records = [record for record in caplog.records if record.levelname == "WARNING"]
162+
warning_records = caplog.records
162163
assert len(warning_records) == 1
163164
assert "Failed to decode message" in warning_records[0].message
164165
unsub()

tests/test_protocol.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
3+
from roborock.protocol import create_local_decoder, create_local_encoder
4+
from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol
5+
6+
TEST_LOCAL_KEY = "local_key"
7+
8+
9+
@pytest.mark.parametrize(
10+
("garbage"),
11+
[
12+
b"",
13+
b"\x00\x00\x05\xa1",
14+
b"\x00\x00\x05\xa1\xff\xff",
15+
],
16+
)
17+
def test_decoder_clean_message(garbage: bytes):
18+
encoder = create_local_encoder(TEST_LOCAL_KEY)
19+
decoder = create_local_decoder(TEST_LOCAL_KEY)
20+
msg = RoborockMessage(
21+
protocol=RoborockMessageProtocol.RPC_REQUEST,
22+
payload=b"test_payload",
23+
version=b"1.0",
24+
seq=1,
25+
random=123,
26+
)
27+
encoded = encoder(msg)
28+
decoded = decoder(garbage + encoded)
29+
assert len(decoded) == 1
30+
assert decoded[0].payload == b"test_payload"
31+
32+
33+
def test_decoder_split_padding_variable():
34+
"""Test variable padding split across chunks."""
35+
encoder = create_local_encoder(TEST_LOCAL_KEY, connect_nonce=123, ack_nonce=456)
36+
decoder = create_local_decoder(TEST_LOCAL_KEY, connect_nonce=123, ack_nonce=456)
37+
38+
msg = RoborockMessage(
39+
protocol=RoborockMessageProtocol.RPC_REQUEST,
40+
payload=b"test_payload",
41+
version=b"L01",
42+
)
43+
encoded = encoder(msg)
44+
45+
garbage = b"\x00\x00\x05\xa1\xff\xff" # 6 bytes
46+
47+
# Send garbage
48+
decoded1 = decoder(garbage)
49+
assert len(decoded1) == 0
50+
51+
# Send message
52+
decoded2 = decoder(encoded)
53+
54+
assert len(decoded2) == 1
55+
assert decoded2[0].payload == b"test_payload"

0 commit comments

Comments
 (0)