|
1 | 1 | """Tests for the DeviceFeaturesTrait related functionality.""" |
2 | 2 |
|
| 3 | +from unittest.mock import AsyncMock |
| 4 | + |
3 | 5 | import pytest |
4 | 6 | from syrupy import SnapshotAssertion |
5 | 7 |
|
6 | 8 | from roborock.data import HomeDataDevice |
7 | 9 | from roborock.data.v1.v1_containers import ConsumableField, StatusField |
| 10 | +from roborock.devices.cache import DeviceCache |
8 | 11 | from roborock.devices.device import RoborockDevice |
9 | 12 | from roborock.devices.traits.v1.consumeable import ConsumableTrait |
10 | 13 | from roborock.devices.traits.v1.status import StatusTrait |
| 14 | +from roborock.exceptions import RoborockParsingException |
11 | 15 | from tests import mock_data |
12 | 16 |
|
13 | 17 | V1_DEVICES = { |
@@ -57,3 +61,29 @@ async def test_is_consumable_field_supported( |
57 | 61 | field.value: device_features_trait.is_field_supported(ConsumableTrait, field) for field in ConsumableField |
58 | 62 | } |
59 | 63 | assert is_v1_supported == snapshot |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("payload", [[], ["ok"]], ids=["empty", "non-dict"]) |
| 67 | +async def test_refresh_unexpected_list_keeps_state( |
| 68 | + device: RoborockDevice, |
| 69 | + device_cache: DeviceCache, |
| 70 | + mock_rpc_channel: AsyncMock, |
| 71 | + payload: list, |
| 72 | +) -> None: |
| 73 | + """An empty or non-dict init status reply raises a parsing error and keeps the previous features.""" |
| 74 | + assert device.v1_properties is not None |
| 75 | + device_features_trait = device.v1_properties.device_features |
| 76 | + previous = device_features_trait.as_dict() |
| 77 | + # Force a device read instead of the cached features from discovery. |
| 78 | + cache_data = await device_cache.get() |
| 79 | + cache_data.device_features = None |
| 80 | + await device_cache.set(cache_data) |
| 81 | + mock_rpc_channel.send_command.return_value = payload |
| 82 | + |
| 83 | + with pytest.raises( |
| 84 | + RoborockParsingException, |
| 85 | + match=r"Failed to parse APP_GET_INIT_STATUS response for DeviceFeaturesTrait.*ValueError.*", |
| 86 | + ): |
| 87 | + await device_features_trait.refresh() |
| 88 | + |
| 89 | + assert device_features_trait.as_dict() == previous |
0 commit comments