Files
aigc-home/calliope-backend/tests/test_media_reference.py
T
2026-10-30 03:44:25 +08:00

368 lines
14 KiB
Python

"""Real saved bytes and synthetic mutations; no codec, fonts, or target execution."""
from __future__ import annotations
import ast
import copy
import json
from pathlib import Path
import pytest
from scripts import audit_reference_conversion as audit
from calliope.comfyui.serialization_reference import MEDIA_PROFILES, MP3_PROFILE, REEL_PROFILE
from calliope.comfyui.ui2api import convert, convert_reference
REPO = Path(__file__).resolve().parents[2]
BASE = "example_ComfyUI_workflows/0-基础处理:图像合并-音频分离-视频截取合并-图像视频放大/"
FILES = {MP3_PROFILE: "音频转MP3格式.json", REEL_PROFILE: "图像嵌入文字描述.json"}
def graph(profile):
return json.loads((REPO / BASE / FILES[profile]).read_bytes())
@pytest.mark.parametrize("profile", MEDIA_PROFILES)
def test_real_candidate_all_fields_links_and_types_have_original_byte_pointers(profile):
source = graph(profile)
original = copy.deepcopy(source)
attempt = audit.profile_attempt(source, profile)
assert attempt["status"] == "complete_reference_candidate"
assert attempt["assumptions"]["source_frontend_declaration"] is None
assert (
attempt["assumptions"]["missing_frontend_policy"] == "explicit_media_reference_assumption"
)
assert attempt["assumptions"]["target_version_match"] == "unknown"
result = convert_reference(source, profile)
assert result.reference_only and result.prompt and not result.issues
assert result.serialization_stage == "restored_saved_state_before_queue_hooks"
assert source == original and convert(source).prompt is None
prompt = result.prompt
assert set(prompt) == {str(n["id"]) for n in source["nodes"]}
for n in source["nodes"]:
actual = prompt[str(n["id"])]
assert actual["class_type"] == n["type"]
assert actual["_meta"] == {"title": n.get("title") or n["type"]}
# AST only, proving the fixed upstream constructor order independently.
if profile == REEL_PROFILE:
for version, clsname, expected in [
(
"4f4d5cbb",
"ImageReel",
[
"image1",
"image1_text",
"image2_text",
"image3_text",
"image4_text",
"reel_height",
"border",
],
),
(
"42ccdd8f",
"ImageReelComposit",
["reel_1", "font_file", "font_size", "border", "color_theme"],
),
]:
path = (
REPO
/ f"docs/hel-892/media-reference/ComfyUI_LayerStyle__{version}__py__image_reel.py"
)
tree = ast.parse(path.read_text())
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == clsname)
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
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
)
)
assert ("LayerUtility: " + clsname, clsname) in [
(k.value, v.id) for k, v in zip(mapping.keys, mapping.values)
]
else:
assert prompt["4"]["inputs"] == {
"filename_prefix": "audio/ComfyUI",
"quality": "V0",
"audioUI": "",
"audio": ["1", 0],
}
assert prompt["1"]["inputs"] == {
"audio": source["nodes"][1]["widgets_values"][0],
"audioUI": "",
}
fields = attempt["field_evidence"]
assert len(fields) == sum(len(n["inputs"]) for n in prompt.values())
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("quality", ["V0", "128k", "320k"])
def test_mp3_changed_file_prefix_and_quality_propagate_without_encoding(quality):
source = graph(MP3_PROFILE)
source["nodes"][0]["widgets_values"] = ["新目录/测试", quality]
source["nodes"][1]["widgets_values"][0] = "different input.wav"
prompt = convert_reference(source, MP3_PROFILE).prompt
assert prompt["4"]["inputs"]["filename_prefix"] == "新目录/测试"
assert prompt["4"]["inputs"]["quality"] == quality
assert prompt["1"]["inputs"]["audio"] == "different input.wav"
assert prompt["4"]["inputs"]["audio"] == ["1", 0]
def test_reel_text_sizes_dynamic_font_and_two_images_remain_independent():
source = graph(REEL_PROFILE)
source["nodes"][0]["widgets_values"][0] = "one.png"
source["nodes"][1]["widgets_values"][0] = "two.png"
source["nodes"][2]["widgets_values"] = ["", "第二行\n第三行", "three", "four", 1024, 64]
source["nodes"][3]["widgets_values"] = ["unverified-font.ttf", 72, 16, "dark"]
prompt = convert_reference(source, REEL_PROFILE).prompt
assert prompt["3"]["inputs"] == {"image": "one.png"}
assert prompt["4"]["inputs"] == {"image": "two.png"}
assert prompt["1"]["inputs"] == {
"image1_text": "",
"image2_text": "第二行\n第三行",
"image3_text": "three",
"image4_text": "four",
"reel_height": 1024,
"border": 64,
"image1": ["3", 0],
"image2": ["4", 0],
}
assert prompt["2"]["inputs"] == {
"font_file": "unverified-font.ttf",
"font_size": 72,
"border": 16,
"color_theme": "dark",
"reel_1": ["1", 0],
}
assert prompt["5"]["inputs"] == {"images": ["2", 0]}
@pytest.mark.parametrize("profile", MEDIA_PROFILES)
@pytest.mark.parametrize(
"mutation",
[
"extra",
"missing",
"null",
"boolean",
"enum",
"provider",
"version",
"frontend",
"custom",
"binding",
"slot_type",
"missing_socket",
"duplicate_socket",
"output",
"mode",
"link",
"link_type",
"widget_link",
"dynamic",
],
)
def test_bad_assumptions_close_whole_graph(profile, mutation):
source = graph(profile)
node = source["nodes"][0] if profile == MP3_PROFILE else source["nodes"][2]
wi = 0 if profile == MP3_PROFILE else 4
if mutation == "extra":
node["widgets_values"].append("custom")
elif mutation == "missing":
node["widgets_values"].pop()
elif mutation == "null":
node["widgets_values"][wi] = None
elif mutation == "boolean":
node["widgets_values"][wi] = True
elif mutation == "enum":
if profile == MP3_PROFILE:
node["widgets_values"][1] = "V3"
else:
source["nodes"][3]["widgets_values"][3] = "unknown"
elif mutation == "provider":
node["properties"]["cnr_id"] = "different"
elif mutation == "version":
node["properties"]["ver"] = "0.38.0"
elif mutation == "frontend":
source["extra"]["frontendVersion"] = "1.53.6"
elif mutation == "custom":
node["serializeValue"] = "custom"
elif mutation == "binding":
next(s for s in node["inputs"] if s.get("widget"))["widget"] = {"name": "wrong"}
elif mutation == "slot_type":
node["inputs"][0]["type"] = "STRING"
elif mutation == "missing_socket":
node["inputs"].pop()
elif mutation == "duplicate_socket":
node["inputs"].append(copy.deepcopy(node["inputs"][-1]))
elif mutation == "output":
source["nodes"][1]["outputs"][0]["type"] = "STRING"
elif mutation == "mode":
node["mode"] = 2
elif mutation == "link":
source["links"][0][2] = 999
elif mutation == "link_type":
source["links"][0][5] = "STRING"
elif mutation == "widget_link":
next(s for s in node["inputs"] if s.get("widget"))["link"] = source["links"][0][0]
elif mutation == "dynamic":
node["widgets_values"][0] = "{one|two}"
result = convert_reference(source, profile)
assert result.prompt is None and result.issues and result.reference_only
@pytest.mark.parametrize("value", [False, 1, "", "recorded.wav"])
def test_audio_dom_hole_is_not_a_serialized_file(value):
source = graph(MP3_PROFILE)
source["nodes"][1]["widgets_values"][1] = value
assert convert_reference(source, MP3_PROFILE).prompt is None
@pytest.mark.parametrize("value", [False, 3.0, 3, 1025])
def test_font_size_boolean_float_and_outside_range_are_rejected(value):
source = graph(REEL_PROFILE)
source["nodes"][3]["widgets_values"][1] = value
assert convert_reference(source, REEL_PROFILE).prompt is None
def test_mixed_plugin_commits_cannot_be_interchanged():
source = graph(REEL_PROFILE)
source["nodes"][3]["properties"]["ver"] = source["nodes"][2]["properties"]["ver"]
assert convert_reference(source, REEL_PROFILE).prompt is None
def test_library_actual_delta_keeps_old_candidates_and_no_golden_claim():
previous = audit.audit_library(
REPO,
tuple(
p
for p in audit.REFERENCE_PROFILES
if p not in (*MEDIA_PROFILES, audit.SEPARATION_PROFILE, *audit.CROP_SPLIT_PROFILES)
),
)
current = audit.audit_library(
REPO,
tuple(
p
for p in audit.REFERENCE_PROFILES
if p not in (audit.SEPARATION_PROFILE, *audit.CROP_SPLIT_PROFILES)
),
baseline_profiles=tuple(
p
for p in audit.REFERENCE_PROFILES
if p not in (*MEDIA_PROFILES, audit.SEPARATION_PROFILE, *audit.CROP_SPLIT_PROFILES)
),
)
assert (
current["coverage_delta"]["previous_complete_paths"]
== previous["coverage_delta"]["current_complete_paths"]
)
assert set(current["coverage_delta"]["new_complete_paths"]) == {
BASE + n for n in FILES.values()
}
assert current["ui_total"] == 145 and current["target_total"] == 155
for row in current["cases"]:
if row["source_path"] in current["coverage_delta"]["new_complete_paths"]:
assert row["comparison_status"] == "comparison_unavailable"
assert (
row["execution_verified"] is False and row["production_prompt_available"] is False
)
assert len(row["candidate_hashes"]) == 1
for attempt in row["attempts"]:
if attempt["status"] != "complete_reference_candidate":
assert "candidate_prompt" not in attempt
async def test_mp3_original_schema_and_synthetic_descriptor_boundary():
tree = ast.parse(
(
REPO
/ "docs/hel-892/structure-reference/ComfyUI__09725967__comfy_extras__nodes_audio.py"
).read_text()
)
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "SaveAudioMP3")
schema = next(
n
for n in ast.walk(cls)
if isinstance(n, ast.Call) and isinstance(n.func, ast.Attribute) and n.func.attr == "Schema"
)
inputs = next(k.value for k in schema.keywords if k.arg == "inputs")
assert [call.args[0].value for call in inputs.elts] == ["audio", "filename_prefix", "quality"]
assert ast.literal_eval(
next(k.value for k in inputs.elts[2].keywords if k.arg == "options")
) == ["V0", "128k", "320k"]
assert next(k.value.value for k in schema.keywords if k.arg == "is_output_node") is True
# Pure synthetic history shape. No serializer imports, codec, waveform or network.
from calliope.comfyui.client import ComfyUIClient
from calliope.comfyui.output_map import auto_output_map
from calliope.comfyui.registry import OUTPUT_CLASSES
# MP3 was already registered before this change; no new registry entry.
assert "SaveAudioMP3" in OUTPUT_CLASSES
prompt = convert_reference(graph(MP3_PROFILE), MP3_PROFILE).prompt
assert [e["kind"] for e in auto_output_map(prompt)] == ["audio"]
client = ComfyUIClient()
await client.close() # extraction is pure, with the transport already closed
fixture = {
"outputs": {
"4": {
"audio": [
{"filename": "synthetic_00001.mp3", "subfolder": "audio", "type": "output"}
]
}
}
}
assert client.extract_outputs(fixture, {"4": "audio"}) == [
{
"filename": "synthetic_00001.mp3",
"subfolder": "audio",
"type": "output",
"node_id": "4",
"media_kind": "audio",
}
]
for path in ["audio/synthetic_00001.mp3", "https://example.invalid/audio.mp3", ""]:
assert client.extract_outputs({"outputs": {"4": {"audio": [path]}}}, {"4": "audio"}) == []
def test_synthetic_two_reel_instances_do_not_share_text_or_size_values():
source = graph(REEL_PROFILE)
# Duplicate a complete branch, with independent IDs/links but shared source image.
reel, composit = copy.deepcopy(source["nodes"][2:4])
reel["id"], composit["id"] = 11, 12
reel["inputs"][0]["link"] = 12
reel["inputs"][1]["link"] = None
reel["outputs"][0]["links"] = [11]
reel["widgets_values"] = ["other", "", "three", "four", 768, 16]
composit["inputs"][0]["link"] = 11
composit["outputs"][0]["links"] = []
composit["widgets_values"] = ["another-font.otf", 60, 8, "dark"]
source["nodes"][0]["outputs"][0]["links"].append(12)
source["nodes"].extend([reel, composit])
source["links"].extend([[12, 3, 0, 11, 0, "IMAGE"], [11, 11, 0, 12, 0, "Reel"]])
prompt = convert_reference(source, REEL_PROFILE).prompt
assert prompt["1"]["inputs"]["image1_text"] == "image1"
assert prompt["11"]["inputs"]["image1_text"] == "other"
assert prompt["11"]["inputs"]["reel_height"] == 768
assert prompt["12"]["inputs"]["font_size"] == 60
assert prompt["2"]["inputs"]["font_size"] == 40
assert prompt["12"]["inputs"]["reel_1"] == ["11", 0]