274 lines
10 KiB
Python
274 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/latent-document-evidence"
|
||
PATHS = [next(p for p in DEFINITIONS if p.endswith(n + ".json")) for n in ["加载本地latent解码"]]
|
||
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"] == 4)["widgets_values"]["videopreview"]["params"][
|
||
"filename"
|
||
] = "forged"
|
||
elif kind in ["missing", "null", "list", "object"]:
|
||
values = next(n for n in s["nodes"] if n["id"] == 4)["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"] == 10)["widgets_values"] = []
|
||
elif kind.startswith("preview_"):
|
||
n = next(n for n in s["nodes"] if n["id"] == 3)
|
||
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"] == 5)["widgets_values"][0] = "forged.safetensors"
|
||
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", "256e6be:" + 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")
|
||
b = now[PATHS[0]].pop("fixed_version_document")
|
||
assert now == old and len(DEFINITIONS) == 26
|
||
assert b["counts"] == dict(
|
||
saved_fields=13,
|
||
scalar_saved_positions=12,
|
||
history_objects=1,
|
||
source_nodes=4,
|
||
physical_edges=3,
|
||
source_notes=0,
|
||
linked_saved_fields=0,
|
||
frontend_only=1,
|
||
final_scalars=0,
|
||
final_prompt_constructed=False,
|
||
)
|
||
assert b["comments"] == []
|
||
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
|
||
assert len(blocks) == 22 and sum(len(b["fields"]) for b in blocks) == 649
|
||
assert sum(len(b.get("comments", [])) for b in blocks) == 14
|
||
g = SOURCES[0]
|
||
nodes = {n["id"]: n for n in g["nodes"]}
|
||
assert len(nodes) == 4 and len(g["links"]) == 3
|
||
assert g["links"] == [
|
||
[2, 3, 0, 4, 0, "IMAGE"],
|
||
[4, 5, 0, 3, 1, "VAE"],
|
||
[5, 10, 0, 3, 0, "LATENT"],
|
||
]
|
||
assert all(n["mode"] == 0 for n in nodes.values())
|
||
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[3]["widgets_values"] == [] and len(b["zero_saved_nodes"]) == 1
|
||
assert [nodes[i]["properties"]["ver"] for i in [10, 3, 4, 5]] == [
|
||
"f6ca9376132600e6245d3d9d2f1e5d0ffa448d99",
|
||
"0.3.57",
|
||
"8e4d79471bf1952154768e8435a9300077b534fa",
|
||
"0.3.68",
|
||
]
|
||
assert [nodes[i]["properties"]["ue_properties"]["version"] for i in [10, 3, 4, 5]] == [
|
||
"7.4.1",
|
||
"7.1",
|
||
"7.1",
|
||
"7.4.1",
|
||
]
|
||
assert "frontendVersion" not in g["extra"]
|
||
assert set(f["node_id"] for f in b["fields"]) == {"10", "4", "5"}
|
||
assert nodes[10]["widgets_values"] == [r"latents\ComfyUI_00001_nmxvu_1762517653.latent"]
|
||
assert nodes[5]["widgets_values"] == ["Wan2_1_VAE_bf16.safetensors"]
|
||
assert nodes[4]["inputs"][1]["link"] is None
|
||
assert nodes[4]["inputs"][4]["link"] is None
|
||
assert nodes[4]["outputs"][0]["links"] is None
|
||
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"])
|
||
text = json.dumps(b, ensure_ascii=False)
|
||
for token in [
|
||
"safetensors.torch.load_file",
|
||
"latent_tensor",
|
||
"latent_format_version_0",
|
||
"1/0.18215",
|
||
"零张量",
|
||
"兼容",
|
||
"不证明原始FPS",
|
||
"没有音频连线",
|
||
"VHS_MetadataImage",
|
||
"-shortest",
|
||
"许可授权未知",
|
||
"恢复/钩子次序",
|
||
"五维",
|
||
"0~1",
|
||
]:
|
||
assert token in text
|
||
|
||
|
||
@pytest.mark.parametrize("node", [10, 3, 4, 5])
|
||
@pytest.mark.parametrize("kind", ["mode", "ver", "ue"])
|
||
def test_every_identity_refuses(node, kind):
|
||
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
||
n = next(n for n in s["nodes"] if n["id"] == node)
|
||
if kind == "ue":
|
||
n["properties"]["ue_properties"]["version"] = "forged"
|
||
else:
|
||
(n if kind == "mode" else n["properties"])[kind] = 2 if kind == "mode" else "forged"
|
||
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(3))
|
||
@pytest.mark.parametrize("side", ["output", "input"])
|
||
def test_every_endpoint_refuses(index, side):
|
||
s, r = copy.deepcopy(SOURCES[0]), 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"]
|