331 lines
12 KiB
Python
331 lines
12 KiB
Python
"""NLF exact source binding, 74 positions and independent old-sixteen preservation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
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/nlf-document-evidence"
|
|
PATH = next(p for p in DEFINITIONS if p.endswith("NLF_视频人物动作骨架获取.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, path):
|
|
for part in path.strip("/").split("/"):
|
|
part = part.replace("~1", "/").replace("~0", "~")
|
|
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
|
|
return obj
|
|
|
|
|
|
def set_value(obj, path, value):
|
|
parent, _, key = path.rpartition("/")
|
|
target = pointer(obj, parent)
|
|
target[int(key) if isinstance(target, list) else key] = value
|
|
|
|
|
|
def doc(source=None, review=None, path=PATH):
|
|
return build_document(
|
|
path,
|
|
SOURCE if source is None else source,
|
|
None,
|
|
None,
|
|
[],
|
|
REVIEW if review is None else review,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("field", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
|
|
def test_value_pointer_json_type_and_citations(field):
|
|
value = pointer(SOURCE, field["pointer"])
|
|
assert canonical(value) == canonical(field["value"])
|
|
assert type(value).__name__ == field["json_type"]
|
|
n = pointer(SOURCE, field["pointer"].split("/widgets_values/")[0])
|
|
assert str(n["id"]) == field["node_id"] and n["type"] == field["class_type"]
|
|
for k in ["label", "schema_default", "unit", "limits", "effect", "execution_note", "role"]:
|
|
assert field[k]
|
|
for k in field["evidence"]:
|
|
e = BLOCK["sources"][k]
|
|
raw = (OUT / e["local_file"]).read_bytes()
|
|
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
|
|
assert len(e["commit"]) == 40 and e["commit"] in e["url"]
|
|
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
|
|
|
|
|
|
@pytest.mark.parametrize("index", range(74))
|
|
@pytest.mark.parametrize("kind", ["value", "type", "pointer", "citation", "role", "default"])
|
|
def test_forged_field_even_with_recalculated_digest_rejected(index, kind):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
b = review["fixed_version_document"]
|
|
f = b["fields"][index]
|
|
if kind in ["value", "type"]:
|
|
set_value(source, f["pointer"], {"forged": True} if kind == "value" else "forged-type")
|
|
f["value"] = pointer(source, f["pointer"])
|
|
f["json_type"] = type(f["value"]).__name__
|
|
elif kind == "citation":
|
|
b["sources"][f["evidence"][0]]["sha256"] = "0" * 64
|
|
else:
|
|
f[{"default": "schema_default"}.get(kind, kind)] = "forged"
|
|
b["canonical_sha256"] = hashlib.sha256(canonical(source)).hexdigest()
|
|
review["caller_digest"] = hashlib.sha256(canonical(b)).hexdigest()
|
|
d = doc(source, review)["fixed_version"]
|
|
assert not d["matched"] and "fields" not in d
|
|
|
|
|
|
def leaves(obj, path):
|
|
if isinstance(obj, dict):
|
|
for k, v in obj.items():
|
|
yield from leaves(v, path + "/" + k)
|
|
elif isinstance(obj, list):
|
|
for i, v in enumerate(obj):
|
|
yield from leaves(v, path + "/" + str(i))
|
|
else:
|
|
yield path
|
|
|
|
|
|
# Includes every physical edge endpoint, source version/UE/mode, nested previews,
|
|
# and named input/output slot properties. No plugin is imported or evaluated.
|
|
MUTATIONS = (
|
|
[f"/links/{i}/{j}" for i in range(22) for j in range(6)]
|
|
+ [p for p in leaves(SOURCE["extra"], "/extra")]
|
|
+ [f"/nodes/{i}/{key}" for i in range(19) for key in ["mode", "type"]]
|
|
+ [
|
|
p
|
|
for i, n in enumerate(SOURCE["nodes"])
|
|
for key in ["properties", "inputs", "outputs"]
|
|
for p in leaves(n.get(key), f"/nodes/{i}/{key}")
|
|
]
|
|
+ [
|
|
p
|
|
for f in BLOCK["fields"]
|
|
if isinstance(f["value"], dict)
|
|
for p in leaves(f["value"], f["pointer"])
|
|
]
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("path", MUTATIONS)
|
|
def test_graph_mode_version_nested_preview_slot_mutation_fails_closed(path):
|
|
source, review = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
|
|
set_value(source, path, "forged")
|
|
review["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(
|
|
canonical(source)
|
|
).hexdigest()
|
|
review["caller_digest"] = hashlib.sha256(
|
|
canonical(review["fixed_version_document"])
|
|
).hexdigest()
|
|
assert not doc(source, review)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("nid", [363, 365])
|
|
@pytest.mark.parametrize("value", [None, {}, "missing"])
|
|
def test_zero_lists_distinguished_from_missing_null_empty_object(nid, value):
|
|
source = copy.deepcopy(SOURCE)
|
|
n = next(n for n in source["nodes"] if n["id"] == nid)
|
|
if value == "missing":
|
|
del n["widgets_values"]
|
|
else:
|
|
n["widgets_values"] = value
|
|
assert not doc(source)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize("value", [[], {}, "missing", False, 0])
|
|
def test_button_null_distinguished_from_other_json_shapes(value):
|
|
source = copy.deepcopy(SOURCE)
|
|
n = next(n for n in source["nodes"] if n["id"] == 372)
|
|
if value == "missing":
|
|
n["widgets_values"].pop()
|
|
else:
|
|
n["widgets_values"][3] = value
|
|
assert not doc(source)["fixed_version"]["matched"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"key",
|
|
[
|
|
"source_path",
|
|
"source_sha1",
|
|
"source_sha256",
|
|
"notice",
|
|
"user_overview",
|
|
"versions",
|
|
"comments",
|
|
"flow",
|
|
"sources",
|
|
"counts",
|
|
"zero_saved_nodes",
|
|
],
|
|
)
|
|
def test_editorial_forgery_rejected(key):
|
|
review = copy.deepcopy(REVIEW)
|
|
review["fixed_version_document"][key] = "forged"
|
|
review["caller_digest"] = hashlib.sha256(
|
|
canonical(review["fixed_version_document"])
|
|
).hexdigest()
|
|
assert not doc(review=review)["fixed_version"]["matched"]
|
|
|
|
|
|
def test_exact_topology_counts_modes_unknowns_and_old_objects():
|
|
d = doc()
|
|
assert d["fixed_version"]["matched"] and d["fixed_version"]["fields"] == BLOCK["fields"]
|
|
assert len(BLOCK["fields"]) == 74 and len(BLOCK["comments"]) == 0
|
|
assert sum(isinstance(f["value"], dict) for f in BLOCK["fields"]) == 3
|
|
assert sum(f["value"] is None for f in BLOCK["fields"]) == 1
|
|
assert sum(f["role"] == "unknown_saved" for f in BLOCK["fields"]) == 7
|
|
expected = {
|
|
203: 1,
|
|
334: 1,
|
|
340: 1,
|
|
137: 11,
|
|
339: 1,
|
|
373: 11,
|
|
364: 3,
|
|
362: 7,
|
|
363: 0,
|
|
106: 2,
|
|
374: 9,
|
|
370: 1,
|
|
130: 9,
|
|
208: 1,
|
|
376: 1,
|
|
375: 9,
|
|
365: 0,
|
|
335: 2,
|
|
372: 4,
|
|
}
|
|
nodes = {n["id"]: n for n in SOURCE["nodes"]}
|
|
assert {nid: len(n["widgets_values"]) for nid, n in nodes.items()} == expected
|
|
assert {nid for nid, n in nodes.items() if n["mode"] == 4} == {106, 375, 365}
|
|
assert len(SOURCE["links"]) == 22 and "definitions" not in SOURCE
|
|
assert [e[0] for e in SOURCE["links"]] == [
|
|
570,
|
|
621,
|
|
623,
|
|
624,
|
|
625,
|
|
627,
|
|
629,
|
|
648,
|
|
650,
|
|
651,
|
|
653,
|
|
654,
|
|
655,
|
|
656,
|
|
657,
|
|
658,
|
|
659,
|
|
660,
|
|
661,
|
|
662,
|
|
663,
|
|
665,
|
|
]
|
|
for eid, origin, slot, target, inp, typ in SOURCE["links"]:
|
|
assert eid in nodes[origin]["outputs"][slot]["links"]
|
|
assert nodes[target]["inputs"][inp]["link"] == eid
|
|
assert nodes[target]["inputs"][inp]["type"] == (
|
|
{648: "INT", 661: "INT,FLOAT", 662: "INT,FLOAT"}.get(eid, typ)
|
|
)
|
|
text = "".join(BLOCK["flow"])
|
|
assert all(
|
|
t in text
|
|
for t in [
|
|
"没有warmup",
|
|
"没有per_batch",
|
|
"round(result)",
|
|
"两Combine.audio都未接",
|
|
"临时目录",
|
|
"UE",
|
|
"mode4",
|
|
"0新源注释",
|
|
"冲突不抹平",
|
|
"taichi",
|
|
"提供方",
|
|
"未实跑",
|
|
]
|
|
)
|
|
assert not BLOCK["counts"]["final_prompt_constructed"]
|
|
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
|
|
old = json.loads(subprocess.check_output(["git", "show", "7e76510:" + resource], cwd=REPO))
|
|
now = json.loads((REPO / resource).read_bytes())
|
|
multivideo = next(p for p in now if p.endswith("/多视频合成分屏展示.json"))
|
|
now[multivideo].pop("fixed_version_document")
|
|
watermark = next(p for p in now if p.endswith("/图像4种添加水印节点.json"))
|
|
now[watermark].pop("fixed_version_document")
|
|
birefnet = next(p for p in now if p.endswith("/birefnet_图像移除背景.json"))
|
|
now[birefnet].pop("fixed_version_document")
|
|
latent = next(p for p in now if p.endswith("/加载本地latent解码.json"))
|
|
now[latent].pop("fixed_version_document")
|
|
later = next(p for p in now if p.endswith("/截取视频每一帧.json"))
|
|
now[later].pop("fixed_version_document")
|
|
for added in [
|
|
p
|
|
for p in now
|
|
if p.endswith(
|
|
("视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json")
|
|
)
|
|
]:
|
|
now[added].pop("fixed_version_document")
|
|
reverse = next(p for p in now if p.endswith("/视频倒放.json"))
|
|
now[reverse].pop("fixed_version_document")
|
|
assert now[PATH].pop("fixed_version_document") == BLOCK
|
|
assert now == old and len(DEFINITIONS) == 26
|
|
old_blocks = [
|
|
r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r
|
|
]
|
|
assert len(old_blocks) == 16 and sum(len(b["fields"]) for b in old_blocks) == 421
|
|
assert (
|
|
sum(len(b.get("comments", [])) for b in old_blocks) == 14
|
|
) # separate historical ASR flow note
|
|
assert d["status"] == dict(imported=True, doc_done=False, exec_verified=False)
|
|
assert d["dependencies"]["check_state"] == "unchecked"
|
|
for p in DEFINITIONS:
|
|
if p in [watermark, multivideo]:
|
|
continue
|
|
if p.endswith("/birefnet_图像移除背景.json"):
|
|
continue
|
|
if p.endswith(("/截取视频每一帧.json", "/加载本地latent解码.json")):
|
|
continue
|
|
if p.endswith(
|
|
("视频转gif动图图片.json", "加载图片合成视频.json", "双图像或视频合并显示.json")
|
|
):
|
|
continue
|
|
if p == PATH or p.endswith("/视频倒放.json"):
|
|
continue
|
|
r = copy.deepcopy(REVIEW)
|
|
r["fixed_version_document"] = old[p]["fixed_version_document"]
|
|
assert not doc(review=r)["fixed_version"]["matched"]
|
|
assert not doc(path="untrusted.json").get("fixed_version")
|
|
|
|
|
|
def test_raw_source_sha_and_missing_packages(tmp_path):
|
|
assert review_for(PATH, "0" * 40) is None
|
|
review = copy.deepcopy(REVIEW)
|
|
review["source_sha"] = "0" * 40
|
|
assert not doc(review=review)["fixed_version"]["matched"]
|
|
import sys
|
|
|
|
code = (
|
|
"import runpy,sys; from pathlib import Path; ns=r"
|
|
"unpy.run_path('recompute.py'); ns['require_packa"
|
|
"ges'].__globals__['REPO']=Path(sys.argv[1]); ns["
|
|
"'require_packages']()"
|
|
)
|
|
r = subprocess.run(
|
|
[sys.executable, "-c", code, str(tmp_path)], cwd=OUT, capture_output=True, text=True
|
|
)
|
|
assert r.returncode != 0 and "缺少wheel/sdist包产物" in r.stderr
|
|
assert "uv build --project calliope-backend --offline" in r.stderr
|