Skip to content

Commit 79c337e

Browse files
authored
Merge pull request #391 from LambdaTest/stage
4.1.37-beta.0
2 parents 1e23081 + eab0af5 commit 79c337e

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdatest/smartui-cli",
3-
"version": "4.1.36",
3+
"version": "4.1.37-beta.0",
44
"description": "A command line interface (CLI) to run SmartUI tests on LambdaTest",
55
"files": [
66
"dist/**/*"

src/lib/httpClient.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export default class httpClient {
375375
}, ctx.log)
376376
}
377377

378-
processSnapshotCaps(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors) {
378+
processSnapshotCaps(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors, variantCount: number, sync: boolean = false) {
379379
return this.request({
380380
url: `/build/${capsBuildId}/snapshot`,
381381
method: 'POST',
@@ -387,17 +387,19 @@ export default class httpClient {
387387
name: snapshot.name,
388388
url: snapshot.url,
389389
snapshotUuid: snapshotUuid,
390+
variantCount: variantCount,
390391
test: {
391392
type: ctx.testType,
392393
source: 'cli'
393394
},
394395
doRemoteDiscovery: snapshot.options.doRemoteDiscovery,
395396
discoveryErrors: discoveryErrors,
397+
sync: sync
396398
}
397399
}, ctx.log)
398400
}
399401

400-
uploadSnapshotForCaps(ctx: Context, snapshot: ProcessedSnapshot, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors) {
402+
uploadSnapshotForCaps(ctx: Context, snapshot: ProcessedSnapshot, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors, variantCount: number, sync: boolean = false) {
401403
// Use capsBuildId if provided, otherwise fallback to ctx.build.id
402404
const buildId = capsBuildId !== '' ? capsBuildId : ctx.build.id;
403405

@@ -415,6 +417,8 @@ export default class httpClient {
415417
source: 'cli'
416418
},
417419
discoveryErrors: discoveryErrors,
420+
variantCount: variantCount,
421+
sync: sync
418422
}
419423
}, ctx.log);
420424
}
@@ -660,9 +664,9 @@ export default class httpClient {
660664
}, ctx.log)
661665
}
662666

