"""Offline H3 saved-state contracts; never execute plugins or inspect local models.""" import copy import hashlib import json from pathlib import Path import pytest from scripts import audit_reference_conversion as audit from scripts.verify_frontend_sources import verify from calliope.comfyui.serialization_reference import H3_COMMIT, serialize_h3_node from calliope.comfyui.structure_reference import H3_PROFILE from calliope.comfyui.ui2api import convert, convert_reference REPO = Path(__file__).resolve().parents[2] PATH = next((REPO / "example_ComfyUI_workflows").rglob("Minimax-H3提示词增强.json")) def graph(): return json.loads(PATH.read_bytes()) def node(g, nid): return next(n for n in g["nodes"] if n["id"] == nid) def put(g, nid, field, value): n = node(g, nid) i = list(n["widgets_values_named"]).index(field) n["widgets_values"][i] = value n["widgets_values_named"][field] = value def closed(g): before = copy.deepcopy(g) r = convert_reference(g, H3_PROFILE) assert r.prompt is None and r.issues and r.reference_only and g == before assert all(i.get("pointer") is not None for i in r.issues) a = audit.profile_attempt(g, H3_PROFILE) assert a["status"] in ("rejected", "not_applicable") assert "candidate_prompt" not in a def test_real_source_complete_four_entities_saved_state_only(): assert hashlib.sha256(PATH.read_bytes()).hexdigest() == ( "ffea53b27d52c63aa9ec9c60144489cc4f9e5a44d9478bcc297be9d9008f1a4c" ) g = graph() before = copy.deepcopy(g) r = convert_reference(g, H3_PROFILE) assert r.prompt is not None, r.issues assert r.reference_only and g == before and convert(g).prompt is None assert set(r.prompt) == {"169", "170", "171", "172"} assert r.serialization_stage == "restored_saved_state_before_queue_hooks" writer = r.prompt["169"]["inputs"] assert len(writer) == 14 and "control_after_generate" not in writer assert writer["seed"] == 42 and writer["duration"] == 10 assert writer["model"] == node(g, 169)["widgets_values_named"]["model"] assert writer["prompt"] == node(g, 169)["widgets_values_named"]["prompt"] assert r.prompt["170"]["inputs"] == {"prompt": ["169", 0], "task": "T2VA", "duration": 5} assert r.prompt["171"]["inputs"] == {"source": ["170", 0]} assert r.prompt["172"]["inputs"] == {"source": ["170", 8]} fields = audit.h3_field_evidence(g, r.prompt) assert len(fields) == 19 assert sum(f["json_type"] == "list" for f in fields) == 3 shadowed = next(f for f in fields if f["node_id"] == "170" and f["field"] == "prompt") assert shadowed["shadowed_saved_pointers"] == [ "/nodes/1/widgets_values/0", "/nodes/1/widgets_values_named/prompt", ] assert all(f["corroborating_pointers"] for f in fields if f["json_type"] != "list") a = audit.profile_attempt(g, H3_PROFILE) assert a["candidate_sha256"] == audit.canonical_sha(r.prompt) assert a["assumptions"]["target_version_match"] == "unknown" @pytest.mark.parametrize( "field,value", [ ("model", "opaque model display string, no file lookup"), ("prompt", "opaque\ntext\u0000中文"), ("system_prompt", "opaque-system"), ("reference_material", "opaque-reference"), ("aspect_ratio", "opaque-ratio"), ("library_pick", '{"file":"not-read.json","id":"unresolved"}'), ("repeat_last", True), ("bypass", True), ("keep_model_loaded", True), ("greedy", False), ("seed", 0), ("seed", 4294967295), ("duration", 4), ("duration", 15), ("task", "I2VA"), ("task", "FL2VA"), ("task", "L2VA"), ("resolution", "9:16"), ], ) def test_writer_instance_changes_propagate_without_interpreting_text_or_running(field, value): g = graph() put(g, 169, field, value) r = convert_reference(g, H3_PROFILE) assert r.prompt is not None, r.issues actual = r.prompt["169"]["inputs"][field] assert type(actual) is type(value) and actual == value assert r.prompt["170"]["inputs"]["prompt"] == ["169", 0] @pytest.mark.parametrize( "field,value", [ ("task", "Ref2VA"), ("duration", 0.1), ("duration", 600.0), ("duration", 7.5), ("prompt", "saved value overridden by physical edge"), ], ) def test_check_float_and_union_placeholder_are_not_synthetic_media(field, value): g = graph() put(g, 170, field, value) r = convert_reference(g, H3_PROFILE) assert r.prompt is not None, r.issues assert r.prompt["170"]["inputs"][field] == (["169", 0] if field == "prompt" else value) assert "options" not in r.prompt["170"]["inputs"] assert "references.ref_0" not in r.prompt["170"]["inputs"] @pytest.mark.parametrize( "nid,field,value", [ (169, "seed", True), (169, "seed", 42.0), (169, "seed", -1), (169, "seed", 4294967296), (169, "greedy", 1), (169, "bypass", "false"), (169, "duration", 10.0), (169, "duration", 3), (169, "duration", 16), (169, "duration", "10"), (169, "model", ""), (169, "model", None), (169, "reference_material", []), (169, "library_pick", {}), (169, "task", "Ref2VA"), (169, "resolution", "1920x1080"), (169, "control_after_generate", "increment"), (169, "control_after_generate", "decrement"), (169, "control_after_generate", "randomize"), (169, "control_after_generate", None), (170, "task", "unknown"), (170, "duration", 0), (170, "duration", 600.1), (170, "duration", True), (170, "duration", float("inf")), ], ) def test_types_ranges_enums_and_unproved_queue_controls_close_whole_graph(nid, field, value): g = graph() put(g, nid, field, value) closed(g) @pytest.mark.parametrize("nid", [169, 170, 171, 172]) @pytest.mark.parametrize( "mutation", ["provider", "version", "aux", "mode", "ue", "hook", "extra-output"] ) def test_registration_identity_modes_hooks_and_output_schemas_are_exact(nid, mutation): g = graph() n = node(g, nid) if mutation == "provider": n["properties"]["cnr_id"] = "same-named-other-plugin" elif mutation == "version": n["properties"]["ver"] = "0.18.6" if nid < 171 else "0.38.0" elif mutation == "aux": n["properties"]["aux_id"] = "pytraveler/MiniMax-H3-Prompt-Rewriter-ComfyUI" elif mutation == "mode": n["mode"] = 4 elif mutation == "ue": n["properties"]["ue_properties"] = {"version": "7.7"} elif mutation == "hook": n["serializeValue"] = "unknown" else: n["outputs"].append({"type": "STRING", "links": None}) closed(g) @pytest.mark.parametrize("nid", [169, 170, 171, 172]) @pytest.mark.parametrize( "mutation", ["short", "long", "named-missing", "named-extra", "missing-map"] ) def test_dual_save_layout_is_required_even_for_empty_preview(nid, mutation): g = graph() n = node(g, nid) if mutation == "short": n["widgets_values"] = n["widgets_values"][:-1] if nid < 171 else None elif mutation == "long": n["widgets_values"].append("unexpected") elif mutation == "named-missing": if n["widgets_values_named"]: n["widgets_values_named"].pop(next(iter(n["widgets_values_named"]))) else: n["widgets_values_named"] = None elif mutation == "named-extra": n["widgets_values_named"]["preview"] = "legacy" else: n.pop("widgets_values_named") closed(g) @pytest.mark.parametrize("field", ["prompt", "seed", "library_pick", "control_after_generate"]) def test_named_positional_conflicts_are_not_resolved_by_precedence_guess(field): g = graph() node(g, 169)["widgets_values_named"][field] = "conflict" closed(g) def test_synthetic_instance_replacement_and_nonzero_check_slot_propagate(): g = graph() node(g, 169)["id"] = 900 g["links"][0][1] = 900 g["links"][2][2] = 7 node(g, 170)["outputs"][8]["links"] = None node(g, 170)["outputs"][7]["links"] = [338] r = convert_reference(g, H3_PROFILE) assert r.prompt is not None, r.issues assert "169" not in r.prompt assert r.prompt["170"]["inputs"]["prompt"] == ["900", 0] assert r.prompt["172"]["inputs"]["source"] == ["170", 7] @pytest.mark.parametrize( "mutation", [ "frontend", "missing-frontend", "input-type", "union-type", "duplicate-input", "bad-output-type", "bad-slot", "bad-edge-type", "duplicate-link", "dangling", "cycle", "subgraph", "root-ue", "unknown-node", "unknown-ref", "bad-widget", ], ) def test_unproven_structure_and_physical_types_fail_closed(mutation): g = graph() if mutation == "frontend": g["extra"]["frontendVersion"] = "1.53.6" elif mutation == "missing-frontend": g["extra"].pop("frontendVersion") elif mutation == "input-type": node(g, 170)["inputs"][2]["type"] = "FLOAT" elif mutation == "union-type": node(g, 170)["inputs"][1]["type"] = "IMAGE" elif mutation == "duplicate-input": node(g, 170)["inputs"].append(copy.deepcopy(node(g, 170)["inputs"][0])) elif mutation == "bad-output-type": node(g, 170)["outputs"][8]["type"] = "FLOAT" elif mutation == "bad-slot": g["links"][2][2] = 9 elif mutation == "bad-edge-type": g["links"][2][5] = "FLOAT" elif mutation == "duplicate-link": g["links"].append(g["links"][0]) elif mutation == "dangling": g["links"][0][1] = 999 elif mutation == "cycle": node(g, 169)["inputs"].append({"name": "prompt", "type": "STRING", "link": 339}) node(g, 170)["outputs"][0]["links"].append(339) g["links"].append([339, 170, 0, 169, 1, "STRING"]) elif mutation == "subgraph": g["definitions"] = {"subgraphs": [{"id": "unproven"}]} elif mutation == "root-ue": g["extra"]["ue_links"] = [[1, 2, 3]] elif mutation == "unknown-node": node(g, 169)["type"] = "SameNameUnregistered" elif mutation == "unknown-ref": node(g, 170)["inputs"][1]["name"] = "references.ref_1" else: node(g, 170)["inputs"][2]["widget"]["name"] = "seed" closed(g) def test_options_and_reference_union_cannot_accept_string_output_by_name(): for index in (0, 1): g = graph() node(g, 170)["inputs"][index]["link"] = 339 node(g, 169)["outputs"][1]["links"] = [339] g["links"].append([339, 169, 1, 170, index, "STRING"]) closed(g) def test_fixed_sources_are_cached_not_imported_and_include_registration(): r = verify(REPO / "docs/hel-892/h3-reference") assert r["target_version_match"] == "unknown" and not r["upstream_code_executed"] assert len(r["sources_verified"]) >= 40 assert H3_COMMIT in H3_PROFILE.replace("a59888df", H3_COMMIT) assert serialize_h3_node(node(graph(), 171)) == {} def test_full_library_old_25_attempts_gaps_eight_hashes_and_six_pairs_frozen(): old = json.loads((REPO / "docs/hel-892/grid-evidence/library.json").read_bytes()) baseline = tuple(old["profiles"]) assert len(baseline) == 25 and baseline == audit.REFERENCE_PROFILES[:25] before = audit.audit_library(REPO, baseline) now = audit.audit_library(REPO, baseline_profiles=baseline) assert old["gap_groups"] == before["gap_groups"] for a, b, c in zip(old["cases"], before["cases"], now["cases"]): assert a["source_path"] == b["source_path"] == c["source_path"] assert a["attempts"] == b["attempts"] == c["attempts"][:25] assert a.get("comparisons") == b.get("comparisons") == c.get("comparisons") if a["candidate_hashes"]: assert a["candidate_hashes"] == c["candidate_hashes"] target = PATH.relative_to(REPO).as_posix() assert now["coverage_delta"]["new_complete_paths"] == [target] assert len(now["coverage_delta"]["current_complete_paths"]) == 9 assert sum(r["status"] == "blocked" for r in now["cases"]) == 136 r = next(r for r in now["cases"] if r["source_path"] == target) assert r["comparison_status"] == "comparison_unavailable" and not r["execution_verified"]