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

257 lines
9.8 KiB
Python

"""Exact Gemma source binding, real document output, old definitions and refusal regressions."""
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/gemma-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("gemma4_提示词反推(图像视频音频).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 part in path.strip("/").split("/"):
part = part.replace("~1", "/").replace("~0", "~")
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
return obj
def doc(source=None, review=None, path=PATH):
return build_document(
path,
SOURCE if source is None else source,
None,
None,
[],
REVIEW if review is None else review,
)
@pytest.mark.parametrize("field", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
def test_saved_values_types_and_fixed_citations(field):
v = pointer(SOURCE, field["pointer"])
assert canonical(v) == canonical(field["value"]) and type(v).__name__ == field["json_type"]
node = pointer(SOURCE, field["pointer"].split("/widgets_values/")[0])
assert str(node["id"]) == field["node_id"] and node["type"] == field["class_type"]
assert node["properties"]["ver"] == "0.21.0"
for k in ["label", "schema_default", "unit", "limits", "effect", "execution_note"]:
assert field[k]
for k in field["evidence"]:
e = BLOCK["sources"][k]
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())
@pytest.mark.parametrize("index", range(25))
@pytest.mark.parametrize("kind", ["value", "type", "pointer", "citation", "role", "default"])
def test_forged_fields_fail_even_after_caller_digest_recalculation(index, kind):
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = review["fixed_version_document"]
f = b["fields"][index]
if kind in ["value", "type"]:
parent, _, key = f["pointer"].rpartition("/")
pointer(source, parent)[int(key)] = (
{"forged": True} if kind == "value" else (1 if isinstance(f["value"], bool) else False)
)
# Even when source and editorial field are forged consistently, trust cannot expand.
f["value"] = pointer(source, f["pointer"])
f["json_type"] = type(f["value"]).__name__
elif kind == "citation":
b["sources"][f["evidence"][0]]["sha256"] = "0" * 64
else:
f[{"default": "schema_default"}.get(kind, kind)] = "forged"
b["canonical_sha256"] = hashlib.sha256(canonical(source)).hexdigest()
review["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
actual = doc(source, review)["fixed_version"]
assert actual["matched"] is False and "fields" not in actual
MUTATIONS = (
[
"/links/0/1",
"/extra/frontendVersion",
"/nodes/2/widgets_values/0",
"/nodes/3/widgets_values/0",
"/nodes/0/properties/models/0/url",
"/nodes/7/outputs/0/links",
"/nodes/8/inputs/2/link",
]
+ [
f"/nodes/{i}/{suffix}"
for i in range(9)
for suffix in ["mode", "type", "properties/ue_properties/version"]
]
+ [f"/nodes/{i}/properties/{key}" for i in [0, 1, 4, 5, 6, 7, 8] for key in ["cnr_id", "ver"]]
)
@pytest.mark.parametrize("path", MUTATIONS)
def test_versions_providers_modes_edges_notes_and_model_metadata_fail_closed(path):
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
parent, _, key = path.rpartition("/")
target = pointer(source, parent)
target[int(key) if isinstance(target, list) else key] = "forged"
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(source, review)["fixed_version"]["matched"]
@pytest.mark.parametrize(
"key",
[
"source_path",
"source_sha1",
"source_sha256",
"notice",
"user_overview",
"versions",
"comments",
"flow",
"sources",
],
)
def test_editorial_and_identity_forgery_rejected(key):
review = copy.deepcopy(REVIEW)
review["fixed_version_document"][key] = "forged"
review["caller_digest"] = hashlib.sha256(
canonical(review["fixed_version_document"])
).hexdigest()
assert not doc(review=review)["fixed_version"]["matched"]
def test_real_document_and_frozen_old_thirteen_blocks():
d = doc()
assert d["fixed_version"]["matched"] and d["fixed_version"]["fields"] == BLOCK["fields"]
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
assert d["dependencies"]["check_state"] == "unchecked"
assert "fixed_version" not in doc(path="untrusted.json")
review = copy.deepcopy(REVIEW)
other = next(p for p in DEFINITIONS if p != PATH)
review["fixed_version_document"] = json.loads(
(REPO / "calliope-backend/src/calliope/comfyui/source_reviews.json").read_bytes()
)[other]["fixed_version_document"]
assert not doc(review=review)["fixed_version"]["matched"]
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(subprocess.check_output(["git", "show", "9dac1b4:" + 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")
birefnet = next(p for p in now if p.endswith("/birefnet_图像移除背景.json"))
now[birefnet].pop("fixed_version_document")
latent = next(p for p in now if p.endswith("/加载本地latent解码.json"))
now[latent].pop("fixed_version_document")
later = next(p for p in now if p.endswith("/截取视频每一帧.json"))
now[later].pop("fixed_version_document")
for added in [
p
for p in now
if p.endswith(
("视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json")
)
]:
now[added].pop("fixed_version_document")
reverse = next(p for p in now if p.endswith("/视频倒放.json"))
now[reverse].pop("fixed_version_document")
nlf = next(p for p in now if p.endswith("NLF_视频人物动作骨架获取.json"))
now[nlf].pop("fixed_version_document")
openpose = next(p for p in now if p.endswith("openpose-video-姿态检测.json"))
now[openpose].pop("fixed_version_document")
# SDPose is independently frozen against 7291887 in its own suite.
sdpose = next(p for p in now if p.endswith("sdpose_视频人物姿势骨架提取.json"))
now[sdpose].pop("fixed_version_document")
assert len(now) == 156 and len(DEFINITIONS) == 26
assert len([r for r in old.values() if "fixed_version_document" in r]) == 13
assert (
sum(
len(r["fixed_version_document"]["fields"])
for r in old.values()
if "fixed_version_document" in r
)
== 351
)
assert now[PATH].pop("fixed_version_document") == BLOCK
assert now == old
def test_exact_topology_nulls_conditions_and_reference_identity():
assert BLOCK["source_sha256"] == hashlib.sha256(RAW).hexdigest()
assert BLOCK["canonical_sha256"] == hashlib.sha256(canonical(SOURCE)).hexdigest()
assert SOURCE["links"] == [
[2, 3, 0, 1, 0, "CLIP"],
[3, 1, 0, 4, 0, "STRING"],
[5, 6, 0, 7, 0, "VIDEO"],
[7, 2, 0, 1, 1, "IMAGE"],
[8, 5, 0, 1, 3, "AUDIO"],
]
assert {n["id"]: n["mode"] for n in SOURCE["nodes"]} == {
1: 0,
3: 0,
4: 0,
8: 0,
9: 0,
2: 4,
5: 4,
6: 4,
7: 4,
}
assert len(BLOCK["fields"]) == 25 and len(BLOCK["comments"]) == 2
assert len([f for f in BLOCK["fields"] if f["role"] == "frontend"]) == 7
for n in BLOCK["comments"]:
assert pointer(SOURCE, n["pointer"]) == n["text"]
preview = [f for f in BLOCK["fields"] if f["node_id"] == "4"]
assert [f["value"] for f in preview] == [None, None, None]
text = [f for f in BLOCK["fields"] if f["node_id"] == "1"]
assert len(text) == 12 and "fixed" not in [f["value"] for f in text]
assert [f["value"] for f in text][-4:] == [0, 0, False, True]
assert text[9]["json_type"] == "int" and text[9]["schema_default"] == "0.0"
assert [f["field"] for f in text][3:10] == [
"sampling_mode." + n
for n in [
"temperature",
"top_k",
"top_p",
"min_p",
"repetition_penalty",
"seed",
"presence_penalty",
]
]
assert {e["commit"] for e in BLOCK["sources"]} == {
"52976f3ea33cc2312c7b5a32e1c7510b203eefb6",
"6a207bb52a887b1a70fbf654641ebc57710f97c7",
}
assert BLOCK["zero_saved_nodes"][0]["node_id"] == "7"
assert all(
t in "".join(BLOCK["flow"])
for t in [
"旁路",
"video的link=null",
"images的links=[]",
"null不替换默认",
"RETURN_TYPES",
"UE7.8",
]
)
assert not BLOCK["counts"]["final_prompt_constructed"]