"""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 peak level, so each speaker's text tells which speaker's audio reached the model. """ 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.median(np.abs(c)):.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 = [] def synthesize(self, text, rate): if self.delay: time.sleep(self.delay) if "FAIL" in text: raise RuntimeError("engine broke") self.texts.append(text) 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 return (np.full(n * channels, level) * 32767).astype("