Skip to content

Commit

Permalink
test: mp4-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
hughfenghen committed Apr 22, 2024
1 parent f46f5fc commit c7cbdde
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import '../../__tests__/mock';
import { beforeAll, describe, expect, test, vi } from 'vitest';
import mp4box from '@webav/mp4box.js';
import { file2stream } from '..';
import { autoReadStream } from '../../av-utils';

beforeAll(() => {
vi.useFakeTimers();
Expand All @@ -10,23 +10,28 @@ beforeAll(() => {
describe('file2stream', () => {
test('enqueue data to stream', () => {
const file = mp4box.createFile();
file.boxes = [];
file.boxes.push(
...Array(5)
.fill(0)
.map(() => ({
write: (ds) => {
ds.writeUint8Array(new Uint8Array([1]));
},
})),
);

const { stop, stream } = file2stream(file, 500);
expect(stream).toBeInstanceOf(ReadableStream);
expect(stop).toBeInstanceOf(Function);

vi.advanceTimersByTime(500);
expect(stream._ctrl.enqueue).not.toBeCalled();

file.boxes.push(
...Array(5)
.fill(0)
.map(() => ({ write: vi.fn(), data: new ArrayBuffer(0) })),
);

vi.advanceTimersByTime(500);
expect(stream._ctrl.enqueue).toHaveBeenCalledWith(new Uint8Array());
autoReadStream(stream, {
onChunk: async (chunk) => {
expect(chunk).toEqual(new Uint8Array([1, 1, 1, 1, 1]));
},
onDone: () => {},
});
// 内存引用被清理
expect([...file.boxes]).toEqual(Array(5).fill(undefined));
});
Expand All @@ -37,16 +42,14 @@ describe('file2stream', () => {
.fill(0)
.map(() => ({ write: vi.fn(), data: new ArrayBuffer(0) }));
vi.spyOn(file, 'flush');
vi.spyOn(global, 'clearInterval');
vi.spyOn(globalThis, 'clearInterval');

const { stop, stream } = file2stream(file, 500);
const { stop } = file2stream(file, 500);
stop();

expect(file.flush).toBeCalled();
expect(stream._ctrl.enqueue).toBeCalled();
expect(stream._ctrl.close).toBeCalled();
expect(vi.getTimerCount()).toBe(1);
expect(global.clearInterval).toBeCalled();
expect(globalThis.clearInterval).toBeCalled();
});

test('cancel stream', () => {
Expand All @@ -55,13 +58,13 @@ describe('file2stream', () => {
.fill(0)
.map(() => ({ write: vi.fn(), data: new ArrayBuffer(0) }));
vi.spyOn(file, 'flush');
vi.spyOn(global, 'clearInterval');
vi.spyOn(globalThis, 'clearInterval');

const spyCancel = vi.fn();
const { stream } = file2stream(file, 500, spyCancel);
stream.cancel();

expect(global.clearInterval).toBeCalled();
expect(globalThis.clearInterval).toBeCalled();
expect(spyCancel).toBeCalled();
});
});

0 comments on commit c7cbdde

Please sign in to comment.