|
| 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, expect, it, vi } from 'vitest'; |
| 9 | + |
| 10 | +import { audioClipFilter, createRenderEventDetail, createStreamTarget, toOpusSampleRate, withError } from './utils'; |
| 11 | +import { ArrayBufferTarget, FileSystemWritableFileStreamTarget } from 'mp4-muxer'; |
| 12 | +import { Clip, MediaClip } from '../clips'; |
| 13 | + |
| 14 | + |
| 15 | +describe('createStreamTarget', () => { |
| 16 | + it('should create a downloadable stream target', async () => { |
| 17 | + const res = await createStreamTarget('test.mp4'); |
| 18 | + |
| 19 | + expect(res.target).toBeInstanceOf(ArrayBufferTarget); |
| 20 | + expect(res.fastStart).toBe('in-memory'); |
| 21 | + |
| 22 | + const a = document.createElement('a'); |
| 23 | + |
| 24 | + const clickSpy = vi.spyOn(a, 'click'); |
| 25 | + const createSpy = vi.spyOn(document, 'createElement').mockReturnValue(a); |
| 26 | + |
| 27 | + await res.close(true); |
| 28 | + |
| 29 | + expect(clickSpy).toBeCalledTimes(1); |
| 30 | + expect(createSpy).toBeCalledTimes(1); |
| 31 | + expect(a.download).toBe('test.mp4'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should upload the file if the target is an http address', async () => { |
| 35 | + const res = await createStreamTarget('https://s3.com/test.mp4'); |
| 36 | + |
| 37 | + expect(res.target).toBeInstanceOf(ArrayBufferTarget); |
| 38 | + expect(res.fastStart).toBe('in-memory'); |
| 39 | + |
| 40 | + const fetchSpy = vi.spyOn(global, 'fetch'); |
| 41 | + |
| 42 | + await res.close(true); |
| 43 | + |
| 44 | + expect(fetchSpy).toBeCalledTimes(1); |
| 45 | + expect(fetchSpy.mock.calls[0][0]).toBe('https://s3.com/test.mp4'); |
| 46 | + expect(fetchSpy.mock.calls[0][1]?.method).toBe('PUT'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should throw an error when using http upload and the response is not ok', async () => { |
| 50 | + const res = await createStreamTarget('https://s3.com/test.mp4'); |
| 51 | + |
| 52 | + const fetchSpy = vi.spyOn(global, 'fetch').mockReturnValue({ |
| 53 | + ok: false, |
| 54 | + } as any) |
| 55 | + |
| 56 | + await expect(() => res.close(true)).rejects.toThrowError(); |
| 57 | + |
| 58 | + fetchSpy.mockRestore(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle the file system access', async () => { |
| 62 | + const handle = new FileSystemFileHandle(); |
| 63 | + const res = await createStreamTarget(handle); |
| 64 | + |
| 65 | + expect(res.target).toBeInstanceOf(FileSystemWritableFileStreamTarget); |
| 66 | + expect(res.fastStart).toBe(false); |
| 67 | + |
| 68 | + const closeSpy = vi.spyOn(FileSystemWritableFileStream.prototype, 'close'); |
| 69 | + |
| 70 | + await res.close(true); |
| 71 | + |
| 72 | + expect(closeSpy).toBeCalledTimes(1); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('withError', () => { |
| 77 | + it('should reject promise all errors', async () => { |
| 78 | + const promise1 = Promise.resolve(3); |
| 79 | + const promise2 = new Promise((_, reject) => |
| 80 | + setTimeout(reject, 100, 'foo'), |
| 81 | + ); |
| 82 | + |
| 83 | + const promise = withError(Promise.allSettled([promise1, promise2])); |
| 84 | + |
| 85 | + await expect(promise).rejects.toThrowError(); |
| 86 | + }) |
| 87 | +}); |
| 88 | + |
| 89 | +describe('createRenderEventDetail', () => { |
| 90 | + it('should should calculate the remaining time', async () => { |
| 91 | + const date = new Date(); |
| 92 | + date.setSeconds(date.getSeconds() - 10); |
| 93 | + const detail = createRenderEventDetail(50, 100, date.getTime()); |
| 94 | + expect(detail.progress).toBe(50); |
| 95 | + expect(detail.total).toBe(100); |
| 96 | + // it took 10 secs for 50% there should be 10 seconds remaining |
| 97 | + expect(detail.remaining.getSeconds()).toBe(10); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('audioClipFilter', () => { |
| 102 | + it('should filter clips', async () => { |
| 103 | + const clips = [new Clip(), new Clip(), new MediaClip({ disabled: true }), new MediaClip()]; |
| 104 | + |
| 105 | + expect(clips.filter(audioClipFilter).length).toBe(1); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('toOpusSampleRate', () => { |
| 110 | + it('should find the closes available opus sample rate', async () => { |
| 111 | + expect(toOpusSampleRate(0)).toBe(8000); |
| 112 | + expect(toOpusSampleRate(10000)).toBe(8000); |
| 113 | + expect(toOpusSampleRate(10001)).toBe(12000); |
| 114 | + expect(toOpusSampleRate(50000)).toBe(48000); |
| 115 | + }); |
| 116 | +}); |
0 commit comments