|
- import {
- describe,
- it,
- expect,
- vi,
- beforeEach,
- Mock,
- afterEach,
- } from 'vitest';
- import { spawnSync } from 'child_process';
- import { readFileSync, unlinkSync } from 'fs';
- import * as webVideoClipCore from '../src';
-
- vi.mock('child_process');
-
- vi.mock('fs');
-
- describe('createVideoClipper', () => {
- beforeEach(() => {
- (readFileSync as Mock).mockReturnValueOnce(Buffer.from(''));
- });
-
- describe('without postprocessing', () => {
- let clipper: webVideoClipCore.VideoClipEventEmitter;
- beforeEach(() => {
- clipper = webVideoClipCore.createVideoClipper({
- downloaderExecutablePath: 'yt-dlp',
- type: webVideoClipCore.VideoType.YOUTUBE,
- url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
- start: 0,
- end: 0,
- });
- });
-
- afterEach(() => {
- (spawnSync as Mock).mockReset();
- });
-
- it('calls the downloader function', () => new Promise((done) => {
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ stdout: Buffer.from('') });
-
- clipper.on('end', done);
- clipper.process();
- expect(spawnSync).toBeCalledTimes(2);
- expect(spawnSync).nthCalledWith(1, 'yt-dlp', expect.arrayContaining(['--print']));
- expect(spawnSync).nthCalledWith(2, 'yt-dlp', expect.anything());
- }));
-
- it('emits downloader errors', () => new Promise((done) => {
- const causeError = new Error('generic downloader message');
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ error: causeError });
-
- clipper.on('error', (err: Error) => {
- expect(err).toHaveProperty('message', causeError.message);
- });
- clipper.on('end', done);
- clipper.process();
- }));
-
- it('calls the buffer extract function', () => new Promise((done) => {
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ stdout: Buffer.from('') });
-
- clipper.on('end', done);
- clipper.process();
-
- expect(readFileSync).toBeCalled();
- }));
-
- it('calls the cleanup function', () => new Promise((done) => {
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ stdout: Buffer.from('') });
-
- clipper.on('end', done);
- clipper.process();
-
- expect(unlinkSync).toBeCalled();
- }));
- });
- });
|