|
| 1 | +"""Tests for the Maps related functionality.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from roborock.devices.device import RoborockDevice |
| 8 | +from roborock.devices.traits.v1.maps import MapsTrait |
| 9 | +from roborock.devices.traits.v1.status import StatusTrait |
| 10 | +from roborock.roborock_typing import RoborockCommand |
| 11 | +from tests import mock_data |
| 12 | + |
| 13 | +UPDATED_STATUS = { |
| 14 | + **mock_data.STATUS, |
| 15 | + "map_status": 123 * 4 + 3, # Set current map to 123 |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +MULTI_MAP_LIST_DATA = [ |
| 20 | + { |
| 21 | + "max_multi_map": 1, |
| 22 | + "max_bak_map": 1, |
| 23 | + "multi_map_count": 1, |
| 24 | + "map_info": [ |
| 25 | + { |
| 26 | + "mapFlag": 0, |
| 27 | + "add_time": 1747132930, |
| 28 | + "length": 0, |
| 29 | + "name": "Map 1", |
| 30 | + "bak_maps": [{"mapFlag": 4, "add_time": 1747132936}], |
| 31 | + }, |
| 32 | + { |
| 33 | + "mapFlag": 123, |
| 34 | + "add_time": 1747132930, |
| 35 | + "length": 0, |
| 36 | + "name": "Map 1", |
| 37 | + "bak_maps": [{"mapFlag": 4, "add_time": 1747132936}], |
| 38 | + }, |
| 39 | + ], |
| 40 | + } |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def status_trait(device: RoborockDevice) -> StatusTrait: |
| 46 | + """Create a MapsTrait instance with mocked dependencies.""" |
| 47 | + assert device.v1_properties |
| 48 | + return device.v1_properties.status |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def maps_trait(device: RoborockDevice) -> MapsTrait: |
| 53 | + """Create a MapsTrait instance with mocked dependencies.""" |
| 54 | + assert device.v1_properties |
| 55 | + return device.v1_properties.maps |
| 56 | + |
| 57 | + |
| 58 | +async def test_refresh_maps_trait( |
| 59 | + maps_trait: MapsTrait, |
| 60 | + mock_rpc_channel: AsyncMock, |
| 61 | +) -> None: |
| 62 | + """Test successfully getting multi maps list.""" |
| 63 | + # Setup mock to return the sample multi maps list |
| 64 | + mock_rpc_channel.send_command.return_value = MULTI_MAP_LIST_DATA |
| 65 | + |
| 66 | + # Call the method |
| 67 | + await maps_trait.refresh() |
| 68 | + |
| 69 | + assert maps_trait.max_multi_map == 1 |
| 70 | + assert maps_trait.max_bak_map == 1 |
| 71 | + assert maps_trait.multi_map_count == 1 |
| 72 | + assert maps_trait.map_info |
| 73 | + assert len(maps_trait.map_info) == 2 |
| 74 | + map_infos = maps_trait.map_info |
| 75 | + assert len(map_infos) == 2 |
| 76 | + assert map_infos[0].map_flag == 0 |
| 77 | + assert map_infos[0].name == "Map 1" |
| 78 | + assert map_infos[0].add_time == 1747132930 |
| 79 | + assert map_infos[1].map_flag == 123 |
| 80 | + assert map_infos[1].name == "Map 1" |
| 81 | + assert map_infos[1].add_time == 1747132930 |
| 82 | + |
| 83 | + # Verify the RPC call was made correctly |
| 84 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_MULTI_MAPS_LIST) |
| 85 | + |
| 86 | + |
| 87 | +async def test_set_current_map( |
| 88 | + status_trait: StatusTrait, |
| 89 | + maps_trait: MapsTrait, |
| 90 | + mock_rpc_channel: AsyncMock, |
| 91 | +) -> None: |
| 92 | + """Test successfully setting the current map.""" |
| 93 | + mock_rpc_channel.send_command.side_effect = [ |
| 94 | + mock_data.STATUS, # Initial status fetch |
| 95 | + MULTI_MAP_LIST_DATA, # Response for LOAD_MULTI_MAP |
| 96 | + {}, # Response for setting the current map |
| 97 | + UPDATED_STATUS, # Response for refreshing status |
| 98 | + ] |
| 99 | + await status_trait.refresh() |
| 100 | + |
| 101 | + # First refresh to populate initial state |
| 102 | + await maps_trait.refresh() |
| 103 | + |
| 104 | + # Call the method to set current map |
| 105 | + await maps_trait.set_current_map(123) |
| 106 | + |
| 107 | + # Verify the current map is updated |
| 108 | + assert maps_trait.current_map == 123 |
| 109 | + |
| 110 | + # Verify the RPC call was made correctly to load the map |
| 111 | + mock_rpc_channel.send_command.assert_any_call(RoborockCommand.LOAD_MULTI_MAP, params=[123]) |
| 112 | + # Command sent are: |
| 113 | + # 1. GET_STATUS to get initial status |
| 114 | + # 2. GET_MULTI_MAPS_LIST to get the map list |
| 115 | + # 3. LOAD_MULTI_MAP to set the map |
| 116 | + # 4. GET_STATUS to refresh the current map in status |
| 117 | + assert mock_rpc_channel.send_command.call_count == 4 |
0 commit comments