663-
getSnapshotStatus(snapshotName: string, snapshotUuid: string, ctx: Context): Promise<Record<string, any>> {
667+
getSnapshotStatus(buildId: string, snapshotName: string, snapshotUuid: string, ctx: Context): Promise<Record<string, any>> {
664668
return this.request({
665-
url: `/snapshot/status?buildId=${ctx.build.id}&snapshotName=${snapshotName}&snapshotUUID=${snapshotUuid}`,
669+
url: `/snapshot/status?buildId=${buildId}&snapshotName=${snapshotName}&snapshotUUID=${snapshotUuid}`,
666670
method: 'GET',
667671
headers: {
668672
'Content-Type': 'application/json',

src/lib/server.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
118118
}
119119

120120
if (contextId && ctx.contextToSnapshotMap) {
121-
ctx.contextToSnapshotMap.set(contextId, 0);
121+
ctx.contextToSnapshotMap.set(contextId, '0');
122122
ctx.log.debug(`Marking contextId as captured and added to queue: ${contextId}`);
123123
}
124124

@@ -252,26 +252,36 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
252252
if (ctx.contextToSnapshotMap?.has(contextId)) {
253253
let contextStatus = ctx.contextToSnapshotMap.get(contextId);
254254

255-
while (contextStatus==0) {
255+
let counter= 60;
256+
while (contextStatus==='0') {
257+
if(counter<=0){
258+
throw new Error('Snapshot processing failed');
259+
}
260+
contextStatus = ctx.contextToSnapshotMap.get(contextId);
256261
// Wait 5 seconds before next check
257262
await new Promise(resolve => setTimeout(resolve, 5000));
258-
259-
contextStatus = ctx.contextToSnapshotMap.get(contextId);
263+
counter--;
260264
}
261265

262-
if(contextStatus==2){
266+
if(contextStatus==='2'){
263267
throw new Error("Snapshot Failed");
264268
}
265269

266270
ctx.log.debug("Snapshot uploaded successfully");
267271

272+
const buildId = contextStatus;
273+
if (!buildId) {
274+
throw new Error(`No buildId found for contextId: ${contextId}`);
275+
}
276+
268277
// Poll external API until it returns 200 or timeout is reached
269278
let lastExternalResponse: any = null;
270279
const startTime = Date.now();
271280

272281
while (true) {
273282
try {
274283
const externalResponse = await ctx.client.getSnapshotStatus(
284+
buildId,
275285
snapshotName,
276286
contextId,
277287
ctx

src/lib/snapshotQueue.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ export default class Queue {
364364
if (useCapsBuildId) {
365365
this.ctx.log.info(`Using cached buildId: ${capsBuildId}`);
366366
if (useKafkaFlowCaps) {
367-
const snapshotUuid = uuidv4();
367+
let snapshotUuid = uuidv4();
368+
if (snapshot?.options?.contextId && this.ctx.contextToSnapshotMap?.has(snapshot.options.contextId)) {
369+
snapshotUuid = snapshot.options.contextId;
370+
}
368371
let uploadDomToS3 = this.ctx.config.useLambdaInternal || uploadDomToS3ViaEnv;
369372
if (!uploadDomToS3) {
370373
this.ctx.log.debug(`Uploading dom to S3 for snapshot using presigned URL for CAPS`);
@@ -375,16 +378,20 @@ export default class Queue {
375378
this.ctx.log.debug(`Uploading dom to S3 for snapshot using LSRS`);
376379
await this.ctx.client.sendDomToLSRSForCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken);
377380
}
378-
await this.ctx.client.processSnapshotCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken, discoveryErrors);
381+
await this.ctx.client.processSnapshotCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken, discoveryErrors, calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config), snapshot?.options?.sync);
379382
} else {
380-
await this.ctx.client.uploadSnapshotForCaps(this.ctx, processedSnapshot, capsBuildId, capsProjectToken, discoveryErrors);
383+
await this.ctx.client.uploadSnapshotForCaps(this.ctx, processedSnapshot, capsBuildId, capsProjectToken, discoveryErrors, calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config), snapshot?.options?.sync);
381384
}
382385

383386
// Increment snapshot count for the specific buildId
384387
const cachedCapabilities = this.ctx.sessionCapabilitiesMap.get(sessionId);
385388
const currentCount = cachedCapabilities?.snapshotCount || 0; // Get the current snapshot count for sessionId
386389
cachedCapabilities.snapshotCount = currentCount + 1; // Increment snapshot count
387390
this.ctx.sessionCapabilitiesMap.set(sessionId, cachedCapabilities);
391+
392+
if (snapshot?.options?.contextId && this.ctx.contextToSnapshotMap) {
393+
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, capsBuildId);
394+
}
388395
} else {
389396
if (!this.ctx.build?.id) {
390397
if (this.ctx.authenticatedInitially) {
@@ -440,15 +447,15 @@ export default class Queue {
440447
}
441448
}
442449
if(snapshot?.options?.contextId){
443-
this.ctx.contextToSnapshotMap?.set(snapshot?.options?.contextId,2);
450+
this.ctx.contextToSnapshotMap?.set(snapshot?.options?.contextId,'2');
444451
}
445452
this.processNext();
446453
} else {
447454
let approvalThreshold = snapshot?.options?.approvalThreshold || this.ctx.config.approvalThreshold;
448455
let rejectionThreshold = snapshot?.options?.rejectionThreshold || this.ctx.config.rejectionThreshold;
449456
await this.ctx.client.processSnapshot(this.ctx, processedSnapshot, snapshotUuid, discoveryErrors,calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config),snapshot?.options?.sync, approvalThreshold, rejectionThreshold);
450457
if(snapshot?.options?.contextId && this.ctx.contextToSnapshotMap?.has(snapshot.options.contextId)){
451-
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, 1);
458+
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, this.ctx.build.id);
452459
}
453460
this.ctx.log.debug(`ContextId: ${snapshot?.options?.contextId} status set to uploaded`);
454461
}
@@ -465,7 +472,7 @@ export default class Queue {
465472
this.ctx.log.debug(`snapshot failed; ${error}`);
466473
this.processedSnapshots.push({ name: snapshot?.name, error: error.message });
467474
if (snapshot?.options?.contextId && this.ctx.contextToSnapshotMap) {
468-
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, 2);
475+
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, '2');
469476
}
470477
}
471478
// Close open browser contexts and pages

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export interface Context {
9393
mergeBuildTargetId?: string;
9494
mergeByBranch?: boolean;
9595
mergeByBuild?: boolean;
96-
contextToSnapshotMap?: Map<string, number>;
96+
contextToSnapshotMap?: Map<string, string>;
9797
sourceCommand?: string;
9898
autoTunnelStarted?: boolean;
9999
}

0 commit comments

Comments
 (0)