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

239 lines
8.4 KiB
Python

"RTX saved values, physical links, unknown identities and fail-closed binding."
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/rtx-video-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("/高清放大/RTX_video_upscale.json"))
SOURCE = json.loads((REPO / PATH).read_bytes())
REVIEW = review_for(PATH, hashlib.sha1((REPO / PATH).read_bytes()).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(source=None, review=None):
return build_document(
PATH,
SOURCE if source is None else source,
None,
None,
[],
REVIEW if review is None else review,
)
def refuse(s, r):
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(s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
def test_each_saved_value_type_pointer_and_exact_evidence(f):
v = pointer(SOURCE, f["pointer"])
assert canonical(v) == canonical(f["value"]) and type(v).__name__ == f["json_type"]
assert pointer(SOURCE, f["input_pointer"])["name"] == f["field"]
for k in [
"label",
"type",
"schema_default",
"unit",
"limits",
"effect",
"execution_note",
"role",
]:
assert f[k]
for k in f["evidence"]:
e = BLOCK["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 or e["git_commit_binding"] == "unknown"
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
@pytest.mark.parametrize("variant", ["value", "type", "text"])
def test_each_saved_position_refuses_forged_source_or_definition(f, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = r["fixed_version_document"]
bf = next(x for x in b["fields"] if x["pointer"] == f["pointer"])
if variant == "text":
bf["limits"] = "forged"
else:
parent, _, key = f["pointer"].rpartition("/")
target = pointer(s, parent)
target[int(key) if isinstance(target, list) else key] = (
"forged" if variant == "type" else None
)
bf["value"] = pointer(s, f["pointer"])
bf["json_type"] = type(bf["value"]).__name__
refuse(s, r)
@pytest.mark.parametrize("i", range(5))
@pytest.mark.parametrize("variant", ["mode", "identity"])
def test_each_node_mode_and_identity_bound(i, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = s["nodes"][i]
if variant == "mode":
n["mode"] = 0 if n["mode"] == 4 else 4
else:
n["properties"]["ver"] = "forged"
refuse(s, r)
@pytest.mark.parametrize("i", range(6))
@pytest.mark.parametrize("direction", ["record", "input", "output"])
def test_each_physical_endpoint_bound_both_directions(i, direction):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
edge, src, slot, dst, inp, _ = s["links"][i]
ns = {n["id"]: n for n in s["nodes"]}
if direction == "record":
s["links"][i][3] = 99999
elif direction == "input":
ns[dst]["inputs"][inp]["link"] = 99999
else:
ns[src]["outputs"][slot]["links"] = [99999]
refuse(s, r)
@pytest.mark.parametrize(
"variant",
[
"empty",
"missing",
"connectable",
"ue",
"frontend",
"summary",
"citation",
"source_sha",
"path",
"layout",
"dynamic",
"fps_slot",
],
)
def test_special_shapes_and_identity_fail_closed(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
ns = {n["id"]: n for n in s["nodes"]}
b = r["fixed_version_document"]
if variant == "empty":
ns[7]["widgets_values"] = {}
elif variant == "missing":
del ns[7]["widgets_values"]
elif variant == "connectable":
ns[8]["properties"]["ue_properties"]["widget_ue_connectable"]["fps"] = False
elif variant == "ue":
ns[1]["properties"]["ue_properties"]["version"] = "forged"
elif variant == "frontend":
s["extra"]["frontendVersion"] = "forged"
elif variant == "summary":
b["user_overview"] = "forged"
elif variant == "citation":
b["sources"][0]["sha256"] = "0" * 64
elif variant == "source_sha":
b["source_sha1"] = "0" * 40
elif variant == "path":
b["source_path"] = "forged"
elif variant == "layout":
ns[1]["pos"][0] += 1
elif variant == "dynamic":
ns[1]["inputs"][2]["name"] = "width"
elif variant == "fps_slot":
ns[7]["outputs"][2]["name"] = "audio"
refuse(s, r)
def test_independent_inventory_edges_and_reference_conditions():
ns = {n["id"]: n for n in SOURCE["nodes"]}
assert len(ns) == 5 and len(SOURCE["links"]) == 6
assert all(n["mode"] == 0 for n in ns.values())
assert ns[1]["widgets_values"] == ["scale by multiplier", 1, "ULTRA"]
assert type(ns[1]["widgets_values"][1]) is int
assert ns[7]["widgets_values"] == []
assert ns[8]["widgets_values"] == [30] and type(ns[8]["widgets_values"][0]) is int
assert ns[8]["properties"]["ue_properties"]["widget_ue_connectable"] == {"fps": True}
assert ns[6]["widgets_values"] == ["WanVideo_00003-audio.mp4", "image"]
assert ns[9]["widgets_values"] == ["video/ComfyUI", "auto", "auto"]
assert SOURCE["links"] == [
[5, 6, 0, 7, 0, "VIDEO"],
[7, 1, 0, 8, 0, "IMAGE"],
[8, 7, 1, 8, 1, "AUDIO"],
[9, 7, 2, 8, 2, "FLOAT"],
[10, 8, 0, 9, 0, "VIDEO"],
[19, 7, 0, 1, 0, "IMAGE"],
]
for e, src, slot, dst, inp, _ in SOURCE["links"]:
assert e in ns[src]["outputs"][slot]["links"] and ns[dst]["inputs"][inp]["link"] == e
assert len(BLOCK["fields"]) == len({f["pointer"] for f in BLOCK["fields"]}) == 9
actual = {
f"/nodes/{i}/widgets_values/{k}"
for i, n in enumerate(SOURCE["nodes"])
for k in range(len(n["widgets_values"]))
}
assert actual == {f["pointer"] for f in BLOCK["fields"]}
assert all(not isinstance(f["value"], (list, dict)) for f in BLOCK["fields"])
assert BLOCK["comments"] == [] and not doc()["source_notes"]
assert BLOCK["zero_saved_nodes"][0]["pointer"] == "/nodes/0/widgets_values"
assert BLOCK["counts"]["linked_saved_fields"] == 1
assert BLOCK["counts"]["frontend_only"] == 1
assert doc()["fixed_version"]["matched"] and not BLOCK["counts"]["final_prompt_constructed"]
assert doc()["dependencies"]["check_state"] == "unchecked"
assert doc()["status"] == dict(imported=True, doc_done=False, exec_verified=False)
for word in [
"原音频",
"round",
"Git绑定未知",
"MAX_PIXELS",
"CUDA",
"output",
"UE7.7",
"fps:true",
"onAfterGraphConfigured",
"千分率",
"音画同步",
"H264",
"AAC",
"9标量",
]:
assert word in " ".join(BLOCK["flow"])
def test_old_29_review_objects_unchanged():
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(subprocess.check_output(["git", "show", "334549a:" + resource], cwd=REPO))
now = json.loads((REPO / resource).read_bytes())
now[PATH].pop("fixed_version_document")
assert now == old and len(DEFINITIONS) == 30
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
assert len(blocks) == 29 and sum(len(b["fields"]) for b in blocks) == 895
assert (
sum(
len(
build_document(p, json.loads((REPO / p).read_bytes()), None, None, [], r)[
"source_notes"
]
)
for p, r in old.items()
if "fixed_version_document" in r
)
== 21
)
assert review_for(PATH, "0" * 40) is None