#!/usr/bin/env python3 """Standalone verification: T2V == R2V normalization + one-model loader. No ComfyUI required. Checks (matching the standing verification rules): 1. utils/h3_project.py - h3_task_type / h3_generation_mode normalize T2V to the exact same value as R2V (functions extracted via AST). 2. nodes/basic.py - _multitrack_task_type mirrors the normalization. 3. utils/llm_api.py - reference-mode with zero media falls back to the cloud-native text-only mode instead of raising. 4. moxie_loader.py - schema shows a single model picker; execute() auto-picks LoRA/VAE/CLIP from the pack's models/ folders and still applies the LoRA to model+clip at strength 1.0. Run: python3 scripts/test_t2v_r2v_and_loader.py """ from __future__ import annotations import ast import enum import importlib.util import os import sys import tempfile import types FAILED = [] def check(name, condition, detail=""): status = "PASS" if condition else "FAIL" print(f"[{status}] {name}" + (f" - {detail}" if detail and not condition else "")) if not condition: FAILED.append(name) # --------------------------------------------------------------------------- # AST extraction helpers # --------------------------------------------------------------------------- def extract_functions(path, names, constants=()): """Return {name: function} for top-level (or class-level) defs. All requested functions plus any requested module-level constants are compiled into ONE shared namespace so they can reference each other. """ with open(path, "r", encoding="utf-8") as handle: tree = ast.parse(handle.read()) body = [] def visit(nodes): for node in nodes: if isinstance(node, ast.FunctionDef) and node.name in names: body.append(ast.FunctionDef( name=node.name, args=node.args, body=node.body, decorator_list=[], returns=node.returns, type_comment=None, type_params=[], )) elif isinstance(node, ast.ClassDef): visit(node.body) elif ( isinstance(node, ast.Assign) and constants and any( isinstance(target, ast.Name) and target.id in constants for target in node.targets ) ): body.append(node) visit(tree.body) ast.fix_missing_locations(ast.Module(body=body, type_ignores=[])) namespace = {"__name__": "extracted"} exec(compile(ast.Module(body=body, type_ignores=[]), path, "exec"), namespace) missing = set(names) - set(namespace) if missing: raise RuntimeError(f"could not extract from {path}: {sorted(missing)}") return namespace def extract_constant(path, name): with open(path, "r", encoding="utf-8") as handle: tree = ast.parse(handle.read()) for node in tree.body: if isinstance(node, ast.Assign) and any( isinstance(target, ast.Name) and target.id == name for target in node.targets ): return ast.literal_eval(node.value) raise RuntimeError(f"constant {name} not found in {path}") # --------------------------------------------------------------------------- # 1. utils/h3_project.py - T2V -> R2V at the resolution choke point # --------------------------------------------------------------------------- def verify_h3_project(): print("\n== 1. utils/h3_project.py: h3_task_type / h3_generation_mode ==") h3_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "utils") h3_file = os.path.join(h3_dir, "h3_project.py") h3_ns = extract_functions( h3_file, ["h3_task_type", "h3_generation_mode", "_normalize_task_type", "_frame_value"], constants=("_T2V_EQUIVALENT_TASK_TYPE",), ) equiv = h3_ns["_T2V_EQUIVALENT_TASK_TYPE"] check("t2v equivalent constant is r2v", equiv == "r2v", repr(equiv)) def muted_image(image): return bool(isinstance(image, dict) and image.get("muted")) h3_ns["multitrack_is_muted_image"] = muted_image h3_task_type = h3_ns["h3_task_type"] h3_generation_mode = h3_ns["h3_generation_mode"] def make_info(with_video=False): tracks = [{"type": "task", "segments": [{"start_frame": 0, "end_frame": 48, "content": {}}]}] if with_video: tracks.append({ "type": "video", "segments": [{"start_frame": 0, "end_frame": 48, "content": {}}], }) return {"tracks": tracks} def entry_for(mode=None, task_type=None, images=None, info=None): content = {} if mode is not None: content["task_mode"] = mode if task_type is not None: content["task_type"] = task_type if images is not None: content["images"] = images return {"start_frame": 0, "end_frame": 48, "task": {"content": content}} check("default mode, no images -> r2v (was t2v)", h3_task_type(entry_for(mode="default"), make_info()) == "r2v") check("no task_mode, no images -> r2v (was t2v)", h3_task_type(entry_for(), make_info()) == "r2v") check("explicit t2v -> r2v", h3_task_type(entry_for(task_type="t2v"), make_info()) == "r2v") check("explicit T2V (uppercase) -> r2v", h3_task_type(entry_for(task_type="T2V"), make_info()) == "r2v") check("explicit custom type passes through", h3_task_type(entry_for(task_type="wan-2.2-fun"), make_info()) == "wan-2.2-fun") check("muted images ignored -> r2v (was t2v)", h3_task_type(entry_for(images=[{"muted": True}]), make_info()) == "r2v") check("one active image -> i2v unchanged", h3_task_type(entry_for(images=[{"id": "a"}]), make_info()) == "i2v") check("ref mode, no video -> r2v", h3_task_type(entry_for(mode="ref"), make_info()) == "r2v") check("ref mode, video in range -> rv2v", h3_task_type(entry_for(mode="ref"), make_info(with_video=True)) == "rv2v") check("l2v mode -> l2v", h3_task_type(entry_for(mode="l2v"), make_info()) == "l2v") check("edit mode, no images -> v2v", h3_task_type(entry_for(mode="edit"), make_info()) == "v2v") check("generation_mode(t2v) -> reference (belt and braces)", h3_generation_mode("t2v") == "reference") check("generation_mode(r2v) -> reference", h3_generation_mode("r2v") == "reference") check("generation_mode(rv2v) -> reference", h3_generation_mode("rv2v") == "reference") check("generation_mode(i2v) -> multi_frames", h3_generation_mode("i2v") == "multi_frames") check("generation_mode(l2v) -> last_frame", h3_generation_mode("l2v") == "last_frame") # --------------------------------------------------------------------------- # 2. nodes/basic.py - editor task output mirrors the normalization # --------------------------------------------------------------------------- def verify_basic_node(): print("\n== 2. nodes/basic.py: _multitrack_task_type ==") basic_file = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "nodes", "basic.py" ) resolver = extract_functions(basic_file, ["_multitrack_task_type"])["_multitrack_task_type"] check("explicit t2v -> r2v", resolver({"content": {"task_type": "t2v"}}, 0, False) == "r2v") check("default mode, no images -> r2v (was t2v)", resolver({"content": {"task_mode": "default"}}, 0, False) == "r2v") check("no content -> r2v (was t2v)", resolver({}, 0, False) == "r2v") check("custom type kept", resolver({"content": {"task_type": "wan-2.2-fun"}}, 0, False) == "wan-2.2-fun") check("ref mode no video -> r2v", resolver({"content": {"task_mode": "ref"}}, 0, False) == "r2v") check("one image -> i2v", resolver({"content": {"task_mode": "default"}}, 1, False) == "i2v") check("edit mode no images -> v2v", resolver({"content": {"task_mode": "edit"}}, 0, False) == "v2v") # --------------------------------------------------------------------------- # 3. utils/llm_api.py - reference-mode with zero media -> text-only cloud mode # --------------------------------------------------------------------------- def verify_llm_api(): print("\n== 3. utils/llm_api.py: _h3_media_for_task_type ==") llm_file = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "utils", "llm_api.py" ) media_resolver = extract_functions(llm_file, ["_h3_media_for_task_type"])["_h3_media_for_task_type"] check("r2v with zero media -> t2va text-only (no raise)", media_resolver("r2v", [], [], []) == ("t2va", [], [], [])) check("rv2v with zero media -> t2va text-only", media_resolver("rv2v", [], [], []) == ("t2va", [], [], [])) check("r2v with media -> r2va", media_resolver("r2v", ["img1", "img2"], ["vid"], ["aud"]) == ("r2va", ["img1", "img2"], ["vid"], ["aud"])) check("direct t2v still -> t2va", media_resolver("t2v", [], [], []) == ("t2va", [], [], [])) check("i2v with image -> i2va", media_resolver("i2v", ["img"], [], []) == ("i2va", ["img"], [], [])) # --------------------------------------------------------------------------- # 4. moxie_loader.py - one-model UI + auto-picked auxiliary models # --------------------------------------------------------------------------- def _install_loader_stubs(state): comfy_api = types.ModuleType("comfy_api") comfy_latest = types.ModuleType("comfy_api.latest") class _FakeDiffusionModel: def clone(self): return _FakeDiffusionModel() def set_model_optimized_attention(self, attention_function): state.calls.append(("set_model_optimized_attention",)) return None state.model = _FakeDiffusionModel() class _Port: def __init__(self, name, **kwargs): self.name = name self.kwargs = kwargs class _Schema: def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) class _NodeOutput: def __init__(self, *values): self.values = values io = types.SimpleNamespace( Schema=_Schema, NodeOutput=_NodeOutput, ComfyNode=type("ComfyNode", (), {}), Combo=types.SimpleNamespace(Input=staticmethod(lambda name, **kw: _Port(name, **kw))), Float=types.SimpleNamespace(Input=staticmethod(lambda name, **kw: _Port(name, **kw))), Model=types.SimpleNamespace(Output=staticmethod(lambda name=None, **kw: _Port(name, **kw))), Clip=types.SimpleNamespace(Output=staticmethod(lambda name=None, **kw: _Port(name, **kw))), Vae=types.SimpleNamespace(Output=staticmethod(lambda name=None, **kw: _Port(name, **kw))), ) comfy_latest.io = io comfy_api.latest = comfy_latest sys.modules["comfy_api"] = comfy_api sys.modules["comfy_api.latest"] = comfy_latest folder_paths = types.ModuleType("folder_paths") folder_paths.get_folder_paths = lambda kind: [] sys.modules["folder_paths"] = folder_paths comfy = types.ModuleType("comfy") class _FakeVAE: def __init__(self, sd=None, metadata=None): self.sd = sd def throw_exception_if_invalid(self): return None class _CLIPType(enum.Enum): MINIMAX = "minimax" def load_diffusion_model(path, model_options=None): state.calls.append(("load_diffusion_model", os.path.basename(path))) return state.model def load_clip(ckpt_paths=None, embedding_directory=None, clip_type=None, model_options=None): state.calls.append(("load_clip", clip_type)) return state.clip def load_torch_file(path, safe_load=False, return_metadata=False): state.calls.append(("load_torch_file", os.path.basename(path))) sd = {"dummy": 1} return (sd, {"meta": True}) if return_metadata else sd def load_lora_for_models(model, clip, lora_sd, strength_model, strength_clip, lora_metadata=None): state.calls.append(("load_lora_for_models", strength_model, strength_clip)) return state.lora_model, clip def get_attention_function(name, *args): state.calls.append(("get_attention_function", name)) return state.attention_fn def state_dict_prefix_replace(sd, mapping, filter_keys=False): return sd comfy.sd = types.SimpleNamespace( load_diffusion_model=load_diffusion_model, load_clip=load_clip, load_lora_for_models=load_lora_for_models, VAE=_FakeVAE, CLIPType=_CLIPType, ) comfy.utils = types.SimpleNamespace( load_torch_file=load_torch_file, state_dict_prefix_replace=state_dict_prefix_replace, ) comfy_ldm = types.ModuleType("comfy.ldm") comfy_ldm_modules = types.ModuleType("comfy.ldm.modules") comfy_attention = types.ModuleType("comfy.ldm.modules.attention") comfy_attention.get_attention_function = get_attention_function comfy.ldm = comfy_ldm comfy.ldm.modules = comfy_ldm_modules comfy.ldm.modules.attention = comfy_attention comfy.__path__ = [] # make "comfy" importable as a package sys.modules["comfy"] = comfy sys.modules["comfy.sd"] = comfy.sd sys.modules["comfy.utils"] = comfy.utils sys.modules["comfy.ldm"] = comfy_ldm sys.modules["comfy.ldm.modules"] = comfy_ldm_modules sys.modules["comfy.ldm.modules.attention"] = comfy_attention def _load_loader_module(): pack_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) loader_file = os.path.join(pack_dir, "moxie_loader.py") spec = importlib.util.spec_from_file_location("moxie_loader_standalone", loader_file) module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) return module def verify_loader(): print("\n== 4. moxie_loader.py: single-model UI + auto-picked aux models ==") state = types.SimpleNamespace( calls=[], clip=object(), attention_fn=object(), lora_model=object(), ) _install_loader_stubs(state) loader = _load_loader_module() schema = loader.MoxieMultimediaLoader.define_schema() input_names = [port.name for port in schema.inputs] output_names = [port.name for port in schema.outputs] check("schema shows exactly the single-model inputs", input_names == ["diffusion_model", "weight_dtype", "attention_backend"], repr(input_names)) for hidden in ("lora_name", "lora_strength_model", "lora_strength_clip", "video_vae", "audio_vae", "clip", "clip_device"): check(f"widget hidden: {hidden}", hidden not in input_names) check("outputs still model/clip/video_vae/audio_vae", [port.name or port.kwargs.get("display_name") for port in schema.outputs] == ["model", "clip", "video_vae", "audio_vae"]) with tempfile.TemporaryDirectory() as tmp: models = os.path.join(tmp, "models") for subdir, name in ( ("diffusion", "moxie.safetensors"), ("vae", "video_vae.safetensors"), ("audio_vae", "audio_vae.safetensors"), ("clip", "MM-VL.safetensors"), ("loras", "MM3step.safetensors"), ): os.makedirs(os.path.join(models, subdir), exist_ok=True) with open(os.path.join(models, subdir, name), "wb") as handle: handle.write(b"stub") loader.MODELS_DIR = models loader._loaded_lora = None result = loader.MoxieMultimediaLoader.execute( diffusion_model="moxie.safetensors", weight_dtype="default", attention_backend="comfy_kitchen_attention", ) names = [call[0] for call in state.calls] check("full chain runs: model -> attention -> clip -> lora -> vaes", names == [ "load_diffusion_model", "get_attention_function", "set_model_optimized_attention", "load_clip", "load_torch_file", "load_lora_for_models", "load_torch_file", "load_torch_file", ], repr(names)) lora_calls = [call for call in state.calls if call[0] == "load_lora_for_models"] check("LoRA auto-applied at strength 1.0/1.0", lora_calls == [("load_lora_for_models", 1.0, 1.0)], repr(lora_calls)) clip_calls = [call for call in state.calls if call[0] == "load_clip"] check("CLIP auto-picked and loaded with the locked recipe", len(clip_calls) == 1 and clip_calls[0][1] is not None, repr(clip_calls)) check("four outputs returned", isinstance(result, loader.io.NodeOutput) and len(result.values) == 4) check("model output carries LoRA", result.values[0] is state.lora_model) check("clip output is the loaded clip", result.values[1] is state.clip) # Empty loras folder -> LoRA silently skipped, everything else loads. os.remove(os.path.join(models, "loras", "MM3step.safetensors")) state.calls.clear() loader._loaded_lora = None loader.MoxieMultimediaLoader.execute( diffusion_model="moxie.safetensors", weight_dtype="default", attention_backend="pytorch_attention", ) check("empty loras folder skips LoRA without breaking the load", "load_lora_for_models" not in [call[0] for call in state.calls]) # Missing clip folder -> loud setup error (same message as before). state.calls.clear() os.rename(os.path.join(models, "clip"), os.path.join(models, "clip_backup")) try: loader.MoxieMultimediaLoader.execute( diffusion_model="moxie.safetensors", weight_dtype="default", attention_backend="pytorch_attention", ) check("missing clip raises setup error", False) except RuntimeError as error: check("missing clip raises setup error", "no model files found in models/clip" in str(error), str(error)) os.rename(os.path.join(models, "clip_backup"), os.path.join(models, "clip")) # Multiple files -> deterministic first-sorted pick, no crash. with open(os.path.join(models, "clip", "AAA-other.safetensors"), "wb") as handle: handle.write(b"stub2") picked = loader._auto_pick("clip") check("multiple clip files pick first sorted deterministically", picked == "AAA-other.safetensors", repr(picked)) def main(): verify_h3_project() verify_basic_node() verify_llm_api() verify_loader() print() if FAILED: print(f"FAILED CHECKS ({len(FAILED)}): {FAILED}") return 1 print("ALL CHECKS PASSED") return 0 if __name__ == "__main__": sys.exit(main())