345 lines
13 KiB
Python
345 lines
13 KiB
Python
"""Synthetic mutation fixtures; no target execution or golden export claims."""
|
|
|
|
import json
|
|
from copy import deepcopy
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from calliope.comfyui.serialization_reference import (
|
|
PRIMITIVE_PROFILE,
|
|
fold_reference_primitives,
|
|
serialize_reference_widgets,
|
|
)
|
|
from calliope.comfyui.structure_reference import expand_reference_subgraphs
|
|
from calliope.comfyui.ui2api import convert, convert_reference
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
CASES = json.loads((REPO / "docs/hel-892/golden-cases.json").read_text())["cases"]
|
|
PROFILE = "comfy-frontend-1.48.7-structure-reference"
|
|
DEFINITION = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
|
|
NESTED = "aaaaaaaa-bbbb-cccc-dddd-ffffffffffff"
|
|
|
|
|
|
def real(case):
|
|
row = next(c for c in CASES if c["id"] == case)
|
|
return json.loads((REPO / row["source_path"]).read_text())
|
|
|
|
|
|
def subtitle_fixture():
|
|
"""Synthetic reduced graph derived from source02; not an original export."""
|
|
graph = real("02")
|
|
graph["nodes"] = [n for n in graph["nodes"] if n["id"] in (6, 7, 9)]
|
|
graph["links"] = [x for x in graph["links"] if x[0] in (13, 14)]
|
|
subtitle = next(n for n in graph["nodes"] if n["id"] == 9)
|
|
for output in subtitle["outputs"]:
|
|
output["links"] = []
|
|
return graph
|
|
|
|
|
|
def test_primitive_mutated_value_reaches_bound_field_without_mutating_source():
|
|
graph = subtitle_fixture()
|
|
primitive = next(n for n in graph["nodes"] if n["id"] == 7)
|
|
primitive["widgets_values"] = ["new terminology\n保留大小写"]
|
|
before = deepcopy(graph)
|
|
result = convert_reference(graph, PRIMITIVE_PROFILE)
|
|
assert result.prompt["9"]["inputs"]["hints"] == primitive["widgets_values"][0]
|
|
assert result.prompt["9"]["inputs"]["output_path"] == ""
|
|
assert result.prompt["6"]["inputs"]["audioUI"] == ""
|
|
assert "7" not in result.prompt
|
|
assert graph == before
|
|
assert convert(graph).prompt is None
|
|
|
|
|
|
def test_primitive_fanout_by_name_not_widget_array_position():
|
|
graph = subtitle_fixture()
|
|
target = next(n for n in graph["nodes"] if n["id"] == 9)
|
|
primitive = next(n for n in graph["nodes"] if n["id"] == 7)
|
|
target["inputs"][8]["link"] = 19 # output_path, not hints
|
|
primitive["outputs"][0]["links"].append(19)
|
|
graph["links"].append([19, 7, 0, 9, 8, "STRING"])
|
|
primitive["widgets_values"] = ["mutated"]
|
|
result = convert_reference(graph, PRIMITIVE_PROFILE)
|
|
assert result.prompt["9"]["inputs"]["output_path"] == "mutated"
|
|
assert result.prompt["9"]["inputs"]["hints"] == "mutated"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mutation",
|
|
[
|
|
"type",
|
|
"binding",
|
|
"extra_seed",
|
|
"replacement",
|
|
"orphan",
|
|
"missing_widget",
|
|
"bad_port",
|
|
"mode",
|
|
"duplicate_link",
|
|
],
|
|
)
|
|
def test_primitive_bad_contract_rejects_entire_graph(mutation):
|
|
graph = subtitle_fixture()
|
|
primitive = next(n for n in graph["nodes"] if n["id"] == 7)
|
|
target = next(n for n in graph["nodes"] if n["id"] == 9)
|
|
if mutation == "type":
|
|
target["inputs"][6]["type"] = "INT"
|
|
elif mutation == "binding":
|
|
target["inputs"][6]["widget"] = {"name": "output_path"}
|
|
elif mutation == "extra_seed":
|
|
primitive["widgets_values"].append("randomize")
|
|
elif mutation == "replacement":
|
|
primitive["properties"]["Run widget replace on values"] = True
|
|
elif mutation == "orphan":
|
|
primitive["outputs"][0]["links"] = []
|
|
graph["links"] = [x for x in graph["links"] if x[0] != 14]
|
|
target["inputs"][6]["link"] = None
|
|
elif mutation == "missing_widget":
|
|
target["widgets_values"].pop()
|
|
elif mutation == "bad_port":
|
|
graph["links"][1][4] = 99
|
|
elif mutation == "mode":
|
|
primitive["mode"] = 4
|
|
else:
|
|
graph["links"].append(deepcopy(graph["links"][1]))
|
|
assert convert_reference(graph, PRIMITIVE_PROFILE).prompt is None
|
|
with pytest.raises((ValueError, IndexError)):
|
|
fold_reference_primitives(graph, PRIMITIVE_PROFILE)
|
|
|
|
|
|
def test_primitive_reroute_is_not_assumed_to_forward_widget_binding():
|
|
graph = subtitle_fixture()
|
|
reroute = {
|
|
"id": 10,
|
|
"type": "Reroute",
|
|
"inputs": [{"name": "", "link": 14}],
|
|
"outputs": [{"type": "STRING", "links": [19]}],
|
|
}
|
|
graph["nodes"].append(reroute)
|
|
graph["links"][1] = [14, 7, 0, 10, 0, "STRING"]
|
|
graph["links"].append([19, 10, 0, 9, 6, "STRING"])
|
|
next(n for n in graph["nodes"] if n["id"] == 9)["inputs"][6]["link"] = 19
|
|
assert convert_reference(graph, PRIMITIVE_PROFILE).prompt is None
|
|
|
|
|
|
def test_primitive_output_binding_and_conflicting_fanout_reject():
|
|
source = subtitle_fixture()
|
|
primitive = next(n for n in source["nodes"] if n["id"] == 7)
|
|
primitive["outputs"][0]["widget"] = {"name": "wrong"}
|
|
assert convert_reference(source, PRIMITIVE_PROFILE).prompt is None
|
|
primitive["outputs"][0]["widget"] = {"name": "hints"}
|
|
primitive["outputs"][0]["links"].append(19)
|
|
target = next(n for n in source["nodes"] if n["id"] == 9)
|
|
target["inputs"][-1]["link"] = 19 # BOOLEAN unload_models conflicts with STRING
|
|
source["links"].append([19, 7, 0, 9, len(target["inputs"]) - 1, "BOOLEAN"])
|
|
assert convert_reference(source, PRIMITIVE_PROFILE).prompt is None
|
|
|
|
|
|
def graph_fixture():
|
|
"""Synthetic two instances with nonzero source/output slots and object links."""
|
|
definition = {
|
|
"id": DEFINITION,
|
|
"inputNode": {"id": -10},
|
|
"outputNode": {"id": -20},
|
|
"inputs": [{"name": "in", "type": "IMAGE", "linkIds": [10]}],
|
|
"outputs": [{"name": "out", "type": "MASK", "linkIds": [11]}],
|
|
"nodes": [
|
|
{
|
|
"id": 3,
|
|
"type": "SyntheticTransform",
|
|
"mode": 0,
|
|
"inputs": [{"name": "x", "type": "IMAGE", "link": 10}],
|
|
"outputs": [{"type": "IMAGE", "links": []}, {"type": "MASK", "links": [11]}],
|
|
}
|
|
],
|
|
"links": [
|
|
dict(id=10, origin_id=-10, origin_slot=0, target_id=3, target_slot=0, type="IMAGE"),
|
|
dict(id=11, origin_id=3, origin_slot=1, target_id=-20, target_slot=0, type="MASK"),
|
|
],
|
|
}
|
|
nodes = [
|
|
{
|
|
"id": 1,
|
|
"type": "SyntheticSource",
|
|
"outputs": [{"type": "MASK", "links": []}, {"type": "IMAGE", "links": [1, 2]}],
|
|
}
|
|
]
|
|
for instance, incoming, outgoing in ((5, 1, 3), (6, 2, 4)):
|
|
nodes.append(
|
|
{
|
|
"id": instance,
|
|
"type": DEFINITION,
|
|
"inputs": [{"name": "in", "type": "IMAGE", "link": incoming}],
|
|
"outputs": [{"name": "out", "type": "MASK", "links": [outgoing]}],
|
|
}
|
|
)
|
|
nodes.append(
|
|
{
|
|
"id": 9,
|
|
"type": "SyntheticSink",
|
|
"inputs": [
|
|
{"name": "a", "type": "MASK", "link": 3},
|
|
{"name": "b", "type": "MASK", "link": 4},
|
|
],
|
|
}
|
|
)
|
|
return {
|
|
"nodes": nodes,
|
|
"links": [
|
|
[1, 1, 1, 5, 0, "IMAGE"],
|
|
[2, 1, 1, 6, 0, "IMAGE"],
|
|
[3, 5, 0, 9, 0, "MASK"],
|
|
[4, 6, 0, 9, 1, "MASK"],
|
|
],
|
|
"definitions": {"subgraphs": [definition]},
|
|
"extra": {"frontendVersion": "1.48.7"},
|
|
}
|
|
|
|
|
|
def endpoints(graph):
|
|
return {(x[3], x[4]): (x[1], x[2]) for x in graph["links"]}
|
|
|
|
|
|
def test_scope_isolation_and_nonzero_slot_preservation():
|
|
source = graph_fixture()
|
|
before = deepcopy(source)
|
|
result = expand_reference_subgraphs(source, PROFILE)
|
|
assert result.issues == result.unresolved == []
|
|
assert endpoints(result.graph) == {
|
|
("5:3", 0): ("1", 1),
|
|
("6:3", 0): ("1", 1),
|
|
("9", 0): ("5:3", 1),
|
|
("9", 1): ("6:3", 1),
|
|
}
|
|
assert source == before
|
|
assert convert_reference(source, PROFILE).prompt is None # unknown synthetic serializers
|
|
assert convert(source).prompt is None
|
|
|
|
|
|
def test_nested_scope_boundaries_in_both_directions():
|
|
source = graph_fixture()
|
|
outer = source["definitions"]["subgraphs"][0]
|
|
inner = deepcopy(outer)
|
|
inner["id"] = NESTED
|
|
outer["nodes"][0] = {
|
|
"id": 3,
|
|
"type": NESTED,
|
|
"inputs": [{"name": "in", "type": "IMAGE", "link": 10}],
|
|
"outputs": [{"name": "out", "type": "MASK", "links": [11]}],
|
|
}
|
|
outer["links"][1]["origin_slot"] = 0
|
|
source["definitions"]["subgraphs"].append(inner)
|
|
result = expand_reference_subgraphs(source, PROFILE)
|
|
assert result.graph is not None
|
|
assert endpoints(result.graph)[("9", 1)] == ("6:3:3", 1)
|
|
assert endpoints(result.graph)[("5:3:3", 0)] == ("1", 1)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mutation",
|
|
[
|
|
"missing_definition",
|
|
"duplicate_id",
|
|
"duplicate_definition",
|
|
"recursive",
|
|
"dangling",
|
|
"bad_boundary",
|
|
"ambiguous_output",
|
|
"wrong_instance",
|
|
"cycle",
|
|
"wrong_version",
|
|
"bad_link_format",
|
|
],
|
|
)
|
|
def test_structure_corruption_never_returns_partial_graph(mutation):
|
|
source = graph_fixture()
|
|
definition = source["definitions"]["subgraphs"][0]
|
|
if mutation == "missing_definition":
|
|
source["definitions"]["subgraphs"] = []
|
|
elif mutation == "duplicate_id":
|
|
definition["nodes"].append(deepcopy(definition["nodes"][0]))
|
|
elif mutation == "duplicate_definition":
|
|
source["definitions"]["subgraphs"].append(deepcopy(definition))
|
|
elif mutation == "recursive":
|
|
definition["nodes"][0]["type"] = DEFINITION
|
|
definition["nodes"][0]["inputs"][0]["name"] = "in"
|
|
definition["nodes"][0]["outputs"] = [{"name": "out", "type": "MASK", "links": [11]}]
|
|
definition["links"][1]["origin_slot"] = 0
|
|
elif mutation == "dangling":
|
|
source["links"][0][1] = 999
|
|
elif mutation == "bad_boundary":
|
|
definition["inputs"][0]["linkIds"] = [999]
|
|
elif mutation == "ambiguous_output":
|
|
definition["outputs"][0]["linkIds"] = [11, 11]
|
|
elif mutation == "wrong_instance":
|
|
source["nodes"][1]["inputs"][0]["name"] = "wrong"
|
|
elif mutation == "cycle":
|
|
source["links"][0][1:3] = [5, 0]
|
|
source["nodes"][0]["outputs"][1]["links"] = [2]
|
|
source["nodes"][1]["outputs"][0]["links"].append(1)
|
|
elif mutation == "wrong_version":
|
|
source["extra"]["frontendVersion"] = "main"
|
|
else:
|
|
source["links"][0] = [1, 1]
|
|
result = expand_reference_subgraphs(source, PROFILE)
|
|
assert result.graph is None
|
|
assert convert_reference(source, PROFILE).prompt is None
|
|
|
|
|
|
def test_unbound_boundary_is_reported_without_guessing_positional_values():
|
|
source = graph_fixture()
|
|
source["links"] = source["links"][1:]
|
|
source["nodes"][0]["outputs"][1]["links"] = [2]
|
|
source["nodes"][1]["inputs"][0]["link"] = None
|
|
source["nodes"][1]["widgets_values"] = ["must not be used"]
|
|
result = convert_reference(source, PROFILE)
|
|
assert result.diagnostic_graph is not None and result.prompt is None
|
|
assert result.unresolved_boundaries == [
|
|
{
|
|
"node_id": "5:3",
|
|
"input_slot": 0,
|
|
"input_name": "x",
|
|
"boundary_instance": "5",
|
|
"slot": 0,
|
|
"name": "in",
|
|
"type": "IMAGE",
|
|
}
|
|
]
|
|
|
|
|
|
def test_mode_is_a_concrete_blocker_even_after_structural_expansion():
|
|
source = graph_fixture()
|
|
source["definitions"]["subgraphs"][0]["nodes"][0]["mode"] = 4
|
|
result = convert_reference(source, PROFILE)
|
|
assert result.prompt is None
|
|
assert any(x["code"] == "mode_requires_export" and x["node_id"] == "5:3" for x in result.issues)
|
|
|
|
|
|
@pytest.mark.parametrize("case", ["03", "04"])
|
|
def test_real_structure_preserves_composite_output_and_stays_nonexecutable(case):
|
|
source = real(case)
|
|
profile = "comfy-frontend-" + source["extra"]["frontendVersion"] + "-structure-reference"
|
|
result = convert_reference(source, profile)
|
|
assert result.prompt is None
|
|
assert result.diagnostic_graph is not None
|
|
ids = {str(n["id"]) for n in result.diagnostic_graph["nodes"]}
|
|
assert ("37:43" if case == "03" else "459:457") in ids
|
|
assert result.unresolved_boundaries
|
|
assert all(x["code"] != "subgraph_requires_export" for x in result.issues)
|
|
|
|
|
|
def test_real_primitive_keeps_preview_gap_and_source_vs_api_schema_difference():
|
|
source = real("02")
|
|
result = convert_reference(source, PRIMITIVE_PROFILE)
|
|
assert result.prompt is None
|
|
assert {x["node_id"] for x in result.issues} == {"1", "2", "3", "4"}
|
|
target = next(n for n in result.diagnostic_graph["nodes"] if n["id"] == 9)
|
|
values = serialize_reference_widgets(target, PRIMITIVE_PROFILE)
|
|
assert "normalize_text" not in values
|
|
|
|
|
|
def test_real_case07_bad_reciprocal_link_is_rejected():
|
|
result = convert_reference(real("07"), "comfy-frontend-1.48.6-structure-reference")
|
|
assert result.prompt is None and result.diagnostic_graph is None
|
|
assert result.issues[0]["code"] == "reference_structure_invalid"
|