Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lambdatest/smartui-cli",
"version": "4.1.36",
"version": "4.1.37-beta.0",
"description": "A command line interface (CLI) to run SmartUI tests on LambdaTest",
"files": [
"dist/**/*"
Expand Down
12 changes: 8 additions & 4 deletions src/lib/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export default class httpClient {
}, ctx.log)
}

processSnapshotCaps(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors) {
processSnapshotCaps(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors, variantCount: number, sync: boolean = false) {
return this.request({
url: `/build/${capsBuildId}/snapshot`,
method: 'POST',
Expand All @@ -387,17 +387,19 @@ export default class httpClient {
name: snapshot.name,
url: snapshot.url,
snapshotUuid: snapshotUuid,
variantCount: variantCount,
test: {
type: ctx.testType,
source: 'cli'
},
doRemoteDiscovery: snapshot.options.doRemoteDiscovery,
discoveryErrors: discoveryErrors,
sync: sync
}
}, ctx.log)
}

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

Expand All @@ -415,6 +417,8 @@ export default class httpClient {
source: 'cli'
},
discoveryErrors: discoveryErrors,
variantCount: variantCount,
sync: sync
}
}, ctx.log);
}
Expand Down Expand Up @@ -660,9 +664,9 @@ export default class httpClient {
}, ctx.log)
}

getSnapshotStatus(snapshotName: string, snapshotUuid: string, ctx: Context): Promise<Record<string, any>> {
getSnapshotStatus(buildId: string, snapshotName: string, snapshotUuid: string, ctx: Context): Promise<Record<string, any>> {
return this.request({
url: `/snapshot/status?buildId=${ctx.build.id}&snapshotName=${snapshotName}&snapshotUUID=${snapshotUuid}`,
url: `/snapshot/status?buildId=${buildId}&snapshotName=${snapshotName}&snapshotUUID=${snapshotUuid}`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
Expand Down
20 changes: 15 additions & 5 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
}

if (contextId && ctx.contextToSnapshotMap) {
ctx.contextToSnapshotMap.set(contextId, 0);
ctx.contextToSnapshotMap.set(contextId, '0');
ctx.log.debug(`Marking contextId as captured and added to queue: ${contextId}`);
}

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

while (contextStatus==0) {
let counter= 60;
while (contextStatus==='0') {
if(counter<=0){
throw new Error('Snapshot processing failed');
}
contextStatus = ctx.contextToSnapshotMap.get(contextId);
// Wait 5 seconds before next check
await new Promise(resolve => setTimeout(resolve, 5000));

contextStatus = ctx.contextToSnapshotMap.get(contextId);
counter--;
}

if(contextStatus==2){
if(contextStatus==='2'){
throw new Error("Snapshot Failed");
}

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

const buildId = contextStatus;
if (!buildId) {
throw new Error(`No buildId found for contextId: ${contextId}`);
}

// Poll external API until it returns 200 or timeout is reached
let lastExternalResponse: any = null;
const startTime = Date.now();

while (true) {
try {
const externalResponse = await ctx.client.getSnapshotStatus(
buildId,
snapshotName,
contextId,
ctx
Expand Down
19 changes: 13 additions & 6 deletions src/lib/snapshotQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ export default class Queue {
if (useCapsBuildId) {
this.ctx.log.info(`Using cached buildId: ${capsBuildId}`);
if (useKafkaFlowCaps) {
const snapshotUuid = uuidv4();
let snapshotUuid = uuidv4();
if (snapshot?.options?.contextId && this.ctx.contextToSnapshotMap?.has(snapshot.options.contextId)) {
snapshotUuid = snapshot.options.contextId;
}
let uploadDomToS3 = this.ctx.config.useLambdaInternal || uploadDomToS3ViaEnv;
if (!uploadDomToS3) {
this.ctx.log.debug(`Uploading dom to S3 for snapshot using presigned URL for CAPS`);
Expand All @@ -375,16 +378,20 @@ export default class Queue {
this.ctx.log.debug(`Uploading dom to S3 for snapshot using LSRS`);
await this.ctx.client.sendDomToLSRSForCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken);
}
await this.ctx.client.processSnapshotCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken, discoveryErrors);
await this.ctx.client.processSnapshotCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken, discoveryErrors, calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config), snapshot?.options?.sync);
} else {
await this.ctx.client.uploadSnapshotForCaps(this.ctx, processedSnapshot, capsBuildId, capsProjectToken, discoveryErrors);
await this.ctx.client.uploadSnapshotForCaps(this.ctx, processedSnapshot, capsBuildId, capsProjectToken, discoveryErrors, calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config), snapshot?.options?.sync);
}

// Increment snapshot count for the specific buildId
const cachedCapabilities = this.ctx.sessionCapabilitiesMap.get(sessionId);
const currentCount = cachedCapabilities?.snapshotCount || 0; // Get the current snapshot count for sessionId
cachedCapabilities.snapshotCount = currentCount + 1; // Increment snapshot count
this.ctx.sessionCapabilitiesMap.set(sessionId, cachedCapabilities);

if (snapshot?.options?.contextId && this.ctx.contextToSnapshotMap) {
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, capsBuildId);
}
} else {
if (!this.ctx.build?.id) {
if (this.ctx.authenticatedInitially) {
Expand Down Expand Up @@ -440,15 +447,15 @@ export default class Queue {
}
}
if(snapshot?.options?.contextId){
this.ctx.contextToSnapshotMap?.set(snapshot?.options?.contextId,2);
this.ctx.contextToSnapshotMap?.set(snapshot?.options?.contextId,'2');
}
this.processNext();
} else {
let approvalThreshold = snapshot?.options?.approvalThreshold || this.ctx.config.approvalThreshold;
let rejectionThreshold = snapshot?.options?.rejectionThreshold || this.ctx.config.rejectionThreshold;
await this.ctx.client.processSnapshot(this.ctx, processedSnapshot, snapshotUuid, discoveryErrors,calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config),snapshot?.options?.sync, approvalThreshold, rejectionThreshold);
if(snapshot?.options?.contextId && this.ctx.contextToSnapshotMap?.has(snapshot.options.contextId)){
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, 1);
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, this.ctx.build.id);
}
this.ctx.log.debug(`ContextId: ${snapshot?.options?.contextId} status set to uploaded`);
}
Expand All @@ -465,7 +472,7 @@ export default class Queue {
this.ctx.log.debug(`snapshot failed; ${error}`);
this.processedSnapshots.push({ name: snapshot?.name, error: error.message });
if (snapshot?.options?.contextId && this.ctx.contextToSnapshotMap) {
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, 2);
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, '2');
}
}
// Close open browser contexts and pages
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface Context {
mergeBuildTargetId?: string;
mergeByBranch?: boolean;
mergeByBuild?: boolean;
contextToSnapshotMap?: Map<string, number>;
contextToSnapshotMap?: Map<string, string>;
sourceCommand?: string;
autoTunnelStarted?: boolean;
}
Expand Down