Files
aigc-home/calliope-backend/tests/test_multivideo_fixed_document.py
T

284 lines
11 KiB
Python

"""One editorial addition: identity refusal, exact positions and media conditions."""
import copy
import hashlib
import json
import subprocess
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/multivideo-document-evidence"
PATHS = [next(p for p in DEFINITIONS if p.endswith(n + ".json")) for n in ["多视频合成分屏展示"]]
SOURCES = [json.loads((REPO / p).read_bytes()) for p in PATHS]
REVIEWS = [review_for(p, hashlib.sha1((REPO / p).read_bytes()).hexdigest()) for p in PATHS]
FIELDS = [(i, f) for i, r in enumerate(REVIEWS) for f in r["fixed_version_document"]["fields"]]
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(i, source=None, review=None):
return build_document(
PATHS[i],
SOURCES[i] if source is None else source,
None,
None,
[],
REVIEWS[i] if review is None else review,
)
@pytest.mark.parametrize("i,field", FIELDS)
def test_exact_positions_and_fixed_text_citations(i, field):
value = pointer(SOURCES[i], field["pointer"])
assert (
canonical(value) == canonical(field["value"]) and type(value).__name__ == field["json_type"]
)
for k in [
"label",
"type",
"schema_default",
"unit",
"limits",
"effect",
"execution_note",
"role",
]:
assert field[k]
for k in field["evidence"]:
e = REVIEWS[i]["fixed_version_document"]["sources"][k]
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())
assert (len(e["commit"]) == 40 and e["commit"] in e["url"]) or e.get(
"git_commit_binding"
) == "unknown"
@pytest.mark.parametrize("i,field", FIELDS)
@pytest.mark.parametrize("variant", ["saved", "type", "text"])
def test_every_position_refuses_even_recomputed_caller_digest(i, field, variant):
s, r = copy.deepcopy(SOURCES[i]), copy.deepcopy(REVIEWS[i])
b = r["fixed_version_document"]
f = next(f for f in b["fields"] if f["pointer"] == field["pointer"])
if variant == "text":
f["schema_default"] = "forged"
else:
parent, _, key = field["pointer"].rpartition("/")
target = pointer(s, parent)
target[int(key) if isinstance(target, list) else key] = (
"forged" if variant == "type" else ("changed-null" if field["value"] is None else None)
)
f["value"] = pointer(s, field["pointer"])
f["json_type"] = type(f["value"]).__name__
b["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
r["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
assert not doc(i, s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("node", [n["id"] for n in SOURCES[0]["nodes"]])
@pytest.mark.parametrize("variant", ["mode", "identity", "ue", "saved_shape"])
def test_every_node_identity_mode_ue_and_saved_shape_refuses(node, variant):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
n = next(n for n in s["nodes"] if n["id"] == node)
if variant == "mode":
n["mode"] = 2
elif variant == "identity":
n["properties"]["ver"] = "forged"
elif variant == "ue":
n["properties"]["ue_properties"]["version"] = "forged"
else:
n["widgets_values"] = [] if "widgets_values" not in n else None
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
r["caller_digest"] = hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest()
assert not doc(0, s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("edge", range(11))
@pytest.mark.parametrize("slot", range(6))
def test_every_physical_endpoint_refuses(edge, slot):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
s["links"][edge][slot] = "forged" if slot == 5 else 999
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert not doc(0, s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize(
"variant",
[
"missing",
"empty",
"null",
"object",
"first_input",
"frontend",
"renderer",
"ue",
"citation",
"summary",
"raw",
"path",
"cache",
"provider",
],
)
def test_binding_unknowns_and_missing_keys_refuse(variant):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
ns = {n["id"]: n for n in s["nodes"]}
b = r["fixed_version_document"]
if variant in ["missing", "empty", "null", "object"]:
if variant == "missing":
del ns[3]["widgets_values"]["videopreview"]
else:
ns[18]["widgets_values"] = {"empty": [], "null": None, "object": {}}[variant]
elif variant == "first_input":
ns[10]["inputs"][0]["link"] = 16
elif variant == "frontend":
s["extra"]["frontendVersion"] = "1.43.17"
elif variant == "renderer":
s["extra"]["workflowRendererVersion"] = "forged"
elif variant == "ue":
ns[5]["properties"]["ue_properties"]["widget_ue_connectable"]["width"] = False
elif variant == "citation":
b["sources"][0]["sha256"] = "0" * 64
elif variant == "summary":
b["user_overview"] = "伪造已可执行"
elif variant == "raw":
r["source_sha"] = "0" * 40
elif variant == "path":
b["source_path"] = "forged"
elif variant == "cache":
ns[12]["widgets_values"]["videopreview"]["params"]["fullpath"] = "forged"
else:
ns[18]["properties"]["cnr_id"] = "rgthree-comfy"
b["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
r["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
assert not doc(0, s, r)["fixed_version"]["matched"]
def test_exact_graph_semantics_and_old_25_objects():
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(subprocess.check_output(["git", "show", "197f3e1:" + resource], cwd=REPO))
now = json.loads((REPO / resource).read_bytes())
b = now[PATHS[0]].pop("fixed_version_document")
assert now == old and len(DEFINITIONS) == 26
oldblocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
assert len(oldblocks) == 25 and sum(len(b["fields"]) for b in oldblocks) == 721
ns = {n["id"]: n for n in SOURCES[0]["nodes"]}
assert len(ns) == 14 and len(SOURCES[0]["links"]) == 11
assert {i for i, n in ns.items() if n["mode"] == 4} == {2, 1, 3, 7, 5, 8, 6, 4}
assert {i for i, n in ns.items() if n["mode"] == 0} == {10, 11, 12, 17, 18, 16}
assert {i for i, n in ns.items() if "widgets_values" not in n} == {18}
assert sum(len(n.get("widgets_values", [])) for n in ns.values()) == 99
assert len({f["pointer"] for f in b["fields"]}) == 99
assert sum(isinstance(f["value"], dict) for f in b["fields"]) == 7
assert sum(f["value"] is None for f in b["fields"]) == 4
assert sum(not isinstance(f["value"], dict) for f in b["fields"]) == 92
assert ns[3]["widgets_values"]["videopreview"] == ns[12]["widgets_values"]["videopreview"]
assert (
len(
[f for f in b["fields"] if f["node_id"] in ["3", "12"] and f["field"] == "videopreview"]
)
== 2
)
assert ns[10]["inputs"][0]["name"] == "image_1" and ns[10]["inputs"][0]["link"] is None
for eid, origin, slot, target, inp, typ in SOURCES[0]["links"]:
assert eid in ns[origin]["outputs"][slot]["links"]
assert ns[origin]["outputs"][slot]["type"] == typ
assert ns[target]["inputs"][inp]["link"] == eid
assert ns[target]["inputs"][inp]["type"] == typ
assert SOURCES[0]["links"][-4:] == [
[18, 11, 0, 10, 1, "IMAGE"],
[21, 10, 0, 12, 0, "IMAGE"],
[24, 16, 0, 11, 1, "IMAGE"],
[25, 17, 0, 11, 0, "IMAGE"],
]
assert all(
next(i for i in ns[n]["inputs"] if i["name"] == "audio")["link"] is None for n in [3, 12]
)
assert all(ns[n]["outputs"][2]["links"] is None for n in [4, 6, 8, 17, 16])
for n in [5, 7]:
assert ns[n]["widgets_values"] == [
512,
896,
"lanczos",
"crop",
"0, 0, 0",
"center",
32,
"cpu",
]
assert ns[n]["properties"]["ue_properties"]["widget_ue_connectable"] == dict(
width=True, height=True
)
assert "frontendVersion" not in SOURCES[0]["extra"]
assert SOURCES[0]["extra"]["workflowRendererVersion"] == "LG"
d = doc(0)
assert d["fixed_version"]["matched"]
assert d["dependencies"]["check_state"] == "unchecked"
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
assert not b["counts"]["final_prompt_constructed"] and b["comments"] == []
for token in [
"10.image_1原link=null",
"上半8",
"不能称三屏完整可执行",
"音频均未接",
"temp",
"历史缓存",
]:
assert token in b["user_overview"]
texts = " ".join(f["effect"] for f in b["fields"])
for token in [
"kwargs",
"向下取整",
"gpu+lanczos",
"crop",
"VHS_MetadataImage",
"-shortest",
"target_rate",
"按钮候选",
]:
assert token in texts
commits = {e["id"]: e["commit"] for e in b["sources"]}
assert commits == dict(
concat="ff7f876c09ff5a53be051073c3c1731b96ee31a3",
resize="50e7dd34d3b6e6bbab1d41e8068e1ddd19bd4d1b",
load="8550981384301e9bc5bfea83e5c2c75258102593",
combine="0a75c7958fe320efcb052f1d9f8451fd20c730a8",
)
@pytest.mark.parametrize("edge", range(11))
@pytest.mark.parametrize("endpoint", ["origin", "target"])
def test_both_node_endpoint_records_refuse(edge, endpoint):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
ns = {n["id"]: n for n in s["nodes"]}
eid, origin, slot, target, inp, _ = s["links"][edge]
if endpoint == "origin":
ns[origin]["outputs"][slot]["links"].remove(eid)
else:
ns[target]["inputs"][inp]["link"] = None
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert not doc(0, s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("node", [3, 12, 4, 6, 8, 17, 16])
@pytest.mark.parametrize("shape", ["missing", "empty", "list", "null"])
def test_seven_history_objects_are_individually_bound(node, shape):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
values = next(n for n in s["nodes"] if n["id"] == node)["widgets_values"]
if shape == "missing":
del values["videopreview"]
else:
values["videopreview"] = {"empty": {}, "list": [], "null": None}[shape]
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert not doc(0, s, r)["fixed_version"]["matched"]