|
| 1 | +"""Tests for V3 CLI argument parsing, quality presets, and routing logic.""" |
| 2 | +import pytest |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | + |
| 5 | + |
| 6 | +# ============================================================================ |
| 7 | +# Quality Presets |
| 8 | +# ============================================================================ |
| 9 | + |
| 10 | +class TestQualityPresets: |
| 11 | + def test_all_presets_exist(self): |
| 12 | + from scripts.animate import QUALITY_PRESETS |
| 13 | + assert "draft" in QUALITY_PRESETS |
| 14 | + assert "standard" in QUALITY_PRESETS |
| 15 | + assert "high" in QUALITY_PRESETS |
| 16 | + assert "max" in QUALITY_PRESETS |
| 17 | + |
| 18 | + def test_preset_has_required_keys(self): |
| 19 | + from scripts.animate import QUALITY_PRESETS |
| 20 | + for name, preset in QUALITY_PRESETS.items(): |
| 21 | + assert "num_inference_steps" in preset, f"{name} missing num_inference_steps" |
| 22 | + assert "guidance_scale" in preset, f"{name} missing guidance_scale" |
| 23 | + |
| 24 | + def test_draft_is_fastest(self): |
| 25 | + from scripts.animate import QUALITY_PRESETS |
| 26 | + assert QUALITY_PRESETS["draft"]["num_inference_steps"] < QUALITY_PRESETS["standard"]["num_inference_steps"] |
| 27 | + |
| 28 | + def test_max_is_slowest(self): |
| 29 | + from scripts.animate import QUALITY_PRESETS |
| 30 | + assert QUALITY_PRESETS["max"]["num_inference_steps"] > QUALITY_PRESETS["high"]["num_inference_steps"] |
| 31 | + |
| 32 | + def test_steps_increase_with_quality(self): |
| 33 | + from scripts.animate import QUALITY_PRESETS |
| 34 | + order = ["draft", "standard", "high", "max"] |
| 35 | + steps = [QUALITY_PRESETS[q]["num_inference_steps"] for q in order] |
| 36 | + assert steps == sorted(steps), "Steps should increase: draft < standard < high < max" |
| 37 | + |
| 38 | + |
| 39 | +# ============================================================================ |
| 40 | +# CLI Arg Parsing |
| 41 | +# ============================================================================ |
| 42 | + |
| 43 | +class TestCLIParsing: |
| 44 | + def _parse(self, args_list): |
| 45 | + """Helper to parse CLI args.""" |
| 46 | + import sys |
| 47 | + from scripts.animate import main_cli |
| 48 | + import argparse |
| 49 | + |
| 50 | + # We need to re-create the parser from main_cli, but since it calls parse_args() |
| 51 | + # and then runs, we'll test the parser construction instead |
| 52 | + from scripts.animate import main_cli |
| 53 | + import scripts.animate as animate_mod |
| 54 | + |
| 55 | + # Create parser manually (same as main_cli but without running) |
| 56 | + parser = argparse.ArgumentParser() |
| 57 | + parser.add_argument("--backend", type=str, default=None, |
| 58 | + choices=["auto", "wan", "hunyuan", "cogvideo", "ltx", "animatediff"]) |
| 59 | + parser.add_argument("--prompt", type=str, default=None) |
| 60 | + parser.add_argument("--negative-prompt", type=str, default=None) |
| 61 | + parser.add_argument("--model-path", type=str, default=None) |
| 62 | + parser.add_argument("--model-variant", type=str, default=None) |
| 63 | + parser.add_argument("--quality", type=str, default="standard", |
| 64 | + choices=["draft", "standard", "high", "max"]) |
| 65 | + parser.add_argument("--quantization", type=str, default=None, |
| 66 | + choices=["none", "nf4", "int8", "fp8"]) |
| 67 | + parser.add_argument("--offload", type=str, default=None, |
| 68 | + choices=["none", "model_cpu", "sequential_cpu"]) |
| 69 | + parser.add_argument("--no-compile", action="store_true") |
| 70 | + parser.add_argument("--steps", type=int, default=None) |
| 71 | + parser.add_argument("--guidance-scale", type=float, default=None) |
| 72 | + parser.add_argument("--seed", type=int, default=-1) |
| 73 | + parser.add_argument("--fps", type=int, default=8) |
| 74 | + parser.add_argument("--pipeline", type=str, default=None, |
| 75 | + choices=["legacy", "v2", "sdxl", "lightning"]) |
| 76 | + parser.add_argument("--config", type=str, default=None) |
| 77 | + parser.add_argument("--format", type=str, default="mp4", choices=["gif", "mp4"]) |
| 78 | + parser.add_argument("--L", type=int, default=0) |
| 79 | + parser.add_argument("--W", type=int, default=0) |
| 80 | + parser.add_argument("--H", type=int, default=0) |
| 81 | + parser.add_argument("--scheduler", type=str, default="ddim") |
| 82 | + parser.add_argument("--device", type=str, default=None) |
| 83 | + return parser.parse_args(args_list) |
| 84 | + |
| 85 | + def test_backend_auto(self): |
| 86 | + args = self._parse(["--backend", "auto", "--prompt", "test"]) |
| 87 | + assert args.backend == "auto" |
| 88 | + assert args.prompt == "test" |
| 89 | + |
| 90 | + def test_backend_wan(self): |
| 91 | + args = self._parse(["--backend", "wan", "--prompt", "hello"]) |
| 92 | + assert args.backend == "wan" |
| 93 | + |
| 94 | + def test_quality_default(self): |
| 95 | + args = self._parse(["--backend", "auto", "--prompt", "x"]) |
| 96 | + assert args.quality == "standard" |
| 97 | + |
| 98 | + def test_quality_override(self): |
| 99 | + args = self._parse(["--backend", "auto", "--prompt", "x", "--quality", "max"]) |
| 100 | + assert args.quality == "max" |
| 101 | + |
| 102 | + def test_legacy_pipeline(self): |
| 103 | + args = self._parse(["--pipeline", "v2", "--config", "test.yaml"]) |
| 104 | + assert args.pipeline == "v2" |
| 105 | + assert args.config == "test.yaml" |
| 106 | + |
| 107 | + def test_format_default(self): |
| 108 | + args = self._parse(["--backend", "auto", "--prompt", "x"]) |
| 109 | + assert args.format == "mp4" |
| 110 | + |
| 111 | + def test_format_gif(self): |
| 112 | + args = self._parse(["--backend", "auto", "--prompt", "x", "--format", "gif"]) |
| 113 | + assert args.format == "gif" |
| 114 | + |
| 115 | + def test_seed_default(self): |
| 116 | + args = self._parse(["--backend", "auto", "--prompt", "x"]) |
| 117 | + assert args.seed == -1 |
| 118 | + |
| 119 | + def test_seed_override(self): |
| 120 | + args = self._parse(["--backend", "auto", "--prompt", "x", "--seed", "42"]) |
| 121 | + assert args.seed == 42 |
| 122 | + |
| 123 | + def test_quantization(self): |
| 124 | + args = self._parse(["--backend", "wan", "--prompt", "x", "--quantization", "nf4"]) |
| 125 | + assert args.quantization == "nf4" |
| 126 | + |
| 127 | + def test_offload(self): |
| 128 | + args = self._parse(["--backend", "wan", "--prompt", "x", "--offload", "model_cpu"]) |
| 129 | + assert args.offload == "model_cpu" |
| 130 | + |
| 131 | + def test_no_compile(self): |
| 132 | + args = self._parse(["--backend", "wan", "--prompt", "x", "--no-compile"]) |
| 133 | + assert args.no_compile is True |
| 134 | + |
| 135 | + def test_dimensions(self): |
| 136 | + args = self._parse(["--backend", "wan", "--prompt", "x", "--W", "720", "--H", "480", "--L", "33"]) |
| 137 | + assert args.W == 720 |
| 138 | + assert args.H == 480 |
| 139 | + assert args.L == 33 |
| 140 | + |
| 141 | + |
| 142 | +# ============================================================================ |
| 143 | +# Prompt Loading |
| 144 | +# ============================================================================ |
| 145 | + |
| 146 | +class TestPromptLoading: |
| 147 | + def test_load_from_cli_prompt(self): |
| 148 | + from scripts.animate import _load_prompts |
| 149 | + args = MagicMock() |
| 150 | + args.config = None |
| 151 | + args.prompt = "a cat playing" |
| 152 | + args.negative_prompt = "bad quality" |
| 153 | + args.seed = 42 |
| 154 | + prompts = _load_prompts(args) |
| 155 | + assert len(prompts) == 1 |
| 156 | + assert prompts[0] == ("a cat playing", "bad quality", 42) |
| 157 | + |
| 158 | + def test_load_from_cli_no_negative(self): |
| 159 | + from scripts.animate import _load_prompts |
| 160 | + args = MagicMock() |
| 161 | + args.config = None |
| 162 | + args.prompt = "test" |
| 163 | + args.negative_prompt = None |
| 164 | + args.seed = -1 |
| 165 | + prompts = _load_prompts(args) |
| 166 | + assert prompts[0] == ("test", "", -1) |
| 167 | + |
| 168 | + def test_no_prompt_or_config_raises(self): |
| 169 | + from scripts.animate import _load_prompts |
| 170 | + args = MagicMock() |
| 171 | + args.config = None |
| 172 | + args.prompt = None |
| 173 | + with pytest.raises(ValueError, match="Provide either"): |
| 174 | + _load_prompts(args) |
| 175 | + |
| 176 | + def test_load_from_config(self, tmp_path): |
| 177 | + from scripts.animate import _load_prompts |
| 178 | + import yaml |
| 179 | + |
| 180 | + config_path = tmp_path / "test_config.yaml" |
| 181 | + config_data = [ |
| 182 | + {"prompt": ["a dog running", "a cat sleeping"], "n_prompt": ["ugly"], "seed": [10, 20]}, |
| 183 | + ] |
| 184 | + config_path.write_text(yaml.dump(config_data)) |
| 185 | + |
| 186 | + args = MagicMock() |
| 187 | + args.config = str(config_path) |
| 188 | + args.prompt = None |
| 189 | + prompts = _load_prompts(args) |
| 190 | + assert len(prompts) == 2 |
| 191 | + assert prompts[0][0] == "a dog running" |
| 192 | + assert prompts[0][2] == 10 |
| 193 | + assert prompts[1][0] == "a cat sleeping" |
| 194 | + assert prompts[1][2] == 20 |
0 commit comments