-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dictate_unit.py
More file actions
662 lines (521 loc) · 26 KB
/
test_dictate_unit.py
File metadata and controls
662 lines (521 loc) · 26 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#!/usr/bin/env python3
"""
Unit tests for dictate.py with mocked external dependencies.
Tests graceful exception handling and edge cases.
"""
import json
import os
import sys
import threading
import time
from io import BytesIO
from unittest.mock import MagicMock, Mock, PropertyMock, mock_open, patch
import pytest
import speech_recognition as sr
@pytest.fixture
def mock_dependencies():
"""Mock all external dependencies."""
with patch('multi_dictate.dictate.pasimple.PaSimple') as mock_pasimple, \
patch('pyautogui.typewrite') as mock_typewrite, \
patch('webrtcvad.Vad') as mock_vad, \
patch('multi_dictate.dictate.gTTS') as mock_gtts, \
patch('multi_dictate.dictate.AudioSegment') as mock_audio_segment, \
patch('vosk.SetLogLevel') as mock_vosk_log, \
patch('multi_dictate.kbd_utils.check_dictation_keybindings') as mock_check_kb, \
patch('multi_dictate.kbd_utils.get_current_keyboard_layout') as mock_get_layout, \
patch('multi_dictate.kbd_utils.for_typewrite') as mock_for_typewrite, \
patch('multi_dictate.kbd_utils.kbd_cfg') as mock_kbd_cfg, \
patch('subprocess.run') as mock_subprocess, \
patch('os.mkfifo') as mock_mkfifo, \
patch('os.remove') as mock_remove, \
patch('os.path.exists') as mock_exists, \
patch('builtins.open', mock_open(read_data='''general:
recognizer_engine: google
colors:
light:
fg: black
listening: lightcoral
processing: lightsalmon
error: lightcoral
echo: lightblue
default: lightgray
dark:
fg: white
listening: indianred
processing: peru
error: indianred
echo: steelblue
default: dimgray
''')) as mock_file:
# Configure mocks
mock_get_layout.return_value = 'us'
mock_for_typewrite.side_effect = lambda layout, text: text # Pass through text
mock_exists.return_value = False
# Mock kbd_cfg with proper structure
mock_layout = MagicMock()
mock_layout.tts = 'en'
mock_layout.stt = 'en-US'
mock_kbd_cfg.layouts = {'us': mock_layout}
# Mock VAD
vad_instance = MagicMock()
vad_instance.is_speech.return_value = True
mock_vad.return_value = vad_instance
# Mock pasimple
pa_instance = MagicMock()
pa_instance.read.return_value = b'audio_data'
pa_instance.write.return_value = None
pa_instance.drain.return_value = None
mock_pasimple.return_value = pa_instance
# Mock AudioSegment
audio_instance = MagicMock()
audio_instance.channels = 2
audio_instance.frame_rate = 44100
audio_instance.raw_data = b'raw_audio_data'
audio_instance.__sub__ = MagicMock(return_value=audio_instance)
mock_audio_segment.from_mp3.return_value = audio_instance
# Mock gTTS
tts_instance = MagicMock()
tts_instance.write_to_fp = MagicMock()
mock_gtts.return_value = tts_instance
yield {
'pasimple': mock_pasimple,
'typewrite': mock_typewrite,
'vad': mock_vad,
'gtts': mock_gtts,
'audio_segment': mock_audio_segment,
'check_kb': mock_check_kb,
'get_layout': mock_get_layout,
'for_typewrite': mock_for_typewrite,
'kbd_cfg': mock_kbd_cfg,
'subprocess': mock_subprocess,
'mkfifo': mock_mkfifo,
'remove': mock_remove,
'exists': mock_exists,
'vad_instance': vad_instance,
'pa_instance': pa_instance,
}
@pytest.fixture
def dictate_app(mock_dependencies):
"""Create DictationApp instance with mocked dependencies."""
# Import after mocking
sys.modules.pop('multi_dictate.dictate', None)
from multi_dictate import dictate
# Mock params to avoid None issues
mock_params = MagicMock()
mock_params.trigger = '/tmp/test_dictate_trigger'
mock_params.echo = False
dictate.params = mock_params
return dictate.DictationApp()
class TestDictationApp:
"""Test DictationApp class."""
def test_init_success(self, dictate_app):
"""Test successful initialization."""
assert dictate_app.recognizer_engine == 'google'
assert dictate_app.recording_active is False
assert dictate_app.shutdown_flag is False
assert 'record' in dictate_app.commands
def test_init_missing_config(self, mock_dependencies):
"""Test initialization with missing config file."""
with patch('builtins.open', side_effect=FileNotFoundError):
from multi_dictate import dictate
app = dictate.DictationApp()
# With default_box=True, accessing undefined keys returns empty Box, not None
# The recognizer_engine will default to 'google' in the code
assert app.recognizer_engine == 'google'
def test_init_invalid_yaml(self, mock_dependencies):
"""Test initialization with invalid YAML."""
with patch('builtins.open', mock_open(read_data='invalid: yaml: content:')):
with patch('yaml.safe_load', side_effect=Exception("YAML error")):
from multi_dictate import dictate
app = dictate.DictationApp()
# With default_box=True, accessing undefined keys returns empty Box, not None
# The recognizer_engine will default to 'google' in the code
assert app.recognizer_engine == 'google'
def test_speak_text_gtts_success(self, dictate_app, mock_dependencies):
"""Test successful text-to-speech with gTTS."""
dictate_app.curr_layout = 'us'
# Need to patch the context manager
with patch('pasimple.PaSimple') as mock_pa_constructor:
mock_pa = MagicMock()
mock_pa.__enter__ = MagicMock(return_value=mock_pa)
mock_pa.__exit__ = MagicMock(return_value=False)
mock_pa_constructor.return_value = mock_pa
dictate_app.speak_text("Hello", sync=True)
mock_dependencies['gtts'].assert_called_once()
mock_pa.write.assert_called_once()
mock_pa.drain.assert_called_once()
def test_speak_text_gtts_failure_fallback_espeak(self, dictate_app, mock_dependencies):
"""Test TTS fallback to espeak when gTTS fails."""
mock_dependencies['gtts'].side_effect = Exception("gTTS failed")
mock_dependencies['subprocess'].return_value = MagicMock(returncode=0)
dictate_app.speak_text("Hello", sync=True)
mock_dependencies['subprocess'].assert_called_with(
["espeak", "-a", "10", "Hello"],
check=True,
capture_output=True
)
def test_speak_text_all_methods_fail(self, dictate_app, mock_dependencies):
"""Test when all TTS methods fail."""
mock_dependencies['gtts'].side_effect = Exception("gTTS failed")
mock_dependencies['subprocess'].side_effect = FileNotFoundError("espeak not found")
with patch('builtins.print') as mock_print:
dictate_app.speak_text("Hello", sync=True)
mock_print.assert_called_with("All TTS methods failed for: Hello")
def test_speak_text_empty(self, dictate_app):
"""Test speak_text with empty string."""
with patch.object(dictate_app, '_speak_with_gtts') as mock_speak:
dictate_app.speak_text("")
mock_speak.assert_not_called()
def test_record_audio_success(self, dictate_app, mock_dependencies):
"""Test successful audio recording."""
mock_dependencies['vad_instance'].is_speech.side_effect = [True, True, False, False, False]
# Mock to simulate stop after a few chunks
dictate_app.stop_recording_flag = False
call_count = 0
def read_chunk(*args, **kwargs):
nonlocal call_count
call_count += 1
if call_count >= 5:
dictate_app.stop_recording_flag = True
return b'chunk'
mock_dependencies['pa_instance'].read.side_effect = read_chunk
result = dictate_app.record_audio(max_duration=1)
assert result == b'chunkchunkchunkchunkchunk'
mock_dependencies['pa_instance'].close.assert_called_once()
def test_record_audio_with_silence_detection(self, dictate_app, mock_dependencies):
"""Test audio recording with silence detection."""
# Use cycle to provide infinite values
import itertools
speech_values = itertools.chain([True] * 10, [False] * 100, itertools.repeat(False))
mock_dependencies['vad_instance'].is_speech.side_effect = speech_values
mock_dependencies['pa_instance'].read.return_value = b'chunk'
result = dictate_app.record_audio(max_duration=10, stop_on_silence=True)
# Should stop after detecting silence
assert len(result) > 0
assert mock_dependencies['pa_instance'].read.call_count < 300 # Much less than max
def test_record_audio_no_speech_timeout(self, dictate_app, mock_dependencies):
"""Test recording timeout when no speech detected."""
mock_dependencies['vad_instance'].is_speech.return_value = False
mock_dependencies['pa_instance'].read.return_value = b'chunk'
result = dictate_app.record_audio(max_duration=10, stop_on_silence=True)
# Should stop after no speech timeout
assert len(result) > 0
assert mock_dependencies['pa_instance'].read.call_count < 200
def test_record_audio_exception_cleanup(self, dictate_app, mock_dependencies):
"""Test cleanup when exception occurs during recording."""
mock_dependencies['pa_instance'].read.side_effect = Exception("Read error")
with pytest.raises(Exception):
dictate_app.record_audio()
mock_dependencies['pa_instance'].close.assert_called_once()
def test_process_audio_success(self, dictate_app, mock_dependencies):
"""Test successful audio processing."""
with patch.object(dictate_app, '_recognize', return_value="Hello world"):
mock_audio = MagicMock()
result = dictate_app._process_audio(mock_audio)
assert result == "Hello world"
mock_dependencies['typewrite'].assert_called_once_with("Hello world ", interval=0.05)
def test_process_audio_unknown_value_error(self, dictate_app, mock_dependencies):
"""Test handling UnknownValueError."""
with patch.object(dictate_app, '_recognize', side_effect=sr.UnknownValueError):
with patch('builtins.print') as mock_print:
result = dictate_app._process_audio(MagicMock())
assert result is None
mock_print.assert_called_with("No speech detected")
def test_process_audio_request_error(self, dictate_app, mock_dependencies):
"""Test handling RequestError."""
with patch.object(dictate_app, '_recognize', side_effect=sr.RequestError("API error")):
with patch('builtins.print') as mock_print:
result = dictate_app._process_audio(MagicMock())
assert result is None
mock_print.assert_called_with("❌ Speech service error: API error")
def test_process_audio_generic_exception(self, dictate_app, mock_dependencies):
"""Test handling generic exception."""
with patch.object(dictate_app, '_recognize', side_effect=Exception("Unknown error")):
with patch('builtins.print') as mock_print:
result = dictate_app._process_audio(MagicMock())
assert result is None
mock_print.assert_called_with("❌ Recognition error: Unknown error")
def test_recognize_google(self, dictate_app):
"""Test Google recognition."""
dictate_app.curr_layout = 'us'
dictate_app.cur_lang = 'en-US'
# Mock audio data
mock_audio = MagicMock(spec=sr.AudioData)
mock_audio.sample_rate = 16000
mock_audio.sample_width = 2
# Replace the engine's recognize function with a mock
dictate_app.recognizer_engines['google'] = {
'recognize': lambda audio, **kwargs: "Test text",
'parser': lambda result: result
}
result = dictate_app._recognize(mock_audio)
assert result == "Test text"
def test_recognize_vosk(self, dictate_app):
"""Test Vosk recognition."""
dictate_app.recognizer_engine = 'vosk'
dictate_app.curr_layout = 'us'
dictate_app.cur_lang = 'en-US'
# Mock audio data
mock_audio = MagicMock(spec=sr.AudioData)
mock_audio.sample_rate = 16000
mock_audio.sample_width = 2
# Need to ensure the engine config is properly set up
dictate_app.recognizer_engines['vosk'] = {
'recognize': lambda audio, **kwargs: '{"text": "Test vosk"}',
'parser': lambda result: json.loads(result).get("text", "")
}
result = dictate_app._recognize(mock_audio)
assert result == "Test vosk"
def test_start_manual_recording_already_active(self, dictate_app):
"""Test start recording when already active."""
dictate_app.recording_active = True
with patch('builtins.print') as mock_print:
dictate_app.start_manual_recording()
mock_print.assert_called_with("Recording already active")
def test_start_manual_recording_exception(self, dictate_app, mock_dependencies):
"""Test exception during manual recording."""
with patch.object(dictate_app, 'record_audio', side_effect=Exception("Record error")):
with patch('threading.Thread') as mock_thread:
dictate_app.start_manual_recording()
# Get the target function and call it
target_func = mock_thread.call_args[1]['target']
target_func()
assert dictate_app.recording_active is False
def test_stop_manual_recording(self, dictate_app):
"""Test stopping manual recording."""
dictate_app.recording_active = True
dictate_app.stop_recording_flag = False
dictate_app.stop_manual_recording()
assert dictate_app.stop_recording_flag is True
def test_stop_manual_recording_not_active(self, dictate_app):
"""Test stopping when not recording."""
dictate_app.recording_active = False
dictate_app.stop_manual_recording()
# Should not raise any exception
def test_toggle_recording(self, dictate_app):
"""Test toggle recording functionality."""
with patch.object(dictate_app, 'start_manual_recording') as mock_start:
dictate_app.recording_active = False
dictate_app._toggle_recording()
mock_start.assert_called_once()
with patch.object(dictate_app, 'stop_manual_recording') as mock_stop:
dictate_app.recording_active = True
dictate_app._toggle_recording()
mock_stop.assert_called_once()
def test_continuous_recording_exception(self, dictate_app):
"""Test exception in continuous recording."""
with patch.object(dictate_app, 'record_audio', side_effect=Exception("Error")):
with patch('threading.Thread') as mock_thread:
dictate_app.start_continuous_recording()
# Get and execute the thread target
target_func = mock_thread.call_args[1]['target']
target_func()
assert dictate_app.continuous_mode_active is False
def test_show_hide_status_window(self, dictate_app):
"""Test status window show/hide."""
with patch('tkinter.Tk') as mock_tk:
dictate_app.show_status_window("Test", "red")
mock_tk.assert_called_once()
# Set status window before testing hide
mock_window = MagicMock()
dictate_app.status_window = mock_window
dictate_app.hide_status_window()
mock_window.destroy.assert_called_once()
def test_signal_handler(self, dictate_app):
"""Test signal handler sets shutdown flag."""
dictate_app.signal_handler(None, None)
assert dictate_app.shutdown_flag is True
def test_input_command_valid(self, dictate_app):
"""Test processing valid command."""
mock_fifo = MagicMock()
mock_fifo.readline.return_value = "record"
# Mock the command method
mock_method = MagicMock()
dictate_app.commands["record"] = (mock_method, "Test description")
with patch('select.select', return_value=([mock_fifo], [], [])):
dictate_app.input_command(mock_fifo)
# Since fifo has data, it should read and process
mock_fifo.readline.assert_called_once()
mock_method.assert_called_once()
def test_input_command_invalid(self, dictate_app):
"""Test processing invalid command."""
mock_fifo = MagicMock()
mock_fifo.readline.return_value = "invalid_command"
with patch('select.select', return_value=([mock_fifo], [], [])):
with patch('builtins.print') as mock_print:
dictate_app.input_command(mock_fifo)
mock_print.assert_called_with("Unknown command: invalid_command")
def test_input_command_no_data(self, dictate_app):
"""Test when no data available in FIFO."""
mock_fifo = MagicMock()
with patch('select.select', return_value=([], [], [])):
dictate_app.input_command(mock_fifo)
mock_fifo.readline.assert_not_called()
def test_cleanup_multiple_calls(self, dictate_app, mock_dependencies):
"""Test cleanup is idempotent."""
mock_dependencies['exists'].return_value = True
# Mock params object
mock_params = MagicMock()
mock_params.trigger = '/tmp/test'
# Set up params
with patch('multi_dictate.dictate.params', mock_params):
dictate_app._cleanup()
dictate_app._cleanup() # Second call should do nothing
mock_dependencies['remove'].assert_called_once()
def test_run_with_shutdown(self, dictate_app, mock_dependencies):
"""Test run loop with shutdown."""
dictate_app.shutdown_flag = True
# Mock params object
mock_params = MagicMock()
mock_params.trigger = '/tmp/test'
with patch('multi_dictate.dictate.params', mock_params):
dictate_app.run()
mock_dependencies['mkfifo'].assert_called_once()
def test_calibrate_success(self, dictate_app, mock_dependencies):
"""Test calibration functionality."""
with patch.object(dictate_app, 'speak_text') as mock_speak:
with patch.object(dictate_app, 'record_audio', return_value=b'audio'):
with patch.object(dictate_app, '_convert_raw_audio_to_sr_format') as mock_convert:
with patch('builtins.print'):
# Create mock audio
mock_audio = MagicMock(spec=sr.AudioData)
mock_convert.return_value = mock_audio
# Mock recognizers properly
with patch.object(dictate_app.recognizer, 'recognize_google', return_value="test text"):
with patch.object(dictate_app.recognizer, 'recognize_vosk', return_value='{"text": "test text"}'):
dictate_app.calibrate()
assert mock_speak.call_count >= 2
def test_calibrate_recognition_error(self, dictate_app, mock_dependencies):
"""Test calibration with recognition errors."""
with patch.object(dictate_app, 'speak_text'):
with patch.object(dictate_app, 'record_audio', return_value=b'audio'):
with patch.object(dictate_app, '_convert_raw_audio_to_sr_format') as mock_convert:
with patch('builtins.print'):
# Create mock audio
mock_audio = MagicMock(spec=sr.AudioData)
mock_convert.return_value = mock_audio
# Make recognition fail
with patch.object(dictate_app.recognizer, 'recognize_google', side_effect=Exception("API error")):
with patch.object(dictate_app.recognizer, 'recognize_vosk', side_effect=Exception("Model error")):
dictate_app.calibrate()
# Should handle errors gracefully
class TestMainFunction:
"""Test main function and entry point."""
def test_main_calibrate_mode(self, mock_dependencies):
"""Test main with calibrate flag."""
test_args = ['dictate.py', '--calibrate']
with patch('sys.argv', test_args):
with patch('multi_dictate.dictate.DictationApp') as mock_app_class:
mock_app = MagicMock()
mock_app_class.return_value = mock_app
with pytest.raises(SystemExit) as exc_info:
from multi_dictate import dictate
dictate.main()
assert exc_info.value.code == 0
mock_app.calibrate.assert_called_once()
def test_main_with_custom_trigger(self, mock_dependencies):
"""Test main with custom trigger path."""
test_args = ['dictate.py', '--trigger', '/tmp/custom_trigger']
with patch('sys.argv', test_args):
with patch('multi_dictate.dictate.DictationApp') as mock_app_class:
mock_app = MagicMock()
mock_app_class.return_value = mock_app
from multi_dictate import dictate
dictate.main()
mock_app.run.assert_called_once()
def test_main_kill_previous_instance(self, mock_dependencies):
"""Test killing previous instance."""
test_args = ['dictate.py']
# Test the kill logic directly to avoid argparse issues
pid_file = "/tmp/dictate.pid"
with patch('os.path.exists', return_value=True):
with patch('builtins.open', mock_open(read_data='12345')):
with patch('os.kill') as mock_kill:
# Simulate the kill logic from main()
try:
with open(pid_file, 'r') as f:
pid = int(f.read().strip())
os.kill(pid, 15) # SIGTERM
except Exception:
pass
mock_kill.assert_called_once_with(12345, 15)
def test_main_exception_during_run(self, mock_dependencies):
"""Test exception during app.run()."""
test_args = ['dictate.py']
with patch('sys.argv', test_args):
with patch('multi_dictate.dictate.DictationApp') as mock_app_class:
mock_app = MagicMock()
mock_app.run.side_effect = Exception("Runtime error")
mock_app_class.return_value = mock_app
with pytest.raises(SystemExit) as exc_info:
from multi_dictate import dictate
dictate.main()
assert exc_info.value.code == 1
class TestCheckDependencies:
"""Test dependency checking."""
def test_dependencies_available(self):
"""Test when all dependencies are available."""
from multi_dictate import dictate
assert dictate.check_dependencies() is True
def test_dependencies_missing(self):
"""Test when dependencies are missing."""
# Create a custom check_dependencies that simulates import failure
def mock_check_dependencies():
try:
raise ImportError("Missing dependency: speech_recognition")
except ImportError as e:
print(f"Missing dependency: {e}")
print("Please install required packages:")
print("pip install -r requirements.txt")
return False
with patch('builtins.print'):
assert mock_check_dependencies() is False
class TestConvertRawAudioToSRFormat:
"""Test audio format conversion."""
def test_convert_success(self, dictate_app):
"""Test successful audio conversion."""
test_data = b'test_audio_data'
with patch('wave.open') as mock_wave:
result = dictate_app._convert_raw_audio_to_sr_format(test_data)
assert isinstance(result, sr.AudioData)
mock_wave.assert_called_once()
class TestEdgeCases:
"""Test edge cases and boundary conditions."""
def test_empty_audio_data(self, dictate_app):
"""Test with empty audio data."""
result = dictate_app._convert_raw_audio_to_sr_format(b'')
assert isinstance(result, sr.AudioData)
def test_very_long_audio(self, dictate_app, mock_dependencies):
"""Test with very long audio duration."""
# Simulate very long recording
mock_dependencies['pa_instance'].read.return_value = b'x' * 1024 * 1024
dictate_app.stop_recording_flag = False
# Force stop after some iterations
call_count = 0
def side_effect(*args):
nonlocal call_count
call_count += 1
if call_count > 10:
dictate_app.stop_recording_flag = True
return b'x' * 1024 * 1024
mock_dependencies['pa_instance'].read.side_effect = side_effect
result = dictate_app.record_audio(max_duration=1000)
assert len(result) > 0
def test_concurrent_recording_attempts(self, dictate_app):
"""Test multiple concurrent recording attempts."""
dictate_app.recording_active = True
# Try to start multiple recordings
with patch('builtins.print') as mock_print:
dictate_app.start_manual_recording()
dictate_app.start_continuous_recording()
assert mock_print.call_count == 2
assert all("already active" in str(call) for call in mock_print.call_args_list)
def test_rapid_toggle(self, dictate_app):
"""Test rapid toggling of recording."""
with patch.object(dictate_app, 'start_manual_recording'):
with patch.object(dictate_app, 'stop_manual_recording'):
for _ in range(10):
dictate_app._toggle_recording()
dictate_app.recording_active = not dictate_app.recording_active
if __name__ == "__main__":
pytest.main([__file__, "-v", "--tb=short"])