import { describe, expect, it } from "vitest"; import { decodeTtsFrame, encodeAudioFrame, KIND_AUDIO, KIND_TTS } from "../src/core/frames.js"; describe("binary frames (voice_worker/protocol.py layout)", () => { it("audio frame is kind, id length, UTF-8 id, then PCM", () => { const pcm = Buffer.from([1, 0, 2, 0]); const f = encodeAudioFrame("유저", pcm); const id = Buffer.from("유저", "utf8"); expect([...f]).toEqual([KIND_AUDIO, id.length, ...id, 1, 0, 2, 0]); }); it("rejects ids that do not fit in a byte", () => { expect(() => encodeAudioFrame("", Buffer.alloc(2))).toThrow(); expect(() => encodeAudioFrame("x".repeat(256), Buffer.alloc(2))).toThrow(); }); it("decodes a TTS frame and refuses truncated ones", () => { const id = Buffer.from("r1"); const seq = Buffer.alloc(4); seq.writeUInt32LE(7); const frame = Buffer.concat([Buffer.from([KIND_TTS, id.length]), id, seq, Buffer.from([9, 9])]); expect(decodeTtsFrame(frame)).toEqual({ id: "r1", seq: 7, pcm: Buffer.from([9, 9]) }); expect(decodeTtsFrame(frame.subarray(0, 5))).toBeNull(); expect(decodeTtsFrame(Buffer.from([KIND_AUDIO, 1, 65, 0, 0, 0, 0]))).toBeNull(); }); });