HEL-892: pin AudioCrop and GuHai split reference contracts
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -0,0 +1,379 @@
|
||||
"""Read pinned source AST/bytes; synthetic mutations never execute upstream plugins."""
|
||||
|
||||
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 (
|
||||
CROP_PROFILE,
|
||||
CROP_SPLIT_PROFILES,
|
||||
SEPARATION_PROFILE,
|
||||
SPLIT_PROFILE,
|
||||
serialize_reference_widgets,
|
||||
)
|
||||
from calliope.comfyui.ui2api import convert, convert_reference
|
||||
|
||||
REPO = Path(__file__).resolve().parents[2]
|
||||
SOURCES = REPO / "docs/hel-892/crop-split-reference"
|
||||
PREFIX = "example_ComfyUI_workflows/0-基础处理:图像合并-音频分离-视频截取合并-图像视频放大/"
|
||||
CROP = PREFIX + "音频人声分离.json"
|
||||
SPLIT = PREFIX + "图像分割-多宫格分割.json"
|
||||
LAYOUTS = {
|
||||
"AudioCrop": ["start_time", "end_time"],
|
||||
"GuHaiAutoImageSplit": [
|
||||
"保存目录",
|
||||
"水平张数",
|
||||
"垂直张数",
|
||||
"移除画布边缘",
|
||||
"移除描边",
|
||||
"文件名前缀",
|
||||
"保存格式",
|
||||
],
|
||||
"AudioSeparation": ["chunk_fade_shape", "chunk_length", "chunk_overlap"],
|
||||
"LoadAudio": ["audio"],
|
||||
"PreviewAudio": [],
|
||||
"LoadImage": ["image"],
|
||||
"PreviewImage": [],
|
||||
}
|
||||
|
||||
|
||||
def graph(profile):
|
||||
return json.loads((REPO / (CROP if profile == CROP_PROFILE else SPLIT)).read_bytes())
|
||||
|
||||
|
||||
def node(source, nid):
|
||||
return next(n for n in source["nodes"] if n["id"] == nid)
|
||||
|
||||
|
||||
def synthetic_split():
|
||||
# ONLY this synthetic fixture removes the unexplained source PreviewImage
|
||||
# output. The original remains unchanged/rejected in all library audits.
|
||||
source = graph(SPLIT_PROFILE)
|
||||
node(source, 3)["outputs"] = []
|
||||
return source
|
||||
|
||||
|
||||
def schema_and_class(filename, name):
|
||||
tree = ast.parse((SOURCES / filename).read_bytes())
|
||||
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 = ast.literal_eval(next(n.value for n in ast.walk(method) if isinstance(n, ast.Return)))
|
||||
return tree, cls, schema
|
||||
|
||||
|
||||
def test_pinned_ast_schemas_and_registration_no_plugin_import():
|
||||
_, crop, schema = schema_and_class("crop.py", "AudioCrop")
|
||||
assert list(schema["required"]) == ["audio", "start_time", "end_time"]
|
||||
assert schema["required"]["start_time"] == ("STRING", {"default": "0:00"})
|
||||
assert schema["required"]["end_time"] == ("STRING", {"default": "1:00"})
|
||||
tree, split, schema = schema_and_class("guhai__nodes__自动分割图像.py", "GuHaiAutoImageSplit")
|
||||
assert list(schema["required"]) == ["图像", *LAYOUTS["GuHaiAutoImageSplit"]]
|
||||
assert schema["required"]["水平张数"] == ("INT", {"default": 4, "min": 1})
|
||||
assert schema["required"]["垂直张数"] == ("INT", {"default": 2, "min": 1})
|
||||
assert schema["required"]["移除描边"] == ("INT", {"default": 0, "min": 0})
|
||||
assert schema["required"]["保存格式"] == (["PNG", "JPG"], {"default": "PNG"})
|
||||
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 [(k.value, v.id) for k, v in zip(mapping.keys, mapping.values)] == [
|
||||
("GuHaiAutoImageSplit", "GuHaiAutoImageSplit")
|
||||
]
|
||||
for cls in (crop, split):
|
||||
returns = next(
|
||||
n.value
|
||||
for n in cls.body
|
||||
if isinstance(n, ast.Assign)
|
||||
and any(isinstance(t, ast.Name) and t.id == "RETURN_TYPES" for t in n.targets)
|
||||
)
|
||||
assert ast.literal_eval(returns) == (("AUDIO",) if cls is crop else ("IMAGE",))
|
||||
|
||||
|
||||
def independently_check(source, attempt):
|
||||
prompt = attempt["candidate_prompt"]
|
||||
assert set(prompt) == {str(n["id"]) for n in source["nodes"]}
|
||||
for n in source["nodes"]:
|
||||
actual = prompt[str(n["id"])]
|
||||
expected = dict(zip(LAYOUTS[n["type"]], n["widgets_values"]))
|
||||
if n["type"] in ("LoadAudio", "PreviewAudio"):
|
||||
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 == {
|
||||
"class_type": n["type"],
|
||||
"inputs": expected,
|
||||
"_meta": {"title": n.get("title") or n["type"]},
|
||||
}
|
||||
assert {k: type(v) for k, v in expected.items()} == {
|
||||
k: type(v) for k, v in actual["inputs"].items()
|
||||
}
|
||||
for field in attempt["field_evidence"]:
|
||||
pointer = field["source_pointer"]
|
||||
if pointer is None:
|
||||
assert field["field"] == "audioUI" and field["value"] == ""
|
||||
continue
|
||||
value = source
|
||||
for part in pointer.split("/")[1:]:
|
||||
value = value[int(part)] if isinstance(value, list) else value[part]
|
||||
if pointer.startswith("/links/"):
|
||||
value = [str(value[1]), value[2]]
|
||||
assert type(value) is type(field["value"]) and value == field["value"]
|
||||
raw = json.dumps(
|
||||
prompt, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False
|
||||
).encode()
|
||||
assert attempt["candidate_sha256"] == hashlib.sha256(raw).hexdigest()
|
||||
|
||||
|
||||
def test_actual_crop_candidate_every_field_type_pointer_and_final_nonzero_slot():
|
||||
source = graph(CROP_PROFILE)
|
||||
original = copy.deepcopy(source)
|
||||
attempt = audit.profile_attempt(source, CROP_PROFILE)
|
||||
assert attempt["status"] == "complete_reference_candidate"
|
||||
independently_check(source, attempt)
|
||||
assert len(attempt["field_evidence"]) == 11
|
||||
assert attempt["candidate_prompt"]["11"]["inputs"]["audio"] == ["18", 0]
|
||||
assert attempt["candidate_prompt"]["15"]["inputs"]["audio"] == ["11", 3]
|
||||
assert attempt["assumptions"]["source_frontend_declaration"] is None
|
||||
assert source == original and convert(source).prompt is None
|
||||
assert convert_reference(source, SEPARATION_PROFILE).prompt is None
|
||||
|
||||
|
||||
def test_real_split_keeps_unexplained_preview_output_and_whole_graph_closed():
|
||||
source = graph(SPLIT_PROFILE)
|
||||
original = copy.deepcopy(source)
|
||||
assert serialize_reference_widgets(node(source, 2), SPLIT_PROFILE) == dict(
|
||||
zip(LAYOUTS["GuHaiAutoImageSplit"], node(source, 2)["widgets_values"])
|
||||
)
|
||||
attempt = audit.profile_attempt(source, SPLIT_PROFILE)
|
||||
assert attempt["status"] == "rejected" and "candidate_prompt" not in attempt
|
||||
assert any(i["node_id"] == "3" and "输出槽" in i["message"] for i in attempt["issues"])
|
||||
assert convert_reference(source, SPLIT_PROFILE).prompt is None
|
||||
assert source == original and convert(source).prompt is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"start,end",
|
||||
[
|
||||
("12", "60"),
|
||||
("0:01", "0:59"),
|
||||
("-1", "0"),
|
||||
("0:00", "0:00"),
|
||||
("1:90", "999:00"),
|
||||
(" 2 ", "+30"),
|
||||
],
|
||||
)
|
||||
def test_crop_saved_strings_propagate_without_time_parse_clamp_or_round(start, end):
|
||||
source = graph(CROP_PROFILE)
|
||||
node(source, 18)["widgets_values"] = [start, end]
|
||||
prompt = convert_reference(source, CROP_PROFILE).prompt
|
||||
assert prompt["18"]["inputs"]["start_time"] == start
|
||||
assert prompt["18"]["inputs"]["end_time"] == end
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"bad", [True, False, None, 0, 1.25, float("nan"), float("inf"), [], {}, "%date%", "{1|2}"]
|
||||
)
|
||||
@pytest.mark.parametrize("index", [0, 1])
|
||||
def test_crop_rejects_nonstring_or_dynamic_value_whole_graph(index, bad):
|
||||
source = graph(CROP_PROFILE)
|
||||
node(source, 18)["widgets_values"][index] = bad
|
||||
assert convert_reference(source, CROP_PROFILE).prompt is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fmt", ["PNG", "JPG"])
|
||||
@pytest.mark.parametrize("counts,trim,edge", [((1, 2), 0, False), ((2048, 2048), 2048, True)])
|
||||
def test_synthetic_split_full_contract_unicode_and_values_propagate(fmt, counts, trim, edge):
|
||||
source = synthetic_split()
|
||||
node(source, 2)["widgets_values"] = ["目录/子目录", *counts, edge, trim, "新 前缀_", fmt]
|
||||
attempt = audit.profile_attempt(source, SPLIT_PROFILE)
|
||||
assert attempt["status"] == "complete_reference_candidate"
|
||||
independently_check(source, attempt)
|
||||
actual = attempt["candidate_prompt"]["2"]["inputs"]
|
||||
assert actual["图像"] == ["1", 0]
|
||||
assert actual["保存目录"] == "目录/子目录" and actual["保存格式"] == fmt
|
||||
# Serialized path is just data. No directories/files/rendering were executed.
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"index,bad",
|
||||
[
|
||||
(0, None),
|
||||
(0, "%date%"),
|
||||
(1, True),
|
||||
(1, 0),
|
||||
(1, 2049),
|
||||
(1, 2.0),
|
||||
(2, -1),
|
||||
(2, False),
|
||||
(2, float("inf")),
|
||||
(3, 1),
|
||||
(3, None),
|
||||
(4, -1),
|
||||
(4, True),
|
||||
(4, 2049),
|
||||
(5, []),
|
||||
(5, "{x}"),
|
||||
(6, "png"),
|
||||
(6, "JPEG"),
|
||||
],
|
||||
)
|
||||
def test_synthetic_split_bad_types_enums_and_ui_bounds_fail_closed(index, bad):
|
||||
source = synthetic_split()
|
||||
node(source, 2)["widgets_values"][index] = bad
|
||||
assert convert_reference(source, SPLIT_PROFILE).prompt is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("profile", CROP_SPLIT_PROFILES)
|
||||
@pytest.mark.parametrize(
|
||||
"mutation",
|
||||
[
|
||||
"version",
|
||||
"provider",
|
||||
"extra_provider",
|
||||
"missing_provider",
|
||||
"widget_extra",
|
||||
"widget_missing",
|
||||
"widget_binding",
|
||||
"socket_type",
|
||||
"socket_order",
|
||||
"output_index",
|
||||
"output_extra",
|
||||
"output_name",
|
||||
"slot",
|
||||
"bool_slot",
|
||||
"reciprocal",
|
||||
"link_type",
|
||||
"mode",
|
||||
"bool_mode",
|
||||
"serializer",
|
||||
"named",
|
||||
"ue_widget",
|
||||
"ue_link",
|
||||
"frontend",
|
||||
"unknown_node",
|
||||
"dynamic",
|
||||
],
|
||||
)
|
||||
def test_strict_contract_mutations_close_all_nodes(profile, mutation):
|
||||
source = graph(profile) if profile == CROP_PROFILE else synthetic_split()
|
||||
target = node(source, 18 if profile == CROP_PROFILE else 2)
|
||||
props = target["properties"]
|
||||
provider_key = "cnr_id" if profile == CROP_PROFILE else "aux_id"
|
||||
if mutation == "version":
|
||||
props["ver"] = "0.11.0"
|
||||
elif mutation == "provider":
|
||||
props[provider_key] = "different/provider"
|
||||
elif mutation == "extra_provider":
|
||||
props["aux_id" if profile == CROP_PROFILE else "cnr_id"] = "other"
|
||||
elif mutation == "missing_provider":
|
||||
del props[provider_key]
|
||||
elif mutation == "widget_extra":
|
||||
target["widgets_values"].append(0)
|
||||
elif mutation == "widget_missing":
|
||||
target["widgets_values"].pop()
|
||||
elif mutation == "widget_binding":
|
||||
target["inputs"][1]["widget"] = {"name": "wrong"}
|
||||
elif mutation == "socket_type":
|
||||
target["inputs"][1]["type"] = "FLOAT"
|
||||
elif mutation == "socket_order":
|
||||
target["inputs"][1:3] = target["inputs"][1:3][::-1]
|
||||
elif mutation == "output_index":
|
||||
target["outputs"][0]["slot_index"] = 1
|
||||
elif mutation == "output_extra":
|
||||
target["outputs"].append(copy.deepcopy(target["outputs"][0]))
|
||||
elif mutation == "output_name":
|
||||
target["outputs"][0]["name"] = "other"
|
||||
elif mutation == "slot":
|
||||
source["links"][0][2] = 9
|
||||
elif mutation == "bool_slot":
|
||||
source["links"][0][2] = False
|
||||
elif mutation == "reciprocal":
|
||||
target["inputs"][0]["link"] = None
|
||||
elif mutation == "link_type":
|
||||
source["links"][0][5] = "wrong"
|
||||
elif mutation == "mode":
|
||||
target["mode"] = 2
|
||||
elif mutation == "bool_mode":
|
||||
target["mode"] = False
|
||||
elif mutation == "serializer":
|
||||
props["serializeValue"] = "override"
|
||||
elif mutation == "named":
|
||||
target["widgets_values_named"] = {}
|
||||
elif mutation == "ue_widget":
|
||||
props["ue_properties"]["widget_ue_connectable"] = {"x": True}
|
||||
elif mutation == "ue_link":
|
||||
source["extra"]["ue_links"] = [[1]]
|
||||
elif mutation == "frontend":
|
||||
source["extra"]["frontendVersion"] = "1.53.6"
|
||||
elif mutation == "unknown_node":
|
||||
target["type"] = "unknown"
|
||||
elif mutation == "dynamic":
|
||||
target["widgets_values"][0] = "{a|b}"
|
||||
result = convert_reference(source, profile)
|
||||
assert result.prompt is None and result.issues
|
||||
|
||||
|
||||
@pytest.mark.parametrize("profile", CROP_SPLIT_PROFILES)
|
||||
def test_two_disjoint_instances_preserve_nodes_links_and_values(profile):
|
||||
source = graph(profile) if profile == CROP_PROFILE else synthetic_split()
|
||||
second = copy.deepcopy(source)
|
||||
for n in second["nodes"]:
|
||||
n["id"] += 100
|
||||
for s in n["inputs"]:
|
||||
if s.get("link") is not None:
|
||||
s["link"] += 100
|
||||
for s in n["outputs"]:
|
||||
if s.get("links"):
|
||||
s["links"] = [x + 100 for x in s["links"]]
|
||||
for link in second["links"]:
|
||||
for i in (0, 1, 3):
|
||||
link[i] += 100
|
||||
target = node(second, 118 if profile == CROP_PROFILE else 102)
|
||||
target["widgets_values"][0] = "15" if profile == CROP_PROFILE else "不同目录"
|
||||
source["nodes"].extend(second["nodes"])
|
||||
source["links"].extend(second["links"])
|
||||
result = convert_reference(source, profile)
|
||||
assert result.prompt is not None
|
||||
independently_check(source, audit.profile_attempt(source, profile))
|
||||
assert len(result.prompt) == len(source["nodes"])
|
||||
|
||||
|
||||
def test_all_old_fourteen_profiles_attempts_gaps_and_six_differences_unchanged():
|
||||
profiles = tuple(p for p in audit.REFERENCE_PROFILES if p not in CROP_SPLIT_PROFILES)
|
||||
historical = json.loads((REPO / "docs/hel-892/separation-evidence/library.json").read_bytes())
|
||||
baseline = audit.audit_library(REPO, profiles)
|
||||
current = audit.audit_library(REPO)
|
||||
assert baseline["gap_groups"] == historical["gap_groups"]
|
||||
assert len(profiles) == 14 and len(audit.REFERENCE_PROFILES) == 16
|
||||
assert current["coverage_delta"]["baseline_profiles"] == list(profiles)
|
||||
assert current["coverage_delta"]["new_complete_paths"] == [CROP]
|
||||
assert len(current["coverage_delta"]["current_complete_paths"]) == 8
|
||||
for old, before, after in zip(historical["cases"], baseline["cases"], current["cases"]):
|
||||
assert old["attempts"] == before["attempts"] == after["attempts"][:-2]
|
||||
assert old.get("comparisons") == before.get("comparisons")
|
||||
assert old.get("comparisons") == [
|
||||
c for c in after.get("comparisons", []) if c["profile"] in profiles
|
||||
] or (old.get("comparisons") is None and "comparisons" not in after)
|
||||
if after["source_path"] in (CROP, SPLIT):
|
||||
assert after["comparison_status"] == "comparison_unavailable"
|
||||
assert not after["production_prompt_available"] and not after["execution_verified"]
|
||||
|
||||
def unavailable(r):
|
||||
return {
|
||||
x["source_path"]
|
||||
for x in r["cases"]
|
||||
if all(a["status"] == "not_applicable" for a in x["attempts"])
|
||||
}
|
||||
|
||||
assert unavailable(baseline) == unavailable(current) and len(unavailable(current)) == 98
|
||||
Reference in New Issue
Block a user