231 lines
8.2 KiB
Python
231 lines
8.2 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-print-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) == 58 and doc()["fixed_version"]["matched"]
|
|
assert b["counts"]["saved_fields"] == 37 and b["counts"]["source_notes"] == 1
|
|
assert b["counts"]["root_saved_fields"] == 17 and b["counts"]["internal_saved_fields"] == 20
|
|
assert not doc()["status"]["doc_done"] and not doc()["status"]["exec_verified"]
|
|
assert [len(n["text"]) for n in b["comments"]] == [1396]
|
|
assert {s["id"] for s in b["sources"]} == {"core", "lora", "frontend", "tt", "kj"}
|
|
assert {s["tag"] for s in b["sources"] if s["id"] == "frontend"} == {"v1.36.13"}
|
|
|
|
|
|
def test_inventory_citations_and_bidirectional_endpoints():
|
|
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_print_extraction_semantics_and_exact_boundaries():
|
|
g = SOURCE
|
|
sub = g["definitions"]["subgraphs"][0]
|
|
ns = {n["id"]: n for n in g["nodes"]}
|
|
inner = {n["id"]: n for n in sub["nodes"]}
|
|
assert ns[144]["widgets_values"] == [False, "1080x1920 (9:16) (竖屏)", 1024, 1024]
|
|
assert ns[146]["widgets_values"] == [
|
|
512,
|
|
512,
|
|
"nearest-exact",
|
|
"crop",
|
|
"0, 0, 0",
|
|
"center",
|
|
8,
|
|
"cpu",
|
|
]
|
|
for edge in [
|
|
[300, 146, 1, 149, 7, "INT"],
|
|
[299, 146, 2, 149, 8, "INT"],
|
|
[284, 76, 0, 147, 0, "IMAGE"],
|
|
[290, 149, 0, 147, 1, "IMAGE"],
|
|
[289, 149, 0, 9, 0, "IMAGE"],
|
|
[285, 147, 0, 148, 0, "IMAGE"],
|
|
]:
|
|
assert edge in g["links"]
|
|
for a, ao, z, zi in [
|
|
(135, 0, 111, 0),
|
|
(135, 0, 101, 1),
|
|
(136, 0, 111, 1),
|
|
(136, 0, 101, 2),
|
|
(141, 0, 140, 1),
|
|
(141, 0, 139, 1),
|
|
(111, 0, 103, 4),
|
|
(106, 0, 142, 0),
|
|
(142, 0, 102, 0),
|
|
]:
|
|
assert any(
|
|
(e["origin_id"], e["origin_slot"], e["target_id"], e["target_slot"]) == (a, ao, z, zi)
|
|
for e in sub["links"]
|
|
)
|
|
assert sub["outputs"][0]["linkIds"] == [201, 201]
|
|
assert ns[9]["outputs"] == ns[148]["outputs"] == []
|
|
assert [i["type"] for i in ns[147]["inputs"][:2]] == ["IMAGE,MASK", "IMAGE,MASK"]
|
|
assert inner[109]["widgets_values"] == [""]
|
|
assert inner[110]["widgets_values"] == ["flux2-vae.safetensors"]
|
|
f = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]}
|
|
assert "原图在左、生成在右" in f["147", "direction"]["effect"]
|
|
assert "仅明确KJ1.3.9参考假设" in f["147", "direction"]["effect"]
|
|
assert "不要擦除、模糊或移除图案" in f["149:108", "text"]["effect"]
|
|
assert "透明PNG、矢量、无缝" in f["149:108", "text"]["effect"]
|
|
assert "MASK links=null未接" in f["76", "image"]["effect"]
|
|
assert "边界295" in f["149:108", "text"]["effect"]
|
|
|
|
|
|
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(10)] + [
|
|
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"] = 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("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",
|
|
[
|
|
"path",
|
|
"source_sha",
|
|
"note",
|
|
"proxy",
|
|
"output_duplicates",
|
|
"boundary_name",
|
|
"boundary_type",
|
|
"definition_widgets",
|
|
"frontend",
|
|
"source_citation",
|
|
],
|
|
)
|
|
def test_metadata_fail_closed(variant):
|
|
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
sub = s["definitions"]["subgraphs"][0]
|
|
if variant == "path":
|
|
assert (
|
|
not build_document(PATH + ".forged", s, None, None, [], r)
|
|
.get("fixed_version", {})
|
|
.get("matched", False)
|
|
)
|
|
return
|
|
if variant == "source_sha":
|
|
r["fixed_version_document"]["source_sha256"] = "0" * 64
|
|
elif variant == "source_citation":
|
|
r["fixed_version_document"]["sources"][0]["sha256"] = "0" * 64
|
|
elif variant == "note":
|
|
next(n for n in s["nodes"] if n["id"] == 97)["widgets_values"][0] += "forged"
|
|
elif variant == "proxy":
|
|
s["nodes"][6]["properties"]["proxyWidgets"][0][0] = "999"
|
|
elif variant == "output_duplicates":
|
|
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 == "definition_widgets":
|
|
sub["widgets"] = [0]
|
|
elif variant == "frontend":
|
|
s["extra"]["frontendVersion"] = "1.40.0"
|
|
refuse(s, r)
|