-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_manage_components.py
More file actions
346 lines (282 loc) · 9.48 KB
/
test_manage_components.py
File metadata and controls
346 lines (282 loc) · 9.48 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
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
async def test_manage_components_set_property_single_json_value(monkeypatch):
"""Test Color-style object payloads are parsed from JSON strings."""
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="Light",
component_type="Light",
property="color",
value='{"r": 1.0, "g": 1.0, "b": 0.0, "a": 1.0}',
)
assert resp.get("success") is True
assert captured["params"]["property"] == "color"
assert captured["params"]["value"] == {
"r": 1.0,
"g": 1.0,
"b": 0.0,
"a": 1.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],
),
(
"localScale",
'{"x": 2.0, "y": 3.0, "z": 4.0}',
{"x": 2.0, "y": 3.0, "z": 4.0},
),
(
"rotation",
'{"x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0}',
{"x": 0.0, "y": 0.0, "z": 0.0, "w": 1.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},
),
(
"pixelRect",
'{"x": 10.0, "y": 20.0, "width": 1920.0, "height": 1080.0}',
{"x": 10.0, "y": 20.0, "width": 1920.0, "height": 1080.0},
),
],
)
async def test_manage_components_set_property_single_json_value_for_unity_structs(
monkeypatch,
property_name,
raw_value,
expected_value,
):
"""Test JSON-string single values preserve the intended Unity struct shape.
These cases document the payload forms we rely on:
- Vector3 accepts array or object JSON
- Quaternion accepts object JSON
- Color and Rect should use object JSON matching Unity field names
"""
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_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"