test_run_workflow.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """Tests for run_workflow.py — focuses on logic that doesn't require a server."""
  2. from __future__ import annotations
  3. from extract_schema import extract_schema
  4. from run_workflow import (
  5. ComfyRunner,
  6. download_outputs,
  7. inject_params,
  8. parse_input_image_arg,
  9. )
  10. class TestParseInputImageArg:
  11. def test_with_name(self, tmp_path):
  12. f = tmp_path / "x.png"
  13. f.write_text("x", encoding="utf-8")
  14. n, p = parse_input_image_arg(f"image={f}")
  15. assert n == "image"
  16. assert p == f
  17. def test_without_name_defaults(self, tmp_path):
  18. f = tmp_path / "x.png"
  19. f.write_text("x", encoding="utf-8")
  20. n, _p = parse_input_image_arg(str(f))
  21. assert n == "image"
  22. def test_custom_name(self, tmp_path):
  23. f = tmp_path / "x.png"
  24. f.write_text("x", encoding="utf-8")
  25. n, _p = parse_input_image_arg(f"mask_image={f}")
  26. assert n == "mask_image"
  27. class TestInjectParams:
  28. def test_basic_injection(self, sd15_workflow):
  29. schema = extract_schema(sd15_workflow)
  30. wf, warnings = inject_params(sd15_workflow, schema, {
  31. "prompt": "new prompt",
  32. "seed": 999,
  33. "steps": 25,
  34. })
  35. assert wf["6"]["inputs"]["text"] == "new prompt"
  36. assert wf["3"]["inputs"]["seed"] == 999
  37. assert wf["3"]["inputs"]["steps"] == 25
  38. assert warnings == []
  39. def test_unknown_param_warns(self, sd15_workflow):
  40. schema = extract_schema(sd15_workflow)
  41. _, warnings = inject_params(sd15_workflow, schema, {"foobar": "x"})
  42. assert any("foobar" in w for w in warnings)
  43. def test_seed_minus_one_randomizes(self, sd15_workflow):
  44. schema = extract_schema(sd15_workflow)
  45. wf, warnings = inject_params(sd15_workflow, schema, {"seed": -1})
  46. assert wf["3"]["inputs"]["seed"] != -1
  47. assert isinstance(wf["3"]["inputs"]["seed"], int)
  48. assert any("expanded" in w.lower() for w in warnings)
  49. def test_randomize_seed_when_unset(self, sd15_workflow):
  50. schema = extract_schema(sd15_workflow)
  51. original = sd15_workflow["3"]["inputs"]["seed"]
  52. wf, _warnings = inject_params(sd15_workflow, schema, {}, randomize_seed_if_unset=True)
  53. assert wf["3"]["inputs"]["seed"] != original
  54. assert isinstance(wf["3"]["inputs"]["seed"], int)
  55. def test_does_not_mutate_original(self, sd15_workflow):
  56. schema = extract_schema(sd15_workflow)
  57. original_text = sd15_workflow["6"]["inputs"]["text"]
  58. inject_params(sd15_workflow, schema, {"prompt": "MUTATED"})
  59. assert sd15_workflow["6"]["inputs"]["text"] == original_text
  60. def test_refuses_to_overwrite_link(self):
  61. wf = {
  62. "1": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "x"}},
  63. "5": {"class_type": "EmptyLatentImage",
  64. "inputs": {"width": 512, "height": 512, "batch_size": 1}},
  65. "6": {"class_type": "CLIPTextEncode",
  66. "inputs": {"text": ["3", 0], "clip": ["1", 1]}}, # text is a link!
  67. "3": {"class_type": "KSampler",
  68. "inputs": {"seed": 1, "steps": 20, "cfg": 7.5,
  69. "sampler_name": "euler", "scheduler": "normal", "denoise": 1.0,
  70. "model": ["1", 0], "positive": ["6", 0], "negative": ["6", 0],
  71. "latent_image": ["5", 0]}},
  72. "9": {"class_type": "SaveImage", "inputs": {"filename_prefix": "x", "images": ["3", 0]}},
  73. }
  74. # Manually create a schema that has prompt pointing at 6.text
  75. schema = {
  76. "parameters": {
  77. "prompt": {"node_id": "6", "field": "text", "type": "string", "value": ""},
  78. }
  79. }
  80. wf2, warnings = inject_params(wf, schema, {"prompt": "literal value"})
  81. # The link should NOT have been overwritten
  82. assert wf2["6"]["inputs"]["text"] == ["3", 0]
  83. assert any("link" in w.lower() for w in warnings)
  84. # =============================================================================
  85. # Output download walk
  86. # =============================================================================
  87. class TestDownloadOutputsWalk:
  88. """Test that download_outputs walks the structure correctly."""
  89. def test_handles_videos_plural(self, tmp_path, monkeypatch):
  90. """Local ComfyUI uses 'videos'/'gifs' (plural) keys."""
  91. downloads = []
  92. class FakeRunner:
  93. def download_output(self, *, filename, subfolder, file_type, output_dir, preserve_subfolder, overwrite):
  94. downloads.append((filename, subfolder, file_type))
  95. p = output_dir / filename
  96. p.parent.mkdir(parents=True, exist_ok=True)
  97. p.write_bytes(b"x")
  98. return p
  99. outputs = {
  100. "9": {"images": [{"filename": "img1.png", "subfolder": "", "type": "output"}]},
  101. "10": {"videos": [{"filename": "vid1.mp4", "subfolder": "", "type": "output"}]},
  102. "11": {"gifs": [{"filename": "anim1.gif", "subfolder": "", "type": "output"}]},
  103. }
  104. result = download_outputs(FakeRunner(), outputs, tmp_path)
  105. files = sorted(d["filename"] for d in result)
  106. assert files == ["anim1.gif", "img1.png", "vid1.mp4"]
  107. def test_handles_video_singular_cloud(self, tmp_path):
  108. """Cloud uses 'video' (singular)."""
  109. class FakeRunner:
  110. def download_output(self, *, filename, subfolder, file_type, output_dir, preserve_subfolder, overwrite):
  111. p = output_dir / filename
  112. p.parent.mkdir(parents=True, exist_ok=True)
  113. p.write_bytes(b"x")
  114. return p
  115. outputs = {
  116. "10": {"video": [{"filename": "cloud.mp4", "subfolder": "", "type": "output"}]},
  117. }
  118. result = download_outputs(FakeRunner(), outputs, tmp_path)
  119. assert len(result) == 1
  120. assert result[0]["filename"] == "cloud.mp4"
  121. def test_preserves_subfolder(self, tmp_path):
  122. """When preserve_subfolder=True, server subfolder becomes local subdir."""
  123. class FakeRunner:
  124. def download_output(self, *, filename, subfolder, file_type, output_dir, preserve_subfolder, overwrite):
  125. if preserve_subfolder and subfolder:
  126. p = output_dir / subfolder / filename
  127. else:
  128. p = output_dir / filename
  129. p.parent.mkdir(parents=True, exist_ok=True)
  130. p.write_bytes(b"x")
  131. return p
  132. outputs = {
  133. "9": {"images": [
  134. {"filename": "img.png", "subfolder": "myrun", "type": "output"},
  135. {"filename": "img.png", "subfolder": "otherrun", "type": "output"},
  136. ]},
  137. }
  138. result = download_outputs(FakeRunner(), outputs, tmp_path, preserve_subfolder=True)
  139. files = [d["file"] for d in result]
  140. assert any("myrun" in f for f in files)
  141. assert any("otherrun" in f for f in files)
  142. # Both must exist (no collision)
  143. assert len({str(f) for f in files}) == 2
  144. # =============================================================================
  145. # ComfyRunner construction
  146. # =============================================================================
  147. class TestRunnerConstruction:
  148. def test_local_default(self):
  149. r = ComfyRunner()
  150. assert r.is_cloud is False
  151. assert r.host == "http://127.0.0.1:8188"
  152. def test_cloud_detection(self):
  153. r = ComfyRunner(host="https://cloud.comfy.org", api_key="abc")
  154. assert r.is_cloud is True
  155. assert "X-API-Key" in r.headers
  156. def test_cloud_subdomain_detected(self):
  157. r = ComfyRunner(host="https://staging.cloud.comfy.org", api_key="abc")
  158. assert r.is_cloud is True
  159. def test_partner_key_does_not_pollute_extra_data(self):
  160. r = ComfyRunner(host="https://cloud.comfy.org", api_key="auth-key")
  161. # No partner-key set → no extra_data should appear in submitted prompt
  162. # (This is a static check; runtime check happens in submit())
  163. assert r.partner_key is None
  164. def test_url_routing_local(self):
  165. r = ComfyRunner()
  166. url = r._url("/prompt")
  167. assert url == "http://127.0.0.1:8188/prompt"
  168. def test_url_routing_cloud(self):
  169. r = ComfyRunner(host="https://cloud.comfy.org", api_key="x")
  170. url = r._url("/prompt")
  171. assert url == "https://cloud.comfy.org/api/prompt"
  172. def test_url_routing_cloud_history_renamed(self):
  173. r = ComfyRunner(host="https://cloud.comfy.org", api_key="x")
  174. url = r._url("/history/abc-123")
  175. assert url == "https://cloud.comfy.org/api/history_v2/abc-123"