367 lines
13 KiB
Python
367 lines
13 KiB
Python
"""Fixed saved-state reference only: synthetic no-UE fixtures are not user exports."""
|
|
|
|
import copy
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from scripts import audit_reference_conversion as audit
|
|
from scripts.verify_frontend_sources import verify
|
|
|
|
from calliope.comfyui.serialization_reference import serialize_grid_node
|
|
from calliope.comfyui.structure_reference import GRID_PROFILE
|
|
from calliope.comfyui.ui2api import convert, convert_reference
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
PATH = next((REPO / "example_ComfyUI_workflows").rglob("图像合并成四宫格.json"))
|
|
|
|
|
|
def graph(real=False):
|
|
g = json.loads(PATH.read_bytes())
|
|
if not real:
|
|
# Explicit synthetic premise. Never mutate the immutable source or write
|
|
# this counterfactual as an actual library candidate.
|
|
for n in g["nodes"]:
|
|
n["properties"].pop("ue_properties", None)
|
|
return g
|
|
|
|
|
|
def node(g, nid):
|
|
return next(n for n in g["nodes"] if n["id"] == nid)
|
|
|
|
|
|
def test_real_graph_stays_closed_at_exact_ue_pointers():
|
|
g = graph(True)
|
|
original = copy.deepcopy(g)
|
|
result = convert_reference(g, GRID_PROFILE)
|
|
assert result.prompt is None and result.reference_only and g == original
|
|
rejected = [i for i in result.issues if i["code"] == "reference_widget_unverified"]
|
|
assert {i["node_id"] for i in rejected} == {
|
|
"12",
|
|
"13",
|
|
"14",
|
|
"15",
|
|
"16",
|
|
"17",
|
|
"18",
|
|
"19",
|
|
"21",
|
|
}
|
|
for issue in rejected:
|
|
assert "UE7.7" in issue["message"]
|
|
i = int(issue["pointer"].split("/")[-1])
|
|
assert str(g["nodes"][i]["id"]) == issue["node_id"]
|
|
attempt = audit.profile_attempt(g, GRID_PROFILE)
|
|
assert attempt["status"] == "rejected" and "candidate_prompt" not in attempt
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"width,height,crop,method",
|
|
[
|
|
(2048, 2048, "center", "lanczos"),
|
|
(0, 1234, "disabled", "area"),
|
|
(16384, 0, "center", "bilinear"),
|
|
(345, 678, "disabled", "nearest-exact"),
|
|
(3, 4, "center", "bicubic"),
|
|
],
|
|
)
|
|
def test_dynamic_restored_layout_keeps_qualified_names_types_and_metadata(
|
|
width, height, crop, method
|
|
):
|
|
g = graph()
|
|
node(g, 21)["widgets_values"] = ["scale dimensions", width, height, crop, method]
|
|
node(g, 19)["widgets_values"] = ["saved-prefix-中文"]
|
|
node(g, 21)["title"] = "精确尺寸"
|
|
before = copy.deepcopy(g)
|
|
result = convert_reference(g, GRID_PROFILE)
|
|
assert result.prompt is not None, result.issues
|
|
assert result.reference_only and g == before and convert(g).prompt is None
|
|
assert result.serialization_stage == "restored_saved_state_before_queue_hooks"
|
|
assert result.prompt["21"]["inputs"] == {
|
|
"resize_type": "scale dimensions",
|
|
"resize_type.width": width,
|
|
"resize_type.height": height,
|
|
"resize_type.crop": crop,
|
|
"scale_method": method,
|
|
"input": ["18", 0],
|
|
}
|
|
assert result.prompt["21"]["_meta"] == {"title": "精确尺寸"}
|
|
assert result.prompt["19"]["inputs"]["filename_prefix"] == "saved-prefix-中文"
|
|
assert "24" not in result.prompt
|
|
assert all("upload" not in n["inputs"] for n in result.prompt.values())
|
|
evidence = audit.grid_field_evidence(g, result.prompt)
|
|
assert len(evidence) == 30 # 22 scalars plus eight physical edges.
|
|
assert (
|
|
next(e for e in evidence if e["field"] == "resize_type.width")["source_pointer"]
|
|
== "/nodes/3/widgets_values/1"
|
|
)
|
|
|
|
|
|
def test_physical_image_links_use_original_slots_and_changed_instances():
|
|
g = graph()
|
|
# Replace top-left with the fourth image. Keep all reverse records valid.
|
|
node(g, 12)["outputs"][0]["links"] = []
|
|
node(g, 15)["outputs"][0]["links"].append(11)
|
|
next(e for e in g["links"] if e[0] == 11)[1] = 15
|
|
node(g, 15)["widgets_values"][0] = "synthetic-fourth.png"
|
|
result = convert_reference(g, GRID_PROFILE)
|
|
assert result.prompt is not None, result.issues
|
|
assert result.prompt["16"]["inputs"]["image1"] == ["15", 0]
|
|
assert result.prompt["15"]["inputs"]["image"] == "synthetic-fourth.png"
|
|
assert result.prompt["17"]["inputs"]["image2"] == ["15", 0]
|
|
assert result.prompt["19"]["inputs"]["images"] == ["21", 0]
|
|
|
|
|
|
def test_qualified_dimension_link_overrides_saved_scalar_only_in_synthetic_pipeline(monkeypatch):
|
|
from calliope.comfyui import ui2api
|
|
from calliope.comfyui.serialization_reference import grid_definitions
|
|
|
|
g = graph()
|
|
n = node(g, 21)
|
|
n["inputs"][2]["link"] = 100
|
|
g["links"].append([100, 99, 0, 21, 2, "INT"])
|
|
g["nodes"].append(
|
|
{
|
|
"id": 99,
|
|
"type": "SyntheticIntSource",
|
|
"mode": 0,
|
|
"inputs": [],
|
|
"outputs": [{"name": "INT", "type": "INT", "links": [100]}],
|
|
}
|
|
)
|
|
# This invented source is deliberately outside the public profile. An actual
|
|
# graph with it must close; only a test-local serializer verifies precedence.
|
|
assert convert_reference(g, GRID_PROFILE).prompt is None
|
|
original = ui2api.serialize_reference_widgets
|
|
|
|
def serialize(node, profile):
|
|
return {} if node["type"] == "SyntheticIntSource" else original(node, profile)
|
|
|
|
monkeypatch.setattr(ui2api, "serialize_reference_widgets", serialize)
|
|
definitions = grid_definitions()
|
|
definitions["SyntheticIntSource"] = {"input": {"required": {}}, "output": ["INT"]}
|
|
result = ui2api._convert(g, definitions, reference=True, reference_profile=GRID_PROFILE)
|
|
assert result.prompt is not None, result.issues
|
|
assert result.prompt["21"]["inputs"]["resize_type.width"] == ["99", 0]
|
|
assert result.prompt["21"]["inputs"]["resize_type.height"] == 2048
|
|
n["widgets_values"][1] = 17
|
|
assert ui2api._convert(g, definitions, reference=True, reference_profile=GRID_PROFILE).prompt[
|
|
"21"
|
|
]["inputs"]["resize_type.width"] == ["99", 0]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mutation",
|
|
[
|
|
"branch",
|
|
"missing_value",
|
|
"extra_value",
|
|
"named_object",
|
|
"width_bool",
|
|
"height_float",
|
|
"negative_width",
|
|
"oversize",
|
|
"crop",
|
|
"method",
|
|
"provider",
|
|
"version",
|
|
"aux",
|
|
"short_commit",
|
|
"frontend",
|
|
"missing_frontend",
|
|
"renderer",
|
|
"mode",
|
|
"bool_mode",
|
|
"missing_socket",
|
|
"extra_socket",
|
|
"qualified_name",
|
|
"widget_name",
|
|
"widget_override",
|
|
"socket_type",
|
|
"output_name",
|
|
"output_type",
|
|
"extra_output",
|
|
"bool_slot",
|
|
"bad_slot",
|
|
"reverse",
|
|
"type_edge",
|
|
"extra_edge_field",
|
|
"duplicate_edge",
|
|
"missing_edge",
|
|
"unknown_node",
|
|
"note_edge",
|
|
"subgraph",
|
|
"ue_empty",
|
|
"ue_nested",
|
|
"ue_top",
|
|
"ue_graph",
|
|
"ue_bad_array",
|
|
"custom_serializer",
|
|
"custom_dynamic",
|
|
"hook",
|
|
"upload_value",
|
|
"upload_link",
|
|
"spacing_bool",
|
|
"spacing_odd",
|
|
"direction",
|
|
"color",
|
|
"selector_link",
|
|
"layout_order",
|
|
"cycle",
|
|
"filename_expression",
|
|
],
|
|
)
|
|
def test_any_unknown_branch_layout_type_slot_extension_or_identity_closes_whole_graph(mutation):
|
|
g = graph()
|
|
n = node(g, 21)
|
|
if mutation == "branch":
|
|
n["widgets_values"][0] = "scale by"
|
|
elif mutation == "missing_value":
|
|
n["widgets_values"].pop(2)
|
|
elif mutation == "extra_value":
|
|
n["widgets_values"].append(0)
|
|
elif mutation == "named_object":
|
|
n["widgets_values"] = {"width": 2048}
|
|
elif mutation == "width_bool":
|
|
n["widgets_values"][1] = True
|
|
elif mutation == "height_float":
|
|
n["widgets_values"][2] = 2048.0
|
|
elif mutation == "negative_width":
|
|
n["widgets_values"][1] = -1
|
|
elif mutation == "oversize":
|
|
n["widgets_values"][2] = 16385
|
|
elif mutation == "crop":
|
|
n["widgets_values"][3] = "unknown"
|
|
elif mutation == "method":
|
|
n["widgets_values"][4] = "unknown"
|
|
elif mutation == "provider":
|
|
n["properties"]["cnr_id"] = "other"
|
|
elif mutation == "version":
|
|
n["properties"]["ver"] = "0.21.1"
|
|
elif mutation == "short_commit":
|
|
n["properties"]["ver"] = "63d1bbdb"
|
|
elif mutation == "aux":
|
|
n["properties"]["aux_id"] = "other"
|
|
elif mutation == "frontend":
|
|
g["extra"]["frontendVersion"] = "1.41.21"
|
|
elif mutation == "missing_frontend":
|
|
g["extra"].pop("frontendVersion")
|
|
elif mutation == "renderer":
|
|
g["extra"]["workflowRendererVersion"] = "Vue"
|
|
elif mutation == "mode":
|
|
n["mode"] = 4
|
|
elif mutation == "bool_mode":
|
|
n["mode"] = False
|
|
elif mutation == "missing_socket":
|
|
n["inputs"].pop(2)
|
|
elif mutation == "extra_socket":
|
|
n["inputs"].append(copy.deepcopy(n["inputs"][2]))
|
|
elif mutation == "qualified_name":
|
|
n["inputs"][2]["name"] = "width"
|
|
elif mutation == "widget_name":
|
|
n["inputs"][2]["widget"]["name"] = "width"
|
|
elif mutation == "widget_override":
|
|
n["inputs"][2]["widget"]["serializeValue"] = "unknown"
|
|
elif mutation == "socket_type":
|
|
n["inputs"][2]["type"] = "FLOAT"
|
|
elif mutation == "output_name":
|
|
n["outputs"][0]["name"] = "IMAGE"
|
|
elif mutation == "output_type":
|
|
n["outputs"][0]["type"] = "MASK"
|
|
elif mutation == "extra_output":
|
|
n["outputs"].append({"name": "unused", "type": "IMAGE"})
|
|
elif mutation == "bool_slot":
|
|
g["links"][0][2] = False
|
|
elif mutation == "bad_slot":
|
|
g["links"][0][2] = 1
|
|
elif mutation == "reverse":
|
|
node(g, 12)["outputs"][0]["links"] = []
|
|
elif mutation == "type_edge":
|
|
g["links"][0][5] = "MASK"
|
|
elif mutation == "extra_edge_field":
|
|
g["links"][0].append("unknown")
|
|
elif mutation == "duplicate_edge":
|
|
g["links"].append(copy.deepcopy(g["links"][0]))
|
|
elif mutation == "missing_edge":
|
|
g["links"].pop()
|
|
elif mutation == "unknown_node":
|
|
node(g, 12)["type"] = "unknown"
|
|
elif mutation == "note_edge":
|
|
node(g, 24)["outputs"] = [{"type": "IMAGE", "name": "IMAGE"}]
|
|
elif mutation == "subgraph":
|
|
g["definitions"] = {"subgraphs": [{"id": "unknown"}]}
|
|
elif mutation == "ue_empty":
|
|
n["properties"]["ue_properties"] = {}
|
|
elif mutation == "ue_nested":
|
|
n["properties"]["ue_properties"] = {"widget_ue_connectable": {"resize_type.width": True}}
|
|
elif mutation == "ue_top":
|
|
n["widget_ue_connectable"] = {"width": True}
|
|
elif mutation == "ue_graph":
|
|
g["extra"]["ue_links"] = [1]
|
|
elif mutation == "ue_bad_array":
|
|
g["extra"]["ue_links"] = {}
|
|
elif mutation == "custom_serializer":
|
|
n["serializeValue"] = "unknown"
|
|
elif mutation == "custom_dynamic":
|
|
n["properties"]["comfyDynamic"] = {}
|
|
elif mutation == "hook":
|
|
n["onConfigure"] = "unknown"
|
|
elif mutation == "upload_value":
|
|
node(g, 12)["widgets_values"][1] = "file"
|
|
elif mutation == "upload_link":
|
|
node(g, 12)["inputs"][1]["link"] = 11
|
|
elif mutation == "spacing_bool":
|
|
node(g, 16)["widgets_values"][2] = False
|
|
elif mutation == "spacing_odd":
|
|
node(g, 16)["widgets_values"][2] = 3
|
|
elif mutation == "direction":
|
|
node(g, 16)["widgets_values"][0] = "unknown"
|
|
elif mutation == "color":
|
|
node(g, 16)["widgets_values"][3] = "unknown"
|
|
elif mutation == "selector_link":
|
|
n["inputs"][1]["link"] = 19
|
|
elif mutation == "layout_order":
|
|
n["inputs"][2:4] = list(reversed(n["inputs"][2:4]))
|
|
elif mutation == "filename_expression":
|
|
node(g, 19)["widgets_values"] = ["%date:yyyy-MM-dd%"]
|
|
elif mutation == "cycle":
|
|
# Valid reciprocal IMAGE slots but a real cycle 18 -> 21 -> 18.
|
|
node(g, 16)["outputs"][0]["links"].remove(15)
|
|
n["outputs"][0]["links"].append(15)
|
|
next(e for e in g["links"] if e[0] == 15)[1] = 21
|
|
result = convert_reference(g, GRID_PROFILE)
|
|
assert result.prompt is None and result.issues and result.reference_only, mutation
|
|
|
|
|
|
def test_fixed_sources_and_original_profile_cohorts_remain_identical():
|
|
assert verify(REPO / "docs/hel-892/grid-reference")["sources_verified"]
|
|
old = json.loads((REPO / "docs/hel-892/scail-load-evidence/library.json").read_bytes())
|
|
profiles = tuple(old["profiles"])
|
|
assert (
|
|
len(profiles) == 24
|
|
and tuple(
|
|
p for p in audit.REFERENCE_PROFILES[:26] if p not in (GRID_PROFILE, audit.H3_PROFILE)
|
|
)
|
|
== profiles
|
|
)
|
|
current = audit.audit_library(REPO, (*profiles, GRID_PROFILE), baseline_profiles=profiles)
|
|
for before, after in zip(old["cases"], current["cases"]):
|
|
assert before["source_path"] == after["source_path"]
|
|
assert before["attempts"] == after["attempts"][:24]
|
|
assert before.get("comparisons") == after.get("comparisons")
|
|
assert before["candidate_hashes"] == after["candidate_hashes"]
|
|
before = audit.audit_library(REPO, profiles)
|
|
assert old["gap_groups"] == before["gap_groups"]
|
|
assert current["coverage_delta"]["new_complete_paths"] == []
|
|
assert current["cases"][0]["execution_verified"] is False
|
|
|
|
|
|
def test_load_save_exact_core_identity_and_upload_is_not_execution_field():
|
|
g = graph()
|
|
for nid in (12, 13, 14, 15, 19):
|
|
n = node(g, nid)
|
|
assert serialize_grid_node(n)
|
|
n["properties"]["ver"] = "0.21.1"
|
|
with pytest.raises(ValueError, match="精确"):
|
|
serialize_grid_node(n)
|