| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- """Tests for configuration loading."""
- import tempfile
- from pathlib import Path
- from stt_daemon.config import load_config
- def test_load_config_defaults():
- """Test that load_config returns defaults when no file exists."""
- with tempfile.TemporaryDirectory() as tmpdir:
- # Change to temp dir and load (no config.toml exists)
- import os
- old_cwd = os.getcwd()
- try:
- os.chdir(tmpdir)
- cfg = load_config()
- assert cfg.audio.backend == "parec"
- assert cfg.audio.device == "VirtualMicSink.monitor"
- assert cfg.audio.sample_rate == 16000
- assert cfg.vad.aggressiveness == 2
- assert cfg.whisper.model == "small"
- assert cfg.whisper.language is None
- assert cfg.output.stdout is True
- finally:
- os.chdir(old_cwd)
- def test_load_config_from_file():
- """Test loading config from a TOML file."""
- config_toml = """
- [audio]
- backend = "sounddevice"
- device = "hw:0"
- sample_rate = 44100
- [vad]
- aggressiveness = 3
- pre_roll_ms = 500
- [whisper]
- model = "base"
- device = "cuda"
- language = "es"
- [output]
- stdout = false
- jsonl_dir = "logs"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.audio.backend == "sounddevice"
- assert cfg.audio.device == "hw:0"
- assert cfg.audio.sample_rate == 44100
- assert cfg.vad.aggressiveness == 3
- assert cfg.vad.pre_roll_ms == 500
- assert cfg.whisper.model == "base"
- assert cfg.whisper.device == "cuda"
- assert cfg.whisper.language == "es"
- assert cfg.output.stdout is False
- assert cfg.output.jsonl_dir == "logs"
- def test_language_normalization_auto():
- """Test that language 'auto' is normalized to None."""
- config_toml = """
- [whisper]
- language = "auto"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language is None
- def test_language_normalization_empty_string():
- """Test that language '' is normalized to None."""
- config_toml = """
- [whisper]
- language = ""
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language is None
- def test_language_normalization_none():
- """Test that language 'none' is normalized to None."""
- config_toml = """
- [whisper]
- language = "none"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language is None
- def test_language_explicit_en():
- """Test that explicit language codes are preserved."""
- config_toml = """
- [whisper]
- language = "en"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- assert cfg.whisper.language == "en"
- def test_partial_config():
- """Test that partial configs merge with defaults."""
- config_toml = """
- [audio]
- device = "custom_device"
- """
- with tempfile.TemporaryDirectory() as tmpdir:
- config_path = Path(tmpdir) / "config.toml"
- config_path.write_text(config_toml)
- cfg = load_config(str(config_path))
- # Overridden
- assert cfg.audio.device == "custom_device"
- # Defaults
- assert cfg.audio.backend == "parec"
- assert cfg.audio.sample_rate == 16000
- assert cfg.vad.aggressiveness == 2
|