Clip Web videos.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

87 rindas
2.4 KiB

  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. });