192 lines
8.2 KiB
Python
192 lines
8.2 KiB
Python
"""Two media documents: exact pointers, pinned citations and cross-graph trust rejection."""
|
|
|
|
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(("视频-音频分离.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)
|
|
video = "视频-音频" in path
|
|
assert d["matched"] and len(d["fields"]) == (8 if video else 14)
|
|
assert d["counts"]["physical_edges"] == len(s["links"]) == (6 if video else 4)
|
|
assert d["counts"]["final_scalars"] == 12
|
|
assert d["counts"]["frontend_only"] == (1 if video else 2)
|
|
assert "假设" in d["notice"] and "目标环境未核对" in d["notice"]
|
|
assert hashlib.sha256(canonical(r["fixed_version_document"])).hexdigest() == DEFINITIONS[path]
|
|
assert d["source_sha256"] == hashlib.sha256((REPO / path).read_bytes()).hexdigest()
|
|
assert len(s["nodes"]) == (7 if video else 5)
|
|
assert len(d["fields"]) == sum(len(n.get("widgets_values") or []) for n in s["nodes"])
|
|
assert len({f["pointer"] for f in d["fields"]}) == len(d["fields"])
|
|
for f in d["fields"]:
|
|
parts = f["pointer"].split("/")
|
|
node = s["nodes"][int(parts[2])]
|
|
value = node["widgets_values"][int(parts[4])]
|
|
assert canonical(value) == canonical(f["value"])
|
|
assert type(value).__name__ == f["json_type"]
|
|
assert str(node["id"]) == f["node_id"] and node["type"] == f["class_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/media-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"]
|
|
start, end = ev["lines"]
|
|
assert 1 <= start <= end <= len(raw.decode().splitlines())
|
|
fields = {(f["node_id"], f["field"]): f for f in d["fields"]}
|
|
flow = "\n".join(d["flow"])
|
|
if video:
|
|
assert fields["17", "quality"]["value"] == "320k"
|
|
assert "bit_rate=320000" in fields["17", "quality"]["effect"]
|
|
assert "schema默认V0" in fields["17", "quality"]["schema_default"]
|
|
assert fields["10", "audioUI"]["value"] is None
|
|
assert fields["10", "upload"]["value"] is None
|
|
assert fields["11", "chunk_fade_shape"]["value"] == "half_sine"
|
|
assert type(fields["11", "chunk_length"]["value"]) is int
|
|
assert fields["11", "chunk_length"]["value"] == 20
|
|
assert fields["11", "chunk_overlap"]["value"] == 0.2
|
|
for line in [
|
|
"10槽0 → 17.audio",
|
|
"11槽0 Bass低音 → 18.audio",
|
|
"11槽1 Drums鼓 → 19.audio",
|
|
"11槽2 Other其他 → 20.audio",
|
|
"11槽3 Vocals人声 → 15.audio",
|
|
]:
|
|
assert line in flow
|
|
assert "原音轨,不是分离后人声" in flow
|
|
assert "临时目录FLAC" in flow and "不保证永久四轨导出" in flow
|
|
assert not any("src/crop.py" == ev["file"] for ev in d["sources"])
|
|
else:
|
|
assert fields["3", "image"]["value"] == fields["4", "image"]["value"] == "1 (8).png"
|
|
assert fields["3", "upload"]["role"] == fields["4", "upload"]["role"] == "frontend"
|
|
for name in ["image3_text", "image4_text"]:
|
|
assert "不会凭此保存文本多出图块" in fields["1", name]["effect"]
|
|
assert "不同处理阶段" in fields["2", "border"]["limits"]
|
|
assert "字形支持未知" in fields["2", "font_file"]["limits"]
|
|
assert "没有自动换行" in fields["2", "font_size"]["effect"]
|
|
assert "临时目录" not in flow or "PNG" in flow
|
|
assert "ui.images" in flow and "temp目录PNG" in flow
|
|
assert "不是修改图片描述元数据" in flow and "两个MASK出口都未接" in flow
|
|
assert "混合提交不表示目标安装兼容" in "".join(d["versions"])
|
|
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.get("widgets_values"))
|
|
old = n["widgets_values"][0]
|
|
n["widgets_values"][0] = None if isinstance(old, str) else str(old)
|
|
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)
|