267 lines
11 KiB
Python
267 lines
11 KiB
Python
"""One editorial addition: identity refusal, exact positions and media conditions."""
|
|
|
|
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/birefnet-document-evidence"
|
|
PATHS = [next(p for p in DEFINITIONS if p.endswith(n + ".json")) for n in ["birefnet_图像移除背景"]]
|
|
SOURCES = [json.loads((REPO / p).read_bytes()) for p in PATHS]
|
|
REVIEWS = [review_for(p, hashlib.sha1((REPO / p).read_bytes()).hexdigest()) for p in PATHS]
|
|
FIELDS = [(i, f) for i, r in enumerate(REVIEWS) for f in r["fixed_version_document"]["fields"]]
|
|
|
|
|
|
def pointer(obj, path):
|
|
for part in path.strip("/").split("/"):
|
|
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
|
|
return obj
|
|
|
|
|
|
def doc(i, source=None, review=None):
|
|
return build_document(
|
|
PATHS[i],
|
|
SOURCES[i] if source is None else source,
|
|
None,
|
|
None,
|
|
[],
|
|
REVIEWS[i] if review is None else review,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("i,field", FIELDS)
|
|
def test_exact_positions_and_fixed_text_citations(i, field):
|
|
value = pointer(SOURCES[i], field["pointer"])
|
|
assert (
|
|
canonical(value) == canonical(field["value"]) and type(value).__name__ == field["json_type"]
|
|
)
|
|
for k in [
|
|
"label",
|
|
"type",
|
|
"schema_default",
|
|
"unit",
|
|
"limits",
|
|
"effect",
|
|
"execution_note",
|
|
"role",
|
|
]:
|
|
assert field[k]
|
|
for k in field["evidence"]:
|
|
e = REVIEWS[i]["fixed_version_document"]["sources"][k]
|
|
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())
|
|
assert (len(e["commit"]) == 40 and e["commit"] in e["url"]) or e.get(
|
|
"git_commit_binding"
|
|
) == "unknown"
|
|
|
|
|
|
@pytest.mark.parametrize("i,field", FIELDS)
|
|
@pytest.mark.parametrize("variant", ["saved", "type", "text"])
|
|
def test_every_position_refuses_even_recomputed_caller_digest(i, field, variant):
|
|
s, r = copy.deepcopy(SOURCES[i]), copy.deepcopy(REVIEWS[i])
|
|
b = r["fixed_version_document"]
|
|
f = next(f for f in b["fields"] if f["pointer"] == field["pointer"])
|
|
if variant == "text":
|
|
f["schema_default"] = "forged"
|
|
else:
|
|
parent, _, key = field["pointer"].rpartition("/")
|
|
target = pointer(s, parent)
|
|
target[int(key) if isinstance(target, list) else key] = (
|
|
"forged" if variant == "type" else ("changed-null" if field["value"] is None else None)
|
|
)
|
|
f["value"] = pointer(s, field["pointer"])
|
|
f["json_type"] = type(f["value"]).__name__
|
|
b["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
r["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
|
|
assert not doc(i, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("node", [22, 24, 25, 23, 21, 18, 20, 17])
|
|
@pytest.mark.parametrize("kind", ["mode", "ver", "cnr", "ue"])
|
|
def test_each_identity_and_mode_refuses_recomputed_digest(node, kind):
|
|
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
|
n = next(n for n in s["nodes"] if n["id"] == node)
|
|
if kind == "mode":
|
|
n["mode"] = 2
|
|
elif kind == "ue":
|
|
n["properties"]["ue_properties"]["version"] = "forged"
|
|
else:
|
|
n["properties"]["ver" if kind == "ver" else "cnr_id"] = "forged"
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("node", [22, 24, 25, 18, 20])
|
|
@pytest.mark.parametrize("kind", ["missing", "null", "object"])
|
|
def test_zero_positions_are_not_missing_null_or_object(node, kind):
|
|
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
|
n = next(n for n in s["nodes"] if n["id"] == node)
|
|
if kind == "missing":
|
|
del n["widgets_values"]
|
|
else:
|
|
n["widgets_values"] = None if kind == "null" else {}
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("edge", range(7))
|
|
@pytest.mark.parametrize("side", ["input", "output", "physical"])
|
|
def test_each_physical_and_dual_endpoint_refuses(edge, side):
|
|
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
|
e = s["links"][edge]
|
|
nodes = {n["id"]: n for n in s["nodes"]}
|
|
if side == "input":
|
|
nodes[e[3]]["inputs"][e[4]]["link"] = None
|
|
elif side == "output":
|
|
nodes[e[1]]["outputs"][e[2]]["links"].remove(e[0])
|
|
else:
|
|
e[1] = 999
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not doc(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"kind",
|
|
[
|
|
"note",
|
|
"source_sha",
|
|
"citation",
|
|
"frontend",
|
|
"ue",
|
|
"models",
|
|
"tab",
|
|
"swap",
|
|
"unconnected_mask",
|
|
],
|
|
)
|
|
def test_source_note_references_and_binding_fail_closed(kind):
|
|
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
|
|
nodes = {n["id"]: n for n in s["nodes"]}
|
|
if kind == "note":
|
|
nodes[21]["widgets_values"][0] = "forged"
|
|
r["fixed_version_document"]["comments"][0]["text"] = "forged"
|
|
elif kind == "source_sha":
|
|
r["source_sha"] = "0" * 40
|
|
elif kind == "citation":
|
|
r["fixed_version_document"]["sources"][0]["sha256"] = "0" * 64
|
|
elif kind == "frontend":
|
|
s["extra"]["frontendVersion"] = "forged"
|
|
elif kind == "ue":
|
|
s["extra"]["links_added_by_ue"] = [1]
|
|
elif kind == "models":
|
|
nodes[23]["properties"]["models"][0]["url"] = "forged"
|
|
elif kind == "tab":
|
|
nodes[17]["properties"]["enableTabs"] = True
|
|
elif kind == "unconnected_mask":
|
|
nodes[17]["outputs"][1]["links"] = []
|
|
else:
|
|
other = next(p for p in DEFINITIONS if p.endswith("/加载本地latent解码.json"))
|
|
r = copy.deepcopy(review_for(other, hashlib.sha1((REPO / other).read_bytes()).hexdigest()))
|
|
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(0, s, r)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_actual_graph_counts_pointers_notes_unknowns_and_old_objects():
|
|
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
|
|
old = json.loads(subprocess.check_output(["git", "show", "6e1e47b:" + resource], cwd=REPO))
|
|
now = json.loads((REPO / resource).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")
|
|
b = now[PATHS[0]].pop("fixed_version_document")
|
|
assert now == old and len(DEFINITIONS) == 26
|
|
old_blocks = [
|
|
r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r
|
|
]
|
|
assert len(old_blocks) == 23 and sum(len(b["fields"]) for b in old_blocks) == 662
|
|
assert sum(len(b.get("comments", [])) for b in old_blocks) == 14 # ASR flow note separate
|
|
assert b["counts"] == dict(
|
|
saved_fields=3,
|
|
scalar_saved_positions=3,
|
|
history_objects=0,
|
|
source_nodes=8,
|
|
physical_edges=7,
|
|
source_notes=1,
|
|
linked_saved_fields=0,
|
|
frontend_only=1,
|
|
final_scalars=0,
|
|
final_prompt_constructed=False,
|
|
)
|
|
g = SOURCES[0]
|
|
nodes = {n["id"]: n for n in g["nodes"]}
|
|
assert len(g["nodes"]) == 8 and len(g["links"]) == 7
|
|
assert all(n["mode"] == 0 for n in g["nodes"])
|
|
assert {i for i, n in nodes.items() if n["widgets_values"] == []} == {22, 24, 25, 18, 20}
|
|
assert {n["node_id"] for n in b["zero_saved_nodes"]} == {"22", "24", "25", "18", "20"}
|
|
assert g["extra"]["frontendVersion"] == "1.43.17"
|
|
assert g["extra"]["ue_links"] == g["extra"]["links_added_by_ue"] == []
|
|
for n in g["nodes"]:
|
|
assert n["properties"]["ue_properties"]["version"] == "7.8"
|
|
if n["id"] == 17:
|
|
assert n["properties"]["cnr_id"] == "comfy-core" and n["properties"]["ver"] == "0.19.3"
|
|
else:
|
|
assert "cnr_id" not in n["properties"] and "ver" not in n["properties"]
|
|
assert g["links"] == [
|
|
[17, 23, 0, 22, 1, "BACKGROUND_REMOVAL"],
|
|
[18, 22, 0, 24, 0, "MASK"],
|
|
[19, 24, 0, 25, 1, "MASK"],
|
|
[20, 17, 0, 22, 0, "IMAGE"],
|
|
[21, 17, 0, 25, 0, "IMAGE"],
|
|
[22, 25, 0, 18, 0, "IMAGE"],
|
|
[23, 22, 0, 20, 0, "MASK"],
|
|
]
|
|
for eid, origin, slot, target, inp, typ in g["links"]:
|
|
assert eid in nodes[origin]["outputs"][slot]["links"]
|
|
assert nodes[origin]["outputs"][slot]["type"] == typ
|
|
assert nodes[target]["inputs"][inp]["link"] == eid
|
|
assert nodes[target]["inputs"][inp]["type"] == typ
|
|
assert nodes[17]["outputs"][1]["links"] is None
|
|
assert len(b["comments"]) == 1
|
|
note = b["comments"][0]
|
|
assert pointer(g, note["pointer"]) == note["text"] == nodes[21]["widgets_values"][0]
|
|
d = doc(0)
|
|
assert d["fixed_version"]["matched"] and all(step in d["steps"] for step in b["steps"])
|
|
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
|
|
assert d["dependencies"]["check_state"] == "unchecked"
|
|
assert review_for(PATHS[0], "0" * 40) is None
|
|
for token in [
|
|
"1-resize(1-M)",
|
|
"bilinear",
|
|
"未显式clamp",
|
|
"RGBA",
|
|
"极性未知",
|
|
"temp PNG",
|
|
"固定参考假设",
|
|
"有界静态检索",
|
|
"options.serialize=false",
|
|
"widget.serialize",
|
|
"未接",
|
|
"HuggingFace",
|
|
]:
|
|
assert token in json.dumps(b, ensure_ascii=False)
|
|
|
|
|
|
@pytest.mark.parametrize("other", [p for p in DEFINITIONS if p not in PATHS])
|
|
def test_every_old_block_and_new_block_cannot_be_swapped(other):
|
|
raw = (REPO / other).read_bytes()
|
|
old_source = json.loads(raw)
|
|
old_review = copy.deepcopy(review_for(other, hashlib.sha1(raw).hexdigest()))
|
|
new_review = copy.deepcopy(REVIEWS[0])
|
|
old_block = old_review["fixed_version_document"]
|
|
old_review["fixed_version_document"] = new_review["fixed_version_document"]
|
|
new_review["fixed_version_document"] = old_block
|
|
assert not doc(0, review=new_review)["fixed_version"]["matched"]
|
|
assert not build_document(other, old_source, None, None, [], old_review)["fixed_version"][
|
|
"matched"
|
|
]
|