Files
aigc-home/calliope-backend/tests/test_flux2_clothing_fixed_document.py
T
2026-10-31 07:45:54 +08:00

271 lines
9.6 KiB
Python

"Flux2 editorial source binding, distinct saved states and unchanged eligibility."
import copy
import hashlib
import json
import runpy
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-clothing-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 part in p.strip("/").split("/"):
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
return obj
def doc(s=SOURCE, r=REVIEW):
return build_document(PATH, s, None, None, [], r)
def refuse(s, r):
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert not doc(s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
def test_every_saved_position_json_type_and_exact_citations(f):
assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"])
assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"]
assert "named_value" not in f and "无widgets_values_named" in f["named_pointer"]
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", ["position", "editorial"])
def test_caller_rehash_cannot_expand_trust(f, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
if variant == "editorial":
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged"
else:
parent, _, key = f["pointer"].rpartition("/")
pointer(s, parent)[int(key)] = "forged"
refuse(s, r)
def test_exact_source_and_unchanged_qualification():
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
assert canonical(b) == canonical(BLOCK)
assert len(DEFINITIONS) == 57 and doc()["fixed_version"]["matched"]
assert b["counts"]["saved_fields"] == 46 and b["counts"]["source_notes"] == 3
assert b["counts"]["root_saved_fields"] == 6 and b["counts"]["internal_saved_fields"] == 40
assert not doc()["status"]["doc_done"] and not doc()["status"]["exec_verified"]
assert [len(n["text"]) for n in b["comments"]] == [1407, 4, 4]
assert {s["id"] for s in b["sources"]} == {
"core",
"negative",
"lora",
"frontend",
"rgthree",
"layer",
}
assert {s["tag"] for s in b["sources"] if s["id"] == "frontend"} == {"v1.40.0"}
assert {s["tag"] for s in b["sources"] if s["id"] == "negative"} == {"v0.9.1"}
assert "准确1.36.13" not in canonical(b).decode()
def test_inventory_citations_and_bidirectional_endpoints():
import sys
sys.path.insert(0, str(OUT))
try:
runpy.run_path(str(OUT / "verify_sources.py"))["main"]()
finally:
sys.path.remove(str(OUT))
def test_two_references_canvas_and_comparison_meaning():
sub = SOURCE["definitions"]["subgraphs"][0]
ns = {n["id"]: n for n in sub["nodes"]}
root = {n["id"]: n for n in SOURCE["nodes"]}
assert root[167]["widgets_values"] == sub["widgets"] == []
assert len(root[167]["properties"]["proxyWidgets"]) == 10 and len(root[167]["inputs"]) == 8
assert root[167]["inputs"][7]["link"] is None
assert ["172", "scale_to_length"] in root[167]["properties"]["proxyWidgets"]
assert ["171", "scale_to_length"] not in root[167]["properties"]["proxyWidgets"]
assert (
ns[171]["widgets_values"]
== ns[172]["widgets_values"]
== ["original", 1, 1, "crop", "lanczos", "8", "longest", 1920, "#000000"]
)
assert ns[127]["widgets_values"] == ["full_encoder_small_decoder.safetensors"]
assert ns[164]["widgets_values"] == ["Flux\\Klien9B_bs_vton_服装迁移.safetensors", 1]
assert ns[164]["mode"] == 0 and ns[173]["mode"] == 4
assert ns[125]["widgets_values"] == [1, "fixed"]
assert ns[136]["properties"]["ver"] == "0.9.1" and ns[135]["properties"]["ver"] == "0.8.2"
assert sub["outputs"][0]["linkIds"] == [200, 200]
assert sum(e["id"] == 200 for e in sub["links"]) == 1
for a, ao, z, zi in [
(76, 0, 167, 0),
(81, 0, 167, 1),
(81, 0, 166, 1),
(167, 0, 166, 0),
(167, 0, 94, 0),
]:
assert any(e[1:5] == [a, ao, z, zi] for e in SOURCE["links"])
for a, ao, z, zi in [
(-10, 0, 172, 0),
(-10, 1, 171, 0),
(-10, 7, 172, 9),
(-10, 7, 171, 9),
(172, 0, 140, 0),
(172, 0, 128, 0),
(171, 0, 143, 0),
(128, 0, 138, 1),
(128, 1, 138, 2),
(128, 0, 129, 0),
(128, 1, 129, 1),
(135, 0, 141, 0),
(136, 0, 139, 0),
(140, 0, 141, 1),
(140, 0, 139, 1),
(141, 0, 144, 0),
(139, 0, 142, 0),
(143, 0, 144, 1),
(143, 0, 142, 1),
(144, 0, 137, 1),
(142, 0, 137, 2),
(129, 0, 123, 4),
(123, 0, 124, 0),
(124, 0, -20, 0),
(126, 0, 173, 0),
(173, 0, 164, 0),
(164, 0, 137, 0),
(-10, 3, 164, 1),
]:
assert any(
(e["origin_id"], e["origin_slot"], e["target_id"], e["target_slot"]) == (a, ao, z, zi)
for e in sub["links"]
)
f = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
assert "第二张原图" in f["166", "rgthree_comparer"]["effect"]
assert "不控制输出画布" in f["167:171", "scale_to_length"]["effect"]
assert f["167:136", "text"]["unit"] == "负向编码文本"
assert f["167:135", "text"]["unit"] == "正向编码文本"
assert f["167:164", "strength_model"]["json_type"] == "int"
NODE_POINTERS = [f"/nodes/{i}" for i in range(9)] + [
f"/definitions/subgraphs/0/nodes/{i}" for i in range(23)
]
EDGE_POINTERS = [f"/links/{i}" for i in range(5)] + [
f"/definitions/subgraphs/0/links/{i}" for i in range(41)
]
@pytest.mark.parametrize("p", NODE_POINTERS)
@pytest.mark.parametrize("variant", ["mode", "identity", "named"])
def test_all_root_and_internal_nodes_bound(p, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = pointer(s, p)
if variant == "mode":
n["mode"] = 0 if n["mode"] == 4 else 4
elif variant == "identity":
n["properties"]["ver"] = "forged"
else:
n["widgets_values_named"] = {"forged": 0}
refuse(s, r)
@pytest.mark.parametrize("p", EDGE_POINTERS)
def test_every_edge_bound_without_repair(p):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
e = pointer(s, p)
if isinstance(e, list):
e[3] = 999
else:
e["target_id"] = 999
refuse(s, r)
@pytest.mark.parametrize("z", BLOCK["zero_saved_nodes"], ids=lambda z: z["node_id"])
@pytest.mark.parametrize("variant", ["missing", "empty", "null", "added"])
def test_absent_empty_null_stay_distinct(z, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = pointer(s, z["pointer"])
before = copy.deepcopy(n)
if variant == "missing":
n.pop("widgets_values", None)
else:
n["widgets_values"] = {"empty": [], "null": None, "added": [0]}[variant]
if n != before:
refuse(s, r)
@pytest.mark.parametrize(
"variant",
[
"overview",
"citation",
"sha",
"frontend",
"count",
"proxy",
"duplicate",
"boundary_name",
"boundary_type",
"boundary_label",
"definition_widgets",
"note",
"unconnected_output",
"history",
"lora_identity",
"host_identity",
],
)
def test_all_metadata_and_boundary_declarations_bound(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = r["fixed_version_document"]
sub = s["definitions"]["subgraphs"][0]
if variant == "overview":
b["user_overview"] = "forged"
elif variant == "citation":
b["sources"][0]["sha256"] = "forged"
elif variant == "sha":
r["source_sha"] = "forged"
elif variant == "frontend":
s["extra"]["frontendVersion"] = "1.36.14"
elif variant == "count":
b["counts"]["final_prompt_constructed"] = True
elif variant == "proxy":
s["nodes"][6]["properties"]["proxyWidgets"][0][0] = "999"
elif variant == "duplicate":
sub["outputs"][0]["linkIds"] = [200]
elif variant == "boundary_name":
sub["inputs"][7]["name"] = "width"
elif variant == "boundary_type":
sub["inputs"][7]["type"] = "FLOAT"
elif variant == "boundary_label":
sub["inputs"][7]["label"] = "forged"
elif variant == "definition_widgets":
sub["widgets"] = None
elif variant == "note":
s["nodes"][0]["widgets_values"][0] = "forged"
elif variant == "unconnected_output":
next(n for n in sub["nodes"] if n["id"] == 123)["outputs"][1]["links"] = [999]
elif variant == "history":
s["nodes"][5]["widgets_values"][0][0]["url"] = "forged"
elif variant == "lora_identity":
next(n for n in sub["nodes"] if n["id"] == 164)["properties"]["ver"] = "0.8.2"
else:
s["nodes"][6]["properties"]["ver"] = "0.8.2"
refuse(s, r)