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

138 lines
5.5 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/flux2-action-document-evidence"
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/flux2_klein-图生图_人物动作迁移.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("i", [0])
@pytest.mark.parametrize("key", ["id", "inputs", "outputs", "widgets", "links"])
def test_definition_mutations_fail_closed(i, key):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
s["definitions"]["subgraphs"][i][key] = "forged"
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert not doc(s, r)["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) == 60 and doc()["fixed_version"]["matched"]
assert BLOCK["counts"]["saved_fields"] == 43 and BLOCK["counts"]["root_saved_fields"] == 26
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_action_chains_and_conflicts_remain_reference_only():
nodes = {n["id"]: n for n in SOURCE["nodes"]}
sub = SOURCE["definitions"]["subgraphs"][0]
inner = {n["id"]: n for n in sub["nodes"]}
assert nodes[27]["widgets_values"] == ["1080"]
assert nodes[27]["inputs"][0]["link"] is None
assert nodes[16]["inputs"][9]["link"] == 59
assert [nodes[12]["inputs"][i]["link"] for i in [0, 1]] == [58, 44]
assert nodes[11]["inputs"][0]["link"] == 47
assert nodes[19]["inputs"][0]["link"] == 42
assert nodes[19]["inputs"][1]["link"] == 48
assert inner[9]["inputs"][3]["link"] == 14
assert next(e for e in sub["links"] if e["id"] == 14)["origin_slot"] == 2
assert sub["outputs"][0]["linkIds"] == [17, 17]
assert sum(e["id"] == 17 for e in sub["links"]) == 1
assert len(nodes[23]["properties"]["proxyWidgets"]) == 10
assert len(nodes[23]["inputs"]) == 8
for nid in [6, 13]:
assert "links" not in nodes[nid]["outputs"][1]
assert "links" not in nodes[17]["outputs"][1]
assert len([f for f in BLOCK["fields"] if isinstance(f["value"], list)]) == 1
assert BLOCK["counts"]["history_objects"] == 1
assert "EmptyFlux2LatentImage" not in [n["type"] for n in sub["nodes"]]
text = "\n".join(BLOCK["flow"])
for required in [
"latent",
"2_image",
"Autogrow",
"image_0_mask",
"image_1_mask",
"RETURN_TYPES=()",
"a_images",
"scale_to_length",
"JSON字符串",
"不是第一图6",
"不是19颜色匹配后图",
"不保证身份",
]:
assert required in text
assert BLOCK["fields"][0]["class_type"] == "DWPreprocessor"
assert all(not n.get("widgets_values_named") for n in SOURCE["nodes"])
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
r = copy.deepcopy(REVIEW)
r["source_sha"] = "forged"
assert not doc(SOURCE, r)["fixed_version"]["matched"]