"""Pinned source AST, actual saved graph, synthetic mutations; no upstream execution.""" from __future__ import annotations import ast import copy import hashlib import json from pathlib import Path import pytest from scripts import audit_reference_conversion as audit from calliope.comfyui.serialization_reference import MP3_PROFILE, SEPARATION_PROFILE from calliope.comfyui.ui2api import convert, convert_reference REPO = Path(__file__).resolve().parents[2] SOURCE = ( "example_ComfyUI_workflows/0-基础处理:图像合并-音频分离-视频截取合并-图像视频放大/" "视频-音频分离.json" ) SOURCES = REPO / "docs/hel-892/separation-reference" def graph(): return json.loads((REPO / SOURCE).read_bytes()) def node(source, nid): return next(n for n in source["nodes"] if n["id"] == nid) def converted(source): result = convert_reference(source, SEPARATION_PROFILE) assert result.reference_only return result def test_original_plugin_schema_registration_and_artifact_bytes_without_import(): raw = (SOURCES / "plugin-5291831d__src__separation.py").read_bytes() assert raw == (SOURCES / "registry__src__separation.py").read_bytes() tree = ast.parse(raw) cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "AudioSeparation") method = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "INPUT_TYPES") schema = ast.literal_eval(next(n.value for n in ast.walk(method) if isinstance(n, ast.Return))) assert list(schema["required"]) == ["audio"] assert list(schema["optional"]) == ["chunk_fade_shape", "chunk_length", "chunk_overlap"] assert schema["optional"]["chunk_fade_shape"][0] == [ "linear", "half_sine", "logarithmic", "exponential", ] for field in ("chunk_length", "chunk_overlap"): assert schema["optional"][field][0] == "FLOAT" assert not {"min", "max"}.intersection(schema["optional"][field][1]) attrs = { n.targets[0].id: ast.literal_eval(n.value) for n in cls.body if isinstance(n, ast.Assign) and isinstance(n.targets[0], ast.Name) } assert attrs["RETURN_TYPES"] == ("AUDIO",) * 4 assert attrs["RETURN_NAMES"] == ("Bass", "Drums", "Other", "Vocals") output_method = next( n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "sources_to_tuple" ) order = next( n for n in ast.walk(output_method) if isinstance(n, ast.Assign) and any(isinstance(t, ast.Name) and t.id == "output_order" for t in n.targets) ) assert ast.literal_eval(order.value) == ["bass", "drums", "other", "vocals"] tree = ast.parse((SOURCES / "plugin-5291831d____init__.py").read_text()) mapping = next(n.value for n in tree.body if isinstance(n, ast.Assign)) assert ("AudioSeparation", "AudioSeparation") in [ (k.value, v.id) for k, v in zip(mapping.keys, mapping.values) ] @pytest.mark.parametrize( "version,classes", [("0.3.68", ["LoadAudio", "PreviewAudio"]), ("0.3.71", ["SaveAudioMP3"])] ) def test_each_pinned_core_definition_and_registration_has_its_own_identity(version, classes): tree = ast.parse((SOURCES / f"core-{version}__comfy_extras__nodes_audio.py").read_text()) mapping = next( n.value 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) ) names = {k.value: v.id for k, v in zip(mapping.keys, mapping.values)} expected = { "LoadAudio": ["audio"], "PreviewAudio": ["audio"], "SaveAudioMP3": ["audio", "filename_prefix", "quality"], } for name in classes: assert names[name] == name cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == name) method = next( n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "INPUT_TYPES" ) schema = next(n.value for n in ast.walk(method) if isinstance(n, ast.Return)) required = next(v for k, v in zip(schema.keys, schema.values) if k.value == "required") assert [k.value for k in required.keys] == expected[name] if name == "SaveAudioMP3": assert ast.literal_eval(required.values[2])[0] == ["V0", "128k", "320k"] if name == "PreviewAudio": assert cls.bases[0].id == "SaveAudio" def test_real_graph_complete_candidate_independently_checks_every_field_and_pointer(): source = graph() original = copy.deepcopy(source) result = converted(source) assert source == original and convert(source).prompt is None assert ( not result.issues and result.serialization_stage == "restored_saved_state_before_queue_hooks" ) prompt = result.prompt assert prompt is not None and len(prompt) == 7 layouts = { "SaveAudioMP3": ["filename_prefix", "quality"], "LoadAudio": ["audio"], "PreviewAudio": [], "AudioSeparation": ["chunk_fade_shape", "chunk_length", "chunk_overlap"], } for n in source["nodes"]: actual = prompt[str(n["id"])] assert actual["class_type"] == n["type"] and actual["_meta"] == {"title": n["type"]} expected = dict(zip(layouts[n["type"]], n["widgets_values"])) if n["type"] != "AudioSeparation": expected["audioUI"] = "" for socket in n["inputs"]: if socket["link"] is not None: link = next(link for link in source["links"] if link[0] == socket["link"]) expected[socket["name"]] = [str(link[1]), link[2]] assert actual["inputs"] == expected assert {k: type(v) for k, v in actual["inputs"].items()} == { k: type(v) for k, v in expected.items() } assert prompt["17"]["inputs"]["audio"] == ["10", 0] # original audio, NOT a separated stem assert {nid: prompt[nid]["inputs"]["audio"] for nid in ("18", "19", "20", "15")} == { "18": ["11", 0], "19": ["11", 1], "20": ["11", 2], "15": ["11", 3], } assert type(prompt["11"]["inputs"]["chunk_length"]) is int assert prompt["11"]["inputs"]["chunk_overlap"] == 0.2 attempt = audit.profile_attempt(source, SEPARATION_PROFILE) assert attempt["candidate_prompt"] == prompt assert attempt["candidate_sha256"] == audit.canonical_sha(prompt) assert attempt["assumptions"]["source_frontend_declaration"] is None assert attempt["assumptions"]["missing_node_declarations"] == [] assert attempt["assumptions"]["target_version_match"] == "unknown" fields = attempt["field_evidence"] assert len(fields) == 18 for field in fields: if field["source_pointer"] is None: assert field["field"] == "audioUI" and field["value"] == "" continue value = source for part in field["source_pointer"].split("/")[1:]: value = value[int(part)] if isinstance(value, list) else value[part] expected = ( [str(value[1]), value[2]] if field["source_pointer"].startswith("/links/") else value ) assert type(expected) is type(field["value"]) and expected == field["value"] @pytest.mark.parametrize("shape", ["linear", "half_sine", "logarithmic", "exponential"]) @pytest.mark.parametrize( "length,overlap", [(20, 0.2), (20.0, 1), (0, 0.0), (2048, 2048.0), (1.25, 0.123456)] ) def test_changed_float_values_and_enums_preserve_exact_saved_numeric_form(shape, length, overlap): source = graph() node(source, 11)["widgets_values"] = [shape, length, overlap] actual = converted(source).prompt["11"]["inputs"] assert actual["chunk_fade_shape"] == shape assert type(actual["chunk_length"]) is type(length) and actual["chunk_length"] == length assert type(actual["chunk_overlap"]) is type(overlap) and actual["chunk_overlap"] == overlap @pytest.mark.parametrize("quality", ["V0", "128k", "320k"]) def test_prefix_and_quality_and_audio_name_propagate(quality): source = graph() node(source, 17)["widgets_values"] = ["新目录/前缀", quality] node(source, 10)["widgets_values"][0] = "第二段 音频.mp4" prompt = converted(source).prompt assert prompt["17"]["inputs"]["quality"] == quality assert prompt["17"]["inputs"]["filename_prefix"] == "新目录/前缀" assert prompt["10"]["inputs"]["audio"] == "第二段 音频.mp4" @pytest.mark.parametrize("index", [1, 2]) @pytest.mark.parametrize( "bad", [True, False, None, "20", [], -0.1, 2048.01, float("nan"), float("inf"), 10**1000] ) def test_invalid_numeric_type_or_frontend_bound_closes_whole_graph(index, bad): source = graph() node(source, 11)["widgets_values"][index] = bad result = converted(source) assert result.prompt is None and result.issues @pytest.mark.parametrize( "mutation", [ "enum", "null_enum", "extra_widget", "missing_widget", "preview_widget", "load_dom", "quality", "version", "plugin_version", "provider", "missing_version", "swap_core", "frontend", "serializer", "named_override", "ue_override", "ue_link", "bad_widget", "socket_type", "missing_socket", "socket_order", "extra_output", "output_index", "bad_slot", "bool_slot", "link_type", "dangling", "unused_bad_link", "muted", "bool_mode", "unknown_node", "dynamic_prefix", "preview_audio_ui_link", ], ) def test_unsupported_layout_version_override_and_structure_fail_closed(mutation): source = graph() separation = node(source, 11) if mutation == "enum": separation["widgets_values"][0] = "cosine" elif mutation == "null_enum": separation["widgets_values"][0] = None elif mutation == "extra_widget": separation["widgets_values"].append(1) elif mutation == "missing_widget": separation["widgets_values"].pop() elif mutation == "preview_widget": node(source, 15)["widgets_values"] = [None] elif mutation == "load_dom": node(source, 10)["widgets_values"][1] = "audio/server.wav" elif mutation == "quality": node(source, 17)["widgets_values"][1] = "invalid" elif mutation == "version": node(source, 10)["properties"]["ver"] = "0.11.0" elif mutation == "plugin_version": separation["properties"]["ver"] = "1.5.0" elif mutation == "provider": separation["properties"]["cnr_id"] = "other" elif mutation == "missing_version": separation["properties"].pop("ver") elif mutation == "swap_core": node(source, 17)["properties"]["ver"] = "0.3.68" elif mutation == "frontend": source["extra"]["frontendVersion"] = "1.53.6" elif mutation == "serializer": separation["serializeValue"] = "custom" elif mutation == "named_override": separation["widgets_values_named"] = {"chunk_length": 1} elif mutation == "ue_override": separation["properties"]["ue_properties"]["widget_ue_connectable"] = {"chunk_length": True} elif mutation == "ue_link": source["extra"]["ue_links"] = [[1, 2]] elif mutation == "bad_widget": separation["inputs"][1]["widget"] = {"name": "wrong"} elif mutation == "socket_type": separation["inputs"][2]["type"] = "INT" elif mutation == "missing_socket": separation["inputs"].pop() elif mutation == "socket_order": separation["inputs"][1:3] = reversed(separation["inputs"][1:3]) elif mutation == "extra_output": separation["outputs"].append({"name": "other", "type": "AUDIO", "links": []}) elif mutation == "output_index": separation["outputs"][3]["slot_index"] = 0 elif mutation == "bad_slot": source["links"][1][2] = 4 elif mutation == "bool_slot": source["links"][1][2] = True elif mutation == "link_type": source["links"][1][5] = "IMAGE" elif mutation == "dangling": source["links"][1][1] = 99 elif mutation == "unused_bad_link": source["links"].append([999, 11, 0, 15, 0, "AUDIO"]) elif mutation == "muted": node(source, 15)["mode"] = 2 elif mutation == "bool_mode": node(source, 15)["mode"] = False elif mutation == "unknown_node": node(source, 15)["type"] = "UnknownPreview" elif mutation == "dynamic_prefix": node(source, 17)["widgets_values"][0] = "%date%/audio" elif mutation == "preview_audio_ui_link": node(source, 15)["inputs"][1]["link"] = 15 result = converted(source) assert result.prompt is None and result.issues def test_two_independent_graph_instances_keep_all_four_nonzero_slots_and_scalars(): source = graph() other = graph() for n in other["nodes"]: n["id"] += 100 for socket in n["inputs"]: if socket.get("link") is not None: socket["link"] += 1000 for socket in n["outputs"]: socket["links"] = [lid + 1000 for lid in socket.get("links", [])] for link in other["links"]: link[0] += 1000 link[1] += 100 link[3] += 100 node(other, 111)["widgets_values"] = ["exponential", 7.5, 0.375] node(other, 117)["widgets_values"] = ["第二路/音频", "128k"] node(other, 110)["widgets_values"][0] = "second.wav" source["nodes"].extend(other["nodes"]) source["links"].extend(other["links"]) prompt = converted(source).prompt assert len(prompt) == 14 for out, slot in [(18, 0), (19, 1), (20, 2), (15, 3)]: assert prompt[str(out)]["inputs"]["audio"] == ["11", slot] assert prompt[str(out + 100)]["inputs"]["audio"] == ["111", slot] assert prompt["11"]["inputs"]["chunk_length"] == 20 assert prompt["111"]["inputs"]["chunk_length"] == 7.5 assert prompt["117"]["inputs"]["audio"] == ["110", 0] assert prompt["17"]["inputs"]["audio"] == ["10", 0] assert prompt["117"]["inputs"]["filename_prefix"] == "第二路/音频" def test_old_mp3_profile_rejects_real_graph_and_new_profile_rejects_old_core(): source = graph() assert convert_reference(source, MP3_PROFILE).prompt is None for n in source["nodes"]: if n["properties"]["cnr_id"] == "comfy-core": n["properties"]["ver"] = "0.11.0" assert converted(source).prompt is None def test_library_actual_delta_keeps_every_old_attempt_and_all_six_golden_comparisons(): previous = json.loads((REPO / "docs/hel-892/media-evidence/library.json").read_bytes()) profiles = tuple( p for p in audit.REFERENCE_PROFILES[:26] if p not in ( *audit.CROP_SPLIT_PROFILES, *audit.VHS_PROFILES, *audit.SETGET_PROFILES, *audit.VIDEOINFO_PROFILES, *audit.SCAIL_LOAD_PROFILES, audit.GRID_PROFILE, audit.H3_PROFILE, ) ) baseline = tuple(p for p in profiles if p != SEPARATION_PROFILE) old = audit.audit_library(REPO, baseline) current = audit.audit_library(REPO, profiles, baseline_profiles=baseline) for historical, before, after in zip(previous["cases"], old["cases"], current["cases"]): assert historical["source_path"] == before["source_path"] == after["source_path"] assert historical["attempts"] == before["attempts"] == after["attempts"][:-1] assert historical.get("comparisons") == before.get("comparisons") old_comparisons = [c for c in after.get("comparisons", []) if c["profile"] in baseline] assert old_comparisons == before.get("comparisons", []) assert current["coverage_delta"]["baseline_profiles"] == list(baseline) assert current["coverage_delta"]["new_complete_paths"] == [SOURCE] assert current["ui_total"] == 145 row = next(r for r in current["cases"] if r["source_path"] == SOURCE) assert row["source_sha256"] == hashlib.sha256((REPO / SOURCE).read_bytes()).hexdigest() assert row["comparison_status"] == "comparison_unavailable" and "comparisons" not in row assert not row["production_prompt_available"] and not row["execution_verified"] assert all( "candidate_prompt" not in a for r in current["cases"] for a in r["attempts"] if a["status"] != "complete_reference_candidate" ) # Other frontends remain out of scope: all-not-applicable set is identical. def unavailable(report): return { r["source_path"] for r in report["cases"] if all(a["status"] == "not_applicable" for a in r["attempts"]) } assert unavailable(previous) == unavailable(current) async def test_synthetic_preview_and_mp3_history_only_accept_file_descriptors(): from calliope.comfyui.client import ComfyUIClient client = ComfyUIClient("http://unused.invalid") # Synthetic protocol fixture, not real encoding, history, or separation. fixture = { "outputs": { nid: { "audio": [ {"filename": f"synthetic_{nid}.{ext}", "subfolder": "audio", "type": kind} ] } for nid, ext, kind in [("15", "flac", "temp"), ("17", "mp3", "output")] } } assert len(client.extract_outputs(fixture, {"15": "audio", "17": "audio"})) == 2 for bad in ["/server/audio.mp3", "https://example.invalid/a.mp3", "", {"audioUI": ""}]: assert client.extract_outputs({"outputs": {"15": {"audio": [bad]}}}, {"15": "audio"}) == [] await client.close()