"""Tests for voice TSV + emit_voice_event (path + TSV line).""" from __future__ import annotations import tempfile from pathlib import Path from app.config import Settings from app.services.voice_file_log import ( append_voice_tsv_line, emit_voice_event, init_voice_log_file, resolved_voice_log_path, ) def test_resolved_voice_log_path_replaces_surgery_id() -> None: s = Settings() s.voice_file_log_path = "logs/voice_{surgery_id}.txt" p = resolved_voice_log_path("123456", s) assert p.name == "voice_123456.txt" assert "logs" in str(p) def test_init_and_append_tsv() -> None: with tempfile.TemporaryDirectory() as d: base = Path(d) s = Settings() s.voice_file_log_enabled = True s.voice_file_log_path = str((base / "v_{surgery_id}.txt").resolve()) init_voice_log_file("999999", s) p = resolved_voice_log_path("999999", s) assert p.exists() h = p.read_text(encoding="utf-8") assert "来源" in h and "confirmation_id" in h line = "ts\ttest\trecognized\tcid1\t同\t品\tfalse\t\tk.wav\n" append_voice_tsv_line("999999", line, s) assert p.read_text(encoding="utf-8").endswith(line) def test_emit_voice_event_writes_when_enabled() -> None: s = Settings() s.voice_file_log_enabled = True with tempfile.TemporaryDirectory() as d: s.voice_file_log_path = str((Path(d) / "v_{surgery_id}.txt").resolve()) init_voice_log_file("111111", s) emit_voice_event( s, surgery_id="111111", source="wav", status="recognized", confirmation_id="c1", asr_text="纱布", resolved_label="纱布", rejected=False, audio_object_key="k.wav", ) p = resolved_voice_log_path("111111", s) body = p.read_text(encoding="utf-8") assert "纱布" in body assert "recognized" in body assert "k.wav" in body