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

327 lines
12 KiB
Python

"""Pinned schema and saved graph transformations; never evaluate media/history."""
from __future__ import annotations
import ast
import copy
import json
from pathlib import Path
import pytest
from scripts import audit_reference_conversion as audit
from scripts.verify_frontend_sources import verify
from calliope.comfyui.serialization_reference import (
VIDEOINFO_COMMIT,
VIDEOINFO_NAMES,
VIDEOINFO_OUTPUTS,
serialize_videoinfo_node,
)
from calliope.comfyui.structure_reference import VIDEOINFO_PROFILES
from calliope.comfyui.ui2api import convert, convert_reference
from tests.test_setget_reference import connect, virtual
from tests.test_vhs_reference import synthetic as vhs_graph
REPO = Path(__file__).resolve().parents[2]
ROOT = REPO / "docs/hel-892/videoinfo-reference"
PROFILE = next(iter(VIDEOINFO_PROFILES))
def entity(nid=546):
return {
"id": nid,
"type": "VHS_VideoInfoLoaded",
"mode": 0,
"properties": {"cnr_id": "comfyui-videohelpersuite", "ver": VIDEOINFO_COMMIT},
"widgets_values": {},
"inputs": [{"name": "video_info", "type": "VHS_VIDEOINFO", "link": None}],
"outputs": [
{"name": name, "type": t, "links": []}
for name, t in zip(VIDEOINFO_NAMES, VIDEOINFO_OUTPUTS)
],
}
def graph(slot=2):
g = vhs_graph()
g["nodes"] += [
entity(),
virtual(568, "SetNode", "metric", "FLOAT"),
virtual(535, "GetNode", "metric", "FLOAT"),
]
connect(g, 10, 17, 3, 546, 0, "VHS_VIDEOINFO")
connect(g, 11, 546, slot, 568, 0, "FLOAT")
connect(g, 12, 535, 0, 12, 4, "FLOAT")
return g
def test_original_schema_ast_and_full_registration_not_execution():
assert len(verify(ROOT)["sources_verified"]) == 6
tree = ast.parse((ROOT / "videohelpersuite__nodes.py").read_text())
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "VideoInfoLoaded")
assigns = {n.targets[0].id: n.value for n in cls.body if isinstance(n, ast.Assign)}
assert ast.literal_eval(assigns["RETURN_TYPES"]) == VIDEOINFO_OUTPUTS
assert ast.literal_eval(assigns["RETURN_NAMES"]) == (
"fps🟦",
"frame_count🟦",
"duration🟦",
"width🟦",
"height🟦",
)
schema = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "INPUT_TYPES")
assert ast.literal_eval(schema.body[0].value) == {
"required": {"video_info": ("VHS_VIDEOINFO",)}
}
method = next(
n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "get_video_info"
)
assert ast.literal_eval(method.body[0].value) == [
"fps",
"frame_count",
"duration",
"width",
"height",
]
assert "loaded_info.append(video_info[f'loaded_{key}'])" in ast.unparse(method)
assert (
'"VHS_VideoInfoLoaded": VideoInfoLoaded,'
in (ROOT / "videohelpersuite__nodes.py").read_text()
)
js = (ROOT / "web__js__VHS.core.js").read_text()
assert 'if(nodeData?.name?.startsWith("VHS_"))' in js
assert "info.widgets_values = {};" in js
assert "if (!this.widgets)" in js
@pytest.mark.parametrize("profile", VIDEOINFO_PROFILES)
@pytest.mark.parametrize("slot", [0, 2])
@pytest.mark.parametrize("empty", [{}, []])
def test_nonzero_slots_external_precedence_no_scalar_invention(profile, slot, empty):
g = graph(slot)
g["extra"] = {"frontendVersion": VIDEOINFO_PROFILES[profile]}
g["nodes"][2]["widgets_values"] = empty
original = copy.deepcopy(g)
r = convert_reference(g, profile)
assert r.prompt is not None, r.issues
assert r.prompt["546"]["inputs"] == {"video_info": ["17", 3]}
assert r.prompt["12"]["inputs"]["frame_rate"] == ["546", slot]
assert set(r.prompt) == {"12", "17", "546"}
assert "568" not in r.prompt and "535" not in r.prompt
assert r.reference_only and convert(g).prompt is None
assert g == original
g["nodes"][1]["widgets_values"]["frame_rate"] = 49
assert convert_reference(g, profile).prompt["12"]["inputs"]["frame_rate"] == ["546", slot]
@pytest.mark.parametrize(
"mutation",
[
"version",
"release",
"provider",
"aux",
"mode",
"bool_mode",
"missing_input",
"extra_input",
"name",
"type",
"widget",
"null_link",
"bool_link",
"extra_widgets",
"missing_widgets",
"null_widgets",
"scalar_widgets",
"output_order",
"output_count",
"same_type_swapped_names",
"unused_output_type",
"slot_bool",
"slot_wrong",
"output_bad_reverse",
"input_bad_reverse",
"edge_bool_slot",
"edge_type",
"node_override",
"props_override",
"ue",
"literal_aux",
"missing_set",
"duplicate_set",
"unknown_loader",
"frontend",
"nested",
],
)
def test_mutations_close_whole_graph(mutation):
g = graph()
n = g["nodes"][2]
if mutation == "version":
n["properties"]["ver"] = "4ee72c065db22c9d96c2427954dc69e7b908444b"
elif mutation == "release":
n["properties"]["ver"] = "1.7.9"
elif mutation == "provider":
n["properties"]["cnr_id"] = "other"
elif mutation == "aux":
n["properties"]["aux_id"] = "VHS_VideoInfoLoaded"
elif mutation == "mode":
n["mode"] = 4
elif mutation == "bool_mode":
n["mode"] = False
elif mutation == "missing_input":
n["inputs"] = []
elif mutation == "extra_input":
n["inputs"].append(copy.deepcopy(n["inputs"][0]))
elif mutation == "name":
n["inputs"][0]["name"] = "info"
elif mutation == "type":
n["inputs"][0]["type"] = "ANY"
elif mutation == "widget":
n["inputs"][0]["widget"] = {"name": "video_info"}
elif mutation == "null_link":
n["inputs"][0]["link"] = None
elif mutation == "bool_link":
n["inputs"][0]["link"] = True
elif mutation == "extra_widgets":
n["widgets_values"] = {"fps": 30}
elif mutation == "missing_widgets":
n.pop("widgets_values")
elif mutation == "null_widgets":
n["widgets_values"] = None
elif mutation == "scalar_widgets":
n["widgets_values"] = ""
elif mutation == "output_order":
n["outputs"][0], n["outputs"][1] = n["outputs"][1], n["outputs"][0]
elif mutation == "same_type_swapped_names":
n["outputs"][0]["name"], n["outputs"][2]["name"] = (
n["outputs"][2]["name"],
n["outputs"][0]["name"],
)
elif mutation == "output_count":
n["outputs"].pop()
elif mutation == "unused_output_type":
n["outputs"][4]["type"] = "FLOAT"
elif mutation == "slot_bool":
n["outputs"][0]["slot_index"] = False
elif mutation == "slot_wrong":
n["outputs"][4]["slot_index"] = 3
elif mutation == "output_bad_reverse":
n["outputs"][2]["links"] = []
elif mutation == "input_bad_reverse":
n["inputs"][0]["link"] = 999
elif mutation == "edge_bool_slot":
g["links"][2][4] = False
elif mutation == "edge_type":
g["links"][3][5] = "INT"
elif mutation == "node_override":
n["onConfigure"] = "custom"
elif mutation == "props_override":
n["properties"]["serializeValue"] = "custom"
elif mutation == "ue":
n["properties"]["ue_properties"] = {"widget_ue_connectable": {"x": True}}
elif mutation == "literal_aux":
g["nodes"][3]["properties"]["aux_id"] = "SetNode"
elif mutation == "missing_set":
g["nodes"][3]["widgets_values"] = ["else"]
elif mutation == "duplicate_set":
other = copy.deepcopy(g["nodes"][3])
other["id"] = 569
other["inputs"][0]["link"] = None
g["nodes"].append(other)
elif mutation == "unknown_loader":
g["nodes"][0]["properties"]["ver"] = VIDEOINFO_COMMIT
elif mutation == "frontend":
g["extra"] = {"frontendVersion": "1.53.6"}
elif mutation == "nested":
g["definitions"] = {"subgraphs": [{"id": "unknown"}]}
r = convert_reference(g, PROFILE)
assert r.prompt is None and r.issues, mutation
def test_multiple_entities_names_and_nonzero_reroute_do_not_cross():
g = graph(2)
g["nodes"] += [
entity(101),
virtual(104, "SetNode", "second", "FLOAT"),
virtual(121, "GetNode", "second", "FLOAT"),
{
"id": 600,
"type": "Reroute",
"widgets_values": [],
"inputs": [{"name": "", "type": "FLOAT", "link": None}],
"outputs": [{"name": "", "type": "FLOAT", "links": []}],
},
]
connect(g, 13, 17, 3, 101, 0, "VHS_VIDEOINFO")
connect(g, 14, 101, 0, 600, 0, "FLOAT")
connect(g, 15, 600, 0, 104, 0, "FLOAT")
assert convert_reference(g, PROFILE).prompt["12"]["inputs"]["frame_rate"] == ["546", 2]
g["nodes"][4]["widgets_values"] = ["second"]
r = convert_reference(g, PROFILE)
assert r.prompt is not None, r.issues
assert r.prompt["12"]["inputs"]["frame_rate"] == ["101", 0]
assert r.prompt["101"]["inputs"] == {"video_info": ["17", 3]}
def test_real_entities_strict_source_schema_and_old_profiles_stay_frozen():
old = json.loads((REPO / "docs/hel-892/setget-evidence/library.json").read_bytes())
for row in old["cases"]:
if "Scail-2" not in row["source_path"]:
continue
source = json.loads((REPO / row["source_path"]).read_bytes())
n = next(n for n in source["nodes"] if n["type"] == "VHS_VideoInfoLoaded")
assert serialize_videoinfo_node(n) == {}
result = convert_reference(source, PROFILE)
assert result.prompt is None and result.issues
assert all(a["status"] != "complete_reference_candidate" for a in row["attempts"])
assert (
len(
[
p
for p in audit.REFERENCE_PROFILES[:26]
if p not in (*audit.SCAIL_LOAD_PROFILES, audit.GRID_PROFILE, audit.H3_PROFILE)
]
)
== 22
)
assert tuple(audit.REFERENCE_PROFILES[:20]) == tuple(old["profiles"])
@pytest.mark.parametrize("slot", range(5))
def test_all_five_types_propagate_through_known_alias_and_reroute(slot):
from calliope.comfyui.structure_reference import project_reference_setget
g = graph()
typ = VIDEOINFO_OUTPUTS[slot]
for n in g["nodes"][3:]:
for socket in n["inputs"] + n["outputs"]:
socket["type"] = socket["name"] = typ
g["nodes"][2]["outputs"][2]["links"] = []
g["nodes"][2]["outputs"][slot]["links"] = [11]
g["nodes"][1]["inputs"][4]["type"] = typ
g["links"][3][2] = slot
for edge in g["links"][3:]:
edge[5] = typ
g["nodes"].append(
{
"id": 600,
"type": "Reroute",
"widgets_values": [],
"inputs": [{"name": "", "type": typ, "link": 11}],
"outputs": [{"name": "", "type": typ, "links": [13]}],
}
)
g["links"][3][3:5] = [600, 0]
g["nodes"][3]["inputs"][0]["link"] = 13
g["links"].append([13, 600, 0, 568, 0, typ])
projected = project_reference_setget(g)
assert projected.graph is not None, projected.issues
resolution = projected.resolved[0]
assert resolution["endpoint"] == ["546", slot]
node = next(n for n in projected.graph["nodes"] if n["id"] == 546)
assert node["outputs"][slot]["type"] == typ
# INT endpoints are typed topology evidence, not FLOAT coercion for Combine.
result = convert_reference(g, PROFILE)
assert (result.prompt is not None) == (typ == "FLOAT")