|
1 | | -from comfy_cli.command.models.models import check_civitai_url, check_huggingface_url |
| 1 | +import pathlib |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import typer.testing |
| 5 | + |
| 6 | +from comfy_cli.command.models.models import app, check_civitai_url, check_huggingface_url, list_models |
| 7 | + |
| 8 | + |
| 9 | +def _make_model_tree(tmp_path: pathlib.Path) -> pathlib.Path: |
| 10 | + """Create a realistic model directory tree and return its root.""" |
| 11 | + model_dir = tmp_path / "models" |
| 12 | + # Root-level file |
| 13 | + (model_dir / "root_model.safetensors").parent.mkdir(parents=True, exist_ok=True) |
| 14 | + (model_dir / "root_model.safetensors").write_bytes(b"x" * 100) |
| 15 | + # One level deep (checkpoints/) |
| 16 | + (model_dir / "checkpoints").mkdir() |
| 17 | + (model_dir / "checkpoints" / "sd15.safetensors").write_bytes(b"x" * 200) |
| 18 | + # Two levels deep (loras/SD1.5/) |
| 19 | + (model_dir / "loras" / "SD1.5").mkdir(parents=True) |
| 20 | + (model_dir / "loras" / "SD1.5" / "detail.safetensors").write_bytes(b"x" * 300) |
| 21 | + # An empty subdirectory (should be ignored) |
| 22 | + (model_dir / "empty_dir").mkdir() |
| 23 | + return model_dir |
| 24 | + |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | +# list_models tests |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | + |
| 30 | + |
| 31 | +def test_list_models_finds_files_in_subdirectories(tmp_path): |
| 32 | + model_dir = _make_model_tree(tmp_path) |
| 33 | + result = list_models(model_dir) |
| 34 | + names = {f.name for f in result} |
| 35 | + assert "sd15.safetensors" in names |
| 36 | + # Two levels deep: loras/SD1.5/detail.safetensors |
| 37 | + deep = [f for f in result if f.name == "detail.safetensors"] |
| 38 | + assert len(deep) == 1 |
| 39 | + assert deep[0].relative_to(model_dir) == pathlib.Path("loras/SD1.5/detail.safetensors") |
| 40 | + |
| 41 | + |
| 42 | +def test_list_models_finds_root_level_files(tmp_path): |
| 43 | + model_dir = _make_model_tree(tmp_path) |
| 44 | + result = list_models(model_dir) |
| 45 | + names = {f.name for f in result} |
| 46 | + assert "root_model.safetensors" in names |
| 47 | + |
| 48 | + |
| 49 | +def test_list_models_returns_empty_for_missing_directory(tmp_path): |
| 50 | + assert list_models(tmp_path / "nonexistent") == [] |
| 51 | + |
| 52 | + |
| 53 | +def test_list_models_ignores_directories(tmp_path): |
| 54 | + model_dir = _make_model_tree(tmp_path) |
| 55 | + result = list_models(model_dir) |
| 56 | + assert all(f.is_file() for f in result) |
| 57 | + dir_names = {f.name for f in result} |
| 58 | + assert "empty_dir" not in dir_names |
| 59 | + assert "checkpoints" not in dir_names |
| 60 | + |
| 61 | + |
| 62 | +# --------------------------------------------------------------------------- |
| 63 | +# list_command tests |
| 64 | +# --------------------------------------------------------------------------- |
| 65 | + |
| 66 | +runner = typer.testing.CliRunner() |
| 67 | + |
| 68 | + |
| 69 | +def test_list_command_shows_type_column(tmp_path): |
| 70 | + _make_model_tree(tmp_path) |
| 71 | + with patch("comfy_cli.command.models.models.get_workspace", return_value=tmp_path): |
| 72 | + result = runner.invoke(app, ["list", "--relative-path", "models"]) |
| 73 | + assert result.exit_code == 0 |
| 74 | + assert "Type" in result.output |
| 75 | + assert "checkpoints" in result.output |
| 76 | + # Deeply nested type should show full subdirectory path |
| 77 | + assert "loras/SD1.5" in result.output |
| 78 | + # Root-level model should still appear |
| 79 | + assert "root_model.safetensors" in result.output |
| 80 | + |
| 81 | + |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | +# remove tests |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | + |
| 86 | + |
| 87 | +def test_remove_with_path_traversal_is_rejected(tmp_path): |
| 88 | + model_dir = tmp_path / "models" |
| 89 | + model_dir.mkdir() |
| 90 | + (model_dir / "legit.bin").write_bytes(b"x") |
| 91 | + |
| 92 | + # Create a file outside the model dir that an attacker might target |
| 93 | + secret = tmp_path / "secret.txt" |
| 94 | + secret.write_text("sensitive") |
| 95 | + |
| 96 | + with patch("comfy_cli.command.models.models.get_workspace", return_value=tmp_path): |
| 97 | + result = runner.invoke( |
| 98 | + app, |
| 99 | + ["remove", "--relative-path", "models", "--model-names", "../secret.txt", "--confirm"], |
| 100 | + ) |
| 101 | + # The secret file must still exist |
| 102 | + assert secret.exists() |
| 103 | + assert "Invalid model path" in result.output |
| 104 | + |
| 105 | + |
| 106 | +def test_remove_deletes_model_in_subdirectory(tmp_path): |
| 107 | + model_dir = _make_model_tree(tmp_path) |
| 108 | + target = model_dir / "checkpoints" / "sd15.safetensors" |
| 109 | + assert target.exists() |
| 110 | + |
| 111 | + with patch("comfy_cli.command.models.models.get_workspace", return_value=tmp_path): |
| 112 | + result = runner.invoke( |
| 113 | + app, |
| 114 | + ["remove", "--relative-path", "models", "--model-names", "checkpoints/sd15.safetensors", "--confirm"], |
| 115 | + ) |
| 116 | + assert result.exit_code == 0 |
| 117 | + assert not target.exists() |
| 118 | + |
| 119 | + |
| 120 | +def test_remove_rejects_directory_name(tmp_path): |
| 121 | + _make_model_tree(tmp_path) |
| 122 | + |
| 123 | + with patch("comfy_cli.command.models.models.get_workspace", return_value=tmp_path): |
| 124 | + result = runner.invoke( |
| 125 | + app, |
| 126 | + ["remove", "--relative-path", "models", "--model-names", "checkpoints", "--confirm"], |
| 127 | + ) |
| 128 | + # Directory must still exist, not be unlinked |
| 129 | + assert (tmp_path / "models" / "checkpoints").is_dir() |
| 130 | + assert "not found" in result.output |
| 131 | + |
| 132 | + |
| 133 | +def test_remove_deletes_root_level_model(tmp_path): |
| 134 | + model_dir = _make_model_tree(tmp_path) |
| 135 | + target = model_dir / "root_model.safetensors" |
| 136 | + assert target.exists() |
| 137 | + |
| 138 | + with patch("comfy_cli.command.models.models.get_workspace", return_value=tmp_path): |
| 139 | + result = runner.invoke( |
| 140 | + app, |
| 141 | + ["remove", "--relative-path", "models", "--model-names", "root_model.safetensors", "--confirm"], |
| 142 | + ) |
| 143 | + assert result.exit_code == 0 |
| 144 | + assert not target.exists() |
| 145 | + |
| 146 | + |
| 147 | +# --------------------------------------------------------------------------- |
| 148 | +# URL validation tests (existing, kept for completeness) |
| 149 | +# --------------------------------------------------------------------------- |
2 | 150 |
|
3 | 151 |
|
4 | 152 | def test_valid_model_url(): |
|
0 commit comments