|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Regression test for SharpAI/DeepCamera#207: load_optimized() crashed with |
| 4 | +"No module named 'ultralytics'" on mps machines where framework_ok is False |
| 5 | +(the CoreML execution-provider check failed), because the final fallback |
| 6 | +branch unconditionally did `from ultralytics import YOLO` — but mps installs |
| 7 | +deliberately never ship torch/ultralytics (see requirements_mps.txt). |
| 8 | +
|
| 9 | +Run: python -m pytest skills/lib/test_env_config_mps_fallback.py -v |
| 10 | +""" |
| 11 | + |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | +from unittest import mock |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 19 | +from env_config import HardwareEnv # noqa: E402 |
| 20 | + |
| 21 | + |
| 22 | +def _mps_env(framework_ok=False): |
| 23 | + return HardwareEnv( |
| 24 | + backend="mps", |
| 25 | + device="mps", |
| 26 | + export_format="onnx", |
| 27 | + framework_ok=framework_ok, |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +class TestMpsFrameworkMissingFallback: |
| 32 | + """load_optimized() when framework_ok is False on mps (Brian's exact case).""" |
| 33 | + |
| 34 | + def test_uses_onnx_coreml_when_prebuilt_model_exists(self, tmp_path, monkeypatch): |
| 35 | + monkeypatch.chdir(tmp_path) |
| 36 | + onnx_path = tmp_path / "yolo26n.onnx" |
| 37 | + onnx_path.write_bytes(b"fake-onnx") |
| 38 | + |
| 39 | + env = _mps_env(framework_ok=False) |
| 40 | + sentinel = object() |
| 41 | + with mock.patch.object(env, "_load_onnx_coreml", return_value=sentinel) as m: |
| 42 | + model, fmt = env.load_optimized("yolo26n", use_optimized=True) |
| 43 | + |
| 44 | + m.assert_called_once_with("yolo26n.onnx") |
| 45 | + assert model is sentinel |
| 46 | + assert fmt == "onnx" |
| 47 | + |
| 48 | + def test_never_imports_ultralytics_when_framework_missing(self, tmp_path, monkeypatch): |
| 49 | + """The historical bug: this path must not need ultralytics at all.""" |
| 50 | + monkeypatch.chdir(tmp_path) |
| 51 | + (tmp_path / "yolo26n.onnx").write_bytes(b"fake-onnx") |
| 52 | + |
| 53 | + env = _mps_env(framework_ok=False) |
| 54 | + with mock.patch.object(env, "_load_onnx_coreml", return_value=object()): |
| 55 | + with mock.patch.dict(sys.modules, {"ultralytics": None}): |
| 56 | + # If the code path tried `import ultralytics` here, this would |
| 57 | + # raise ImportError since sys.modules["ultralytics"] is None. |
| 58 | + env.load_optimized("yolo26n", use_optimized=True) |
| 59 | + |
| 60 | + def test_raises_clear_error_when_no_prebuilt_model_and_no_framework(self, tmp_path, monkeypatch): |
| 61 | + monkeypatch.chdir(tmp_path) |
| 62 | + env = _mps_env(framework_ok=False) |
| 63 | + with pytest.raises(RuntimeError, match="torch/ultralytics"): |
| 64 | + env.load_optimized("yolo26n", use_optimized=True) |
0 commit comments