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

178 lines
6.9 KiB
Python

"""Pinned-source contracts and real-source mutations, never target execution."""
import copy
import json
from pathlib import Path
import pytest
from calliope.comfyui.serialization_reference import (
AUX_PROFILE,
PROFILE,
reference_definitions,
resolve_reference_link,
serialize_reference_widgets,
)
from calliope.comfyui.ui2api import convert, convert_reference
REPO = Path(__file__).resolve().parents[2]
@pytest.fixture
def case01():
record = json.loads((REPO / "docs/hel-892/supplied-api/manifest.json").read_text())["cases"][0]
return json.loads((REPO / record["source_path"]).read_bytes())
def by_id(graph, nid):
return next(n for n in graph["nodes"] if n["id"] == nid)
def test_real_full_candidate_includes_all_fields_and_preserves_raw_meta_differences(case01):
api = json.loads((REPO / "docs/hel-892/supplied-api/01.api.json").read_bytes())
before = copy.deepcopy(case01)
result = convert_reference(case01, AUX_PROFILE)
assert result.reference_only and not result.issues
assert len(result.prompt) == len(api) == 22
for nid, node in result.prompt.items():
assert node["class_type"] == api[nid]["class_type"]
assert node["inputs"] == api[nid]["inputs"]
for name, value in node["inputs"].items():
assert type(value) is type(api[nid]["inputs"][name])
assert node["_meta"]["title"] == (
by_id(case01, int(nid)).get("title") or node["class_type"]
)
assert sum(result.prompt[n]["_meta"] != api[n]["_meta"] for n in api) == 13
assert case01 == before
assert convert_reference(case01, PROFILE).prompt is None
assert convert(case01).prompt is None
assert convert(case01, reference_definitions(AUX_PROFILE)).prompt is None
@pytest.mark.parametrize("nid", [1, 12, 18, 19, 20, 21, 33, 34, 4])
def test_each_real_aux_node_serializes_changed_values_not_api_constants(case01, nid):
original_image = convert_reference(case01, AUX_PROFILE).prompt[str(nid)]["inputs"]["image"]
node = by_id(case01, nid)
field = "ckpt_name" if nid == 4 else "preprocessor"
selected = "depth_anything_vits14.pth" if nid == 4 else "none"
node["widgets_values"] = [selected, 768]
candidate = convert_reference(case01, AUX_PROFILE)
assert not candidate.issues
assert candidate.prompt[str(nid)]["inputs"][field] == selected
assert candidate.prompt[str(nid)]["inputs"]["resolution"] == 768
assert candidate.prompt[str(nid)]["inputs"]["image"] == original_image
@pytest.mark.parametrize("nid", [1, 4])
@pytest.mark.parametrize(
"change",
[
"provider",
"version",
"missing_provider",
"missing_version",
"extra_widget",
"missing_widget",
"named_widgets",
"bool_resolution",
"float_resolution",
"combo_type",
"extra_socket",
"wrong_socket_type",
"wrong_binding",
"duplicate_socket",
"missing_image",
"wrong_output",
"bad_link",
"bypass",
],
)
def test_bad_aux_contract_fails_whole_graph(case01, nid, change):
node = by_id(case01, nid)
if change == "provider":
node["properties"]["cnr_id"] = "same_name_other_plugin"
elif change == "version":
node["properties"]["ver"] = "1.1.6"
elif change.startswith("missing_") and change in ("missing_provider", "missing_version"):
node["properties"].pop("cnr_id" if change == "missing_provider" else "ver")
elif change == "extra_widget":
node["widgets_values"].append("unknown")
elif change == "missing_widget":
node["widgets_values"].pop()
elif change == "named_widgets":
node["widgets_values"] = {"resolution": 512}
elif change == "bool_resolution":
node["widgets_values"][1] = True
elif change == "float_resolution":
node["widgets_values"][1] = 512.0
elif change == "combo_type":
node["widgets_values"][0] = ["dynamic", "combo"]
elif change == "extra_socket":
node["inputs"].append({"name": "unknown", "type": "STRING", "link": None})
elif change == "wrong_socket_type":
node["inputs"][0]["type"] = "MASK"
elif change == "wrong_binding":
node["inputs"][1]["widget"] = {"name": "resolution"}
elif change == "duplicate_socket":
node["inputs"].append(copy.deepcopy(node["inputs"][0]))
elif change == "missing_image":
node["inputs"].pop(0)
elif change == "wrong_output":
node["outputs"][0]["type"] = "MASK"
elif change == "bad_link":
node["inputs"][0]["link"] = 9999
elif change == "bypass":
node["mode"] = 4
result = convert_reference(case01, AUX_PROFILE)
assert result.reference_only and result.prompt is None and result.issues
def test_dynamic_combo_membership_is_unknown_and_static_enum_is_checked(case01):
# Synthetic selection is deliberately NOT asserted to exist in any environment.
by_id(case01, 1)["widgets_values"][0] = "UninstalledSyntheticPreprocessor"
result = convert_reference(case01, AUX_PROFILE)
assert result.prompt["1"]["inputs"]["preprocessor"] == "UninstalledSyntheticPreprocessor"
assert result.reference_only
assert convert(case01).prompt is None
by_id(case01, 4)["widgets_values"][0] = "unlisted.pth"
assert convert_reference(case01, AUX_PROFILE).prompt is None
def test_unconnected_optional_slots_may_be_absent_but_values_are_never_defaulted(case01):
by_id(case01, 4)["inputs"] = by_id(case01, 4)["inputs"][:1]
assert convert_reference(case01, AUX_PROFILE).prompt is not None
by_id(case01, 4)["widgets_values"] = []
assert convert_reference(case01, AUX_PROFILE).prompt is None
def test_aux_reroute_keeps_nonzero_source_slot_then_rejects_image_type(case01):
# Synthetic mutation of the real path: LoadImage MASK output through Reroute.
source = by_id(case01, 2)
reroute = by_id(case01, 32)
lid = reroute["inputs"][0]["link"]
link = next(link for link in case01["links"] if link[0] == lid)
source["outputs"][0]["links"].remove(lid)
source["outputs"][1]["links"] = [lid]
link[2] = 1
endpoint = resolve_reference_link(
{str(n["id"]): n for n in case01["nodes"]},
{str(link[0]): link for link in case01["links"]},
by_id(case01, 4)["inputs"][0]["link"],
)
assert endpoint == ["2", 1]
result = convert_reference(case01, AUX_PROFILE)
assert result.prompt is None
assert "link_type_unverified" in {i["code"] for i in result.issues}
def test_existing_reroute_guards_apply_to_extended_profile(case01):
by_id(case01, 32)["inputs"][0]["link"] = None
assert convert_reference(case01, AUX_PROFILE).prompt is None
def test_unknown_named_node_is_never_globally_registered(case01):
by_id(case01, 1)["type"] = "UnknownAux"
assert convert_reference(case01, AUX_PROFILE).prompt is None
with pytest.raises(ValueError):
serialize_reference_widgets(by_id(case01, 4), "invented-profile")