Clip Web videos.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.test.ts 2.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. describe,
  3. it,
  4. expect,
  5. vi,
  6. beforeEach,
  7. Mock,
  8. afterEach,
  9. } from 'vitest';
  10. import { spawnSync } from 'child_process';
  11. import { readFileSync, unlinkSync } from 'fs';
  12. import * as webVideoClipCore from '../src';
  13. vi.mock('child_process');
  14. vi.mock('fs');
  15. describe('createVideoClipper', () => {
  16. beforeEach(() => {
  17. (readFileSync as Mock).mockReturnValueOnce(Buffer.from(''));
  18. });
  19. describe('without postprocessing', () => {
  20. let clipper: webVideoClipCore.VideoClipEventEmitter;
  21. beforeEach(() => {
  22. clipper = webVideoClipCore.createVideoClipper({
  23. downloaderExecutablePath: 'yt-dlp',
  24. type: webVideoClipCore.VideoType.YOUTUBE,
  25. url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
  26. start: 0,
  27. end: 0,
  28. });
  29. });
  30. afterEach(() => {
  31. (spawnSync as Mock).mockReset();
  32. });
  33. it('calls the downloader function', () => new Promise((done) => {
  34. (spawnSync as Mock)
  35. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  36. .mockReturnValueOnce({ stdout: Buffer.from('') });
  37. clipper.on('end', done);
  38. clipper.process();
  39. expect(spawnSync).toBeCalledTimes(2);
  40. expect(spawnSync).nthCalledWith(1, 'yt-dlp', expect.arrayContaining(['--print']));
  41. expect(spawnSync).nthCalledWith(2, 'yt-dlp', expect.anything());
  42. }));
  43. it('emits downloader errors', () => new Promise((done) => {
  44. const causeError = new Error('generic downloader message');
  45. (spawnSync as Mock)
  46. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  47. .mockReturnValueOnce({ error: causeError });
  48. clipper.on('error', (err: Error) => {
  49. expect(err).toHaveProperty('message', causeError.message);
  50. });
  51. clipper.on('end', done);
  52. clipper.process();
  53. }));
  54. it('calls the buffer extract function', () => new Promise((done) => {
  55. (spawnSync as Mock)
  56. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  57. .mockReturnValueOnce({ stdout: Buffer.from('') });
  58. clipper.on('end', done);
  59. clipper.process();
  60. expect(readFileSync).toBeCalled();
  61. }));
  62. it('calls the cleanup function', () => new Promise((done) => {
  63. (spawnSync as Mock)
  64. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  65. .mockReturnValueOnce({ stdout: Buffer.from('') });
  66. clipper.on('end', done);
  67. clipper.process();
  68. expect(unlinkSync).toBeCalled();
  69. }));
  70. });
  71. });