import { once } from "node:events"; import { afterEach, describe, expect, it } from "vitest"; import { WebSocketServer, type WebSocket } from "ws"; import { decodeTtsFrame, encodeAudioFrame, KIND_TTS } from "../src/core/frames.js"; import { VoiceWorkerClient, type TtsDone } from "../src/core/workerClient.js"; const TOKEN = "t0ken"; const FMT = { rate: 48000, channels: 2 } as const; /** A stand-in voice worker: checks the hello and records what the client sends. */ async function fakeWorker(port = 0) { const wss = new WebSocketServer({ host: "127.0.0.1", port }); await once(wss, "listening"); const received: (Record | Buffer)[] = []; const sockets: WebSocket[] = []; wss.on("connection", (ws) => { sockets.push(ws); ws.on("message", (data, isBinary) => { if (isBinary) return void received.push(data as Buffer); const msg = JSON.parse(String(data)); received.push(msg); if (msg.type === "hello") { if (msg.token !== TOKEN) return ws.close(4401, "bad token"); ws.send(JSON.stringify({ type: "hello_ok", stt_ready: true, tts_ready: true, stt: msg.stt })); } }); }); const addr = wss.address() as { port: number }; return { wss, received, sockets, url: `ws://127.0.0.1:${addr.port}`, port: addr.port }; } const cleanups: (() => void | Promise)[] = []; afterEach(async () => { for (const c of cleanups.splice(0)) await c(); }); function client(url: string, extra: Partial[0]> = {}) { const c = new VoiceWorkerClient({ url, token: TOKEN, audio: FMT, tts: FMT, backoffMs: 20, maxBackoffMs: 50, ...extra }); cleanups.push(() => c.close()); return c; } const until = async (pred: () => boolean, ms = 3000) => { const end = Date.now() + ms; while (!pred()) { if (Date.now() > end) throw new Error("timed out"); await new Promise((r) => setTimeout(r, 10)); } }; describe("VoiceWorkerClient", () => { it("says hello with the token, formats and STT settings, then streams audio frames", async () => { const w = await fakeWorker(); cleanups.push(() => void w.wss.close()); const c = client(w.url, { stt: { hotwords: ["자비스"] } }); c.start(); const ok = await c.waitReady(2000); expect(ok.stt).toEqual({ hotwords: ["자비스"] }); expect(w.received[0]).toEqual({ type: "hello", token: TOKEN, audio: FMT, tts: FMT, stt: { hotwords: ["자비스"] } }); const pcm = Buffer.alloc(3840, 1); c.sendAudio("123", pcm); c.speakingEnd("123"); c.speakerLeave("123"); await until(() => w.received.length >= 4); expect(w.received[1]).toEqual(encodeAudioFrame("123", pcm)); expect(w.received.slice(2)).toEqual([{ type: "speaking_end", speaker: "123" }, { type: "speaker_leave", speaker: "123" }]); }); it("passes worker events on and only TTS audio of requests it opened", async () => { const w = await fakeWorker(); cleanups.push(() => void w.wss.close()); const c = client(w.url); c.start(); await c.waitReady(2000); const finals: unknown[] = []; const audio: { id: string; seq: number }[] = []; const done: TtsDone[] = []; c.on("final", (f) => finals.push(f)); c.on("tts_audio", (a) => audio.push({ id: a.id, seq: a.seq })); c.on("tts_done", (d) => done.push(d)); expect(c.tts("r1", "안녕하세요.")).toBe(true); await until(() => w.received.some((m) => !Buffer.isBuffer(m) && m.type === "tts")); const ws = w.sockets[0]!; const frame = (id: string, seq: number) => { const s = Buffer.alloc(4); s.writeUInt32LE(seq); return Buffer.concat([Buffer.from([KIND_TTS, id.length]), Buffer.from(id), s, Buffer.alloc(8)]); }; ws.send(frame("r1", 0)); ws.send(frame("zz", 0)); // not ours ws.send(JSON.stringify({ type: "final", speaker: "1", utt: 1, text: "네", end_reason: "silence", audio_ms: 900, wait_ms: 100, stt_ms: 80 })); ws.send(JSON.stringify({ type: "tts_done", id: "r1", chunks: 1, cancelled: false, failed: false })); await until(() => done.length === 1 && finals.length === 1); expect(audio).toEqual([{ id: "r1", seq: 0 }]); expect(decodeTtsFrame(frame("r1", 0))?.pcm.length).toBe(8); expect(done[0]).toEqual({ id: "r1", chunks: 1, cancelled: false, failed: false }); }); it("a refused TTS request (no engine) ends as failed", async () => { const w = await fakeWorker(); cleanups.push(() => void w.wss.close()); const c = client(w.url); c.start(); await c.waitReady(2000); const done: TtsDone[] = []; c.on("tts_done", (d) => done.push(d)); c.tts("r9", "x"); await until(() => w.received.length >= 2); w.sockets[0]!.send(JSON.stringify({ type: "error", code: "tts_unavailable", message: "no", id: "r9" })); await until(() => done.length === 1); expect(done[0]).toMatchObject({ id: "r9", failed: true }); }); it("reconnects after the worker restarts and fails the TTS that was in flight", async () => { let w = await fakeWorker(); const port = w.port; const c = client(w.url); let readies = 0; let disconnects = 0; c.on("ready", () => readies++); c.on("disconnected", () => disconnects++); const done: TtsDone[] = []; c.on("tts_done", (d) => done.push(d)); c.start(); await c.waitReady(2000); c.tts("r1", "길게 말하는 중"); await until(() => w.received.length >= 2); // The worker dies... for (const s of w.sockets) s.terminate(); await new Promise((r) => w.wss.close(() => r())); await until(() => disconnects === 1); expect(done).toEqual([{ id: "r1", chunks: 0, cancelled: false, failed: true }]); expect(c.isReady).toBe(false); // Audio while it is down is dropped, not queued. c.sendAudio("1", Buffer.alloc(4)); // ...and comes back on the same port. w = await fakeWorker(port); cleanups.push(() => void w.wss.close()); await until(() => readies === 2); expect(w.received[0]).toMatchObject({ type: "hello", token: TOKEN }); expect(w.received.filter(Buffer.isBuffer)).toEqual([]); }); it("settings changed with configure() are sent now and in every later hello", async () => { let w = await fakeWorker(); const port = w.port; const c = client(w.url, { stt: { language: "ko" } }); c.start(); await c.waitReady(2000); c.configure({ partials: false }); await until(() => w.received.length >= 2); expect(w.received[1]).toEqual({ type: "config", partials: false }); for (const s of w.sockets) s.terminate(); await new Promise((r) => w.wss.close(() => r())); await until(() => !c.isReady); w = await fakeWorker(port); cleanups.push(() => void w.wss.close()); await until(() => c.isReady); expect(w.received[0]).toMatchObject({ stt: { language: "ko", partials: false } }); }); it("tts while disconnected fails at once instead of waiting forever", async () => { const c = client("ws://127.0.0.1:1"); const done: TtsDone[] = []; c.on("tts_done", (d) => done.push(d)); expect(c.tts("r1", "x")).toBe(false); await until(() => done.length === 1); expect(done[0]).toMatchObject({ id: "r1", failed: true }); }); });