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

218 lines
7.5 KiB
Python

"RTX saved values, physical links, unknown identities and fail-closed binding."
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/rtx-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("/高清放大/RTX_image_upscale.json"))
SOURCE = json.loads((REPO / PATH).read_bytes())
REVIEW = review_for(PATH, hashlib.sha1((REPO / PATH).read_bytes()).hexdigest())
BLOCK = REVIEW["fixed_version_document"]
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(source=None, review=None):
return build_document(
PATH,
SOURCE if source is None else source,
None,
None,
[],
REVIEW if review is None else review,
)
def refuse(s, r):
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("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
def test_each_saved_value_type_pointer_and_exact_evidence(f):
v = pointer(SOURCE, f["pointer"])
assert canonical(v) == canonical(f["value"]) and type(v).__name__ == f["json_type"]
for k in [
"label",
"type",
"schema_default",
"unit",
"limits",
"effect",
"execution_note",
"role",
]:
assert f[k]
for k in f["evidence"]:
e = BLOCK["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 or e["git_commit_binding"] == "unknown"
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
@pytest.mark.parametrize("variant", ["value", "type", "text"])
def test_each_saved_position_refuses_forged_source_or_definition(f, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
b = r["fixed_version_document"]
bf = next(x for x in b["fields"] if x["pointer"] == f["pointer"])
if variant == "text":
bf["limits"] = "forged"
else:
parent, _, key = f["pointer"].rpartition("/")
target = pointer(s, parent)
target[int(key) if isinstance(target, list) else key] = (
"forged" if variant == "type" else None
)
bf["value"] = pointer(s, f["pointer"])
bf["json_type"] = type(bf["value"]).__name__
refuse(s, r)
@pytest.mark.parametrize("i", range(4))
@pytest.mark.parametrize("variant", ["mode", "identity"])
def test_each_node_mode_and_identity_bound(i, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = s["nodes"][i]
if variant == "mode":
n["mode"] = 0 if n["mode"] == 4 else 4
else:
n["properties"]["ver"] = "forged"
refuse(s, r)
@pytest.mark.parametrize("i", range(4))
def test_each_physical_endpoint_bound_both_directions(i):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
s["links"][i][3] = 99999
refuse(s, r)
@pytest.mark.parametrize(
"variant",
[
"empty",
"missing",
"slide",
"ue",
"frontend",
"summary",
"citation",
"source_sha",
"path",
"layout",
"dynamic",
"mask",
],
)
def test_special_shapes_and_identity_fail_closed(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
ns = {n["id"]: n for n in s["nodes"]}
b = r["fixed_version_document"]
if variant == "empty":
ns[7]["widgets_values"] = []
elif variant == "missing":
del ns[7]["widgets_values"]
elif variant == "slide":
ns[7]["properties"]["comparer_mode"] = "Click"
elif variant == "ue":
ns[1]["properties"]["ue_properties"]["version"] = "forged"
elif variant == "frontend":
s["extra"]["frontendVersion"] = "forged"
elif variant == "summary":
b["user_overview"] = "forged"
elif variant == "citation":
b["sources"][0]["sha256"] = "0" * 64
elif variant == "source_sha":
b["source_sha1"] = "0" * 40
elif variant == "path":
b["source_path"] = "forged"
elif variant == "layout":
ns[1]["pos"][0] += 1
elif variant == "dynamic":
ns[1]["inputs"][2]["name"] = "width"
elif variant == "mask":
ns[2]["outputs"][1]["links"] = [1]
refuse(s, r)
def test_independent_inventory_edges_and_reference_conditions():
ns = {n["id"]: n for n in SOURCE["nodes"]}
assert len(ns) == 4 and len(SOURCE["links"]) == 4 and all(n["mode"] == 0 for n in ns.values())
assert ns[1]["widgets_values"] == ["scale by multiplier", 2, "ULTRA"]
assert type(ns[1]["widgets_values"][1]) is int
assert ns[7]["widgets_values"] == [[]] and ns[7]["properties"]["comparer_mode"] == "Slide"
assert not any(k in ns[7]["properties"] for k in ["cnr_id", "aux_id", "ver"])
assert ns[2]["widgets_values"] == ["ComfyUI_01117_.png", "image"] and ns[3][
"widgets_values"
] == ["ComfyUI"]
assert ns[2]["outputs"][1]["links"] is None
assert ns[1]["inputs"][2]["type"] == "FLOAT" and ns[1]["inputs"][2]["link"] is None
assert SOURCE["links"] == [
[1, 2, 0, 1, 0, "IMAGE"],
[2, 1, 0, 3, 0, "IMAGE"],
[5, 1, 0, 7, 1, "IMAGE"],
[6, 2, 0, 7, 0, "IMAGE"],
]
for e, src, slot, dst, inp, _ in SOURCE["links"]:
assert e in ns[src]["outputs"][slot]["links"] and ns[dst]["inputs"][inp]["link"] == e
assert len(BLOCK["fields"]) == len({f["pointer"] for f in BLOCK["fields"]}) == 7
assert sum(isinstance(f["value"], list) for f in BLOCK["fields"]) == 1
assert BLOCK["comments"] == [] and not doc()["source_notes"]
assert doc()["fixed_version"]["matched"] and not BLOCK["counts"]["final_prompt_constructed"]
assert doc()["dependencies"]["check_state"] == "unchecked"
assert doc()["status"] == dict(imported=True, doc_done=False, exec_verified=False)
for word in [
"原图A",
"放大B",
"round",
"8",
"Git绑定未知",
"MAX_PIXELS",
"CUDA",
"temp",
"output",
"UE7.7",
"缺version",
"[[]]",
]:
assert word in " ".join(BLOCK["flow"])
def test_old_28_review_objects_unchanged():
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(subprocess.check_output(["git", "show", "82621b3:" + resource], cwd=REPO))
now = json.loads((REPO / resource).read_bytes())
now[PATH].pop("fixed_version_document")
assert now == old and len(DEFINITIONS) == 29
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
assert len(blocks) == 28 and sum(len(b["fields"]) for b in blocks) == 888
assert (
sum(
len(
build_document(p, json.loads((REPO / p).read_bytes()), None, None, [], r)[
"source_notes"
]
)
for p, r in old.items()
if "fixed_version_document" in r
)
== 21
)
assert review_for(PATH, "0" * 40) is None