|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 The Diffusion Studio Authors |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla |
| 5 | + * Public License, v. 2.0 that can be found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 9 | +import { FrameBuffer } from './buffer'; |
| 10 | + |
| 11 | +describe('FrameBuffer', () => { |
| 12 | + let frameBuffer: FrameBuffer; |
| 13 | + let mockVideoFrame: any; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + // Mock VideoFrame |
| 17 | + mockVideoFrame = { |
| 18 | + close: vi.fn(), |
| 19 | + }; |
| 20 | + |
| 21 | + frameBuffer = new FrameBuffer(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should enqueue frames and trigger onenqueue callback', () => { |
| 25 | + const mockOnEnqueue = vi.fn(); |
| 26 | + frameBuffer.onenqueue = mockOnEnqueue; |
| 27 | + |
| 28 | + frameBuffer.enqueue(mockVideoFrame); |
| 29 | + |
| 30 | + expect(frameBuffer['buffer'].length).toBe(1); |
| 31 | + expect(frameBuffer['buffer'][0]).toBe(mockVideoFrame); |
| 32 | + expect(mockOnEnqueue).toHaveBeenCalled(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should dequeue frames in FIFO order', async () => { |
| 36 | + const frame1 = { ...mockVideoFrame }; |
| 37 | + const frame2 = { ...mockVideoFrame }; |
| 38 | + |
| 39 | + frameBuffer.enqueue(frame1); |
| 40 | + frameBuffer.enqueue(frame2); |
| 41 | + |
| 42 | + const dequeuedFrame1 = await frameBuffer.dequeue(); |
| 43 | + const dequeuedFrame2 = await frameBuffer.dequeue(); |
| 44 | + |
| 45 | + expect(dequeuedFrame1).toBe(frame1); |
| 46 | + expect(dequeuedFrame2).toBe(frame2); |
| 47 | + expect(frameBuffer['buffer'].length).toBe(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should wait for a frame to be enqueued if buffer is empty and state is active', async () => { |
| 51 | + const mockOnEnqueue = vi.fn(); |
| 52 | + const mockWaitFor = vi.spyOn(frameBuffer as any, 'waitFor'); |
| 53 | + |
| 54 | + frameBuffer.onenqueue = mockOnEnqueue; |
| 55 | + const dequeuePromise = frameBuffer.dequeue(); |
| 56 | + |
| 57 | + // Simulate enqueuing a frame after some delay |
| 58 | + setTimeout(() => { |
| 59 | + frameBuffer.enqueue(mockVideoFrame); |
| 60 | + }, 100); |
| 61 | + |
| 62 | + const result = await dequeuePromise; |
| 63 | + |
| 64 | + expect(result).toBe(mockVideoFrame); |
| 65 | + expect(mockWaitFor).toHaveBeenCalledWith(20000); // 20s timeout |
| 66 | + }); |
| 67 | + |
| 68 | + it('should resolve immediately if buffer is closed and empty', async () => { |
| 69 | + frameBuffer.close(); |
| 70 | + |
| 71 | + const result = await frameBuffer.dequeue(); |
| 72 | + expect(result).toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should call onclose callback when buffer is closed', () => { |
| 76 | + const mockOnClose = vi.fn(); |
| 77 | + frameBuffer.onclose = mockOnClose; |
| 78 | + |
| 79 | + frameBuffer.close(); |
| 80 | + |
| 81 | + expect(frameBuffer['state']).toBe('closed'); |
| 82 | + expect(mockOnClose).toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should close all frames when terminate is called', () => { |
| 86 | + const frame1 = { ...mockVideoFrame, close: vi.fn() }; |
| 87 | + const frame2 = { ...mockVideoFrame, close: vi.fn() }; |
| 88 | + |
| 89 | + frameBuffer.enqueue(frame1); |
| 90 | + frameBuffer.enqueue(frame2); |
| 91 | + |
| 92 | + frameBuffer.terminate(); |
| 93 | + |
| 94 | + expect(frame1.close).toHaveBeenCalled(); |
| 95 | + expect(frame2.close).toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should reject after timeout if no enqueue or close happens', async () => { |
| 99 | + await expect((frameBuffer as any).waitFor(50)).rejects.toThrow('Promise timed out after 50 ms'); |
| 100 | + }); |
| 101 | +}); |
0 commit comments