File size: 14,269 Bytes
93dc283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""Unit tests for STTProviderBase abstract class."""

import pytest
from unittest.mock import Mock, patch, MagicMock
import tempfile
from pathlib import Path

from src.infrastructure.base.stt_provider_base import STTProviderBase
from src.domain.models.audio_content import AudioContent
from src.domain.models.text_content import TextContent
from src.domain.exceptions import SpeechRecognitionException


class ConcreteSTTProvider(STTProviderBase):
    """Concrete implementation for testing."""
    
    def __init__(self, provider_name="test", supported_languages=None, available=True, models=None):
        super().__init__(provider_name, supported_languages)
        self._available = available
        self._models = models or ["model1", "model2"]
        self._should_fail = False
        self._transcription_result = "Hello world"
    
    def _perform_transcription(self, audio_path, model):
        if self._should_fail:
            raise Exception("Test transcription error")
        return self._transcription_result
    
    def is_available(self):
        return self._available
    
    def get_available_models(self):
        return self._models
    
    def get_default_model(self):
        return self._models[0] if self._models else "default"
    
    def set_should_fail(self, should_fail):
        self._should_fail = should_fail
    
    def set_transcription_result(self, result):
        self._transcription_result = result


class TestSTTProviderBase:
    """Test cases for STTProviderBase abstract class."""
    
    def setup_method(self):
        """Set up test fixtures."""
        self.provider = ConcreteSTTProvider()
        self.audio_content = AudioContent(
            data=b"fake_audio_data",
            format="wav",
            sample_rate=16000,
            duration=5.0,
            filename="test.wav"
        )
    
    def test_provider_initialization(self):
        """Test provider initialization with default values."""
        provider = ConcreteSTTProvider("test_provider", ["en", "es"])
        
        assert provider.provider_name == "test_provider"
        assert provider.supported_languages == ["en", "es"]
        assert isinstance(provider._temp_dir, Path)
        assert provider._temp_dir.exists()
    
    def test_provider_initialization_no_languages(self):
        """Test provider initialization without supported languages."""
        provider = ConcreteSTTProvider("test_provider")
        
        assert provider.provider_name == "test_provider"
        assert provider.supported_languages == []
    
    @patch('builtins.open', create=True)
    def test_transcribe_success(self, mock_open):
        """Test successful transcription."""
        mock_file = MagicMock()
        mock_open.return_value.__enter__.return_value = mock_file
        
        result = self.provider.transcribe(self.audio_content, "model1")
        
        assert isinstance(result, TextContent)
        assert result.text == "Hello world"
        assert result.language == "en"
        assert result.encoding == "utf-8"
    
    def test_transcribe_empty_audio_fails(self):
        """Test that empty audio data raises exception."""
        empty_audio = AudioContent(
            data=b"",
            format="wav",
            sample_rate=16000,
            duration=0.1
        )
        
        with pytest.raises(SpeechRecognitionException, match="Audio data cannot be empty"):
            self.provider.transcribe(empty_audio, "model1")
    
    def test_transcribe_audio_too_long_fails(self):
        """Test that audio longer than 1 hour raises exception."""
        long_audio = AudioContent(
            data=b"fake_audio_data",
            format="wav",
            sample_rate=16000,
            duration=3601.0  # Over 1 hour
        )
        
        with pytest.raises(SpeechRecognitionException, match="Audio duration exceeds maximum limit"):
            self.provider.transcribe(long_audio, "model1")
    
    def test_transcribe_audio_too_short_fails(self):
        """Test that audio shorter than 100ms raises exception."""
        short_audio = AudioContent(
            data=b"fake_audio_data",
            format="wav",
            sample_rate=16000,
            duration=0.05  # 50ms
        )
        
        with pytest.raises(SpeechRecognitionException, match="Audio duration too short"):
            self.provider.transcribe(short_audio, "model1")
    
    def test_transcribe_invalid_format_fails(self):
        """Test that invalid audio format raises exception."""
        # Create audio with invalid format by mocking is_valid_format
        invalid_audio = AudioContent(
            data=b"fake_audio_data",
            format="wav",
            sample_rate=16000,
            duration=5.0
        )
        
        with patch.object(invalid_audio, 'is_valid_format', False):
            with pytest.raises(SpeechRecognitionException, match="Unsupported audio format"):
                self.provider.transcribe(invalid_audio, "model1")
    
    @patch('builtins.open', create=True)
    def test_transcribe_provider_error(self, mock_open):
        """Test handling of provider-specific errors."""
        mock_file = MagicMock()
        mock_open.return_value.__enter__.return_value = mock_file
        
        self.provider.set_should_fail(True)
        
        with pytest.raises(SpeechRecognitionException, match="STT transcription failed"):
            self.provider.transcribe(self.audio_content, "model1")
    
    @patch('builtins.open', create=True)
    @patch('pathlib.Path.unlink')
    def test_transcribe_cleanup_temp_file(self, mock_unlink, mock_open):
        """Test that temporary files are cleaned up."""
        mock_file = MagicMock()
        mock_open.return_value.__enter__.return_value = mock_file
        
        self.provider.transcribe(self.audio_content, "model1")
        
        # Verify cleanup was attempted
        mock_unlink.assert_called()
    
    @patch('builtins.open', create=True)
    def test_preprocess_audio(self, mock_open):
        """Test audio preprocessing."""
        mock_file = MagicMock()
        mock_open.return_value.__enter__.return_value = mock_file
        
        processed_path = self.provider._preprocess_audio(self.audio_content)
        
        assert isinstance(processed_path, Path)
        assert processed_path.suffix == ".wav"
        mock_file.write.assert_called_once_with(self.audio_content.data)
    
    def test_preprocess_audio_error(self):
        """Test audio preprocessing error handling."""
        with patch('builtins.open', side_effect=IOError("Test error")):
            with pytest.raises(SpeechRecognitionException, match="Audio preprocessing failed"):
                self.provider._preprocess_audio(self.audio_content)
    
    @patch('pydub.AudioSegment.from_wav')
    @patch('pydub.AudioSegment.export')
    def test_convert_audio_format_wav(self, mock_export, mock_from_wav):
        """Test audio format conversion for WAV."""
        mock_audio = Mock()
        mock_audio.set_frame_rate.return_value.set_channels.return_value = mock_audio
        mock_from_wav.return_value = mock_audio
        
        test_path = Path("/tmp/test.wav")
        result_path = self.provider._convert_audio_format(test_path, self.audio_content)
        
        mock_from_wav.assert_called_once_with(test_path)
        mock_audio.set_frame_rate.assert_called_once_with(16000)
        mock_audio.set_channels.assert_called_once_with(1)
        mock_export.assert_called_once()
    
    @patch('pydub.AudioSegment.from_mp3')
    def test_convert_audio_format_mp3(self, mock_from_mp3):
        """Test audio format conversion for MP3."""
        mp3_audio = AudioContent(
            data=b"fake_mp3_data",
            format="mp3",
            sample_rate=44100,
            duration=5.0
        )
        
        mock_audio = Mock()
        mock_audio.set_frame_rate.return_value.set_channels.return_value = mock_audio
        mock_from_mp3.return_value = mock_audio
        
        test_path = Path("/tmp/test.mp3")
        self.provider._convert_audio_format(test_path, mp3_audio)
        
        mock_from_mp3.assert_called_once_with(test_path)
    
    def test_convert_audio_format_no_pydub(self):
        """Test audio format conversion when pydub is not available."""
        test_path = Path("/tmp/test.wav")
        
        with patch('pydub.AudioSegment', side_effect=ImportError("pydub not available")):
            result_path = self.provider._convert_audio_format(test_path, self.audio_content)
            
            # Should return original path when pydub is not available
            assert result_path == test_path
    
    def test_convert_audio_format_error(self):
        """Test audio format conversion error handling."""
        test_path = Path("/tmp/test.wav")
        
        with patch('pydub.AudioSegment.from_wav', side_effect=Exception("Conversion error")):
            result_path = self.provider._convert_audio_format(test_path, self.audio_content)
            
            # Should return original path on error
            assert result_path == test_path
    
    def test_detect_language_english(self):
        """Test language detection for English text."""
        english_text = "The quick brown fox jumps over the lazy dog and it is very nice"
        language = self.provider._detect_language(english_text)
        assert language == "en"
    
    def test_detect_language_few_indicators(self):
        """Test language detection with few English indicators."""
        text = "Hello world"
        language = self.provider._detect_language(text)
        assert language == "en"
    
    def test_detect_language_no_indicators(self):
        """Test language detection with no clear indicators."""
        text = "xyz abc def"
        language = self.provider._detect_language(text)
        assert language == "en"  # Should default to English
    
    def test_detect_language_error(self):
        """Test language detection error handling."""
        with patch.object(self.provider, '_detect_language', side_effect=Exception("Detection error")):
            language = self.provider._detect_language("test")
            assert language is None
    
    def test_ensure_temp_directory(self):
        """Test temporary directory creation."""
        temp_dir = self.provider._ensure_temp_directory()
        
        assert isinstance(temp_dir, Path)
        assert temp_dir.exists()
        assert temp_dir.is_dir()
        assert "stt_temp" in str(temp_dir)
    
    def test_cleanup_temp_file(self):
        """Test temporary file cleanup."""
        # Create a temporary file
        temp_file = self.provider._temp_dir / "test_file.wav"
        temp_file.touch()
        
        assert temp_file.exists()
        
        self.provider._cleanup_temp_file(temp_file)
        
        assert not temp_file.exists()
    
    def test_cleanup_temp_file_not_exists(self):
        """Test cleanup of non-existent file."""
        non_existent = Path("/tmp/non_existent_file.wav")
        
        # Should not raise exception
        self.provider._cleanup_temp_file(non_existent)
    
    def test_cleanup_temp_file_error(self):
        """Test cleanup error handling."""
        with patch('pathlib.Path.unlink', side_effect=OSError("Permission denied")):
            temp_file = Path("/tmp/test.wav")
            
            # Should not raise exception
            self.provider._cleanup_temp_file(temp_file)
    
    @patch('time.time')
    @patch('pathlib.Path.glob')
    def test_cleanup_old_temp_files(self, mock_glob, mock_time):
        """Test cleanup of old temporary files."""
        mock_time.return_value = 1000000
        
        # Mock old file
        old_file = Mock()
        old_file.is_file.return_value = True
        old_file.stat.return_value.st_mtime = 900000  # Old file
        
        # Mock recent file
        recent_file = Mock()
        recent_file.is_file.return_value = True
        recent_file.stat.return_value.st_mtime = 999000  # Recent file
        
        mock_glob.return_value = [old_file, recent_file]
        
        self.provider._cleanup_old_temp_files(24)
        
        # Old file should be deleted
        old_file.unlink.assert_called_once()
        recent_file.unlink.assert_not_called()
    
    def test_cleanup_old_temp_files_error(self):
        """Test cleanup error handling."""
        with patch.object(self.provider._temp_dir, 'glob', side_effect=Exception("Test error")):
            # Should not raise exception
            self.provider._cleanup_old_temp_files()
    
    def test_handle_provider_error(self):
        """Test provider error handling."""
        original_error = ValueError("Original error")
        
        with pytest.raises(SpeechRecognitionException) as exc_info:
            self.provider._handle_provider_error(original_error, "testing")
        
        assert "test error during testing: Original error" in str(exc_info.value)
        assert exc_info.value.__cause__ is original_error
    
    def test_handle_provider_error_no_context(self):
        """Test provider error handling without context."""
        original_error = ValueError("Original error")
        
        with pytest.raises(SpeechRecognitionException) as exc_info:
            self.provider._handle_provider_error(original_error)
        
        assert "test error: Original error" in str(exc_info.value)
        assert exc_info.value.__cause__ is original_error
    
    def test_abstract_methods_not_implemented(self):
        """Test that abstract methods raise NotImplementedError."""
        # Create instance of base class directly (should fail)
        with pytest.raises(TypeError):
            STTProviderBase("test")
    
    def test_provider_unavailable(self):
        """Test behavior when provider is unavailable."""
        provider = ConcreteSTTProvider(available=False)
        assert provider.is_available() is False
    
    def test_no_models_available(self):
        """Test behavior when no models are available."""
        provider = ConcreteSTTProvider(models=[])
        assert provider.get_available_models() == []
        assert provider.get_default_model() == "default"