244 lines
9.6 KiB
Python
244 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-inpaint-document-evidence")
|
|
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/4_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)
|
|
|
|
|
|
@pytest.mark.parametrize("index", range(27))
|
|
@pytest.mark.parametrize("variant", ["mode", "identity", "named"])
|
|
def test_all_nodes_bound(index, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
n = s["nodes"][index]
|
|
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("index", range(34))
|
|
def test_all_edges_bound(index):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
s["links"][index][3] = 9999
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize("node", [103, 141, 140, 139, 104, 143, 151, 152, 153])
|
|
@pytest.mark.parametrize("variant", ["missing", "empty", "null", "added"])
|
|
def test_missing_empty_null_distinct(node, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
n = next(n for n in s["nodes"] if n["id"] == node)
|
|
old = copy.deepcopy(n)
|
|
if variant == "missing":
|
|
n.pop("widgets_values", None)
|
|
else:
|
|
n["widgets_values"] = {"empty": [], "null": None, "added": [0]}[variant]
|
|
if n != old:
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"variant",
|
|
["overview", "citation", "sha", "frontend", "counts", "tab", "ue", "unconnected-output"],
|
|
)
|
|
def test_metadata_and_unconnected_branch_bound(variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
b = r["fixed_version_document"]
|
|
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.52.7"
|
|
elif variant == "counts":
|
|
b["counts"]["final_prompt_constructed"] = True
|
|
elif variant == "tab":
|
|
s["nodes"][0]["properties"]["tabWidth"] = 99
|
|
elif variant == "ue":
|
|
s["nodes"][0]["properties"]["ue_properties"]["version"] = "7.8"
|
|
else:
|
|
next(n for n in s["nodes"] if n["id"] == 103)["outputs"][1]["links"] = [999]
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_exact_document_and_unchanged_execution_status():
|
|
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
|
|
assert canonical(b) == canonical(BLOCK)
|
|
assert len(DEFINITIONS) == 50
|
|
assert len(b["fields"]) == 34 and len(b["comments"]) == 2
|
|
assert sum(f["role"] == "linked_saved_cache" for f in b["fields"]) == 4
|
|
assert sum(f["role"] == "frontend_reference_only" for f in b["fields"]) == 3
|
|
d = doc()
|
|
assert d["fixed_version"]["matched"]
|
|
assert not d["status"]["doc_done"] and not d["status"]["exec_verified"]
|
|
assert hashlib.sha256(canonical(BLOCK)).hexdigest() == DEFINITIONS[PATH]
|
|
assert BLOCK["comments"][0]["text"] == SOURCE["nodes"][0]["widgets_values"][0]
|
|
assert len(BLOCK["comments"][0]["text"]) == 1396
|
|
assert all(s["id"] != "impact" for s in BLOCK["sources"])
|
|
assert {s["tag"] for s in BLOCK["sources"] if s["id"] == "lora"} == {"v0.21.1"}
|
|
|
|
|
|
def test_mask_initial_latent_reference_conditions_comparer_and_vae_fanout():
|
|
ns = {n["id"]: n for n in SOURCE["nodes"]}
|
|
edges = {(a, b, c, d, e) for _, a, b, c, d, e in SOURCE["links"]}
|
|
for edge in [
|
|
(155, 0, 150, 0, "IMAGE"),
|
|
(155, 1, 150, 1, "MASK"),
|
|
(150, 3, 146, 2, "MASK"),
|
|
(150, 0, 153, 0, "IMAGE"),
|
|
(153, 0, 146, 0, "IMAGE"),
|
|
(153, 0, 152, 0, "IMAGE"),
|
|
(152, 0, 145, 1, "IMAGE"),
|
|
(150, 0, 141, 0, "IMAGE"),
|
|
(146, 0, 103, 4, "LATENT"),
|
|
(110, 0, 151, 0, "VAE"),
|
|
(151, 0, 141, 1, "VAE"),
|
|
(151, 0, 146, 1, "VAE"),
|
|
(151, 0, 104, 1, "VAE"),
|
|
(141, 0, 140, 1, "LATENT"),
|
|
(141, 0, 139, 1, "LATENT"),
|
|
(150, 1, 101, 1, "INT"),
|
|
(150, 2, 101, 2, "INT"),
|
|
(104, 0, 145, 0, "IMAGE"),
|
|
]:
|
|
assert edge in edges
|
|
assert not any(n["type"] == "EmptyFlux2LatentImage" for n in ns.values())
|
|
assert ns[103]["outputs"][1]["links"] == []
|
|
fs = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
|
|
assert fs["108", "text"]["value"] == "将遮罩中的物品换成紫色葡萄"
|
|
assert fs["109", "text"]["value"] == ""
|
|
assert fs["105", "noise_seed"]["value"] == 775361803314234
|
|
assert fs["101", "steps"]["value"] == 12
|
|
assert fs["146", "grow_mask_by"]["value"] == 6
|
|
assert fs["155", "image"]["value"] == "ComfyUI_00150_.png"
|
|
assert fs["150", "divisible_by"]["value"] == 2
|
|
assert fs["150", "upscale_method"]["value"] == "nearest-exact"
|
|
assert fs["150", "keep_proportion"]["value"] == "resize"
|
|
assert fs["144", "resolution"]["value"] == "720x1280 (9:16) (竖屏)"
|
|
assert "遮罩外像素严格不变" in BLOCK["user_overview"]
|
|
assert BLOCK["comments"][1]["text"] == "# 右键遮罩编辑添加遮罩"
|
|
assert [c["node_id"] for c in BLOCK["comments"]] == ["97", "154"]
|
|
assert [x["name"] for x in fs["145", "rgthree_comparer"]["value"]] == ["A", "B"]
|
|
assert all("temp_uvrnh" in x["url"] for x in fs["145", "rgthree_comparer"]["value"])
|
|
|
|
|
|
def test_exact_version_passive_inpaint_load_mask_and_resize_semantics():
|
|
ref = OUT.parent / "flux2-img-reference"
|
|
nodes = (ref / "lora__nodes.py").read_text()
|
|
assert "downscale_ratio = vae.spacial_compression_encode()" in nodes
|
|
assert (
|
|
"mask_erosion = torch.clamp(torch"
|
|
".nn.functional.conv2d(mask.round"
|
|
"(), kernel_tensor, padding=paddi"
|
|
"ng), 0, 1)"
|
|
) in nodes
|
|
assert '"noise_mask": (mask_erosion[:,:,:x,:y].round())' in nodes
|
|
assert "components.alpha[..., -1]" in nodes
|
|
assert "torch.zeros((components.images.shape[0], 64, 64)" in nodes
|
|
kj = (ref / "kj__nodes__image_nodes.py").read_text()
|
|
assert "mask.shape[-2:] == (64, 64) and (H != 64 or W != 64)" in kj
|
|
assert 'RETURN_NAMES = ("IMAGE", "width", "height", "mask",)' in kj
|
|
assert 'mask.unsqueeze(1), W, H, "bilinear"' in kj
|
|
assert "out_mask.unsqueeze(1), width, height, upscale_method" in kj
|
|
sampler = (ref / "core__comfy_extras__nodes_custom_sampler.py").read_text()
|
|
assert "denoise_mask=noise_mask" in sampler
|
|
assert "RETURN_TYPES = ()" in (ref / "core__nodes.py").read_text()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"variant",
|
|
[
|
|
"note",
|
|
"mask-note",
|
|
"history",
|
|
"slide",
|
|
"image",
|
|
"label",
|
|
"lora",
|
|
"mask-slot",
|
|
"inpaint-version",
|
|
"load-version",
|
|
],
|
|
)
|
|
def test_nested_state_and_distinct_provider_versions_fail_closed(variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
ns = {n["id"]: n for n in s["nodes"]}
|
|
if variant in ["note", "mask-note"]:
|
|
ns[97 if variant == "note" else 154]["widgets_values"][0] += "forged"
|
|
elif variant == "history":
|
|
ns[145]["widgets_values"][0][1]["url"] = "forged"
|
|
elif variant == "slide":
|
|
ns[145]["properties"]["comparer_mode"] = "Click"
|
|
elif variant == "image":
|
|
ns[155]["properties"]["image"] = "forged"
|
|
elif variant == "label":
|
|
ns[143]["properties"]["cnr_id"] = "rgthree-comfy"
|
|
elif variant == "lora":
|
|
ns[142]["mode"] = 4
|
|
elif variant == "mask-slot":
|
|
ns[150]["outputs"][3]["type"] = "IMAGE"
|
|
else:
|
|
ns[146 if variant == "inpaint-version" else 155]["properties"]["ver"] = "0.8.2"
|
|
refuse(s, r)
|