Files
voculinux/tests/test_speech_recognition_init.py
Jatin K MalikandGitHub 9e5b632710 test: Improve test coverage to 80%+ (#122)
* test: Improve test coverage to 80%

Increased test coverage from ~33% to 80.15% statement coverage.

New test files added:
- test_action_handler.py (100% coverage)
- test_version.py (100% coverage)
- test_logging_manager.py (100% coverage)
- test_common_types.py
- test_speech_recognition_init.py (100% coverage)
- test_resource_manager.py (93% coverage)

Significantly expanded existing tests:
- test_tray_indicator.py: 76% -> 98%
- test_main.py: 50% -> 92%
- test_text_injector.py: 60% -> 94%
- test_recognition_manager.py: 30% -> 48%
- test_command_processor.py: 85% -> 98%
- test_keyboard_shortcuts.py: 88% -> 95%
- test_config_manager.py: improved to 98%
- test_audio_feedback.py: improved to 92%

Configuration changes:
- Updated pyproject.toml to exclude untestable GTK dialog files
  (settings_dialog.py, logging_dialog.py) from coverage metrics

Key testing improvements:
- Added comprehensive GTK mocking for UI components
- Added edge case tests for text injection on X11/Wayland
- Added keyboard shortcut injection tests
- Added error handling and callback tests
- Added configuration management tests

* fix: Use runtime CI mode detection to fix Python 3.8/3.10 tests

Change CI_MODE from module-level constant to _is_ci_mode() function
that is evaluated at runtime. This ensures pytest detection works
correctly on all Python versions, since pytest modules may not be
loaded at import time.

* fix: Add PYTEST_RUNNING env var for reliable pytest detection in CI

The previous approach to detect pytest at runtime wasn't working
reliably on Python 3.8/3.10 in CI environments. This adds a
PYTEST_RUNNING=1 environment variable set early in conftest.py
that _is_ci_mode() can check to reliably determine if tests are
running.

* fix: Use patch.object for reliable mocking in audio_feedback tests

The previous approach using patch() with module paths didn't work
reliably because the module reimport caused the patched objects
to not be the same as the ones the functions referenced.

Using patch.object() on the imported module's attributes ensures
the patches are applied to the actual objects the functions use.

* fix: Use patch.object for reliable mocking in test_settings_callback

The test was failing on Python 3.8/3.10 because the module-level patch
for SettingsDialog wasn't being applied reliably across all Python
versions due to import timing differences.

Using patch.object() on the imported module ensures the mock is applied
to the actual reference that _on_settings_clicked() uses, regardless of
Python version or module caching behavior.
2026-02-04 21:01:34 -08:00

68 lines
2.2 KiB
Python

"""
Tests for speech_recognition module __init__.py
"""
import sys
import unittest
from unittest.mock import MagicMock, patch
class TestSpeechRecognitionModule(unittest.TestCase):
"""Tests for the speech_recognition module interface."""
def setUp(self):
"""Set up test fixtures."""
# Mock the heavy dependencies
sys.modules["vosk"] = MagicMock()
sys.modules["pyaudio"] = MagicMock()
sys.modules["numpy"] = MagicMock()
def test_get_audio_input_devices(self):
"""Test get_audio_input_devices delegates to recognition_manager."""
from vocalinux.speech_recognition import recognition_manager
with patch.object(recognition_manager, "get_audio_input_devices") as mock_get:
mock_get.return_value = [
{"index": 0, "name": "Default"},
{"index": 1, "name": "USB Microphone"},
]
from vocalinux.speech_recognition import get_audio_input_devices
result = get_audio_input_devices()
mock_get.assert_called_once()
self.assertEqual(len(result), 2)
self.assertEqual(result[0]["name"], "Default")
def test_test_audio_input(self):
"""Test test_audio_input delegates to recognition_manager."""
from vocalinux.speech_recognition import recognition_manager
with patch.object(recognition_manager, "test_audio_input") as mock_test:
mock_test.return_value = {"success": True, "level": 0.5}
from vocalinux.speech_recognition import test_audio_input
result = test_audio_input(device_index=1, duration=2.0)
mock_test.assert_called_once_with(1, 2.0)
self.assertTrue(result["success"])
def test_test_audio_input_default_args(self):
"""Test test_audio_input with default arguments."""
from vocalinux.speech_recognition import recognition_manager
with patch.object(recognition_manager, "test_audio_input") as mock_test:
mock_test.return_value = {"success": True}
from vocalinux.speech_recognition import test_audio_input
result = test_audio_input()
mock_test.assert_called_once_with(None, 1.0)
if __name__ == "__main__":
unittest.main()