Skip to content

Commit a4ef488

Browse files
author
Harry Coureau
committed
fix: wrap empty cleaning record responses as parsing errors
1 parent ed5f859 commit a4ef488

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎roborock/devices/traits/v1/clean_summary.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def convert(self, response: common.V1ResponseData) -> CleanRecord:
3939
if isinstance(response, dict):
4040
return CleanRecord.from_dict(response)
4141
if isinstance(response, list):
42+
if not response:
43+
raise ValueError("Empty clean record response")
4244
if isinstance(response[-1], dict):
4345
records = [CleanRecord.from_dict(rec) for rec in response]
4446
final_record = records[-1]

‎tests/devices/traits/v1/test_clean_summary.py‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from roborock.data import CleanSummary
88
from roborock.devices.device import RoborockDevice
99
from roborock.devices.traits.v1.clean_summary import CleanSummaryTrait
10-
from roborock.exceptions import RoborockException
10+
from roborock.exceptions import RoborockException, RoborockParsingException
1111
from roborock.roborock_typing import RoborockCommand
1212
from tests.mock_data import CLEAN_RECORD
1313

@@ -193,6 +193,58 @@ async def test_get_clean_record_success(
193193
)
194194

195195

196+
@pytest.mark.parametrize("response", [[], [[]]], ids=["empty", "wrapped-empty"])
197+
async def test_get_clean_record_empty_response(
198+
clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock, response: list
199+
) -> None:
200+
"""Report empty records through the public parsing exception boundary."""
201+
mock_rpc_channel.send_command.return_value = response
202+
203+
with pytest.raises(RoborockParsingException, match="GET_CLEAN_RECORD") as exc:
204+
await clean_summary_trait.get_clean_record(1738864366)
205+
206+
assert isinstance(exc.value.__cause__, ValueError)
207+
assert f"Payload: {response!r}" in str(exc.value)
208+
mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_CLEAN_RECORD, params=[1738864366])
209+
210+
211+
@pytest.mark.parametrize(
212+
"response",
213+
[
214+
[CLEAN_RECORD],
215+
[
216+
{
217+
"begin": 1700000000,
218+
"end": 1700000100,
219+
"duration": 100,
220+
"area": 1000000,
221+
"avoid_count": 1,
222+
"wash_count": 1,
223+
},
224+
{
225+
"begin": 1700000200,
226+
"end": 1700000400,
227+
"duration": 200,
228+
"area": 2000000,
229+
"avoid_count": 2,
230+
"wash_count": 2,
231+
},
232+
],
233+
],
234+
ids=["wrapped-dict", "multi-phase"],
235+
)
236+
async def test_get_clean_record_structured_responses(
237+
clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock, response: list[dict]
238+
) -> None:
239+
"""Keep wrapped and multi-phase records working through the public API."""
240+
mock_rpc_channel.send_command.return_value = response
241+
242+
record = await clean_summary_trait.get_clean_record(1738864366)
243+
244+
assert record.begin == response[0]["begin"]
245+
assert record.end == response[-1]["end"]
246+
247+
196248
async def test_get_clean_record_dict_response(
197249
clean_summary_trait: CleanSummaryTrait,
198250
mock_rpc_channel: AsyncMock,

0 commit comments

Comments
 (0)