Files
whisper-local/tests/test_config.py
T
2026-04-14 21:26:36 +02:00

139 lines
4.6 KiB
Python

from pathlib import Path
from unittest.mock import patch
import os
import tomllib
from whisper_local.config import Config, load_config, save_config
class TestConfigDefaults:
def test_default_hotkey(self):
config = Config()
assert config.hotkey == "KEY_F12"
def test_default_whisper_model(self):
config = Config()
assert config.whisper_model == "small"
def test_default_language(self):
config = Config()
assert config.language == "de"
def test_default_compute_type(self):
config = Config()
assert config.compute_type == "int8"
def test_default_sample_rate(self):
config = Config()
assert config.sample_rate == 16000
def test_default_channels(self):
config = Config()
assert config.channels == 1
def test_default_min_duration(self):
config = Config()
assert config.min_duration == 0.5
class TestLoadConfig:
def test_load_from_toml(self, tmp_path):
config_file = tmp_path / "config.toml"
config_file.write_text(
'[hotkey]\nkey = "KEY_F8"\n\n'
'[whisper]\nmodel = "tiny"\nlanguage = "en"\n'
)
config = load_config(config_file)
assert config.hotkey == "KEY_F8"
assert config.whisper_model == "tiny"
assert config.language == "en"
# Defaults bleiben erhalten
assert config.compute_type == "int8"
def test_load_nonexistent_returns_defaults(self, tmp_path):
config = load_config(tmp_path / "nope.toml")
assert config.hotkey == "KEY_F12"
def test_load_empty_file(self, tmp_path):
config_file = tmp_path / "config.toml"
config_file.write_text("")
config = load_config(config_file)
assert config.hotkey == "KEY_F12"
class TestConfigPath:
@patch.dict(os.environ, {"APPDATA": "C:\\Users\\test\\AppData\\Roaming"})
@patch("whisper_local.config.sys")
def test_windows_config_path(self, mock_sys):
mock_sys.platform = "win32"
from whisper_local.config import _default_config_path
result = _default_config_path()
assert result == Path("C:\\Users\\test\\AppData\\Roaming") / "whisper-local" / "config.toml"
@patch("whisper_local.config.sys")
def test_linux_config_path(self, mock_sys):
mock_sys.platform = "linux"
from whisper_local.config import _default_config_path
result = _default_config_path()
assert result == Path.home() / ".config" / "whisper-local" / "config.toml"
class TestMicrophoneConfig:
def test_default_microphone_is_empty(self):
config = Config()
assert config.microphone == ""
def test_load_device_from_toml(self, tmp_path):
config_file = tmp_path / "config.toml"
config_file.write_text('[audio]\ndevice = "Headset Mic"\n')
config = load_config(config_file)
assert config.microphone == "Headset Mic"
class TestSaveConfig:
def test_save_and_reload(self, tmp_path):
from whisper_local.config import save_config
path = tmp_path / "config.toml"
config = Config(hotkey="KEY_F8", microphone="USB Mic")
save_config(config, path)
loaded = load_config(path)
assert loaded.hotkey == "KEY_F8"
assert loaded.microphone == "USB Mic"
def test_save_creates_parent_dirs(self, tmp_path):
from whisper_local.config import save_config
path = tmp_path / "subdir" / "config.toml"
save_config(Config(), path)
assert path.exists()
def test_save_handles_special_characters_in_microphone(self, tmp_path):
from whisper_local.config import save_config
path = tmp_path / "config.toml"
config = Config(microphone='USB "Pro" Mic')
save_config(config, path)
loaded = load_config(path)
assert loaded.microphone == 'USB "Pro" Mic'
class TestPauseMediaDuringRecording:
def test_default_pause_media_during_recording_is_true(self):
config = Config()
assert config.pause_media_during_recording is True
def test_load_config_reads_pause_media_false(self, tmp_path: Path):
cfg_path = tmp_path / "config.toml"
cfg_path.write_text("[media]\npause_during_recording = false\n", encoding="utf-8")
config = load_config(cfg_path)
assert config.pause_media_during_recording is False
def test_save_config_roundtrip_preserves_pause_media(self, tmp_path: Path):
cfg_path = tmp_path / "config.toml"
original = Config(pause_media_during_recording=False)
save_config(original, cfg_path)
loaded = load_config(cfg_path)
assert loaded.pause_media_during_recording is False