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