import assert from "node:assert/strict"; import test from "node:test"; import { DownlinkBatcher } from "./downlink-batcher.js"; import { decodeVoiceBatch } from "../shared/voice-audio.js"; function fixture(frameMs: 20 | 40 = 20) { let now = 0; const packets: { packet: Uint8Array; entries: number; sentAt: number }[] = []; const batcher = new DownlinkBatcher({ frameMs, now: () => now, subscribe: () => () => {}, send: (packet, entries, sentAt) => { packets.push({ packet, entries, sentAt }); }, }); return { batcher, packets, setNow: (value: number) => { now = value; } }; } test("batcher coalesces one tick of speakers into a single v3 packet", () => { const f = fixture(); f.batcher.push(4, 2, Uint8Array.from([1, 2, 3])); f.batcher.push(5, 3, Uint8Array.from([4, 5])); f.batcher.push(4, 4, Uint8Array.from([6])); f.batcher.flush(0); assert.equal(f.packets.length, 1); const decoded = decodeVoiceBatch(f.packets[0]!.packet); assert.ok(decoded); assert.equal(decoded.audioStartMs, 0); assert.deepEqual(decoded.entries.map(entry => entry.clientId), [2, 3, 4]); assert.deepEqual(decoded.entries.map(entry => entry.seq), [1, 1, 1]); assert.equal(f.batcher.getStats().messages, 1); assert.equal(f.batcher.getStats().entries, 3); assert.equal(f.batcher.getStats().maxEntries, 3); }); test("batcher caps entries and bytes per packet while keeping every frame", () => { const f = fixture(); for (let id = 1; id <= 40; id++) f.batcher.push(4, id, new Uint8Array(100)); f.batcher.flush(0); assert.ok(f.packets.length >= 2); const decoded = f.packets.flatMap(entry => decodeVoiceBatch(entry.packet)?.entries ?? []); assert.equal(decoded.length, 40); assert.ok(Math.max(...f.packets.map(entry => entry.entries)) <= 32); assert.equal(f.batcher.getStats().droppedFrames, 0); }); test("stale frames and per-speaker sequence gaps survive a tunnel stall", () => { const f = fixture(); f.batcher.push(4, 2, Uint8Array.from([1])); f.setNow(150); f.batcher.flush(150); assert.equal(f.packets.length, 0); assert.equal(f.batcher.getStats().droppedFrames, 1); f.batcher.push(4, 2, Uint8Array.from([2])); f.setNow(160); f.batcher.flush(160); const decoded = decodeVoiceBatch(f.packets[0]!.packet); assert.equal(decoded?.entries[0]?.seq, 2, "a dropped frame leaves a detectable gap"); }); test("40 ms batching waits a full negotiated tick", () => { const f = fixture(40); f.batcher.push(4, 2, Uint8Array.from([1])); f.batcher.flush(0); const decoded = decodeVoiceBatch(f.packets[0]!.packet); assert.equal(decoded?.audioStartMs, 0); f.batcher.setFrameMs(20); f.setNow(20); f.batcher.push(4, 2, Uint8Array.from([2])); f.batcher.flush(20); assert.equal(decodeVoiceBatch(f.packets[1]!.packet)?.audioStartMs, 20); }); test("a late flush keeps one audio timestamp per missed tick", () => { const f = fixture(); f.batcher.push(4, 2, Uint8Array.from([1])); f.setNow(20); f.batcher.push(4, 2, Uint8Array.from([2])); // Simulate a 100 ms timer stall: both ticks are still inside the queue // horizon and must not collapse onto a single playback instant. f.setNow(100); f.batcher.flush(100); assert.equal(f.packets.length, 2); assert.deepEqual(f.packets.map(entry => decodeVoiceBatch(entry.packet)?.audioStartMs), [0, 20]); });