202 lines
9.1 KiB
Python
202 lines
9.1 KiB
Python
"Exact graph binding, all saved positions, dual scopes and closed eligibility."
|
|
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
import runpy
|
|
import sys
|
|
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/zimage-generalb-document-evidence")
|
|
PATH = "example_ComfyUI_workflows/3-图像生成/Z-Image/Z_Image_turbo_十八种风格_通用b.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, p):
|
|
for x in p.strip("/").split("/"):
|
|
obj = obj[int(x)] if isinstance(obj, list) else obj[x]
|
|
return obj
|
|
|
|
|
|
def doc(s=SOURCE, r=REVIEW):
|
|
return build_document(PATH, s, None, None, [], r)
|
|
|
|
|
|
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
|
|
def test_each_saved_position_and_citation(f):
|
|
assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"])
|
|
assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"]
|
|
assert "无此具名保存键" in f["named_pointer"] and "named_value" not in f
|
|
assert all(
|
|
f[k] for k in ["label", "unit", "limits", "schema_default", "effect", "execution_note"]
|
|
)
|
|
if f["input_pointer"].startswith("/"):
|
|
assert pointer(SOURCE, f["input_pointer"])["name"] == f["input_name"]
|
|
for i in f["evidence"]:
|
|
e = BLOCK["sources"][i]
|
|
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())
|
|
|
|
|
|
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
|
|
@pytest.mark.parametrize("variant", ["source", "text"])
|
|
def test_caller_rehash_cannot_expand_trust(f, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
if variant == "text":
|
|
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged"
|
|
else:
|
|
p, _, key = f["pointer"].rpartition("/")
|
|
pointer(s, p)[int(key)] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["extra", "links", "id", "groups", "version"])
|
|
def test_whole_graph_mutations_fail_closed(key):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
source[key] = "forged"
|
|
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
|
canonical(source)
|
|
).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["mode", "properties", "inputs", "outputs"])
|
|
@pytest.mark.parametrize("index", [0, 4, 7, 8])
|
|
def test_node_identity_and_state_fail_closed(key, index):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
source["nodes"][index][key] = "forged"
|
|
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
|
canonical(source)
|
|
).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_exact_build_and_qualification():
|
|
assert canonical(runpy.run_path(str(OUT / "build_definition.py"))["build"]()) == canonical(
|
|
BLOCK
|
|
)
|
|
assert len(DEFINITIONS) == 76 and doc()["fixed_version"]["matched"]
|
|
assert BLOCK["counts"]["saved_fields"] == 175 and BLOCK["counts"]["root_saved_fields"] == 175
|
|
assert BLOCK["counts"]["source_notes"] == 4 and len(BLOCK["comments"]) == 4
|
|
assert not doc()["status"]["doc_done"] and not doc()["status"]["exec_verified"]
|
|
sys.path.insert(0, str(OUT))
|
|
try:
|
|
runpy.run_path(str(OUT / "verify_sources.py"))["main"]()
|
|
finally:
|
|
sys.path.remove(str(OUT))
|
|
|
|
|
|
def test_path_and_source_binding_cannot_be_extended():
|
|
from calliope.comfyui.fixed_document import fixed_document_for
|
|
|
|
assert fixed_document_for(PATH + "forged", SOURCE, REVIEW) is None
|
|
review = copy.deepcopy(REVIEW)
|
|
review["source_sha"] = "forged"
|
|
assert not doc(SOURCE, review)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("suffix", [b"\n", b" ", b"\t"])
|
|
def test_original_byte_identity_rejects_equivalent_json(suffix):
|
|
altered = RAW + suffix
|
|
assert json.loads(altered) == SOURCE
|
|
assert review_for(PATH, hashlib.sha1(altered).hexdigest()) is None
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["user_overview", "source_sha256", "sources", "flow", "counts"])
|
|
def test_editorial_or_citation_rehash_cannot_replace_code_owned_pin(key):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
review["fixed_version_document"][key] = "forged"
|
|
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
|
canonical(source)
|
|
).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_static_templates_modes_objects_bad_and_branches():
|
|
ns = {n["id"]: n for n in SOURCE["nodes"]}
|
|
assert len(SOURCE["nodes"]) == 266 and len(SOURCE["links"]) == 332
|
|
assert sum(n["mode"] == 2 for n in ns.values()) == 38 and "definitions" not in SOURCE
|
|
templates = [n for n in ns.values() if n["type"] == "PrimitiveStringMultiline"]
|
|
assert len(templates) == 18 and [n["id"] for n in templates if n["mode"] == 0] == [125]
|
|
assert all("{$@}" in n["widgets_values"][0] for n in templates)
|
|
assert ns[392]["widgets_values"] == [{}, {"type": "PowerLoraLoaderHeaderWidget"}, {}, ""]
|
|
assert ns[566]["inputs"][0]["label"] == "BAD" and ns[566]["inputs"][0]["type"] == "*"
|
|
assert not ns[57]["widgets_values"][0].endswith("\n") and ns[385]["widgets_values"][1] == "Both"
|
|
assert ns[60]["widgets_values"] == [""] and ns[6]["widgets_values"] == [""]
|
|
assert ns[50]["inputs"][0]["link"] is None and ns[50]["inputs"][5]["link"] == 936
|
|
assert ns[563]["widgets_values"][3] == 20 and ns[563]["widgets_values"][7:9] == [18, 20]
|
|
assert ns[244]["widgets_values"] == [1088, 1600, 1] and ns[56]["widgets_values"] == [
|
|
944,
|
|
1408,
|
|
1,
|
|
]
|
|
assert [n["link"] for n in ns[244]["inputs"]] == [1410, 1411, 1420]
|
|
assert SOURCE["extra"]["frontendVersion"] == "1.35.9"
|
|
assert sum(n["type"] == "VAEDecode" for n in ns.values()) == 6
|
|
assert not any("widgets_values_named" in n for n in ns.values())
|
|
|
|
|
|
@pytest.mark.parametrize("note", BLOCK["comments"])
|
|
def test_full_note_original_and_forgery(note):
|
|
assert pointer(SOURCE, note["pointer"]) == note["text"]
|
|
r = copy.deepcopy(REVIEW)
|
|
r["fixed_version_document"]["comments"][BLOCK["comments"].index(note)]["text"] = "forged"
|
|
assert not doc(SOURCE, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("key", ["properties", "inputs", "outputs", "mode"])
|
|
@pytest.mark.parametrize("nid", [125, 524, 531, 37, 459, 392, 566, 96, 307, 244, 9])
|
|
def test_control_identity_and_state_forgery_fail_closed(nid, key):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
next(n for n in s["nodes"] if n["id"] == nid)[key] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_general_b_film_template_bf16_and_complete_prerequisites():
|
|
ns = {n["id"]: n for n in SOURCE["nodes"]}
|
|
assert hashlib.sha256(RAW).hexdigest() == (
|
|
"8d9e66744e0266c5f53a6396e030c38bb90053f2525ac9c3c6cc1b321006a976"
|
|
)
|
|
assert ns[125]["mode"] == 0 and ns[38]["mode"] == 2
|
|
film = ns[125]["widgets_values"][0]
|
|
assert "dark fantasy film" in film and "1980s" in film
|
|
assert all(title in film for title in ["Excalibur", "Legend", "Conan the Barbarian"])
|
|
assert "android phone" not in film
|
|
assert "stage and theater photographer" in ns[38]["widgets_values"][0]
|
|
assert "Dynamic Heroic Realism" in ns[165]["widgets_values"][0]
|
|
assert ns[524]["mode"] == 0 and ns[531]["mode"] == 2
|
|
assert ns[572]["widgets_values"] == [r"Z-Image\z_image_turbo_bf16.safetensors", "default"]
|
|
assert ns[9]["outputs"] == []
|
|
assert [i["name"] for i in ns[341]["inputs"]] == ["image", "seed", "strength"]
|
|
seed = next(f for f in BLOCK["fields"] if f["node_id"] == "341" and f["field"] == "seed")
|
|
assert seed["input_pointer"] == "/nodes/159/inputs/1"
|
|
assert pointer(SOURCE, "/nodes/258/inputs/0") == ns[566]["inputs"][0]
|
|
text = ns[57]["widgets_values"][0]
|
|
assert "steampunk workshop" in text and "mechanical spider" in text
|
|
assert text == ns[54]["widgets_values"][0] == ns[385]["widgets_values"][0]
|
|
assert not text.endswith("\n")
|
|
assert {n["node_id"]: len(n["text"]) for n in BLOCK["comments"]} == {
|
|
"111": 234,
|
|
"569": 209,
|
|
"570": 622,
|
|
"571": 1618,
|
|
}
|
|
prerequisite = next(n for n in BLOCK["comments"] if n["node_id"] == "571")["text"]
|
|
assert prerequisite.startswith("Amazing Z-Image(b) WF v4.0")
|
|
assert "## More Info ##" in prerequisite and "Created by Martin Rizzo" in prerequisite
|
|
assert BLOCK["counts"]["linked_saved_fields"] == 14
|
|
assert "仅125黑暗奇幻" in BLOCK["user_overview"]
|
|
assert "不能当当前模板或生成内容" in BLOCK["user_overview"]
|