from __future__ import annotations from pathlib import Path import pytest from src.config import settings from src.core.services.abmil_checkpoint import resolve_abmil_checkpoint_path def test_resolve_abmil_checkpoint_local_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: pt = tmp_path / "production_model.pt" pt.write_bytes(b"not-a-real-checkpoint") monkeypatch.setattr(settings, "abmil_hf_repo_id", None) monkeypatch.setattr(settings, "model_checkpoint_path", str(pt)) resolved = resolve_abmil_checkpoint_path() assert Path(resolved) == pt.resolve() def test_resolve_abmil_checkpoint_local_missing(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "abmil_hf_repo_id", None) monkeypatch.setattr(settings, "model_checkpoint_path", "/nonexistent/path/model.pt") with pytest.raises(RuntimeError, match="MODEL_CHECKPOINT_PATH"): resolve_abmil_checkpoint_path() def test_resolve_abmil_checkpoint_hub(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: cached = tmp_path / "from_hub.pt" cached.write_bytes(b"x") monkeypatch.setattr(settings, "abmil_hf_repo_id", "org/private-abmil") monkeypatch.setattr(settings, "abmil_hf_filename", "weights.pt") monkeypatch.setattr(settings, "abmil_hf_revision", "v1.0.0") monkeypatch.setattr(settings, "abmil_hf_subfolder", "checkpoints") monkeypatch.setattr(settings, "abmil_hf_local_files_only", False) monkeypatch.setattr(settings, "model_checkpoint_path", "/ignored/local.pt") captured: dict = {} def fake_hf_hub_download(**kwargs: object) -> str: captured.update(kwargs) return str(cached) monkeypatch.setattr( "src.core.services.abmil_checkpoint.hf_hub_download", fake_hf_hub_download, ) out = resolve_abmil_checkpoint_path() assert out == str(cached) assert captured["repo_id"] == "org/private-abmil" assert captured["filename"] == "weights.pt" assert captured["revision"] == "v1.0.0" assert captured["subfolder"] == "checkpoints" assert captured["local_files_only"] is False def test_resolve_abmil_checkpoint_not_configured(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "abmil_hf_repo_id", None) monkeypatch.setattr(settings, "model_checkpoint_path", "") with pytest.raises(RuntimeError, match="ABMIL checkpoint not configured"): resolve_abmil_checkpoint_path() def test_resolve_abmil_checkpoint_hub_empty_filename(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(settings, "abmil_hf_repo_id", "org/repo") monkeypatch.setattr(settings, "abmil_hf_filename", " ") with pytest.raises(RuntimeError, match="ABMIL_HF_FILENAME"): resolve_abmil_checkpoint_path()