Skip to content

Commit 54964d7

Browse files
committed
chore: Add end to end tests of the device cache
Use the existing test to also exercise the cache by closing the device manager and reconnecting.
1 parent 620e36b commit 54964d7

File tree

3 files changed

+149
-8
lines changed

3 files changed

+149
-8
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ repos:
2626
rev: v2.2.6
2727
hooks:
2828
- id: codespell
29+
exclude: >
30+
(?x)^(
31+
.*\.ambr
32+
)$
2933
- repo: https://github.com/charliermarsh/ruff-pre-commit
3034
rev: v0.13.2
3135
hooks:

tests/e2e/__snapshots__/test_device_manager.ambr

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,94 @@
200200
00000400 22 54 f5 8f 15 f6 5f 5c 0b f2 7e 71 34 7d d1 12 |"T...._\..~q4}..|
201201
00000410 5c 4c 83 4b d3 d1 37 b7 01 d4 00 04 13 e7 40 c4 |\L.K..7.......@.|
202202
00000420 0d ff 46 6d ef 57 d3 05 18 3d 90 |..Fm.W...=.|
203+
[mqtt >]
204+
00000000 e0 00 |..|
205+
[mqtt <]
206+
00000000 20 09 02 00 06 22 00 0a 21 00 14 | ...."..!..|
207+
[mqtt <]
208+
00000000 90 04 00 01 00 00 |......|
209+
[local >]
210+
00000000 00 00 00 15 31 2e 30 00 00 00 01 00 00 23 8c 68 |....1.0......#.h|
211+
00000010 a6 a2 2c 00 00 b8 95 0e 86 |..,......|
212+
[local <]
213+
00000000 00 00 00 27 31 2e 30 00 00 00 01 00 00 00 17 68 |...'1.0........h|
214+
00000010 a6 a2 2b 00 01 00 10 6d b9 48 37 ed 43 59 7a 90 |..+....m.H7.CYz.|
215+
00000020 ff 43 2f 0a 8f 81 44 e7 b6 b3 85 |.C/...D....|
216+
[local >]
217+
00000000 00 00 00 77 31 2e 30 00 00 23 8e 00 00 23 8f 68 |...w1.0..#...#.h|
218+
00000010 a6 a2 2e 00 04 00 60 a9 a0 ac af 22 80 bb 11 b7 |......`...."....|
219+
00000020 e4 74 fa c3 0e bd c3 d5 a1 f9 a8 1d f1 4e 04 b9 |.t...........N..|
220+
00000030 05 50 39 bc b6 68 62 5b fc 54 1c ce ac c5 df ce |.P9..hb[.T......|
221+
00000040 93 8f 5c 61 d5 4c 66 8b c3 19 e0 cd b4 8f 63 be |..\a.Lf.......c.|
222+
00000050 2b c0 16 46 4e c8 e7 70 d3 a4 de 0f ac 57 5b e6 |+..FN..p.....W[.|
223+
00000060 79 84 2c ab 87 75 c7 7a d7 64 d6 51 3b 5a 04 85 |y.,..u.z.d.Q;Z..|
224+
00000070 38 a4 39 90 99 6f 4c 84 b5 1b ba |8.9..oL....|
225+
[local <]
226+
00000000 00 00 04 27 31 2e 30 00 00 00 02 00 00 00 17 68 |...'1.0........h|
227+
00000010 a6 a2 2d 00 66 04 10 5e 5f 0b 3d ac 87 95 d7 72 |..-.f..^_.=....r|
228+
00000020 96 43 49 74 68 c3 35 e3 c7 bd 9d d2 41 b1 15 99 |.CIth.5.....A...|
229+
00000030 23 aa 66 a2 1a b7 54 da d3 d1 17 a7 d5 96 8c 29 |#.f...T........)|
230+
00000040 1e a0 17 b7 40 ed 89 49 70 0d f9 1e aa b0 68 73 |....@..Ip.....hs|
231+
00000050 6c 0e 82 1e 46 6b b7 7f 75 d6 7e b8 a9 4c d1 57 |l...Fk..u.~..L.W|
232+
00000060 42 95 10 c8 f1 d9 04 9e b9 2c b6 3f dd 1c e5 2f |B........,.?.../|
233+
00000070 08 1f 0f b7 e9 85 5a 34 4b 69 9b 6f 68 dd 45 74 |......Z4Ki.oh.Et|
234+
00000080 d4 bc c6 ad 11 07 98 9f fb aa 7b 67 96 2f 67 60 |..........{g./g`|
235+
00000090 e1 4b 2d 80 72 48 47 09 f0 c3 a2 65 a4 32 3b d2 |.K-.rHG....e.2;.|
236+
000000a0 02 ac 38 76 72 30 27 9d 75 d6 1a aa 05 e5 08 c5 |..8vr0'.u.......|
237+
000000b0 2e c4 3e ab 86 72 6b 61 ce d0 77 1e c4 6f 91 6f |..>..rka..w..o.o|
238+
000000c0 29 d3 19 89 45 21 6b 1e 7a 27 76 e1 1a 71 32 0f |)...E!k.z'v..q2.|
239+
000000d0 af bb 54 7d ea 30 19 e6 95 0b db 0b 88 0b eb 19 |..T}.0..........|
240+
000000e0 18 f1 03 80 3e 2d d3 dd 47 61 93 d1 5a b9 b1 13 |....>-..Ga..Z...|
241+
000000f0 c6 5c b1 f6 52 0a 94 6f 27 05 fe 67 80 3d 3a 51 |.\..R..o'..g.=:Q|
242+
00000100 d6 65 28 10 23 4f 42 9c d6 40 c3 3e c6 64 13 a8 |.e(.#OB..@.>.d..|
243+
00000110 c1 7d 13 17 d2 a0 a1 7b 6d 35 a5 62 73 75 f0 9b |.}.....{m5.bsu..|
244+
00000120 e9 32 96 62 14 0e 97 b3 73 b1 df 48 b4 4c 96 b1 |.2.b....s..H.L..|
245+
00000130 52 8d d5 5b 2f b2 21 f0 e6 81 9b e4 a3 68 11 6c |R..[/.!......h.l|
246+
00000140 bd d8 6c 4c ba a3 1a fd 12 4a c8 e1 39 ce 74 f5 |..lL.....J..9.t.|
247+
00000150 52 71 4d 28 f1 d4 8e a6 68 95 fb e2 b9 f6 62 10 |RqM(....h.....b.|
248+
00000160 a0 51 1b 44 13 53 71 1d 32 94 54 e8 d8 3a d9 d1 |.Q.D.Sq.2.T..:..|
249+
00000170 15 e3 f9 09 91 38 2a ba 4c e1 ca a8 6e 3f cf 38 |.....8*.L...n?.8|
250+
00000180 8a 8f 38 f7 da d1 b4 13 17 df 84 d4 74 0c 2a 65 |..8.........t.*e|
251+
00000190 64 96 ec 5e 9b fb 4d f5 eb ab e8 9a 91 ae 3b d1 |d..^..M.......;.|
252+
000001a0 ac 54 66 a1 18 65 92 f9 7c 67 a0 e2 d2 7e 79 3f |.Tf..e..|g...~y?|
253+
000001b0 5a 9d 31 8d f4 98 cd cf 1b c5 51 04 12 05 34 6c |Z.1.......Q...4l|
254+
000001c0 36 4e 9a 00 2b 10 9c 53 1f cf fe ff 28 d3 ab fe |6N..+..S....(...|
255+
000001d0 dc 7c 85 fb 23 06 00 58 2f 4d 36 fb 1b 04 76 a4 |.|..#..X/M6...v.|
256+
000001e0 7f 73 b8 58 b1 1a 22 2c e6 27 29 09 c7 48 72 f7 |.s.X..",.')..Hr.|
257+
000001f0 63 ce 7b ff 61 60 23 8a 92 06 a0 fa cc fd 6a d0 |c.{.a`#.......j.|
258+
00000200 13 e5 24 33 f7 2c c2 95 06 06 d8 c8 28 21 09 c9 |..$3.,......(!..|
259+
00000210 d8 d5 52 67 a6 81 68 80 00 f1 8b d9 dc bf 16 ea |..Rg..h.........|
260+
00000220 8f 76 2f bd 6e a8 89 77 94 d3 58 52 0e da aa 06 |.v/.n..w..XR....|
261+
00000230 2c 4d 35 89 23 45 8a 58 d0 b1 19 fb d4 05 da f0 |,M5.#E.X........|
262+
00000240 6f 2f 00 e5 11 85 4e 34 a8 55 13 39 a9 2a 46 75 |o/....N4.U.9.*Fu|
263+
00000250 52 e1 1f 66 e3 b8 2a 55 ab b0 ad ea 9b b6 4b 4d |R..f..*U......KM|
264+
00000260 9c 79 9b 09 d1 b6 61 48 89 37 b0 98 3c bf bb 4a |.y....aH.7..<..J|
265+
00000270 f1 22 e2 dd ad e6 3c 98 f6 d0 1f 14 c1 73 ed 7e |."....<......s.~|
266+
00000280 f8 9a fb 4e 64 e9 2f d9 f9 94 58 b1 f4 9d 67 13 |...Nd./...X...g.|
267+
00000290 6d 08 4b 6e 1b ca d6 76 02 3f b0 2c fc 0b 0b 32 |m.Kn...v.?.,...2|
268+
000002a0 ce 38 a1 05 86 d3 db ce dd 06 63 49 e8 c1 d1 41 |.8........cI...A|
269+
000002b0 00 e0 3b 0a ed 7b 7a b1 04 86 a8 bb 1b 18 52 63 |..;..{z.......Rc|
270+
000002c0 db 42 2a c3 41 d8 1c 3c 31 ef 6b 72 7e c2 54 a5 |.B*.A..<1.kr~.T.|
271+
000002d0 20 94 23 b9 8d d0 5e 94 ef 85 b0 73 47 5d 7c f0 | .#...^....sG]|.|
272+
000002e0 ef ac 82 55 13 04 ba ca 4f 1b af fb 5c 0c b9 e7 |...U....O...\...|
273+
000002f0 21 c7 97 75 d3 db 18 51 d1 92 5c 51 97 8a ba 3e |!..u...Q..\Q...>|
274+
00000300 79 80 5c c9 21 8f 6b 8f 04 9e 9a 47 ed ef fe 66 |y.\.!.k....G...f|
275+
00000310 fe d3 76 5b ff dc 31 d9 f9 94 58 b1 f4 9d 67 13 |..v[..1...X...g.|
276+
00000320 6d 08 4b 6e 1b ca d6 7f 0c de c5 f4 a5 0f 49 cf |m.Kn..........I.|
277+
00000330 f9 e5 74 88 96 8a 14 bd 8a 19 0a ce 93 be 30 b8 |..t...........0.|
278+
00000340 35 1a 1e bb f3 0b ed 34 28 7f 73 ca 76 4a 8d 03 |5......4(.s.vJ..|
279+
00000350 81 0a 2c 2c cc fe e8 c6 e2 dd 9d e0 be 45 63 57 |..,,.........EcW|
280+
00000360 ae 34 40 e1 c4 5c 2b ee 40 68 5e d0 35 83 94 ce |.4@..\+.@h^.5...|
281+
00000370 be 2a 27 08 a5 dc 00 d5 b6 af 48 b3 6b 57 99 80 |.*'.......H.kW..|
282+
00000380 ba e8 2d 7e b5 57 4e 91 5e 15 7a 13 af 9d ed 24 |..-~.WN.^.z....$|
283+
00000390 03 6a 35 fb 75 b0 56 36 90 70 ff 59 f9 3f dd 7e |.j5.u.V6.p.Y.?.~|
284+
000003a0 09 1b 05 a1 38 dc 1c 12 5b 9c 4d 82 b2 32 95 1f |....8...[.M..2..|
285+
000003b0 80 4f 29 53 51 9f b3 12 72 1a b4 b9 79 d5 7b 74 |.O)SQ...r...y.{t|
286+
000003c0 99 94 71 90 83 b5 51 1a 47 4e 99 ae 54 78 98 fe |..q...Q.GN..Tx..|
287+
000003d0 38 91 1e 5e 67 72 1b f1 b8 7d 66 83 55 21 94 7c |8..^gr...}f.U!.||
288+
000003e0 d6 79 8b 61 65 0e 0d 46 c4 3b 6f e8 0e f2 ec 4b |.y.ae..F.;o....K|
289+
000003f0 b6 60 24 40 83 2b 94 06 de a8 88 76 d6 c4 7b 17 |.`$@.+.....v..{.|
290+
00000400 77 50 dd e5 02 1f 70 86 1b f5 f5 56 19 5e 54 00 |wP....p....V.^T.|
291+
00000410 cc 52 61 40 f9 d5 02 3c dd 7d 66 3a 06 89 a0 7a |.Ra@...<.}f:...z|
292+
00000420 98 80 37 eb 64 ea ee b2 82 5a 2a |..7.d....Z*|
203293
# ---

tests/e2e/test_device_manager.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import pytest
1717
import syrupy
1818

19+
from roborock.devices.cache import Cache, InMemoryCache
1920
from roborock.devices.device_manager import DeviceManager, UserParams, create_device_manager
2021
from roborock.protocol import MessageParser
2122
from roborock.protocols.v1_protocol import LocalProtocolVersion
@@ -56,16 +57,10 @@ def auto_fast_backoff(fast_backoff_fixture: None) -> None:
5657
def mqtt_server_fixture(mock_paho_mqtt_create_connection: None, mock_paho_mqtt_select: None) -> None:
5758
"""Fixture to mock the MQTT connection.
5859
59-
This is here to pull in the mock socket pixtures into all tests used here.
60+
This is here to pull in the mock socket fixtures into all tests used here.
6061
"""
6162

6263

63-
@pytest.fixture
64-
def auto_deterministic_message_fixtures(deterministic_message_fixtures: None) -> None:
65-
"""Auto-use deterministic message fixtures for all tests in this module."""
66-
pass
67-
68-
6964
@pytest.fixture(autouse=True)
7065
def auto_mock_local_client(mock_async_create_local_connection: None) -> None:
7166
"""Automatically use the mock local client fixture."""
@@ -76,10 +71,11 @@ async def device_manager_factory_fixture() -> AsyncGenerator[Callable[[UserParam
7671
"""Fixture to create a device manager and handle auto shutdown on test failure."""
7772

7873
cleanup_tasks: list[Callable[[], Awaitable[None]]] = []
74+
cache: Cache = InMemoryCache()
7975

8076
async def factory(user_params: UserParams) -> DeviceManager:
8177
"""Create a device manager and auto cleanup."""
82-
device_manager = await create_device_manager(user_params)
78+
device_manager = await create_device_manager(user_params, cache=cache)
8379
cleanup_tasks.append(device_manager.close)
8480
return device_manager
8581

@@ -220,4 +216,55 @@ async def test_device_manager(
220216
assert device.v1_properties.device_features.is_customized_clean_supported
221217
assert not device.v1_properties.device_features.is_matter_supported
222218

219+
# Close the device manager. We will test re-connecting and reusing the network
220+
# information and device discovery information from the cache.
221+
await device_manager.close()
222+
223+
mqtt_responses = [
224+
# MQTT connection response
225+
mqtt_packet.gen_connack(rc=0, flags=2),
226+
# ACK the request to subscribe to the topic
227+
mqtt_packet.gen_suback(mid=1),
228+
# No network info call this time since it should be cached
229+
]
230+
for response in mqtt_responses:
231+
push_mqtt_response(response)
232+
233+
# Prepare local device responses.
234+
local_response_queue.put_nowait(
235+
response_builder.build(protocol=RoborockMessageProtocol.HELLO_RESPONSE, seq=1, payload=b"ok")
236+
)
237+
238+
device_manager = await device_manager_factory(user_params)
239+
240+
# The mocked Home Data API returns a single v1 device
241+
devices = await device_manager.get_devices()
242+
assert len(devices) == 1
243+
device = devices[0]
244+
assert device.duid == "abc123"
245+
assert device.name == "Roborock S7 MaxV"
246+
assert device.is_connected
247+
assert device.is_local_connected
248+
249+
# Verify arbitrary device features from cache
250+
assert device.v1_properties
251+
assert device.v1_properties.device_features
252+
assert device.v1_properties.device_features.is_show_clean_finish_reason_supported
253+
assert device.v1_properties.device_features.is_customized_clean_supported
254+
assert not device.v1_properties.device_features.is_matter_supported
255+
256+
# In the previous test, the dock information is fetched and has the side effect of
257+
# populating the status trait. This test gets dock information from the cache so
258+
# we have to manually refresh status the first time (like other traits).
259+
assert device.v1_properties
260+
assert device.v1_properties.status
261+
assert device.v1_properties.status.state_name is None
262+
263+
# Exercise a GET_STATUS call. id is deterministic based on deterministic_message_fixtures
264+
local_response_queue.put_nowait(response_builder.build_rpc(data={"id": 9101, "result": [mock_data.STATUS]}))
265+
266+
# Verify GET_STATUS response
267+
await device.v1_properties.status.refresh()
268+
assert device.v1_properties.status.state_name == "charging"
269+
223270
assert snapshot == log

0 commit comments

Comments
 (0)