test_extract_schema.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. """Tests for extract_schema.py."""
  2. from __future__ import annotations
  3. from extract_schema import (
  4. extract_schema,
  5. find_negative_prompt_node,
  6. find_positive_prompt_node,
  7. trace_to_node,
  8. )
  9. # =============================================================================
  10. # Connection tracing
  11. # =============================================================================
  12. class TestConnectionTracing:
  13. def test_direct_link(self):
  14. wf = {
  15. "1": {"class_type": "CLIPTextEncode", "inputs": {"text": "x"}},
  16. "2": {"class_type": "KSampler",
  17. "inputs": {"positive": ["1", 0], "negative": ["1", 0]}},
  18. }
  19. assert trace_to_node(wf, ["1", 0]) == "1"
  20. def test_through_reroute(self):
  21. wf = {
  22. "1": {"class_type": "CLIPTextEncode", "inputs": {"text": "x"}},
  23. "2": {"class_type": "Reroute", "inputs": {"input": ["1", 0]}},
  24. "3": {"class_type": "Reroute", "inputs": {"input": ["2", 0]}},
  25. }
  26. assert trace_to_node(wf, ["3", 0]) == "1"
  27. def test_circular_safe(self):
  28. wf = {
  29. "1": {"class_type": "Reroute", "inputs": {"input": ["2", 0]}},
  30. "2": {"class_type": "Reroute", "inputs": {"input": ["1", 0]}},
  31. }
  32. # Should hit max_hops without infinite loop
  33. result = trace_to_node(wf, ["1", 0], max_hops=5)
  34. assert result in {"1", "2"} # any node, just don't hang
  35. class TestPositiveNegativeDetection:
  36. def test_basic(self, sd15_workflow):
  37. # In sd15_workflow.json node 6 is positive, node 7 is negative
  38. assert find_positive_prompt_node(sd15_workflow) == "6"
  39. assert find_negative_prompt_node(sd15_workflow) == "7"
  40. def test_swapped_order(self):
  41. wf = {
  42. "3": {"class_type": "KSampler",
  43. "inputs": {
  44. "positive": ["7", 0], "negative": ["6", 0],
  45. "model": ["4", 0], "latent_image": ["5", 0],
  46. "seed": 1, "steps": 20, "cfg": 7.5,
  47. "sampler_name": "euler", "scheduler": "normal", "denoise": 1.0,
  48. }},
  49. "4": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "x"}},
  50. "5": {"class_type": "EmptyLatentImage", "inputs": {"width": 512, "height": 512, "batch_size": 1}},
  51. "6": {"class_type": "CLIPTextEncode", "inputs": {"text": "ugly", "clip": ["4", 1]}},
  52. "7": {"class_type": "CLIPTextEncode", "inputs": {"text": "beautiful", "clip": ["4", 1]}},
  53. }
  54. # Now 7 is the positive (despite higher node ID)
  55. assert find_positive_prompt_node(wf) == "7"
  56. assert find_negative_prompt_node(wf) == "6"
  57. # =============================================================================
  58. # Schema extraction
  59. # =============================================================================
  60. class TestExtractSchema:
  61. def test_basic_sd15(self, sd15_workflow):
  62. schema = extract_schema(sd15_workflow)
  63. params = schema["parameters"]
  64. assert "prompt" in params
  65. assert "negative_prompt" in params
  66. assert "seed" in params
  67. assert "steps" in params
  68. assert "cfg" in params
  69. assert "width" in params
  70. assert "height" in params
  71. def test_prompt_value_correct(self, sd15_workflow):
  72. schema = extract_schema(sd15_workflow)
  73. # The positive prompt in the example is the landscape one
  74. assert "landscape" in schema["parameters"]["prompt"]["value"]
  75. assert "ugly" in schema["parameters"]["negative_prompt"]["value"]
  76. def test_model_dependencies(self, sd15_workflow):
  77. schema = extract_schema(sd15_workflow)
  78. deps = schema["model_dependencies"]
  79. ckpts = [d["value"] for d in deps if d["folder"] == "checkpoints"]
  80. assert "v1-5-pruned-emaonly.safetensors" in ckpts
  81. def test_output_nodes(self, sd15_workflow):
  82. schema = extract_schema(sd15_workflow)
  83. assert "9" in schema["output_nodes"]
  84. def test_summary(self, sd15_workflow):
  85. schema = extract_schema(sd15_workflow)
  86. s = schema["summary"]
  87. assert s["has_negative_prompt"] is True
  88. assert s["has_seed"] is True
  89. assert s["is_video_workflow"] is False
  90. assert s["parameter_count"] > 5
  91. def test_flux_workflow(self, flux_workflow):
  92. schema = extract_schema(flux_workflow)
  93. # Flux uses RandomNoise for seed
  94. assert schema["summary"]["has_seed"] is True
  95. # Flux has only positive prompt (no negative encoder)
  96. assert schema["summary"]["has_negative_prompt"] is False
  97. def test_video_detected(self, video_workflow):
  98. schema = extract_schema(video_workflow)
  99. assert schema["summary"]["is_video_workflow"] is True
  100. class TestEmbeddingDeps:
  101. def test_extract_from_prompt(self):
  102. wf = {
  103. "1": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "x"}},
  104. "5": {"class_type": "EmptyLatentImage",
  105. "inputs": {"width": 512, "height": 512, "batch_size": 1}},
  106. "6": {"class_type": "CLIPTextEncode",
  107. "inputs": {
  108. "text": "a cat, embedding:goodvibes, embedding:art:1.2",
  109. "clip": ["1", 1]
  110. }},
  111. "7": {"class_type": "CLIPTextEncode",
  112. "inputs": {
  113. "text": "ugly, embedding:badhands",
  114. "clip": ["1", 1]
  115. }},
  116. "3": {"class_type": "KSampler",
  117. "inputs": {
  118. "positive": ["6", 0], "negative": ["7", 0],
  119. "model": ["1", 0], "latent_image": ["5", 0],
  120. "seed": 1, "steps": 20, "cfg": 7.5,
  121. "sampler_name": "euler", "scheduler": "normal", "denoise": 1.0,
  122. }},
  123. "9": {"class_type": "SaveImage", "inputs": {"filename_prefix": "x", "images": ["3", 0]}},
  124. }
  125. schema = extract_schema(wf)
  126. names = [d["embedding_name"] for d in schema["embedding_dependencies"]]
  127. assert sorted(names) == ["art", "badhands", "goodvibes"]
  128. class TestDuplicateDeduplication:
  129. def test_two_ksamplers_get_unique_names(self):
  130. wf = {
  131. "1": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "x"}},
  132. "5": {"class_type": "EmptyLatentImage",
  133. "inputs": {"width": 512, "height": 512, "batch_size": 1}},
  134. "6": {"class_type": "CLIPTextEncode", "inputs": {"text": "a", "clip": ["1", 1]}},
  135. "7": {"class_type": "CLIPTextEncode", "inputs": {"text": "b", "clip": ["1", 1]}},
  136. "3": {"class_type": "KSampler",
  137. "inputs": {
  138. "positive": ["6", 0], "negative": ["7", 0],
  139. "model": ["1", 0], "latent_image": ["5", 0],
  140. "seed": 42, "steps": 20, "cfg": 7.5,
  141. "sampler_name": "euler", "scheduler": "normal", "denoise": 1.0,
  142. }},
  143. "4": {"class_type": "KSampler",
  144. "inputs": {
  145. "positive": ["6", 0], "negative": ["7", 0],
  146. "model": ["1", 0], "latent_image": ["5", 0],
  147. "seed": 99, "steps": 30, "cfg": 8.0,
  148. "sampler_name": "euler", "scheduler": "normal", "denoise": 0.6,
  149. }},
  150. "9": {"class_type": "SaveImage", "inputs": {"filename_prefix": "x", "images": ["3", 0]}},
  151. }
  152. schema = extract_schema(wf)
  153. params = schema["parameters"]
  154. # Both seeds present with disambiguated names
  155. # Symmetric: both renamed (no bare "seed")
  156. assert "seed" not in params
  157. assert "seed_3" in params and "seed_4" in params
  158. assert params["seed_3"]["value"] == 42
  159. assert params["seed_4"]["value"] == 99