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

151 lines
5.7 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-person-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, 1])
@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) == 59 and doc()["fixed_version"]["matched"]
assert BLOCK["counts"]["saved_fields"] == 39 and BLOCK["counts"]["root_saved_fields"] == 14
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_graph_specific_chains_and_protocol_conflict():
ns = {n["id"]: n for n in SOURCE["nodes"]}
a, b = SOURCE["definitions"]["subgraphs"]
inner = {n["id"]: n for n in a["nodes"]}
assert ns[52]["widgets_values"] == [
"original",
1,
1,
"letterbox",
"lanczos",
"8",
"height",
1280,
"#000000",
]
assert "links" not in ns[69]["outputs"][1] and "links" not in ns[70]["outputs"][1]
assert ns[52]["outputs"][3]["links"] == ns[52]["outputs"][4]["links"] == []
assert ns[43]["outputs"] == [{"localized_name": "images", "name": "images", "type": "IMAGE"}]
assert inner[107]["widgets_values"] == ["full_encoder_small_decoder.safetensors"]
assert inner[87]["properties"]["ver"] == "0.9.1" and inner[87]["widgets_values"] == [""]
assert inner[113]["widgets_values"] == [
"把图2的人物替换图1的人物,只替换人物及服装,同时保持图1场景、动作不变,场景光效一致"
]
assert inner[110]["widgets_values"] == inner[85]["widgets_values"] == ["lanczos", 1, 1]
assert inner[103]["outputs"][1]["links"] == []
assert ns[132]["outputs"][1]["links"] == []
assert (
len(ns[131]["properties"]["proxyWidgets"]) == 8
and len(ns[132]["properties"]["proxyWidgets"]) == 1
)
assert len(ns[131]["inputs"]) == 6 and len(ns[132]["inputs"]) == 2
edges = {x["id"]: x for x in a["links"]}
for eid, src, dst in [
(171, 108, 115),
(172, 108, 109),
(173, 108, 115),
(174, 108, 109),
(200, 126, 127),
(199, 126, 125),
(209, 127, 130),
(210, 125, 128),
(208, 129, 130),
(207, 129, 128),
]:
assert (edges[eid]["origin_id"], edges[eid]["target_id"]) == (src, dst)
assert [n["type"] for n in b["nodes"]] == [
"RemoveBackground",
"LoadBackgroundRemovalModel",
"InvertMask",
"JoinImageWithAlpha",
]
flow = "\n".join(BLOCK["flow"])
for term in [
"RETURN_TYPES=()",
"第一再第二",
"四尺寸覆盖",
"1.0-",
"不控制输出画布",
"无LoRA/Comparer/历史列表",
"881bc917",
"foreground",
]:
assert term in flow