|
1 | 1 | """Tests for customized Q10 room-cleaning commands and pushed settings.""" |
2 | 2 |
|
3 | 3 | import base64 |
| 4 | +import pathlib |
| 5 | +from dataclasses import replace |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
@@ -52,7 +54,7 @@ async def test_set_settings_publishes_then_requests_full_settings( |
52 | 54 | settings = (_settings(),) |
53 | 55 | await q10_api.room_cleaning.set_settings(settings) |
54 | 56 | assert fake_channel.published_commands == [ |
55 | | - (B01_Q10_DP.COMMON, {"62": encode_room_clean_settings(settings)}), |
| 57 | + (B01_Q10_DP.COMMON, {"62": "AQMDAwECAg=="}), |
56 | 58 | (B01_Q10_DP.COMMON, {"63": 0}), |
57 | 59 | ] |
58 | 60 | assert q10_api.room_cleaning.settings is None |
@@ -137,3 +139,127 @@ def test_first_empty_push_is_reported(q10_api: Q10PropertiesApi) -> None: |
137 | 139 | assert trait.settings == [] |
138 | 140 | assert trait.as_dict() == {"settings": []} |
139 | 141 | assert notified == [True] |
| 142 | + |
| 143 | + |
| 144 | +@pytest.mark.parametrize("complete", [False, True]) |
| 145 | +@pytest.mark.parametrize( |
| 146 | + ("field", "compact_offset", "full_offset", "width"), |
| 147 | + [ |
| 148 | + ("fan_level", 1, 8, 1), |
| 149 | + ("water_level", 2, 9, 1), |
| 150 | + ("clean_type", 3, 7, 1), |
| 151 | + ("clean_count", 4, 5, 2), |
| 152 | + ("clean_line", 5, 11, 1), |
| 153 | + ], |
| 154 | +) |
| 155 | +def test_unknown_mode_codes_use_enum_fallback( |
| 156 | + q10_api: Q10PropertiesApi, field: str, compact_offset: int, full_offset: int, width: int, complete: bool |
| 157 | +) -> None: |
| 158 | + trait = q10_api.room_cleaning |
| 159 | + payload = _complete_payload(_settings()) if complete else "AQMDAwECAg==" |
| 160 | + raw = bytearray(base64.b64decode(payload)) |
| 161 | + offset = full_offset + 1 if complete else compact_offset + 1 |
| 162 | + code_width = width if complete else 1 |
| 163 | + raw[offset : offset + code_width] = (99).to_bytes(code_width, "big") |
| 164 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: base64.b64encode(raw).decode()}) |
| 165 | + expected = _settings() |
| 166 | + setattr(expected, field, type(getattr(expected, field)).UNKNOWN) |
| 167 | + assert trait.settings == [expected] |
| 168 | + # A compact update must preserve the cached unknown code during conversion. |
| 169 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: "AQkDAwECAg=="}) |
| 170 | + assert trait.settings == [expected, _settings(9)] |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.parametrize("complete", [False, True]) |
| 174 | +def test_zero_mode_codes_are_preserved(q10_api: Q10PropertiesApi, complete: bool) -> None: |
| 175 | + settings = replace(_settings(), water_level=YXWaterLevel.OFF, clean_line=YXCleanLine.FAST) |
| 176 | + payload = _complete_payload(settings) if complete else "AQMDAAECAA==" |
| 177 | + q10_api.room_cleaning.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: payload}) |
| 178 | + assert q10_api.room_cleaning.settings == [settings] |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.parametrize("populated", [False, True]) |
| 182 | +@pytest.mark.parametrize("payload", [None, 1, {}, [], "invalid", "", "AQAA"]) |
| 183 | +def test_invalid_push_does_not_change_settings(q10_api: Q10PropertiesApi, populated: bool, payload: object) -> None: |
| 184 | + trait = q10_api.room_cleaning |
| 185 | + if populated: |
| 186 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: "AQMDAwECAg=="}) |
| 187 | + before = trait.settings |
| 188 | + notified: list[bool] = [] |
| 189 | + trait.add_update_listener(lambda: notified.append(True)) |
| 190 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: payload}) |
| 191 | + trait.update_from_dps({B01_Q10_DP.BATTERY: 100}) |
| 192 | + assert trait.settings == before |
| 193 | + assert not notified |
| 194 | + |
| 195 | + |
| 196 | +def test_malformed_later_record_is_atomic(q10_api: Q10PropertiesApi) -> None: |
| 197 | + trait = q10_api.room_cleaning |
| 198 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: "AQMDAwECAg=="}) |
| 199 | + notified: list[bool] = [] |
| 200 | + trait.add_update_listener(lambda: notified.append(True)) |
| 201 | + first = base64.b64decode(_complete_payload(_settings(9)))[1:] |
| 202 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: base64.b64encode(b"\x02" + first + b"\x00").decode()}) |
| 203 | + assert trait.settings == [_settings(3)] |
| 204 | + assert not notified |
| 205 | + |
| 206 | + |
| 207 | +def test_compact_update_preserves_existing_room_order(q10_api: Q10PropertiesApi) -> None: |
| 208 | + trait = q10_api.room_cleaning |
| 209 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: "AgMDAwECAgkDAwECAg=="}) |
| 210 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: "AgkEAwECAgEBAwECAg=="}) |
| 211 | + assert trait.settings == [ |
| 212 | + _settings(3), |
| 213 | + _settings(9, fan=Q10RoomFanLevel.MAX), |
| 214 | + _settings(1, fan=Q10RoomFanLevel.QUIET), |
| 215 | + ] |
| 216 | + |
| 217 | + |
| 218 | +def test_sanitized_full_capture_builds_typed_models(q10_api: Q10PropertiesApi) -> None: |
| 219 | + payload = ( |
| 220 | + pathlib.Path("tests/protocols/testdata/b01_q10_protocol/customer_clean_full_sanitized.b64").read_text().strip() |
| 221 | + ) |
| 222 | + q10_api.room_cleaning.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: payload}) |
| 223 | + assert q10_api.room_cleaning.settings == [ |
| 224 | + _settings(1, fan=Q10RoomFanLevel.QUIET), |
| 225 | + _settings(2, fan=Q10RoomFanLevel.MAX_PLUS), |
| 226 | + replace( |
| 227 | + _settings(3, fan=Q10RoomFanLevel.MAX_PLUS), |
| 228 | + water_level=YXWaterLevel.MEDIUM, |
| 229 | + clean_type=Q10RoomCleanType.VACUUM, |
| 230 | + ), |
| 231 | + replace( |
| 232 | + _settings(4, fan=Q10RoomFanLevel.MAX_PLUS), |
| 233 | + water_level=YXWaterLevel.MEDIUM, |
| 234 | + clean_type=Q10RoomCleanType.VACUUM, |
| 235 | + ), |
| 236 | + _settings(5, fan=Q10RoomFanLevel.QUIET), |
| 237 | + _settings(6, fan=Q10RoomFanLevel.MAX_PLUS), |
| 238 | + ] |
| 239 | + |
| 240 | + |
| 241 | +@pytest.mark.parametrize("method", ["set_settings", "clean_rooms"]) |
| 242 | +async def test_commands_leave_reported_settings_unchanged(q10_api: Q10PropertiesApi, method: str) -> None: |
| 243 | + q10_api.room_cleaning.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: "AQMDAwECAg=="}) |
| 244 | + target = q10_api.room_cleaning if method == "set_settings" else q10_api.vacuum |
| 245 | + await getattr(target, method)((_settings(9),)) |
| 246 | + assert q10_api.room_cleaning.settings == [_settings(3)] |
| 247 | + |
| 248 | + |
| 249 | +def test_populated_diagnostics_use_model_field_names_and_enum_labels(q10_api: Q10PropertiesApi) -> None: |
| 250 | + trait = q10_api.room_cleaning |
| 251 | + trait.update_from_dps({B01_Q10_DP.CUSTOMER_CLEAN: "AQMDAwECAg=="}) |
| 252 | + expected = { |
| 253 | + "settings": [ |
| 254 | + { |
| 255 | + "roomId": 3, |
| 256 | + "fanLevel": "turbo", |
| 257 | + "waterLevel": "high", |
| 258 | + "cleanType": "vac_and_mop", |
| 259 | + "cleanCount": "twice", |
| 260 | + "cleanLine": "fine", |
| 261 | + } |
| 262 | + ] |
| 263 | + } |
| 264 | + assert trait.as_dict() == expected |
| 265 | + assert q10_api.as_dict()["room_cleaning"] == expected |
0 commit comments