"""Tests for voice TSV + emit_voice_event (path + TSV line).""" from __future__ import annotations import tempfile from pathlib import Path import pytest from app.baked import pipeline as bp 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( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(bp, "VOICE_FILE_LOG_PATH", "logs/voice_{surgery_id}.txt") p = resolved_voice_log_path("123456") assert p.name == "voice_123456.txt" assert "logs" in str(p) def test_init_and_append_tsv(monkeypatch: pytest.MonkeyPatch) -> None: with tempfile.TemporaryDirectory() as d: base = Path(d) monkeypatch.setattr(bp, "VOICE_FILE_LOG_ENABLED", True) monkeypatch.setattr( bp, "VOICE_FILE_LOG_PATH", str((base / "v_{surgery_id}.txt").resolve()), ) init_voice_log_file("999999") p = resolved_voice_log_path("999999") 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) assert p.read_text(encoding="utf-8").endswith(line) def test_emit_voice_event_writes_when_enabled( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(bp, "VOICE_FILE_LOG_ENABLED", True) with tempfile.TemporaryDirectory() as d: monkeypatch.setattr( bp, "VOICE_FILE_LOG_PATH", str((Path(d) / "v_{surgery_id}.txt").resolve()), ) init_voice_log_file("111111") emit_voice_event( 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") body = p.read_text(encoding="utf-8") assert "纱布" in body assert "recognized" in body assert "k.wav" in body