Skip to content

Commit ffa118a

Browse files
committed
resolved merge conflicts
2 parents 76b4f20 + 0993b97 commit ffa118a

File tree

12 files changed

+100
-22
lines changed

12 files changed

+100
-22
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
"which": "^4.0.0",
5050
"winston": "^3.10.0"
5151
},
52+
"overrides": {
53+
"simple-swizzle": "0.2.2"
54+
},
5255
"devDependencies": {
5356
"typescript": "^5.3.2"
5457
}

src/commander/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import getGitInfo from '../tasks/getGitInfo.js';
88
import createBuildExec from '../tasks/createBuildExec.js';
99
import snapshotQueue from '../lib/snapshotQueue.js';
1010
import { startPolling, startPingPolling } from '../lib/utils.js';
11+
import startTunnel from '../tasks/startTunnel.js'
1112

1213
const command = new Command();
1314

@@ -27,12 +28,13 @@ command
2728
ctx.snapshotQueue = new snapshotQueue(ctx);
2829
ctx.totalSnapshots = 0
2930
ctx.isStartExec = true
30-
31+
3132
let tasks = new Listr<Context>(
3233
[
3334
authExec(ctx),
3435
startServer(ctx),
3536
getGitInfo(ctx),
37+
...(ctx.config.tunnel && ctx.config.tunnel?.type === 'auto' ? [startTunnel(ctx)] : []),
3638
createBuildExec(ctx),
3739

3840
],
@@ -51,14 +53,15 @@ command
5153
try {
5254
await tasks.run(ctx);
5355
if (ctx.build && ctx.build.id) {
54-
startPingPolling(ctx);
56+
startPingPolling(ctx, 'exec-start');
5557
}
5658
if (ctx.options.fetchResults && ctx.build && ctx.build.id) {
5759
startPolling(ctx, '', false, '')
5860
}
5961

6062
} catch (error) {
6163
console.error('Error during server execution:', error);
64+
process.exit(1);
6265
}
6366
});
6467

