test_config.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """Tests for configuration loading."""
  2. import tempfile
  3. from pathlib import Path
  4. from stt_daemon.config import load_config
  5. def test_load_config_defaults():
  6. """Test that load_config returns defaults when no file exists."""
  7. with tempfile.TemporaryDirectory() as tmpdir:
  8. # Change to temp dir and load (no config.toml exists)
  9. import os
  10. old_cwd = os.getcwd()
  11. try:
  12. os.chdir(tmpdir)
  13. cfg = load_config()
  14. assert cfg.audio.backend == "parec"
  15. assert cfg.audio.device == "VirtualMicSink.monitor"
  16. assert cfg.audio.sample_rate == 16000
  17. assert cfg.vad.aggressiveness == 2
  18. assert cfg.whisper.model == "small"
  19. assert cfg.whisper.language is None
  20. assert cfg.output.stdout is True
  21. finally:
  22. os.chdir(old_cwd)
  23. def test_load_config_from_file():
  24. """Test loading config from a TOML file."""
  25. config_toml = """
  26. [audio]
  27. backend = "sounddevice"
  28. device = "hw:0"
  29. sample_rate = 44100
  30. [vad]
  31. aggressiveness = 3
  32. pre_roll_ms = 500
  33. [whisper]
  34. model = "base"
  35. device = "cuda"
  36. language = "es"
  37. [output]
  38. stdout = false
  39. jsonl_dir = "logs"
  40. """
  41. with tempfile.TemporaryDirectory() as tmpdir:
  42. config_path = Path(tmpdir) / "config.toml"
  43. config_path.write_text(config_toml)
  44. cfg = load_config(str(config_path))
  45. assert cfg.audio.backend == "sounddevice"
  46. assert cfg.audio.device == "hw:0"
  47. assert cfg.audio.sample_rate == 44100
  48. assert cfg.vad.aggressiveness == 3
  49. assert cfg.vad.pre_roll_ms == 500
  50. assert cfg.whisper.model == "base"
  51. assert cfg.whisper.device == "cuda"
  52. assert cfg.whisper.language == "es"
  53. assert cfg.output.stdout is False
  54. assert cfg.output.jsonl_dir == "logs"
  55. def test_language_normalization_auto():
  56. """Test that language 'auto' is normalized to None."""
  57. config_toml = """
  58. [whisper]
  59. language = "auto"
  60. """
  61. with tempfile.TemporaryDirectory() as tmpdir:
  62. config_path = Path(tmpdir) / "config.toml"
  63. config_path.write_text(config_toml)
  64. cfg = load_config(str(config_path))
  65. assert cfg.whisper.language is None
  66. def test_language_normalization_empty_string():
  67. """Test that language '' is normalized to None."""
  68. config_toml = """
  69. [whisper]
  70. language = ""
  71. """
  72. with tempfile.TemporaryDirectory() as tmpdir:
  73. config_path = Path(tmpdir) / "config.toml"
  74. config_path.write_text(config_toml)
  75. cfg = load_config(str(config_path))
  76. assert cfg.whisper.language is None
  77. def test_language_normalization_none():
  78. """Test that language 'none' is normalized to None."""
  79. config_toml = """
  80. [whisper]
  81. language = "none"
  82. """
  83. with tempfile.TemporaryDirectory() as tmpdir:
  84. config_path = Path(tmpdir) / "config.toml"
  85. config_path.write_text(config_toml)
  86. cfg = load_config(str(config_path))
  87. assert cfg.whisper.language is None
  88. def test_language_explicit_en():
  89. """Test that explicit language codes are preserved."""
  90. config_toml = """
  91. [whisper]
  92. language = "en"
  93. """
  94. with tempfile.TemporaryDirectory() as tmpdir:
  95. config_path = Path(tmpdir) / "config.toml"
  96. config_path.write_text(config_toml)
  97. cfg = load_config(str(config_path))
  98. assert cfg.whisper.language == "en"
  99. def test_partial_config():
  100. """Test that partial configs merge with defaults."""
  101. config_toml = """
  102. [audio]
  103. device = "custom_device"
  104. """
  105. with tempfile.TemporaryDirectory() as tmpdir:
  106. config_path = Path(tmpdir) / "config.toml"
  107. config_path.write_text(config_toml)
  108. cfg = load_config(str(config_path))
  109. # Overridden
  110. assert cfg.audio.device == "custom_device"
  111. # Defaults
  112. assert cfg.audio.backend == "parec"
  113. assert cfg.audio.sample_rate == 16000
  114. assert cfg.vad.aggressiveness == 2