Skip to content

Commit

Permalink
Video compression. Add several unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AmsterGet committed Sep 11, 2024
1 parent f84b668 commit 5a2c1f5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/utils/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const compressVideo = (filePath, crfValue) => {

const getVideoFile = async (
specFileName,
videoCompression,
videoCompression = false,
videosFolder = '**',
timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT,
interval = DEFAULT_WAIT_FOR_FILE_INTERVAL,
Expand Down Expand Up @@ -170,4 +170,5 @@ module.exports = {
waitForVideoFile,
getFilePathByGlobPattern,
checkVideoFileReady,
compressVideo,
};
58 changes: 57 additions & 1 deletion test/utils/attachments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const fsPromises = require('fs/promises');
const mockFs = require('mock-fs');
const path = require('path');
const glob = require('glob');

jest.mock('fluent-ffmpeg');
const ffmpeg = require('fluent-ffmpeg');
const attachmentUtils = require('../../lib/utils/attachments');

const {
Expand All @@ -10,6 +13,7 @@ const {
waitForVideoFile,
getFilePathByGlobPattern,
checkVideoFileReady,
compressVideo,
} = attachmentUtils;

const sep = path.sep;
Expand Down Expand Up @@ -169,7 +173,7 @@ describe('attachment utils', () => {
jest.spyOn(attachmentUtils, 'waitForVideoFile').mockResolvedValueOnce(mockVideoFilePath);
jest.spyOn(fsPromises, 'readFile').mockResolvedValueOnce(mockFileContent);

const result = await getVideoFile('video', '**', 5000, 1000);
const result = await getVideoFile('video', false, '**', 5000, 1000);

expect(result).toEqual({
name: 'video.mp4',
Expand Down Expand Up @@ -205,4 +209,56 @@ describe('attachment utils', () => {
expect(console.warn).toHaveBeenCalledWith('Failed to read file');
});
});

// TODO: add test for the real video file
describe('compressVideo', () => {
const spyPathJoin = jest.spyOn(path, 'join');
const spyDirnameJoin = jest.spyOn(path, 'dirname');
const spyBasenameJoin = jest.spyOn(path, 'basename');

const mockFilePath = 'path/to/video.mp4';
const mockOutputFilePath = 'path/to/compressed_video.mp4';

beforeEach(() => {
spyPathJoin.mockReturnValueOnce(mockOutputFilePath);
spyDirnameJoin.mockReturnValueOnce('path/to');
spyBasenameJoin.mockReturnValueOnce('video.mp4');
});

test('resolves with the correct output file path on successful compression', async () => {
const mockFfmpeg = {
outputOptions: jest.fn().mockReturnThis(),
save: jest.fn().mockReturnThis(),
on: jest.fn((event, handler) => {
if (event === 'end') {
handler();
}
return mockFfmpeg;
}),
};
ffmpeg.mockReturnValue(mockFfmpeg);

await expect(compressVideo(mockFilePath, 23)).resolves.toBe(mockOutputFilePath);
expect(ffmpeg).toHaveBeenCalledWith(mockFilePath);
expect(mockFfmpeg.outputOptions).toHaveBeenCalledWith('-crf 23');
expect(mockFfmpeg.save).toHaveBeenCalledWith(mockOutputFilePath);
});

test('rejects with an error if compression fails', async () => {
const mockError = new Error('Compression failed');
const mockFfmpeg = {
outputOptions: jest.fn().mockReturnThis(),
save: jest.fn().mockReturnThis(),
on: jest.fn((event, handler) => {
if (event === 'error') {
handler(mockError);
}
return mockFfmpeg;
}),
};
ffmpeg.mockReturnValue(mockFfmpeg);

await expect(compressVideo(mockFilePath, 23)).rejects.toThrow('Compression failed');
});
});
});

0 comments on commit 5a2c1f5

Please sign in to comment.