242 lines
9.5 KiB
Python
242 lines
9.5 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-real-document-evidence")
|
|
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/7_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) == 53
|
|
assert len(b["fields"]) == 27 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]
|
|
ns = {n["id"]: n for n in SOURCE["nodes"]}
|
|
for c in b["comments"]:
|
|
assert c["text"] == ns[int(c["node_id"])]["widgets_values"][0]
|
|
assert ns[141]["widgets_values"][0].count("转年轻的亚洲男人写实风格") == 2
|
|
assert {s["id"] for s in b["sources"]} == {"core", "lora", "frontend", "rgthree"}
|
|
for key, tag in [
|
|
("core", "v0.8.2"),
|
|
("lora", "v0.15.1"),
|
|
("frontend", "v1.40.0"),
|
|
("rgthree", "1.0.2512112053"),
|
|
]:
|
|
assert {s["tag"] for s in b["sources"] if s["id"] == key} == {tag}
|
|
|
|
|
|
def test_original_reference_and_start_size_overrides_and_comparison_sides():
|
|
edges = {tuple(e[1:]) for e in SOURCE["links"]}
|
|
for e in [
|
|
(76, 0, 134, 0, "IMAGE"),
|
|
(134, 0, 136, 0, "IMAGE"),
|
|
(134, 0, 138, 0, "IMAGE"),
|
|
(136, 0, 123, 1, "INT"),
|
|
(136, 0, 133, 0, "INT"),
|
|
(136, 1, 123, 2, "INT"),
|
|
(136, 1, 133, 1, "INT"),
|
|
(138, 0, 139, 1, "LATENT"),
|
|
(138, 0, 137, 1, "LATENT"),
|
|
(133, 0, 125, 4, "LATENT"),
|
|
(126, 0, 143, 0, "IMAGE"),
|
|
(76, 0, 143, 1, "IMAGE"),
|
|
(128, 0, 140, 0, "MODEL"),
|
|
(140, 0, 124, 0, "MODEL"),
|
|
]:
|
|
assert e in edges
|
|
f = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
|
|
assert f["130", "text"]["value"] == "转年轻的亚洲少女写实风格"
|
|
assert f["131", "text"]["value"] == ""
|
|
assert f["127", "noise_seed"]["value"] == 1111269642602890
|
|
assert f["140", "strength_model"]["json_type"] == "int"
|
|
assert f["140", "lora_name"]["value"] == "Flux\\Klein9B_动漫转写实真人.safetensors"
|
|
assert f["9", "filename_prefix"]["value"] == "Flux2-Klein-4b-base"
|
|
history = f["143", "rgthree_comparer"]["value"]
|
|
assert [x["name"] for x in history] == ["A", "B"]
|
|
assert all(x["selected"] and "temp_kiegy" in x["url"] for x in history)
|
|
assert [f["134", k]["value"] for k in ("upscale_method", "megapixels", "resolution_steps")] == [
|
|
"nearest-exact",
|
|
1,
|
|
1,
|
|
]
|
|
|
|
|
|
def test_exact_passive_schema_reading_and_protocol_discrepancies():
|
|
ref = OUT.parent / "flux2-real-reference"
|
|
size = (ref / "core__comfy_extras__nodes_images.py").read_text()
|
|
assert "return IO.NodeOutput(width, height, batch_size)" in size
|
|
assert "width = image.shape[2]" in size and "height = image.shape[1]" in size
|
|
assert "GetImageSize," in size
|
|
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
|
|
lora = (ref / "lora__nodes.py").read_text()
|
|
assert "class LoraLoaderModelOnly(LoraLoader):" in lora
|
|
assert "if strength_model == 0 and strength_clip == 0:" in lora
|
|
assert "self.loaded_lora[0] == lora_path" in lora
|
|
assert "self.load_lora(model, None, lora_name, strength_model, 0)[0]" in lora
|
|
assert "safe_load=True" in lora
|
|
compare = (ref / "rgthree__py__image_comparer.py").read_text()
|
|
assert "class RgthreeImageComparer(PreviewImage):" in compare
|
|
assert "result['ui']['a_images']" in compare and "result['ui']['b_images']" in compare
|
|
js = (ref / "rgthree__web__comfyui__image_comparer.js").read_text()
|
|
assert "Array.isArray(v)" in js and "return { images: v };" in js
|
|
assert "delete d.img;" in js and "sel.img.src = sel.url;" in js
|
|
assert all("冲突保留" in f["effect"] for f in BLOCK["fields"] if f["node_id"] in ["9", "143"])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"variant",
|
|
[
|
|
"note97",
|
|
"note141",
|
|
"history",
|
|
"slide",
|
|
"lora",
|
|
"width-slot",
|
|
"scale-version",
|
|
"comparer-version",
|
|
],
|
|
)
|
|
def test_nested_text_and_independent_versions_are_bound(variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
ns = {n["id"]: n for n in s["nodes"]}
|
|
if variant.startswith("note"):
|
|
ns[int(variant[4:])]["widgets_values"][0] += "forged"
|
|
elif variant == "history":
|
|
ns[143]["widgets_values"][0][1]["url"] = "forged"
|
|
elif variant == "slide":
|
|
ns[143]["properties"]["comparer_mode"] = "Click"
|
|
elif variant == "lora":
|
|
ns[140]["properties"]["ver"] = "0.15.0"
|
|
elif variant == "width-slot":
|
|
ns[136]["outputs"][0]["name"] = "height"
|
|
elif variant == "scale-version":
|
|
ns[134]["properties"]["ver"] = "0.21.1"
|
|
else:
|
|
ns[143]["properties"]["ver"] = "1.0.2604070017"
|
|
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(23))
|
|
@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(28))
|
|
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", [125, 136, 138, 137, 139, 126])
|
|
@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"] == 125)["outputs"][1]["links"] = [999]
|
|
assert not doc(s, r)["fixed_version"]["matched"]
|