203 lines
7.9 KiB
Python
203 lines
7.9 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-img-document-evidence"
|
||
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/2_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(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(31))
|
||
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, 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_actual_document_overrides_and_status():
|
||
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
|
||
assert canonical(b) == canonical(BLOCK)
|
||
assert len(DEFINITIONS) == 47 and len(b["fields"]) == 36 and len(b["comments"]) == 1
|
||
assert sum(f["role"] == "linked_saved_cache" for f in b["fields"]) == 6
|
||
assert sum(f["role"] == "frontend_reference_only" for f in b["fields"]) == 3
|
||
f = {(x["node_id"], x["field"]): x for x in b["fields"]}
|
||
assert f["102", "cfg"]["json_type"] == "int"
|
||
assert f["101", "steps"]["limits"] == "1~4096"
|
||
assert f["144", "custom_width"]["limits"].startswith("64~8192,step1")
|
||
assert f["144", "use_custom_resolution"]["value"] is False
|
||
assert f["108", "text"]["value"] == "将人物换成欧美人"
|
||
assert f["109", "text"]["value"] == ""
|
||
assert f["146", "pad_color"]["value"] == "0, 0, 0"
|
||
history = f["145", "rgthree_comparer"]
|
||
assert history["json_type"] == "list" and len(history["value"]) == 2
|
||
assert history["value"] == SOURCE["nodes"][15]["widgets_values"][0]
|
||
assert BLOCK["comments"][0]["text"] == SOURCE["nodes"][0]["widgets_values"][0]
|
||
assert len(BLOCK["comments"][0]["text"]) == 1396
|
||
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]
|
||
for term in [
|
||
"mode4",
|
||
"1.36.13",
|
||
"0.21.1",
|
||
"无LICENSE",
|
||
"四处1024",
|
||
"B直接来自76",
|
||
"无IMAGE出口",
|
||
"Git绑定未知",
|
||
]:
|
||
assert term in "\n".join(b["flow"] + b["versions"])
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"variant", ["note", "nested-history", "slide", "clipspace", "label-identity", "lora-mode"]
|
||
)
|
||
def test_complete_history_note_and_independent_identity_bound(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 == "nested-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-identity":
|
||
ns[143]["properties"]["cnr_id"] = "rgthree-comfy"
|
||
else:
|
||
ns[142]["mode"] = 0
|
||
refuse(s, r)
|
||
|
||
|
||
def test_physical_topology_and_exact_version_schema_conflicts():
|
||
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 [
|
||
(104, 0, 145, 0, "IMAGE"),
|
||
(76, 0, 145, 1, "IMAGE"),
|
||
(141, 0, 140, 1, "LATENT"),
|
||
(141, 0, 139, 1, "LATENT"),
|
||
(111, 0, 103, 4, "LATENT"),
|
||
(146, 1, 135, 0, "INT"),
|
||
(146, 2, 136, 0, "INT"),
|
||
]:
|
||
assert edge in edges
|
||
assert ns[76]["outputs"][1]["links"] is None
|
||
assert ns[146]["inputs"][1]["link"] is None
|
||
assert ns[146]["outputs"][3]["links"] == []
|
||
assert ns[142]["mode"] == 4 and ns[103]["outputs"][1]["links"] == []
|
||
assert ns[9]["outputs"][0]["type"] == "IMAGE"
|
||
assert "RETURN_TYPES = ()" in (OUT.parent / "flux2-img-reference/core__nodes.py").read_text()
|
||
assert {e["tag"] for e in BLOCK["sources"] if e["id"] == "frontend"} == {"v1.36.13"}
|
||
assert {e["tag"] for e in BLOCK["sources"] if e["id"] == "lora"} == {"v0.21.1"}
|