293 lines
11 KiB
Python
293 lines
11 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-restore-document-evidence")
|
|
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/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)
|
|
|
|
|
|
def test_exact_source_and_unchanged_qualification():
|
|
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
|
|
assert canonical(b) == canonical(BLOCK)
|
|
assert len(DEFINITIONS) == 56 and doc()["fixed_version"]["matched"]
|
|
assert len(b["fields"]) == 34 and len(b["comments"]) == 1
|
|
assert b["counts"]["root_saved_fields"] == 14 and b["counts"]["internal_saved_fields"] == 20
|
|
assert b["counts"]["linked_saved_fields"] == 10 and b["counts"]["frontend_only"] == 3
|
|
assert not doc()["status"]["doc_done"] and not doc()["status"]["exec_verified"]
|
|
assert len(b["comments"][0]["text"]) == 1396
|
|
assert b["comments"][0]["text"] == SOURCE["nodes"][0]["widgets_values"][0]
|
|
assert {s["id"] for s in b["sources"]} == {
|
|
"core",
|
|
"lora",
|
|
"primitive",
|
|
"frontend",
|
|
"rgthree",
|
|
"layer",
|
|
}
|
|
assert {s["tag"] for s in b["sources"] if s["id"] == "frontend"} == {"v1.36.13"}
|
|
assert {s["commit"] for s in b["sources"] if s["id"] == "layer"} == {
|
|
"89014d833d2ad0998d724090eff1456692692a1d"
|
|
}
|
|
assert {s["tag"] for s in b["sources"] if s["id"] == "primitive"} == {"v0.28.3"}
|
|
|
|
|
|
def test_independent_inventory_and_all_endpoint_checks():
|
|
import sys
|
|
|
|
sys.path.insert(0, str(OUT))
|
|
try:
|
|
runpy.run_path(str(OUT / "verify_sources.py"))["main"]()
|
|
finally:
|
|
sys.path.remove(str(OUT))
|
|
|
|
|
|
def test_saved_fields_boundaries_and_comparison_are_not_invented():
|
|
root = {n["id"]: n for n in SOURCE["nodes"]}
|
|
sub = SOURCE["definitions"]["subgraphs"][0]
|
|
ns = {n["id"]: n for n in sub["nodes"]}
|
|
assert root[150]["widgets_values"] == sub["widgets"] == []
|
|
assert len(root[150]["properties"]["proxyWidgets"]) == 10
|
|
assert len(root[150]["inputs"]) == len(sub["inputs"]) == 9
|
|
assert [(i["name"], i["label"], i["type"]) for i in sub["inputs"][-2:]] == [
|
|
("", "width", "*"),
|
|
("_1", "height", "*"),
|
|
]
|
|
assert root[152]["widgets_values"] == ns[108]["widgets_values"]
|
|
assert root[150]["inputs"][6]["name"] == "text_1" and root[150]["inputs"][6]["link"] is None
|
|
assert sub["outputs"][0]["linkIds"] == [201, 201, 201]
|
|
assert sum(e["id"] == 201 for e in sub["links"]) == 1
|
|
assert ns[103]["outputs"][1]["links"] == []
|
|
f = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
|
|
assert f["150:109", "text"]["unit"] == "负向编码文本"
|
|
assert f["150:108", "text"]["unit"] == "正向编码文本"
|
|
assert f["147", "round_to_multiple"]["value"] == "8"
|
|
assert f["150:102", "cfg"]["json_type"] == "int"
|
|
assert f["150:142", "strength_model"]["value"] == 0.8
|
|
assert f["150:106", "unet_name"]["value"] == "Flux\\flux-2-klein-9b-fp8.safetensors"
|
|
assert "temp_acghq" in canonical(f["145", "rgthree_comparer"]["value"]).decode()
|
|
for a, ao, z, zi, k in [
|
|
(76, 0, 147, 0, "IMAGE"),
|
|
(147, 0, 150, 0, "IMAGE"),
|
|
(147, 0, 151, 0, "IMAGE"),
|
|
(151, 0, 145, 1, "IMAGE"),
|
|
(150, 0, 145, 0, "IMAGE"),
|
|
(150, 0, 9, 0, "IMAGE"),
|
|
(152, 0, 150, 5, "STRING"),
|
|
(147, 3, 150, 7, "INT"),
|
|
(147, 4, 150, 8, "INT"),
|
|
]:
|
|
assert any(e[1:] == [a, ao, z, zi, k] for e in SOURCE["links"])
|
|
for a, ao, z, zi in [
|
|
(-10, 0, 141, 0),
|
|
(141, 0, 140, 1),
|
|
(141, 0, 139, 1),
|
|
(108, 0, 140, 0),
|
|
(109, 0, 139, 0),
|
|
(140, 0, 102, 1),
|
|
(139, 0, 102, 2),
|
|
(111, 0, 103, 4),
|
|
(103, 0, 104, 0),
|
|
(104, 0, -20, 0),
|
|
(-10, 7, 135, 0),
|
|
(-10, 8, 136, 0),
|
|
(135, 0, 101, 1),
|
|
(135, 0, 111, 0),
|
|
(136, 0, 101, 2),
|
|
(136, 0, 111, 1),
|
|
]:
|
|
assert any(
|
|
(e["origin_id"], e["origin_slot"], e["target_id"], e["target_slot"]) == (a, ao, z, zi)
|
|
for e in sub["links"]
|
|
)
|
|
|
|
|
|
def test_precise_passive_primitive_layer_and_comparer_text():
|
|
def read(key, file):
|
|
e = next(s for s in BLOCK["sources"] if s["id"] == key and s["file"] == file)
|
|
return (OUT / e["local_file"]).read_text()
|
|
|
|
prim = read("primitive", "comfy_extras/nodes_primitive.py")
|
|
assert 'node_id="PrimitiveStringMultiline"' in prim
|
|
assert 'io.String.Input("value", multiline=True)' in prim
|
|
assert "return io.NodeOutput(value)" in prim and "StringMultiline," in prim
|
|
nodes = read("primitive", "nodes.py")
|
|
assert '"nodes_primitive.py"' in nodes
|
|
layer = read("layer", "py/image_scale_by_aspect_ratio_v2.py")
|
|
assert (
|
|
"ratio = orig_width / orig_height" in layer and "multiple = int(round_to_multiple)" in layer
|
|
)
|
|
assert "num_round_up_to_multiple(target_width, multiple)" in layer
|
|
assert "NODE_CLASS_MAPPINGS" in layer
|
|
imagefunc = read("layer", "py/imagefunc.py")
|
|
assert "elif fit == 'crop':" in imagefunc
|
|
edit = read("core", "comfy_extras/nodes_edit_model.py")
|
|
assert "append=True" in edit and "if latent is not None:" in edit
|
|
host = read("frontend", "src/lib/litegraph/src/subgraph/SubgraphNode.ts")
|
|
assert "slot.name === input.name" in host and "connectedWidget.value = widget.value" in host
|
|
cmp = read("rgthree", "web/comfyui/image_comparer.js")
|
|
assert "serialised.widgets_values[index]" in cmp and "return { images: v };" in cmp
|
|
assert 'this.properties["comparer_mode"] = "Slide"' in cmp
|
|
assert "image_a" in read("rgthree", "py/image_comparer.py")
|
|
|
|
|
|
NODE_POINTERS = [f"/nodes/{i}" for i in range(9)] + [
|
|
f"/definitions/subgraphs/0/nodes/{i}" for i in range(18)
|
|
]
|
|
EDGE_POINTERS = [f"/links/{i}" for i in range(9)] + [
|
|
f"/definitions/subgraphs/0/links/{i}" for i in range(32)
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("p", NODE_POINTERS)
|
|
@pytest.mark.parametrize("variant", ["mode", "identity", "named"])
|
|
def test_all_root_and_internal_nodes_bound(p, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
n = pointer(s, p)
|
|
if variant == "mode":
|
|
n["mode"] = 4
|
|
elif variant == "identity":
|
|
n["properties"]["ver"] = "forged"
|
|
else:
|
|
n["widgets_values_named"] = {"forged": 0}
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize("p", EDGE_POINTERS)
|
|
def test_every_edge_bound_without_repair(p):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
e = pointer(s, p)
|
|
if isinstance(e, list):
|
|
e[3] = 999
|
|
else:
|
|
e["target_id"] = 999
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize("z", BLOCK["zero_saved_nodes"], ids=lambda z: z["node_id"])
|
|
@pytest.mark.parametrize("variant", ["missing", "empty", "null", "added"])
|
|
def test_absent_empty_null_stay_distinct(z, variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
n = pointer(s, z["pointer"])
|
|
before = copy.deepcopy(n)
|
|
if variant == "missing":
|
|
n.pop("widgets_values", None)
|
|
else:
|
|
n["widgets_values"] = {"empty": [], "null": None, "added": [0]}[variant]
|
|
if n != before:
|
|
refuse(s, r)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"variant",
|
|
[
|
|
"overview",
|
|
"citation",
|
|
"sha",
|
|
"frontend",
|
|
"count",
|
|
"proxy",
|
|
"duplicate",
|
|
"boundary_name",
|
|
"boundary_type",
|
|
"boundary_label",
|
|
"definition_widgets",
|
|
"note",
|
|
"unconnected_output",
|
|
"history",
|
|
"lora_identity",
|
|
"host_identity",
|
|
],
|
|
)
|
|
def test_all_metadata_and_boundary_declarations_bound(variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
b = r["fixed_version_document"]
|
|
sub = s["definitions"]["subgraphs"][0]
|
|
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.36.14"
|
|
elif variant == "count":
|
|
b["counts"]["final_prompt_constructed"] = True
|
|
elif variant == "proxy":
|
|
s["nodes"][7]["properties"]["proxyWidgets"][0][0] = "999"
|
|
elif variant == "duplicate":
|
|
sub["outputs"][0]["linkIds"] = [201]
|
|
elif variant == "boundary_name":
|
|
sub["inputs"][7]["name"] = "width"
|
|
elif variant == "boundary_type":
|
|
sub["inputs"][7]["type"] = "INT"
|
|
elif variant == "boundary_label":
|
|
sub["inputs"][7]["label"] = "forged"
|
|
elif variant == "definition_widgets":
|
|
sub["widgets"] = None
|
|
elif variant == "note":
|
|
s["nodes"][0]["widgets_values"][0] = "forged"
|
|
elif variant == "unconnected_output":
|
|
next(n for n in sub["nodes"] if n["id"] == 103)["outputs"][1]["links"] = [999]
|
|
elif variant == "history":
|
|
s["nodes"][5]["widgets_values"][0][0]["url"] = "forged"
|
|
elif variant == "lora_identity":
|
|
next(n for n in sub["nodes"] if n["id"] == 142)["properties"]["ver"] = "0.8.2"
|
|
else:
|
|
s["nodes"][7]["properties"]["ver"] = "0.8.2"
|
|
refuse(s, r)
|