"""Synthetic contract tests are distinct from the three unchanged real closed graphs.""" 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_core_reference import ( CORE_PROFILE, LAYOUTS, NUMERIC_UNION, definitions, saved_values, serialize_node, ) from calliope.comfyui.ui2api import convert, convert_reference REPO = Path(__file__).resolve().parents[2] PATH = next((REPO / "example_ComfyUI_workflows").rglob("minimax-h3文生视频无审核版.json")) 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["outputs"]: s["links"] = None for s in n["inputs"]: s["link"] = None return {"nodes": [n], "links": [], "extra": {}} def closed(g): old = copy.deepcopy(g) r = convert_reference(g, CORE_PROFILE) assert r.prompt is None and r.issues and r.reference_only and g == old assert all(i.get("pointer") is not None for i in r.issues) a = audit.profile_attempt(g, CORE_PROFILE) assert a["status"] in ("rejected", "not_applicable") and "candidate_prompt" not in a def synthetic_math(): g = single(133) n = node(132) # Construct the fixed frontend shape as a TEST fixture, never alter source bytes. n["inputs"] = [ {"name": "values.a", "type": NUMERIC_UNION, "link": 1}, {"name": "values.b", "type": NUMERIC_UNION, "link": None}, ] for s in n["outputs"]: s["links"] = None g["nodes"][0]["outputs"][0]["links"] = [1] g["nodes"].append(n) g["links"] = [[1, 133, 0, 132, 0, "FLOAT"]] return g def test_real_main_seven_instances_five_layouts_and_two_explicit_dynamic_rejections(): g = real() assert len(g["nodes"]) == 21 and len(g["links"]) == 24 selected = [n for n in g["nodes"] if n["type"] in LAYOUTS] assert len(selected) == 7 and len(definitions()) == 6 assert sum(len(saved_values(n)) for n in selected) == 11 for n in selected: if n["type"] == "ComfyMathExpression": with pytest.raises(ValueError, match="动态布局"): serialize_node(n) else: assert serialize_node(n) == saved_values(n) closed(g) a = audit.profile_attempt(g, CORE_PROFILE) assert a["status"] == "rejected" evidence = a["supported_node_evidence"] assert len(evidence) == 7 and sum(e["layout_accepted"] for e in evidence) == 5 assert sum(len(e["saved_fields"]) for e in evidence) == 11 assert all(e["not_a_submittable_node_or_partial_prompt"] for e in evidence) assert all(e["reasons"] for e in evidence if not e["layout_accepted"]) assert a["assumptions"]["source_frontend_declaration"] is None assert a["assumptions"]["source_core_provider_version"].startswith("unknown") assert a["assumptions"]["core_version"] == "0.33.0" assert convert(g).prompt is None @pytest.mark.parametrize("nid", [1, 115, 133, 129]) def test_supported_saved_state_instances_under_explicit_missing_identity_assumption(nid): g = single(nid) before = copy.deepcopy(g) r = convert_reference(g, CORE_PROFILE) assert r.prompt is not None and r.reference_only and g == before assert r.prompt[str(nid)]["inputs"] == serialize_node(g["nodes"][0]) assert r.serialization_stage == "restored_saved_state_before_queue_hooks" for n in g["nodes"]: n["properties"].update(cnr_id="comfy-core", ver="0.33.0") g["extra"]["frontendVersion"] = "1.53.10" assert convert_reference(g, CORE_PROFILE).prompt == r.prompt @pytest.mark.parametrize("nid", [1, 115, 133, 129, 124, 132]) @pytest.mark.parametrize( "mutation", ["provider", "version", "aux", "mode", "ue", "hook", "short", "long"] ) def test_identity_mode_layout_unknown_coverage_never_yields_partial_prompt(nid, mutation): g = single(nid) n = g["nodes"][0] if mutation == "provider": n["properties"]["cnr_id"] = "other" elif mutation == "version": n["properties"]["ver"] = "0.30.0" elif mutation == "aux": n["properties"]["aux_id"] = "comfy-core" elif mutation == "mode": n["mode"] = True elif mutation == "ue": n["properties"]["ue_properties"] = {} elif mutation == "hook": n["onConfigure"] = "unknown" elif mutation == "short": n["widgets_values"].pop() else: n["widgets_values"].append("extra") with pytest.raises(ValueError): serialize_node(n) closed(g) @pytest.mark.parametrize( "nid,index,value", [ (1, 0, None), (1, 0, []), (115, 0, "16:9"), (115, 1, True), (115, 1, 0.09), (115, 1, 16.1), (115, 1, "0.7"), (115, 2, 32.0), (115, 2, 7), (115, 2, 129), (133, 0, True), (133, 0, "10"), (133, 0, float("inf")), (133, 0, float("nan")), (133, 0, 10**500), (129, 0, True), (129, 0, 42.0), (129, 0, -1), (129, 0, 2**53), (129, 0, 2**64), (129, 1, "randomize"), (129, 1, "increment"), (129, 1, "decrement"), (129, 1, None), (124, 0, "unknown"), (124, 1, 4.0), (124, 1, True), (124, 1, 0), (124, 1, 10001), (124, 2, -0.01), (124, 2, 1.01), (124, 2, True), (124, 2, "1.0"), ], ) def test_types_enums_boundaries_seed_and_nonfixed_controls(nid, index, value): n = node(nid) n["widgets_values"][index] = value with pytest.raises(ValueError): saved_values(n) @pytest.mark.parametrize( "nid,index,value", [ (1, 0, "opaque\ntext\u0000原样"), (115, 1, 0.1), (115, 1, 16.0), (115, 2, 8), (115, 2, 128), (129, 0, 0), (129, 0, 2**53 - 1), (133, 0, -100.5), (124, 0, "kl_optimal"), (124, 1, 1), (124, 1, 10000), (124, 2, 0.0), (124, 2, 1.0), ], ) def test_proved_values_propagate_without_coercion_or_calculation(nid, index, value): n = node(nid) n["widgets_values"][index] = value field = LAYOUTS[n["type"]][index] v = saved_values(n)[field] assert v == value and type(v) is type(value) @pytest.mark.parametrize("nid", [1, 115, 133, 129, 124, 132]) @pytest.mark.parametrize("mutation", ["missing", "extra", "conflict", "typed-conflict"]) def test_optional_named_save_must_be_complete_and_agree(nid, mutation): n = node(nid) layout = LAYOUTS[n["type"]] n["widgets_values_named"] = dict(zip(layout, n["widgets_values"])) if mutation == "missing": n["widgets_values_named"].pop(layout[0]) elif mutation == "extra": n["widgets_values_named"]["extra"] = 0 elif mutation == "typed-conflict": n["widgets_values_named"][layout[0]] = True else: n["widgets_values_named"][layout[0]] = "conflict" with pytest.raises(ValueError): saved_values(n) def test_synthetic_math_expression_is_opaque_physical_union_edge_and_instance_switch(): g = synthetic_math() original_expression = g["nodes"][1]["widgets_values"][0] r = convert_reference(g, CORE_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["132"]["inputs"] == { "expression": original_expression, "values.a": ["133", 0], } g["nodes"][1]["widgets_values"][0] = "__import__('never_called') / 0 + unknown" g["nodes"][0]["id"] = 990 g["links"][0][1] = 990 r = convert_reference(g, CORE_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["132"]["inputs"]["values.a"] == ["990", 0] assert r.prompt["132"]["inputs"]["expression"] == g["nodes"][1]["widgets_values"][0] def test_synthetic_physical_widget_input_overrides_saved_value(): g = single(133) n = copy.deepcopy(g["nodes"][0]) n["id"] = 991 n["inputs"] = [{"name": "value", "type": "FLOAT", "link": 1, "widget": {"name": "value"}}] n["widgets_values"] = [99.0] g["nodes"][0]["outputs"][0]["links"] = [1] g["nodes"].append(n) g["links"] = [[1, 133, 0, 991, 0, "FLOAT"]] r = convert_reference(g, CORE_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["991"]["inputs"]["value"] == ["133", 0] @pytest.mark.parametrize( "mutation", [ "frontend", "ue", "renderer", "subgraph", "missing-end", "wrong-slot", "wrong-type", "duplicate-link", "reverse-input", "reverse-output", "duplicate-input", "cycle", "wrong-output-name", "wrong-slot-index", "unknown-class", "math-placeholder", ], ) def test_synthetic_graph_structure_is_strict(mutation): g = synthetic_math() a, b = g["nodes"] if mutation == "frontend": g["extra"]["frontendVersion"] = "1.53.6" elif mutation == "ue": g["extra"]["ue_properties"] = {} elif mutation == "renderer": g["extra"]["workflowRendererVersion"] = "Vue" elif mutation == "subgraph": g["definitions"] = {"subgraphs": [{"id": "unknown"}]} elif mutation == "missing-end": g["links"][0][1] = 999 elif mutation == "wrong-slot": g["links"][0][2] = 1 elif mutation == "wrong-type": g["links"][0][5] = "INT" elif mutation == "duplicate-link": g["links"].append(g["links"][0]) elif mutation == "reverse-input": b["inputs"][0]["link"] = 9 elif mutation == "reverse-output": a["outputs"][0]["links"] = [] elif mutation == "duplicate-input": b["inputs"].append(copy.deepcopy(b["inputs"][0])) elif mutation == "cycle": a["inputs"] = [{"name": "value", "type": "FLOAT", "link": 2}] b["outputs"][0]["links"] = [2] g["links"].append([2, 132, 0, 133, 0, "FLOAT"]) elif mutation == "wrong-output-name": b["outputs"][1]["name"] = "FLOAT" elif mutation == "wrong-slot-index": b["outputs"][0]["slot_index"] = True elif mutation == "unknown-class": a["type"] = "TestOnlyHelperNotRegistered" else: b["inputs"].append({"name": "values", "type": "COMFY_AUTOGROW_V3", "link": None}) closed(g) def test_three_unchanged_uncensored_graphs_remain_closed(): paths = sorted(PATH.parent.glob("*.json")) assert len(paths) == 3 for path in paths: g = json.loads(path.read_bytes()) closed(g) a = audit.profile_attempt(g, CORE_PROFILE) assert a["status"] == "rejected" def test_pinned_sources_without_importing_upstream(): r = verify(REPO / "docs/hel-892/h3-core-reference") assert not r["upstream_code_executed"] and len(r["sources_verified"]) >= 9 def test_all_old_26_profiles_attempts_gaps_hashes_and_six_companions_frozen(): old = json.loads((REPO / "docs/hel-892/h3-evidence/library.json").read_bytes()) profiles = tuple(old["profiles"]) assert len(profiles) == 26 and profiles == audit.REFERENCE_PROFILES[:26] before = audit.audit_library(REPO, profiles) now = audit.audit_library(REPO, baseline_profiles=profiles) assert old["gap_groups"] == before["gap_groups"] for a, b, c in zip(old["cases"], before["cases"], now["cases"]): assert a["attempts"] == b["attempts"] == c["attempts"][:26] 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 assert sum(r["status"] == "blocked" for r in now["cases"]) == 136 six = audit.audit(REPO, "case04-batch-reference") assert six == json.loads( (REPO / "docs/hel-892/media-evidence/protected-six-cases.json").read_bytes() ) @pytest.mark.parametrize( "key", [ "serialize_widgets", "fallbackWidgetsValuesNames", "comfyClass", "onNodeCreated", "onConnectInput", "onConnectionsChange", "onInputAdded", ], ) def test_unknown_frontend_construction_overrides_are_not_ignored(key): g = single(133) g["nodes"][0][key] = False closed(g) def test_float_platform_limit_is_symbolic_and_only_conservative_intersection_accepted(): n = node(133) for value in (-(2**31 - 1), 2**31 - 1): n["widgets_values"] = [value] assert saved_values(n)["value"] == value for value in (-(2**31), 2**31): n["widgets_values"] = [value] with pytest.raises(ValueError): saved_values(n) def test_official_schema_ids_registration_enums_and_widget_order_by_static_ast_only(): import ast from calliope.comfyui.h3_core_reference import RATIOS, SCHEDULERS root = REPO / "docs/hel-892/h3-core-reference" expected = { "nodes_primitive.py": { "StringMultiline": ("PrimitiveStringMultiline", ["value"]), "Float": ("PrimitiveFloat", ["value"]), }, "nodes_resolution.py": { "ResolutionSelector": ( "ResolutionSelector", ["aspect_ratio", "megapixels", "multiple"], ), }, "nodes_math.py": { "MathExpressionNode": ("ComfyMathExpression", ["expression", "values"]), }, "nodes_custom_sampler.py": { "RandomNoise": ("RandomNoise", ["noise_seed"]), "BasicScheduler": ("BasicScheduler", ["model", "scheduler", "steps", "denoise"]), }, } for filename, classes in expected.items(): tree = ast.parse((root / ("core__comfy_extras__" + filename)).read_text()) registered = { n.id for fn in ast.walk(tree) if isinstance(fn, ast.AsyncFunctionDef) and fn.name == "get_node_list" for ret in fn.body if isinstance(ret, ast.Return) and isinstance(ret.value, ast.List) for n in ret.value.elts if isinstance(n, ast.Name) } for name, (node_id, fields) in classes.items(): assert name in registered cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == name) fn = next( n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "define_schema" ) schema = next( c for c in ast.walk(fn) if isinstance(c, ast.Call) and isinstance(c.func, ast.Attribute) and c.func.attr == "Schema" ) kw = {k.arg: k.value for k in schema.keywords} assert ast.literal_eval(kw["node_id"]) == node_id actual = [] for c in kw["inputs"].elts: ident = c.args[0] if c.args else next(k.value for k in c.keywords if k.arg == "id") actual.append(ast.literal_eval(ident)) assert actual == fields tree = ast.parse((root / "core__comfy_extras__nodes_resolution.py").read_text()) ratios = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "AspectRatio") assert ( tuple(ast.literal_eval(n.value) for n in ratios.body if isinstance(n, ast.Assign)) == RATIOS ) tree = ast.parse((root / "core__comfy__samplers.py").read_text()) registry = next( n for n in tree.body if isinstance(n, ast.Assign) and any(isinstance(t, ast.Name) and t.id == "SCHEDULER_HANDLERS" for t in n.targets) ) assert tuple(ast.literal_eval(k) for k in registry.value.keys) == SCHEDULERS