@@ -4,8 +4,7 @@ import fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify';
44import { readFileSync , truncate } from 'fs'
55import { Context } from '../types.js'
66import { validateSnapshot } from './schemaValidation.js'
7- import { pingIntervalId } from './utils.js' ;
8- import { startPolling } from './utils.js' ;
7+ import { pingIntervalId , startPollingForTunnel , stopTunnelHelper , isTunnelPolling } from './utils.js' ;
98
109const uploadDomToS3ViaEnv = process . env . USE_LAMBDA_INTERNAL || false ;
1110export default async ( ctx : Context ) : Promise < FastifyInstance < Server , IncomingMessage , ServerResponse > > => {
@@ -38,6 +37,12 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
3837 try {
3938 let { snapshot, testType } = request . body ;
4039 if ( ! validateSnapshot ( snapshot ) ) throw new Error ( validateSnapshot . errors [ 0 ] . message ) ;
40+
41+ if ( snapshot ?. options ?. approvalThreshold !== undefined && snapshot ?. options ?. rejectionThreshold !== undefined ) {
42+ if ( snapshot ?. options ?. rejectionThreshold <= snapshot ?. options ?. approvalThreshold ) {
43+ throw new Error ( `Invalid snapshot options; rejectionThreshold (${ snapshot . options . rejectionThreshold } ) must be greater than approvalThreshold (${ snapshot . options . approvalThreshold } )` ) ;
44+ }
45+ }
4146
4247 // Fetch sessionId from snapshot options if present
4348 const sessionId = snapshot ?. options ?. sessionId ;
@@ -53,7 +58,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
5358 } else {
5459 // If not cached, fetch from API and cache it
5560 try {
56- let fetchedCapabilitiesResp = await ctx . client . getSmartUICapabilities ( sessionId , ctx . config , ctx . git , ctx . log ) ;
61+ let fetchedCapabilitiesResp = await ctx . client . getSmartUICapabilities ( sessionId , ctx . config , ctx . git , ctx . log , ctx . isStartExec ) ;
5762 capsBuildId = fetchedCapabilitiesResp ?. buildId || ''
5863 ctx . log . debug ( `fetch caps for sessionId: ${ sessionId } are ${ JSON . stringify ( fetchedCapabilitiesResp ) } ` )
5964 if ( capsBuildId ) {
@@ -106,6 +111,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
106111 let replyCode : number ;
107112 let replyBody : Record < string , any > ;
108113 try {
114+ ctx . log . info ( 'Received stop command. Finalizing build ...' ) ;
109115 if ( ctx . config . delayedUpload ) {
110116 ctx . log . debug ( "started after processing because of delayedUpload" )
111117 ctx . snapshotQueue ?. startProcessingfunc ( )
@@ -118,6 +124,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
118124 }
119125 } , 1000 ) ;
120126 } )
127+ let buildUrls = `build url: ${ ctx . build . url } \n` ;
121128
122129 for ( const [ sessionId , capabilities ] of ctx . sessionCapabilitiesMap . entries ( ) ) {
123130 try {
@@ -126,9 +133,12 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
126133 const totalSnapshots = capabilities ?. snapshotCount || 0 ;
127134 const sessionBuildUrl = capabilities ?. buildURL || '' ;
128135 const testId = capabilities ?. id || '' ;
129-
136+ ctx . log . debug ( `Capabilities for sessionId ${ sessionId } : ${ JSON . stringify ( capabilities ) } ` )
130137 if ( buildId && projectToken ) {
131138 await ctx . client . finalizeBuildForCapsWithToken ( buildId , totalSnapshots , projectToken , ctx . log ) ;
139+ if ( ctx . autoTunnelStarted ) {
140+ await startPollingForTunnel ( ctx , buildId , false , projectToken , capabilities ?. buildName ) ;
141+ }
132142 }
133143
134144 if ( testId && buildId ) {
@@ -151,6 +161,16 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
151161 }
152162 }
153163
164+
165+ //If Tunnel Details are present, start polling for tunnel status
166+ if ( ctx . tunnelDetails && ctx . tunnelDetails . tunnelHost != "" && ctx . build ?. id ) {
167+ await startPollingForTunnel ( ctx , ctx . build . id , false , '' , '' ) ;
168+ }
169+ //stop the tunnel if it was auto started and no tunnel polling is active
170+ if ( ctx . autoTunnelStarted && isTunnelPolling === null ) {
171+ await stopTunnelHelper ( ctx ) ;
172+ }
173+
154174 await ctx . browser ?. close ( ) ;
155175 if ( ctx . server ) {
156176 ctx . server . close ( ) ;
@@ -168,7 +188,9 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
168188 replyCode = 500 ;
169189 replyBody = { error : { message : error . message } } ;
170190 }
171-
191+
192+ ctx . log . info ( 'Stop command processed. Tearing down server.' ) ;
193+
172194 // Step 5: Return the response
173195 return reply . code ( replyCode ) . send ( replyBody ) ;
174196 } ) ;
0 commit comments