Files
aigc-home/calliope-backend/tests/test_seedvr2_loop_fixed_document.py
T

313 lines
11 KiB
Python

"SeedVR2 FOR loop source, duplicate history, special boundaries and fail-closed trust."
import ast
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/seedvr2-loop-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("/Seedvr2_视频放大FOR循环长视频.json"))
SOURCE = json.loads((REPO / PATH).read_bytes())
REVIEW = review_for(PATH, hashlib.sha1((REPO / PATH).read_bytes()).hexdigest())
BLOCK = REVIEW["fixed_version_document"]
def pointer(obj, path):
for part in path.strip("/").split("/"):
obj = obj[int(part)] if isinstance(obj, list) else obj[part]
return obj
def doc(s=SOURCE, r=REVIEW):
return build_document(PATH, s, None, None, [], r)
def refuse(s, r):
r["fixed_version_document"]["canonical_sha256"] = hashlib.sha256(canonical(s)).hexdigest()
assert not doc(s, r)["fixed_version"]["matched"]
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
def test_all_saved_positions_dual_pointers_types_and_local_evidence(f):
assert canonical(pointer(SOURCE, f["pointer"])) == canonical(f["value"])
assert type(pointer(SOURCE, f["pointer"])).__name__ == f["json_type"]
assert f["scope"] == (
"root" if ":" not in f["node_id"] else SOURCE["definitions"]["subgraphs"][0]["id"]
)
assert canonical(pointer(SOURCE, f["named_pointer"])) == canonical(f["named_value"])
if f["input_pointer"].startswith("/"):
inp = pointer(SOURCE, f["input_pointer"])
assert inp.get("widget", {}).get("name", inp["name"]) == f["field"]
for key in [
"effect",
"label",
"schema_default",
"unit",
"limits",
"execution_note",
"role",
"type",
]:
assert f[key]
for i in f["evidence"]:
e = BLOCK["sources"][i]
raw = (OUT / e["local_file"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == e["sha256"]
assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines())
@pytest.mark.parametrize("f", BLOCK["fields"], ids=lambda f: f["node_id"] + "." + f["field"])
@pytest.mark.parametrize("variant", ["source", "text", "named"])
def test_each_saved_value_or_description_forgery_cannot_retrust(f, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
if variant == "text":
r["fixed_version_document"]["fields"][BLOCK["fields"].index(f)]["effect"] = "forged"
else:
p = f["pointer"] if variant == "source" else f["named_pointer"]
parent, _, key = p.rpartition("/")
c = pointer(s, parent)
c[int(key) if isinstance(c, list) else key] = "forged"
refuse(s, r)
@pytest.mark.parametrize(
"scope,index",
[
(base, i)
for base, ns in [
("/nodes", SOURCE["nodes"]),
("/definitions/subgraphs/0/nodes", SOURCE["definitions"]["subgraphs"][0]["nodes"]),
]
for i in range(len(ns))
],
)
@pytest.mark.parametrize("variant", ["mode", "identity"])
def test_every_node_identity_and_mode_bound(scope, index, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = pointer(s, scope)[index]
if variant == "mode":
n["mode"] = 1 if n["mode"] != 1 else 0
else:
n["properties"]["ver"] = "forged"
refuse(s, r)
@pytest.mark.parametrize(
"scope,index",
[
(base, i)
for base, es in [
("/links", SOURCE["links"]),
("/definitions/subgraphs/0/links", SOURCE["definitions"]["subgraphs"][0]["links"]),
]
for i in range(len(es))
],
)
def test_every_physical_or_boundary_edge_bound(scope, index):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
e = pointer(s, scope)[index]
if isinstance(e, list):
e[3] = 99999
else:
e["target_slot"] = 99999
refuse(s, r)
@pytest.mark.parametrize(
"io,index",
[
(k, i)
for k in ["inputs", "outputs"]
for i in range(len(SOURCE["definitions"]["subgraphs"][0][k]))
],
)
def test_boundary_declaration_cannot_retrust(io, index):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
s["definitions"]["subgraphs"][0][io][index]["linkIds"] = []
refuse(s, r)
@pytest.mark.parametrize("i", range(12))
@pytest.mark.parametrize("variant", ["physical", "named", "meaning"])
def test_each_complete_note_bound(i, variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
n = BLOCK["comments"][i]
if variant == "meaning":
r["fixed_version_document"]["comments"][i]["meaning"] = "forged"
else:
p = n["pointer"] if variant == "physical" else n["named_pointer"]
parent, _, key = p.rpartition("/")
c = pointer(s, parent)
c[int(key) if isinstance(c, list) else key] = "forged"
refuse(s, r)
@pytest.mark.parametrize(
"variant", ["source_sha", "overview", "citation", "empty_block", "missing_block"]
)
def test_other_trust_boundaries_refuse(variant):
s, r = copy.deepcopy(SOURCE), copy.deepcopy(REVIEW)
if variant == "source_sha":
r["source_sha"] = "forged"
elif variant == "overview":
r["fixed_version_document"]["user_overview"] = "forged"
elif variant == "citation":
r["fixed_version_document"]["sources"][0]["sha256"] = "forged"
elif variant == "empty_block":
r["fixed_version_document"] = {}
else:
r.pop("fixed_version_document")
assert not doc(s, r)["fixed_version"]["matched"]
def test_independent_inventory_and_source_unknowns():
inv = json.loads((OUT / "independent-inventory.json").read_bytes())
assert [f["pointer"] for f in inv["fields"]] == [f["pointer"] for f in BLOCK["fields"]]
assert len(BLOCK["fields"]) == 86 and len(BLOCK["comments"]) == 12
assert sum(f["scope"] == "root" for f in BLOCK["fields"]) == 64
assert len(BLOCK["zero_saved_nodes"]) == 8
assert (
sorted(x["value_state"] for x in BLOCK["zero_saved_nodes"])
== ["dict", "list"] + ["missing"] * 6
)
assert len([f for f in BLOCK["fields"] if isinstance(f["value"], dict)]) == 2
assert "choose video to upload" not in [f["field"] for f in BLOCK["fields"]]
for count, predicate in [
(
"linked_saved_fields",
lambda f: (
f["input_pointer"].startswith("/")
and pointer(SOURCE, f["input_pointer"]).get("link") is not None
),
),
("frontend_only", lambda f: not f["input_pointer"].startswith("/")),
]:
assert BLOCK["counts"][count] == sum(predicate(f) for f in BLOCK["fields"])
flow = "\n".join(BLOCK["flow"])
for fact in [
"mode4",
"121.Set(audio)输入link=null",
"无file_path",
"无audio/audio_mode",
"三普通控件",
"values.a/values.b",
"不套0.35.1",
"八宿主缓存/九输入/内部22",
"三SeedVR类准确实现未知",
"不继承旧profile信任",
"true/false",
]:
assert fact in flow
ns = {n["id"]: n for n in SOURCE["nodes"]}
assert ns[185]["mode"] == 4 and ns[185]["outputs"][0]["links"] == []
assert ns[204]["outputs"][0]["links"] is None and ns[204]["outputs"][2]["links"] is None
assert ns[121]["inputs"][0]["link"] is None
assert ns[116]["inputs"][3]["link"] is None
assert len(ns[143]["widgets_values"]) == 8 and len(ns[143]["inputs"]) == 9
def test_old_thirty_six_blocks_and_other_reviews_unchanged():
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(
subprocess.check_output(
["git", "show", f"cf90d2ae96bc3dc2da40b2fdf2ca22b8335433e9:{resource}"], cwd=REPO
)
)
cur = json.loads((REPO / resource).read_bytes())
cur[PATH].pop("fixed_version_document")
assert cur == old
blocks = [r["fixed_version_document"] for r in old.values() if "fixed_version_document" in r]
assert len(blocks) == 36 and sum(len(b["fields"]) for b in blocks) == 1195
assert (
sum(
len(
build_document(p, json.loads((REPO / p).read_bytes()), None, None, [], r)[
"source_notes"
]
)
for p, r in old.items()
if "fixed_version_document" in r
)
== 28
)
assert doc()["fixed_version"]["matched"]
@pytest.mark.parametrize("generated", [False, True])
def test_release_consistency_and_not_importing_newer_schema_frontend(generated):
if generated:
import runpy
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
else:
b = BLOCK
assert {e["commit"] for e in b["sources"] if e["id"] == "frontend"} == {
"8c05266b83b5024f2c12c8de6c2a0a4ecf2ebd33"
}
assert not any(e["commit"].startswith("71d2f2f2") for e in b["sources"])
pathrefs = [e for e in b["sources"] if e["id"] == "path"]
assert pathrefs and {e["commit"] for e in pathrefs} == {
"4ee72c065db22c9d96c2427954dc69e7b908444b"
}
assert {e["commit"] for e in b["sources"] if e["id"] == "core-preview"} == {
"322122449c9d2ba8b8df1bb517364527dd0615f1"
}
fixed = next(
f
for f in b["fields"]
if f["node_id"] == "143:133" and f["field"] == "control_after_generate"
)
assert "randomize" in fixed["schema_default"] and "范围钳制" in fixed["limits"]
for e in fixed["evidence"]:
assert 0 <= e < len(b["sources"])
rootload = [f for f in b["fields"] if f["node_id"] == "204"]
assert all("身份未知" in f["schema_default"] and "仅参考假设" in f["effect"] for f in rootload)
def test_exact_schemas_expose_interface_conflicts_without_execution():
ref = OUT.parent / "vosr2-loop-reference"
tree = ast.parse((ref / "info__videohelpersuite__nodes.py").read_text())
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "VideoInfoLoaded")
ret = next(
n.value
for n in cls.body
if isinstance(n, ast.Assign)
and isinstance(n.targets[0], ast.Name)
and n.targets[0].id == "RETURN_NAMES"
)
assert len(ret.elts) == 5
merge = (
(ref / "merge__nodes__video.py").read_text().split("class EasyMergeVideosFromPaths", 1)[1]
)
assert "audio_mode" not in merge
math = (
OUT.parent / "seedvr2-loop-reference/core-math__comfy_extras__nodes_math.py"
).read_text()
assert 'io.Autogrow.Input("values"' in math and 'node_id="ComfyMathExpression"' in math
assert BLOCK["counts"]["final_prompt_constructed"] is False
def test_float_rounding_is_documented_from_exact_source_without_evaluating_it():
tree = ast.parse(
(OUT.parent / "seedvr2-loop-reference/easy-float__py__nodes__logic.py").read_text()
)
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "Float")
execute = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "execute")
assert any(
isinstance(n, ast.Call)
and isinstance(n.func, ast.Name)
and n.func.id == "round"
and ast.literal_eval(n.args[1]) == 3
for n in ast.walk(execute)
)
f = next(f for f in BLOCK["fields"] if f["node_id"] == "147")
assert "round(value,3)" in f["effect"] and "三位小数" in f["effect"]