-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_manage_components.py
More file actions
332 lines (266 loc) · 9.18 KB
/
test_manage_components.py
File metadata and controls
332 lines (266 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
Tests for the manage_components tool.
This tool handles component lifecycle operations (add, remove, set_property).
"""
import pytest
from .test_helpers import DummyContext
import services.tools.manage_components as manage_comp_mod
@pytest.mark.asyncio
async def test_manage_components_add_single(monkeypatch):
"""Test adding a single component."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["cmd"] = cmd
captured["params"] = params
return {
"success": True,
"data": {
"addedComponents": [{"typeName": "UnityEngine.Rigidbody", "instanceID": 12345}]
},
}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="add",
target="Player",
component_type="Rigidbody",
)
assert resp.get("success") is True
assert captured["cmd"] == "manage_components"
assert captured["params"]["action"] == "add"
assert captured["params"]["target"] == "Player"
assert captured["params"]["componentType"] == "Rigidbody"
@pytest.mark.asyncio
async def test_manage_components_remove(monkeypatch):
"""Test removing a component."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {"instanceID": 12345, "name": "Player"}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="remove",
target="Player",
component_type="Rigidbody",
)
assert resp.get("success") is True
assert captured["params"]["action"] == "remove"
assert captured["params"]["componentType"] == "Rigidbody"
@pytest.mark.asyncio
async def test_manage_components_set_property_single(monkeypatch):
"""Test setting a single component property."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {"instanceID": 12345}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="set_property",
target="Player",
component_type="Rigidbody",
property="mass",
value=5.0,
)
assert resp.get("success") is True
assert captured["params"]["action"] == "set_property"
assert captured["params"]["property"] == "mass"
assert captured["params"]["value"] == 5.0
@pytest.mark.asyncio
async def test_manage_components_set_property_multiple(monkeypatch):
"""Test setting multiple component properties via properties dict."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {"instanceID": 12345}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="set_property",
target="Player",
component_type="Rigidbody",
properties={"mass": 5.0, "drag": 0.5},
)
assert resp.get("success") is True
assert captured["params"]["action"] == "set_property"
assert captured["params"]["properties"] == {"mass": 5.0, "drag": 0.5}
@pytest.mark.asyncio
async def test_manage_components_set_property_json_string(monkeypatch):
"""Test setting component properties with JSON string input."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {"instanceID": 12345}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="set_property",
target="Player",
component_type="Rigidbody",
properties='{"mass": 10.0}', # JSON string
)
assert resp.get("success") is True
assert captured["params"]["properties"] == {"mass": 10.0}
@pytest.mark.asyncio
@pytest.mark.parametrize(
("property_name", "raw_value", "expected_value"),
[
(
"position",
"[1.0, 2.0, 3.0]",
[1.0, 2.0, 3.0],
),
(
"color",
'{"r": 1.0, "g": 1.0, "b": 0.0, "a": 1.0}',
{"r": 1.0, "g": 1.0, "b": 0.0, "a": 1.0},
),
],
)
async def test_manage_components_set_property_single_structured_json_value(
monkeypatch,
property_name,
raw_value,
expected_value,
):
"""Test JSON-string single values are normalized before dispatch.
The Python-side contract is intentionally generic:
- array-shaped JSON should become Python lists
- object-shaped JSON should become Python dicts
Detailed Unity struct compatibility is covered by Unity-side tests.
"""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {"instanceID": 12345}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="set_property",
target="TestObject",
component_type="Transform",
property=property_name,
value=raw_value,
)
assert resp.get("success") is True
assert captured["params"]["property"] == property_name
assert captured["params"]["value"] == expected_value
@pytest.mark.asyncio
async def test_manage_components_set_property_single_plain_string_value_stays_string(monkeypatch):
"""Test ordinary string values are forwarded unchanged.
This guards the conservative behavior of parse_json_payload: plain strings
should not be coerced just because structured JSON-string values are now supported.
"""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {"instanceID": 12345}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="set_property",
target="GameManager",
component_type="ExampleComponent",
property="displayName",
value="Player One",
)
assert resp.get("success") is True
assert captured["params"]["property"] == "displayName"
assert captured["params"]["value"] == "Player One"
assert isinstance(captured["params"]["value"], str)
@pytest.mark.asyncio
async def test_manage_components_add_with_properties(monkeypatch):
"""Test adding a component with initial properties."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {
"success": True,
"data": {"addedComponents": [{"typeName": "Rigidbody", "instanceID": 123}]},
}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="add",
target="Player",
component_type="Rigidbody",
properties={"mass": 2.0, "useGravity": False},
)
assert resp.get("success") is True
assert captured["params"]["properties"] == {"mass": 2.0, "useGravity": False}
@pytest.mark.asyncio
async def test_manage_components_search_method_passthrough(monkeypatch):
"""Test that search_method is correctly passed through."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="add",
target="Canvas/Panel",
component_type="Image",
search_method="by_path",
)
assert resp.get("success") is True
assert captured["params"]["searchMethod"] == "by_path"
@pytest.mark.asyncio
async def test_manage_components_target_by_id(monkeypatch):
"""Test targeting by instance ID."""
captured = {}
async def fake_send(cmd, params, **kwargs):
captured["params"] = params
return {"success": True, "data": {}}
monkeypatch.setattr(
manage_comp_mod,
"async_send_command_with_retry",
fake_send,
)
resp = await manage_comp_mod.manage_components(
ctx=DummyContext(),
action="add",
target=12345, # Integer instance ID
component_type="BoxCollider",
search_method="by_id",
)
assert resp.get("success") is True
assert captured["params"]["target"] == 12345
assert captured["params"]["searchMethod"] == "by_id"