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

239 lines
10 KiB
Python

"""Source-bound editorial trust, noncontiguous host boundaries and overview-only revision."""
from __future__ import annotations
import copy
import hashlib
import json
import subprocess
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/background-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("Qwen_image2.1_图像编辑_背景移除.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, path):
for p in path.strip("/").split("/"):
p = p.replace("~1", "/").replace("~0", "~")
obj = obj[int(p)] if isinstance(obj, list) else obj[p]
return obj
def doc(s=None, r=None, path=PATH):
return build_document(
path, SOURCE if s is None else s, None, None, [], REVIEW if r is None else r
)
@pytest.mark.parametrize("index", range(44))
def test_every_saved_position_and_fixed_citation(index):
f = BLOCK["fields"][index]
v = pointer(SOURCE, f["pointer"])
assert canonical(v) == canonical(f["value"]) == canonical(pointer(SOURCE, f["named_pointer"]))
assert type(v).__name__ == f["json_type"]
n = pointer(SOURCE, f["pointer"].split("/widgets_values/")[0])
assert str(n["id"]) == f["definition_node_id"] and n["type"] == f["class_type"]
assert f["node_id"] == ("459:" if f["scope"] == "definition" else "") + str(n["id"])
assert all(
f[k]
for k in [
"label",
"effect",
"type",
"schema_default",
"limits",
"unit",
"execution_note",
"evidence",
]
)
for i in f["evidence"]:
e = BLOCK["sources"][i]
raw = (OUT / e["local_file"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
assert len(e["commit"]) == 40 and e["commit"] in e["url"]
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
if e["local_file"].startswith("../background-reference/"):
assert e["commit"] == "73c9bad4d21e7addbe1d13bc92eee0f1431b017d"
@pytest.mark.parametrize("index", range(16))
def test_noncontiguous_host_widgets_restore_to_exact_boundaries(index):
b = BLOCK["bindings"][index]
assert b["value_pointer"] == f"/nodes/4/widgets_values/{index}"
assert canonical(pointer(SOURCE, b["value_pointer"])) == canonical(
pointer(SOURCE, b["named_pointer"])
)
boundary = pointer(SOURCE, b["boundary_pointer"])
assert boundary["id"] == b["boundary_id"] and boundary["name"] == b["name"]
for t in b["targets"]:
socket = pointer(SOURCE, t["target_pointer"])
edge = pointer(SOURCE, t["link_pointer"])
assert socket["name"] == t["field"] and socket["link"] == edge["id"]
assert edge["origin_slot"] == int(b["boundary_pointer"].split("/")[-1])
assert edge["target_id"] == int(t["definition_node_id"])
assert "widgetId(rootGraph.id, 459," in b["widget_id_rule"]
@pytest.mark.parametrize("index", range(44))
@pytest.mark.parametrize("kind", ["value", "type", "named", "pointer", "citation", "scope"])
def test_saved_mismatch_and_recomputed_caller_digest_fail_closed(index, kind):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
f = r["fixed_version_document"]["fields"][index]
if kind in ["pointer", "scope"]:
f[kind] = "forged"
elif kind == "citation":
r["fixed_version_document"]["sources"][f["evidence"][0]]["commit"] = "0" * 40
else:
p = f["named_pointer"] if kind == "named" else f["pointer"]
parent, _, key = p.rpartition("/")
target = pointer(s, parent)
target[int(key) if isinstance(target, list) else key] = (
str(f["value"]) + "tampered" if kind == "type" else {"forged": True}
)
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
r["caller_digest"] = hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest()
d = doc(s, r)["fixed_version"]
assert not d["matched"] and "fields" not in d and "user_overview" not in d
@pytest.mark.parametrize(
"location",
[c["pointer"] for c in BLOCK["comments"]]
+ [b["boundary_pointer"] for b in BLOCK["bindings"]]
+ [
"/links/0",
"/definitions/subgraphs/0/id",
"/definitions/subgraphs/0/links/0/target_slot",
"/nodes/4/type",
"/extra/frontendVersion",
"/definitions/subgraphs/0/nodes/0/properties/ver",
],
)
def test_source_notes_edges_version_and_definition_mismatch_rejected(location):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
parent, _, key = location.rpartition("/")
target = pointer(s, parent)
target[int(key) if isinstance(target, list) else key] = "forged"
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
r["caller_digest"] = hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest()
assert not doc(s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("path", list(DEFINITIONS))
def test_summary_forgery_does_not_expand_trust(path):
raw = (REPO / path).read_bytes()
s = json.loads(raw)
r = copy.deepcopy(review_for(path, hashlib.sha1(raw).hexdigest()))
r["fixed_version_document"]["user_overview"] = '<img src="https://example.invalid">forged'
r["caller_digest"] = hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest()
assert not doc(s, r, path)["fixed_version"]["matched"]
def test_counts_controls_topology_and_transparency_limits():
b = doc()["fixed_version"]
fs = {(f["node_id"], f["field"]): f for f in b["fields"]}
assert b["matched"] and len(b["fields"]) == 44 and len(b["comments"]) == 4
assert b["counts"]["root_saved"] == b["counts"]["definition_saved"] == 22
assert b["counts"]["source_nodes"] == 17 and b["counts"]["physical_edges"] == 42
assert fs["459", "switch"]["value"] is False
assert fs["459:458", "control_after_generate"]["value"] == "randomize"
assert fs["459", "seed"]["value"] == 121465604207346
assert fs["459:458", "seed"]["value"] == 0
assert fs["459:458", "seed"]["binding"]["name"] == "seed"
assert [t["field"] for t in fs["459", "seed"]["binding"]["targets"]] == ["seed"]
assert fs["459", "scheduler"]["binding"]["targets"][0]["field"] == "sampler_name"
assert fs["459", "scheduler_1"]["binding"]["targets"][0]["field"] == "scheduler"
assert fs["459", "device"]["binding"]["boundary_pointer"].endswith("/24")
assert fs["459", "dtype"]["binding"]["boundary_pointer"].endswith("/25")
assert len(SOURCE["definitions"]["subgraphs"][0]["nodes"]) == 9
assert SOURCE["links"] == [
[707, 470, 0, 472, 0, "IMAGE"],
[708, 461, 0, 472, 1, "IMAGE"],
[738, 470, 0, 459, 14, "IMAGE"],
[672, 459, 0, 461, 0, "IMAGE"],
]
assert all(pointer(SOURCE, c["pointer"]) == c["text"] for c in b["comments"])
text = "".join(b["flow"])
assert all(
t in text
for t in [
"无TextGenerate",
"on_false",
"resolution=0",
"无外线选择器",
"PNG容器不等于",
"输出列表为空",
"temp PNG",
"randomize",
]
)
assert all(
f["class_type"] not in ["TextGenerate", "ResolutionSelector", "Image Comparer (rgthree)"]
for f in b["fields"]
)
assert [n["node_id"] for n in b["zero_saved_nodes"]] == ["472", "459:457"]
assert not doc(path="unknown.json").get("fixed_version")
def test_ten_documents_only_gain_overview_and_new_block_is_independent():
p = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(subprocess.check_output(["git", "show", "ec3388a:" + p], cwd=REPO))
now = json.loads((REPO / p).read_bytes())
multivideo = next(p for p in now if p.endswith("/多视频合成分屏展示.json"))
now[multivideo].pop("fixed_version_document")
watermark = next(p for p in now if p.endswith("/图像4种添加水印节点.json"))
now[watermark].pop("fixed_version_document")
birefnet = next(p for p in now if p.endswith("/birefnet_图像移除背景.json"))
now[birefnet].pop("fixed_version_document")
# Freeze the historical eleven-document revision; the two grid additions are
# independently compared against 27e6d56 in test_grid_fixed_document.
# Gemma is separately checked against 9dac1b4 in test_gemma_fixed_document.
additions = [
p
for p in DEFINITIONS
if p.endswith(
(
"图像合并成四宫格.json",
"图像分割-多宫格分割.json",
"gemma4_提示词反推(图像视频音频).json",
"sdpose_视频人物姿势骨架提取.json",
"openpose-video-姿态检测.json",
"NLF_视频人物动作骨架获取.json",
"/视频倒放.json",
"视频转gif动图图片.json",
"加载图片合成视频.json",
"双图像或视频合并显示.json",
"截取视频每一帧.json",
"加载本地latent解码.json",
)
)
]
assert len(additions) == 12
for p in additions:
now[p].pop("fixed_version_document")
historical = [
p for p in DEFINITIONS if p not in additions and p not in [birefnet, watermark, multivideo]
]
assert len(historical) == 11
for path in historical:
block = now[path]["fixed_version_document"]
assert block["user_overview"] and "待核对" in block["user_overview"]
assert hashlib.sha256(canonical(block)).hexdigest() == DEFINITIONS[path]
if path == PATH:
now[path].pop("fixed_version_document")
else:
block.pop("user_overview")
assert block == old[path]["fixed_version_document"]
assert now == old