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

281 lines
10 KiB
Python

"""SeedVR2 scope/boundary, dual storage, exact source and editorial trust checks."""
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/seedvr2-video-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("/Seedvr2_3b_int8_视频放大.json"))
SOURCE = json.loads((REPO / PATH).read_bytes())
REVIEW = review_for(PATH, hashlib.sha1((REPO / PATH).read_bytes()).hexdigest())
BLOCK = REVIEW["fixed_version_document"]
SUB = SOURCE["definitions"]["subgraphs"][0]
SCOPES = [("", SOURCE), ("/definitions/subgraphs/0", SUB)]
NODES = [(f"{prefix}/nodes/{i}", n) for prefix, g in SCOPES for i, n in enumerate(g["nodes"])]
EDGES = [(f"{prefix}/links/{i}", e) for prefix, g in SCOPES for i, e in enumerate(g["links"])]
def pointer(obj, path):
for k in path.strip("/").split("/"):
obj = obj[int(k)] if isinstance(obj, list) else obj[k.replace("~1", "/").replace("~0", "~")]
return obj
def doc(source=SOURCE, review=REVIEW):
return build_document(PATH, source, None, None, [], review)
def refuse(source, review):
b = review["fixed_version_document"]
b["canonical_sha256"] = hashlib.sha256(canonical(source)).hexdigest()
review["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
assert not doc(source, review)["fixed_version"]["matched"]
@pytest.mark.parametrize(
"f", BLOCK["fields"], ids=lambda f: f["scope"] + ":" + f["node_id"] + "." + f["field"]
)
def test_each_saved_position_dual_storage_type_scope_and_exact_source(f):
v = pointer(SOURCE, f["pointer"])
named = pointer(SOURCE, f["named_pointer"])
assert canonical(v) == canonical(f["value"]) and type(v).__name__ == f["json_type"]
assert named == ({"images": v} if f["field"] == "rgthree_comparer" else v)
for k in [
"effect",
"limits",
"schema_default",
"unit",
"execution_note",
"role",
"label",
"type",
]:
assert f[k]
assert f["named_pointer"] in f["execution_note"]
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())
and len(e["commit"]) == 40
)
@pytest.mark.parametrize("f", BLOCK["fields"])
@pytest.mark.parametrize("variant", ["saved", "named", "text"])
def test_every_field_forgery_rejected_even_recomputed_caller_digest(f, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
if variant == "text":
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["limits"] = "forged"
else:
p = f["pointer"] if variant == "saved" else f["named_pointer"]
base, _, k = p.rpartition("/")
parent = pointer(s, base)
parent[int(k) if isinstance(parent, list) else k] = None
refuse(s, r)
@pytest.mark.parametrize("pair", NODES, ids=lambda pair: pair[0])
@pytest.mark.parametrize("variant", ["mode", "identity"])
def test_all_root_and_definition_nodes_bound(pair, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = pointer(s, pair[0])
if variant == "mode":
n["mode"] = 4
else:
n["properties"]["ver"] = "forged"
refuse(s, r)
@pytest.mark.parametrize("pair", EDGES, ids=lambda pair: pair[0])
def test_every_physical_and_boundary_edge_record_bound(pair):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
e = pointer(s, pair[0])
if isinstance(e, list):
e[3] = 99999
else:
e["target_id"] = 99999
refuse(s, r)
@pytest.mark.parametrize("b", BLOCK["boundaries"])
@pytest.mark.parametrize("variant", ["host", "definition", "target"])
def test_all_host_child_boundary_endpoints_bound(b, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
p = b[variant + "_pointer"]
port = pointer(s, p)
if variant == "definition":
port["linkIds"] = []
elif variant == "host":
port["name"] = "forged"
else:
port["link"] = 99999
refuse(s, r)
@pytest.mark.parametrize(
"variant",
[
"dangling",
"extra_missing",
"extra_empty",
"schema_port",
"format_codec",
"bit_depth",
"note0",
"note1",
"missing_widgets",
"preview",
"ue",
"sub_id",
"review_digest",
],
)
def test_anomaly_metadata_and_notes_cannot_be_rebound(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
sub = s["definitions"]["subgraphs"][0]
ns = {n["id"]: n for n in sub["nodes"]}
if variant == "dangling":
ns[79]["outputs"][0]["links"] = [77]
elif variant == "extra_missing":
s["extra"]["links_added_by_ue"] = []
elif variant == "extra_empty":
sub["extra"]["ue_links"] = None
elif variant == "schema_port":
ns[75]["inputs"][3]["type"] = "INT"
elif variant == "format_codec":
s["nodes"][3]["widgets_values_named"].pop("format.codec")
elif variant == "bit_depth":
ns[75]["widgets_values"][1] = 10
elif variant.startswith("note"):
s["nodes"][int(variant[-1])]["widgets_values"][0] = "forged"
elif variant == "missing_widgets":
ns[74]["widgets_values"] = []
elif variant == "preview":
s["nodes"][4]["properties"]["previewExposures"] = []
elif variant == "ue":
s["nodes"][4]["properties"]["ue_properties"]["version"] = "other"
elif variant == "sub_id":
sub["id"] = "different"
else:
r["fixed_version_document"]["sources"][0]["sha256"] = "0" * 64
refuse(s, r)
def test_independent_inventory_boundaries_anomaly_and_semantics():
assert (len(SOURCE["nodes"]), len(SOURCE["links"]), len(SUB["nodes"]), len(SUB["links"])) == (
5,
2,
13,
34,
)
assert all(n["mode"] == 0 for _, n in NODES)
saved = {
f"{prefix}/nodes/{i}/widgets_values/{k}"
for prefix, g in SCOPES
for i, n in enumerate(g["nodes"])
if n["type"] != "MarkdownNote"
for k in range(len(n.get("widgets_values", [])))
}
assert saved == {f["pointer"] for f in BLOCK["fields"]} and len(saved) == 47
assert sum(f["scope"] == "root" for f in BLOCK["fields"]) == 17
assert all(not isinstance(f["value"], (dict, list)) for f in BLOCK["fields"])
assert [n["id"] for _, n in NODES if "widgets_values" not in n] == [58, 61, 74]
assert len(SUB["inputs"]) == 12 and len(BLOCK["boundaries"]) == 13 and SUB["widgets"] == []
ns = {n["id"]: n for n in SUB["nodes"]}
assert ns[79]["outputs"][0]["links"] == [76, 77] and 76 not in {e["id"] for e in SUB["links"]}
assert "links_added_by_ue" not in SOURCE["extra"]
for pre, g in SCOPES:
nodes = {n["id"]: n for n in g["nodes"]}
for e in g["links"]:
eid, src, slot, dst, inp, kind = (
e
if isinstance(e, list)
else [
e[k]
for k in ["id", "origin_id", "origin_slot", "target_id", "target_slot", "type"]
]
)
assert eid in (
nodes[src]["outputs"][slot]["links"] if src >= 0 else g["inputs"][slot]["linkIds"]
)
if dst >= 0:
assert nodes[dst]["inputs"][inp]["link"] == eid
else:
assert eid in g["outputs"][inp]["linkIds"]
for n in BLOCK["comments"]:
assert pointer(SOURCE, n["pointer"]) == pointer(SOURCE, n["named_pointer"]) == n["text"]
assert doc()["fixed_version"]["matched"] and len(doc()["source_notes"]) == 2
assert doc()["dependencies"]["check_state"] == "unchecked"
assert doc()["status"] == dict(imported=True, doc_done=False, exec_verified=False)
text = " ".join(BLOCK["flow"])
for phrase in [
"true选择on_true",
"[76,77]",
"format.codec",
"无color_space/codec",
"INT",
"无steps/cfg/temporal宿主边",
"941",
"988",
"未实跑",
]:
assert phrase in text
assert review_for(PATH, "0" * 40) is None
def test_exact_video_schema_conflicts_are_supported_by_static_originals():
import ast
ref = OUT.parent / "seedvr2-video-reference"
video = (ref / "video__comfy_extras__nodes_video.py").read_text()
tree = ast.parse(video)
def source(name):
return ast.get_source_segment(
video, next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == name)
)
save = source("SaveVideo")
create = source("CreateVideo")
split = source("GetVideoComponents")
assert "format.codec" not in save and 'io.Combo.Input("format"' in save
assert '"color_space"' not in create and '"codec"' not in create
assert "io.Int.Input(" in create and '"bit_depth"' in create
assert 'io.Int.Output(display_name="bit_depth")' in split and '"color_space"' not in split
switch = (ref / "video__comfy_extras__nodes_logic.py").read_text()
assert "io.NodeOutput(on_true if switch else on_false)" in switch
assert 'return ["on_true"]' in switch and 'return ["on_false"]' in switch
def test_old_thirty_one_objects_and_entire_other_reviews_unchanged():
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(subprocess.check_output(["git", "show", "291ff48:" + resource], cwd=REPO))
now = json.loads((REPO / resource).read_bytes())
now[PATH].pop("fixed_version_document")
assert now == old and len(DEFINITIONS) == 32
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
assert (
len(blocks) == 31
and sum(len(b["fields"]) for b in blocks) == 941
and 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
)
== 22
)