"""Saved-state transformation tests; never execute upstream JS or a ComfyUI job.""" from __future__ import annotations 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.structure_reference import ( SETGET_PROFILES, project_reference_setget, ) from calliope.comfyui.ui2api import convert, convert_reference REPO = Path(__file__).resolve().parents[2] PROFILE = next(iter(SETGET_PROFILES)) def virtual(nid, kind, name, typ="IMAGE"): return { "id": nid, "type": kind, "mode": 0, "properties": {"aux_id": "kijai/ComfyUI-KJNodes", "previousName": "old"}, "widgets_values": [name], "inputs": [{"name": typ, "type": typ, "link": None}] if kind == "SetNode" else [], "outputs": [{"name": typ, "type": typ, "links": []}], } def connect(graph, lid, origin, oslot, target, tslot, typ="IMAGE"): nodes = {n["id"]: n for n in graph["nodes"]} nodes[origin]["outputs"][oslot].setdefault("links", []).append(lid) nodes[target]["inputs"][tslot]["link"] = lid graph["links"].append([lid, origin, oslot, target, tslot, typ]) def synthetic(): graph = { "nodes": [ { "id": 1, "type": "LoadImage", "widgets_values": ["one.png", "image"], "inputs": [], "outputs": [{"type": "IMAGE", "links": []}, {"type": "MASK", "links": []}], }, virtual(2, "SetNode", "图片"), virtual(3, "GetNode", "图片"), { "id": 4, "type": "PreviewImage", "widgets_values": [], "inputs": [{"name": "images", "type": "IMAGE", "link": None}], "outputs": [], }, ], "links": [], } connect(graph, 1, 1, 0, 2, 0) connect(graph, 2, 3, 0, 4, 0) return graph def test_fixed_originals_and_registration_call_chain(): root = REPO / "docs/hel-892/setget-reference" assert len(verify(root)["sources_verified"]) == 19 js = (root / "web__js__setgetnodes.js").read_text() for text in ( 'LiteGraph.registerNodeType("SetNode", SetNode)', 'LiteGraph.registerNodeType("GetNode", GetNode)', "this.isVirtualNode = true", "getInputLink(slot)", "n.type === 'SetNode' && n.widgets[0].value === name", "if (app.configuringGraph) return;", "if (this._justAdded && !app.configuringGraph)", "this.properties.previousName = this.widgets[0].value;", ): assert text in js for v in SETGET_PROFILES.values(): dto = (root / f"{v}__src__lib__litegraph__src__subgraph__ExecutableNodeDTO.ts").read_text() assert "const virtualLink = this.node.getInputLink(slot)" in dto assert "return inputNodeDto.resolveInput(virtualLink.target_slot, visited, type)" in dto node = (root / f"{v}__src__lib__litegraph__src__LGraphNode.ts").read_text() assert "widget.value = info.widgets_values[i++]" in node assert "getInputLink(slot: number)" in node @pytest.mark.parametrize("profile", SETGET_PROFILES) def test_complete_projection_and_production_stays_closed(profile): g = synthetic() g["extra"] = {"frontendVersion": SETGET_PROFILES[profile]} original = copy.deepcopy(g) result = convert_reference(g, profile) assert result.prompt is not None, result.issues assert set(result.prompt) == {"1", "4"} assert result.prompt["4"]["inputs"]["images"] == ["1", 0] assert result.prompt["1"]["inputs"]["image"] == "one.png" assert [h["node_id"] for h in result.virtual_resolutions[0]["hops"]] == ["3", "2", "1"] assert result.reference_only assert convert(g).prompt is None assert g == original @pytest.mark.parametrize("name", ["图片", " A ", "a", "A", "e\u0301", "\u00e9", " "]) def test_exact_names_rename_previous_name_and_upstream_changes(name): g = synthetic() g["nodes"][1]["widgets_values"] = [name] g["nodes"][2]["widgets_values"] = [name] g["nodes"][1]["properties"]["previousName"] = "图片" result = convert_reference(g, PROFILE) assert result.prompt is not None assert result.virtual_resolutions[0]["hops"][0]["name"] == name g["nodes"][0]["widgets_values"][0] = "changed.png" assert convert_reference(g, PROFILE).prompt["1"]["inputs"]["image"] == "changed.png" g["nodes"][2]["widgets_values"] = ["not_current"] g["nodes"][1]["properties"]["previousName"] = "not_current" g["nodes"][1]["title"] = "not_current" assert convert_reference(g, PROFILE).prompt is None @pytest.mark.parametrize("other", ["a", "A ", " A", "e\u0301"]) def test_no_name_normalization(other): g = synthetic() g["nodes"][1]["widgets_values"] = ["A" if other != "e\u0301" else "\u00e9"] g["nodes"][2]["widgets_values"] = [other] assert convert_reference(g, PROFILE).prompt is None def test_multiple_names_and_changing_binding_selects_correct_source(): g = synthetic() other = copy.deepcopy(g["nodes"][0]) other["id"] = 11 other["widgets_values"][0] = "two.png" other["outputs"][0]["links"] = [] g["nodes"] += [other, virtual(12, "SetNode", "second")] connect(g, 11, 11, 0, 12, 0) assert convert_reference(g, PROFILE).prompt["4"]["inputs"]["images"] == ["1", 0] g["nodes"][2]["widgets_values"] = ["second"] result = convert_reference(g, PROFILE) assert result.prompt["4"]["inputs"]["images"] == ["11", 0] assert result.prompt["11"]["inputs"]["image"] == "two.png" def test_fanout_direct_set_and_multilevel_reroute_preserve_original_slot(): g = synthetic() g["nodes"][0]["outputs"][1]["type"] = "IMAGE" # topology fixture only g["nodes"][0]["outputs"][0]["links"] = [] g["nodes"][0]["outputs"][1]["links"] = [1] g["links"][0][2] = 1 g["nodes"] += [ virtual(5, "GetNode", "图片"), virtual(6, "SetNode", "next"), virtual(7, "GetNode", "next"), { "id": 8, "type": "Reroute", "widgets_values": [], "inputs": [{"name": "", "type": "IMAGE", "link": None}], "outputs": [{"name": "", "type": "IMAGE", "links": []}], }, copy.deepcopy(g["nodes"][3]), ] g["nodes"][-1]["id"] = 9 g["nodes"][-1]["inputs"][0]["link"] = None connect(g, 3, 5, 0, 6, 0) connect(g, 4, 7, 0, 8, 0) connect(g, 5, 8, 0, 9, 0) # Direct Set output feeds an additional sink; Get fanout is independent. sink = copy.deepcopy(g["nodes"][3]) sink["id"] = 10 sink["inputs"][0]["link"] = None g["nodes"].append(sink) connect(g, 6, 2, 0, 10, 0) projected = project_reference_setget(g) assert projected.graph is not None, projected.issues assert {tuple(e["endpoint"]) for e in projected.resolved} == {("1", 1)} assert len(projected.resolved) == 3 last = next(r for r in projected.resolved if r["target_node_id"] == "9") assert [h["node_id"] for h in last["hops"]] == ["8", "7", "6", "5", "2", "1"] assert [e[2] for e in projected.graph["links"]] == [1, 1, 1] @pytest.mark.parametrize( "mutation", [ "duplicate", "missing_set", "empty", "bad_name", "extra_widget", "mapping_widget", "bad_aux", "missing_aux", "literal_aux", "bad_version", "bad_cnr", "extra_input", "extra_output", "bad_type", "wildcard", "union", "mode", "bool_mode", "custom_serializer", "custom_properties", "custom_binding", "bad_slot_name", "bad_reciprocal", "dangling", "bool_slot", "bool_link", "wrong_edge_type", "physical_cycle", "logical_cycle", "unused_bad_get", "unused_bad_set", "nested", "uuid", "ue_edges", ], ) def test_fail_closed_mutations_including_unused_branches(mutation): g = synthetic() s = g["nodes"][1] get = g["nodes"][2] if mutation == "duplicate": extra = copy.deepcopy(s) extra["id"] = 22 extra["inputs"][0]["link"] = None g["nodes"].append(extra) elif mutation == "missing_set": s["widgets_values"] = ["other"] elif mutation == "empty": get["widgets_values"] = [""] elif mutation == "bad_name": get["widgets_values"] = [True] elif mutation == "extra_widget": get["widgets_values"].append("extra") elif mutation == "mapping_widget": get["widgets_values"] = {"Constant": "图片"} elif mutation == "bad_aux": s["properties"]["aux_id"] = "rgthree/rgthree-comfy" elif mutation == "missing_aux": s["properties"].pop("aux_id") elif mutation == "literal_aux": s["properties"]["aux_id"] = "SetNode" elif mutation == "bad_version": s["properties"]["ver"] = "1.0.0" elif mutation == "bad_cnr": s["properties"]["cnr_id"] = "other" elif mutation == "extra_input": get["inputs"] = [{"name": "IMAGE", "type": "IMAGE", "link": None}] elif mutation == "extra_output": get["outputs"].append(copy.deepcopy(get["outputs"][0])) elif mutation == "bad_type": get["outputs"][0]["type"] = "FLOAT" g["links"][1][5] = "FLOAT" g["nodes"][3]["inputs"][0]["type"] = "FLOAT" elif mutation in ("wildcard", "union"): s["outputs"][0]["type"] = "*" if mutation == "wildcard" else "IMAGE,MASK" elif mutation == "mode": get["mode"] = 4 elif mutation == "bool_mode": get["mode"] = False elif mutation == "custom_serializer": get["serializeValue"] = "override" elif mutation == "custom_properties": s["properties"]["getInputLink"] = "override" elif mutation == "custom_binding": s["properties"]["ue_properties"] = {"widget_ue_connectable": {"Constant": True}} elif mutation == "bad_slot_name": s["inputs"][0]["name"] = "other" elif mutation == "bad_reciprocal": g["nodes"][0]["outputs"][0]["links"] = [] elif mutation == "dangling": g["links"][0][1] = 999 elif mutation == "bool_slot": g["links"][0][2] = False elif mutation == "bool_link": g["links"][0][0] = True elif mutation == "wrong_edge_type": g["links"][0][5] = "MASK" elif mutation in ("physical_cycle", "logical_cycle"): g["nodes"][0]["outputs"][0]["links"] = [] g["links"].pop(0) connect(g, 1, 2 if mutation == "physical_cycle" else 3, 0, 2, 0) elif mutation == "unused_bad_get": g["nodes"].append(virtual(99, "GetNode", "missing")) elif mutation == "unused_bad_set": g["nodes"].append(virtual(99, "SetNode", "unused")) elif mutation == "nested": g["definitions"] = {"subgraphs": [{"id": "any"}]} elif mutation == "uuid": g["nodes"].append({"id": 88, "type": "e61f4fd0-e251-4d58-bfa4-cf163fa86577"}) elif mutation == "ue_edges": g["extra"] = {"ue_links": [1]} result = convert_reference(g, PROFILE) assert result.prompt is None, mutation assert result.issues, mutation def test_unknown_ordinary_node_blocks_whole_graph_with_reference_trace_retained(): g = synthetic() g["nodes"][0]["type"] = "UnknownImageProducer" result = convert_reference(g, PROFILE) assert result.prompt is None assert result.virtual_resolutions[0]["endpoint"] == ["1", 0] assert any(i["code"] == "reference_node_unsupported" for i in result.issues) def test_ambiguous_provider_trace_is_explicitly_untrusted(): g = synthetic() g["nodes"][1]["properties"]["aux_id"] = "SetNode" result = convert_reference(g, PROFILE) assert result.prompt is None and result.diagnostic_graph is None trace = result.virtual_resolutions[0] assert trace["endpoint"] == ["1", 0] assert trace["authority"] == "literal_topology_only" and trace["binding_rejections"] def test_wrong_frontend_missing_policy_and_profile_inventory(): p1486 = list(SETGET_PROFILES)[1] assert convert_reference(synthetic(), p1486).prompt is None for v in ["1.48.7", "1.53.6", "1.37.11-other"]: g = synthetic() g["extra"] = {"frontendVersion": v} assert convert_reference(g, PROFILE).prompt is None assert ( len( [ p for p in audit.REFERENCE_PROFILES[:26] if p not in ( *audit.VIDEOINFO_PROFILES, *audit.SCAIL_LOAD_PROFILES, audit.GRID_PROFILE, audit.H3_PROFILE, ) ] ) == 20 ) @pytest.mark.parametrize("profile", SETGET_PROFILES) def test_vhs_composition_preserves_nonzero_audio_slot_and_exact_commits(profile): prefix = "example_ComfyUI_workflows/0-基础处理:图像合并-音频分离-视频截取合并-图像视频放大/" source = json.loads((REPO / (prefix + "多视频合成分屏展示.json")).read_bytes()) load = next(n for n in source["nodes"] if n["id"] == 17) source = json.loads( ( REPO / ( "example_ComfyUI_workflows/4-视频生成/Minimax-h3/" "Minimax_H3_T8双时钟采样_参考生视频-8步高质量极速版.json" ) ).read_bytes() ) combine = next(n for n in source["nodes"] if n["id"] == 12) for n in (load, combine): n.pop("widgets_values_named", None) for s in n["inputs"]: s["link"] = None for s in n["outputs"]: s["links"] = [] graph = { "nodes": [ load, combine, virtual(900, "SetNode", "audio", "AUDIO"), virtual(901, "GetNode", "audio", "AUDIO"), ], "links": [], "extra": {"frontendVersion": SETGET_PROFILES[profile]}, } connect(graph, 1, 17, 0, 12, 0) connect(graph, 2, 17, 2, 900, 0, "AUDIO") connect(graph, 3, 901, 0, 12, 1, "AUDIO") result = convert_reference(graph, profile) assert result.prompt is not None, result.issues assert result.prompt["12"]["inputs"]["audio"] == ["17", 2] assert result.prompt["12"]["inputs"]["images"] == ["17", 0] assert result.prompt["17"]["inputs"]["force_rate"] == 16 load["widgets_values"]["force_rate"] = 20 assert convert_reference(graph, profile).prompt["17"]["inputs"]["force_rate"] == 20 load["properties"]["ver"] = "1.7.9" assert convert_reference(graph, profile).prompt is None @pytest.mark.parametrize( "mutation", ["reroute_type", "ordinary_override", "ordinary_mode", "ordinary_cycle"] ) def test_combined_structure_rejects_other_graph_overrides_and_cycles(mutation): g = synthetic() if mutation == "reroute_type": g["nodes"][1] = { "id": 2, "type": "Reroute", "widgets_values": [], "inputs": [{"name": "", "type": "IMAGE", "link": 1}], "outputs": [{"type": "MASK", "links": []}], } elif mutation == "ordinary_override": g["nodes"][0]["resolveOutput"] = "custom" elif mutation == "ordinary_mode": g["nodes"][0]["mode"] = False else: # Ordinary producer feeds Set, Get feeds the same producer input. g["nodes"][0]["inputs"] = [{"name": "images", "type": "IMAGE", "link": None}] connect(g, 3, 3, 0, 1, 0) assert convert_reference(g, PROFILE).prompt is None def test_historical_18_profiles_attempts_gaps_and_six_comparisons_unchanged(): old = json.loads((REPO / "docs/hel-892/vhs-evidence/library.json").read_bytes()) baseline = tuple( p for p in audit.REFERENCE_PROFILES[:26] if p not in ( *SETGET_PROFILES, *audit.VIDEOINFO_PROFILES, *audit.SCAIL_LOAD_PROFILES, audit.GRID_PROFILE, audit.H3_PROFILE, ) ) now = audit.audit_library( REPO, tuple(p for p in audit.REFERENCE_PROFILES[:26] if p != audit.H3_PROFILE), baseline_profiles=baseline, ) actual_old = audit.audit_library(REPO, baseline) assert len(baseline) == 18 assert actual_old["gap_groups"] == old["gap_groups"] for historical, previous, current in zip(old["cases"], actual_old["cases"], now["cases"]): assert historical["source_path"] == previous["source_path"] == current["source_path"] assert historical["attempts"] == previous["attempts"] == current["attempts"][:18] assert ( historical.get("comparisons") == previous.get("comparisons") == current.get("comparisons") ) assert now["coverage_delta"]["baseline_profiles"] == list(baseline) assert audit.audit(REPO, "case04-batch-reference") == json.loads( (REPO / "docs/hel-892/media-evidence/protected-six-cases.json").read_bytes() )