Files
aigc-home/calliope-backend/tests/test_minimax_music_fixed_document.py
T
2026-10-31 05:02:30 +08:00

223 lines
8.1 KiB
Python

"""MiniMax source evidence stays separate from conversion/execution eligibility."""
import copy
import hashlib
import json
import runpy
from pathlib import Path
import pytest
from calliope.comfyui.fixed_document import DEFINITIONS, canonical
from calliope.comfyui.library import build_document
from calliope.comfyui.source_reviews import review_for
REPO = Path(__file__).resolve().parents[2]
OUT = REPO / "docs/hel-892/minimax-music-document-evidence"
PATH = "example_ComfyUI_workflows/2-音乐生成/minimax_music_文生音乐.json"
RAW = (REPO / PATH).read_bytes()
SOURCE = json.loads(RAW)
REVIEW = review_for(PATH, hashlib.sha1(RAW).hexdigest())
BLOCK = REVIEW["fixed_version_document"]
def pointer(obj, path):
for part in path.strip("/").split("/"):
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
return obj
def doc(s=SOURCE, r=REVIEW):
return build_document(PATH, s, None, None, [], r)
def refuse(s, r):
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert not doc(s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
def test_raw_position_named_copy_type_scope_and_citations(f):
assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"])
assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"]
assert (
canonical(pointer(SOURCE, f["named_pointer"]))
== canonical(f["named_value"])
== canonical(f["value"])
)
if f["input_pointer"].startswith("/"):
assert pointer(SOURCE, f["input_pointer"])["name"] == f["input_name"]
assert ("subgraph:" in f["scope"]) == (f["pointer"].startswith("/definitions/"))
for i in f["evidence"]:
e = BLOCK["sources"][i]
raw = (OUT / e["local_file"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
@pytest.mark.parametrize("variant", ["position", "named", "editorial"])
def test_caller_rehash_cannot_expand_source_or_definition_trust(f, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
if variant == "editorial":
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged"
else:
ptr = f["pointer"] if variant == "position" else f["named_pointer"]
parent, _, key = ptr.rpartition("/")
obj = pointer(s, parent)
obj[int(key) if isinstance(obj, list) else key] = "forged"
refuse(s, r)
NODES = [f"/nodes/{i}" for i in range(4)] + [
f"/definitions/subgraphs/0/nodes/{i}" for i in range(11)
]
@pytest.mark.parametrize("path", NODES)
@pytest.mark.parametrize("variant", ["mode", "identity", "named"])
def test_every_root_and_internal_node_bound(path, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = pointer(s, path)
if variant == "mode":
n["mode"] = 4
elif variant == "identity":
n["properties"]["ver"] = "forged"
else:
n["widgets_values_named"] = {"forged": 0}
refuse(s, r)
EDGES = ["/links/0"] + [f"/definitions/subgraphs/0/links/{i}" for i in range(24)]
@pytest.mark.parametrize("path", EDGES)
def test_all_physical_and_boundary_edges_bound(path):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
e = pointer(s, path)
if isinstance(e, list):
e[3] = 99999
else:
e["target_id"] = 99999
refuse(s, r)
@pytest.mark.parametrize(
"variant",
[
"overview",
"citation",
"source_sha",
"missing",
"empty",
"frontend",
"note39",
"note40",
"counts",
"widgets",
"preview",
"boundary-input",
"boundary-output",
"ue-switch",
],
)
def test_metadata_and_subgraph_restore_claims_fail_closed(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = r["fixed_version_document"]
sg = s["definitions"]["subgraphs"][0]
if variant == "overview":
b["user_overview"] = "forged"
elif variant == "citation":
b["sources"][0]["sha256"] = "forged"
elif variant == "source_sha":
r["source_sha"] = "forged"
elif variant == "missing":
r.pop("fixed_version_document")
elif variant == "empty":
r["fixed_version_document"] = {}
elif variant == "frontend":
s["extra"]["frontendVersion"] = "1.52.7"
elif variant.startswith("note"):
b["comments"][int(variant[-2:]) - 39]["text"] = "forged"
elif variant == "counts":
b["counts"]["final_prompt_constructed"] = True
elif variant == "widgets":
sg["widgets"] = [{"forged": True}]
elif variant == "preview":
s["nodes"][3]["properties"]["previewExposures"] = [0]
elif variant == "boundary-input":
sg["inputs"][0]["name"] = "forged"
elif variant == "boundary-output":
sg["outputs"][0]["name"] = "forged"
else:
s["nodes"][3]["properties"]["ue_properties"]["widget_ue_connectable"]["switch"] = False
assert not doc(s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("node", [10, 12])
@pytest.mark.parametrize("variant", [[], None, [0]])
def test_missing_widget_key_distinct_from_empty_null_or_added(node, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = next(n for n in s["definitions"]["subgraphs"][0]["nodes"] if n["id"] == node)
assert "widgets_values" not in n
n["widgets_values"] = variant
refuse(s, r)
def test_actual_document_keeps_gaps_overrides_and_status():
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
assert canonical(b) == canonical(BLOCK)
assert len(DEFINITIONS) == 45 and len(b["fields"]) == 38 and len(b["comments"]) == 2
f = {(x["node_id"], x["field"]): x for x in b["fields"]}
assert (
f["37", "max_duration"]["json_type"] == "int" and f["37:9", "denoise"]["json_type"] == "int"
)
assert f["37:43", "switch"]["value"] is False and f["37", "switch"]["value"] is True
assert f["37:38", "fixed"]["named_value"] == "randomize"
assert sum(x["role"] == "linked_saved_cache" for x in b["fields"]) == 11
assert sum(x["role"] == "frontend_reference_only" for x in b["fields"]) == 3
assert f["37:13", "seed"]["value"] == 222 and f["37:9", "seed"]["value"] == 1111111112
assert f["37", "seed"]["value"] == 617970236276137
assert f["37:15", "seconds"]["value"] == 120
assert len(f["37", "caption"]["value"]) > 1500 and "[Bridge]" in f["37", "lyrics"]["value"]
for node in [39, 40]:
source = next(n for n in SOURCE["nodes"] if n["id"] == node)
assert (
next(n["text"] for n in b["comments"] if n["node_id"] == str(node))
== source["widgets_values"][0]
)
text = "\n".join(b["flow"])
for term in [
"两个音乐类",
"13节点实际有widgets_values_named",
"qscale=1",
"8输入/1输出",
"1.48.7",
"未实跑",
"完整定稿0/实跑0/新增转换0",
]:
assert term in text or term in b["notice"]
d = doc()
assert d["fixed_version"]["matched"]
assert not d["status"]["doc_done"] and not d["status"]["exec_verified"]
assert hashlib.sha256(canonical(BLOCK)).hexdigest() == DEFINITIONS[PATH]
def test_exact_branch_source_static_only():
import ast
path = OUT.parent / "minimax-music-reference/core__comfy_extras__nodes_logic.py"
tree = ast.parse(path.read_text())
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "SwitchNode")
execute = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "execute")
branch = execute.body[0].value.args[0]
assert isinstance(branch, ast.IfExp)
assert (
branch.test.id == "switch"
and branch.body.id == "on_true"
and branch.orelse.id == "on_false"
)
missing = json.loads(
(OUT.parent / "minimax-music-reference/missing-music-classes.json").read_bytes()
)
assert missing["returncode"] == 1 and missing["stdout"] == ""