test(coverage): improve test coverage from 57% to 80%+ (#327)

* test(coverage): improve test coverage from 57% to 80%+

Add 30 new test files with 861 additional test cases to improve
overall test coverage from 57% to 80%+ (statement coverage).

Key modules covered:
- recognition_manager.py: 43% → 69% (audio device detection, transcription,
  model download, engine initialization, buffer management)
- text_injector.py: improved coverage for X11/Wayland text injection,
  environment detection, clipboard fallback, shortcut injection
- ibus_engine.py: 22% → 73% (engine lifecycle, text injection, socket server)
- evdev_backend.py: 11% → 78% (device detection, key event handling)
- pynput_backend.py: 43% → 76% (start/stop, key handling, callbacks)
- keyboard_backends/__init__.py: 54% → improved (backend creation, env detection)
- whispercpp_model_info.py: 32% → improved (GPU detection, model recommendations)
- about_dialog.py, first_run_dialog.py, main.py, single_instance.py,
  common_types.py, config_manager.py, autostart_manager.py: various improvements

All 1433 tests pass with proper test isolation to prevent mock pollution
between test modules.

* test(quality): audit and fix test quality issues

Code review audit identified and fixed the following issues:

- Remove bare try/except blocks that swallow all errors (7 tests fixed
  in recognition_manager_deep, replaced with proper assertions or deleted)
- Delete source code inspection tests that read .py files as strings
  instead of testing behavior (deleted test_about_dialog_coverage.py and
  test_first_run_dialog_coverage.py entirely - ~35 useless tests removed)
- Add meaningful assertions replacing weak assertIsNotNone/assertIsInstance
  checks (common_types_coverage, ibus_engine_coverage, keyboard_backends_extra)
- Fix tests that mock the system under test instead of its dependencies
  (text_injector_extra now mocks subprocess.run instead of injection methods)
- Add missing assertions to tests with no verification
  (ibus_engine focus_out, log window info, copy to clipboard)
- Verify download tests check requests.get was called with correct URLs
  (coverage_boost download tests)
- Fix pytest warning by renaming test_audio_input import to avoid
  pytest collecting it as a test function

All 1356 tests pass with 0 failures and 0 warnings.
Statement coverage: 80.3% (3257/4054 lines covered).

* style(tests): fix black formatting and isort import ordering

Auto-format all new test files with black and isort to pass CI lint checks.

* fix(tests): resolve CI hangs and rename test files for clarity

- Fix CI hangs by properly mocking gi/GTK/IBus in all test files
- Fix sys.modules pollution in recognition manager tests by using tmp_path
- Add pytest-timeout to prevent future hangs
- Rename 28 test files to use descriptive, consistent names mapping to source modules

* fix(tests): resolve Python 3.13 CI failures

- Fix _load_stdlib_module() in test_recognition_manager_downloads.py to
  handle stdlib modules that are packages (directories with __init__.py)
  rather than single .py files. In Python 3.13, zipfile became a package,
  causing FileNotFoundError during test collection (exit code 2).

- Fix invalid escape sequence '$' in test_cross_distro_compatibility.py
  by using a raw string. Python 3.13 reports this as SyntaxWarning and
  Python 3.14 will make it a SyntaxError.

* fix(tests): use direct import for zipfile instead of _load_stdlib_module

The _load_stdlib_module approach fails on Python 3.13 where zipfile is a
package (zipfile/__init__.py). Loading it via spec_from_file_location with
a custom name breaks relative imports inside the package (e.g.
'from ._path import ...' resolves to '_stdlib_zipfile._path' which doesn't
exist).

Simply import zipfile at module level before any test mocks can pollute
sys.modules — this is safe because the module-level code runs during test
collection, before any test fixtures or patches are applied.

* fix(tests): mock gi/GTK in conftest to prevent CI hangs

On CI, real PyGObject is installed and module-level imports in vocalinux
source files (ibus_engine, tray_indicator, settings_dialog, etc.) trigger
GTK initialization or IBus daemon connections that hang indefinitely in
headless environments. pytest-timeout's signal-based timeout cannot
interrupt these C-level blocking calls.

Fix by mocking gi and gi.repository in conftest.py (which runs before any
test module is collected), ensuring all test files are protected regardless
of whether they remember to mock individually.

Also add timeout-minutes: 10 to the CI test job as a safety net so that
any remaining hangs fail fast instead of running indefinitely.

* fix(tests): unconditionally mock gi in conftest for CI

The previous 'if gi not in sys.modules' guard was ineffective on CI
because PyGObject is installed via pip, so the real gi module is already
loaded. This caused GTK initialization and IBus daemon connections that
hang indefinitely in headless CI environments.

Now unconditionally replace gi and gi.repository with mocks during test
collection, regardless of whether the real package is installed.

* fix(tests): reduce timeout to 10s and use thread-based timeout

Use timeout_method=thread instead of signal (default) so that
pytest-timeout can interrupt C-level blocking calls (GTK operations,
socket accepts, subprocess waits) that the signal-based method cannot
handle. Also reduce per-test timeout from 30s to 10s since no legitimate
unit test should take longer, and hanging tests were consuming the entire
CI job timeout budget.

* fix(tests): set global socket timeout to prevent blocking on CI

Tests in test_ibus_engine.py create real Unix sockets with daemon threads
that block on socket.accept(). When tests fail or timeout, these threads
remain blocked indefinitely, preventing pytest from completing. The
thread-based timeout catches the hang but cannot terminate the blocking
accept() call.

Set socket.setdefaulttimeout(5) in conftest.py so all socket operations
(accept, connect, recv) will raise socket.timeout after 5 seconds instead
of blocking forever.

* fix(tests): add cleanup fixture for IBus socket server threads

Add autouse fixture that stops any leftover VocalinuxEngine socket server
threads after each test. Tests that trigger _start_socket_server() spawn
daemon threads blocking on socket.accept() that persist after the test
completes, eventually causing pytest-timeout to kill the process.

The cleanup fixture sets _server_running=False and closes the server
socket, which unblocks the accept() call and allows the daemon thread
to terminate.

* fix(tests): reduce socket timeout to 0.1s to avoid slowing tests

The 5-second socket timeout caused socket operations in daemon threads
to block for 5 seconds per iteration before timing out, significantly
slowing the test suite on CI. Reduce to 0.1s so blocking socket calls
fail fast without noticeably impacting test execution time.

* fix(tests): use 2s socket timeout and increase CI timeout to 15min

The 0.1s socket timeout was too aggressive and caused legitimate test
socket operations to fail. Use 2s which is long enough for test sockets
to work but short enough to unblock daemon threads.

Increase CI job timeout to 15 minutes to accommodate the socket timeout
overhead from daemon threads that retry every 2 seconds.

* fix(tests): block known-blocking daemon threads to prevent CI hangs

Instead of using socket timeouts (which either break tests or are too
slow), prevent specific known-blocking daemon threads from starting:
- ibus_engine.server_thread: blocks on socket.accept()
- evdev_backend._monitor_devices: blocks on select.select()

These threads are identified by their target function name and skipped
during Thread.start(). The thread is marked as started/stopped so
join() calls in cleanup code don't raise RuntimeError.

Test-created daemon threads (e.g. mock socket servers) are unaffected
since they use different target function names.

* fix(tests): patch Thread.join for Python 3.13 compatibility

Python 3.13 changed threading internals — join() now uses _handle.join()
which raises 'thread not started' for threads that were never actually
started. Instead of trying to fake internal state, patch join() to be a
no-op for threads that were skipped by our _safe_thread_start guard.

This fixes the 2 remaining test_evdev_backend_ext failures on Python 3.13.
This commit is contained in:
Jatin K Malik
2026-03-17 14:05:16 -07:00
committed by GitHub
parent 9c16ab0aad
commit fa4da40e7b
32 changed files with 12708 additions and 2 deletions
+645
View File
@@ -0,0 +1,645 @@
"""
Comprehensive coverage tests for whispercpp_model_info module.
Tests all functions and constants in the module.
"""
import subprocess
import unittest
from unittest.mock import MagicMock, patch
class TestComputeBackendClass(unittest.TestCase):
"""Test cases for ComputeBackend class."""
def test_compute_backend_has_vulkan_constant(self):
"""Test that ComputeBackend has VULKAN constant."""
from vocalinux.utils.whispercpp_model_info import ComputeBackend
self.assertTrue(hasattr(ComputeBackend, "VULKAN"))
self.assertEqual(ComputeBackend.VULKAN, "vulkan")
def test_compute_backend_has_cuda_constant(self):
"""Test that ComputeBackend has CUDA constant."""
from vocalinux.utils.whispercpp_model_info import ComputeBackend
self.assertTrue(hasattr(ComputeBackend, "CUDA"))
self.assertEqual(ComputeBackend.CUDA, "cuda")
def test_compute_backend_has_cpu_constant(self):
"""Test that ComputeBackend has CPU constant."""
from vocalinux.utils.whispercpp_model_info import ComputeBackend
self.assertTrue(hasattr(ComputeBackend, "CPU"))
self.assertEqual(ComputeBackend.CPU, "cpu")
class TestModelInfo(unittest.TestCase):
"""Test cases for model information constants."""
def test_whispercpp_model_info_exists(self):
"""Test that WHISPERCPP_MODEL_INFO is defined."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
self.assertIsInstance(WHISPERCPP_MODEL_INFO, dict)
self.assertGreater(len(WHISPERCPP_MODEL_INFO), 0)
def test_whispercpp_model_info_contains_tiny(self):
"""Test that WHISPERCPP_MODEL_INFO contains tiny model."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
self.assertIn("tiny", WHISPERCPP_MODEL_INFO)
def test_whispercpp_model_info_contains_base(self):
"""Test that WHISPERCPP_MODEL_INFO contains base model."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
self.assertIn("base", WHISPERCPP_MODEL_INFO)
def test_whispercpp_model_info_contains_small(self):
"""Test that WHISPERCPP_MODEL_INFO contains small model."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
self.assertIn("small", WHISPERCPP_MODEL_INFO)
def test_whispercpp_model_info_contains_medium(self):
"""Test that WHISPERCPP_MODEL_INFO contains medium model."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
self.assertIn("medium", WHISPERCPP_MODEL_INFO)
def test_whispercpp_model_info_contains_large(self):
"""Test that WHISPERCPP_MODEL_INFO contains large model."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
self.assertIn("large", WHISPERCPP_MODEL_INFO)
def test_model_info_has_size_mb(self):
"""Test that each model has size_mb field."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
for model_name, model_info in WHISPERCPP_MODEL_INFO.items():
self.assertIn("size_mb", model_info, f"{model_name} missing size_mb")
self.assertIsInstance(model_info["size_mb"], int)
self.assertGreater(model_info["size_mb"], 0)
def test_model_info_has_params(self):
"""Test that each model has params field."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
for model_name, model_info in WHISPERCPP_MODEL_INFO.items():
self.assertIn("params", model_info, f"{model_name} missing params")
self.assertIsInstance(model_info["params"], str)
def test_model_info_has_desc(self):
"""Test that each model has desc field."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
for model_name, model_info in WHISPERCPP_MODEL_INFO.items():
self.assertIn("desc", model_info, f"{model_name} missing desc")
self.assertIsInstance(model_info["desc"], str)
def test_model_info_has_url(self):
"""Test that each model has url field."""
from vocalinux.utils.whispercpp_model_info import WHISPERCPP_MODEL_INFO
for model_name, model_info in WHISPERCPP_MODEL_INFO.items():
self.assertIn("url", model_info, f"{model_name} missing url")
self.assertIsInstance(model_info["url"], str)
self.assertTrue(model_info["url"].startswith("https://"))
def test_available_models_list_exists(self):
"""Test that AVAILABLE_MODELS list exists."""
from vocalinux.utils.whispercpp_model_info import AVAILABLE_MODELS
self.assertIsInstance(AVAILABLE_MODELS, list)
self.assertGreater(len(AVAILABLE_MODELS), 0)
def test_available_models_contains_all_models(self):
"""Test that AVAILABLE_MODELS contains all defined models."""
from vocalinux.utils.whispercpp_model_info import (
AVAILABLE_MODELS,
WHISPERCPP_MODEL_INFO,
)
self.assertEqual(set(AVAILABLE_MODELS), set(WHISPERCPP_MODEL_INFO.keys()))
class TestDetectVulkanSupport(unittest.TestCase):
"""Test cases for detect_vulkan_support function."""
def test_detect_vulkan_support_returns_tuple(self):
"""Test that detect_vulkan_support returns a tuple."""
from vocalinux.utils.whispercpp_model_info import detect_vulkan_support
result = detect_vulkan_support()
self.assertIsInstance(result, tuple)
self.assertEqual(len(result), 2)
def test_detect_vulkan_support_when_available(self):
"""Test Vulkan detection when vulkaninfo is available."""
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="deviceName: Intel Arc\n")
from vocalinux.utils.whispercpp_model_info import detect_vulkan_support
is_available, device_name = detect_vulkan_support()
self.assertTrue(is_available)
self.assertIsNotNone(device_name)
def test_detect_vulkan_support_when_unavailable(self):
"""Test Vulkan detection when vulkaninfo is not available."""
with patch("subprocess.run") as mock_run:
mock_run.side_effect = FileNotFoundError("vulkaninfo not found")
from vocalinux.utils.whispercpp_model_info import detect_vulkan_support
is_available, device_name = detect_vulkan_support()
self.assertFalse(is_available)
self.assertIsNone(device_name)
def test_detect_vulkan_support_on_timeout(self):
"""Test Vulkan detection when command times out."""
with patch("subprocess.run") as mock_run:
mock_run.side_effect = subprocess.TimeoutExpired("vulkaninfo", 5)
from vocalinux.utils.whispercpp_model_info import detect_vulkan_support
is_available, device_name = detect_vulkan_support()
self.assertFalse(is_available)
self.assertIsNone(device_name)
def test_detect_vulkan_support_on_error(self):
"""Test Vulkan detection when command returns error."""
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=1, stdout="")
from vocalinux.utils.whispercpp_model_info import detect_vulkan_support
is_available, device_name = detect_vulkan_support()
self.assertFalse(is_available)
self.assertIsNone(device_name)
class TestDetectCudaSupport(unittest.TestCase):
"""Test cases for detect_cuda_support function."""
def test_detect_cuda_support_returns_tuple(self):
"""Test that detect_cuda_support returns a tuple."""
from vocalinux.utils.whispercpp_model_info import detect_cuda_support
result = detect_cuda_support()
self.assertIsInstance(result, tuple)
self.assertEqual(len(result), 2)
def test_detect_cuda_support_when_available(self):
"""Test CUDA detection when nvidia-smi is available."""
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="NVIDIA RTX 3080, 10240 MiB\n")
from vocalinux.utils.whispercpp_model_info import detect_cuda_support
is_available, device_info = detect_cuda_support()
self.assertTrue(is_available)
self.assertIsNotNone(device_info)
self.assertIn("RTX 3080", device_info)
def test_detect_cuda_support_when_unavailable(self):
"""Test CUDA detection when nvidia-smi is not available."""
with patch("subprocess.run") as mock_run:
mock_run.side_effect = FileNotFoundError("nvidia-smi not found")
from vocalinux.utils.whispercpp_model_info import detect_cuda_support
is_available, device_info = detect_cuda_support()
self.assertFalse(is_available)
self.assertIsNone(device_info)
def test_detect_cuda_support_on_timeout(self):
"""Test CUDA detection when command times out."""
with patch("subprocess.run") as mock_run:
mock_run.side_effect = subprocess.TimeoutExpired("nvidia-smi", 5)
from vocalinux.utils.whispercpp_model_info import detect_cuda_support
is_available, device_info = detect_cuda_support()
self.assertFalse(is_available)
self.assertIsNone(device_info)
class TestDetectCPUInfo(unittest.TestCase):
"""Test cases for detect_cpu_info function."""
def test_detect_cpu_info_returns_string(self):
"""Test that detect_cpu_info returns a string."""
from vocalinux.utils.whispercpp_model_info import detect_cpu_info
result = detect_cpu_info()
self.assertIsInstance(result, str)
self.assertGreater(len(result), 0)
def test_detect_cpu_info_from_proc_cpuinfo(self):
"""Test CPU detection from /proc/cpuinfo."""
cpu_info_content = "model name : Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz\n"
with patch("builtins.open", create=True) as mock_open:
mock_open.return_value.__enter__.return_value = iter([cpu_info_content])
from vocalinux.utils.whispercpp_model_info import detect_cpu_info
result = detect_cpu_info()
self.assertIsInstance(result, str)
self.assertGreater(len(result), 0)
def test_detect_cpu_info_fallback_to_nproc(self):
"""Test CPU detection falls back to nproc."""
with patch("builtins.open", create=True, side_effect=Exception("No /proc/cpuinfo")), patch(
"subprocess.run"
) as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="8\n")
from vocalinux.utils.whispercpp_model_info import detect_cpu_info
result = detect_cpu_info()
self.assertIsInstance(result, str)
def test_detect_cpu_info_default_fallback(self):
"""Test CPU detection returns default when all methods fail."""
with patch("builtins.open", create=True, side_effect=Exception()), patch(
"subprocess.run", side_effect=Exception()
):
from vocalinux.utils.whispercpp_model_info import detect_cpu_info
result = detect_cpu_info()
self.assertEqual(result, "CPU")
class TestDetectComputeBackend(unittest.TestCase):
"""Test cases for detect_compute_backend function."""
def test_detect_compute_backend_returns_tuple(self):
"""Test that detect_compute_backend returns a tuple."""
from vocalinux.utils.whispercpp_model_info import detect_compute_backend
result = detect_compute_backend()
self.assertIsInstance(result, tuple)
self.assertEqual(len(result), 2)
def test_detect_compute_backend_prefers_vulkan(self):
"""Test that detect_compute_backend prefers Vulkan."""
with patch(
"vocalinux.utils.whispercpp_model_info.detect_vulkan_support"
) as mock_vulkan, patch(
"vocalinux.utils.whispercpp_model_info.detect_cuda_support"
) as mock_cuda:
mock_vulkan.return_value = (True, "Intel Arc GPU")
mock_cuda.return_value = (True, "NVIDIA RTX 3080 (10GB)")
from vocalinux.utils.whispercpp_model_info import (
ComputeBackend,
detect_compute_backend,
)
backend, info = detect_compute_backend()
self.assertEqual(backend, ComputeBackend.VULKAN)
def test_detect_compute_backend_falls_back_to_cuda(self):
"""Test that detect_compute_backend falls back to CUDA."""
with patch(
"vocalinux.utils.whispercpp_model_info.detect_vulkan_support"
) as mock_vulkan, patch(
"vocalinux.utils.whispercpp_model_info.detect_cuda_support"
) as mock_cuda:
mock_vulkan.return_value = (False, None)
mock_cuda.return_value = (True, "NVIDIA RTX 3080 (10GB)")
from vocalinux.utils.whispercpp_model_info import (
ComputeBackend,
detect_compute_backend,
)
backend, info = detect_compute_backend()
self.assertEqual(backend, ComputeBackend.CUDA)
def test_detect_compute_backend_falls_back_to_cpu(self):
"""Test that detect_compute_backend falls back to CPU."""
with patch(
"vocalinux.utils.whispercpp_model_info.detect_vulkan_support"
) as mock_vulkan, patch(
"vocalinux.utils.whispercpp_model_info.detect_cuda_support"
) as mock_cuda, patch(
"vocalinux.utils.whispercpp_model_info.detect_cpu_info"
) as mock_cpu:
mock_vulkan.return_value = (False, None)
mock_cuda.return_value = (False, None)
mock_cpu.return_value = "Intel Core i7"
from vocalinux.utils.whispercpp_model_info import (
ComputeBackend,
detect_compute_backend,
)
backend, info = detect_compute_backend()
self.assertEqual(backend, ComputeBackend.CPU)
class TestGetRecommendedModel(unittest.TestCase):
"""Test cases for get_recommended_model function."""
def test_get_recommended_model_returns_tuple(self):
"""Test that get_recommended_model returns a tuple."""
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=8 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend",
return_value=("cpu", "Intel Core i7"),
):
import importlib
import vocalinux.utils.whispercpp_model_info as mod
importlib.reload(mod)
result = mod.get_recommended_model()
self.assertIsInstance(result, tuple)
self.assertEqual(len(result), 2)
def test_get_recommended_model_with_vulkan_high_ram(self):
"""Test model recommendation with Vulkan and 8GB+ RAM."""
import sys
# Create a mock psutil module
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=8 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend"
) as mock_backend:
mock_backend.return_value = ("vulkan", "Intel Arc A770")
from vocalinux.utils.whispercpp_model_info import get_recommended_model
model, reason = get_recommended_model()
self.assertEqual(model, "small")
def test_get_recommended_model_with_cuda_high_vram(self):
"""Test model recommendation with CUDA and high VRAM."""
import sys
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=16 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend"
) as mock_backend:
mock_backend.return_value = ("cuda", "NVIDIA RTX 4090 (24GB)")
from vocalinux.utils.whispercpp_model_info import get_recommended_model
model, reason = get_recommended_model()
# High VRAM should recommend medium or higher
self.assertIn(model, ["medium", "large", "small"])
def test_get_recommended_model_with_cpu_high_ram(self):
"""Test model recommendation with CPU and 16GB+ RAM."""
import sys
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=16 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend"
) as mock_backend:
mock_backend.return_value = ("cpu", "Intel Core i7")
from vocalinux.utils.whispercpp_model_info import get_recommended_model
model, reason = get_recommended_model()
self.assertEqual(model, "base")
def test_get_recommended_model_with_cpu_medium_ram(self):
"""Test model recommendation with CPU and 8GB RAM."""
import sys
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=8 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend"
) as mock_backend:
mock_backend.return_value = ("cpu", "AMD Ryzen 5")
from vocalinux.utils.whispercpp_model_info import get_recommended_model
model, reason = get_recommended_model()
self.assertEqual(model, "tiny")
def test_get_recommended_model_with_cpu_low_ram(self):
"""Test model recommendation with CPU and 4GB RAM."""
import sys
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=4 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend"
) as mock_backend:
mock_backend.return_value = ("cpu", "Intel Atom")
from vocalinux.utils.whispercpp_model_info import get_recommended_model
model, reason = get_recommended_model()
self.assertEqual(model, "tiny")
def test_get_recommended_model_returns_valid_model_name(self):
"""Test that get_recommended_model returns a valid model name."""
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=8 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend",
return_value=("cpu", "Intel Core i7"),
):
import importlib
import vocalinux.utils.whispercpp_model_info as mod
importlib.reload(mod)
model, reason = mod.get_recommended_model()
self.assertIn(model, mod.AVAILABLE_MODELS)
def test_get_recommended_model_returns_reason_string(self):
"""Test that get_recommended_model returns a reason string."""
mock_psutil = MagicMock()
mock_psutil.virtual_memory.return_value = MagicMock(total=8 * (1024**3))
with patch.dict("sys.modules", {"psutil": mock_psutil}):
with patch(
"vocalinux.utils.whispercpp_model_info.detect_compute_backend",
return_value=("cpu", "Intel Core i7"),
):
import importlib
import vocalinux.utils.whispercpp_model_info as mod
importlib.reload(mod)
model, reason = mod.get_recommended_model()
self.assertIsInstance(reason, str)
self.assertGreater(len(reason), 0)
class TestGetModelPath(unittest.TestCase):
"""Test cases for get_model_path function."""
def test_get_model_path_returns_string(self):
"""Test that get_model_path returns a string."""
from vocalinux.utils.whispercpp_model_info import get_model_path
result = get_model_path("tiny")
self.assertIsInstance(result, str)
self.assertGreater(len(result), 0)
def test_get_model_path_tiny(self):
"""Test get_model_path for tiny model."""
from vocalinux.utils.whispercpp_model_info import get_model_path
path = get_model_path("tiny")
self.assertIn("ggml-tiny.bin", path)
def test_get_model_path_base(self):
"""Test get_model_path for base model."""
from vocalinux.utils.whispercpp_model_info import get_model_path
path = get_model_path("base")
self.assertIn("ggml-base.bin", path)
def test_get_model_path_small(self):
"""Test get_model_path for small model."""
from vocalinux.utils.whispercpp_model_info import get_model_path
path = get_model_path("small")
self.assertIn("ggml-small.bin", path)
def test_get_model_path_medium(self):
"""Test get_model_path for medium model."""
from vocalinux.utils.whispercpp_model_info import get_model_path
path = get_model_path("medium")
self.assertIn("ggml-medium.bin", path)
def test_get_model_path_large_uses_v3(self):
"""Test get_model_path for large model uses v3 variant."""
from vocalinux.utils.whispercpp_model_info import get_model_path
path = get_model_path("large")
self.assertIn("ggml-large-v3.bin", path)
class TestIsModelDownloaded(unittest.TestCase):
"""Test cases for is_model_downloaded function."""
def test_is_model_downloaded_returns_bool(self):
"""Test that is_model_downloaded returns a boolean."""
from vocalinux.utils.whispercpp_model_info import is_model_downloaded
result = is_model_downloaded("tiny")
self.assertIsInstance(result, bool)
def test_is_model_downloaded_when_exists(self):
"""Test is_model_downloaded when model file exists."""
with patch("os.path.exists", return_value=True):
from vocalinux.utils.whispercpp_model_info import is_model_downloaded
result = is_model_downloaded("base")
self.assertTrue(result)
def test_is_model_downloaded_when_not_exists(self):
"""Test is_model_downloaded when model file does not exist."""
with patch("os.path.exists", return_value=False):
from vocalinux.utils.whispercpp_model_info import is_model_downloaded
result = is_model_downloaded("base")
self.assertFalse(result)
class TestGetBackendDisplayName(unittest.TestCase):
"""Test cases for get_backend_display_name function."""
def test_get_backend_display_name_returns_string(self):
"""Test that get_backend_display_name returns a string."""
from vocalinux.utils.whispercpp_model_info import (
ComputeBackend,
get_backend_display_name,
)
result = get_backend_display_name(ComputeBackend.CPU)
self.assertIsInstance(result, str)
self.assertGreater(len(result), 0)
def test_get_backend_display_name_vulkan(self):
"""Test display name for Vulkan backend."""
from vocalinux.utils.whispercpp_model_info import (
ComputeBackend,
get_backend_display_name,
)
result = get_backend_display_name(ComputeBackend.VULKAN)
self.assertEqual(result, "Vulkan GPU")
def test_get_backend_display_name_cuda(self):
"""Test display name for CUDA backend."""
from vocalinux.utils.whispercpp_model_info import (
ComputeBackend,
get_backend_display_name,
)
result = get_backend_display_name(ComputeBackend.CUDA)
self.assertEqual(result, "NVIDIA CUDA")
def test_get_backend_display_name_cpu(self):
"""Test display name for CPU backend."""
from vocalinux.utils.whispercpp_model_info import (
ComputeBackend,
get_backend_display_name,
)
result = get_backend_display_name(ComputeBackend.CPU)
self.assertEqual(result, "CPU")
def test_get_backend_display_name_unknown(self):
"""Test display name for unknown backend."""
from vocalinux.utils.whispercpp_model_info import get_backend_display_name
result = get_backend_display_name("rocm")
self.assertEqual(result, "ROCM")
if __name__ == "__main__":
unittest.main()