"""Saved state contracts; synthetic upstream helpers confer no public support.""" import ast 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.h3_chain_reference import ( CHAIN_PROFILE, CLIP_TYPES, LAYOUTS, SAMPLERS, WEIGHT_DTYPES, definitions, saved_values, serialize_node, ) from calliope.comfyui.h3_core_reference import CORE_PROFILE from calliope.comfyui.ui2api import convert, convert_reference REPO = Path(__file__).resolve().parents[2] PATH = next((REPO / "example_ComfyUI_workflows").rglob("minimax-h3文生视频无审核版.json")) IDS = [119, 120, 127, 128, 135, 126, 125, 122, 121, 130, 92] def real(): return json.loads(PATH.read_bytes()) def node(nid): return copy.deepcopy(next(n for n in real()["nodes"] if n["id"] == nid)) def single(nid): n = node(nid) for s in n["inputs"]: s["link"] = None for s in n["outputs"]: s["links"] = None return {"nodes": [n], "links": [], "extra": {}} def closed(g): old = copy.deepcopy(g) r = convert_reference(g, CHAIN_PROFILE) assert r.prompt is None and r.issues and r.reference_only and g == old assert all("pointer" in i for i in r.issues) return r def test_actual_main_ten_types_eleven_instances_one_layout_conflict(): g = real() nodes = [n for n in g["nodes"] if n["type"] in LAYOUTS] assert len(LAYOUTS) == 10 and len(nodes) == 11 and len(definitions()) == 16 for n in nodes: if n["id"] == 130: with pytest.raises(ValueError, match="布局"): serialize_node(n) assert n["widgets_values"] == [24, 8, "sRGB"] else: assert serialize_node(n) == saved_values(n) closed(g) a = audit.profile_attempt(g, CHAIN_PROFILE) es = a["supported_node_evidence"] assert len(es) == 18 and sum(e["layout_accepted"] for e in es) == 15 second = [e for e in es if e["type"] in LAYOUTS] assert len(second) == 11 and sum(e["layout_accepted"] for e in second) == 10 assert sum(len(e["saved_fields"]) for e in second) == 11 rejected = next(e for e in second if e["node_id"] == "130") assert rejected["saved_fields"] == [] and len(rejected["raw_saved_values"]) == 3 assert rejected["raw_saved_values"][2]["value"] == "sRGB" assert a["assumptions"]["dynamic_model_membership"].startswith("unknown") assert convert(g).prompt is None @pytest.mark.parametrize("nid", IDS) @pytest.mark.parametrize( "mutation", [ "provider", "version", "aux", "mode", "ue", "hook", "short", "long", "named-missing", "named-extra", "named-conflict", ], ) def test_identity_restore_layout_coverage_conflicts(nid, mutation): g = single(nid) n = g["nodes"][0] if nid == 130: n["widgets_values"] = [24, 8] # synthetic fixed schema layout only if mutation == "provider": n["properties"]["cnr_id"] = "other" elif mutation == "version": n["properties"]["ver"] = "0.30.0" elif mutation == "aux": n["properties"]["aux_id"] = "other" elif mutation == "mode": n["mode"] = True elif mutation == "ue": n["properties"]["ue_properties"] = {} elif mutation == "hook": n["serializeValue"] = "unknown" elif mutation == "short": if n["widgets_values"]: n["widgets_values"].pop() else: n["widgets_values"] = None elif mutation == "long": n["widgets_values"].append("extra") else: n["widgets_values_named"] = dict(zip(LAYOUTS[n["type"]], n["widgets_values"])) if mutation == "named-extra": n["widgets_values_named"]["extra"] = 0 elif LAYOUTS[n["type"]]: k = LAYOUTS[n["type"]][0] if mutation == "named-missing": n["widgets_values_named"].pop(k) else: n["widgets_values_named"][k] = True else: n["widgets_values_named"] = None with pytest.raises((ValueError, TypeError)): serialize_node(n) closed(g) @pytest.mark.parametrize( "nid,index,value", [ (119, 0, ""), (119, 0, None), (120, 0, True), (127, 0, []), (127, 1, "fp16"), (128, 0, 17), (128, 1, "unknown"), (128, 2, "cuda"), (135, 0, "unknown"), (130, 0, True), (130, 0, 0), (130, 0, 121), (130, 0, float("nan")), (130, 0, float("inf")), (130, 1, 8.0), (130, 1, True), (130, 1, 7), (130, 1, 11), (92, 0, "%date:yyyy%"), (92, 0, "%name.value%"), (92, 0, "%width%"), (92, 0, "%"), (92, 0, 0), (92, 1, "webm"), (92, 2, "h264"), (92, 2, {"codec": "auto"}), ], ) def test_types_static_enums_ranges_dynamic_branches_and_context_prefix_rejected(nid, index, value): n = node(nid) if nid == 130: n["widgets_values"] = [24, 8] n["widgets_values"][index] = value with pytest.raises(ValueError): saved_values(n) @pytest.mark.parametrize("nid", [119, 120, 127, 128, 135, 126, 125, 122, 121, 92]) @pytest.mark.parametrize( "mutation", [ "output-name", "output-type", "output-extra", "output-index", "input-duplicate", "input-unknown", "input-type", "binding", ], ) def test_all_output_and_input_slots_strict(nid, mutation): n = node(nid) if mutation == "output-name": n["outputs"][0]["name"] = "unknown" elif mutation == "output-type": n["outputs"][0]["type"] = "UNKNOWN" elif mutation == "output-extra": n["outputs"].append(copy.deepcopy(n["outputs"][0])) elif mutation == "output-index": n["outputs"][0]["slot_index"] = True else: spec = definitions()[n["type"]]["input"] name, typ = next(iter({**spec["required"], **spec["optional"]}.items())) n["inputs"] = [{"name": name, "type": typ[0], "link": None}] if mutation == "input-duplicate": n["inputs"] *= 2 elif mutation == "input-unknown": n["inputs"][0]["name"] = "unknown" elif mutation == "input-type": n["inputs"][0]["type"] = "UNKNOWN" else: n["inputs"][0]["widget"] = {"name": "unknown"} with pytest.raises(ValueError): serialize_node(n) @pytest.mark.parametrize("nid", [119, 120, 127, 128, 135]) def test_loaders_preserve_opaque_model_strings_and_explicit_identities(nid): g = single(nid) n = g["nodes"][0] if nid != 135: n["widgets_values"][0] = "folder\\literal/../原名.safetensors" before = copy.deepcopy(g) r = convert_reference(g, CHAIN_PROFILE) assert r.prompt is not None and r.reference_only and g == before assert r.prompt[str(nid)]["inputs"] == saved_values(n) n["widgets_values_named"] = dict(zip(LAYOUTS[n["type"]], n["widgets_values"])) n["properties"].update(cnr_id="comfy-core", ver="0.33.0") g["extra"]["frontendVersion"] = "1.53.10" assert convert_reference(g, CHAIN_PROFILE).prompt == r.prompt assert convert_reference(g, CORE_PROFILE).prompt is None def synthetic_chain(monkeypatch, audio=True): # Only this local test injects a schema+serializer. It is NOT publicly registered. import calliope.comfyui.ui2api as ui g = {"nodes": [node(n) for n in [125, 122, 121, 119, 120, 130, 92]], "links": [], "extra": {}} for n in g["nodes"]: for s in n["inputs"]: s["link"] = None for s in n["outputs"]: s["links"] = None if n["id"] == 130: n["widgets_values"] = [24, 8] helper = { "id": 999, "type": "TEST_ONLY_UPSTREAM", "inputs": [], "outputs": [ {"name": x, "type": x, "links": None} for x in ["NOISE", "GUIDER", "SAMPLER", "SIGMAS", "LATENT"] ], "widgets_values": [], "mode": 0, } g["nodes"].append(helper) def connect(src, so, dst, name): a = next(n for n in g["nodes"] if n["id"] == src) b = next(n for n in g["nodes"] if n["id"] == dst) si = next(i for i, s in enumerate(b["inputs"]) if s["name"] == name) lid = len(g["links"]) + 1 a["outputs"][so]["links"] = (a["outputs"][so]["links"] or []) + [lid] b["inputs"][si]["link"] = lid g["links"].append([lid, src, so, dst, si, a["outputs"][so]["type"]]) for si, name in enumerate(["noise", "guider", "sampler", "sigmas", "latent_image"]): connect(999, si, 125, name) connect(125, 1, 122, "samples") connect(119, 0, 122, "vae") connect(125, 0, 121, "samples") connect(120, 0, 121, "vae") connect(122, 0, 130, "images") if audio: connect(121, 0, 130, "audio") else: g["nodes"] = [n for n in g["nodes"] if n["id"] not in [121, 120]] g["links"] = [e for e in g["links"] if e[3] != 121] g["nodes"][0]["outputs"][0]["links"] = None connect(130, 0, 92, "video") # Reject helper before installing local injection. assert convert_reference(g, CHAIN_PROFILE).prompt is None original_defs = ui.reference_definitions original_ser = ui.serialize_reference_widgets def defs(profile): d = original_defs(profile) if profile == CHAIN_PROFILE: d["TEST_ONLY_UPSTREAM"] = { "input": {"required": {}}, "output": [s["type"] for s in helper["outputs"]], } return d monkeypatch.setattr(ui, "reference_definitions", defs) monkeypatch.setattr( ui, "serialize_reference_widgets", lambda n, p: {} if n["type"] == "TEST_ONLY_UPSTREAM" else original_ser(n, p), ) return g @pytest.mark.parametrize("audio", [True, False]) def test_synthetic_video_chain_nonzero_slot_optional_audio_and_instance_propagation( monkeypatch, audio ): g = synthetic_chain(monkeypatch, audio) r = convert_reference(g, CHAIN_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["122"]["inputs"]["samples"] == ["125", 1] assert ("audio" in r.prompt["130"]["inputs"]) == audio assert r.prompt["92"]["inputs"]["codec"] == "auto" for n in g["nodes"]: if n["id"] == 125: n["id"] = 888 for e in g["links"]: if e[1] == 125: e[1] = 888 if e[3] == 125: e[3] = 888 r = convert_reference(g, CHAIN_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["122"]["inputs"]["samples"] == ["888", 1] @pytest.mark.parametrize( "mutation", [ "frontend", "ue", "mode", "duplicate", "bad-ref", "slot", "type", "reverse", "cycle", "codec-line", ], ) def test_synthetic_structure_closes_entire_graph(monkeypatch, mutation): g = synthetic_chain(monkeypatch) if mutation == "frontend": g["extra"]["frontendVersion"] = "1.53.6" elif mutation == "ue": g["extra"]["ue_properties"] = {} elif mutation == "mode": g["nodes"][0]["mode"] = 4 elif mutation == "duplicate": g["links"].append(g["links"][0]) elif mutation == "bad-ref": g["links"][0][1] = 888 elif mutation == "slot": g["links"][0][2] = 99 elif mutation == "type": g["links"][0][5] = "UNKNOWN" elif mutation == "reverse": g["nodes"][0]["inputs"][0]["link"] = 999 elif mutation == "cycle": g["links"].append([999, 125, 0, 125, 0, "LATENT"]) else: n = next(n for n in g["nodes"] if n["id"] == 92) n["inputs"].append({"name": "codec", "type": "COMFY_DYNAMICCOMBO_V3", "link": 1}) closed(g) def test_fixed_widget_physical_input_wins(monkeypatch): g = single(133) b = node(130) b["widgets_values"] = [24, 8] b["inputs"] = [{"name": "fps", "type": "FLOAT", "link": 1, "widget": {"name": "fps"}}] # Local helper supplies IMAGE; all physical paths checked by real validator. # First test the serializer/evidence independently of required IMAGE input. assert serialize_node(b) == {"fps": 24, "bit_depth": 8} g["nodes"].append(b) g["nodes"][0]["outputs"][0]["links"] = [1] g["links"] = [[1, 133, 0, 130, 0, "FLOAT"]] a = audit.profile_attempt(g, CHAIN_PROFILE) e = next(e for e in a["supported_node_evidence"] if e["node_id"] == "130") assert next(f for f in e["effective_field_evidence"] if f["field"] == "fps")["value"] == [ "133", 0, ] # Missing images still prevents ANY partial candidate. closed(g) def test_old_27_profiles_every_attempt_gap_hash_and_comparison_frozen(): old = json.loads((REPO / "docs/hel-892/h3-core-evidence/library.json").read_bytes()) ps = tuple(old["profiles"]) assert len(ps) == 27 and ps == audit.REFERENCE_PROFILES[:27] before = audit.audit_library(REPO, ps) now = audit.audit_library(REPO, baseline_profiles=ps) assert before["gap_groups"] == old["gap_groups"] for a, b, c in zip(old["cases"], before["cases"], now["cases"]): assert a["attempts"] == b["attempts"] == c["attempts"][:27] assert a["candidate_hashes"] == c["candidate_hashes"] assert a.get("comparisons") == b.get("comparisons") == c.get("comparisons") assert now["coverage_delta"]["new_complete_paths"] == [] assert len(now["coverage_delta"]["current_complete_paths"]) == 9 for p in PATH.parent.glob("*.json"): closed(json.loads(p.read_bytes())) def test_pinned_manifests_and_literal_enums_without_upstream_execution(): assert not verify(REPO / "docs/hel-892/h3-chain-reference")["upstream_code_executed"] tree = ast.parse((REPO / "docs/hel-892/h3-core-reference/core__comfy__samplers.py").read_text()) assignment = next( n for n in tree.body if isinstance(n, ast.Assign) and any(isinstance(t, ast.Name) and t.id == "KSAMPLER_NAMES" for t in n.targets) ) assert tuple(ast.literal_eval(assignment.value)) + ("ddim", "uni_pc", "uni_pc_bh2") == SAMPLERS tree = ast.parse((REPO / "docs/hel-892/h3-core-reference/core__nodes.py").read_text()) for clsname, key, expected in [ ("UNETLoader", "weight_dtype", WEIGHT_DTYPES), ("CLIPLoader", "type", CLIP_TYPES), ]: cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == clsname) fn = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "INPUT_TYPES") ret = next(n for n in fn.body if isinstance(n, ast.Return)).value required = next( v for k, v in zip(ret.keys, ret.values) if ast.literal_eval(k) == "required" ) value = next( v for k, v in zip(required.keys, required.values) if ast.literal_eval(k) == key ) assert tuple(ast.literal_eval(value.elts[0])) == expected @pytest.mark.parametrize("dtype", WEIGHT_DTYPES) def test_all_static_precision_options(dtype): n = node(127) n["widgets_values"][1] = dtype assert saved_values(n)["weight_dtype"] == dtype @pytest.mark.parametrize("kind", CLIP_TYPES) def test_all_static_clip_types(kind): n = node(128) n["widgets_values"][1] = kind assert saved_values(n)["type"] == kind @pytest.mark.parametrize("sampler", SAMPLERS) def test_all_fixed_sampler_options(sampler): n = node(135) n["widgets_values"][0] = sampler assert saved_values(n)["sampler_name"] == sampler def test_complete_synthetic_physical_fps_overrides_save_and_upstream_switch(monkeypatch): g = synthetic_chain(monkeypatch) f = single(133)["nodes"][0] f["widgets_values"] = [60.0] f["outputs"][0]["links"] = [100] g["nodes"].append(f) video = next(n for n in g["nodes"] if n["id"] == 130) video["inputs"].append({"name": "fps", "type": "FLOAT", "link": 100, "widget": {"name": "fps"}}) g["links"].append([100, 133, 0, 130, 2, "FLOAT"]) r = convert_reference(g, CHAIN_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["130"]["inputs"]["fps"] == ["133", 0] assert video["widgets_values"] == [24, 8] # Replace a LATENT upstream while retaining nonzero source slot semantics. edge = next(e for e in g["links"] if e[3] == 122 and e[4] == 0) sampler = next(n for n in g["nodes"] if n["id"] == 125) helper = next(n for n in g["nodes"] if n["id"] == 999) sampler["outputs"][1]["links"].remove(edge[0]) helper["outputs"][4]["links"].append(edge[0]) edge[1], edge[2] = 999, 4 r = convert_reference(g, CHAIN_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["122"]["inputs"]["samples"] == ["999", 4] def test_ten_real_class_registrations_and_schema_field_orders_ast_only(): root = REPO / "docs/hel-892" modules = { "h3-core-reference/core__nodes.py": { "VAELoader": ["vae_name"], "UNETLoader": ["unet_name", "weight_dtype"], "CLIPLoader": ["clip_name", "type", "device"], "VAEDecode": ["samples", "vae"], }, "h3-core-reference/core__comfy_extras__nodes_custom_sampler.py": { "KSamplerSelect": ["sampler_name"], "BasicGuider": ["model", "conditioning"], "SamplerCustomAdvanced": ["noise", "guider", "sampler", "sigmas", "latent_image"], }, "h3-chain-reference/core__comfy_extras__nodes_audio.py": { "VAEDecodeAudio": ["samples", "vae"] }, "h3-chain-reference/core__comfy_extras__nodes_video.py": { "CreateVideo": ["images", "fps", "audio", "bit_depth"], "SaveVideo": ["video", "filename_prefix", "format", "codec"], }, } for path, specs in modules.items(): tree = ast.parse((root / path).read_text()) if path.endswith("core__nodes.py"): mapping = next( n for n in tree.body if isinstance(n, ast.Assign) and any( isinstance(t, ast.Name) and t.id == "NODE_CLASS_MAPPINGS" for t in n.targets ) ) registered = { ast.literal_eval(k): v.id for k, v in zip(mapping.value.keys, mapping.value.values) } else: fn = next( n for n in ast.walk(tree) if isinstance(n, ast.AsyncFunctionDef) and n.name == "get_node_list" ) registered = { n.id: n.id for r in fn.body if isinstance(r, ast.Return) for n in r.value.elts } for name, expected in specs.items(): assert registered[name] == name cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == name) if path.endswith("core__nodes.py"): fn = next( n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "INPUT_TYPES" ) ret = next(n for n in fn.body if isinstance(n, ast.Return)).value fields = [ ast.literal_eval(k) for k, v in zip(ret.keys, ret.values) if ast.literal_eval(k) in ("required", "optional") for k in v.keys ] else: fn = next( n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "define_schema" ) schema = next( n for n in ast.walk(fn) if isinstance(n, ast.Call) and isinstance(n.func, ast.Attribute) and n.func.attr == "Schema" ) kw = {k.arg: k.value for k in schema.keywords} assert ast.literal_eval(kw["node_id"]) == name fields = [ast.literal_eval(n.args[0]) for n in kw["inputs"].elts] assert fields == expected