|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +"""Path-routing tests for fastvideo.utils.maybe_download_lora.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import huggingface_hub |
| 6 | + |
| 7 | +import fastvideo.utils as fv_utils |
| 8 | +from fastvideo.utils import maybe_download_lora |
| 9 | + |
| 10 | + |
| 11 | +def test_triple_slash_downloads_single_file(monkeypatch): |
| 12 | + """org/repo/sub/file.safetensors -> hf_hub_download(repo_id=org/repo, filename=sub/file).""" |
| 13 | + calls: list[dict] = [] |
| 14 | + |
| 15 | + def fake_hf_hub_download(*, repo_id, filename, local_dir=None, **kwargs): |
| 16 | + calls.append({"repo_id": repo_id, "filename": filename, "local_dir": local_dir}) |
| 17 | + return f"/cache/{repo_id}/{filename}" |
| 18 | + |
| 19 | + monkeypatch.setattr(huggingface_hub, "hf_hub_download", fake_hf_hub_download) |
| 20 | + |
| 21 | + result = maybe_download_lora("vita-video-gen/svi-model/version-1.0/svi-shot.safetensors") |
| 22 | + |
| 23 | + assert len(calls) == 1 |
| 24 | + assert calls[0]["repo_id"] == "vita-video-gen/svi-model" |
| 25 | + assert calls[0]["filename"] == "version-1.0/svi-shot.safetensors" |
| 26 | + assert result == "/cache/vita-video-gen/svi-model/version-1.0/svi-shot.safetensors" |
| 27 | + |
| 28 | + |
| 29 | +def test_triple_slash_keeps_only_first_two_segments_as_repo(monkeypatch): |
| 30 | + """A deeper nesting still maps repo_id to the first two segments.""" |
| 31 | + captured: dict = {} |
| 32 | + |
| 33 | + def fake_hf_hub_download(*, repo_id, filename, local_dir=None, **kwargs): |
| 34 | + captured["repo_id"] = repo_id |
| 35 | + captured["filename"] = filename |
| 36 | + return "ok" |
| 37 | + |
| 38 | + monkeypatch.setattr(huggingface_hub, "hf_hub_download", fake_hf_hub_download) |
| 39 | + |
| 40 | + maybe_download_lora("org/repo/a/b/c/weights.safetensors") |
| 41 | + |
| 42 | + assert captured["repo_id"] == "org/repo" |
| 43 | + assert captured["filename"] == "a/b/c/weights.safetensors" |
| 44 | + |
| 45 | + |
| 46 | +def test_local_file_short_circuits(tmp_path, monkeypatch): |
| 47 | + """An existing local .safetensors file is returned verbatim, no download.""" |
| 48 | + |
| 49 | + def boom(*args, **kwargs): |
| 50 | + raise AssertionError("hf_hub_download must not be called for a local file") |
| 51 | + |
| 52 | + monkeypatch.setattr(huggingface_hub, "hf_hub_download", boom) |
| 53 | + |
| 54 | + f = tmp_path / "version-1.0" / "svi-shot.safetensors" |
| 55 | + f.parent.mkdir(parents=True) |
| 56 | + f.write_bytes(b"\x00") |
| 57 | + |
| 58 | + assert maybe_download_lora(str(f)) == str(f) |
| 59 | + |
| 60 | + |
| 61 | +def test_plain_repo_id_falls_through_to_repo_download(monkeypatch): |
| 62 | + """A two-segment HF id (no .safetensors suffix) does NOT hit the single-file branch.""" |
| 63 | + |
| 64 | + def boom(*args, **kwargs): |
| 65 | + raise AssertionError("two-segment repo id must not take the single-file branch") |
| 66 | + |
| 67 | + monkeypatch.setattr(huggingface_hub, "hf_hub_download", boom) |
| 68 | + monkeypatch.setattr(fv_utils, "maybe_download_model", lambda *a, **k: "/cache/org/repo") |
| 69 | + monkeypatch.setattr(fv_utils, "_best_guess_weight_name", lambda *a, **k: "adapter.safetensors") |
| 70 | + |
| 71 | + result = maybe_download_lora("org/repo") |
| 72 | + |
| 73 | + assert result == "/cache/org/repo/adapter.safetensors" |
0 commit comments