import sys from types import SimpleNamespace from typing import Any # The detector only needs NumPy for annotations; keep this unit test independent # from binary packages and real image/model execution. _inserted_numpy_stub = "numpy" not in sys.modules if _inserted_numpy_stub: sys.modules["numpy"] = SimpleNamespace( ndarray=object, isscalar=lambda value: isinstance(value, (int, float)), ) from src.config_loader import DetectorSettings from src.detector import PersonDetector if _inserted_numpy_stub: sys.modules.pop("numpy", None) class FakeModel: def __init__(self) -> None: self.kwargs: dict[str, Any] = {} def predict(self, **kwargs: Any) -> list[Any]: self.kwargs = kwargs return [] class FakeFrame: shape = (10, 10, 3) def make_settings(half_precision: bool) -> DetectorSettings: return DetectorSettings( model_path="unused.pt", device="auto", confidence_threshold=0.4, iou_threshold=0.5, image_size=640, person_class_id=0, max_detections=30, half_precision=half_precision, use_agnostic_nms=False, ) def run_detection(half_precision: bool, effective_half: bool) -> dict[str, Any]: detector = PersonDetector(make_settings(half_precision)) model = FakeModel() detector._model = model detector._device_name = "cuda" if effective_half else "cpu" detector._half = effective_half detector.detect(FakeFrame()) # type: ignore[arg-type] return model.kwargs def test_cpu_does_not_pass_half_when_disabled() -> None: assert "half" not in run_detection(half_precision=False, effective_half=False) def test_cpu_does_not_pass_half_when_requested_but_unavailable() -> None: assert "half" not in run_detection(half_precision=True, effective_half=False) def test_cuda_passes_half_only_when_effectively_enabled() -> None: kwargs = run_detection(half_precision=True, effective_half=True) assert kwargs["half"] is True assert "quantize" not in kwargs