216 lines
8.7 KiB
Python
216 lines
8.7 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-outpaint-document-evidence")
|
|
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/5_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(29))
|
|
@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(33))
|
|
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, 157, 161, 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) == 51
|
|
assert len(b["fields"]) == 28 and len(b["comments"]) == 3
|
|
assert sum(f["role"] == "linked_saved_cache" for f in b["fields"]) == 2
|
|
assert sum(f["role"] == "frontend_reference_only" for f in b["fields"]) == 2
|
|
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_outpaint_mask_dimensions_concat_and_versions_from_original():
|
|
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, 156, 0, "IMAGE"),
|
|
(156, 0, 153, 0, "IMAGE"),
|
|
(156, 1, 146, 2, "MASK"),
|
|
(153, 0, 146, 0, "IMAGE"),
|
|
(153, 0, 141, 0, "IMAGE"),
|
|
(153, 0, 157, 0, "IMAGE"),
|
|
(153, 0, 152, 0, "IMAGE"),
|
|
(152, 0, 160, 1, "IMAGE"),
|
|
(104, 0, 160, 0, "IMAGE"),
|
|
(160, 0, 161, 0, "IMAGE"),
|
|
(157, 0, 101, 1, "INT"),
|
|
(157, 1, 101, 2, "INT"),
|
|
(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"),
|
|
(104, 0, 9, 0, "IMAGE"),
|
|
]:
|
|
assert edge in edges
|
|
assert not ns[155]["outputs"][1]["links"]
|
|
assert ns[103]["outputs"][1]["links"] == []
|
|
assert not any(n["type"] == "EmptyFlux2LatentImage" for n in ns.values())
|
|
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"] == 315518168455707
|
|
assert fs["101", "steps"]["value"] == 20
|
|
assert fs["146", "grow_mask_by"]["value"] == 20
|
|
for side in ["left", "top", "right", "bottom"]:
|
|
assert fs["156", side]["value"] == 248
|
|
assert fs["156", "feathering"]["value"] == 25
|
|
assert fs["160", "direction"]["value"] == "right"
|
|
assert fs["160", "match_image_size"]["value"] is True
|
|
assert fs["155", "image"]["value"] == "Flux2-Klein-4b-base_00011_.png"
|
|
assert "小志" in fs["142", "lora_name"]["value"]
|
|
assert [c["node_id"] for c in BLOCK["comments"]] == ["97", "154", "159"]
|
|
assert BLOCK["comments"][2]["text"] == "# 增加上下左右扩展的尺寸"
|
|
assert all(s["id"] not in ["tt", "rgthree", "impact"] for s in BLOCK["sources"])
|
|
for nid in [155, 156, 157, 146, 142, 161]:
|
|
assert ns[nid]["properties"]["ver"] == "0.21.1"
|
|
|
|
|
|
def test_pinned_passive_pad_size_preview_concat_protocol():
|
|
ref = OUT.parent / "flux2-img-reference"
|
|
nodes = (ref / "lora__nodes.py").read_text()
|
|
assert "feathering * 2 < d2 and feathering * 2 < d3" in nodes
|
|
assert "t[i, j] = v * v" in nodes
|
|
assert "return (new_image, mask.unsqueeze(0))" in nodes
|
|
assert 'self.type = "temp"' in nodes and "class PreviewImage(SaveImage)" in nodes
|
|
assert '"noise_mask": (mask_erosion[:,:,:x,:y].round())' in nodes
|
|
size = (
|
|
OUT.parent / "flux2-outpaint-reference/outpaint__comfy_extras__nodes_images.py"
|
|
).read_text()
|
|
assert "return IO.NodeOutput(width, height, batch_size)" in size
|
|
kj = (ref / "kj__nodes__image_nodes.py").read_text()
|
|
assert "torch.cat((image1, image2_resized), dim=2)" in kj
|
|
assert "target_width = int(target_height * original_aspect_ratio)" in kj
|
|
assert "return concatenated_image," in kj
|
|
|
|
|
|
@pytest.mark.parametrize("node", [97, 154, 159, 155, 156, 157, 161, 142])
|
|
def test_notes_and_new_node_versions_are_bound(node):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
n = next(n for n in s["nodes"] if n["id"] == node)
|
|
if node in [97, 154, 159]:
|
|
n["widgets_values"][0] += "forged"
|
|
else:
|
|
n["properties"]["ver"] = "0.8.2"
|
|
refuse(s, r)
|