Clip Web videos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 line
2.9 KiB

  1. import {
  2. describe, it, expect, vi, beforeEach, Mock, afterEach,
  3. } from 'vitest';
  4. import { spawnSync } from 'child_process';
  5. import { readFileSync, unlinkSync } from 'fs';
  6. import * as webVideoClipCore from '../src';
  7. import { ClipEventEmitter } from '../src';
  8. vi.mock('child_process', () => ({
  9. spawnSync: vi.fn(),
  10. }));
  11. vi.mock('fs', () => ({
  12. readFileSync: vi.fn(),
  13. unlinkSync: vi.fn(),
  14. }));
  15. describe('createVideoClipper', () => {
  16. beforeEach(() => {
  17. (readFileSync as Mock).mockReturnValueOnce(Buffer.from(''));
  18. });
  19. let clipper: ClipEventEmitter;
  20. beforeEach(() => {
  21. clipper = webVideoClipCore.createVideoClipper({
  22. downloaderExecutablePath: 'yt-dlp',
  23. postprocessorExecutablePath: 'ffmpeg',
  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('') })
  36. .mockReturnValueOnce({});
  37. clipper.on('end', done);
  38. clipper.process();
  39. expect(spawnSync).nthCalledWith(1, 'yt-dlp', expect.anything());
  40. }));
  41. it('emits downloader errors', () => new Promise((done) => {
  42. const causeError = new Error('generic downloader message');
  43. (spawnSync as Mock)
  44. .mockReturnValueOnce({ error: causeError })
  45. .mockReturnValueOnce({});
  46. clipper.on('error', (err: Error) => {
  47. expect(err.cause).toHaveProperty('message', causeError.message);
  48. });
  49. clipper.on('end', done);
  50. clipper.process();
  51. }));
  52. it('calls the postprocess function', () => new Promise((done) => {
  53. (spawnSync as Mock)
  54. .mockReturnValueOnce({ stdout: Buffer.from('') })
  55. .mockReturnValueOnce({});
  56. clipper.on('end', done);
  57. clipper.process();
  58. expect(spawnSync).nthCalledWith(2, 'ffmpeg', expect.anything());
  59. }));
  60. it('emits postprocess errors', () => new Promise((done) => {
  61. const causeError = new Error('generic postprocessor message');
  62. (spawnSync as Mock)
  63. .mockReturnValueOnce({ stdout: Buffer.from('') })
  64. .mockReturnValueOnce({ error: causeError });
  65. clipper.on('error', (err: Error) => {
  66. expect(err.cause).toHaveProperty('message', causeError.message);
  67. });
  68. clipper.on('end', done);
  69. clipper.process();
  70. }));
  71. it('calls the buffer extract function', () => new Promise((done) => {
  72. (spawnSync as Mock)
  73. .mockReturnValueOnce({ stdout: Buffer.from('') })
  74. .mockReturnValueOnce({});
  75. clipper.on('end', done);
  76. clipper.process();
  77. expect(readFileSync).toBeCalled();
  78. }));
  79. it('calls the cleanup function', () => new Promise((done) => {
  80. (spawnSync as Mock)
  81. .mockReturnValueOnce({ stdout: Buffer.from('') })
  82. .mockReturnValueOnce({});
  83. clipper.on('end', done);
  84. clipper.process();
  85. expect(unlinkSync).toBeCalled();
  86. }));
  87. });