266 lines
10 KiB
Python
266 lines
10 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/frames-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("i", [0])
|
|
@pytest.mark.parametrize(
|
|
"kind",
|
|
[
|
|
"edge",
|
|
"mode",
|
|
"version",
|
|
"history",
|
|
"missing",
|
|
"null",
|
|
"list",
|
|
"object",
|
|
"swap",
|
|
"source_sha",
|
|
"citation",
|
|
"ue",
|
|
"empty_node",
|
|
"reference",
|
|
"preview_missing",
|
|
"preview_null",
|
|
"preview_object",
|
|
],
|
|
)
|
|
def test_graph_history_shape_and_binding_refuse(i, kind):
|
|
s, r = copy.deepcopy(SOURCES[i]), copy.deepcopy(REVIEWS[i])
|
|
b = r["fixed_version_document"]
|
|
if kind == "edge":
|
|
s["links"][0][1] = 999
|
|
elif kind == "mode":
|
|
s["nodes"][0]["mode"] = 2
|
|
elif kind == "version":
|
|
s["nodes"][0]["properties"]["ver"] = "forged"
|
|
elif kind == "history":
|
|
next(n for n in s["nodes"] if n["id"] == 9)["widgets_values"]["videopreview"]["params"][
|
|
"filename"
|
|
] = "forged"
|
|
elif kind in ["missing", "null", "list", "object"]:
|
|
values = next(n for n in s["nodes"] if n["id"] == 9)["widgets_values"]
|
|
if kind == "missing":
|
|
del values["videopreview"]
|
|
else:
|
|
values["videopreview"] = {"null": None, "list": [], "object": {}}[kind]
|
|
elif kind == "swap":
|
|
r = copy.deepcopy(
|
|
review_for(
|
|
next(p for p in DEFINITIONS if p.endswith("视频倒放.json")),
|
|
hashlib.sha1(
|
|
(
|
|
REPO / next(p for p in DEFINITIONS if p.endswith("视频倒放.json"))
|
|
).read_bytes()
|
|
).hexdigest(),
|
|
)
|
|
)
|
|
elif kind == "source_sha":
|
|
r["source_sha"] = "0" * 40
|
|
elif kind == "ue":
|
|
s["extra"]["ue_links"] = [1]
|
|
elif kind == "empty_node":
|
|
next(n for n in s["nodes"] if n["id"] == 13)["widgets_values"] = []
|
|
elif kind.startswith("preview_"):
|
|
n = next(n for n in s["nodes"] if n["id"] == 11)
|
|
if kind == "preview_missing":
|
|
del n["widgets_values"]
|
|
else:
|
|
n["widgets_values"] = None if kind == "preview_null" else {}
|
|
elif kind == "reference":
|
|
next(n for n in s["nodes"] if n["id"] == 1)["widgets_values"]["video"] = "forged.mp4"
|
|
else:
|
|
b["sources"][0]["sha256"] = "0" * 64
|
|
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"]
|
|
|
|
|
|
def test_exact_graph_versions_and_preservation():
|
|
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
|
|
old = json.loads(subprocess.check_output(["git", "show", "9db8857:" + resource], cwd=REPO))
|
|
now = json.loads((REPO / resource).read_bytes())
|
|
multivideo = next(p for p in now if p.endswith("/多视频合成分屏展示.json"))
|
|
now[multivideo].pop("fixed_version_document")
|
|
watermark = next(p for p in now if p.endswith("/图像4种添加水印节点.json"))
|
|
now[watermark].pop("fixed_version_document")
|
|
birefnet = next(p for p in now if p.endswith("/birefnet_图像移除背景.json"))
|
|
now[birefnet].pop("fixed_version_document")
|
|
latent = next(p for p in now if p.endswith("/加载本地latent解码.json"))
|
|
now[latent].pop("fixed_version_document")
|
|
b = now[PATHS[0]].pop("fixed_version_document")
|
|
assert now == old and len(DEFINITIONS) == 26
|
|
assert len(b["fields"]) == 31 and b["counts"]["history_objects"] == 3
|
|
assert b["counts"]["scalar_saved_positions"] == 28
|
|
assert b["comments"] == [] and b["counts"]["linked_saved_fields"] == 0
|
|
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
|
|
assert len(blocks) == 21 and sum(len(b["fields"]) for b in blocks) == 618
|
|
assert sum(len(b.get("comments", [])) for b in blocks) == 14 # one ASR flow note
|
|
g = SOURCES[0]
|
|
nodes = {n["id"]: n for n in g["nodes"]}
|
|
assert len(nodes) == len(g["links"]) == 6
|
|
assert g["links"] == [
|
|
[5, 1, 0, 8, 0, "IMAGE"],
|
|
[7, 1, 2, 9, 1, "AUDIO"],
|
|
[8, 10, 0, 11, 0, "IMAGE"],
|
|
[10, 10, 0, 13, 0, "IMAGE"],
|
|
[11, 1, 0, 13, 1, "IMAGE"],
|
|
[12, 13, 0, 9, 0, "IMAGE"],
|
|
]
|
|
assert {i: n["mode"] for i, n in nodes.items()} == {11: 0, 13: 0, 8: 4, 9: 0, 1: 0, 10: 0}
|
|
for e in g["links"]:
|
|
assert e[0] in nodes[e[1]]["outputs"][e[2]]["links"]
|
|
assert nodes[e[1]]["outputs"][e[2]]["type"] == e[5]
|
|
assert nodes[e[3]]["inputs"][e[4]]["link"] == e[0]
|
|
assert nodes[e[3]]["inputs"][e[4]]["type"] == e[5]
|
|
assert nodes[8]["widgets_values"] == nodes[11]["widgets_values"] == []
|
|
assert nodes[13]["widgets_values"] == [2, None]
|
|
assert [nodes[i]["properties"]["ver"] for i in [1, 10, 9, 11, 8, 13]] == [
|
|
"1.7.7",
|
|
"1.7.7",
|
|
"1.7.9",
|
|
"0.16.3",
|
|
"0.3.71",
|
|
"a9ab19d2a7933e03f72bc00103863f06b9483300",
|
|
]
|
|
assert "frontendVersion" not in g["extra"]
|
|
d = doc(0)
|
|
assert d["fixed_version"]["matched"]
|
|
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
|
|
assert d["dependencies"]["check_state"] == "unchecked"
|
|
assert review_for(PATHS[0], "0" * 40) is None
|
|
assert all(step in d["steps"] for step in b["steps"])
|
|
assert not b["counts"]["final_prompt_constructed"]
|
|
text = json.dumps(b, ensure_ascii=False)
|
|
for token in [
|
|
"8n+1",
|
|
"32对齐",
|
|
"不是原片FPS",
|
|
"没有SaveImage",
|
|
"临时PNG",
|
|
"bilinear",
|
|
"零图",
|
|
"不是短批重复末帧",
|
|
"自动同步",
|
|
"-shortest",
|
|
"Git绑定未知",
|
|
"恢复次序",
|
|
]:
|
|
assert token in text
|
|
f = next(f for f in b["fields"] if f["field"] == "saved_position_1")
|
|
assert f["value"] is None and f["role"] == "frontend_state_unknown"
|
|
assert set(f["node_id"] for f in b["fields"]) == {"13", "9", "1", "10"}
|
|
for node in (1, 10):
|
|
assert nodes[node]["widgets_values"]["force_rate"] == 25
|
|
assert nodes[node]["widgets_values"]["format"] == "LTXV"
|
|
assert nodes[1]["widgets_values"]["frame_load_cap"] == 505
|
|
assert nodes[10]["widgets_values"]["frame_load_cap"] == 1
|
|
|
|
|
|
@pytest.mark.parametrize("node", [1, 10, 9, 11, 8, 13])
|
|
def test_each_version_and_mode_refuses(node):
|
|
for key, value in [("mode", 2), ("ver", "forged")]:
|
|
s = copy.deepcopy(SOURCES[0])
|
|
r = copy.deepcopy(REVIEWS[0])
|
|
n = next(n for n in s["nodes"] if n["id"] == node)
|
|
(n if key == "mode" else n["properties"])[key] = value
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("index", range(6))
|
|
@pytest.mark.parametrize("side", ["output", "input"])
|
|
def test_every_endpoint_refuses(index, side):
|
|
s = copy.deepcopy(SOURCES[0])
|
|
r = copy.deepcopy(REVIEWS[0])
|
|
e = s["links"][index]
|
|
nodes = {n["id"]: n for n in s["nodes"]}
|
|
if side == "output":
|
|
nodes[e[1]]["outputs"][e[2]]["links"].remove(e[0])
|
|
else:
|
|
nodes[e[3]]["inputs"][e[4]]["link"] = None
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|