Skip to content

Commit d966b84

Browse files
authored
chore: add snapshot tests for parsing device wire formats (#457)
* chore: add snapshot tests for parsing device wire formats * chore: sort imports
1 parent 5daad6b commit d966b84

File tree

6 files changed

+96
-3
lines changed

6 files changed

+96
-3
lines changed

poetry.lock

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pyshark = "^0.6"
4949
aioresponses = "^0.7.7"
5050
freezegun = "^1.5.1"
5151
pytest-timeout = "^2.3.1"
52+
syrupy = "^4.9.1"
5253

5354
[tool.semantic_release]
5455
branch = "main"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# serializer version: 1
2+
# name: test_decode_rpc_payload[get_dnd]
3+
20002
4+
# ---
5+
# name: test_decode_rpc_payload[get_dnd].1
6+
'''
7+
{
8+
"start_hour": 22,
9+
"start_minute": 0,
10+
"end_hour": 8,
11+
"end_minute": 0,
12+
"enabled": 1
13+
}
14+
'''
15+
# ---
16+
# name: test_decode_rpc_payload[get_status]
17+
20001
18+
# ---
19+
# name: test_decode_rpc_payload[get_status].1
20+
'''
21+
{
22+
"msg_ver": 2,
23+
"msg_seq": 515,
24+
"state": 8,
25+
"battery": 100,
26+
"clean_time": 5405,
27+
"clean_area": 91287500,
28+
"error_code": 0,
29+
"map_present": 1,
30+
"in_cleaning": 0,
31+
"in_returning": 0,
32+
"in_fresh_state": 1,
33+
"lab_status": 1,
34+
"water_box_status": 0,
35+
"fan_power": 106,
36+
"dnd_enabled": 1,
37+
"map_status": 3,
38+
"is_locating": 0,
39+
"lock_status": 0,
40+
"water_box_mode": 204,
41+
"distance_off": 0,
42+
"water_box_carriage_status": 0,
43+
"mop_forbidden_enable": 0,
44+
"unsave_map_reason": 4,
45+
"unsave_map_flag": 0
46+
}
47+
'''
48+
# ---

tests/protocols/test_v1_protocol.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""Tests for the v1 protocol message encoding and decoding."""
22

3+
import json
4+
import pathlib
35
from collections.abc import Generator
46
from unittest.mock import patch
57

68
import pytest
79
from freezegun import freeze_time
10+
from syrupy import SnapshotAssertion
811

912
from roborock.containers import RoborockBase, UserData
1013
from roborock.exceptions import RoborockException
@@ -30,6 +33,11 @@
3033
)
3134

3235

36+
TESTDATA_PATH = pathlib.Path("tests/protocols/testdata/v1_protocol/")
37+
TESTDATA_FILES = list(TESTDATA_PATH.glob("*.json"))
38+
TESTDATA_IDS = [x.stem for x in TESTDATA_FILES]
39+
40+
3341
@pytest.fixture(autouse=True)
3442
def fixed_time_fixture() -> Generator[None, None, None]:
3543
"""Fixture to freeze time for predictable request IDs."""
@@ -132,6 +140,25 @@ def test_decode_rpc_response(payload: bytes, expected: RoborockBase) -> None:
132140
assert decoded_message.data == expected
133141

134142

143+
@pytest.mark.parametrize("filename", TESTDATA_FILES, ids=TESTDATA_IDS)
144+
def test_decode_rpc_payload(filename: str, snapshot: SnapshotAssertion) -> None:
145+
"""Test decoding a v1 RPC response protocol message."""
146+
with open(filename, "rb") as f:
147+
payload = f.read()
148+
# The values other than the payload are arbitrary
149+
message = RoborockMessage(
150+
protocol=RoborockMessageProtocol.GENERAL_RESPONSE,
151+
payload=payload,
152+
seq=12750,
153+
version=b"1.0",
154+
random=97431,
155+
timestamp=1652547161,
156+
)
157+
decoded_message = decode_rpc_response(message)
158+
assert decoded_message.request_id == snapshot
159+
assert json.dumps(decoded_message.data, indent=2) == snapshot
160+
161+
135162
def test_create_map_response_decoder():
136163
"""Test creating and using a map response decoder."""
137164
test_data = b"some map\n"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"t": 1755785801, "dps": {"102": "{\"id\":20002,\"result\":[{\"start_hour\":22,\"start_minute\":0,\"end_hour\":8,\"end_minute\":0,\"enabled\":1}]}"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"t": 1755785773, "dps": {"102": "{\"id\":20001,\"result\":[{\"msg_ver\":2,\"msg_seq\":515,\"state\":8,\"battery\":100,\"clean_time\":5405,\"clean_area\":91287500,\"error_code\":0,\"map_present\":1,\"in_cleaning\":0,\"in_returning\":0,\"in_fresh_state\":1,\"lab_status\":1,\"water_box_status\":0,\"fan_power\":106,\"dnd_enabled\":1,\"map_status\":3,\"is_locating\":0,\"lock_status\":0,\"water_box_mode\":204,\"distance_off\":0,\"water_box_carriage_status\":0,\"mop_forbidden_enable\":0,\"unsave_map_reason\":4,\"unsave_map_flag\":0}]}"}}

0 commit comments

Comments
 (0)