171 lines
6.7 KiB
Python
171 lines
6.7 KiB
Python
"""Multi-document trust, exact types and fixed audio semantics without running media."""
|
|
|
|
from __future__ import annotations
|
|
|
|
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]
|
|
PATHS = [p for p in DEFINITIONS if p.endswith(("音频转MP3格式.json", "音频人声分离.json"))]
|
|
|
|
|
|
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_exact_values_citations_and_independent_counts(path):
|
|
s, r = data(path)
|
|
d = block(path)
|
|
mp3 = "MP3" in path
|
|
assert d["matched"] and len(d["fields"]) == (5 if mp3 else 8)
|
|
assert d["counts"]["physical_edges"] == len(s["links"]) == (1 if mp3 else 3)
|
|
assert d["counts"]["final_scalars"] == (5 if mp3 else 8)
|
|
assert "假设" in d["notice"] and "目标环境未核对" in d["notice"]
|
|
assert "声明版本核对," not in d["notice"]
|
|
assert hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest() == DEFINITIONS[path]
|
|
for f in d["fields"]:
|
|
parts = f["pointer"].split("/")
|
|
value = s["nodes"][int(parts[2])]["widgets_values"][int(parts[4])]
|
|
assert canonical(value) == canonical(f["value"])
|
|
assert type(value).__name__ == f["json_type"]
|
|
assert all(f[k] for k in ["effect", "limits", "schema_default", "evidence", "unit"])
|
|
for i in f["evidence"]:
|
|
ev = d["sources"][i]
|
|
raw = (REPO / "docs/hel-892/audio-document-evidence" / ev["local_file"]).read_bytes()
|
|
assert hashlib.sha256(raw).hexdigest() == ev["sha256"]
|
|
assert len(ev["commit"]) == 40 and ev["commit"] in ev["url"]
|
|
a, b = ev["lines"]
|
|
assert 1 <= a <= b <= len(raw.decode().splitlines())
|
|
fields = {f["field"]: f for f in d["fields"]}
|
|
assert fields["audioUI"]["value"] is None
|
|
assert "空字符串" in fields["audioUI"]["execution_note"]
|
|
assert fields["audioUI"]["role"] == "scalar"
|
|
assert fields["upload"]["role"] == "frontend"
|
|
assert fields["upload"]["value"] == ("" if mp3 else None)
|
|
if mp3:
|
|
assert "qscale=1" in fields["quality"]["effect"]
|
|
assert "固定码率" in fields["quality"]["limits"]
|
|
else:
|
|
assert type(fields["chunk_length"]["value"]) is int
|
|
assert type(fields["chunk_overlap"]["value"]) is float
|
|
assert fields["chunk_length"]["value"] == 16
|
|
assert fields["chunk_overlap"]["value"] == 1.1
|
|
assert "后端" in fields["chunk_length"]["limits"]
|
|
assert any("PreviewAudio15继承" in line and "临时" in line for line in d["flow"])
|
|
assert any("Vocals" in line and "槽3" in line for line in d["flow"])
|
|
doc = build_document(path, s, None, None, [], r)
|
|
assert doc["params"] == [] and not doc["status"]["doc_done"]
|
|
assert doc["dependencies"]["check_state"] == "unchecked"
|
|
assert "fixed_version_document" not in doc["source_review"]
|
|
d["fields"][0]["value"] = "consumer edit"
|
|
assert block(path)["fields"][0]["value"] != "consumer edit"
|
|
|
|
|
|
@pytest.mark.parametrize("path", PATHS)
|
|
@pytest.mark.parametrize(
|
|
"change",
|
|
[
|
|
"definition_swap",
|
|
"path",
|
|
"sha",
|
|
"id",
|
|
"class",
|
|
"version",
|
|
"frontend",
|
|
"value",
|
|
"type",
|
|
"widget",
|
|
"edge",
|
|
"evidence",
|
|
"digest",
|
|
"graph",
|
|
"pointer",
|
|
"defaults",
|
|
"notice",
|
|
],
|
|
)
|
|
def test_refuse_cross_graph_or_recomputed_untrusted_definitions(path, change):
|
|
s, r = data(path)
|
|
b = r["fixed_version_document"]
|
|
if change == "definition_swap":
|
|
other = next(p for p in DEFINITIONS if p != path)
|
|
r["fixed_version_document"] = data(other)[1]["fixed_version_document"]
|
|
elif change == "path":
|
|
b["source_path"] = "another/" + path
|
|
elif change == "sha":
|
|
r["source_sha"] = "0" * 40
|
|
elif change == "id":
|
|
s["nodes"][0]["id"] = 999
|
|
elif change == "class":
|
|
s["nodes"][0]["type"] = "SameNameOtherNode"
|
|
elif change == "version":
|
|
s["nodes"][0]["properties"]["ver"] = "different"
|
|
elif change == "frontend":
|
|
s["extra"]["frontendVersion"] = "1.37.11"
|
|
elif change == "value":
|
|
s["nodes"][0]["widgets_values"][0] = "changed"
|
|
elif change == "type":
|
|
# A value equal in Python must still fail JSON type binding.
|
|
n = next(
|
|
n
|
|
for n in s["nodes"]
|
|
if n["type"] == ("AudioSeparation" if "MP3" not in path else "LoadAudio")
|
|
)
|
|
n["widgets_values"][1] = 16.0 if "MP3" not in path else ""
|
|
elif change == "widget":
|
|
s["nodes"][0]["inputs"][0]["widget"] = {"name": "changed"}
|
|
elif change == "edge":
|
|
s["links"][0][2] = 2
|
|
elif change == "evidence":
|
|
b["sources"][0]["lines"] = [1, 2]
|
|
elif change == "digest":
|
|
b["sources"][0]["sha256"] = "0" * 64
|
|
elif change == "pointer":
|
|
b["fields"][0]["pointer"] = "/nodes/999"
|
|
elif change == "defaults":
|
|
b["fields"][0]["schema_default"] = "unreviewed default"
|
|
elif change == "notice":
|
|
b["notice"] = "已安装"
|
|
else:
|
|
s["unrelated_graph_change"] = True
|
|
assert not block(path, s, r)["matched"]
|
|
# Attempt to recompute the graph digest too. Trusted definition allowlist must still reject.
|
|
b["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
d = block(path, s, r)
|
|
assert not d["matched"] and "fields" not in d and "待核对" in d["notice"]
|
|
|
|
|
|
@pytest.mark.parametrize("path", PATHS)
|
|
def test_every_saved_pointer_type_and_value_change_is_rejected(path):
|
|
source, review = data(path)
|
|
for field in review["fixed_version_document"]["fields"]:
|
|
s = copy.deepcopy(source)
|
|
parts = field["pointer"].split("/")
|
|
values = s["nodes"][int(parts[2])]["widgets_values"]
|
|
offset = int(parts[4])
|
|
values[offset] = "changed" if values[offset] is None else None
|
|
assert not block(path, s)["matched"]
|
|
r = copy.deepcopy(review)
|
|
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
|
|
assert not block(path, s, r)["matched"]
|
|
no_block = copy.deepcopy(review)
|
|
del no_block["fixed_version_document"]
|
|
assert not block(path, source, no_block)["matched"]
|
|
assert "fixed_version" not in build_document("other/" + path, source, None, None, [], review)
|