"Exact graph binding, all saved positions, dual scopes and closed eligibility." import copy import hashlib import json import runpy import sys 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/qwen21-alpha-document-evidence") PATH = ( "example_ComfyUI_workflows/3-图像生成/Qwen-Image-2.1/Qwen_image2.1_文生_多图编辑_透明背景.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, p): for x in p.strip("/").split("/"): obj = obj[int(x)] if isinstance(obj, list) else obj[x] return obj def doc(s=SOURCE, r=REVIEW): return build_document(PATH, s, None, None, [], r) @pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"]) def test_each_saved_position_and_citation(f): assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"]) assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"] assert canonical(pointer(SOURCE, f["named_pointer"])) == canonical(f["named_value"]) if f["node_id"] != "192": assert canonical(f["value"]) == canonical(f["named_value"]) assert all( f[k] for k in ["label", "unit", "limits", "schema_default", "effect", "execution_note"] ) if f["input_pointer"].startswith("/"): assert pointer(SOURCE, f["input_pointer"])["name"] == f["input_name"] for i in f["evidence"]: e = BLOCK["sources"][i] 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()) @pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"]) @pytest.mark.parametrize("variant", ["source", "text", "named"]) def test_caller_rehash_cannot_expand_trust(f, variant): s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) if variant == "text": r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged" else: selected_pointer = f["named_pointer"] if variant == "named" else f["pointer"] p, _, key = selected_pointer.rpartition("/") parent = pointer(s, p) parent[int(key) if isinstance(parent, list) else key] = "forged" r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest() assert not doc(s, r)["fixed_version"]["matched"] @pytest.mark.parametrize("key", ["extra", "links", "id", "groups", "version", "definitions"]) def test_whole_graph_mutations_fail_closed(key): source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) source[key] = {"subgraphs": []} if key == "definitions" else "forged" review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256( canonical(source) ).hexdigest() assert not doc(source, review)["fixed_version"]["matched"] @pytest.mark.parametrize("key", ["mode", "properties", "inputs", "outputs"]) @pytest.mark.parametrize("index", [4, 5, 7]) def test_node_identity_and_state_fail_closed(key, index): source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) source["nodes"][index][key] = "forged" review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256( canonical(source) ).hexdigest() assert not doc(source, review)["fixed_version"]["matched"] def test_path_and_source_binding_cannot_be_extended(): from calliope.comfyui.fixed_document import fixed_document_for assert fixed_document_for(PATH + "forged", SOURCE, REVIEW) is None review = copy.deepcopy(REVIEW) review["source_sha"] = "forged" assert not doc(SOURCE, review)["fixed_version"]["matched"] @pytest.mark.parametrize("suffix", [b"\n", b" ", b"\t"]) def test_original_byte_identity_rejects_equivalent_json(suffix): altered = RAW + suffix assert json.loads(altered) == SOURCE assert review_for(PATH, hashlib.sha1(altered).hexdigest()) is None @pytest.mark.parametrize("key", ["user_overview", "source_sha256", "sources", "flow", "counts"]) def test_editorial_or_citation_rehash_cannot_replace_code_owned_pin(key): source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) review["fixed_version_document"][key] = "forged" review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256( canonical(source) ).hexdigest() assert not doc(source, review)["fixed_version"]["matched"] @pytest.mark.parametrize("key", ["properties", "inputs", "outputs", "mode"]) @pytest.mark.parametrize("nid", [195, 170, 179, 39]) def test_control_identity_and_state_forgery_fail_closed(nid, key): s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) next(n for n in s["nodes"] if n["id"] == nid)[key] = "forged" r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest() assert not doc(s, r)["fixed_version"]["matched"] def test_exact_build_and_qualification(): assert canonical(runpy.run_path(str(OUT / "build_definition.py"))["build"]()) == canonical( BLOCK ) assert len(DEFINITIONS) == 98 and doc()["fixed_version"]["matched"] assert BLOCK["counts"]["saved_fields"] == 64 and len(BLOCK["comments"]) == 0 assert len(BLOCK["boundaries"]) == 0 and len(BLOCK["zero_saved_nodes"]) == 10 assert not doc()["status"]["doc_done"] and not doc()["status"]["exec_verified"] sys.path.insert(0, str(OUT)) try: runpy.run_path(str(OUT / "verify_sources.py"))["main"]() finally: sys.path.remove(str(OUT)) def test_raw_source_version_mismatch_is_fail_closed(): source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW) source["nodes"][1]["properties"]["ver"] = "other-plugin-version" review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256( canonical(source) ).hexdigest() assert not doc(source, review)["fixed_version"]["matched"] def test_no_standard_note_and_full_showtext_history(): assert BLOCK["comments"] == [] and doc()["source_notes"] == [] node = next(n for n in SOURCE["nodes"] if n["id"] == 111) text = node["widgets_values"][0] assert text == node["widgets_values_named"]["text_0"] and len(text) == 1425 assert ( hashlib.sha256(text.encode()).hexdigest() == "7db60748fae3dabcc080f03330ea414362098a68f6c54e99e29aec56e2169501" ) assert text.count("\n") == 0 and text.count("\\n") == 0 and not text.endswith("\n") assert any(f["node_id"] == "111" and f["value"] == text for f in BLOCK["fields"]) ns = {n["id"]: n for n in SOURCE["nodes"]} assert ns[179]["widgets_values"][5] is True assert all(ns[n]["mode"] == 4 for n in [2, 3, 4, 5, 6, 182, 105]) assert any("不能证明RGBA" in v for v in BLOCK["steps"]) def test_current_alpha_version_values_and_complete_history_forms(): assert BLOCK["counts"]["root_saved_fields"] == 64 assert BLOCK["counts"]["internal_saved_fields"] == 0 fields = {(f["node_id"], f["field"]): f for f in BLOCK["fields"]} history = fields["192", "rgthree_comparer"] assert len(history["value"]) == len(history["named_value"]["images"]) == 1 assert all(v["img"] == {} for v in history["named_value"]["images"]) assert all("img" not in v for v in history["value"]) assert ( json.loads(history["effect"].split("具名保存完整形态(与位置历史分列):")[1]) == history["named_value"] ) assert len(fields["196", "value"]["value"]) == 28 assert len(fields["182", "image"]["value"]) == 119 assert fields["12", "seed"]["value"] == 615818533601009 assert fields["12", "steps"]["value"] == 30 assert fields["179", "种子值"]["value"] == 2037041514 assert fields["39", "自定宽度"]["value"] == 32 assert fields["195", "device"]["schema_default"] == "'auto'" assert fields["195", "dtype"]["schema_default"] == "'default'" assert fields["118", "latent_width"]["schema_default"].startswith("未知") assert fields["179", "用户提示词"]["schema_default"].startswith("未知") assert fields["38", "filename_prefix"]["value"] == "image/Qwen_Image_2.1" ns = {n["id"]: n for n in SOURCE["nodes"]} assert 200 not in ns and 201 not in ns assert ns[182]["mode"] == ns[105]["mode"] == 4 assert ns[36]["properties"]["text"] == "Qwen Image 2.1 文生/图像编辑整合版" assert any("mode2禁用/忽略保存态" in v for v in BLOCK["flow"]) def test_old_reviews_and_qualification_are_unchanged(): import subprocess name = "calliope-backend/src/calliope/comfyui/source_reviews.json" previous = json.loads( subprocess.check_output( ["git", "show", "cbfe0373c8315666d2df21fa49769ad2f4db4dcd:" + name], cwd=REPO ) ) current = json.loads((REPO / name).read_bytes()) for path, review in previous.items(): if path != PATH: assert current[path] == review else: current_without = dict(current[path]) current_without.pop("fixed_version_document") assert current_without == review old = [r["fixed_version_document"] for r in previous.values() if "fixed_version_document" in r] assert len(old) == 97 and sum(len(b["fields"]) for b in old) == 5187