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

77 lines
3.0 KiB
Python

"""API reference trust rejects forged identity, shape, values and connections."""
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
ROOT = Path(__file__).resolve().parents[2]
PATH = "example_ComfyUI_workflows/Krea2_t2i_20260818_API.json"
RAW = (ROOT / PATH).read_bytes()
SOURCE = json.loads(RAW)
REVIEW = review_for(PATH, hashlib.sha1(RAW).hexdigest())
BLOCK = REVIEW["fixed_version_document"]
def document(source=SOURCE, review=REVIEW):
return build_document(PATH, source, None, None, [], review)
@pytest.mark.parametrize("field", BLOCK["fields"], ids=lambda f: f["pointer"])
def test_api_named_input_preserves_original_typed_value_once(field):
original = SOURCE[field["node_id"]]["inputs"][field["field"]]
assert type(original).__name__ == field["json_type"]
assert canonical(original) == canonical(field["value"])
assert field["pointer"] == f"/{field['node_id']}/inputs/{field['field']}"
assert not {"named_value", "named_pointer", "widget_index"}.intersection(field)
assert document()["fixed_version"]["matched"]
@pytest.mark.parametrize(
"mutation", ["scalar", "type", "edge", "title", "mode", "identity", "widget", "colon"]
)
def test_mutated_api_dictionary_cannot_inherit_trust(mutation):
source = copy.deepcopy(SOURCE)
if mutation == "scalar":
source["145"]["inputs"]["width"] = 1024
elif mutation == "type":
source["146"]["inputs"]["cfg"] = 1.0
elif mutation == "edge":
source["172"]["inputs"]["model"] = ["159", 0]
elif mutation == "title":
source["29"]["_meta"]["title"] = "forged"
elif mutation == "mode":
source["145"]["mode"] = 0
elif mutation == "identity":
source["145"]["properties"] = {"ver": "0.25.0"}
elif mutation == "widget":
source["145"]["widgets_values"] = [1280, 720, 1]
else:
source["171"] = source.pop("171:153")
assert not document(source)["fixed_version"]["matched"]
def test_rehashed_editorial_cannot_replace_code_pin():
review = copy.deepcopy(REVIEW)
review["fixed_version_document"]["fields"][0]["value"] = "forged"
assert not document(review=review)["fixed_version"]["matched"]
assert DEFINITIONS[PATH] == hashlib.sha256(canonical(BLOCK)).hexdigest()
assert not document()["status"]["doc_done"]
assert not document()["status"]["exec_verified"]
def test_original_model_and_text_branch_connections_remain_distinct():
assert SOURCE["146"]["inputs"]["model"] == ["159", 0]
assert SOURCE["172"]["inputs"]["model"] == ["152", 0]
assert SOURCE["171:163"]["inputs"]["switch"] == SOURCE["159"]["inputs"]["switch"] == ["160", 0]
assert SOURCE["171:158"]["inputs"]["switch"] == ["161", 0]
assert all(
n["scope"] == "root" and not n["implementation_verified"] for n in BLOCK["node_bindings"]
)
assert BLOCK["sources"] == BLOCK["comments"] == BLOCK["boundaries"] == []