11"""Unit tests for src.diarizer — all Pyannote calls are mocked."""
22
3+ import sys
34from pathlib import Path
45from unittest .mock import MagicMock , patch
56
@@ -37,8 +38,13 @@ def test_loads_and_moves_to_device(self):
3738 mock_pipeline_cls = MagicMock ()
3839 mock_pipeline_cls .from_pretrained .return_value = mock_pipeline
3940
40- # Pipeline is imported locally inside load_pipeline, so patch at the source.
41- with patch ("pyannote.audio.Pipeline" , mock_pipeline_cls ):
41+ # `load_pipeline` does a local `from pyannote.audio import Pipeline`.
42+ # Patching the live attribute triggers the real module import, which pulls in
43+ # matplotlib and crashes in headless CI. Pre-populate sys.modules instead so
44+ # the local import inside load_pipeline picks up the mock without any I/O.
45+ mock_pa = MagicMock ()
46+ mock_pa .Pipeline = mock_pipeline_cls
47+ with patch .dict (sys .modules , {"pyannote.audio" : mock_pa }):
4248 result = load_pipeline (DEFAULT_MODEL , "cuda" , "hf_fake" )
4349
4450 mock_pipeline_cls .from_pretrained .assert_called_once_with (DEFAULT_MODEL )
@@ -49,7 +55,9 @@ def test_raises_diarization_error_on_failure(self):
4955 mock_pipeline_cls = MagicMock ()
5056 mock_pipeline_cls .from_pretrained .side_effect = RuntimeError ("model not found" )
5157
52- with patch ("pyannote.audio.Pipeline" , mock_pipeline_cls ):
58+ mock_pa = MagicMock ()
59+ mock_pa .Pipeline = mock_pipeline_cls
60+ with patch .dict (sys .modules , {"pyannote.audio" : mock_pa }):
5361 with pytest .raises (DiarizationError , match = "Failed to load Pyannote pipeline" ):
5462 load_pipeline (DEFAULT_MODEL , "cuda" , "hf_fake" )
5563
0 commit comments