116 lines
4.7 KiB
Python
116 lines
4.7 KiB
Python
"""Four-reference trust rejects rewritten prompt, file and execution assumptions."""
|
|
|
|
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/video_minimax_h3_r2v_4ref_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_original_scalar_type_and_unique_named_pointer(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",
|
|
[
|
|
"trim",
|
|
"delimiter",
|
|
"filename",
|
|
"fourth_filename",
|
|
"swap_references",
|
|
"missing_fourth",
|
|
"fifth_reference",
|
|
"primitive_type",
|
|
"latent_index",
|
|
"reference_key",
|
|
"save_true",
|
|
"first_frame",
|
|
"title",
|
|
"mode",
|
|
],
|
|
)
|
|
def test_mutated_source_never_inherits_trust(mutation):
|
|
g = copy.deepcopy(SOURCE)
|
|
if mutation == "trim":
|
|
g["160"]["inputs"]["string_a"] = g["160"]["inputs"]["string_a"].strip()
|
|
elif mutation == "delimiter":
|
|
g["160"]["inputs"]["delimiter"] = ""
|
|
elif mutation == "filename":
|
|
g["148"]["inputs"]["image"] = "another.png"
|
|
elif mutation == "fourth_filename":
|
|
g["156"]["inputs"]["image"] = g["155"]["inputs"]["image"]
|
|
elif mutation == "swap_references":
|
|
g["178"]["inputs"]["ref_images.ref_image_0"] = ["149", 0]
|
|
g["178"]["inputs"]["ref_images.ref_image_1"] = ["155", 0]
|
|
g["178"]["inputs"]["ref_images.ref_image_2"] = ["156", 0]
|
|
g["178"]["inputs"]["ref_images.ref_image_3"] = ["148", 0]
|
|
elif mutation == "missing_fourth":
|
|
g["178"]["inputs"].pop("ref_images.ref_image_3")
|
|
elif mutation == "fifth_reference":
|
|
g["178"]["inputs"]["ref_images.ref_image_4"] = ["156", 0]
|
|
elif mutation == "primitive_type":
|
|
g["132"]["inputs"]["value"] = 5.0
|
|
elif mutation == "latent_index":
|
|
g["173"]["inputs"]["latent_image"][1] = 0
|
|
elif mutation == "reference_key":
|
|
g["178"]["inputs"]["ref_images"] = {
|
|
"ref_image_0": g["178"]["inputs"].pop("ref_images.ref_image_0")
|
|
}
|
|
elif mutation == "save_true":
|
|
g["176"]["inputs"]["save_output"] = True
|
|
elif mutation == "first_frame":
|
|
g["178"]["inputs"]["first_frame"] = None
|
|
elif mutation == "title":
|
|
g["148"]["_meta"]["title"] = "forged"
|
|
else:
|
|
g["173"]["mode"] = 0
|
|
assert not document(g)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_editorial_rehash_cannot_replace_code_pin_or_enable_execution():
|
|
r = copy.deepcopy(REVIEW)
|
|
r["fixed_version_document"]["fields"][0]["value"] = "forged"
|
|
assert not document(review=r)["fixed_version"]["matched"]
|
|
assert DEFINITIONS[PATH] == hashlib.sha256(canonical(BLOCK)).hexdigest()
|
|
assert not document()["status"]["doc_done"] and not document()["status"]["exec_verified"]
|
|
|
|
|
|
def test_single_sampler_four_references_and_original_whitespace_boundaries():
|
|
assert [k for k, n in SOURCE.items() if n["class_type"] == "SamplerCustomAdvanced"] == ["173"]
|
|
assert SOURCE["173"]["inputs"]["latent_image"] == ["178", 1]
|
|
assert SOURCE["172"]["inputs"]["conditioning"] == ["178", 0]
|
|
assert SOURCE["178"]["inputs"]["ref_images.ref_image_0"] == ["148", 0]
|
|
assert SOURCE["178"]["inputs"]["ref_images.ref_image_1"] == ["149", 0]
|
|
assert SOURCE["178"]["inputs"]["ref_images.ref_image_2"] == ["155", 0]
|
|
assert SOURCE["178"]["inputs"]["ref_images.ref_image_3"] == ["156", 0]
|
|
assert SOURCE["156"]["inputs"]["image"] == "CharSheet_00003_.png"
|
|
assert SOURCE["155"]["inputs"]["image"] == "CharSheet_00008_.png"
|
|
assert SOURCE["149"]["inputs"]["image"] == "CharSheet_00005_.png"
|
|
assert SOURCE["178"]["inputs"]["audio_vae"] == ["165", 0]
|
|
assert SOURCE["160"]["inputs"]["string_a"].endswith(" ")
|
|
assert SOURCE["160"]["inputs"]["delimiter"] == " " and SOURCE["138"]["inputs"]["value"] == ""
|
|
assert SOURCE["176"]["inputs"]["save_output"] is False
|
|
assert BLOCK["sources"] == BLOCK["comments"] == BLOCK["boundaries"] == []
|