import { getClientAudioBuffersCollection, insertClientAudioBuffer, findClientAudioBuffersByCallSidAndRange, getMergedAudioByCallSid, getAudioChunksByCallSid, } from "../clientAudioBuffer.getters"; import { ObjectId } from "../../index"; import { createAudioBufferInput } from "../../../test-utils/factories"; describe("clientAudioBuffers getters", () => { it('should return the "clientAudioBuffers" collection', () => { const collection = getClientAudioBuffersCollection(); expect(collection.collectionName).toBe("clientAudioBuffers"); }); it("should insert a client audio buffer and return insertedId", async () => { const inputToCheck = createAudioBufferInput(); const result = await insertClientAudioBuffer(inputToCheck); expect(result.insertedId).toBeInstanceOf(ObjectId); const savedDoc = await getClientAudioBuffersCollection().findOne({ _id: result.insertedId, }); expect(savedDoc).toBeDefined(); expect(savedDoc?.createdAt).toBeInstanceOf(Date); expect(savedDoc?.expiresAt).toBeInstanceOf(Date); }); it("should return only buffers in the specified bucketStartMs range", async () => { const callSid = "CA1234567890abcdef1234567890abcdef"; await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 500 }), ); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 1000 }), ); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 3000 }), ); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 6000 }), ); const result = await findClientAudioBuffersByCallSidAndRange( callSid, 500, 3000, ); expect(result).toHaveLength(3); const bucketStartValues = result.map((doc) => doc.bucketStartMs); const bucketStartsSorted = bucketStartValues.sort((a, b) => a - b); expect(bucketStartsSorted).toEqual([500, 1000, 3000]); }); it("should return all buffers when no range is provided", async () => { const callSid = "CA1234567890abcdef1234567890abcdeg"; await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 500 }), ); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 1000 }), ); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 3000 }), ); const result = await findClientAudioBuffersByCallSidAndRange(callSid); expect(result).toHaveLength(3); const bucketStartValues = result.map((doc) => doc.bucketStartMs); const bucketStartsSorted = bucketStartValues.sort((a, b) => a - b); expect(bucketStartsSorted).toEqual([500, 1000, 3000]); }); it("should merge audio buffers by callSid in chronological order", async () => { const callSid = "CA1234567890abcdef1234567890abcdeh"; const firstBuffer = Buffer.from([1, 2]); const secondBuffer = Buffer.from([3]); const thirdBuffer = Buffer.from([4, 5]); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 1000, data: secondBuffer, }), ); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 500, data: firstBuffer, }), ); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 3000, data: thirdBuffer, }), ); const merged = await getMergedAudioByCallSid(callSid, 10); const expected = Buffer.concat([firstBuffer, secondBuffer, thirdBuffer]); expect(merged).toBeInstanceOf(Buffer); expect(merged.length).toBe(expected.length); expect(merged.equals(expected)).toBe(true); }); it("should return all audio chunks when less than or equal to limit buffers exist", async () => { const callSid = "CA1234567890abcdef1234567890abclt"; const limit = 30; const buffers: Buffer[] = []; for (let i = 0; i < limit; i++) { const buf = Buffer.from([i]); buffers.push(buf); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 1000 + i * 1000, data: buf, }), ); } const chunks = await getAudioChunksByCallSid(callSid, limit); expect(chunks).toHaveLength(buffers.length); expect(chunks.every((chunk) => chunk instanceof Buffer)).toBe(true); expect(Buffer.concat(chunks).equals(Buffer.concat(buffers))).toBe(true); }); it("should return only the last N audio chunks when more than limit buffers exist", async () => { const callSid = "CA1234567890abcdef1234567890abclm"; const limit = 30; const buffers: Buffer[] = []; for (let i = 0; i < 35; i++) { const buf = Buffer.from([i]); buffers.push(buf); await insertClientAudioBuffer( createAudioBufferInput({ callSid, bucketStartMs: 1000 + i * 1000, data: buf, }), ); } const chunks = await getAudioChunksByCallSid(callSid, limit); const expectedBuffers = buffers.slice(-limit); expect(chunks).toHaveLength(expectedBuffers.length); expect(Buffer.concat(chunks).equals(Buffer.concat(expectedBuffers))).toBe( true, ); }); });