Files
aigc-home/calliope-backend/tests/test_vosr2_loop_fixed_document.py
T
2026-10-31 02:33:19 +08:00

280 lines
11 KiB
Python

"VOSR2 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/vosr2-loop-document-evidence"
PATH = next(p for p in DEFINITIONS if p.endswith("/VOSR2_视频放大_分批循环长视频.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"] = 4
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(3))
@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)
def test_independent_inventory_and_duplicate_history_not_collapsed():
inv = json.loads((OUT / "independent-inventory.json").read_bytes())
assert [e["pointer"] for e in inv["fields"]] == [e["pointer"] for e in BLOCK["fields"]]
assert len(BLOCK["fields"]) == 89 and len(BLOCK["comments"]) == 3
assert BLOCK["counts"]["linked_saved_fields"] == sum(
f["input_pointer"].startswith("/")
and pointer(SOURCE, f["input_pointer"]).get("link") is not None
for f in BLOCK["fields"]
)
assert BLOCK["counts"]["frontend_only"] == sum(
not f["input_pointer"].startswith("/") for f in BLOCK["fields"]
)
assert sum(f["scope"] == "root" for f in BLOCK["fields"]) == 62
history = [f for f in BLOCK["fields"] if f["node_id"] == "104"]
assert len(history) == 15 and len({f["pointer"] for f in history}) == 15
assert len({f["value"] for f in history}) == 1
assert len([f for f in BLOCK["fields"] if isinstance(f["value"], dict)]) == 2
assert len(BLOCK["zero_saved_nodes"]) == 4
assert "choose video to upload" not in [f["field"] for f in BLOCK["fields"]]
def test_special_negative_endpoint_and_duplicate_declaration_are_reported_as_unknown():
sub = SOURCE["definitions"]["subgraphs"][0]
assert next(e for e in sub["links"] if e["id"] == 143)["target_slot"] == -3
assert sub["outputs"][0]["linkIds"] == [143, 143]
flow = "\n".join(BLOCK["flow"])
for fact in [
"target_slot=-3",
"[143,143]",
"254",
"255",
"不能先判无效",
"五槽",
"无audio/audio_mode",
"format.codec",
"未证明",
]:
assert fact in flow
ns = {n["id"]: n for n in SOURCE["nodes"]}
assert ns[3]["outputs"][0]["links"] == []
assert ns[193]["outputs"][5]["name"] == "file_path🟦"
assert ns[193]["widgets_values"] == {}
for nid in [213]:
assert "widgets_values" not in ns[nid]
internal = {n["id"]: n for n in sub["nodes"]}
assert internal[116]["inputs"][3]["link"] is None
assert "widgets_values" not in internal[129] and "widgets_values" not in internal[103]
def test_exact_code_schemas_statically_expose_real_conflicts():
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 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 and "paths: str, frame_count: int = -1" in merge
video = (
(ref / "core-video__comfy_extras__nodes_video.py")
.read_text()
.split("class SaveVideo", 1)[1]
.split("class CreateVideo", 1)[0]
)
assert "format.codec" not in video and 'io.Combo.Input("codec"' in video
assert BLOCK["counts"]["final_prompt_constructed"] is False
@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_old_thirty_five_blocks_and_other_reviews_unchanged():
resource = "calliope-backend/src/calliope/comfyui/source_reviews.json"
old = json.loads(
subprocess.check_output(
["git", "show", f"d70a286f9b9229dd47773c78cab408bd68e842ec:{resource}"], cwd=REPO
)
)
cur = json.loads((REPO / resource).read_bytes())
cur[PATH].pop("fixed_version_document")
assert cur == old
assert len([r for r in old.values() if "fixed_version_document" in r]) == 35
assert doc()["fixed_version"]["matched"]
@pytest.mark.parametrize("generated", [False, True], ids=["delivered", "template-rebuild"])
def test_frontend_evidence_uses_graph_release_even_when_schema_template_is_newer(generated):
# Check the generator too: editing a delivered summary cannot hide template inheritance.
if generated:
import runpy
b = runpy.run_path(str(OUT / "build_definition.py"))["build"]()
else:
b = BLOCK
frontend = [e for e in b["sources"] if e.get("id") == "frontend"]
assert frontend
assert {e["commit"] for e in frontend} == {"8c05266b83b5024f2c12c8de6c2a0a4ecf2ebd33"}
assert not any("namedValuesRestore" in e["claim"] for e in frontend)
control = next(f for f in b["fields"] if f["node_id"] == "209:205" and f["field"] == "fixed")
cited = [b["sources"][i] for i in control["evidence"]]
assert any(e["file"] == "src/scripts/widgets.ts" and e["lines"] == [59, 139] for e in cited)
assert any(e["file"] == "src/scripts/widgets.ts" and e["lines"] == [228, 280] for e in cited)
assert "恢复对应未知" in control["schema_default"]
assert "范围钳制" in control["limits"]
assert "首次跳过" in control["effect"] and "实际排队调用顺序未知" in "".join(b["flow"])
for field in b["fields"]:
for i in field["evidence"]:
assert 0 <= i < len(b["sources"])
assert all(0 <= i < len(b["sources"]) for i in b["flow_evidence"])