From ff353e32889e4c3e464d0e1fe9f6b539b42c6b06 Mon Sep 17 00:00:00 2001 From: Ashvin Jaiswal Date: Wed, 28 Aug 2024 13:34:58 +0100 Subject: [PATCH] Update attachments.js : Add timeout before resolve files (#203) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update attachments.js : Add timeout before resolve files Cypress video processing may take extra time after the spec is complete, so add a timeout to wait for the video file readiness. * Update attachments.js to check readiness of mp4 file The moov atom in an MP4 file is a crucial part of the file’s structure. It contains metadata about the video, such as the duration, display characteristics, and timing information. Function check for the moov atom in file content and ensure is video file rediness * Fix Lint Issue --- lib/utils/attachments.js | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/utils/attachments.js b/lib/utils/attachments.js index c3e1892..672bf16 100644 --- a/lib/utils/attachments.js +++ b/lib/utils/attachments.js @@ -62,6 +62,45 @@ const waitForFile = ( checkFileExistence().catch(reject); }); +const checkMoovAtom = ( + mp4FilePath, + timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT, + interval = DEFAULT_WAIT_FOR_FILE_INTERVAL, +) => { + return new Promise((resolve, reject) => { + let totalTime = 0; + + const checkFile = () => { + try { + fs.readFile(mp4FilePath, (err, data) => { + if (err) { + return reject(new Error(`Error reading file: ${err.message}`)); + } + + if (data.includes('moov')) { + return resolve(true); + } + + if (totalTime >= timeout) { + return reject( + new Error( + `Timeout of ${timeout}ms reached, 'moov' atom not found in file ${mp4FilePath}.`, + ), + ); + } + totalTime += interval; + setTimeout(checkFile, interval); + return null; + }); + } catch (error) { + return reject(new Error(`Unexpected error: ${error.message}`)); + } + return null; + }; + checkFile(); + }); +}; + const getVideoFile = async ( specFileName, videosFolder = '**', @@ -84,6 +123,13 @@ const getVideoFile = async ( return null; } + try { + await checkMoovAtom(videoFilePath, timeout, interval); + } catch (e) { + console.warn(e.message); + return null; + } + return { name: fileName, type: 'video/mp4', @@ -95,4 +141,5 @@ module.exports = { getScreenshotAttachment, getVideoFile, waitForFile, + checkMoovAtom, };