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

158 lines
6.0 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-dual-document-evidence"
PATH = "example_ComfyUI_workflows/3-图像生成/Z-Image/2_Z-Image+Base+Turbo+双重采样工作流.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 "无widgets_values_named" 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, 12, 15])
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) == 62 and doc()["fixed_version"]["matched"]
assert BLOCK["counts"]["saved_fields"] == BLOCK["counts"]["root_saved_fields"] == 42
assert BLOCK["counts"]["source_notes"] == 0 and BLOCK["comments"] == []
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_dual_overrides_and_types():
ns = {n["id"]: n for n in SOURCE["nodes"]}
assert ns[17]["widgets_values"] == ["12"] and ns[18]["widgets_values"] == ["3"]
assert ns[43]["widgets_values"] == [-1, "", "", ""]
assert ns[12]["widgets_values"] == [
"enable",
664841621470817,
"randomize",
3,
3,
"euler",
"normal",
0,
10000,
"enable",
]
assert ns[13]["widgets_values"] == [
"disable",
547894734911479,
"randomize",
3,
1,
"euler",
"sgm_uniform",
0,
10000,
"disable",
]
assert BLOCK["counts"]["linked_saved_fields"] == 6 and BLOCK["counts"]["frontend_only"] == 5
assert ns[10]["widgets_values"] == [1080, 1920, 1]
assert all("link" not in x for x in ns[10]["inputs"])
assert SOURCE["extra"]["frontendVersion"] == "1.37.11"
assert ns[35]["widgets_values"] == ["%date:yyyyMMdd_hhmmss%_Z-Image"]
assert "links" not in ns[35]["outputs"][0]
for nid in [5, 7]:
f = next(f for f in BLOCK["fields"] if f["node_id"] == str(nid))
assert f["value"] == ns[nid]["widgets_values"][0]
links = {x[0]: x[1:5] for x in SOURCE["links"]}
assert links[23] == [17, 0, 12, 6] and links[71] == [17, 0, 13, 6]
assert links[25] == [18, 0, 12, 11] and links[69] == [18, 0, 13, 10]
assert links[59] == [43, 0, 12, 5] and links[70] == [43, 0, 13, 5]
@pytest.mark.parametrize("variant", ["path", "bytes", "definition", "review_sha"])
def test_path_bytes_and_owned_digest_fail_closed(variant):
from calliope.comfyui.fixed_document import fixed_document_for
review = copy.deepcopy(REVIEW)
if variant == "path":
assert fixed_document_for(PATH + ".forged", SOURCE, review) is None
elif variant == "bytes":
assert review_for(PATH, hashlib.sha1(RAW + b" ").hexdigest()) is None
else:
if variant == "definition":
review["fixed_version_document"]["user_overview"] = "forged"
else:
review["source_sha"] = "0" * 40
assert not fixed_document_for(PATH, SOURCE, review)["matched"]