Files
aigc-home/calliope-backend/tests/test_sam3_fixed_document.py
T
2026-10-30 21:59:57 +08:00

273 lines
10 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/sam3-document-evidence"
PATHS = [
next(p for p in DEFINITIONS if p.endswith(n + ".json"))
for n in ["sam3_图像视频-提示词遮罩分割"]
]
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"]
def refuse(source, review):
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
canonical(source)
).hexdigest()
review["caller_digest"] = hashlib.sha256(
canonical(review["fixed_version_document"])
).hexdigest()
assert not doc(0, source, review)["fixed_version"]["matched"]
SCOPES = [("", SOURCES[0])] + [
(f"/definitions/subgraphs/{i}", g) for i, g in enumerate(SOURCES[0]["definitions"]["subgraphs"])
]
NODES = [(p, i) for p, g in SCOPES for i, n in enumerate(g["nodes"])]
@pytest.mark.parametrize("prefix,index", NODES)
@pytest.mark.parametrize("variant", ["mode", "identity", "saved_shape"])
def test_root_and_scoped_identity_refusal(prefix, index, variant):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
n = pointer(s, f"{prefix}/nodes/{index}")
if variant == "mode":
n["mode"] = 4
elif variant == "identity":
n["properties"]["ver"] = "forged"
else:
n["widgets_values"] = {} if n.get("widgets_values") != {} else []
refuse(s, r)
@pytest.mark.parametrize("prefix,edge", [(p, i) for p, g in SCOPES for i in range(len(g["links"]))])
def test_root_and_definition_endpoints_refuse(prefix, edge):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
g = pointer(s, prefix) if prefix else s
e = g["links"][edge]
if isinstance(e, list):
e[3] = 99999
else:
e["target_slot"] = 99999
refuse(s, r)
@pytest.mark.parametrize(
"variant",
[
"host_empty",
"proxy",
"definition_widgets",
"boundary",
"frontend",
"ue",
"model",
"missing_muter",
"raw",
"citation",
"layout",
"summary",
"path",
],
)
def test_restoration_model_layout_and_binding_refusal(variant):
s, r = copy.deepcopy(SOURCES[0]), copy.deepcopy(REVIEWS[0])
ns = {n["id"]: n for n in s["nodes"]}
d = s["definitions"]["subgraphs"][0]
b = r["fixed_version_document"]
if variant == "host_empty":
ns[137]["widgets_values"] = ["unicorn"]
elif variant == "proxy":
ns[154]["properties"]["proxyWidgets"][0][0] = "100"
elif variant == "definition_widgets":
d["widgets"] = [0.5]
elif variant == "boundary":
d["inputs"][0]["linkIds"] = []
elif variant == "frontend":
s["extra"]["frontendVersion"] = "1.53.10"
elif variant == "ue":
ns[154]["properties"]["ue_properties"]["version"] = "7.5"
elif variant == "model":
next(n for n in d["nodes"] if n["type"] == "CheckpointLoaderSimple")["properties"][
"models"
][0]["name"] = "forged.pt"
elif variant == "missing_muter":
ns[150]["widgets_values"] = []
elif variant == "raw":
r["source_sha"] = "0" * 40
elif variant == "citation":
b["sources"][0]["sha256"] = "0" * 64
elif variant == "layout":
ns[154]["pos"][0] += 1
elif variant == "summary":
b["user_overview"] = "已可运行"
else:
b["source_path"] = "forged"
refuse(s, r)
def test_source_counts_semantics_and_whole_old_reviews():
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(subprocess.check_output(["git", "show", "8b4b9ca:" + resource], cwd=REPO))
now = json.loads((REPO / resource).read_bytes())
b = now[PATHS[0]].pop("fixed_version_document")
assert now == old and len(DEFINITIONS) == 27
oldblocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
assert len(oldblocks) == 26 and sum(len(b["fields"]) for b in oldblocks) == 820
assert (
sum(
len(
build_document(p, json.loads((REPO / p).read_bytes()), None, None, [], old[p])[
"source_notes"
]
)
for p in old
if "fixed_version_document" in old[p]
)
== 16
)
g = SOURCES[0]
ns = {n["id"]: n for n in g["nodes"]}
assert len(ns) == 15 and len(g["links"]) == 14
assert {i for i, n in ns.items() if n["mode"] == 2} == {131, 132, 133, 134, 135, 137, 136}
assert "widgets_values" not in ns[150]
assert ns[137]["widgets_values"] == ns[154]["widgets_values"] == []
assert (
len(ns[137]["properties"]["proxyWidgets"])
== len(ns[154]["properties"]["proxyWidgets"])
== 5
)
assert len(b["fields"]) == len({f["pointer"] for f in b["fields"]}) == 18
assert sum(f["scope"] == "root" for f in b["fields"]) == 8
assert all(not isinstance(f["value"], (dict, list)) for f in b["fields"])
assert ns[152]["outputs"][1]["links"] in ([], None)
assert ns[154]["outputs"][1]["links"] == []
assert all(ns[154]["inputs"][i]["link"] is None for i in [2, 3, 4])
assert ns[134]["widgets_values"] == [30]
assert next(i for i in ns[134]["inputs"] if i["name"] == "fps")["link"] == 314
assert ns[137]["outputs"][2]["links"] is None and ns[137]["outputs"][3]["links"] is None
for eid, src, slot, dst, inp, typ in g["links"]:
assert eid in ns[src]["outputs"][slot]["links"]
assert ns[src]["outputs"][slot]["type"] == typ
assert ns[dst]["inputs"][inp]["link"] == eid and ns[dst]["inputs"][inp]["type"] == typ
for i, d in enumerate(g["definitions"]["subgraphs"]):
assert len(d["nodes"]) == [5, 3][i] and len(d["links"]) == [17, 14][i]
assert d["widgets"] == []
assert all(n["mode"] == [2, 0][i] for n in d["nodes"])
nodes = {n["id"]: n for n in d["nodes"]}
for e in d["links"]:
origin, target = e["origin_id"], e["target_id"]
if origin < 0:
assert origin == d["inputNode"]["id"]
assert e["id"] in d["inputs"][e["origin_slot"]]["linkIds"]
else:
assert e["id"] in nodes[origin]["outputs"][e["origin_slot"]]["links"]
if target < 0:
assert target == d["outputNode"]["id"]
assert e["id"] in d["outputs"][e["target_slot"]]["linkIds"]
else:
assert nodes[target]["inputs"][e["target_slot"]]["link"] == e["id"]
cp = next(n for n in d["nodes"] if n["type"] == "CheckpointLoaderSimple")
assert cp["widgets_values"] == ["sam3.1_multiplex.pt"]
assert cp["properties"]["models"][0]["name"] == "sam3.1_multiplex_fp16.safetensors"
assert cp["outputs"][2]["links"] is None
d = doc(0)
assert d["fixed_version"]["matched"] and d["dependencies"]["check_state"] == "unchecked"
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
assert len(d["source_notes"]) == len(b["comments"]) == 3
for c in b["comments"]:
assert pointer(g, c["pointer"]) == [c["text"]]
assert next(n for n in d["source_notes"] if n["node_id"] == c["node_id"])["content"] == [
c["text"]
]
assert not b["counts"]["final_prompt_constructed"]
assert all(
word in " ".join(b["flow"])
for word in [
"1-alpha",
"temp PNG",
"314",
"VAE未接",
"32 tokens/逗号/:N",
"未找到",
"promoted",
]
)