-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtest_run.py
More file actions
471 lines (398 loc) · 17.9 KB
/
test_run.py
File metadata and controls
471 lines (398 loc) · 17.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import io
import json
import os
import tempfile
import urllib.error
from unittest.mock import MagicMock, patch
import pytest
import typer
from websocket import WebSocketException, WebSocketTimeoutException
from comfy_cli.command.run import (
WorkflowConverterUnavailable,
WorkflowExecution,
convert_ui_workflow_via_server,
execute,
is_ui_workflow,
load_api_workflow,
)
@pytest.fixture
def workflow():
return {
"1": {
"class_type": "EmptyLatentImage",
"inputs": {"width": 64, "height": 64, "batch_size": 1},
"_meta": {"title": "Empty Latent"},
},
"2": {
"class_type": "PreviewAny",
"inputs": {"source": ["1", 0]},
"_meta": {"title": "Preview"},
},
}
@pytest.fixture
def workflow_file(workflow):
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(workflow, f)
f.flush()
yield f.name
os.unlink(f.name)
@pytest.fixture
def mock_execution(workflow):
progress = MagicMock()
progress.add_task.return_value = 0
return WorkflowExecution(
workflow=workflow,
host="127.0.0.1",
port=8188,
verbose=False,
progress=progress,
local_paths=False,
timeout=30,
)
def _make_msg(msg_type, prompt_id, **data_fields):
return json.dumps({"type": msg_type, "data": {"prompt_id": prompt_id, **data_fields}})
class TestLoadApiWorkflow:
def test_valid_api_workflow(self, workflow_file):
result = load_api_workflow(workflow_file)
assert result is not None
assert "1" in result
assert result["1"]["class_type"] == "EmptyLatentImage"
def test_rejects_ui_workflow(self):
ui_workflow = {"nodes": [], "links": []}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(ui_workflow, f)
f.flush()
result = load_api_workflow(f.name)
os.unlink(f.name)
assert result is None
def test_rejects_invalid_node(self):
bad_workflow = {"1": {"not_class_type": "Foo"}}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(bad_workflow, f)
f.flush()
result = load_api_workflow(f.name)
os.unlink(f.name)
assert result is None
class TestIsUiWorkflow:
def test_detects_ui_workflow(self):
assert is_ui_workflow({"nodes": [{"id": 1}], "links": []})
def test_rejects_api_workflow(self):
assert not is_ui_workflow({"1": {"class_type": "X", "inputs": {}}})
def test_rejects_non_dict(self):
assert not is_ui_workflow(["nodes", "links"])
assert not is_ui_workflow(None)
def test_requires_both_keys(self):
assert not is_ui_workflow({"nodes": []})
assert not is_ui_workflow({"links": []})
def _make_http_error(code: int, body: bytes = b"") -> urllib.error.HTTPError:
return urllib.error.HTTPError(
url="http://127.0.0.1:8188/workflow/convert",
code=code,
msg=f"HTTP {code}",
hdrs=None,
fp=io.BytesIO(body),
)
class TestConvertUiWorkflowViaServer:
UI = {"nodes": [{"id": 1, "type": "X"}], "links": []}
CONVERTED = {"1": {"class_type": "X", "inputs": {}}}
def test_returns_api_format_on_success(self):
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps(self.CONVERTED).encode()
with patch("comfy_cli.command.run.request.urlopen", return_value=mock_resp) as mock_open:
result = convert_ui_workflow_via_server(self.UI, "127.0.0.1", 8188, timeout=30)
assert result == self.CONVERTED
sent_req = mock_open.call_args[0][0]
assert sent_req.full_url == "http://127.0.0.1:8188/workflow/convert"
assert json.loads(sent_req.data) == self.UI
@pytest.mark.parametrize("code", [404, 405])
def test_raises_unavailable_on_missing_endpoint(self, code):
with patch("comfy_cli.command.run.request.urlopen", side_effect=_make_http_error(code)):
with pytest.raises(WorkflowConverterUnavailable):
convert_ui_workflow_via_server(self.UI, "127.0.0.1", 8188, timeout=30)
def test_raises_typer_exit_on_server_error(self):
err = _make_http_error(500, b"conversion blew up")
with patch("comfy_cli.command.run.request.urlopen", side_effect=err):
with pytest.raises(typer.Exit) as exc_info:
convert_ui_workflow_via_server(self.UI, "127.0.0.1", 8188, timeout=30)
assert exc_info.value.exit_code == 1
def test_raises_typer_exit_on_network_error(self):
with patch(
"comfy_cli.command.run.request.urlopen",
side_effect=urllib.error.URLError("Connection refused"),
):
with pytest.raises(typer.Exit) as exc_info:
convert_ui_workflow_via_server(self.UI, "127.0.0.1", 8188, timeout=30)
assert exc_info.value.exit_code == 1
def test_raises_typer_exit_on_invalid_json(self):
mock_resp = MagicMock()
mock_resp.read.return_value = b"<html>not json</html>"
with patch("comfy_cli.command.run.request.urlopen", return_value=mock_resp):
with pytest.raises(typer.Exit) as exc_info:
convert_ui_workflow_via_server(self.UI, "127.0.0.1", 8188, timeout=30)
assert exc_info.value.exit_code == 1
def test_raises_typer_exit_on_non_object_response(self):
mock_resp = MagicMock()
mock_resp.read.return_value = b'["not", "an", "object"]'
with patch("comfy_cli.command.run.request.urlopen", return_value=mock_resp):
with pytest.raises(typer.Exit) as exc_info:
convert_ui_workflow_via_server(self.UI, "127.0.0.1", 8188, timeout=30)
assert exc_info.value.exit_code == 1
class TestWatchExecution:
def test_successful_execution(self, mock_execution):
prompt_id = "test-prompt"
mock_execution.prompt_id = prompt_id
messages = [
_make_msg("executing", prompt_id, node="1"),
_make_msg("executed", prompt_id, node="1"),
_make_msg("executing", prompt_id, node="2"),
_make_msg("executed", prompt_id, node="2"),
_make_msg("executing", prompt_id, node=None),
]
mock_ws = MagicMock()
mock_ws.recv.side_effect = messages
mock_execution.ws = mock_ws
mock_execution.watch_execution()
assert len(mock_execution.remaining_nodes) == 0
def test_skips_other_prompt_messages(self, mock_execution):
prompt_id = "my-prompt"
mock_execution.prompt_id = prompt_id
messages = [
_make_msg("executing", "other-prompt", node="1"),
_make_msg("executing", prompt_id, node=None),
]
mock_ws = MagicMock()
mock_ws.recv.side_effect = messages
mock_execution.ws = mock_ws
mock_execution.watch_execution()
assert "1" in mock_execution.remaining_nodes
def test_unknown_node_ids_do_not_crash(self, mock_execution):
prompt_id = "test-prompt"
mock_execution.prompt_id = prompt_id
messages = [
_make_msg("executing", prompt_id, node="1"),
_make_msg("executing", prompt_id, node="406.0.0.428"),
json.dumps(
{"type": "progress", "data": {"prompt_id": prompt_id, "node": "406.0.0.428", "value": 5, "max": 10}}
),
_make_msg("executed", prompt_id, node="406.0.0.428"),
json.dumps({"type": "execution_cached", "data": {"prompt_id": prompt_id, "nodes": ["999"]}}),
_make_msg("executing", prompt_id, node=None),
]
mock_ws = MagicMock()
mock_ws.recv.side_effect = messages
mock_execution.ws = mock_ws
mock_execution.watch_execution()
def test_unknown_node_ids_verbose(self, workflow):
prompt_id = "test-prompt"
progress = MagicMock()
progress.add_task.return_value = 0
execution = WorkflowExecution(
workflow=workflow,
host="127.0.0.1",
port=8188,
verbose=True,
progress=progress,
local_paths=False,
timeout=30,
)
execution.prompt_id = prompt_id
messages = [
_make_msg("executing", prompt_id, node="406.0.0.428"),
json.dumps({"type": "execution_cached", "data": {"prompt_id": prompt_id, "nodes": ["999"]}}),
_make_msg("executing", prompt_id, node=None),
]
mock_ws = MagicMock()
mock_ws.recv.side_effect = messages
execution.ws = mock_ws
execution.watch_execution()
def test_collects_image_outputs(self, mock_execution):
prompt_id = "test-prompt"
mock_execution.prompt_id = prompt_id
executed_msg = json.dumps(
{
"type": "executed",
"data": {
"prompt_id": prompt_id,
"node": "2",
"output": {
"images": [{"filename": "result.png", "subfolder": "", "type": "output"}],
},
},
}
)
messages = [
_make_msg("executing", prompt_id, node="2"),
executed_msg,
_make_msg("executing", prompt_id, node=None),
]
mock_ws = MagicMock()
mock_ws.recv.side_effect = messages
mock_execution.ws = mock_ws
mock_execution.watch_execution()
assert len(mock_execution.outputs) == 1
assert "result.png" in mock_execution.outputs[0]
class TestExecuteErrorHandling:
def _run_execute_expect_exit(self, workflow_file, **overrides):
kwargs = dict(host="127.0.0.1", port=8188, wait=True, verbose=False, local_paths=False, timeout=30)
kwargs.update(overrides)
with pytest.raises(typer.Exit) as exc_info:
execute(workflow_file, **kwargs)
return exc_info.value.exit_code
def test_timeout_exits_with_code_1(self, workflow_file):
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.ExecutionProgress"),
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_exec = MagicMock()
MockExec.return_value = mock_exec
mock_exec.watch_execution.side_effect = WebSocketTimeoutException("timed out")
code = self._run_execute_expect_exit(workflow_file)
assert code == 1
def test_connection_error_exits_with_code_1(self, workflow_file):
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.ExecutionProgress"),
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_exec = MagicMock()
MockExec.return_value = mock_exec
mock_exec.connect.side_effect = ConnectionError("Connection refused")
code = self._run_execute_expect_exit(workflow_file)
assert code == 1
def test_websocket_exception_exits_with_code_1(self, workflow_file):
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.ExecutionProgress"),
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_exec = MagicMock()
MockExec.return_value = mock_exec
mock_exec.watch_execution.side_effect = WebSocketException("Connection lost")
code = self._run_execute_expect_exit(workflow_file)
assert code == 1
def test_successful_execution(self, workflow_file):
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.ExecutionProgress") as MockProgress,
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_progress = MagicMock()
MockProgress.return_value = mock_progress
mock_exec = MagicMock()
MockExec.return_value = mock_exec
mock_exec.outputs = []
execute(workflow_file, host="127.0.0.1", port=8188, wait=True, timeout=30)
mock_exec.connect.assert_called_once()
mock_exec.queue.assert_called_once()
mock_exec.watch_execution.assert_called_once()
def test_file_not_found_exits(self):
with pytest.raises(typer.Exit) as exc_info:
execute("/nonexistent/workflow.json", host="127.0.0.1", port=8188)
assert exc_info.value.exit_code == 1
def test_rejects_invalid_workflow_format(self):
bad = {"1": {"no_class_type_here": "X"}}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(bad, f)
f.flush()
path = f.name
try:
with patch("comfy_cli.command.run.check_comfy_server_running", return_value=True):
with pytest.raises(typer.Exit) as exc_info:
execute(path, host="127.0.0.1", port=8188)
assert exc_info.value.exit_code == 1
finally:
os.unlink(path)
def test_rejects_malformed_json(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
f.write("{ this is not valid json")
f.flush()
path = f.name
try:
with patch("comfy_cli.command.run.check_comfy_server_running", return_value=True):
with pytest.raises(typer.Exit) as exc_info:
execute(path, host="127.0.0.1", port=8188)
assert exc_info.value.exit_code == 1
finally:
os.unlink(path)
def test_rejects_unreadable_file(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
f.write("{}")
path = f.name
try:
real_open = open
def fake_open(file, *args, **kwargs):
if file == path:
raise PermissionError(13, "Permission denied", path)
return real_open(file, *args, **kwargs)
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("builtins.open", side_effect=fake_open),
):
with pytest.raises(typer.Exit) as exc_info:
execute(path, host="127.0.0.1", port=8188)
assert exc_info.value.exit_code == 1
finally:
os.unlink(path)
def test_progress_stopped_on_error(self, workflow_file):
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.ExecutionProgress") as MockProgress,
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_progress = MagicMock()
MockProgress.return_value = mock_progress
mock_exec = MagicMock()
MockExec.return_value = mock_exec
mock_exec.watch_execution.side_effect = WebSocketTimeoutException("timed out")
with pytest.raises(typer.Exit):
execute(workflow_file, host="127.0.0.1", port=8188, wait=True, timeout=30)
mock_progress.stop.assert_called()
class TestExecuteUiWorkflow:
UI = {"nodes": [{"id": 1, "type": "X"}], "links": []}
CONVERTED = {"1": {"class_type": "EmptyLatentImage", "inputs": {"width": 64, "height": 64, "batch_size": 1}}}
@pytest.fixture
def ui_workflow_file(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(self.UI, f)
f.flush()
path = f.name
yield path
os.unlink(path)
def test_ui_workflow_is_converted_then_executed(self, ui_workflow_file):
mock_resp = MagicMock()
mock_resp.read.return_value = json.dumps(self.CONVERTED).encode()
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.request.urlopen", return_value=mock_resp) as mock_open,
patch("comfy_cli.command.run.ExecutionProgress"),
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_exec = MagicMock()
MockExec.return_value = mock_exec
mock_exec.outputs = []
execute(ui_workflow_file, host="127.0.0.1", port=8188, wait=True, timeout=30)
sent_req = mock_open.call_args[0][0]
assert sent_req.full_url == "http://127.0.0.1:8188/workflow/convert"
assert MockExec.call_args.args[0] == self.CONVERTED
mock_exec.queue.assert_called_once()
@pytest.mark.parametrize("code", [404, 405])
def test_ui_workflow_exits_when_endpoint_missing(self, ui_workflow_file, code):
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.request.urlopen", side_effect=_make_http_error(code)),
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
with pytest.raises(typer.Exit) as exc_info:
execute(ui_workflow_file, host="127.0.0.1", port=8188, wait=True, timeout=30)
assert exc_info.value.exit_code == 1
MockExec.assert_not_called()
def test_ui_workflow_exits_when_server_not_running(self, ui_workflow_file):
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=False),
patch("comfy_cli.command.run.request.urlopen") as mock_open,
):
with pytest.raises(typer.Exit) as exc_info:
execute(ui_workflow_file, host="127.0.0.1", port=8188)
assert exc_info.value.exit_code == 1
mock_open.assert_not_called()