- 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.ClipFunction;
- beforeEach(() => {
- clipper = webVideoClipCore.createVideoClipper({
- downloaderExecutablePath: 'yt-dlp',
- type: webVideoClipCore.YouTube.VIDEO_TYPE,
- });
- });
-
- afterEach(() => {
- (spawnSync as Mock).mockReset();
- });
-
- it('calls the downloader function', async () => {
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ stdout: Buffer.from('') });
-
- await clipper({
- url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
- start: 0,
- end: 0,
- });
- 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', async () => {
- const causeError = new Error('generic downloader message');
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ error: causeError });
-
- try {
- await clipper({
- url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
- start: 0,
- end: 0,
- })
- } catch (errRaw) {
- const err = errRaw as Error;
- expect(err).toBeInstanceOf(webVideoClipCore.YouTube.DownloaderFailedToStartError);
- expect(err.cause).toBe(causeError);
- expect(err).toHaveProperty('message', 'Downloader failed to start.');
- }
- });
-
- it('calls the buffer extract function', async () => {
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ stdout: Buffer.from('') });
-
- await clipper({
- url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
- start: 0,
- end: 0,
- });
-
- expect(readFileSync).toBeCalled();
- });
-
- it('calls the cleanup function', async () => {
- (spawnSync as Mock)
- .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
- .mockReturnValueOnce({ stdout: Buffer.from('') });
-
- await clipper({
- url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
- start: 0,
- end: 0,
- });
-
- expect(unlinkSync).toBeCalled();
- });
- });
- });
|