Files
voculinux/tests/test_command_processor.py
T
Jatin K MalikandGitHub 1dc1d44665 feat: Alpha Launch v0.2.0 - Complete UX Overhaul 🚀🎤 (#39)
* feat: prepare for v0.2.0-alpha release

* feat: prepare for v0.2.0-alpha release

* chore: remove CHANGELOG.md, use GitHub Releases instead

* refactor: use existing About dialog instead of Settings section

* style: enhance About dialog UI readability

* style: fix formatting with black and isort

* feat: show VAD and silence timeout settings for Whisper engine

- Renamed 'VOSK Settings' to 'Recognition Settings'
- VAD Sensitivity and Silence Timeout now apply to both engines
- Added tooltips explaining each setting
- VOSK-specific model info only shown when VOSK selected

* fix: resolve overlapping UI elements in settings dialog

* fix: save VAD and silence timeout settings for both engines

Previously these settings were only saved when VOSK was selected.
Now they persist correctly for Whisper as well.

* fix: show download progress when selecting undownloaded model

Previously when auto-applying settings, models would download silently
in the background without showing the progress dialog. Now:
- Download progress dialog appears for undownloaded models
- Model icons update correctly after download completes

* feat: add download progress, speed, ETA and cancel button

- Show real download progress percentage for VOSK models
- Display download speed (MB/s) and estimated time remaining
- Add Cancel button to abort downloads
- Update status during extraction phase
- Whisper downloads still pulse (uses internal downloader)

* fix: defer model download until needed, don't block startup

- App now starts instantly even if models aren't downloaded
- Models are downloaded on-demand when selected in Settings
- If user tries to dictate without a model, plays error sound
- Added model_ready property to check if engine is usable

* fix: improve first-run UX without downloaded models

- Settings dialog now defaults to smallest available model, not saved config
- Only show saved model if it's actually downloaded
- Add desktop notification when trying to dictate without a model
- Prevents auto-downloading large models on first launch

* feat: add download progress tracking for Whisper models

- Implement custom Whisper model download with progress/speed/ETA
- Uses OpenAI's CDN URLs directly instead of whisper.load_model()
- Progress callback works the same as VOSK downloads
- Supports cancellation during download

* fix: enable download progress callback for Whisper models

Progress callback was only being set for VOSK downloads, not Whisper.
Now both engines show real-time progress, speed, and ETA.

* fix: update tests for defer_download and config manager mocking

- test_main: Mock ConfigManager to prevent saved config from overriding test values
- test_settings_dialog: Mock ModelDownloadDialog to prevent GTK init errors
- test_speech_recognition: Use proper MockVoskModule class instead of MagicMock, pass defer_download=False for download test
- test_whisper_support: Pass defer_download=False to trigger model loading in test

* fix: resolve test failures for CI

- test_main: Fix ConfigManager patch path to use source module
- test_settings_dialog: Mock _is_vosk_model_downloaded and _is_whisper_model_downloaded to skip download flow, add missing dialog attributes
- test_speech_recognition: Replace recognizer with properly configured mock that returns valid JSON, fixing test isolation issues when running all tests together

* fix: mock _download_whisper_model in whisper test to avoid requests exception handling issue

* fix: add gir1.2-girepository-2.0 dep for PyGObject on Ubuntu 24.04

- Add missing girepository-2.0 package for newer Ubuntu runners
- Use --no-build-isolation for PyGObject to use system packages

* fix: skip PyGObject pip install, use system python3-gi package

- Use --no-deps for editable install to skip PyGObject
- Install other deps explicitly without PyGObject
- Add python3-gi-cairo for complete GTK support
- Removes problematic girepository-2.0 build requirement

* fix: install PyGObject via pip with proper build deps

- Remove python3-gi/python3-gi-cairo (doesn't work with setup-python)
- Install PyGObject from pip after installing build dependencies
- Simplify venv setup without --system-site-packages hack

* Fix GObject

* fix: set GI_TYPELIB_PATH for pip-installed PyGObject to find AppIndicator3
2025-12-28 15:29:51 -08:00

229 lines
9.5 KiB
Python

"""
Tests for the command processor functionality.
"""
import unittest
from unittest.mock import MagicMock, patch
# Update import path to use the new package structure
from vocalinux.speech_recognition.command_processor import CommandProcessor
class TestCommandProcessor(unittest.TestCase):
"""Test cases for command processor functionality."""
def setUp(self):
"""Set up for tests."""
self.processor = CommandProcessor()
def test_initialization(self):
"""Test initialization of command processor."""
# Verify command dictionaries are initialized
self.assertTrue(hasattr(self.processor, "text_commands"))
self.assertTrue(hasattr(self.processor, "action_commands"))
self.assertTrue(hasattr(self.processor, "format_commands"))
# Verify dictionaries have expected entries
self.assertIn("new line", self.processor.text_commands)
self.assertIn("period", self.processor.text_commands)
self.assertIn("delete that", self.processor.action_commands)
self.assertIn("capitalize", self.processor.format_commands)
# Verify regex patterns are compiled
self.assertTrue(hasattr(self.processor, "text_cmd_regex"))
self.assertTrue(hasattr(self.processor, "action_cmd_regex"))
self.assertTrue(hasattr(self.processor, "format_cmd_regex"))
def test_text_command_processing(self):
"""Test processing of text commands."""
# Test common text commands
test_cases = [
("new line", "\n", []),
("this is a new paragraph", "this is a \n\n", []),
("end of sentence period", "end of sentence.", []),
("add a comma here", "add a, here", []),
("use question mark", "use?", []),
("exclamation mark test", "! test", []),
("semicolon example", "; example", []),
("testing colon usage", "testing:", []),
("dash separator", "- separator", []),
("hyphen example", "- example", []),
("underscore value", "_ value", []),
("quote example", '" example', []),
("single quote test", "' test", []),
("open parenthesis content close parenthesis", "( content)", []),
("open bracket item close bracket", "[ item]", []),
("open brace code close brace", "{ code}", []),
]
for input_text, expected_output, _ in test_cases:
result, actions = self.processor.process_text(input_text)
self.assertEqual(result, expected_output)
self.assertEqual(actions, [])
def test_action_command_processing(self):
"""Test processing of action commands."""
# Test action commands
test_cases = [
("delete that", "", ["delete_last"]),
("scratch that previous text", " previous text", ["delete_last"]),
("undo my last change", " my last change", ["undo"]),
("redo that edit", " that edit", ["redo"]),
("select all text", " text", ["select_all"]),
("select line of code", " of code", ["select_line"]),
("select word here", " here", ["select_word"]),
("select paragraph content", " content", ["select_paragraph"]),
("cut this selection", " this selection", ["cut"]),
("copy this text", " this text", ["copy"]),
("paste here", " here", ["paste"]),
# Test multiple actions
("select all then copy", " then", ["select_all", "copy"]),
]
for input_text, expected_output, expected_actions in test_cases:
result, actions = self.processor.process_text(input_text)
self.assertEqual(result, expected_output)
self.assertEqual(actions, expected_actions)
def test_format_command_processing(self):
"""Test processing of formatting commands."""
# Test format commands
test_cases = [
("capitalize word", "Word", []),
("uppercase letters", "LETTERS", []),
("all caps example", "EXAMPLE", []),
("lowercase TEXT", "text", []),
# Test with text before the command
("make this capitalize next", "make this Next", []),
]
for input_text, expected_output, expected_actions in test_cases:
result, actions = self.processor.process_text(input_text)
self.assertEqual(result, expected_output)
self.assertEqual(actions, expected_actions)
def test_combined_commands(self):
"""Test combinations of different command types."""
test_cases = [
# Text + Action
("new line then delete that", "", ["delete_last"]),
# Format + Text
("capitalize name period", "Name.", []),
# Action + Format
("select all then capitalize text", " then Text", ["select_all"]),
# Complex combination
(
"capitalize name comma new line select paragraph",
"Name,\n",
["select_paragraph"],
),
]
for input_text, expected_output, expected_actions in test_cases:
result, actions = self.processor.process_text(input_text)
self.assertEqual(result, expected_output)
self.assertEqual(actions, expected_actions)
def test_empty_input(self):
"""Test handling of empty input."""
result, actions = self.processor.process_text("")
self.assertEqual(result, "")
self.assertEqual(actions, [])
result, actions = self.processor.process_text(None)
self.assertEqual(result, "")
self.assertEqual(actions, [])
def test_no_commands(self):
"""Test text with no commands."""
input_text = "This is just regular text with no commands."
result, actions = self.processor.process_text(input_text)
self.assertEqual(result, input_text)
self.assertEqual(actions, [])
def test_partial_command_matches(self):
"""Test text with partial command matches."""
# Words that are substrings of commands should not be processed
test_cases = [
("periodic review", "periodic review", []), # shouldn't match "period"
("newcomer", "newcomer", []), # shouldn't match "new"
("paramount", "paramount", []), # shouldn't match "paragraph"
]
for input_text, expected_output, expected_actions in test_cases:
result, actions = self.processor.process_text(input_text)
self.assertEqual(result, expected_output)
self.assertEqual(actions, expected_actions)
def test_case_insensitivity(self):
"""Test that command matching is case-insensitive."""
test_cases = [
("NEW LINE", "\n", []),
("Period", ".", []),
("DELETE THAT", "", ["delete_last"]),
("Capitalize word", "Word", []),
]
for input_text, expected_output, expected_actions in test_cases:
result, actions = self.processor.process_text(input_text)
self.assertEqual(result, expected_output)
self.assertEqual(actions, expected_actions)
def test_multiple_format_modifiers(self):
"""Test multiple format modifiers on a single word."""
# Reset command processor to ensure active_formats is empty
self.processor = CommandProcessor()
# Apply multiple formatting commands
result, _ = self.processor.process_text("capitalize all caps text")
# The second format command overrides the first, resulting in all uppercase
self.assertEqual(result, "TEXT")
def test_format_with_no_target_word(self):
"""Test format command with no following word."""
result, _ = self.processor.process_text("capitalize")
self.assertEqual(result, "")
# The active_formats should be cleared even if no word was formatted
self.assertEqual(self.processor.active_formats, set())
def test_whitespace_handling(self):
"""Test handling of whitespace in command processing."""
# Test with extra spaces
result, _ = self.processor.process_text("new line test")
self.assertEqual(result, "\n test")
# Test with leading/trailing spaces
result, _ = self.processor.process_text(" period ")
self.assertEqual(result, ".")
# Test with mixed whitespace
result, _ = self.processor.process_text(" capitalize word new line ")
self.assertEqual(result, "Word \n")
def test_regex_compilation(self):
"""Test the regex pattern compilation."""
self.processor._compile_patterns()
# Test regex patterns match correctly
self.assertTrue(self.processor.text_cmd_regex.search("new line"))
self.assertTrue(self.processor.text_cmd_regex.search("this is a period"))
self.assertTrue(self.processor.action_cmd_regex.search("delete that"))
self.assertTrue(self.processor.format_cmd_regex.search("capitalize this"))
# Test non-matches
self.assertFalse(self.processor.text_cmd_regex.search("newline")) # no space
self.assertFalse(self.processor.action_cmd_regex.search("deletion")) # not a command
def test_compile_patterns_method(self):
"""Test the _compile_patterns method directly."""
# Add a new command to test recompilation
self.processor.text_commands["test command"] = "TEST"
# Recompile patterns
self.processor._compile_patterns()
# Verify new command is in the pattern
self.assertTrue(self.processor.text_cmd_regex.search("test command"))