Files
aigc-home/calliope-backend/tests/test_flux2_fixed_document.py
T

155 lines
5.7 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-document-evidence"
PATH = "example_ComfyUI_workflows/3-图像生成/Flux/1_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(17))
@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"] = 4
elif variant == "identity":
n["properties"]["ver"] = "forged"
else:
n["widgets_values_named"] = {"forged": 0}
refuse(s, r)
@pytest.mark.parametrize("index", range(20))
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", [82, 83, 94, 95])
@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"] == 82)["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) == 46 and len(b["fields"]) == 24 and not b["comments"]
assert sum(f["role"] == "linked_saved_cache" for f in b["fields"]) == 5
assert sum(f["role"] == "frontend_reference_only" for f in b["fields"]) == 1
f = {(x["node_id"], x["field"]): x for x in b["fields"]}
assert (
f["81", "cfg"]["json_type"] == "int"
and f["93", ("use_custom_resolution")]["value"] is False
)
assert f["76", "value"]["value"] == (
"一辆复古摩托车停在复古餐厅前,夕阳下,温"
"暖橙粉色的天空,霓虹灯招牌闪烁,80年代"
"复古照片风格,胶片颗粒感,暖色调。"
)
assert f["88", "text"]["value"] == f["97", "text"]["value"] == ""
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 [
"无IMAGE出口",
"0.9.1",
"无LICENSE",
"7.5.2/7.7/7.8",
"槽1 denoised_output",
"四处1024",
]:
assert term in "\n".join(b["flow"] + b["versions"])