Files
aigc-home/calliope-backend/tests/test_tts_fixed_document.py
T
2026-10-31 03:43:29 +08:00

197 lines
6.9 KiB
Python

"""Mixed-version dialogue notes stay source-bound and cannot enable execution."""
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/tts-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("/Qwen3-TTS_多角色对话.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_all_70_positions_keep_raw_types_separate_ports_and_citations(f):
assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"])
assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"]
assert not f["named_pointer"].startswith("/") and "named_value" not in f
if f["input_pointer"].startswith("/"):
assert pointer(SOURCE, f["input_pointer"])["name"] == f["input_name"]
assert "未知" in f["execution_note"]
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", ["source", "definition"])
def test_caller_rehash_cannot_bypass_saved_values_or_editorial_text(f, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
if variant == "source":
parent, _, key = f["pointer"].rpartition("/")
pointer(s, parent)[int(key)] = "forged"
else:
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged"
refuse(s, r)
@pytest.mark.parametrize("ni", range(32))
@pytest.mark.parametrize("variant", ["mode", "identity", "named-values"])
def test_modes_exact_or_absent_identity_and_named_values_bound(ni, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = s["nodes"][ni]
if variant == "mode":
n["mode"] = 0 if n["mode"] == 4 else 4
elif variant == "identity":
n["properties"]["ver"] = "forged"
else:
n["widgets_values_named"] = {"forged": 0}
refuse(s, r)
@pytest.mark.parametrize("ei", range(26))
def test_every_physical_edge_bound(ei):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
s["links"][ei][3] = 99999
refuse(s, r)
@pytest.mark.parametrize(
"variant",
[
"overview",
"citation",
"source_sha",
"missing",
"empty",
"frontend",
"notes",
"zero",
"counts",
],
)
def test_all_metadata_fail_closed(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = r["fixed_version_document"]
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 == "notes":
b["comments"][0]["text"] = "forged"
elif variant == "zero":
b["zero_saved_nodes"][0]["effect"] = "forged"
else:
b["counts"]["final_prompt_constructed"] = True
assert not doc(s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("generated", [False, True])
def test_dialogue_specific_semantics_and_unknowns_are_actual_doc(generated):
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]() if generated else BLOCK
assert canonical(b) == canonical(BLOCK)
assert b["counts"]["saved_fields"] == 70 and b["counts"]["source_notes"] == 2
ns = {n["id"]: n for n in SOURCE["nodes"]}
assert ns[8]["widgets_values"] == [] and "widgets_values" not in ns[27]
assert all(ns[i]["mode"] == 4 for i in [19, 20, 21, 22, 23, 24, 25, 26, 42])
assert ns[45]["widgets_values"][2].startswith("ole:")
assert not b["counts"]["final_prompt_constructed"] and not b["boundaries"]
def f(node, name):
return next(x for x in b["fields"] if x["node_id"] == str(node) and x["field"] == name)
assert f(18, "interval")["value"] == 0.5 and f(18, "seed")["value"] == 62556003384494
assert f(18, "control_after_generate")["value"] == "fixed"
assert (
f(9, "speakers_config")["value"].startswith("[\n")
and f(9, "manage_speakers")["value"] == "Edit"
)
assert all(f(12, "saved_null_" + str(i))["value"] is None for i in range(3))
assert "audioUI" in f(19, "saved_null_1")["effect"] and not f(19, "saved_null_1")[
"input_pointer"
].startswith("/")
assert "无版本" in f(38, "alias_name")["schema_default"]
flow = "\n".join(b["flow"])
for phrase in [
"speaker_5未接",
"同名后项覆盖",
"静音",
"不重采样",
"temp FLAC",
"purge_cache变量未读",
"load_mtp",
"未知角色",
"input1",
"609f3afa",
]:
assert phrase in flow
actual = doc()["fixed_version"]
assert actual["matched"] and actual["fields"] == b["fields"]
assert actual["comments"] == b["comments"] and actual["user_overview"] == b["user_overview"]
def test_39_existing_blocks_and_all_other_reviews_frozen():
import subprocess
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
base = json.loads(
subprocess.check_output(
["git", "show", "21f0fd678add77a88a975793d1b0b9dbb72bb059:" + resource], cwd=REPO
)
)
now = json.loads((REPO / resource).read_bytes())
blocks = [r["fixed_version_document"] for r in base.values() if "fixed_version_document" in r]
assert len(blocks) == 39 and sum(len(b["fields"]) for b in blocks) == 1381
assert (
sum(
len(
build_document(p, json.loads((REPO / p).read_bytes()), None, None, [], base[p])[
"source_notes"
]
)
for p in DEFINITIONS
if p != PATH
)
== 41
)
for p, r in base.items():
if p != PATH:
assert now[p] == r
now[PATH].pop("fixed_version_document")
assert now == base