Files
aigc-home/calliope-backend/tests/test_reference_serialization.py
T

236 lines
8.5 KiB
Python

"""Synthetic contract tests and bounded real-source comparison; no execution proof."""
from __future__ import annotations
import copy
import hashlib
import json
from pathlib import Path
import pytest
from calliope.comfyui.serialization_reference import (
reference_definitions,
resolve_reference_link,
serialize_reference_widgets,
)
from calliope.comfyui.ui2api import convert, convert_reference
REPO = Path(__file__).resolve().parents[2]
def synthetic_graph():
return {
"nodes": [
{
"id": 1,
"type": "LoadImage",
"widgets_values": ["测试.png", "image"],
"outputs": [{"type": "IMAGE", "links": [10]}, {"type": "MASK", "links": []}],
},
{
"id": 2,
"type": "Reroute",
"inputs": [{"name": "", "link": 10}],
"outputs": [{"type": "IMAGE", "links": [11]}],
},
{
"id": 3,
"type": "Reroute",
"inputs": [{"name": "", "link": 11}],
"outputs": [{"type": "IMAGE", "links": [12]}],
},
{
"id": 4,
"type": "Canny",
"widgets_values": [0.23, 0.91],
"inputs": [{"name": "image", "link": 12}],
"outputs": [{"type": "IMAGE", "links": [13]}],
},
{
"id": 5,
"type": "PreviewImage",
"widgets_values": [],
"inputs": [{"name": "images", "link": 13}],
"outputs": [],
},
],
"links": [
[10, 1, 0, 2, 0, "IMAGE"],
[11, 2, 0, 3, 0, "IMAGE"],
[12, 3, 0, 4, 0, "IMAGE"],
[13, 4, 0, 5, 0, "IMAGE"],
],
}
def test_identity_widgets_upload_omission_and_reroute_chain():
graph = synthetic_graph()
before = copy.deepcopy(graph)
result = convert_reference(graph)
assert result.reference_only and not result.issues
assert set(result.prompt) == {"1", "4", "5"}
assert result.prompt["1"]["inputs"] == {"image": "测试.png"}
assert result.prompt["4"]["inputs"] == {
"image": ["1", 0],
"low_threshold": 0.23,
"high_threshold": 0.91,
}
assert result.prompt["5"]["inputs"] == {"images": ["4", 0]}
assert graph == before
assert convert(graph).prompt is None
assert not convert(graph).reference_only
# Even a matching input_order does not opt production into public contracts.
definitions = reference_definitions()
for spec in definitions.values():
spec["input_order"] = {"required": list(spec["input"]["required"])}
assert convert(graph, definitions).prompt is None
def test_nonzero_original_source_slot_is_preserved_but_type_mismatch_rejected():
graph = synthetic_graph()
graph["nodes"][0]["outputs"][0]["links"] = []
graph["nodes"][0]["outputs"][1]["links"] = [10]
graph["links"][0][2] = 1
assert resolve_reference_link(
{str(n["id"]): n for n in graph["nodes"]},
{str(link[0]): link for link in graph["links"]},
12,
) == ["1", 1]
result = convert_reference(graph)
assert result.prompt is None
assert "link_type_unverified" in {i["code"] for i in result.issues}
@pytest.mark.parametrize(
"change",
[
"cycle",
"missing_link",
"missing_node",
"negative_slot",
"bool_slot",
"wrong_target",
"output_disagrees",
"duplicate_link",
"missing_output_link",
"extra_reroute_input",
"reroute_widgets",
"bypass",
"subgraph",
"extra_widget",
"missing_widget",
"bool_numeric",
"dynamic_value",
"extra_input",
"ambiguous_output",
"node_version",
"node_provider",
"frontend_version",
],
)
def test_unsupported_graph_never_returns_partial_prompt(change):
graph = synthetic_graph()
if change == "cycle":
graph["links"][0][1:3] = [3, 0]
graph["nodes"][2]["outputs"][0]["links"].append(10)
elif change == "missing_link":
graph["links"].pop(0)
elif change == "missing_node":
graph["nodes"].pop(0)
elif change in ("negative_slot", "bool_slot"):
graph["links"][0][2] = -1 if change == "negative_slot" else False
elif change == "wrong_target":
graph["links"][0][4] = 1
elif change == "output_disagrees":
graph["nodes"][0]["outputs"][0]["links"] = []
elif change == "duplicate_link":
graph["links"].append(graph["links"][0])
elif change == "missing_output_link":
graph["nodes"][0]["outputs"][0]["links"].append(999)
elif change == "extra_reroute_input":
graph["nodes"][1]["inputs"].append({"name": "", "link": None})
elif change == "reroute_widgets":
graph["nodes"][1]["widgets_values"] = [123]
elif change == "bypass":
graph["nodes"][1]["mode"] = 4
elif change == "subgraph":
graph["definitions"] = {"subgraphs": [{"id": "unsupported"}]}
elif change == "extra_widget":
graph["nodes"][3]["widgets_values"].append("randomize")
elif change == "missing_widget":
graph["nodes"][3]["widgets_values"].pop()
elif change in ("bool_numeric", "dynamic_value"):
graph["nodes"][3]["widgets_values"][0] = True if change == "bool_numeric" else {"x": 1}
elif change == "node_version":
graph["nodes"][3]["properties"] = {"cnr_id": "comfy-core", "ver": "9.9.9"}
elif change == "node_provider":
graph["nodes"][3]["properties"] = {"cnr_id": "unknown-plugin"}
elif change == "frontend_version":
graph["extra"] = {"frontendVersion": "9.9.9"}
elif change == "ambiguous_output":
graph["nodes"][0]["outputs"][1]["links"] = [10]
elif change == "extra_input":
graph["nodes"][3]["inputs"].append({"name": "custom", "link": None})
result = convert_reference(graph)
assert result.prompt is None and result.issues and result.reference_only
if change == "cycle":
assert any("循环" in issue["message"] for issue in result.issues)
@pytest.mark.parametrize(
"source,profile",
[
(synthetic_graph(), "input_order_is_not_a_contract"),
({"nodes": "malformed"}, None),
({"nodes": [{"id": 1, "type": "LoadImage", "inputs": None}]}, None),
],
)
def test_bad_contracts_rejected(source, profile):
result = convert_reference(source, profile) if profile else convert_reference(source)
assert result.prompt is None and result.issues and result.reference_only
def test_seed_and_custom_serializers_are_not_inferred():
with pytest.raises(ValueError):
serialize_reference_widgets({"type": "KSampler", "widgets_values": [12, "fixed"]})
with pytest.raises(ValueError):
serialize_reference_widgets({"type": "Canny", "widgets_values": {"low_threshold": 0.4}})
def test_real_case01_edges_and_supported_fields_match_supplied_export():
record = json.loads((REPO / "docs/hel-892/supplied-api/manifest.json").read_text())["cases"][0]
raw = (REPO / record["source_path"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == record["source_sha256"]
api_raw = (REPO / "docs/hel-892/supplied-api" / record["export_file"]).read_bytes()
assert hashlib.sha256(api_raw).hexdigest() == record["export_sha256"]
source, api = json.loads(raw), json.loads(api_raw)
before = copy.deepcopy(source)
nodes = {str(n["id"]): n for n in source["nodes"]}
links = {str(link[0]): link for link in source["links"]}
fields = edges = rerouted = 0
for nid, node in nodes.items():
if node["type"] == "Reroute":
continue
try:
values = serialize_reference_widgets(node)
except ValueError:
pass
else:
for name, value in values.items():
assert type(value) is type(api[nid]["inputs"][name])
assert value == api[nid]["inputs"][name]
fields += 1
for socket in node.get("inputs", []):
if socket.get("link") is None:
continue
lid = socket["link"]
assert resolve_reference_link(nodes, links, lid) == api[nid]["inputs"][socket["name"]]
edges += 1
rerouted += nodes[str(links[str(lid)][1])]["type"] == "Reroute"
assert (fields, edges, rerouted) == (16, 23, 9)
result = convert_reference(source)
assert result.prompt is None
assert "reference_node_unsupported" in {i["code"] for i in result.issues}
assert source == before