Files
voculinux/tests/test_config_manager.py
T
Jatin K MalikandGitHub 57a0ddd0b8 feat(speech): add whisper.cpp with Vulkan support for GPU acceleration (#179)
* feat(speech): add whisper.cpp with Vulkan support for GPU acceleration

Add whisper.cpp as a new speech recognition engine with the following features:
- Vulkan GPU acceleration support (AMD, Intel, NVIDIA)
- Fallback to CUDA for NVIDIA GPUs
- CPU-only mode with automatic thread optimization
- Support for all model sizes: tiny, base, small, medium, large
- Hardware detection to recommend best model/backend
- Models downloaded from Hugging Face (ggml format)
- Updated UI to show compute backend info in settings
- pywhispercpp as the Python binding library
- whisper_cpp as the new default engine

Changes:
- New whispercpp_model_info.py with model metadata and hardware detection
- Updated recognition_manager.py with whisper.cpp init and transcription
- Updated settings_dialog.py with whisper.cpp UI support
- Updated config_manager.py with whisper_cpp_model_size config
- Updated main.py with whisper_cpp CLI option
- Updated pyproject.toml with pywhispercpp dependency
- Updated install.sh with vulkan-tools for GPU detection

Closes #104

* style: fix black and isort formatting issues

* test: update config manager test for whisper_cpp default engine

* feat(install): interactive mode with 3 engine choices and whisper.cpp as default

Major changes to install.sh:
- Interactive mode is now the DEFAULT (not --auto)
- Added --auto flag for non-interactive automatic installation
- New --engine flag to specify engine directly
- Interactive mode shows 3 engine options with visual boxes:
  1. whisper.cpp (recommended) - Vulkan/CUDA/CPU support
  2. whisper (OpenAI) - PyTorch-based, NVIDIA-only
  3. VOSK - lightweight option
- Hardware detection with recommendations:
  - Detects Vulkan support (AMD/Intel/NVIDIA)
  - Detects NVIDIA CUDA
  - Checks available RAM
  - Recommends best engine based on hardware
- Updated help text with new options and examples
- Removed old --with-whisper, --whisper-cpu, --no-whisper flags
- whisper.cpp is now the default engine for automatic mode

* feat(uninstall): kill running vocalinux processes before uninstall

* fix(whisper.cpp): auto-download model, use all CPU cores, hide unavailable engines

Three fixes for whisper.cpp implementation:

1. Auto-download model during install
   - Added install_whispercpp_model() function to install.sh
   - Downloads ggml-tiny.bin (~39MB) from Hugging Face
   - Runs automatically when whisper.cpp engine is selected

2. Use all CPU cores for transcription
   - Added n_threads parameter to Model initialization
   - Uses multiprocessing.cpu_count() to get all cores
   - Significantly improves transcription speed

3. Hide unavailable engines from dropdown
   - Added get_available_engines() function to detect installed engines
   - Settings dialog now only shows engines that are actually installed
   - Falls back to first available engine if current not available

Fixes user-reported issues with whisper.cpp integration.

* ui(settings): display whisper.cpp correctly and increase dialog height

- Changed engine display name from 'whisper_cpp' to 'whisper.cpp'
- Added format_engine_display_name() helper function
- Dialog now uses 75% of screen height for better visibility
- More options visible by default without scrolling

* revert(settings): remove display name change, keep dialog height increase

- Reverted engine display name change (was breaking engine selection)
- Kept dialog height at 75% of screen for better visibility

* style: fix black and isort formatting for whisper.cpp logging

* style: fix black formatting in settings_dialog.py

* ci: fix Fedora 39 container package installation

Add dnf cache update and fallback with --nogpgcheck for Fedora containers
that may have stale or missing package metadata.
2026-02-11 19:37:58 -08:00

381 lines
15 KiB
Python

"""
Tests for the config manager functionality.
"""
import json
import os
import tempfile
import unittest
from unittest.mock import patch
# Update import path to use the new package structure
from vocalinux.ui.config_manager import DEFAULT_CONFIG, ConfigManager
class TestConfigManager(unittest.TestCase):
"""Test cases for the configuration manager."""
def setUp(self):
"""Set up test environment."""
# Create a temporary directory for configuration
self.temp_dir = tempfile.TemporaryDirectory()
self.temp_config_dir = os.path.join(self.temp_dir.name, ".config/vocalinux")
os.makedirs(self.temp_config_dir, exist_ok=True)
self.temp_config_file = os.path.join(self.temp_config_dir, "config.json")
# Patch the config paths to use our temporary directory
self.config_dir_patcher = patch(
"vocalinux.ui.config_manager.CONFIG_DIR", self.temp_config_dir
)
self.config_file_patcher = patch(
"vocalinux.ui.config_manager.CONFIG_FILE", self.temp_config_file
)
self.config_dir_patcher.start()
self.config_file_patcher.start()
# Patch logging to avoid actual logging
self.logger_patcher = patch("vocalinux.ui.config_manager.logger")
self.mock_logger = self.logger_patcher.start()
def tearDown(self):
"""Clean up after tests."""
self.config_dir_patcher.stop()
self.config_file_patcher.stop()
self.logger_patcher.stop()
self.temp_dir.cleanup()
def test_init_default_config(self):
"""Test initialization with default configuration."""
config_manager = ConfigManager()
self.assertEqual(config_manager.config, DEFAULT_CONFIG)
self.mock_logger.info.assert_called_with(
f"Config file not found at {self.temp_config_file}. Using defaults."
)
self.assertTrue(os.path.exists(self.temp_config_dir))
def test_ensure_config_dir(self):
"""Test that _ensure_config_dir creates the directory."""
# Delete the config directory to test creation
os.rmdir(self.temp_config_dir)
self.assertFalse(os.path.exists(self.temp_config_dir))
# Create config manager, which should create the directory
ConfigManager()
self.assertTrue(os.path.exists(self.temp_config_dir))
def test_load_config(self):
"""Test loading configuration from file."""
# Create a test config file
test_config = {
"speech_recognition": {
"engine": "whisper",
"model_size": "large",
},
"ui": {
"start_minimized": True,
},
}
with open(self.temp_config_file, "w") as f:
json.dump(test_config, f)
# Load the config
config_manager = ConfigManager()
# Verify it merged with defaults correctly
self.assertEqual(config_manager.config["speech_recognition"]["engine"], "whisper")
self.assertEqual(config_manager.config["speech_recognition"]["model_size"], "large")
self.assertEqual(
config_manager.config["speech_recognition"]["vad_sensitivity"], 3
) # From defaults
self.assertEqual(config_manager.config["ui"]["start_minimized"], True)
self.assertEqual(config_manager.config["ui"]["show_notifications"], True) # From defaults
def test_load_config_file_error(self):
"""Test handling of errors when loading config file."""
# Create a broken config file
with open(self.temp_config_file, "w") as f:
f.write("{broken json")
# Load the config - with broken JSON, it should use defaults
config_manager = ConfigManager()
# Verify it used defaults - check key structure exists
self.assertIn("speech_recognition", config_manager.config)
self.assertIn("engine", config_manager.config["speech_recognition"])
self.assertIn("ui", config_manager.config)
# Verify logger.error was called for the broken JSON
self.mock_logger.error.assert_called()
def test_save_config(self):
"""Test saving configuration to file."""
config_manager = ConfigManager()
# Modify config
config_manager.config["speech_recognition"]["engine"] = "whisper"
config_manager.config["ui"]["start_minimized"] = True
# Save config
result = config_manager.save_config()
self.assertTrue(result)
# Verify file was created with correct content
self.assertTrue(os.path.exists(self.temp_config_file))
with open(self.temp_config_file, "r") as f:
saved_config = json.load(f)
self.assertEqual(saved_config["speech_recognition"]["engine"], "whisper")
self.assertEqual(saved_config["ui"]["start_minimized"], True)
def test_save_config_error(self):
"""Test handling of errors when saving config file."""
config_manager = ConfigManager()
# Mock open to raise an exception
with patch("builtins.open", side_effect=PermissionError("Permission denied")):
result = config_manager.save_config()
self.assertFalse(result)
self.mock_logger.error.assert_called()
def test_get_existing_value(self):
"""Test getting an existing configuration value from defaults."""
# Test that DEFAULT_CONFIG constant has the expected default engine
# Import directly to get the current value (not cached from earlier imports)
import importlib
import vocalinux.ui.config_manager as cm
importlib.reload(cm)
self.assertEqual(
cm.DEFAULT_CONFIG["speech_recognition"]["engine"], "whisper_cpp"
) # Default is whisper_cpp for best performance with Vulkan support
# Also test the get method works - it should return a valid engine value
config_manager = ConfigManager()
value = config_manager.get("speech_recognition", "engine")
self.assertIn(value, ["vosk", "whisper", "whisper_cpp"]) # Should be one of valid engines
def test_get_nonexistent_value(self):
"""Test getting a nonexistent configuration value."""
config_manager = ConfigManager()
value = config_manager.get("nonexistent", "key", "default_value")
self.assertEqual(value, "default_value")
def test_set_existing_section(self):
"""Test setting a value in an existing section."""
config_manager = ConfigManager()
result = config_manager.set("speech_recognition", "engine", "vosk")
self.assertTrue(result)
self.assertEqual(config_manager.config["speech_recognition"]["engine"], "vosk")
def test_set_new_section(self):
"""Test setting a value in a new section."""
config_manager = ConfigManager()
result = config_manager.set("new_section", "key", "value")
self.assertTrue(result)
self.assertEqual(config_manager.config["new_section"]["key"], "value")
def test_set_error(self):
"""Test handling of errors when setting a value."""
config_manager = ConfigManager()
config_manager.config = 1 # Not a dict, will cause error
result = config_manager.set("section", "key", "value")
self.assertFalse(result)
self.mock_logger.error.assert_called()
def test_update_dict_recursive(self):
"""Test recursive dictionary update."""
target = {"a": {"b": 1, "c": 2}, "d": 3}
source = {"a": {"b": 10, "e": 20}, "f": 30}
config_manager = ConfigManager()
config_manager._update_dict_recursive(target, source)
# Check that values were updated correctly
self.assertEqual(target["a"]["b"], 10) # Updated
self.assertEqual(target["a"]["c"], 2) # Unchanged
self.assertEqual(target["a"]["e"], 20) # Added
self.assertEqual(target["d"], 3) # Unchanged
self.assertEqual(target["f"], 30) # Added
def test_get_model_size_for_engine(self):
"""Test getting model size for a specific engine."""
config_manager = ConfigManager()
# Test default values
self.assertEqual(config_manager.get_model_size_for_engine("vosk"), "small")
self.assertEqual(config_manager.get_model_size_for_engine("whisper"), "tiny")
# Set specific model sizes
config_manager.set_model_size_for_engine("vosk", "medium")
config_manager.set_model_size_for_engine("whisper", "small")
# Verify they are returned correctly
self.assertEqual(config_manager.get_model_size_for_engine("vosk"), "medium")
self.assertEqual(config_manager.get_model_size_for_engine("whisper"), "small")
def test_set_model_size_for_engine(self):
"""Test setting model size for a specific engine."""
config_manager = ConfigManager()
# Set model size for vosk
config_manager.set_model_size_for_engine("vosk", "large")
self.assertEqual(config_manager.config["speech_recognition"]["vosk_model_size"], "large")
self.assertEqual(config_manager.config["speech_recognition"]["model_size"], "large")
# Set model size for whisper
config_manager.set_model_size_for_engine("whisper", "medium")
self.assertEqual(
config_manager.config["speech_recognition"]["whisper_model_size"], "medium"
)
self.assertEqual(config_manager.config["speech_recognition"]["model_size"], "medium")
def test_migrate_old_config(self):
"""Test migration of old config format without per-engine model sizes."""
# Create an old-style config file without per-engine model sizes
old_config = {
"speech_recognition": {
"engine": "vosk",
"model_size": "medium",
"vad_sensitivity": 3,
"silence_timeout": 2.0,
}
}
with open(self.temp_config_file, "w") as f:
json.dump(old_config, f)
# Load the config (should trigger migration)
config_manager = ConfigManager()
# Verify migration added per-engine model sizes
self.assertEqual(config_manager.config["speech_recognition"]["vosk_model_size"], "medium")
self.assertEqual(config_manager.config["speech_recognition"]["whisper_model_size"], "tiny")
def test_update_speech_recognition_settings_per_engine(self):
"""Test that update_speech_recognition_settings saves per-engine model sizes."""
config_manager = ConfigManager()
# Update settings for vosk
config_manager.update_speech_recognition_settings({"engine": "vosk", "model_size": "large"})
self.assertEqual(config_manager.config["speech_recognition"]["vosk_model_size"], "large")
# Update settings for whisper
config_manager.update_speech_recognition_settings(
{"engine": "whisper", "model_size": "small"}
)
self.assertEqual(config_manager.config["speech_recognition"]["whisper_model_size"], "small")
# Verify the vosk setting wasn't changed
self.assertEqual(config_manager.config["speech_recognition"]["vosk_model_size"], "large")
def test_save_settings(self):
"""Test the save_settings method (alias for save_config)."""
config_manager = ConfigManager()
# Modify config
config_manager.config["speech_recognition"]["engine"] = "vosk"
# Save settings
result = config_manager.save_settings()
self.assertTrue(result)
# Verify file was saved
self.assertTrue(os.path.exists(self.temp_config_file))
with open(self.temp_config_file, "r") as f:
saved_config = json.load(f)
self.assertEqual(saved_config["speech_recognition"]["engine"], "vosk")
def test_get_settings(self):
"""Test the get_settings method returns the entire config."""
config_manager = ConfigManager()
# Get settings
settings = config_manager.get_settings()
# Verify it returns the config
self.assertEqual(settings, config_manager.config)
self.assertIn("speech_recognition", settings)
self.assertIn("ui", settings)
def test_get_model_size_for_engine_with_engine_specific_key(self):
"""Test getting model size when engine-specific key exists."""
# Create config with engine-specific model sizes
test_config = {
"speech_recognition": {
"engine": "whisper",
"vosk_model_size": "medium",
"whisper_model_size": "small",
"model_size": "large", # Generic fallback
}
}
with open(self.temp_config_file, "w") as f:
json.dump(test_config, f)
config_manager = ConfigManager()
# Should use engine-specific size, not generic
self.assertEqual(config_manager.get_model_size_for_engine("vosk"), "medium")
self.assertEqual(config_manager.get_model_size_for_engine("whisper"), "small")
def test_set_model_size_for_engine_new_section(self):
"""Test setting model size when speech_recognition section doesn't exist."""
config_manager = ConfigManager()
# Remove the speech_recognition section
del config_manager.config["speech_recognition"]
# Set model size - should create the section
config_manager.set_model_size_for_engine("vosk", "large")
# Verify section was created and value was set
self.assertIn("speech_recognition", config_manager.config)
self.assertEqual(config_manager.config["speech_recognition"]["vosk_model_size"], "large")
def test_get_model_size_for_engine_fallback_to_generic(self):
"""Test fallback to generic model_size when no engine-specific key exists."""
# Create config with only generic model_size (no engine-specific)
test_config = {
"speech_recognition": {
"engine": "vosk",
"model_size": "large",
# No vosk_model_size or whisper_model_size
}
}
with open(self.temp_config_file, "w") as f:
json.dump(test_config, f)
config_manager = ConfigManager()
# Should fall back to generic model_size
# Note: migration might add engine-specific keys, so we test this directly
del config_manager.config["speech_recognition"]["vosk_model_size"]
del config_manager.config["speech_recognition"]["whisper_model_size"]
self.assertEqual(config_manager.get_model_size_for_engine("vosk"), "large")
self.assertEqual(config_manager.get_model_size_for_engine("whisper"), "large")
def test_update_speech_recognition_settings_new_section(self):
"""Test update_speech_recognition_settings when section doesn't exist."""
config_manager = ConfigManager()
# Remove the speech_recognition section
del config_manager.config["speech_recognition"]
# Update settings - should create the section
config_manager.update_speech_recognition_settings(
{"engine": "whisper", "vad_sensitivity": 5}
)
# Verify section was created
self.assertIn("speech_recognition", config_manager.config)
self.assertEqual(config_manager.config["speech_recognition"]["engine"], "whisper")
self.assertEqual(config_manager.config["speech_recognition"]["vad_sensitivity"], 5)