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

242 lines
10 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-daxiong-document-evidence")
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/3_flux2_klein_图生图一致性增强(大雄lora).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(25))
@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(30))
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, 146, 143, 136, 135])
@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) == 49
assert len(b["fields"]) == 27 and len(b["comments"]) == 1
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"] not in ["tt", "kj"] for s in BLOCK["sources"])
assert {s["tag"] for s in BLOCK["sources"] if s["id"] == "impact"} == {"8.28.3"}
def test_mask_intent_reference_conditions_empty_start_and_correct_size_slots():
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 e in [
(76, 0, 112, 0, "IMAGE"),
(112, 0, 141, 0, "IMAGE"),
(112, 0, 146, 0, "IMAGE"),
(146, 1, 136, 0, "INT"),
(146, 2, 135, 0, "INT"),
(136, 0, 101, 2, "INT"),
(136, 0, 111, 1, "INT"),
(135, 0, 101, 1, "INT"),
(135, 0, 111, 0, "INT"),
(141, 0, 140, 1, "LATENT"),
(141, 0, 139, 1, "LATENT"),
(111, 0, 103, 4, "LATENT"),
(104, 0, 145, 0, "IMAGE"),
(76, 0, 145, 1, "IMAGE"),
(106, 0, 142, 0, "MODEL"),
(142, 0, 102, 0, "MODEL"),
]:
assert e in edges
assert ns[76]["outputs"][1]["links"] is None
assert ns[103]["outputs"][1]["links"] == []
assert ns[146]["outputs"][0]["links"] is None
assert ns[146]["outputs"][3]["links"] is None
assert [o["name"] for o in ns[146]["outputs"]] == ["batch", "height", "width", "channel"]
assert all(n["mode"] == 0 for n in ns.values())
f = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
assert f["108", "text"]["value"] == "将遮罩中的物品换成葡萄"
assert "不能保证只改遮罩区域" in f["108", "text"]["effect"]
assert f["109", "text"]["value"] == ""
assert f["105", "noise_seed"]["value"] == 1057988721195140
assert f["101", "steps"]["value"] == 20
assert (
f["142", "lora_name"]["value"]
== "Flux\\Flux2-Klein-9B-consistency-V2_一致性大熊版.safetensors"
)
assert f["142", "strength_model"]["value"] == 0.8
assert "safe_load" in f["142", "lora_name"]["effect"]
history = f["145", "rgthree_comparer"]["value"]
assert [x["name"] for x in history] == ["A", "B"]
assert all(x["selected"] is True and "temp_uvrnh" in x["url"] for x in history)
assert f["76", "image"]["value"] == ns[76]["properties"]["image"]
def test_exact_core_schema_and_impact_registration_from_passive_text():
f = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
assert f["112", "megapixels"]["value"] == 1 and f["112", "megapixels"]["json_type"] == "int"
assert (
f["112", "resolution_steps"]["value"] == 1
and f["112", "resolution_steps"]["json_type"] == "int"
)
assert f["112", "upscale_method"]["value"] == "nearest-exact"
ref = OUT.parent / "flux2-daxiong-reference"
scale = (ref / "core__comfy_extras__nodes_post_processing.py").read_text()
assert ('io.Float.Input("megapixels", default=1.0, min=0.01, max=16.0, step=0.01)') in scale
assert 'io.Int.Input("resolution_steps", default=1, min=1, max=256)' in scale
assert "total = megapixels * 1024 * 1024" in scale
assert (
"width = round(samples.shape[3] * scale_by / resolution_steps) * resolution_steps"
) in scale
assert (
"height = round(samples.shape[2] * scale_by / resolution_steps) * resolution_steps"
) in scale
assert 'upscale_method, "disabled")' in scale
impact = (ref / "impact__modules__impact__logics.py").read_text()
assert 'RETURN_NAMES = ("batch", "height", "width", "channel")' in impact
assert ("return (value.shape[0], value.shape[1], value.shape[2], value.shape[3])") in impact
assert '"ImpactImageInfo": ImpactImageInfo' in (ref / "impact____init__.py").read_text()
assert "RETURN_TYPES = ()" in (OUT.parent / "flux2-img-reference/core__nodes.py").read_text()
empty = {x["node_id"]: x for x in BLOCK["zero_saved_nodes"]}
assert empty["146"]["state"] == "empty_list"
assert "槽1" in empty["136"]["effect"] and "槽2" in empty["135"]["effect"]
@pytest.mark.parametrize(
"variant",
["note", "history", "slide", "clipspace", "label", "lora", "info-slot", "scale-version"],
)
def test_complete_nested_state_and_new_provider_binding(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
ns = {n["id"]: n for n in s["nodes"]}
if variant == "note":
ns[97]["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 == "clipspace":
ns[76]["properties"]["image"] = "forged"
elif variant == "label":
ns[143]["properties"]["cnr_id"] = "rgthree-comfy"
elif variant == "lora":
ns[142]["mode"] = 4
elif variant == "info-slot":
ns[146]["outputs"][1]["name"] = "width"
else:
ns[112]["properties"]["ver"] = "0.21.1"
refuse(s, r)