import assert from "node:assert/strict"; import test from "node:test"; import { decodeVoiceBatch, encodeVoiceBatch, VOICE_BATCH_HEADER_BYTES, VOICE_BATCH_TAG, VOICE_MAX_OPUS_FRAME_BYTES, } from "./voice-audio.js"; test("voice batch round-trips the audio clock and per-speaker entries", () => { const encoded = encodeVoiceBatch(123_456, [ { codec: 4, clientId: 2, seq: 7, data: Uint8Array.from([1, 2, 3]) }, { codec: 5, clientId: 65_535, seq: 255, data: Uint8Array.from([9]) }, ]); assert.ok(encoded); assert.equal(encoded[0], VOICE_BATCH_TAG); assert.equal(encoded[1], 2); assert.equal(encoded.length, VOICE_BATCH_HEADER_BYTES + (6 + 3) + (6 + 1)); const decoded = decodeVoiceBatch(encoded); assert.ok(decoded); assert.equal(decoded.audioStartMs, 123_456); assert.deepEqual(decoded.entries.map(({ codec, clientId, seq, data }) => ({ codec, clientId, seq, data: [...data] })), [ { codec: 4, clientId: 2, seq: 7, data: [1, 2, 3] }, { codec: 5, clientId: 65_535, seq: 255, data: [9] }, ]); }); test("voice batch rejects framing the decoder could not reproduce", () => { const good = encodeVoiceBatch(1, [{ codec: 4, clientId: 2, seq: 1, data: Uint8Array.from([1, 2, 3]) }])!; assert.equal(decodeVoiceBatch(Uint8Array.from([...good, 0])), null, "trailing byte"); assert.equal(decodeVoiceBatch(Uint8Array.from([0xf2, ...good.subarray(1)])), null, "wrong tag"); assert.equal(decodeVoiceBatch(Uint8Array.from([good[0]!, 0, ...good.subarray(2)])), null, "zero entries"); const truncated = good.slice(0, -1); assert.equal(decodeVoiceBatch(truncated), null, "truncated payload"); assert.equal(encodeVoiceBatch(1, []), null); assert.equal(encodeVoiceBatch(1, [{ codec: 4, clientId: 0, seq: 1, data: Uint8Array.from([1]) }]), null); assert.equal(encodeVoiceBatch(1, [{ codec: 4, clientId: 1, seq: 1, data: new Uint8Array(VOICE_MAX_OPUS_FRAME_BYTES + 1) }]), null); });