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

267 lines
10 KiB
Python

"""Real source/API comparison and synthetic mutations; no upstream code execution."""
import json
from copy import deepcopy
from pathlib import Path
import pytest
from calliope.comfyui.serialization_reference import CASE04_PROFILE
from calliope.comfyui.ui2api import convert, convert_reference
from tests.test_promoted_preview_reference import fixture, real
def node(source, nid):
rows = source["nodes"] + source["definitions"]["subgraphs"][0]["nodes"]
return next(n for n in rows if n["id"] == nid)
def edit(source, nid, name, value):
n = node(source, nid)
names = list(n["widgets_values_named"])
n["widgets_values"][names.index(name)] = value
n["widgets_values_named"][name] = value
def candidate(source):
return convert_reference(source, CASE04_PROFILE)
def test_real_complete_saved_state_keeps_api_differences_and_all_types():
source = real("04")
before = deepcopy(source)
result = candidate(source)
assert result.prompt is not None and result.reference_only
assert result.serialization_stage == "restored_saved_state_before_queue_hooks"
repo = Path(__file__).resolve().parents[2]
api = json.loads((repo / "docs/hel-892/supplied-api/04.api.json").read_bytes())
assert result.prompt.keys() == api.keys()
for nid, expected in api.items():
actual = result.prompt[nid]
assert actual["class_type"] == expected["class_type"]
assert actual["inputs"].keys() == expected["inputs"].keys()
for name, value in expected["inputs"].items():
got = actual["inputs"][name]
assert type(got) is type(value)
if (nid, name) in (("459:451", "unet_name"), ("459:473", "clip_name")):
assert got != value and got.replace("\\", "/") == value
else:
assert got == value
assert "control_after_generate" not in result.prompt["459:458"]["inputs"]
assert set(result.prompt["459:472"]["inputs"]) == {"source"}
assert "preview" not in result.prompt["13"]["inputs"]
assert source == before and convert(source).prompt is None
assert convert_reference(source, "comfy-frontend-1.53.6-structure-reference").prompt is None
assert convert_reference(source, "comfy-frontend-1.53.6-promoted-reference").prompt is None
@pytest.mark.parametrize("value", [0, 1, 987654321, 2**53 - 1])
def test_host_seed_changes_both_typed_targets_with_original_pointers(value):
source = real("04")
edit(source, 459, "seed", value)
# Internal seeds differ deliberately; the host is authoritative.
edit(source, 458, "seed", 111)
edit(source, 471, "sampling_mode.seed", 222)
result = candidate(source)
assert result.prompt is not None
assert result.prompt["459:458"]["inputs"]["seed"] == value
assert result.prompt["459:471"]["inputs"]["sampling_mode.seed"] == value
for row in result.resolved_boundaries:
if row["input_name"] in ("seed", "sampling_mode.seed"):
assert row["value_pointer"] == "/nodes/1/widgets_values/10"
assert row["named_value_pointer"] == "/nodes/1/widgets_values_named/seed"
@pytest.mark.parametrize("mode", ["increment", "decrement", "randomize", "bogus", None])
def test_nonfixed_control_never_becomes_a_fabricated_queue_seed(mode):
source = real("04")
edit(source, 458, "control_after_generate", mode)
result = candidate(source)
assert result.prompt is None and result.issues
assert any("fixed" in x.get("message", "") for x in result.issues)
@pytest.mark.parametrize("value", [-1, 2**53, 2**64, 10**400, True, None, "10", 1.25])
def test_host_seed_bad_domain_or_js_precision_closes_whole_graph(value):
source = real("04")
edit(source, 459, "seed", value)
result = candidate(source)
assert result.prompt is None and result.unresolved_boundaries
@pytest.mark.parametrize(
"nid,name,value",
[
(13, "megapixels", 17),
(13, "aspect_ratio", "invented"),
(458, "steps", 0),
(458, "denoise", 2),
(458, "sampler_name", "invented"),
(471, "sampling_mode.top_k", True),
(471, "sampling_mode.temperature", 0),
(471, "sampling_mode.top_p", float("inf")),
(471, "max_length", 32769),
(471, "sampling_mode", "off"),
(471, "mtp", "invented"),
(461, "format", "avif"),
(461, "format.bit_depth", "32-bit float"),
(461, "filename_prefix", "%date:yyyy%"),
(452, "prompt", "{a|b}"),
(452, "negative_prompt", "// comment"),
(480, "dtype", "fp99"),
],
)
def test_static_constraints_and_uncovered_dynamic_behaviors_fail_closed(nid, name, value):
source = real("04")
edit(source, nid, name, value)
assert candidate(source).prompt is None
@pytest.mark.parametrize(
"nid,name,value",
[
(13, "megapixels", 2.5),
(458, "denoise", 0.25),
(471, "sampling_mode.temperature", 0.5),
(471, "sampling_mode.top_k", 77),
(461, "format.bit_depth", "16-bit"),
(461, "filename_prefix", "offline/changed"),
],
)
def test_proven_scalar_changes_are_serialized_without_api_constants(nid, name, value):
source = real("04")
edit(source, nid, name, value)
result = candidate(source)
key = str(nid) if nid in (13, 461) else f"459:{nid}"
assert result.prompt[key]["inputs"][name] == value
assert type(result.prompt[key]["inputs"][name]) is type(value)
@pytest.mark.parametrize(
"mutation",
[
"provider",
"version",
"extra_widget",
"missing_widget",
"named_conflict",
"bad_binding",
"missing_socket",
"serializer",
"duplicate_socket",
"extra_dynamic",
"extra_control",
"autogrow_name",
"frontend",
"fanout_conflict",
],
)
def test_unknown_layout_bindings_and_fanout_configs_never_return_partial_graph(mutation):
source = real("04")
target = node(source, 471)
if mutation in ("provider", "version"):
target["properties"]["cnr_id" if mutation == "provider" else "ver"] = "unknown"
elif mutation == "extra_widget":
target["widgets_values"].append(None)
elif mutation == "missing_widget":
target["widgets_values"].pop()
elif mutation == "named_conflict":
target["widgets_values_named"]["max_length"] = 12
elif mutation == "bad_binding":
target["inputs"][13]["widget"]["name"] = "seed"
elif mutation == "missing_socket":
target["inputs"].pop()
elif mutation == "serializer":
target["serializeValue"] = "unknown"
elif mutation == "duplicate_socket":
target["inputs"].append(deepcopy(target["inputs"][-1]))
elif mutation == "extra_dynamic":
target["inputs"].append({"name": "sampling_mode.fake", "type": "INT", "link": None})
elif mutation == "extra_control":
target["widgets_values"].insert(9, "fixed")
elif mutation == "autogrow_name":
node(source, 452)["inputs"][1]["name"] = "images.image_17"
elif mutation == "frontend":
source["extra"]["frontendVersion"] = "1.53.7"
elif mutation == "fanout_conflict":
# Redirect seed's second binding to top_k (same INT, different range).
edge = next(e for e in source["definitions"]["subgraphs"][0]["links"] if e["id"] == 697)
target["inputs"][13]["link"] = None
target["inputs"][9]["link"] = 697
edge["target_slot"] = 9
result = candidate(source)
assert result.prompt is None and result.issues
def test_external_seed_link_wins_over_invalid_stored_host_scalar():
source = real("04")
# ResolutionSelector.width is INT. This is a synthetic link, not generation.
source["links"].append([9999, 13, 0, 459, 10, "INT"])
node(source, 13)["outputs"][0]["links"].append(9999)
node(source, 459)["inputs"][10]["link"] = 9999
edit(source, 459, "seed", None)
result = candidate(source)
assert result.prompt["459:458"]["inputs"]["seed"] == ["13", 0]
assert result.prompt["459:471"]["inputs"]["sampling_mode.seed"] == ["13", 0]
def test_model_file_string_remains_dynamic_availability_unknown():
source = real("04")
edit(source, 459, "unet_name", "not-installed.safetensors")
assert candidate(source).prompt["459:451"]["inputs"]["unet_name"] == "not-installed.safetensors"
edit(source, 451, "weight_dtype", "not-a-static-option")
assert candidate(source).prompt is None
def test_multiple_instance_host_values_do_not_cross():
source = fixture("one")
second = deepcopy(source["nodes"][0])
second["id"] = 6
second["widgets_values"] = ["two"]
source["nodes"].append(second)
result = candidate(source)
assert result.prompt["5:3"]["inputs"]["value"] == "one"
assert result.prompt["6:3"]["inputs"]["value"] == "two"
def test_nested_hosts_and_same_name_root_keep_distinct_values():
source = fixture("outer")
outer = source["definitions"]["subgraphs"][0]
nested = deepcopy(outer)
nested["id"] = "aaaaaaaa-bbbb-cccc-dddd-ffffffffffff"
host = deepcopy(source["nodes"][0])
host.update(id=3, type=nested["id"], widgets_values=["inner"])
host["inputs"][0]["link"] = 10
host["outputs"][0]["links"] = [11]
outer["nodes"] = [host]
source["definitions"]["subgraphs"].append(nested)
root = deepcopy(nested["nodes"][0])
root["id"] = 9
root["inputs"][0]["link"] = None
root["outputs"][0]["links"] = []
root["widgets_values"] = ["root"]
source["nodes"].append(root)
result = candidate(source)
assert result.prompt["5:3:3"]["inputs"]["value"] == "outer"
assert result.prompt["9"]["inputs"]["value"] == "root"
def test_boundary_raw_text_bypasses_internal_dynamic_serializer():
source = real("04")
edit(source, 459, "negative_prompt", "{kept|raw}")
result = candidate(source)
assert result.prompt["459:452"]["inputs"]["negative_prompt"] == "{kept|raw}"
def test_seed_fanout_order_selects_source_without_equating_controller_construction():
source = real("04")
definition = source["definitions"]["subgraphs"][0]
boundary = next(s for s in definition["inputs"] if s["name"] == "seed")
boundary["linkIds"].reverse()
edit(source, 459, "seed", 12345)
result = candidate(source)
assert result.prompt["459:458"]["inputs"]["seed"] == 12345
assert result.prompt["459:471"]["inputs"]["sampling_mode.seed"] == 12345
edit(source, 458, "control_after_generate", "randomize")
assert candidate(source).prompt is None # conservative even if non-first source