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

218 lines
7.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""36 saved values and complete source/citation bindings cannot grant execution."""
from __future__ import annotations
import copy
import hashlib
import json
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/controlnet-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("ControlNet_aux_常用样例.json"))
RAW = (REPO / PATH).read_bytes()
SOURCE = json.loads(RAW)
REVIEW = review_for(PATH, hashlib.sha1(RAW).hexdigest())
BLOCK = REVIEW["fixed_version_document"]
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,
)
def pointer(obj, path):
for part in path.strip("/").split("/"):
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
return obj
@pytest.mark.parametrize("index", range(36))
def test_saved_positions_and_evidence(index):
f = BLOCK["fields"][index]
v = pointer(SOURCE, f["pointer"])
assert canonical(v) == canonical(f["value"]) and type(v).__name__ == f["json_type"]
n = next(n for n in SOURCE["nodes"] if str(n["id"]) == f["node_id"])
assert n["type"] == f["class_type"]
assert all(
f[k]
for k in [
"effect",
"label",
"schema_default",
"unit",
"limits",
"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())
@pytest.mark.parametrize("index", range(36))
@pytest.mark.parametrize("kind", ["value", "type", "pointer"])
def test_each_source_and_definition_change_is_refused(index, kind):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
f = r["fixed_version_document"]["fields"][index]
if kind == "pointer":
f["pointer"] = "/nodes/999/widgets_values/999"
else:
path = f["pointer"]
parent, _, key = path.rpartition("/")
target = pointer(s, parent)
target[int(key)] = (
str(target[int(key)])
if kind == "type" and not isinstance(target[int(key)], str)
else {"tampered": True}
)
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert doc(s, r)["fixed_version"]["matched"] is False
@pytest.mark.parametrize(
"kind",
[
"path",
"sha1",
"sha256",
"canonical",
"version",
"frontend",
"node",
"mode",
"edge",
"reroute",
"input",
"output",
"citation",
"definition",
"selector",
"preview",
"model",
"upload",
"companion",
],
)
def test_source_identity_graph_and_recomputed_hash_refused(kind):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = r["fixed_version_document"]
if kind == "path":
b["source_path"] = "other.json"
elif kind == "sha1":
r["source_sha"] = "0" * 40
elif kind == "sha256":
b["source_sha256"] = "0" * 64
elif kind == "canonical":
b["canonical_sha256"] = "0" * 64
elif kind == "version":
s["nodes"][0]["properties"]["ver"] = "1.1.6"
elif kind == "frontend":
s.setdefault("extra", {})["frontendVersion"] = "1.53.10"
elif kind == "node":
s["nodes"][0]["id"] = 999
elif kind == "mode":
s["nodes"][0]["mode"] = 2
elif kind == "edge":
s["links"][0][2] = 1
elif kind == "reroute":
s["links"][16][1] = 38
elif kind == "input":
s["nodes"][0]["inputs"][0]["link"] = 45
elif kind == "output":
s["nodes"][0]["outputs"][0]["type"] = "MASK"
elif kind == "citation":
b["sources"][0]["lines"] = [1, 2]
elif kind == "definition":
b["fields"][0]["effect"] = "篡改"
elif kind == "selector":
next(n for n in s["nodes"] if n["id"] == 18)["widgets_values"][0] = "none"
elif kind == "preview":
s["nodes"][1]["widgets_values"] = ["extra"]
elif kind == "model":
s["nodes"][0]["widgets_values"][0] = "different.pth"
elif kind == "upload":
next(n for n in s["nodes"] if n["id"] == 2)["widgets_values"][1] = "other"
else:
b["companion_difference"]["scalar_matches"] = 0
if kind != "canonical":
b["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
r["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
assert doc(s, r)["fixed_version"]["matched"] is False
def test_graph_specific_semantics_and_companion():
d = doc()
b = d["fixed_version"]
assert b["matched"] and len(b["fields"]) == 36
assert b["counts"] == dict(
saved_fields=36, final_scalars=34, linked_saved_fields=0, frontend_only=2, physical_edges=27
)
assert len(SOURCE["nodes"]) == 26 and len(SOURCE["links"]) == 27
saved = {
f"/nodes/{i}/widgets_values/{j}"
for i, n in enumerate(SOURCE["nodes"])
for j, _ in enumerate(n.get("widgets_values", []))
}
assert saved == {f["pointer"] for f in b["fields"]}
assert sum(f["role"] == "frontend" for f in b["fields"]) == 2
assert "前端未声明" in b["notice"] and "假设" in b["notice"]
ep = b["resolved_endpoints"]
assert len(ep) == 23 and sum(len(e["source_chain"]) > 1 for e in ep) == 9
api = json.loads((REPO / "docs/hel-892/supplied-api/01.api.json").read_bytes())
for e in ep:
assert api[e["node_id"]]["inputs"][e["field"]] == e["endpoint"]
for hop in e["source_chain"]:
assert pointer(SOURCE, hop["pointer"]) == hop["edge"]
assert len(b["companion_difference"]["title_differences"]) == 13
f = {(f["node_id"], f["field"]): f for f in b["fields"]}
assert {i: f[str(i), "preprocessor"]["value"] for i in [1, 12, 18, 19, 20, 21, 33, 34]} == {
1: "DepthAnythingPreprocessor",
12: "CannyEdgePreprocessor",
18: "AnyLineArtPreprocessor_aux",
19: "LineArtPreprocessor",
20: "AnimeLineArtPreprocessor",
21: "LineartStandardPreprocessor",
33: "DWPreprocessor",
34: "AnimalPosePreprocessor",
}
assert f["4", "ckpt_name"]["value"] == "depth_anything_vitl14.pth"
assert [f[str(i), "direction"]["value"] for i in [28, 29, 30]] == ["right", "right", "down"]
text = "".join(b["flow"])
assert all(
t in text
for t in ["2×2", "3/5/11/13/22/36/37", "temp", "ui.images", "两素材", "9经转接", "13处标题"]
)
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
assert d["dependencies"]["check_state"] == "unchecked"
def test_other_documents_and_block_swaps():
assert len(DEFINITIONS) == 26
for path in DEFINITIONS:
if path == PATH:
continue
raw = (REPO / path).read_bytes()
review = review_for(path, hashlib.sha1(raw).hexdigest())
assert build_document(path, json.loads(raw), None, None, [], review)["fixed_version"][
"matched"
]
r = copy.deepcopy(REVIEW)
r["fixed_version_document"] = review["fixed_version_document"]
assert not doc(review=r)["fixed_version"]["matched"]