|
15 | 15 | """Tests for audio utilities and VLLMMultimodalModel audio input handling.""" |
16 | 16 |
|
17 | 17 | import base64 |
| 18 | +import contextlib |
18 | 19 | import os |
19 | 20 | import tempfile |
20 | 21 | from unittest.mock import patch |
21 | 22 |
|
22 | 23 | import pytest |
23 | 24 |
|
24 | 25 | from nemo_skills.inference.model.audio_utils import audio_file_to_base64 |
| 26 | +from nemo_skills.inference.model.vllm import VLLMModel |
25 | 27 | from nemo_skills.inference.model.vllm_multimodal import VLLMMultimodalModel |
26 | 28 |
|
27 | 29 |
|
@@ -215,3 +217,78 @@ def test_needs_audio_chunking_task_type_filter(mock_vllm_multimodal_model): |
215 | 217 | # Task type in filter but file doesn't exist - should return False gracefully |
216 | 218 | needs_chunking, _, _ = mock_vllm_multimodal_model._needs_audio_chunking(messages, task_type="transcription") |
217 | 219 | assert needs_chunking is False |
| 220 | + |
| 221 | + |
| 222 | +@contextlib.contextmanager |
| 223 | +def _stub_vllm_base(): |
| 224 | + """Run the real ``VLLMMultimodalModel.__init__`` while stubbing the heavy |
| 225 | + ``VLLMModel`` base so no server / tokenizer / network setup is needed. |
| 226 | +
|
| 227 | + The base only needs to provide the attributes the audio-config logic reads |
| 228 | + (``base_url`` and ``output_dir``); everything the assertions cover lives in |
| 229 | + ``VLLMMultimodalModel.__init__`` itself. |
| 230 | + """ |
| 231 | + |
| 232 | + def _init(self, model=None, base_url=None, **kwargs): |
| 233 | + self.base_url = base_url |
| 234 | + self.output_dir = kwargs.get("output_dir") |
| 235 | + self._tunnel = None # consumed by BaseModel.__del__ |
| 236 | + |
| 237 | + with patch.object(VLLMModel, "__init__", _init): |
| 238 | + yield |
| 239 | + |
| 240 | + |
| 241 | +def test_audio_as_path_defaults_local_default_url(): |
| 242 | + """Local vLLM (no base_url) defaults to audio_url + audio_as_path=True.""" |
| 243 | + with _stub_vllm_base(): |
| 244 | + model = VLLMMultimodalModel(base_url=None) |
| 245 | + assert model._external_api_mode is False |
| 246 | + assert model.audio_format == "audio_url" |
| 247 | + assert model.audio_as_path is True |
| 248 | + |
| 249 | + |
| 250 | +def test_audio_as_path_defaults_local_explicit_localhost(): |
| 251 | + """Explicit local URL is still treated as local (path default on).""" |
| 252 | + with _stub_vllm_base(): |
| 253 | + model = VLLMMultimodalModel(base_url="http://127.0.0.1:5000/v1") |
| 254 | + assert model._external_api_mode is False |
| 255 | + assert model.audio_format == "audio_url" |
| 256 | + assert model.audio_as_path is True |
| 257 | + |
| 258 | + |
| 259 | +def test_audio_as_path_defaults_external_api(): |
| 260 | + """External API defaults to input_audio + audio_as_path=False.""" |
| 261 | + with _stub_vllm_base(): |
| 262 | + model = VLLMMultimodalModel(base_url="https://inference-api.nvidia.com/v1") |
| 263 | + assert model._external_api_mode is True |
| 264 | + assert model.audio_format == "input_audio" |
| 265 | + assert model.audio_as_path is False |
| 266 | + |
| 267 | + |
| 268 | +def test_audio_as_path_local_base64_opt_out(): |
| 269 | + """audio_as_path=False is allowed for local audio_url (base64 fallback).""" |
| 270 | + with _stub_vllm_base(): |
| 271 | + model = VLLMMultimodalModel(base_url=None, audio_as_path=False) |
| 272 | + assert model.audio_format == "audio_url" |
| 273 | + assert model.audio_as_path is False |
| 274 | + |
| 275 | + |
| 276 | +def test_audio_as_path_rejected_for_external_api(): |
| 277 | + """audio_as_path=True against an external API must raise.""" |
| 278 | + with _stub_vllm_base(): |
| 279 | + with pytest.raises(ValueError, match="audio_as_path is only supported"): |
| 280 | + VLLMMultimodalModel(base_url="https://inference-api.nvidia.com/v1", audio_as_path=True) |
| 281 | + |
| 282 | + |
| 283 | +def test_audio_as_path_rejected_for_input_audio_format(): |
| 284 | + """audio_as_path=True with input_audio format must raise even when local.""" |
| 285 | + with _stub_vllm_base(): |
| 286 | + with pytest.raises(ValueError, match="audio_as_path is only supported"): |
| 287 | + VLLMMultimodalModel(base_url=None, audio_format="input_audio", audio_as_path=True) |
| 288 | + |
| 289 | + |
| 290 | +def test_unsupported_audio_format_raises(): |
| 291 | + """An unknown audio_format is rejected.""" |
| 292 | + with _stub_vllm_base(): |
| 293 | + with pytest.raises(ValueError, match="Unsupported audio_format"): |
| 294 | + VLLMMultimodalModel(base_url=None, audio_format="bogus") |
0 commit comments