|
| 1 | +"""Tests for the B01 protocol message encoding and decoding.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import pathlib |
| 5 | +from collections.abc import Generator |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import pytest |
| 9 | +from freezegun import freeze_time |
| 10 | +from syrupy import SnapshotAssertion |
| 11 | + |
| 12 | +from roborock.data.b01_q10.b01_q10_code_mappings import B01_Q10_DP, YXWaterLevel |
| 13 | +from roborock.exceptions import RoborockException |
| 14 | +from roborock.protocols.b01_q10_protocol import ( |
| 15 | + decode_rpc_response, |
| 16 | + encode_mqtt_payload, |
| 17 | +) |
| 18 | +from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol |
| 19 | + |
| 20 | +TESTDATA_PATH = pathlib.Path("tests/protocols/testdata/b01_q10_protocol/") |
| 21 | +TESTDATA_FILES = list(TESTDATA_PATH.glob("*.json")) |
| 22 | +TESTDATA_IDS = [x.stem for x in TESTDATA_FILES] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(autouse=True) |
| 26 | +def fixed_time_fixture() -> Generator[None, None, None]: |
| 27 | + """Fixture to freeze time for predictable request IDs.""" |
| 28 | + with freeze_time("2025-01-20T12:00:00"): |
| 29 | + yield |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize("filename", TESTDATA_FILES, ids=TESTDATA_IDS) |
| 33 | +def test_decode_rpc_payload(filename: str, snapshot: SnapshotAssertion) -> None: |
| 34 | + """Test decoding a B01 RPC response protocol message.""" |
| 35 | + with open(filename, "rb") as f: |
| 36 | + payload = f.read() |
| 37 | + |
| 38 | + message = RoborockMessage( |
| 39 | + protocol=RoborockMessageProtocol.RPC_RESPONSE, |
| 40 | + payload=payload, |
| 41 | + seq=12750, |
| 42 | + version=b"B01", |
| 43 | + random=97431, |
| 44 | + timestamp=1652547161, |
| 45 | + ) |
| 46 | + |
| 47 | + decoded_message = decode_rpc_response(message) |
| 48 | + assert json.dumps(decoded_message, indent=2) == snapshot |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize( |
| 52 | + ("payload", "expected_error_message"), |
| 53 | + [ |
| 54 | + (b"", "missing payload"), |
| 55 | + (b"n", "Invalid B01 json payload"), |
| 56 | + (b"{}", "missing 'dps'"), |
| 57 | + (b'{"dps": []}', "'dps' should be a dictionary"), |
| 58 | + (b'{"dps": {"not_a_number": 123}}', "dps key is not a valid integer"), |
| 59 | + (b'{"dps": {"101": 123}}', "Invalid dpCommon format: expected dict"), |
| 60 | + (b'{"dps": {"101": {"not_a_number": 123}}}', "Invalid dpCommon format: dps key is not a valid intege"), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_decode_invalid_rpc_payload(payload: bytes, expected_error_message: str) -> None: |
| 64 | + """Test decoding a B01 RPC response protocol message.""" |
| 65 | + message = RoborockMessage( |
| 66 | + protocol=RoborockMessageProtocol.RPC_RESPONSE, |
| 67 | + payload=payload, |
| 68 | + seq=12750, |
| 69 | + version=b"B01", |
| 70 | + random=97431, |
| 71 | + timestamp=1652547161, |
| 72 | + ) |
| 73 | + with pytest.raises(RoborockException, match=expected_error_message): |
| 74 | + decode_rpc_response(message) |
| 75 | + |
| 76 | + |
| 77 | +def test_decode_unknown_dps_code() -> None: |
| 78 | + """Test decoding a B01 RPC response protocol message.""" |
| 79 | + message = RoborockMessage( |
| 80 | + protocol=RoborockMessageProtocol.RPC_RESPONSE, |
| 81 | + payload=b'{"dps": {"909090": 123, "122":100}}', |
| 82 | + seq=12750, |
| 83 | + version=b"B01", |
| 84 | + random=97431, |
| 85 | + timestamp=1652547161, |
| 86 | + ) |
| 87 | + |
| 88 | + decoded_message = decode_rpc_response(message) |
| 89 | + assert decoded_message == { |
| 90 | + B01_Q10_DP.BATTERY: 100, |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize( |
| 95 | + ("command", "params"), |
| 96 | + [ |
| 97 | + (B01_Q10_DP.REQUETDPS, {}), |
| 98 | + (B01_Q10_DP.REQUETDPS, None), |
| 99 | + (B01_Q10_DP.START_CLEAN, {"cmd": 1}), |
| 100 | + (B01_Q10_DP.WATER_LEVEL, YXWaterLevel.MIDDLE.code), |
| 101 | + ], |
| 102 | +) |
| 103 | +def test_encode_mqtt_payload(command: B01_Q10_DP, params: dict[str, Any], snapshot) -> None: |
| 104 | + """Test encoding of MQTT payload for B01 Q10 commands.""" |
| 105 | + |
| 106 | + message = encode_mqtt_payload(command, params) |
| 107 | + assert isinstance(message, RoborockMessage) |
| 108 | + assert message.protocol == RoborockMessageProtocol.RPC_REQUEST |
| 109 | + assert message.version == b"B01" |
| 110 | + assert message.payload is not None |
| 111 | + |
| 112 | + # Snapshot the raw payload to ensure stable encoding. We verify it is |
| 113 | + # valid json |
| 114 | + assert snapshot == message.payload |
| 115 | + |
| 116 | + json.loads(message.payload.decode()) |
0 commit comments