src/commander/stopServer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ command
3333
}
3434
} catch (error: any) {
3535
// Handle any errors during the HTTP request
36-
if (error.code === 'ECONNABORTED') {
36+
if (error && error.code === 'ECONNABORTED') {
3737
console.error(chalk.red('Error: SmartUI server did not respond in 15 seconds'));
38+
} if (error && error.code === 'ECONNREFUSED') {
39+
console.error(chalk.red('Error: Looks like smartui cli server is already stopped'));
3840
} else {
3941
console.error(chalk.red('Error while stopping server'));
4042
}

src/lib/ctx.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default (options: Record<string, string>): Context => {
2525
let buildNameObj: string;
2626
let allowDuplicateSnapshotNames: boolean = false;
2727
let useLambdaInternal: boolean = false;
28+
let useExtendedViewport: boolean = false;
29+
let loadDomContent: boolean = false;
2830
try {
2931
if (options.config) {
3032
config = JSON.parse(fs.readFileSync(options.config, 'utf-8'));
@@ -106,6 +108,12 @@ export default (options: Record<string, string>): Context => {
106108
if (config.useLambdaInternal) {
107109
useLambdaInternal = true;
108110
}
111+
if (config.useExtendedViewport) {
112+
useExtendedViewport = true;
113+
}
114+
if (config.loadDomContent) {
115+
loadDomContent = true;
116+
}
109117

110118
//if config.waitForPageRender has value and if its less than 30000 then make it to 30000 default
111119
if (config.waitForPageRender && config.waitForPageRender < 30000) {
@@ -138,6 +146,8 @@ export default (options: Record<string, string>): Context => {
138146
requestHeaders: config.requestHeaders || {},
139147
allowDuplicateSnapshotNames: allowDuplicateSnapshotNames,
140148
useLambdaInternal: useLambdaInternal,
149+
useExtendedViewport: useExtendedViewport,
150+
loadDomContent: loadDomContent,
141151
approvalThreshold: config.approvalThreshold,
142152
rejectionThreshold: config.rejectionThreshold,
143153
},

src/lib/httpClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export default class httpClient {
281281
}
282282

283283

284-
getSmartUICapabilities(sessionId: string, config: any, git: any, log: Logger) {
284+
getSmartUICapabilities(sessionId: string, config: any, git: any, log: Logger, isStartExec: boolean) {
285285
return this.request({
286286
url: '/sessions/capabilities',
287287
method: 'GET',
@@ -290,7 +290,8 @@ export default class httpClient {
290290
},
291291
data: {
292292
git,
293-
config
293+
config,
294+
isStartExec
294295
},
295296
headers: {
296297
projectToken: '',

src/lib/processSnapshot.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export async function prepareSnapshot(snapshot: Snapshot, ctx: Context): Promise
4646
if (options.loadDomContent) {
4747
processedOptions.loadDomContent = true;
4848
}
49-
49+
if (options.useExtendedViewport) {
50+
processedOptions.useExtendedViewport = true;
51+
}
5052
if (options.sessionId) {
5153
const sessionId = options.sessionId;
5254
processedOptions.sessionId = sessionId
@@ -145,6 +147,13 @@ export async function prepareSnapshot(snapshot: Snapshot, ctx: Context): Promise
145147
}
146148
}
147149

150+
if (ctx.config.loadDomContent) {
151+
processedOptions.loadDomContent = true;
152+
}
153+
if (ctx.config.useExtendedViewport) {
154+
processedOptions.useExtendedViewport = true;
155+
}
156+
148157
processedOptions.allowedAssets = ctx.config.allowedAssets;
149158
processedOptions.selectors = selectors;
150159

@@ -522,6 +531,13 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
522531
}
523532
}
524533

534+
if (ctx.config.loadDomContent) {
535+
processedOptions.loadDomContent = true;
536+
}
537+
if (ctx.config.useExtendedViewport) {
538+
processedOptions.useExtendedViewport = true;
539+
}
540+
525541
// process for every viewport
526542
let navigated: boolean = false;
527543
let previousDeviceType: string | null = null;

src/lib/schemaValidation.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ const ConfigSchema = {
239239
type: "string",
240240
errorMessage: "Invalid config; logFile should be a string value"
241241
},
242+
environment: {
243+
type: "string",
244+
enum: ["stage", "prod"],
245+
errorMessage: "Invalid config; environment should be a string value either stage or prod"
246+
}
242247
},
243248
required: ["type"],
244249
additionalProperties: false
@@ -267,6 +272,14 @@ const ConfigSchema = {
267272
type: "boolean",
268273
errorMessage: "Invalid config; useLambdaInternal must be true/false"
269274
},
275+
useExtendedViewport: {
276+
type: "boolean",
277+
errorMessage: "Invalid config; useExtendedViewport must be true/false"
278+
},
279+
loadDomContent: {
280+
type: "boolean",
281+
errorMessage: "Invalid config; loadDomContent must be true/false"
282+
},
270283
approvalThreshold: {
271284
type: "number",
272285
minimum: 0,
@@ -538,6 +551,10 @@ const SnapshotSchema: JSONSchemaType<Snapshot> = {
538551
type: "number",
539552
errorMessage: "Invalid snapshot options; timeout must be a number"
540553
},
554+
useExtendedViewport: {
555+
type: "boolean",
556+
errorMessage: "Invalid snapshot options; useExtendedViewport must be a boolean"
557+
},
541558
approvalThreshold: {
542559
type: "number",
543560
minimum: 0,

src/lib/server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { readFileSync, truncate } from 'fs'
55
import { Context } from '../types.js'
66
import { validateSnapshot } from './schemaValidation.js'
77
import { pingIntervalId } from './utils.js';
8-
import { startPolling } from './utils.js';
8+
import { stopTunnelHelper } from './utils.js';
99

1010
const uploadDomToS3ViaEnv = process.env.USE_LAMBDA_INTERNAL || false;
1111
export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMessage, ServerResponse>> => {
@@ -59,7 +59,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
5959
} else {
6060
// If not cached, fetch from API and cache it
6161
try {
62-
let fetchedCapabilitiesResp = await ctx.client.getSmartUICapabilities(sessionId, ctx.config, ctx.git, ctx.log);
62+
let fetchedCapabilitiesResp = await ctx.client.getSmartUICapabilities(sessionId, ctx.config, ctx.git, ctx.log, ctx.isStartExec);
6363
capsBuildId = fetchedCapabilitiesResp?.buildId || ''
6464
ctx.log.debug(`fetch caps for sessionId: ${sessionId} are ${JSON.stringify(fetchedCapabilitiesResp)}`)
6565
if (capsBuildId) {
@@ -157,6 +157,11 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
157157
}
158158
}
159159

160+
//Handle Tunnel closure
161+
if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
162+
await stopTunnelHelper(ctx)
163+
}
164+
160165
await ctx.browser?.close();
161166
if (ctx.server){
162167
ctx.server.close();
@@ -174,7 +179,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
174179
replyCode = 500;
175180
replyBody = { error: { message: error.message } };
176181
}
177-
182+
178183
// Step 5: Return the response
179184
return reply.code(replyCode).send(replyBody);
180185
});

src/lib/utils.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo
324324

325325
export let pingIntervalId: NodeJS.Timeout | null = null;
326326

327-
export async function startPingPolling(ctx: Context): Promise<void> {
327+
export async function startPingPolling(ctx: Context, event: string): Promise<void> {
328328
try {
329329
ctx.log.debug('Sending initial ping to server...');
330330
await ctx.client.ping(ctx.build.id, ctx.log);
@@ -336,9 +336,9 @@ export async function startPingPolling(ctx: Context): Promise<void> {
336336
// Start the polling interval
337337
pingIntervalId = setInterval(async () => {
338338
try {
339-
ctx.log.debug('Sending ping to server...');
339+
ctx.log.debug('Sending ping to server...'+ event);
340340
await ctx.client.ping(ctx.build.id, ctx.log);
341-
ctx.log.debug('Ping sent successfully.');
341+
ctx.log.debug('Ping sent successfully.'+ event);
342342
} catch (error: any) {
343343
ctx.log.error(`Error during ping polling: ${error.message}`);
344344
}
@@ -390,6 +390,11 @@ export async function startTunnelBinary(ctx: Context) {
390390
ctx.config.tunnel.tunnelName = randomTunnelName
391391
}
392392

393+
if (tunnelConfig?.environment) {
394+
tunnelArguments.environment = tunnelConfig.environment
395+
}
396+
397+
393398
ctx.log.debug(`tunnel config ${JSON.stringify(tunnelArguments)}`)
394399

395400
if (ctx.config.tunnel?.type === 'auto') {
@@ -434,7 +439,7 @@ export async function startPollingForTunnel(ctx: Context, build_id: string, base
434439

435440
const status = await tunnelInstance.stop();
436441
ctx.log.debug('Tunnel is Stopped ? ' + status);
437-
442+
return;
438443
}
439444
} catch (error: any) {
440445
if (error.message.includes('ENOTFOUND')) {
@@ -450,10 +455,10 @@ export async function startPollingForTunnel(ctx: Context, build_id: string, base
450455

451456
export async function stopTunnelHelper(ctx: Context) {
452457
const tunnelRunningStatus = await tunnelInstance.isRunning();
453-
ctx.log.debug('Running status of tunnel before stopping ? ' + tunnelRunningStatus);
458+
ctx.log.debug('stop-tunnel:: Running status of tunnel before stopping ? ' + tunnelRunningStatus);
454459

455460
const status = await tunnelInstance.stop();
456-
ctx.log.debug('Tunnel is Stopped ? ' + status);
461+
ctx.log.debug('stop-tunnel:: Tunnel is Stopped ? ' + status);
457462
}
458463

459464
/**

src/tasks/createBuildExec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
3636
}
3737
task.output = chalk.gray(`build id: ${resp.data.buildId}`);
3838
task.title = 'SmartUI build created'
39+
if (ctx.env.USE_REMOTE_DISCOVERY){
40+
task.output += chalk.gray(`\n Using remote discovery for this build`);
41+
}
3942
} else {
4043
task.output = chalk.gray(`Empty PROJECT_TOKEN and PROJECT_NAME. Skipping Creation of Build!`)
4144
task.title = 'Skipped SmartUI build creation'
@@ -45,9 +48,10 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
4548
}
4649

4750
if (ctx.config.tunnel && ctx.config.tunnel?.type === 'auto') {
48-
startPingPolling(ctx);
4951
if (ctx.build && ctx.build.id) {
5052
startPollingForTunnel(ctx, '', false, '');
53+
} else {
54+
startPingPolling(ctx, "tunnel-process");
5155
}
5256
}
5357

@@ -61,6 +65,14 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
6165
tunnelName: tunnelResp.data.tunnel_name
6266
}
6367
ctx.log.debug(`Tunnel Details: ${JSON.stringify(ctx.tunnelDetails)}`)
68+
//USE_REMOTE_DISCOVERY as default if Tunnel is true
69+
if (process.env.USE_REMOTE_DISCOVERY === undefined) {
70+
ctx.env.USE_REMOTE_DISCOVERY = true;
71+
process.env.USE_REMOTE_DISCOVERY = 'true';
72+
task.output += chalk.gray(`\n Using remote discovery by deafult for this build`);
73+
}
74+
ctx.log.debug(`USE_REMOTE_DISCOVERY is set to ${ctx.env.USE_REMOTE_DISCOVERY}`);
75+
6476
} else if (tunnelResp && tunnelResp.error) {
6577
if (tunnelResp.error.message) {
6678
if (tunnelResp.error.code && tunnelResp.error.code === 400) {

0 commit comments

Comments
 (0)