@@ -3,6 +3,8 @@ import { Context } from '../types.js'
33import { chromium , firefox , webkit , Browser } from '@playwright/test'
44import constants from './constants.js' ;
55
6+ let isPollingActive = false ;
7+
68export function delDir ( dir : string ) : void {
79 if ( fs . existsSync ( dir ) ) {
810 fs . rmSync ( dir , { recursive : true } ) ;
@@ -199,4 +201,87 @@ export function getRenderViewportsForOptions(options: any): Array<Record<string,
199201 ...mobileRenderViewports [ constants . MOBILE_OS_IOS ] ,
200202 ...mobileRenderViewports [ constants . MOBILE_OS_ANDROID ]
201203 ] ;
204+ }
205+
206+ // Global SIGINT handler
207+ process . on ( 'SIGINT' , ( ) => {
208+ if ( isPollingActive ) {
209+ console . log ( 'Fetching results interrupted. Exiting...' ) ;
210+ isPollingActive = false ;
211+ } else {
212+ console . log ( '\nExiting gracefully...' ) ;
213+ }
214+ process . exit ( 0 ) ;
215+ } ) ;
216+
217+ // Background polling function
218+ export async function startPolling ( ctx : Context , task : any ) : Promise < void > {
219+ ctx . log . info ( 'Fetching results in progress....' ) ;
220+ isPollingActive = true ;
221+
222+ const intervalId = setInterval ( async ( ) => {
223+ if ( ! isPollingActive ) {
224+ clearInterval ( intervalId ) ;
225+ return ;
226+ }
227+
228+ try {
229+ const resp = await ctx . client . getScreenshotData ( ctx . build . id , ctx . build . baseline , ctx . log ) ;
230+
231+ if ( ! resp . build ) {
232+ ctx . log . info ( "Error: Build data is null." ) ;
233+ clearInterval ( intervalId ) ;
234+ isPollingActive = false ;
235+ }
236+
237+ fs . writeFileSync ( ctx . options . fetchResultsFileName , JSON . stringify ( resp , null , 2 ) ) ;
238+ ctx . log . debug ( `Updated results in ${ ctx . options . fetchResultsFileName } ` ) ;
239+
240+ if ( resp . build . build_status_ind === constants . BUILD_COMPLETE || resp . build . build_status_ind === constants . BUILD_ERROR ) {
241+ clearInterval ( intervalId ) ;
242+ ctx . log . info ( `Fetching results completed. Final results written to ${ ctx . options . fetchResultsFileName } ` ) ;
243+ isPollingActive = false ;
244+
245+
246+ // Evaluating Summary
247+ let totalScreenshotsWithMismatches = 0 ;
248+ let totalVariantsWithMismatches = 0 ;
249+ const totalScreenshots = Object . keys ( resp . screenshots || { } ) . length ;
250+ let totalVariants = 0 ;
251+
252+ for ( const [ screenshot , variants ] of Object . entries ( resp . screenshots || { } ) ) {
253+ let screenshotHasMismatch = false ;
254+ let variantMismatchCount = 0 ;
255+
256+ totalVariants += variants . length ; // Add to total variants count
257+
258+ for ( const variant of variants ) {
259+ if ( variant . mismatch_percentage > 0 ) {
260+ screenshotHasMismatch = true ;
261+ variantMismatchCount ++ ;
262+ }
263+ }
264+
265+ if ( screenshotHasMismatch ) {
266+ totalScreenshotsWithMismatches ++ ;
267+ totalVariantsWithMismatches += variantMismatchCount ;
268+ }
269+ }
270+
271+ // Display summary
272+ ctx . log . info (
273+ `\nSummary of Mismatches:\n` +
274+ `Total Variants with Mismatches: ${ totalVariantsWithMismatches } out of ${ totalVariants } \n` +
275+ `Total Screenshots with Mismatches: ${ totalScreenshotsWithMismatches } out of ${ totalScreenshots } \n` +
276+ `Branch Name: ${ resp . build . branch } \n` +
277+ `Project Name: ${ resp . project . name } \n` +
278+ `Build ID: ${ resp . build . build_id } \n`
279+ ) ;
280+ }
281+ } catch ( error : any ) {
282+ ctx . log . error ( `Error fetching screenshot data: ${ error . message } ` ) ;
283+ clearInterval ( intervalId ) ;
284+ isPollingActive = false ;
285+ }
286+ } , 5000 ) ;
202287}
0 commit comments