300 lines
12 KiB
Python
300 lines
12 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/watermark-document-evidence"
|
|
PATHS = [next(p for p in DEFINITIONS if p.endswith(n + ".json")) for n in ["图像4种添加水印节点"]]
|
|
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("node", [1, 12, 5, 6, 20, 16, 17, 10, 7, 14, 8, 9, 15, 11, 13, 3, 2])
|
|
@pytest.mark.parametrize("kind", ["mode", "ver", "cnr", "ue"])
|
|
def test_each_identity_and_mode_refuses_recomputed_digest(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 == "mode":
|
|
n["mode"] = 2
|
|
elif kind == "ue":
|
|
n["properties"]["ue_properties"]["version"] = "forged"
|
|
else:
|
|
n["properties"]["ver" if kind == "ver" else "cnr_id"] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("node", [1, 16, 17, 7, 8, 9, 15, 11, 13])
|
|
@pytest.mark.parametrize("kind", ["missing", "null", "object"])
|
|
def test_zero_positions_are_not_missing_null_or_object(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 == "missing":
|
|
del n["widgets_values"]
|
|
else:
|
|
n["widgets_values"] = None if kind == "null" else {}
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("edge", range(15))
|
|
@pytest.mark.parametrize("side", ["input", "output", "physical"])
|
|
def test_each_physical_and_dual_endpoint_refuses(edge, side):
|
|
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
|
e = s["links"][edge]
|
|
nodes = {n["id"]: n for n in s["nodes"]}
|
|
if side == "input":
|
|
nodes[e[3]]["inputs"][e[4]]["link"] = None
|
|
elif side == "output":
|
|
nodes[e[1]]["outputs"][e[2]]["links"].remove(e[0])
|
|
else:
|
|
e[1] = 999
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"kind",
|
|
[
|
|
"source_sha",
|
|
"citation",
|
|
"frontend",
|
|
"ue",
|
|
"swap",
|
|
"unconnected_mask",
|
|
"wrong_path",
|
|
"seed",
|
|
"image",
|
|
"logo",
|
|
"font",
|
|
],
|
|
)
|
|
def test_references_and_binding_fail_closed(kind):
|
|
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
|
nodes = {n["id"]: n for n in s["nodes"]}
|
|
if kind == "source_sha":
|
|
r["source_sha"] = "0" * 40
|
|
elif kind == "citation":
|
|
r["fixed_version_document"]["sources"][0]["sha256"] = "0" * 64
|
|
elif kind == "frontend":
|
|
s["extra"]["frontendVersion"] = "1.43.17"
|
|
elif kind == "ue":
|
|
s["extra"]["links_added_by_ue"] = [1]
|
|
elif kind == "swap":
|
|
other = next(p for p in DEFINITIONS if p.endswith("/birefnet_图像移除背景.json"))
|
|
r = copy.deepcopy(review_for(other, hashlib.sha1((REPO / other).read_bytes()).hexdigest()))
|
|
elif kind == "unconnected_mask":
|
|
nodes[5]["outputs"][1]["links"] = []
|
|
elif kind == "wrong_path":
|
|
r["fixed_version_document"]["source_path"] = "forged"
|
|
elif kind == "seed":
|
|
nodes[20]["widgets_values"][5] = "fixed"
|
|
elif kind == "image":
|
|
nodes[6]["widgets_values"][0] = nodes[5]["widgets_values"][0]
|
|
elif kind == "logo":
|
|
nodes[20]["widgets_values"][13] = "None"
|
|
else:
|
|
nodes[3]["widgets_values"][3] = "forged.ttf"
|
|
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(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("node", [12, 14])
|
|
@pytest.mark.parametrize("variant", [[], None, {}])
|
|
def test_reroute_missing_key_is_not_zero_or_null(node, variant):
|
|
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
|
next(n for n in s["nodes"] if n["id"] == node)["widgets_values"] = variant
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_exact_graph_semantics_and_old_24_objects():
|
|
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
|
|
old = json.loads(subprocess.check_output(["git", "show", "11938ca:" + 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")
|
|
block = now[PATHS[0]].pop("fixed_version_document")
|
|
assert now == old and len(DEFINITIONS) == 26
|
|
oldblocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
|
|
assert len(oldblocks) == 24 and sum(len(b["fields"]) for b in oldblocks) == 665
|
|
assert sum(len(b.get("comments", [])) for b in oldblocks) == 15 # ASR source note independent
|
|
assert block["counts"] == dict(
|
|
saved_fields=56,
|
|
scalar_saved_positions=56,
|
|
history_objects=0,
|
|
source_nodes=17,
|
|
physical_edges=15,
|
|
source_notes=0,
|
|
linked_saved_fields=0,
|
|
frontend_only=4,
|
|
final_scalars=0,
|
|
final_prompt_constructed=False,
|
|
)
|
|
g = SOURCES[0]
|
|
ns = {n["id"]: n for n in g["nodes"]}
|
|
assert len(ns) == 17 and len(g["links"]) == 15 and all(n["mode"] == 0 for n in ns.values())
|
|
assert {i for i, n in ns.items() if "widgets_values" not in n} == {12, 14}
|
|
assert {i for i, n in ns.items() if n.get("widgets_values") == []} == {
|
|
1,
|
|
8,
|
|
16,
|
|
7,
|
|
9,
|
|
11,
|
|
13,
|
|
15,
|
|
17,
|
|
}
|
|
assert sum(len(n.get("widgets_values", [])) for n in ns.values()) == 56
|
|
assert (
|
|
"frontendVersion" not in g["extra"]
|
|
and g["extra"]["ue_links"] == g["extra"]["links_added_by_ue"] == []
|
|
)
|
|
assert len(block["zero_saved_nodes"]) == 9 and not block["comments"]
|
|
assert len({f["pointer"] for f in block["fields"]}) == 56
|
|
assert ns[20]["widgets_values"][4:7] == [166434517983310, "randomize", True]
|
|
assert [f["field"] for f in block["fields"] if f["node_id"] == "20"][4:7] == [
|
|
"seed",
|
|
"control_after_generate",
|
|
"use_exif",
|
|
]
|
|
assert g["links"] == [
|
|
[4, 1, 0, 7, 0, "IMAGE"],
|
|
[6, 8, 0, 9, 0, "IMAGE"],
|
|
[8, 1, 0, 8, 0, "IMAGE"],
|
|
[10, 3, 0, 11, 0, "IMAGE"],
|
|
[11, 5, 0, 12, 0, "IMAGE"],
|
|
[13, 12, 0, 3, 0, "IMAGE"],
|
|
[14, 12, 0, 2, 0, "IMAGE"],
|
|
[16, 2, 0, 13, 0, "IMAGE"],
|
|
[17, 6, 0, 14, 0, "IMAGE"],
|
|
[22, 10, 0, 16, 0, "IMAGE"],
|
|
[23, 16, 0, 17, 0, "IMAGE"],
|
|
[28, 12, 0, 20, 0, "IMAGE"],
|
|
[29, 20, 0, 15, 0, "IMAGE"],
|
|
[30, 12, 0, 1, 0, "IMAGE"],
|
|
[31, 14, 0, 1, 1, "IMAGE"],
|
|
]
|
|
for eid, origin, slot, target, inp, typ in g["links"]:
|
|
assert eid in ns[origin]["outputs"][slot]["links"]
|
|
assert ns[origin]["outputs"][slot]["type"] == typ
|
|
assert ns[target]["inputs"][inp]["link"] == eid
|
|
assert ns[target]["inputs"][inp]["type"] == ("*" if target in [12, 14] else typ)
|
|
assert all(ns[i]["outputs"][1]["links"] is None for i in [5, 6, 10])
|
|
assert ns[3]["outputs"][1]["links"] is None
|
|
assert next(i for i in ns[20]["inputs"] if i["name"] == "exif_data")["link"] is None
|
|
assert next(i for i in ns[2]["inputs"] if i["name"] == "水印图像")["link"] 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"
|
|
for token in [
|
|
"不是四种叠加",
|
|
"独立提取",
|
|
"外线未接",
|
|
"false仍旋转",
|
|
"间距22",
|
|
"temp PNG",
|
|
"unknown",
|
|
"没有frontendVersion",
|
|
"compression_ratio=100",
|
|
"YCbCr",
|
|
"wm_size",
|
|
"bar_width",
|
|
"白",
|
|
"randomize",
|
|
]:
|
|
assert token in json.dumps(block, ensure_ascii=False)
|
|
|
|
|
|
@pytest.mark.parametrize("other", [p for p in DEFINITIONS if p not in PATHS])
|
|
def test_every_old_block_and_new_block_cannot_be_swapped(other):
|
|
raw = (REPO / other).read_bytes()
|
|
old_source = json.loads(raw)
|
|
old_review = copy.deepcopy(review_for(other, hashlib.sha1(raw).hexdigest()))
|
|
new_review = copy.deepcopy(REVIEWS[0])
|
|
old_block = old_review["fixed_version_document"]
|
|
old_review["fixed_version_document"] = new_review["fixed_version_document"]
|
|
new_review["fixed_version_document"] = old_block
|
|
assert not doc(0, review=new_review)["fixed_version"]["matched"]
|
|
assert not build_document(other, old_source, None, None, [], old_review)["fixed_version"][
|
|
"matched"
|
|
]
|