Skip to content

Commit 8bb66ae

Browse files
committed
Add test cases
1 parent b9865e1 commit 8bb66ae

2 files changed

Lines changed: 274 additions & 2 deletions

File tree

deebot_client/commands/xml/map.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
8888
xml.attrib.get("ret") != "ok"
8989
or not (msid := xml.attrib.get("msid"))
9090
or not (area_type := xml.attrib.get("tp"))
91-
or not (m := xml.findall("m"))
91+
or not MapSetType.has_value(area_type)
9292
):
9393
return HandlingResult.analyse()
94-
subsets = cls._find_subsets(m)
94+
95+
xml_subsets = xml.findall("m")
96+
subsets = cls._find_subsets(xml_subsets)
9597
event_bus.notify(MapSetEvent(MapSetType(area_type), subsets=subsets))
9698
args = {
9799
cls._ARGS_MSID: msid,

tests/commands/xml/test_map.py

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from deebot_client.command import Command, CommandResult
6+
from deebot_client.commands.xml import (
7+
GetMapM,
8+
GetMapSet,
9+
GetMapSt,
10+
GetTrM,
11+
PullM,
12+
PullMP,
13+
)
14+
from deebot_client.events import (
15+
CachedMapInfoEvent,
16+
MajorMapEvent,
17+
MapSetEvent,
18+
MapSetType,
19+
MapSubsetEvent,
20+
MinorMapEvent,
21+
)
22+
from deebot_client.message import HandlingState
23+
from tests.commands import assert_command
24+
from tests.commands.xml import get_request_xml
25+
26+
27+
@pytest.mark.parametrize(
28+
("built_flag", "expected_result"),
29+
[("built", True), ("not built", False)],
30+
ids=["built", "not_built"],
31+
)
32+
async def test_GetMapSt(built_flag: str, expected_result: bool) -> None:
33+
json = get_request_xml(f"<ctl ret='ok' st='{built_flag}' method='auto'/>")
34+
await assert_command(
35+
GetMapSt(), json, CachedMapInfoEvent(name="", active=expected_result)
36+
)
37+
38+
39+
@pytest.mark.parametrize(
40+
"xml",
41+
["<ctl ret='error'/>", "<ctl ret='ok'/>"],
42+
ids=["error", "no_state"],
43+
)
44+
async def test_GetMapSt_error(xml: str) -> None:
45+
json = get_request_xml(xml)
46+
await assert_command(
47+
GetMapSt(),
48+
json,
49+
None,
50+
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
51+
)
52+
53+
54+
@pytest.mark.parametrize(
55+
("xml", "map_type", "subsets"),
56+
[
57+
(
58+
'<ctl ret="ok" tp="vw" msid="1"><m mid="1" p="1" /><m mid="2" p="1" /></ctl>',
59+
MapSetType.VIRTUAL_WALLS,
60+
[1, 2],
61+
),
62+
(
63+
'<ctl ret="ok" tp="ar" msid="1" />',
64+
MapSetType.ROOMS.value,
65+
[],
66+
),
67+
(
68+
'<ctl ret="ok" tp="mw" msid="1" ><m p="1" /><m mid="3" p="1" /></ctl>',
69+
MapSetType.NO_MOP_ZONES.value,
70+
[3],
71+
),
72+
],
73+
ids=["virtual_walls", "rooms", "no_mop"],
74+
)
75+
async def test_GetMapSet(
76+
xml: str, map_type: MapSetType | str, subsets: list[int]
77+
) -> None:
78+
json = get_request_xml(xml)
79+
type_as_enum, type_as_str = (
80+
(map_type, map_type.value)
81+
if isinstance(map_type, MapSetType)
82+
else (MapSetType(map_type), map_type)
83+
)
84+
requested_commands: list[Command] = [
85+
PullM(mid=subset, msid="1", type=type_as_enum) for subset in subsets
86+
]
87+
await assert_command(
88+
GetMapSet("unused", type=map_type),
89+
json,
90+
MapSetEvent(
91+
map_type if isinstance(map_type, MapSetType) else MapSetType(map_type),
92+
subsets=subsets,
93+
),
94+
command_result=CommandResult(
95+
HandlingState.SUCCESS,
96+
args={
97+
GetMapSet._ARGS_MSID: "1",
98+
GetMapSet._ARGS_TYPE: type_as_str,
99+
GetMapSet._ARGS_SUBSETS: subsets,
100+
},
101+
requested_commands=requested_commands,
102+
),
103+
)
104+
105+
106+
@pytest.mark.parametrize(
107+
"xml",
108+
[
109+
"<ctl ret='error'/>",
110+
"<ctl ret='ok'/>",
111+
"<ctl ret='ok' msid='1'/>",
112+
"<ctl ret='ok' tp='mw'/>",
113+
'<ctl ret="ok" tp="???" msid="1" />',
114+
],
115+
ids=["error", "no_state_1", "no_state_2", "no_state_3", "invalid_type"],
116+
)
117+
async def test_GetMapSet_error(xml: str) -> None:
118+
json = get_request_xml(xml)
119+
await assert_command(
120+
GetMapSet("unused"),
121+
json,
122+
None,
123+
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
124+
)
125+
126+
127+
@pytest.mark.parametrize(
128+
("index", "map_hash"),
129+
[
130+
("1245233875", [1295764014, 1295764014]),
131+
],
132+
ids=["success"],
133+
)
134+
async def test_GetMapM(index: str, map_hash: list[int]) -> None:
135+
json = get_request_xml(
136+
f'<ctl i="{index}" w="100" h="100" r="8" c="8" p="50" m="{",".join([str(x) for x in map_hash])}"/>'
137+
)
138+
await assert_command(
139+
GetMapM(),
140+
json,
141+
MajorMapEvent(index, values=map_hash, requested=True),
142+
)
143+
144+
145+
@pytest.mark.parametrize(
146+
"xml",
147+
[
148+
"<ctl />",
149+
"<ctl i='1'/>",
150+
"<ctl m='42'/>",
151+
],
152+
ids=["no_state_1", "no_state_2", "no_state_3"],
153+
)
154+
async def test_GetMapM_error(xml: str) -> None:
155+
json = get_request_xml(xml)
156+
await assert_command(
157+
GetMapM(),
158+
json,
159+
None,
160+
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
161+
)
162+
163+
164+
@pytest.mark.parametrize(
165+
("coordinates", "map_type"),
166+
[
167+
("[751,-960,751,-1242,1118,-1242,1118,-960]", MapSetType.ROOMS),
168+
(
169+
"-3000,-4400;-3000,-3650;-2450,-3500;-2450,-2400;-2350,-2300;-2350,-1500;-1750,-1500;-1650,-1600;-1250,-1550;-1250,-2300;-1350,-2300;-1600,-2550;-1500,-2750;-1500,-3850;-1750,-3850;-2100,-4200;-2100,-4450;-2150,-4400;-3000,-4400",
170+
MapSetType.VIRTUAL_WALLS.value,
171+
),
172+
],
173+
ids=["array_data", "string_data"],
174+
)
175+
async def test_PullM(coordinates: str, map_type: MapSetType | str) -> None:
176+
json = get_request_xml(f"<ctl ret='ok' m='{coordinates}'/>")
177+
map_type_enum = (
178+
map_type if isinstance(map_type, MapSetType) else MapSetType(map_type)
179+
)
180+
expected_event = MapSubsetEvent(id=1, type=map_type_enum, coordinates=coordinates)
181+
await assert_command(
182+
PullM(mid=1, msid=2, type=map_type),
183+
json,
184+
expected_event,
185+
command_result=CommandResult(
186+
HandlingState.SUCCESS, {PullM._ARG_COORDS: expected_event.coordinates}
187+
),
188+
)
189+
190+
191+
@pytest.mark.parametrize(
192+
"xml",
193+
[
194+
"<ctl ret='error'/>",
195+
"<ctl ret='ok'/>",
196+
],
197+
ids=["error", "no_state"],
198+
)
199+
async def test_PullM_error(xml: str) -> None:
200+
json = get_request_xml(xml)
201+
await assert_command(
202+
PullM(mid=1, msid=1),
203+
json,
204+
None,
205+
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
206+
)
207+
208+
209+
@pytest.mark.parametrize(
210+
("xml", "expected_event"),
211+
[("<ctl ret='ok' p='base64data' />", MinorMapEvent(index=1, value="base64data"))],
212+
ids=["base64_data"],
213+
)
214+
async def test_PullMP(xml: str, expected_event: MinorMapEvent) -> None:
215+
json = get_request_xml(xml)
216+
await assert_command(
217+
PullMP(piece_index=1),
218+
json,
219+
expected_event,
220+
command_result=CommandResult(
221+
HandlingState.SUCCESS, {PullMP._ARG_PIECE: expected_event.value}
222+
),
223+
)
224+
225+
226+
@pytest.mark.parametrize(
227+
"xml",
228+
[
229+
"<ctl ret='error'/>",
230+
"<ctl ret='ok'/>",
231+
],
232+
ids=["error", "no_state"],
233+
)
234+
async def test_PullMP_error(xml: str) -> None:
235+
json = get_request_xml(xml)
236+
await assert_command(
237+
PullMP(piece_index=1),
238+
json,
239+
None,
240+
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
241+
)
242+
243+
244+
@pytest.mark.parametrize(
245+
"xml",
246+
["<ctl ret='ok'/>"],
247+
ids=["trace_enabled"],
248+
)
249+
async def test_GetTrM(xml: str) -> None:
250+
json = get_request_xml(xml)
251+
await assert_command(
252+
GetTrM(),
253+
json,
254+
None,
255+
)
256+
257+
258+
@pytest.mark.parametrize(
259+
"xml",
260+
["<ctl ret='error'/>"],
261+
ids=["error"],
262+
)
263+
async def test_GetTrM_error(xml: str) -> None:
264+
json = get_request_xml(xml)
265+
await assert_command(
266+
GetTrM(),
267+
json,
268+
None,
269+
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
270+
)

0 commit comments

Comments
 (0)