"""ASR saved fields, virtual provenance, exact source citations and fail-closed bindings.""" from __future__ import annotations import ast 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 REPO = Path(__file__).resolve().parents[2] OUT = REPO / "docs/hel-892/asr-document-evidence" PATHS = [p for p in DEFINITIONS if "/1-语音生成/Qwen3-ASR-" in p] def data(path): raw = (REPO / path).read_bytes() return json.loads(raw), copy.deepcopy(review_for(path, hashlib.sha1(raw).hexdigest())) def block(path, source=None, review=None): s, r = data(path) return build_document( path, s if source is None else source, None, None, [], r if review is None else review )["fixed_version"] @pytest.mark.parametrize("path", PATHS) def test_saved_positions_and_original_citations(path): s, r = data(path) d = block(path) sub = "Subtitle" in path assert d["matched"] and len(d["fields"]) == (30 if sub else 19) assert len(s["nodes"]) == (7 if sub else 6) assert d["counts"]["physical_edges"] == len(s["links"]) == (6 if sub else 4) assert d["counts"]["final_scalars"] == (24 if sub else 16) assert "未声明" in d["notice"] and "假设" in d["notice"] assert hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest() == DEFINITIONS[path] saved = { f"/nodes/{i}/widgets_values/{j}": v for i, n in enumerate(s["nodes"]) if n["type"] != "MarkdownNote" for j, v in enumerate(n.get("widgets_values", [])) } assert set(saved) == {f["pointer"] for f in d["fields"]} for f in d["fields"]: assert canonical(saved[f["pointer"]]) == canonical(f["value"]) assert type(saved[f["pointer"]]).__name__ == f["json_type"] assert all( f[k] for k in ["label", "effect", "schema_default", "unit", "limits", "evidence"] ) for i in f["evidence"]: ev = d["sources"][i] raw = (OUT / ev["local_file"]).read_bytes() assert hashlib.sha256(raw).hexdigest() == ev["sha256"] assert len(ev["commit"]) == 40 and ev["commit"] in ev["url"] assert 1 <= ev["lines"][0] <= ev["lines"][1] <= len(raw.decode().splitlines()) doc = build_document(path, s, None, None, [], r) assert not doc["status"]["doc_done"] and doc["dependencies"]["check_state"] == "unchecked" if not sub: assert len(doc["source_notes"]) == 1 note = next(n for n in s["nodes"] if n["type"] == "MarkdownNote") assert canonical(doc["source_notes"][0]["content"]) == canonical(note["widgets_values"]) assert not any("named_pointer" in f for f in d["fields"]) # no dual saved array in these graphs def test_two_asr_branches_and_subtitle_semantics(): d = block(PATHS[0]) f = {(x["node_id"], x["field"]): x for x in d["fields"]} assert f["32", "hints"]["value"] == "" assert ( f["34", "hints"]["value"] == "Important terms: ComfyUI, Qwen3-ASR\nUse exact casing for these terms." ) assert f["32", "unload_models"]["value"] is True assert f["34", "unload_models"]["value"] is True assert "32.audio" in "".join(d["flow"]) and "34.audio" in "".join(d["flow"]) d = block(PATHS[1]) f = {(x["node_id"], x["field"]): x for x in d["fields"]} assert ( f["9", "attention"]["value"] == "auto" and f["9", "forced_aligner"]["value"] == "Qwen/Qwen3-ForcedAligner-0.6B" ) assert f["9", "hints"]["role"] == "linked" and f["7", "hints"]["role"] == "virtual" assert f["9", "hints"]["value"] == f["7", "hints"]["value"] assert "函数回退60" in f["9", "max_chars"]["schema_default"] assert f["9", "output_format"]["value"] == "none" and f["9", "output_path"]["value"] == "" assert "不写字幕文件" in f["9", "output_path"]["effect"] assert all( token in "".join(d["flow"]) for token in [ "9槽0 TEXT → 4.source", "9槽1 SUBTITLES → 2.source", "9槽2 LANUGAGE → 3.source", "9槽3 OUTPUT_PATH → 1.source", "三个空串", "normalize_text=true", "均无-hf", ] ) api = json.loads((REPO / "docs/hel-892/supplied-api/02.api.json").read_bytes()) c = d["companion_difference"] assert ( c["actual_export_node9_inputs"] == api["9"]["inputs"] and api["9"]["inputs"]["normalize_text"] is True ) assert c["actual_export_preview_sources"] == { k: api[k]["inputs"]["source"] for k in ["1", "2", "3", "4"] } assert "normalize_text" not in {x["field"] for x in d["fields"]} assert c["source_export_equivalence_verified"] is False and c["exported_at"] is None @pytest.mark.parametrize("path", PATHS) def test_preview_recovery_and_unrestored_nulls(path): d = block(path) for node in [n for n in data(path)[0]["nodes"] if n["type"] == "PreviewAny"]: fields = [f for f in d["fields"] if f["node_id"] == str(node["id"])] assert [f["value"] for f in fields] == [None, None, None] assert [f["field"] for f in fields] == [ "previewMode", "saved_position_1", "saved_position_2", ] assert "previewMode=null" in fields[0]["execution_note"] assert all("未恢复" in f["schema_default"] for f in fields[1:]) assert all("源保存位置" in f["execution_note"] for f in fields[1:]) @pytest.mark.parametrize("path", PATHS) @pytest.mark.parametrize( "change", [ "swap", "source_path", "source_sha", "node", "mode", "version", "frontend", "widget", "edge", "output", "note_or_primitive", "citation", "companion", "text", "model", "type", ], ) def test_untrusted_mutations_and_recomputed_digest_rejected(path, change): s, r = data(path) b = r["fixed_version_document"] if change == "swap": r["fixed_version_document"] = data(next(p for p in PATHS if p != path))[1][ "fixed_version_document" ] elif change == "source_path": b["source_path"] = "other/" + path elif change == "source_sha": r["source_sha"] = "0" * 40 elif change == "node": s["nodes"][0]["id"] = 999 elif change == "mode": s["nodes"][0]["mode"] = 2 elif change == "version": s["nodes"][0]["properties"]["ver"] = "0.30.0" elif change == "frontend": s["extra"]["frontendVersion"] = "1.53.10" elif change == "widget": s["nodes"][0]["inputs"][0]["widget"] = {"name": "wrong"} elif change == "edge": s["links"][0][2] = 99 elif change == "output": next(n for n in s["nodes"] if n["type"].startswith("AILab"))["outputs"][0]["name"] = "wrong" elif change == "note_or_primitive": next(n for n in s["nodes"] if n["type"] in ("PrimitiveNode", "MarkdownNote"))[ "widgets_values" ][0] += "changed" elif change == "citation": b["sources"][0]["lines"] = [1, 2] elif change == "companion": b["companion_difference"] = {"export_sha256": "0" * 64} elif change == "text": b["fields"][0]["effect"] = "unreviewed" elif change == "model": next(n for n in s["nodes"] if n["type"].startswith("AILab"))["widgets_values"][0] = "other" else: next(n for n in s["nodes"] if n["type"].startswith("AILab"))["widgets_values"][-1] = 1 assert not block(path, s, r)["matched"] b["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest() assert not block(path, s, r)["matched"] @pytest.mark.parametrize("path", PATHS) def test_every_saved_value_type_and_pointer_cannot_be_changed(path): s, r = data(path) for f in r["fixed_version_document"]["fields"]: other = copy.deepcopy(s) bits = f["pointer"].split("/") node = other["nodes"][int(bits[2])] node["widgets_values"][int(bits[4])] = "changed" if f["value"] is None else None rr = copy.deepcopy(r) assert not block(path, other, rr)["matched"] rr["fixed_version_document"]["canonical_sha256"] = hashlib.sha256( canonical(other) ).hexdigest() assert not block(path, other, rr)["matched"] rr = copy.deepcopy(r) rr["fixed_version_document"]["fields"][r["fixed_version_document"]["fields"].index(f)][ "pointer" ] = "/nodes/999" assert not block(path, s, rr)["matched"] def test_schema_positions_proved_by_ast_without_running_plugin(): tree = ast.parse((REPO / "docs/hel-892/structure-reference/qwen-b037d7be.py").read_text()) for class_name, path, node_id in [ ("AILab_Qwen3ASR", PATHS[0], "32"), ("AILab_Qwen3ASRSubtitle", PATHS[1], "9"), ]: cls = next(c for c in tree.body if isinstance(c, ast.ClassDef) and c.name == class_name) method = next( c for c in cls.body if isinstance(c, ast.FunctionDef) and c.name == "INPUT_TYPES" ) ret = next(c for c in method.body if isinstance(c, ast.Return)).value optional = next( v for k, v in zip(ret.keys, ret.values) if isinstance(k, ast.Constant) and k.value == "optional" ) names = [ast.literal_eval(k) for k in optional.keys] assert names == [f["field"] for f in block(path)["fields"] if f["node_id"] == node_id]