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

269 lines
10 KiB
Python

"""Exact source, nested preview types, package citations and old fifteen definitions."""
from __future__ import annotations
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/openpose-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("openpose-video-姿态检测.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("/"):
part = part.replace("~1", "/").replace("~0", "~")
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
return obj
def set_value(obj, path, value):
parent, _, key = path.rpartition("/")
target = pointer(obj, parent)
target[int(key) if isinstance(target, list) else key] = value
def doc(source=None, review=None, path=PATH):
return build_document(
path,
SOURCE if source is None else source,
None,
None,
[],
REVIEW if review is None else review,
)
@pytest.mark.parametrize("field", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
def test_values_types_citations(field):
v = pointer(SOURCE, field["pointer"])
assert canonical(v) == canonical(field["value"]) and type(v).__name__ == field["json_type"]
node = pointer(SOURCE, field["pointer"].split("/widgets_values/")[0])
assert str(node["id"]) == field["node_id"] and node["type"] == field["class_type"]
for k in ["label", "schema_default", "unit", "limits", "effect", "execution_note"]:
assert field[k]
for k in field["evidence"]:
e = BLOCK["sources"][k]
raw = (OUT / e["local_file"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
if e["id"] == "vhs":
assert e["git_commit_binding"] == "unknown" and "发布包SHA256:" in e["commit"]
else:
assert len(e["commit"]) == 40 and e["commit"] in e["url"]
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
@pytest.mark.parametrize("index", range(27))
@pytest.mark.parametrize("kind", ["value", "type", "pointer", "citation", "role", "default"])
def test_forged_field_and_recalculated_digest_refused(index, kind):
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = review["fixed_version_document"]
f = b["fields"][index]
if kind in ["value", "type"]:
set_value(source, f["pointer"], {"forged": True} if kind == "value" else None)
f["value"] = pointer(source, f["pointer"])
f["json_type"] = type(f["value"]).__name__
elif kind == "citation":
b["sources"][f["evidence"][0]]["sha256"] = "0" * 64
else:
f[{"default": "schema_default"}.get(kind, kind)] = "forged"
b["canonical_sha256"] = hashlib.sha256(canonical(source)).hexdigest()
review["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
actual = doc(source, review)["fixed_version"]
assert not actual["matched"] and "fields" not in actual
def leaves(obj, path):
if isinstance(obj, dict):
for key, value in obj.items():
yield from leaves(value, path + "/" + key)
else:
yield path
MUTATIONS = (
[f"/links/{i}/{j}" for i in range(4) for j in range(6)]
+ ["/extra/frontendVersion", "/nodes/0/widgets_values/0"]
+ [
f"/nodes/{i}/{suffix}"
for i in range(5)
for suffix in [
"mode",
"type",
"properties/ue_properties/version",
"properties/ue_properties/widget_ue_connectable",
"properties/ue_properties/input_ue_unconnectable",
]
]
+ [f"/nodes/{i}/properties/{key}" for i in range(1, 5) for key in ["cnr_id", "ver"]]
+ [
p
for i in [2, 4]
for p in leaves(
SOURCE["nodes"][i]["widgets_values"]["videopreview"],
f"/nodes/{i}/widgets_values/videopreview",
)
]
)
@pytest.mark.parametrize("path", MUTATIONS)
def test_graph_versions_nested_cache_notes_and_ue_fail_closed(path):
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
set_value(source, path, "forged")
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
canonical(source)
).hexdigest()
review["caller_digest"] = hashlib.sha256(
canonical(review["fixed_version_document"])
).hexdigest()
assert not doc(source, review)["fixed_version"]["matched"]
@pytest.mark.parametrize("value", [None, [], "missing"])
def test_empty_object_not_missing_null_or_list(value):
source = copy.deepcopy(SOURCE)
if value == "missing":
del source["nodes"][3]["widgets_values"]
else:
source["nodes"][3]["widgets_values"] = value
assert not doc(source=source)["fixed_version"]["matched"]
@pytest.mark.parametrize(
"key",
[
"source_path",
"source_sha1",
"source_sha256",
"notice",
"user_overview",
"versions",
"comments",
"flow",
"sources",
],
)
def test_editorial_forgery_refused(key):
review = copy.deepcopy(REVIEW)
review["fixed_version_document"][key] = "forged"
review["caller_digest"] = hashlib.sha256(
canonical(review["fixed_version_document"])
).hexdigest()
assert not doc(review=review)["fixed_version"]["matched"]
def test_exact_document_topology_and_preservation():
d = doc()
assert d["fixed_version"]["matched"] and d["fixed_version"]["fields"] == BLOCK["fields"]
assert len(BLOCK["fields"]) == 27 and len(BLOCK["comments"]) == 1
assert sum(isinstance(f["value"], dict) for f in BLOCK["fields"]) == 2
assert len(SOURCE["nodes"]) == 5 and len(SOURCE["links"]) == 4
assert SOURCE["links"] == [
[4, 1, 0, 4, 0, "IMAGE"],
[10, 2, 0, 1, 0, "IMAGE"],
[11, 2, 3, 9, 0, "VHS_VIDEOINFO"],
[12, 9, 0, 4, 4, "FLOAT"],
]
nodes = {n["id"]: n for n in SOURCE["nodes"]}
for edge, origin, slot, target, inp, typ in SOURCE["links"]:
assert edge in nodes[origin]["outputs"][slot]["links"]
assert nodes[target]["inputs"][inp]["link"] == edge
assert nodes[target]["inputs"][inp]["type"] == typ
assert nodes[9]["widgets_values"] == {} and type(nodes[9]["widgets_values"]) is dict
assert all(n["mode"] == 0 for n in SOURCE["nodes"])
assert pointer(SOURCE, BLOCK["comments"][0]["pointer"]) == BLOCK["comments"][0]["text"]
assert not BLOCK["counts"]["final_prompt_constructed"]
assert all(
s in "".join(BLOCK["flow"])
for s in [
"未实跑",
"包同字节不等于提交绑定",
"loaded_fps",
"外线覆盖",
"音频",
"UE7.5.2",
"空对象",
"不是缺键/null/[]",
]
)
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
previous = json.loads(subprocess.check_output(["git", "show", "fec5cca:" + resource], cwd=REPO))
current = json.loads((REPO / resource).read_bytes())
multivideo = next(p for p in current if p.endswith("/多视频合成分屏展示.json"))
current[multivideo].pop("fixed_version_document")
watermark = next(p for p in current if p.endswith("/图像4种添加水印节点.json"))
current[watermark].pop("fixed_version_document")
birefnet = next(p for p in current if p.endswith("/birefnet_图像移除背景.json"))
current[birefnet].pop("fixed_version_document")
latent = next(p for p in current if p.endswith("/加载本地latent解码.json"))
current[latent].pop("fixed_version_document")
later = next(p for p in current if p.endswith("/截取视频每一帧.json"))
current[later].pop("fixed_version_document")
for added in [
p
for p in current
if p.endswith(
("视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json")
)
]:
current[added].pop("fixed_version_document")
reverse = next(p for p in current if p.endswith("/视频倒放.json"))
current[reverse].pop("fixed_version_document")
nlf = next(p for p in current if p.endswith("NLF_视频人物动作骨架获取.json"))
current[nlf].pop("fixed_version_document")
assert current[PATH].pop("fixed_version_document") == BLOCK
assert current == previous and len(DEFINITIONS) == 26
old = [r["fixed_version_document"] for r in previous.values() if "fixed_version_document" in r]
assert len(old) == 15 and sum(len(b["fields"]) for b in old) == 394
assert sum(len(b.get("comments", [])) for b in old) == 13 # one historical ASR flow note
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
assert d["dependencies"]["check_state"] == "unchecked"
assert not doc(path="untrusted.json").get("fixed_version")
for p in DEFINITIONS:
if p in [watermark, multivideo]:
continue
if p.endswith("/birefnet_图像移除背景.json"):
continue
if p.endswith(("/截取视频每一帧.json", "/加载本地latent解码.json")):
continue
if p.endswith(
("视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json")
):
continue
if p in [PATH, nlf, reverse]:
continue
r = copy.deepcopy(REVIEW)
r["fixed_version_document"] = previous[p]["fixed_version_document"]
assert not doc(review=r)["fixed_version"]["matched"]
def test_raw_source_identity_and_missing_packages(tmp_path):
assert review_for(PATH, "0" * 40) is None
r = copy.deepcopy(REVIEW)
r["source_sha"] = "0" * 40
assert not doc(review=r)["fixed_version"]["matched"]
import sys
code = (
"import runpy,sys; from pathlib import Path; ns=runpy.run_path('recompute.py'); "
"ns['require_packages'].__globals__['REPO']=Path(sys.argv[1]); ns['require_packages']()"
)
result = subprocess.run(
[sys.executable, "-c", code, str(tmp_path)], cwd=OUT, capture_output=True, text=True
)
assert result.returncode != 0 and "缺少wheel/sdist包产物" in result.stderr
assert "uv build --project calliope-backend --offline" in result.stderr