251 lines
9.6 KiB
Python
251 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-dual-document-evidence")
|
|
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/8_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"]
|
|
|
|
|
|
def test_exact_definition_source_notes_and_unchanged_qualification():
|
|
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
|
|
assert canonical(b) == canonical(BLOCK)
|
|
assert len(DEFINITIONS) == 54
|
|
assert len(b["fields"]) == 29 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]
|
|
ns = {n["id"]: n for n in SOURCE["nodes"]}
|
|
assert b["comments"][0]["text"] == ns[97]["widgets_values"][0]
|
|
assert len(b["comments"][0]["text"]) == 1407
|
|
assert {v["id"] for v in b["sources"]} == {"core", "negative", "frontend"}
|
|
for key, tag in [("core", "v0.8.2"), ("negative", "v0.9.1"), ("frontend", "v1.40.0")]:
|
|
assert {v["tag"] for v in b["sources"] if v["id"] == key} == {tag}
|
|
|
|
|
|
def test_dual_scaling_ordered_positive_negative_and_empty_start():
|
|
edges = {tuple(e[1:]) for e in SOURCE["links"]}
|
|
for e in [
|
|
(76, 0, 130, 0, "IMAGE"),
|
|
(81, 0, 131, 0, "IMAGE"),
|
|
(130, 0, 128, 0, "IMAGE"),
|
|
(130, 0, 140, 0, "IMAGE"),
|
|
(131, 0, 143, 0, "IMAGE"),
|
|
(128, 0, 138, 1, "INT"),
|
|
(128, 0, 129, 0, "INT"),
|
|
(128, 1, 138, 2, "INT"),
|
|
(128, 1, 129, 1, "INT"),
|
|
(135, 0, 141, 0, "CONDITIONING"),
|
|
(136, 0, 139, 0, "CONDITIONING"),
|
|
(140, 0, 141, 1, "LATENT"),
|
|
(140, 0, 139, 1, "LATENT"),
|
|
(141, 0, 144, 0, "CONDITIONING"),
|
|
(139, 0, 142, 0, "CONDITIONING"),
|
|
(143, 0, 144, 1, "LATENT"),
|
|
(143, 0, 142, 1, "LATENT"),
|
|
(144, 0, 137, 1, "CONDITIONING"),
|
|
(142, 0, 137, 2, "CONDITIONING"),
|
|
(126, 0, 137, 0, "MODEL"),
|
|
(129, 0, 123, 4, "LATENT"),
|
|
(123, 0, 124, 0, "LATENT"),
|
|
(124, 0, 94, 0, "IMAGE"),
|
|
]:
|
|
assert e in edges
|
|
f = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
|
|
assert f["136", "text"]["value"] == ""
|
|
assert (
|
|
f["135", "text"]["value"]
|
|
== next(n for n in SOURCE["nodes"] if n["id"] == 135)["widgets_values"][0]
|
|
)
|
|
assert f["125", "noise_seed"]["value"] == 1062632423628472
|
|
assert f["137", "cfg"]["json_type"] == "int"
|
|
assert f["76", "image"]["value"] == "Flux2-Klein-4b-base_00010_.png"
|
|
assert f["81", "image"]["value"] == "comfy_logo_blue.png"
|
|
for node in ["130", "131"]:
|
|
assert [
|
|
f[node, k]["value"] for k in ("upscale_method", "megapixels", "resolution_steps")
|
|
] == ["nearest-exact", 1, 1]
|
|
assert "LoRA" not in {n["type"] for n in SOURCE["nodes"]}
|
|
ns = {n["id"]: n for n in SOURCE["nodes"]}
|
|
assert ns[123]["outputs"][1]["links"] == ns[128]["outputs"][2]["links"] == []
|
|
assert ns[94]["outputs"] == []
|
|
assert ns[76]["outputs"][1]["links"] is None and ns[81]["outputs"][1]["links"] is None
|
|
|
|
|
|
def test_passive_exact_implementation_reading():
|
|
sources = BLOCK["sources"]
|
|
|
|
def read(key, file):
|
|
e = next(e for e in sources if e["id"] == key and e["file"] == file)
|
|
return (OUT / e["local_file"]).read_text()
|
|
|
|
assert "return IO.NodeOutput(width, height, batch_size)" in read(
|
|
"core", "comfy_extras/nodes_images.py"
|
|
)
|
|
scale = read("core", "comfy_extras/nodes_post_processing.py")
|
|
assert 'io.Int.Input("resolution_steps", default=1, min=1, max=256)' in scale
|
|
assert ('io.Float.Input("megapixels", default=1.0, min=0.01, max=16.0, step=0.01)') in scale
|
|
reference = read("core", "comfy_extras/nodes_edit_model.py")
|
|
assert "if latent is not None:" in reference and "append=True" in reference
|
|
helper = read("core", "node_helpers.py")
|
|
assert "if old_val is not None:" in helper and "val = old_val + val" in helper
|
|
negative = read("negative", "nodes.py")
|
|
assert '"CLIPTextEncode": CLIPTextEncode' in negative
|
|
assert (
|
|
"tokens = clip.tokenize(text)" in negative
|
|
and "clip.encode_from_tokens_scheduled(tokens)" in negative
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"variant",
|
|
[
|
|
"note",
|
|
"positive",
|
|
"negative",
|
|
"negative-version",
|
|
"width-slot",
|
|
"second-reference",
|
|
"second-scale",
|
|
"save-output",
|
|
],
|
|
)
|
|
def test_specific_dual_boundaries_refuse_caller_rehash(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 == "positive":
|
|
ns[135]["widgets_values"][0] += "forged"
|
|
elif variant == "negative":
|
|
ns[136]["widgets_values"][0] = "forged"
|
|
elif variant == "negative-version":
|
|
ns[136]["properties"]["ver"] = "0.8.2"
|
|
elif variant == "width-slot":
|
|
ns[128]["outputs"][0]["name"] = "height"
|
|
elif variant == "second-reference":
|
|
ns[144]["inputs"][1]["link"] = 220
|
|
elif variant == "second-scale":
|
|
ns[131]["widgets_values"][1] = 2
|
|
else:
|
|
ns[94]["outputs"] = [{"name": "IMAGE", "type": "IMAGE", "links": []}]
|
|
refuse(s, r)
|
|
|
|
|
|
@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(32))
|
|
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", [123, 144, 142, 140, 139, 143, 128, 124, 141])
|
|
@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"] == 123)["outputs"][1]["links"] = [999]
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|