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.

100 lines
2.7 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.ClipFunction;
  21. beforeEach(() => {
  22. clipper = webVideoClipCore.createVideoClipper({
  23. downloaderExecutablePath: 'yt-dlp',
  24. type: webVideoClipCore.YouTube.VIDEO_TYPE,
  25. });
  26. });
  27. afterEach(() => {
  28. (spawnSync as Mock).mockReset();
  29. });
  30. it('calls the downloader function', async () => {
  31. (spawnSync as Mock)
  32. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  33. .mockReturnValueOnce({ stdout: Buffer.from('') });
  34. await clipper({
  35. url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
  36. start: 0,
  37. end: 0,
  38. });
  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', async () => {
  44. const causeError = new Error('generic downloader message');
  45. (spawnSync as Mock)
  46. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  47. .mockReturnValueOnce({ error: causeError });
  48. try {
  49. await clipper({
  50. url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
  51. start: 0,
  52. end: 0,
  53. })
  54. } catch (errRaw) {
  55. const err = errRaw as Error;
  56. expect(err).toBeInstanceOf(webVideoClipCore.YouTube.DownloaderFailedToStartError);
  57. expect(err.cause).toBe(causeError);
  58. expect(err).toHaveProperty('message', 'Downloader failed to start.');
  59. }
  60. });
  61. it('calls the buffer extract function', async () => {
  62. (spawnSync as Mock)
  63. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  64. .mockReturnValueOnce({ stdout: Buffer.from('') });
  65. await clipper({
  66. url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
  67. start: 0,
  68. end: 0,
  69. });
  70. expect(readFileSync).toBeCalled();
  71. });
  72. it('calls the cleanup function', async () => {
  73. (spawnSync as Mock)
  74. .mockReturnValueOnce({ stdout: Buffer.from('mkv') })
  75. .mockReturnValueOnce({ stdout: Buffer.from('') });
  76. await clipper({
  77. url: 'https://www.youtube.com/watch?v=BaW_jenozKc',
  78. start: 0,
  79. end: 0,
  80. });
  81. expect(unlinkSync).toBeCalled();
  82. });
  83. });
  84. });