"""Voice worker end to end over a real local WebSocket, with fake STT/VAD/TTS engines. The fake VAD reads speech probability off the amplitude and the fake transcriber names a clip by its "level": each speaker sends a tone whose amplitude is the level (what the fake VAD reads) and whose pitch is level x 1000 Hz (what the fake transcriber reads, since the worker evens out loudness). """ from __future__ import annotations import asyncio import json import time import numpy as np import pytest from voice_worker.protocol import decode_tts_frame, encode_audio_frame from voice_worker.server import CLOSE_BAD_HELLO, CLOSE_BAD_TOKEN, VoiceWorker websockets = pytest.importorskip("websockets") from websockets.asyncio.client import connect # noqa: E402 TOKEN = "test-token" class AmplitudeVad: def prob(self, window): return float(np.max(np.abs(window))) class LevelTranscriber: def __init__(self): self.calls = [] def transcribe(self, clips, opts): self.calls.append((opts, len(clips))) return [f"level {np.argmax(np.abs(np.fft.rfft(c))) * 16000 / len(c) / 1000:.1f}" for c in clips] class FakeTts: """10 samples per character; optional delay per sentence; can fail on a marker.""" def __init__(self, delay: float = 0.0): self.delay = delay self.texts = [] self.speed = 1.5 self.speeds = [] def synthesize(self, text, rate): if self.delay: time.sleep(self.delay) if "FAIL" in text: raise RuntimeError("engine broke") self.texts.append(text) self.speeds.append(self.speed) return np.full(len(text) * 10, 1000, dtype=np.int16) def pcm(level: float, ms: int, rate=16000, channels=1) -> bytes: n = rate * ms // 1000 # 20 ms frames hold whole cycles at these pitches, so frames join without a click. x = level * np.sin(2 * np.pi * level * 1000 * np.arange(n) / rate) return (np.repeat(x, channels) * 32767).astype(" MAX_SPEAKERS and all(m["text"] == "level 0.9" for m in finals) def _index(root): import pathlib days = [d for d in pathlib.Path(root).iterdir() if d.is_dir()] assert len(days) == 1 lines = [json.loads(x) for x in (days[0] / "index.jsonl").read_text(encoding="utf-8").splitlines()] return days[0], lines @pytest.mark.unit def test_with_recordings_on_each_utterance_and_spoken_answer_is_kept_as_a_wav_with_its_text(tmp_path): import wave from voice_worker.recordings import Recorder recorder = Recorder(tmp_path) async def body(url): async with connect(url) as ws: await hello(ws, stt={"partials": False}) await ws.send(json.dumps({"type": "member_join", "user": "alice", "name": "Alice"})) await stream(ws, "alice", 0.9, 600) await ws.send(json.dumps({"type": "speaking_end", "speaker": "alice"})) await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "final") await ws.send(json.dumps({"type": "tts", "id": "r1", "text": "오늘은 맑아요. 기온은 17.5도예요."})) await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done") # The app played both sentences to the end. await ws.send(json.dumps({"type": "tts_played", "id": "r1", "played": 2, "cancelled": False})) await asyncio.sleep(0.2) asyncio.run(with_worker(body, tts=FakeTts(), recorder=recorder)) recorder.close() day, lines = _index(tmp_path) said, spoke = lines assert (said["kind"], said["who"], said["text"]) == ("said", "alice", "level 0.9") assert (spoke["kind"], spoke["text"], spoke["cancelled"]) == ("spoke", "오늘은 맑아요. 기온은 17.5도예요.", False) with wave.open(str(day / said["file"])) as w: assert (w.getframerate(), w.getnchannels()) == (16000, 1) assert 500 <= w.getnframes() * 1000 // 16000 <= 900 with wave.open(str(day / spoke["file"])) as w: # Mono at the session's TTS rate; FakeTts makes 10 samples per character of each sentence. assert (w.getframerate(), w.getnchannels()) == (24000, 1) assert w.getnframes() == (len("오늘은 맑아요.") + len("기온은 17.5도예요.")) * 10 @pytest.mark.unit def test_without_a_recorder_nothing_is_written(tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) async def body(url): async with connect(url) as ws: await hello(ws, stt={"partials": False}) await stream(ws, "alice", 0.9, 600) await ws.send(json.dumps({"type": "speaking_end", "speaker": "alice"})) await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "final") asyncio.run(with_worker(body, tts=FakeTts())) assert list(tmp_path.iterdir()) == [] @pytest.mark.unit def test_recordings_older_than_the_keep_days_are_removed_at_start(tmp_path): from datetime import date, datetime, timedelta from voice_worker.recordings import KEEP_DAYS, Recorder today = date(2026, 11, 16) keep = [today - timedelta(days=i) for i in range(KEEP_DAYS)] old = today - timedelta(days=KEEP_DAYS) for d in keep + [old]: (tmp_path / d.isoformat()).mkdir() (tmp_path / "notes").mkdir() # not a day folder: left alone Recorder(tmp_path, now=datetime(2026, 11, 16, 12).timestamp()).close() assert sorted(p.name for p in tmp_path.iterdir()) == sorted([d.isoformat() for d in keep] + ["notes"]) @pytest.mark.unit def test_a_spoken_answer_is_kept_only_as_far_as_the_app_played_it(tmp_path): import wave from voice_worker.recordings import Recorder recorder = Recorder(tmp_path) async def body(url): async with connect(url) as ws: await hello(ws) # Made in full, then stopped while its first sentence played. await ws.send(json.dumps({"type": "tts", "id": "r1", "text": "오늘은 맑아요. 기온은 17.5도예요."})) await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done") # Stopped 2 ms into its first sentence (FakeTts: 10 samples per character, 24 kHz). await ws.send(json.dumps({"type": "tts_played", "id": "r1", "played": 1, "cancelled": True, "last_ms": 2})) # Stopped before any of it was heard: nothing to keep. await ws.send(json.dumps({"type": "tts", "id": "r2", "text": "두 번째 답이에요."})) await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done") await ws.send(json.dumps({"type": "tts_played", "id": "r2", "played": 0, "cancelled": True})) # Never reported as played (an older app): not kept as if it had been heard. await ws.send(json.dumps({"type": "tts", "id": "r3", "text": "세 번째 답이에요."})) await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done") await asyncio.sleep(0.2) asyncio.run(with_worker(body, tts=FakeTts(), recorder=recorder)) recorder.close() day, lines = _index(tmp_path) assert [(l["id"], l["cancelled"]) for l in lines] == [("r1", True)] with wave.open(str(day / lines[0]["file"])) as w: assert w.getnframes() == 24000 * 2 // 1000, "only the part of the sentence that was played" @pytest.mark.unit def test_old_recordings_are_also_removed_while_the_worker_keeps_running(tmp_path): from datetime import datetime, timedelta import numpy as np from voice_worker.recordings import KEEP_DAYS, Recorder start = datetime(2026, 11, 16, 12) r = Recorder(tmp_path, now=start.timestamp()) r.said(start.timestamp(), "1", "A", "vc", "첫날", np.zeros(1600)) later = start + timedelta(days=KEEP_DAYS) # the first recording of a day past the keep days r.said(later.timestamp(), "1", "A", "vc", "나중", np.zeros(1600)) r.close() assert sorted(p.name for p in tmp_path.iterdir()) == [later.date().isoformat()]