test_config.py 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. """Tests for configuration loading."""
  2. import importlib.util
  3. import tempfile
  4. import tomllib
  5. from pathlib import Path
  6. from stt_daemon.config import load_config
  7. REPO_ROOT = Path(__file__).resolve().parents[1]
  8. LAYA_REV = "970dc8c5f63d7b886a68409493f37d569424f933"
  9. def test_load_config_defaults():
  10. """Test that load_config returns defaults when no file exists."""
  11. with tempfile.TemporaryDirectory() as tmpdir:
  12. # Change to temp dir and load (no config.toml exists)
  13. import os
  14. old_cwd = os.getcwd()
  15. try:
  16. os.chdir(tmpdir)
  17. cfg = load_config()
  18. assert cfg.audio.backend == "parec"
  19. assert cfg.audio.device == "VirtualMicSink.monitor"
  20. assert cfg.audio.sample_rate == 16000
  21. assert cfg.vad.aggressiveness == 2
  22. assert cfg.whisper.model == "small"
  23. assert cfg.whisper.language is None
  24. assert cfg.output.stdout is True
  25. assert cfg.output.jsonl is False
  26. finally:
  27. os.chdir(old_cwd)
  28. def test_load_config_from_file():
  29. """Test loading config from a TOML file."""
  30. config_toml = """
  31. [audio]
  32. backend = "sounddevice"
  33. device = "hw:0"
  34. sample_rate = 44100
  35. [vad]
  36. aggressiveness = 3
  37. pre_roll_ms = 500
  38. [whisper]
  39. model = "base"
  40. device = "cuda"
  41. language = "es"
  42. [output]
  43. stdout = false
  44. jsonl = true
  45. jsonl_dir = "logs"
  46. """
  47. with tempfile.TemporaryDirectory() as tmpdir:
  48. config_path = Path(tmpdir) / "config.toml"
  49. config_path.write_text(config_toml)
  50. cfg = load_config(str(config_path))
  51. assert cfg.audio.backend == "sounddevice"
  52. assert cfg.audio.device == "hw:0"
  53. assert cfg.audio.sample_rate == 44100
  54. assert cfg.vad.aggressiveness == 3
  55. assert cfg.vad.pre_roll_ms == 500
  56. assert cfg.whisper.model == "base"
  57. assert cfg.whisper.device == "cuda"
  58. assert cfg.whisper.language == "es"
  59. assert cfg.output.stdout is False
  60. assert cfg.output.jsonl is True
  61. assert cfg.output.jsonl_dir == "logs"
  62. def test_language_normalization_auto():
  63. """Test that language 'auto' is normalized to None."""
  64. config_toml = """
  65. [whisper]
  66. language = "auto"
  67. """
  68. with tempfile.TemporaryDirectory() as tmpdir:
  69. config_path = Path(tmpdir) / "config.toml"
  70. config_path.write_text(config_toml)
  71. cfg = load_config(str(config_path))
  72. assert cfg.whisper.language is None
  73. def test_language_normalization_empty_string():
  74. """Test that language '' is normalized to None."""
  75. config_toml = """
  76. [whisper]
  77. language = ""
  78. """
  79. with tempfile.TemporaryDirectory() as tmpdir:
  80. config_path = Path(tmpdir) / "config.toml"
  81. config_path.write_text(config_toml)
  82. cfg = load_config(str(config_path))
  83. assert cfg.whisper.language is None
  84. def test_language_normalization_none():
  85. """Test that language 'none' is normalized to None."""
  86. config_toml = """
  87. [whisper]
  88. language = "none"
  89. """
  90. with tempfile.TemporaryDirectory() as tmpdir:
  91. config_path = Path(tmpdir) / "config.toml"
  92. config_path.write_text(config_toml)
  93. cfg = load_config(str(config_path))
  94. assert cfg.whisper.language is None
  95. def test_language_explicit_en():
  96. """Test that explicit language codes are preserved."""
  97. config_toml = """
  98. [whisper]
  99. language = "en"
  100. """
  101. with tempfile.TemporaryDirectory() as tmpdir:
  102. config_path = Path(tmpdir) / "config.toml"
  103. config_path.write_text(config_toml)
  104. cfg = load_config(str(config_path))
  105. assert cfg.whisper.language == "en"
  106. def test_partial_config():
  107. """Test that partial configs merge with defaults."""
  108. config_toml = """
  109. [audio]
  110. device = "custom_device"
  111. """
  112. with tempfile.TemporaryDirectory() as tmpdir:
  113. config_path = Path(tmpdir) / "config.toml"
  114. config_path.write_text(config_toml)
  115. cfg = load_config(str(config_path))
  116. # Overridden
  117. assert cfg.audio.device == "custom_device"
  118. # Defaults
  119. assert cfg.audio.backend == "parec"
  120. assert cfg.audio.sample_rate == 16000
  121. assert cfg.vad.aggressiveness == 2
  122. def test_chunking_and_confidence_fields():
  123. """New chunking/gate fields load from TOML."""
  124. config_toml = """
  125. [vad]
  126. chunk_pause_ms = 500
  127. soft_max_s = 8.0
  128. rms_floor = 0.01
  129. trailing_trim_ms = 150
  130. [whisper]
  131. no_speech_prob_max = 0.7
  132. avg_logprob_min = -0.8
  133. """
  134. with tempfile.TemporaryDirectory() as tmpdir:
  135. config_path = Path(tmpdir) / "config.toml"
  136. config_path.write_text(config_toml)
  137. cfg = load_config(str(config_path))
  138. assert cfg.vad.chunk_pause_ms == 500
  139. assert cfg.vad.soft_max_s == 8.0
  140. assert cfg.vad.rms_floor == 0.01
  141. assert cfg.vad.trailing_trim_ms == 150
  142. assert cfg.whisper.no_speech_prob_max == 0.7
  143. assert cfg.whisper.avg_logprob_min == -0.8
  144. def test_chunking_defaults():
  145. """New fields have sane defaults."""
  146. with tempfile.TemporaryDirectory() as tmpdir:
  147. import os
  148. old_cwd = os.getcwd()
  149. try:
  150. os.chdir(tmpdir)
  151. cfg = load_config()
  152. finally:
  153. os.chdir(old_cwd)
  154. assert cfg.vad.chunk_pause_ms == 350
  155. assert cfg.vad.soft_max_s == 10.0
  156. assert cfg.vad.rms_floor == 0.008
  157. assert cfg.vad.trailing_trim_ms == 200
  158. assert cfg.whisper.no_speech_prob_max == 0.6
  159. assert cfg.whisper.avg_logprob_min == -1.0
  160. def test_output_defaults_disable_audio_saving():
  161. with tempfile.TemporaryDirectory() as tmpdir:
  162. import os
  163. old_cwd = os.getcwd()
  164. try:
  165. os.chdir(tmpdir)
  166. cfg = load_config()
  167. finally:
  168. os.chdir(old_cwd)
  169. assert cfg.output.save_audio is False
  170. assert cfg.output.audio_dir == "audio"
  171. def test_output_save_audio_fields():
  172. config_toml = """
  173. [output]
  174. save_audio = true
  175. audio_dir = "clips"
  176. """
  177. with tempfile.TemporaryDirectory() as tmpdir:
  178. config_path = Path(tmpdir) / "config.toml"
  179. config_path.write_text(config_toml)
  180. cfg = load_config(str(config_path))
  181. assert cfg.output.save_audio is True
  182. assert cfg.output.audio_dir == "clips"
  183. def test_daemon_defaults():
  184. with tempfile.TemporaryDirectory() as tmpdir:
  185. import os
  186. old_cwd = os.getcwd()
  187. try:
  188. os.chdir(tmpdir)
  189. cfg = load_config()
  190. finally:
  191. os.chdir(old_cwd)
  192. assert cfg.daemon.queue_size == 50
  193. assert cfg.daemon.backlog_warn_at == 5
  194. def test_daemon_config_fields():
  195. config_toml = """
  196. [daemon]
  197. queue_size = 20
  198. backlog_warn_at = 3
  199. """
  200. with tempfile.TemporaryDirectory() as tmpdir:
  201. config_path = Path(tmpdir) / "config.toml"
  202. config_path.write_text(config_toml)
  203. cfg = load_config(str(config_path))
  204. assert cfg.daemon.queue_size == 20
  205. assert cfg.daemon.backlog_warn_at == 3
  206. def test_unknown_sections_warn_and_are_ignored(caplog):
  207. """A typo'd section like [assitant] must warn, not silently use defaults."""
  208. config_toml = """
  209. [assitant]
  210. require_viewer = false
  211. """
  212. with tempfile.TemporaryDirectory() as tmpdir:
  213. config_path = Path(tmpdir) / "config.toml"
  214. config_path.write_text(config_toml)
  215. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  216. cfg = load_config(str(config_path))
  217. assert "assitant" in caplog.text
  218. assert cfg.assistant.require_viewer is True
  219. def test_cpu_threads_default_is_auto():
  220. """cpu_threads defaults to 0 (CTranslate2's automatic choice)."""
  221. with tempfile.TemporaryDirectory() as tmpdir:
  222. import os
  223. old_cwd = os.getcwd()
  224. try:
  225. os.chdir(tmpdir)
  226. cfg = load_config()
  227. finally:
  228. os.chdir(old_cwd)
  229. assert cfg.whisper.cpu_threads == 0
  230. def test_cpu_threads_config_field():
  231. config_toml = """
  232. [whisper]
  233. cpu_threads = 16
  234. """
  235. with tempfile.TemporaryDirectory() as tmpdir:
  236. config_path = Path(tmpdir) / "config.toml"
  237. config_path.write_text(config_toml)
  238. cfg = load_config(str(config_path))
  239. assert cfg.whisper.cpu_threads == 16
  240. def test_language_strategy_defaults_to_static():
  241. """language_strategy defaults to 'static'."""
  242. with tempfile.TemporaryDirectory() as tmpdir:
  243. import os
  244. old_cwd = os.getcwd()
  245. try:
  246. os.chdir(tmpdir)
  247. cfg = load_config()
  248. finally:
  249. os.chdir(old_cwd)
  250. assert cfg.whisper.language_strategy == "static"
  251. def test_language_strategy_sticky_is_parsed():
  252. """language_strategy 'sticky' is parsed correctly."""
  253. config_toml = """
  254. [whisper]
  255. language_strategy = "sticky"
  256. """
  257. with tempfile.TemporaryDirectory() as tmpdir:
  258. config_path = Path(tmpdir) / "config.toml"
  259. config_path.write_text(config_toml)
  260. cfg = load_config(str(config_path))
  261. assert cfg.whisper.language_strategy == "sticky"
  262. def test_unknown_language_strategy_falls_back_to_static(caplog):
  263. """An unknown language_strategy value falls back to 'static' with a warning."""
  264. config_toml = """
  265. [whisper]
  266. language_strategy = "bogus"
  267. """
  268. with tempfile.TemporaryDirectory() as tmpdir:
  269. config_path = Path(tmpdir) / "config.toml"
  270. config_path.write_text(config_toml)
  271. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  272. cfg = load_config(str(config_path))
  273. assert cfg.whisper.language_strategy == "static"
  274. assert "Unknown language_strategy" in caplog.text
  275. assert "bogus" in caplog.text
  276. def test_sticky_redetect_logprob_defaults_to_minus_0_8():
  277. """sticky_redetect_logprob defaults to -0.8."""
  278. with tempfile.TemporaryDirectory() as tmpdir:
  279. import os
  280. old_cwd = os.getcwd()
  281. try:
  282. os.chdir(tmpdir)
  283. cfg = load_config()
  284. finally:
  285. os.chdir(old_cwd)
  286. assert cfg.whisper.sticky_redetect_logprob == -0.8
  287. def test_sticky_redetect_logprob_is_parsed():
  288. """sticky_redetect_logprob is parsed from TOML."""
  289. config_toml = """
  290. [whisper]
  291. sticky_redetect_logprob = -0.5
  292. """
  293. with tempfile.TemporaryDirectory() as tmpdir:
  294. config_path = Path(tmpdir) / "config.toml"
  295. config_path.write_text(config_toml)
  296. cfg = load_config(str(config_path))
  297. assert cfg.whisper.sticky_redetect_logprob == -0.5
  298. def test_llm_context_and_compaction_defaults():
  299. with tempfile.TemporaryDirectory() as tmpdir:
  300. import os
  301. old_cwd = os.getcwd()
  302. try:
  303. os.chdir(tmpdir)
  304. cfg = load_config()
  305. finally:
  306. os.chdir(old_cwd)
  307. assert cfg.llm.max_context_tokens == 131072
  308. assert cfg.llm.compaction_safety_margin == 256
  309. assert cfg.llm.compaction_enabled is True
  310. assert cfg.llm.compaction_trigger_ratio == 0.8
  311. assert cfg.llm.compaction_keep_turns == 6
  312. assert cfg.llm.compaction_max_tokens == 512
  313. assert cfg.llm.compaction_timeout_s == 30.0
  314. assert cfg.llm.compaction_input_tokens == 8192
  315. assert cfg.llm.inference_queue_size == 8
  316. assert cfg.llm.prompt_budget == 131072 - 1024 - 256
  317. assert not hasattr(cfg.llm, "max_prompt_tokens")
  318. def test_llm_context_and_compaction_fields_parsed():
  319. config_toml = """
  320. [llm]
  321. max_context_tokens = 8192
  322. max_tokens = 2048
  323. compaction_safety_margin = 128
  324. compaction_enabled = false
  325. compaction_trigger_ratio = 0.5
  326. compaction_keep_turns = 2
  327. compaction_max_tokens = 256
  328. compaction_timeout_s = 12.5
  329. compaction_input_tokens = 4096
  330. inference_queue_size = 3
  331. """
  332. with tempfile.TemporaryDirectory() as tmpdir:
  333. config_path = Path(tmpdir) / "config.toml"
  334. config_path.write_text(config_toml)
  335. cfg = load_config(str(config_path))
  336. assert cfg.llm.max_context_tokens == 8192
  337. assert cfg.llm.compaction_safety_margin == 128
  338. assert cfg.llm.compaction_enabled is False
  339. assert cfg.llm.compaction_trigger_ratio == 0.5
  340. assert cfg.llm.compaction_keep_turns == 2
  341. assert cfg.llm.compaction_max_tokens == 256
  342. assert cfg.llm.compaction_timeout_s == 12.5
  343. assert cfg.llm.compaction_input_tokens == 4096
  344. assert cfg.llm.inference_queue_size == 3
  345. assert cfg.llm.prompt_budget == 8192 - 2048 - 128
  346. def test_max_prompt_tokens_is_deprecated_and_ignored(caplog):
  347. config_toml = """
  348. [llm]
  349. max_prompt_tokens = 50
  350. """
  351. with tempfile.TemporaryDirectory() as tmpdir:
  352. config_path = Path(tmpdir) / "config.toml"
  353. config_path.write_text(config_toml)
  354. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  355. cfg = load_config(str(config_path))
  356. assert "max_prompt_tokens" in caplog.text
  357. assert "deprecated" in caplog.text
  358. assert cfg.llm.prompt_budget == 131072 - 1024 - 256
  359. def test_backend_defaults_to_faster_whisper():
  360. """backend defaults to 'faster-whisper'."""
  361. with tempfile.TemporaryDirectory() as tmpdir:
  362. import os
  363. old_cwd = os.getcwd()
  364. try:
  365. os.chdir(tmpdir)
  366. cfg = load_config()
  367. finally:
  368. os.chdir(old_cwd)
  369. assert cfg.whisper.backend == "faster-whisper"
  370. def test_backend_pywhispercpp_is_parsed():
  371. """backend 'pywhispercpp' is parsed from TOML."""
  372. config_toml = """
  373. [whisper]
  374. backend = "pywhispercpp"
  375. """
  376. with tempfile.TemporaryDirectory() as tmpdir:
  377. config_path = Path(tmpdir) / "config.toml"
  378. config_path.write_text(config_toml)
  379. cfg = load_config(str(config_path))
  380. assert cfg.whisper.backend == "pywhispercpp"
  381. def test_backend_is_normalized():
  382. """Backend names are normalized: stripped, lowercased, underscores to dashes."""
  383. config_toml = """
  384. [whisper]
  385. backend = " Faster_Whisper "
  386. """
  387. with tempfile.TemporaryDirectory() as tmpdir:
  388. config_path = Path(tmpdir) / "config.toml"
  389. config_path.write_text(config_toml)
  390. cfg = load_config(str(config_path))
  391. assert cfg.whisper.backend == "faster-whisper"
  392. def test_unknown_backend_falls_back_to_faster_whisper(caplog):
  393. """An unknown backend value falls back to 'faster-whisper' with a warning."""
  394. config_toml = """
  395. [whisper]
  396. backend = "bogus"
  397. """
  398. with tempfile.TemporaryDirectory() as tmpdir:
  399. config_path = Path(tmpdir) / "config.toml"
  400. config_path.write_text(config_toml)
  401. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  402. cfg = load_config(str(config_path))
  403. assert cfg.whisper.backend == "faster-whisper"
  404. assert "Unknown whisper backend" in caplog.text
  405. assert "bogus" in caplog.text
  406. def test_triage_defaults():
  407. """The [triage] section defaults match the plan's surviving-key list."""
  408. with tempfile.TemporaryDirectory() as tmpdir:
  409. import os
  410. old_cwd = os.getcwd()
  411. try:
  412. os.chdir(tmpdir)
  413. cfg = load_config()
  414. finally:
  415. os.chdir(old_cwd)
  416. assert cfg.triage.enabled is False
  417. assert cfg.triage.questions_file == "questions.json"
  418. assert cfg.triage.window == 500
  419. assert cfg.triage.state_char_budget == 3000
  420. assert cfg.triage.predict_max_tokens == 1024
  421. assert cfg.triage.confidence_field == "answer_confidence"
  422. assert cfg.triage.escalate_threshold == 0.6
  423. assert cfg.triage.choice_min_confidence == 0.85
  424. assert cfg.triage.extract_enabled is True
  425. assert cfg.triage.extract_max_tokens == 1536
  426. assert cfg.triage.extract_timeout_s == 30.0
  427. assert cfg.triage.brief_slot_id is None
  428. assert cfg.triage.brief_gate_enabled is True
  429. assert cfg.triage.brief_gate_threshold == 0.5
  430. assert cfg.triage.brief_max_chars == 4000
  431. assert cfg.triage.brief_pending_char_budget == 3000
  432. assert cfg.triage.device == ""
  433. assert cfg.triage.preload == ["multilingual"]
  434. assert cfg.triage.default_checkpoint == "multilingual"
  435. def test_triage_config_fields_parsed():
  436. """Every surviving [triage] key is applied from TOML."""
  437. config_toml = """
  438. [triage]
  439. enabled = true
  440. questions_file = "custom_questions.json"
  441. window = 4
  442. state_char_budget = 800
  443. predict_max_tokens = 768
  444. confidence_field = "confidence"
  445. escalate_threshold = 0.7
  446. choice_min_confidence = 0.9
  447. extract_enabled = false
  448. extract_max_tokens = 256
  449. extract_timeout_s = 12.5
  450. device = "cuda"
  451. preload = ["multilingual", "english"]
  452. default_checkpoint = "english"
  453. """
  454. with tempfile.TemporaryDirectory() as tmpdir:
  455. config_path = Path(tmpdir) / "config.toml"
  456. config_path.write_text(config_toml)
  457. cfg = load_config(str(config_path))
  458. assert cfg.triage.enabled is True
  459. assert cfg.triage.questions_file == "custom_questions.json"
  460. assert cfg.triage.window == 4
  461. assert cfg.triage.state_char_budget == 800
  462. assert cfg.triage.predict_max_tokens == 768
  463. assert cfg.triage.confidence_field == "confidence"
  464. assert cfg.triage.escalate_threshold == 0.7
  465. assert cfg.triage.choice_min_confidence == 0.9
  466. assert cfg.triage.extract_enabled is False
  467. assert cfg.triage.extract_max_tokens == 256
  468. assert cfg.triage.extract_timeout_s == 12.5
  469. assert cfg.triage.device == "cuda"
  470. assert cfg.triage.preload == ["multilingual", "english"]
  471. assert cfg.triage.default_checkpoint == "english"
  472. def test_llm_slot_and_brief_prompt_defaults():
  473. """Slots default to None; the brief prompts match the plan verbatim."""
  474. with tempfile.TemporaryDirectory() as tmpdir:
  475. import os
  476. old_cwd = os.getcwd()
  477. try:
  478. os.chdir(tmpdir)
  479. cfg = load_config()
  480. finally:
  481. os.chdir(old_cwd)
  482. assert cfg.llm.chat_slot_id is None
  483. assert cfg.llm.command_slot_id is None
  484. assert cfg.llm.triage_system_prompt == (
  485. "You maintain a living brief of a business conversation. Given the current brief "
  486. "(your previous reply, if any) and a new transcript segment, return the UPDATED "
  487. "brief as plain text only, with no markdown fences and no JSON. Preserve prior "
  488. "content unless the new segment contradicts it; merge duplicates; prune stale "
  489. "items; keep owners, responsibilities, and deadlines explicit. Use short sections: "
  490. "Opportunities, Blockers/Risks, Responsibilities, Decisions, Action Items, "
  491. "Deadlines, Open Questions. Output the brief text directly; never emit tool calls, "
  492. "function-call syntax, or special wrapper tokens."
  493. )
  494. assert cfg.llm.brief_compaction_system_prompt == (
  495. "You compress a triage briefing conversation. Preserve every opportunity, blocker, "
  496. "owner, responsibility, decision, deadline, and open question; drop pleasantries. "
  497. "Output only the summary."
  498. )
  499. def test_absent_slot_ids_stay_none():
  500. """Present sections with the slot keys absent leave every slot id None."""
  501. config_toml = """
  502. [llm]
  503. temperature = 0.5
  504. [triage]
  505. window = 12
  506. """
  507. with tempfile.TemporaryDirectory() as tmpdir:
  508. config_path = Path(tmpdir) / "config.toml"
  509. config_path.write_text(config_toml)
  510. cfg = load_config(str(config_path))
  511. assert cfg.llm.chat_slot_id is None
  512. assert cfg.llm.command_slot_id is None
  513. assert cfg.triage.brief_slot_id is None
  514. def test_slot_id_zero_is_preserved():
  515. """A slot id of 0 is valid and must load as 0, not None."""
  516. config_toml = """
  517. [llm]
  518. chat_slot_id = 0
  519. command_slot_id = 0
  520. [triage]
  521. brief_slot_id = 1
  522. """
  523. with tempfile.TemporaryDirectory() as tmpdir:
  524. config_path = Path(tmpdir) / "config.toml"
  525. config_path.write_text(config_toml)
  526. cfg = load_config(str(config_path))
  527. assert cfg.llm.chat_slot_id == 0
  528. assert cfg.llm.command_slot_id == 0
  529. assert cfg.triage.brief_slot_id == 1
  530. def test_non_numeric_slot_degrades_to_none():
  531. """A non-numeric slot value degrades to None without raising."""
  532. config_toml = """
  533. [llm]
  534. chat_slot_id = "abc"
  535. [triage]
  536. brief_slot_id = "nope"
  537. """
  538. with tempfile.TemporaryDirectory() as tmpdir:
  539. config_path = Path(tmpdir) / "config.toml"
  540. config_path.write_text(config_toml)
  541. cfg = load_config(str(config_path))
  542. assert cfg.llm.chat_slot_id is None
  543. assert cfg.triage.brief_slot_id is None
  544. def test_triage_brief_keys_are_parsed():
  545. """Every new [triage] brief key is applied from TOML."""
  546. config_toml = """
  547. [triage]
  548. brief_slot_id = 7
  549. brief_gate_enabled = false
  550. brief_gate_threshold = 0.8
  551. brief_max_chars = 1234
  552. brief_pending_char_budget = 777
  553. """
  554. with tempfile.TemporaryDirectory() as tmpdir:
  555. config_path = Path(tmpdir) / "config.toml"
  556. config_path.write_text(config_toml)
  557. cfg = load_config(str(config_path))
  558. assert cfg.triage.brief_slot_id == 7
  559. assert cfg.triage.brief_gate_enabled is False
  560. assert cfg.triage.brief_gate_threshold == 0.8
  561. assert cfg.triage.brief_max_chars == 1234
  562. assert cfg.triage.brief_pending_char_budget == 777
  563. def test_brief_defaults():
  564. """The [brief] section defaults to no on-disk writing."""
  565. with tempfile.TemporaryDirectory() as tmpdir:
  566. import os
  567. old_cwd = os.getcwd()
  568. try:
  569. os.chdir(tmpdir)
  570. cfg = load_config()
  571. finally:
  572. os.chdir(old_cwd)
  573. assert cfg.brief.write is False
  574. assert cfg.brief.dir == "brief"
  575. assert cfg.brief.filename == "brief.md"
  576. def test_brief_config_fields_are_parsed():
  577. """Every [brief] key is applied from TOML."""
  578. config_toml = """
  579. [brief]
  580. write = true
  581. dir = "briefing"
  582. filename = "live.md"
  583. """
  584. with tempfile.TemporaryDirectory() as tmpdir:
  585. config_path = Path(tmpdir) / "config.toml"
  586. config_path.write_text(config_toml)
  587. cfg = load_config(str(config_path))
  588. assert cfg.brief.write is True
  589. assert cfg.brief.dir == "briefing"
  590. assert cfg.brief.filename == "live.md"
  591. def test_brief_write_false_is_parsed(caplog):
  592. """An explicit [brief] write=false loads as False and is not an unknown section."""
  593. config_toml = """
  594. [brief]
  595. write = false
  596. """
  597. with tempfile.TemporaryDirectory() as tmpdir:
  598. config_path = Path(tmpdir) / "config.toml"
  599. config_path.write_text(config_toml)
  600. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  601. cfg = load_config(str(config_path))
  602. assert cfg.brief.write is False
  603. assert "brief" not in caplog.text
  604. def test_triage_dump_io_defaults_to_false():
  605. """[triage].dump_io is opt-in: False unless explicitly enabled."""
  606. with tempfile.TemporaryDirectory() as tmpdir:
  607. import os
  608. old_cwd = os.getcwd()
  609. try:
  610. os.chdir(tmpdir)
  611. cfg = load_config()
  612. finally:
  613. os.chdir(old_cwd)
  614. assert cfg.triage.dump_io is False
  615. def test_triage_dump_io_is_parsed():
  616. """[triage].dump_io = true is applied from TOML."""
  617. config_toml = """
  618. [triage]
  619. dump_io = true
  620. """
  621. with tempfile.TemporaryDirectory() as tmpdir:
  622. config_path = Path(tmpdir) / "config.toml"
  623. config_path.write_text(config_toml)
  624. cfg = load_config(str(config_path))
  625. assert cfg.triage.dump_io is True
  626. def test_unknown_triag_section_warns_and_keeps_defaults(caplog):
  627. """A typo'd [triag] section must warn and never raise; triage keeps defaults."""
  628. config_toml = """
  629. [triag]
  630. enabled = true
  631. window = 3
  632. """
  633. with tempfile.TemporaryDirectory() as tmpdir:
  634. config_path = Path(tmpdir) / "config.toml"
  635. config_path.write_text(config_toml)
  636. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  637. cfg = load_config(str(config_path))
  638. assert "triag" in caplog.text
  639. assert cfg.triage.enabled is False
  640. assert cfg.triage.window == 500
  641. def test_triage_config_has_no_struck_keys():
  642. """The v5 keys are deliberately gone from TriageConfig (superseded/removed)."""
  643. from stt_daemon.config import TriageConfig
  644. struck = {"socket", "model", "predict_timeout_s", "checkpoint", "subfolder", "max_len"}
  645. for name in struck:
  646. assert not hasattr(TriageConfig, name), f"TriageConfig must not expose {name!r}"
  647. def test_llm_triage_system_prompt_is_parsed():
  648. """[llm].triage_system_prompt overrides the JSON-only extractor default."""
  649. config_toml = """
  650. [llm]
  651. triage_system_prompt = "ONLY JSON, nothing else."
  652. """
  653. with tempfile.TemporaryDirectory() as tmpdir:
  654. config_path = Path(tmpdir) / "config.toml"
  655. config_path.write_text(config_toml)
  656. cfg = load_config(str(config_path))
  657. assert cfg.llm.triage_system_prompt == "ONLY JSON, nothing else."
  658. def test_no_stt_laya_script():
  659. """Triage is in-process only: no stt-laya console script may ever be added."""
  660. with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
  661. data = tomllib.load(fh)
  662. scripts = data["project"]["scripts"]
  663. assert set(scripts) == {"stt-daemon", "stt-assistant", "stt-ctl", "stt-tui"}
  664. assert not any("laya" in name for name in scripts)
  665. def test_layasvc_module_not_importable():
  666. """The retired external laya service package must not exist (in-process lane)."""
  667. assert importlib.util.find_spec("stt_daemon.layasvc") is None
  668. def test_triage_extra_pins_laya_revision():
  669. """The triage extra pins laya to the exact verified upstream revision."""
  670. with (REPO_ROOT / "pyproject.toml").open("rb") as fh:
  671. data = tomllib.load(fh)
  672. assert data["project"]["optional-dependencies"]["triage"] == ["laya"]
  673. source = data["tool"]["uv"]["sources"]["laya"]
  674. assert source["git"] == "https://github.com/NandhaKishorM/laya.git"
  675. assert source["rev"] == LAYA_REV
  676. def test_questions_json_gitignored():
  677. """The user's private questions.json is never committed (like config.toml)."""
  678. lines = (REPO_ROOT / ".gitignore").read_text().splitlines()
  679. assert "questions.json" in lines
  680. def test_speaker_defaults():
  681. """The [speaker] section defaults match the plan's 14-key list."""
  682. with tempfile.TemporaryDirectory() as tmpdir:
  683. import os
  684. old_cwd = os.getcwd()
  685. try:
  686. os.chdir(tmpdir)
  687. cfg = load_config()
  688. finally:
  689. os.chdir(old_cwd)
  690. assert cfg.speaker.enabled is False
  691. assert cfg.speaker.model_path is None
  692. assert cfg.speaker.provider == "cpu"
  693. assert cfg.speaker.threshold_id == 0.60
  694. assert cfg.speaker.threshold_new == 0.45
  695. assert cfg.speaker.min_enroll_s == 2.0
  696. assert cfg.speaker.rms_floor == 0.0
  697. assert cfg.speaker.margin == 0.04
  698. assert cfg.speaker.mint_cooldown_s == 60.0
  699. assert cfg.speaker.confirm_s == 12.0
  700. assert cfg.speaker.max_prototypes == 8
  701. assert cfg.speaker.provisional_ttl_h == 24.0
  702. assert cfg.speaker.store_dir == "speakers"
  703. assert cfg.speaker.join_timeout_s == 2.0
  704. def test_speaker_config_fields_parsed():
  705. """Every [speaker] key is applied from TOML."""
  706. config_toml = """
  707. [speaker]
  708. enabled = true
  709. model_path = "models/nemo_en_titanet_small.onnx"
  710. provider = "cuda"
  711. threshold_id = 0.7
  712. threshold_new = 0.5
  713. min_enroll_s = 3.0
  714. rms_floor = 0.01
  715. margin = 0.05
  716. mint_cooldown_s = 30.0
  717. confirm_s = 8.0
  718. max_prototypes = 4
  719. provisional_ttl_h = 12.0
  720. store_dir = "voiceprints"
  721. join_timeout_s = 1.5
  722. """
  723. with tempfile.TemporaryDirectory() as tmpdir:
  724. config_path = Path(tmpdir) / "config.toml"
  725. config_path.write_text(config_toml)
  726. cfg = load_config(str(config_path))
  727. assert cfg.speaker.enabled is True
  728. assert cfg.speaker.model_path == "models/nemo_en_titanet_small.onnx"
  729. assert cfg.speaker.provider == "cuda"
  730. assert cfg.speaker.threshold_id == 0.7
  731. assert cfg.speaker.threshold_new == 0.5
  732. assert cfg.speaker.min_enroll_s == 3.0
  733. assert cfg.speaker.rms_floor == 0.01
  734. assert cfg.speaker.margin == 0.05
  735. assert cfg.speaker.mint_cooldown_s == 30.0
  736. assert cfg.speaker.confirm_s == 8.0
  737. assert cfg.speaker.max_prototypes == 4
  738. assert cfg.speaker.provisional_ttl_h == 12.0
  739. assert cfg.speaker.store_dir == "voiceprints"
  740. assert cfg.speaker.join_timeout_s == 1.5
  741. def test_unknown_speaker_key_is_ignored():
  742. """An unknown key inside [speaker] is silently ignored (per-field .get)."""
  743. config_toml = """
  744. [speaker]
  745. enabled = true
  746. bogus_key = 42
  747. """
  748. with tempfile.TemporaryDirectory() as tmpdir:
  749. config_path = Path(tmpdir) / "config.toml"
  750. config_path.write_text(config_toml)
  751. cfg = load_config(str(config_path))
  752. assert cfg.speaker.enabled is True
  753. assert not hasattr(cfg.speaker, "bogus_key")
  754. assert cfg.speaker.provider == "cpu"
  755. def test_unknown_speaker_provider_falls_back_to_cpu(caplog):
  756. """An unknown speaker provider falls back to 'cpu' with a warning."""
  757. config_toml = """
  758. [speaker]
  759. provider = "bogus"
  760. """
  761. with tempfile.TemporaryDirectory() as tmpdir:
  762. config_path = Path(tmpdir) / "config.toml"
  763. config_path.write_text(config_toml)
  764. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  765. cfg = load_config(str(config_path))
  766. assert cfg.speaker.provider == "cpu"
  767. assert "Unknown speaker provider" in caplog.text
  768. assert "bogus" in caplog.text
  769. def test_debug_defaults():
  770. """The [debug] section defaults to disabled with signal-log defaults."""
  771. with tempfile.TemporaryDirectory() as tmpdir:
  772. import os
  773. old_cwd = os.getcwd()
  774. try:
  775. os.chdir(tmpdir)
  776. cfg = load_config()
  777. finally:
  778. os.chdir(old_cwd)
  779. assert cfg.debug.enabled is False
  780. assert cfg.debug.dir == "debug"
  781. assert cfg.debug.heartbeat is True
  782. assert cfg.debug.max_bytes_per_day == 50000000
  783. def test_debug_config_fields_are_parsed():
  784. """Every [debug] key is applied from TOML."""
  785. config_toml = """
  786. [debug]
  787. enabled = true
  788. dir = "dbg"
  789. heartbeat = false
  790. max_bytes_per_day = 1000
  791. """
  792. with tempfile.TemporaryDirectory() as tmpdir:
  793. config_path = Path(tmpdir) / "config.toml"
  794. config_path.write_text(config_toml)
  795. cfg = load_config(str(config_path))
  796. assert cfg.debug.enabled is True
  797. assert cfg.debug.dir == "dbg"
  798. assert cfg.debug.heartbeat is False
  799. assert cfg.debug.max_bytes_per_day == 1000
  800. def test_debug_section_not_warned(caplog):
  801. """An explicit [debug] section loads without an unknown-section warning."""
  802. config_toml = """
  803. [debug]
  804. enabled = false
  805. """
  806. with tempfile.TemporaryDirectory() as tmpdir:
  807. config_path = Path(tmpdir) / "config.toml"
  808. config_path.write_text(config_toml)
  809. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  810. cfg = load_config(str(config_path))
  811. assert cfg.debug.enabled is False
  812. assert "debug" not in caplog.text
  813. def test_unknown_debug_key_is_ignored():
  814. """An unknown key inside [debug] is silently ignored (per-field .get)."""
  815. config_toml = """
  816. [debug]
  817. enabled = true
  818. bogus_key = 42
  819. """
  820. with tempfile.TemporaryDirectory() as tmpdir:
  821. config_path = Path(tmpdir) / "config.toml"
  822. config_path.write_text(config_toml)
  823. cfg = load_config(str(config_path))
  824. assert cfg.debug.enabled is True
  825. assert not hasattr(cfg.debug, "bogus_key")
  826. assert cfg.debug.dir == "debug"
  827. def test_unknown_top_level_still_warns(caplog):
  828. """An unknown top-level section still warns after [debug] exists."""
  829. config_toml = """
  830. [debugg]
  831. enabled = true
  832. """
  833. with tempfile.TemporaryDirectory() as tmpdir:
  834. config_path = Path(tmpdir) / "config.toml"
  835. config_path.write_text(config_toml)
  836. with caplog.at_level("WARNING", logger="stt_daemon.config"):
  837. cfg = load_config(str(config_path))
  838. assert "debugg" in caplog.text
  839. assert cfg.debug.enabled is False