Skip to content

Commit 450a653

Browse files
feat: implement smartui caps in cli
1 parent d97b70a commit 450a653

File tree

5 files changed

+42
-50
lines changed

5 files changed

+42
-50
lines changed

src/lib/ctx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export default (options: Record<string, string>): Context => {
134134
isSnapshotCaptured: false
135135
sessionCapabilitiesMap: new Map<string, any[]>(),
136136
sessionToBuildMap: new Map<string, string>(),
137-
buildToSnapshotCountMap: new Map<string, number>() ,
137+
buildToSnapshotCountMap: new Map<string, number>(),
138138
buildToProjectTokenMap: new Map<string, string>()
139139
}
140140
}

src/lib/httpClient.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export default class httpClient {
4747

4848

4949
this.axiosInstance.interceptors.request.use((config) => {
50-
config.headers['projectToken'] = this.projectToken;
50+
if (!config.headers['projectToken']) {
51+
config.headers['projectToken'] = this.projectToken;
52+
}
5153
config.headers['projectName'] = this.projectName;
5254
config.headers['username'] = this.username;
5355
config.headers['accessKey'] = this.accessKey;
@@ -175,18 +177,15 @@ export default class httpClient {
175177

176178

177179
getSmartUICapabilities(sessionId: string, config: any, git: any, log: Logger) {
178-
console.log(`get smartui caps api called with sessionId: ${sessionId}`);
179-
const serializedConfig = JSON.stringify(config);
180-
const serializedGit = JSON.stringify(git);
181-
console.log(`Serialized Config: ${serializedConfig}`);
182-
console.log(`Serialized Git: ${serializedGit}`);
183180
return this.request({
184181
url: '/sessions/capabilities',
185182
method: 'GET',
186183
params: {
187184
sessionId: sessionId,
188-
config: serializedConfig,
189-
git: serializedGit,
185+
},
186+
data: {
187+
git,
188+
config
190189
},
191190
headers: {
192191
projectToken: '',
@@ -208,33 +207,24 @@ export default class httpClient {
208207
}, log)
209208
}
210209

211-
async finalizeBuildForCapsWithToken(buildId: string, totalSnapshots: number, projectToken: string, log: Logger): Promise<void> {
212-
try {
213-
let params: Record<string, string | number> = { buildId };
214-
if (totalSnapshots > -1) params.totalSnapshots = totalSnapshots;
215-
216-
await this.request({
217-
url: '/build',
218-
method: 'DELETE',
219-
params: params,
220-
headers: {
221-
projectToken: projectToken, // Use projectToken dynamically
222-
},
223-
}, log);
224-
225-
log.debug(`Successfully finalized build ${buildId} with ${totalSnapshots} snapshots and projectToken ${projectToken}`);
226-
} catch (error: any) {
227-
log.debug(`Failed to finalize build ${buildId}: ${error.message}`);
228-
throw error; // Re-throw error for further handling if necessary
229-
}
210+
finalizeBuildForCapsWithToken(buildId: string, totalSnapshots: number, projectToken: string, log: Logger) {
211+
let params: Record<string, string | number> = { buildId };
212+
if (totalSnapshots > -1) params.totalSnapshots = totalSnapshots;
213+
return this.request({
214+
url: '/build',
215+
method: 'DELETE',
216+
params: params,
217+
headers: {
218+
projectToken: projectToken, // Use projectToken dynamically
219+
},
220+
}, log);
230221
}
231222

232223

233-
uploadSnapshot(ctx: Context, snapshot: ProcessedSnapshot, capsBuildId: string) {
224+
uploadSnapshot(ctx: Context, snapshot: ProcessedSnapshot) {
234225
// Use capsBuildId if provided, otherwise fallback to ctx.build.id
235-
const buildId = capsBuildId !== '' ? capsBuildId : ctx.build.id;
236226
return this.request({
237-
url: `/builds/${buildId}/snapshot`,
227+
url: `/builds/${ctx.build.id}/snapshot`,
238228
method: 'POST',
239229
headers: { 'Content-Type': 'application/json' },
240230
data: {
@@ -244,7 +234,7 @@ export default class httpClient {
244234
source: 'cli'
245235
}
246236
}
247-
}, ctx.log);
237+
}, ctx.log)
248238
}
249239

250240
processSnapshot(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string) {

src/lib/server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
4040
// Fetch sessionId from snapshot options if present
4141
const sessionId = snapshot?.options?.sessionId;
4242
let capsBuildId = ''
43-
43+
4444
if (sessionId) {
4545
// Check if sessionId exists in the map
4646
if (ctx.sessionCapabilitiesMap?.has(sessionId)) {
@@ -61,14 +61,13 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
6161

6262
if (capsBuildId) {
6363
ctx.sessionToBuildMap.set(sessionId, capsBuildId);
64-
ctx.buildToProjectTokenMap.set(capsBuildId, fetchedCapabilitiesResp.data?.projectToken || '');
64+
ctx.buildToProjectTokenMap.set(capsBuildId, fetchedCapabilitiesResp?.projectToken || '');
6565
}
6666
} catch (error: any) {
6767
console.log(`Failed to fetch capabilities for sessionId ${sessionId}: ${error.message}`);
6868
}
6969
}
7070
}
71-
console.log("I have success in retrive")
7271

7372
ctx.testType = testType;
7473
ctx.snapshotQueue?.enqueue(snapshot);

src/lib/snapshotQueue.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ export default class Queue {
304304
const cachedCapabilities = this.ctx.sessionCapabilitiesMap.get(sessionId);
305305
capsProjectToken = cachedCapabilities?.projectToken || '';
306306
capsBuildId = cachedCapabilities?.buildId || '';
307-
useCapsBuildId = !!capsBuildId; // Set to true if capsBuildId is not empty
307+
if (capsBuildId != '' && capsProjectToken != '') {
308+
useCapsBuildId = true; // Set to true if capsBuildId is not empty
309+
}
308310
}
309311

310312
// Process and upload snapshot
@@ -328,17 +330,18 @@ export default class Queue {
328330
const currentCount = this.ctx.buildToSnapshotCountMap.get(capsBuildId) || 0;
329331
this.ctx.buildToSnapshotCountMap.set(capsBuildId, currentCount + 1);
330332
} else {
331-
await this.ctx.client.uploadSnapshot(this.ctx, processedSnapshot, capsBuildId);
332-
// Increment global totalSnapshots
333+
if (!this.ctx.build?.id) {
334+
throw new Error('Build ID is empty');
335+
}
336+
await this.ctx.client.uploadSnapshot(this.ctx, processedSnapshot);
333337
this.ctx.totalSnapshots++;
334338
}
335-
336-
this.processedSnapshots.push({ name: snapshot.name, warnings });
339+
this.processedSnapshots.push({ name: snapshot?.name, warnings });
337340
}
338341

339342
} catch (error: any) {
340343
this.ctx.log.debug(`snapshot failed; ${error}`);
341-
this.processedSnapshots.push({ name: snapshot.name, error: error.message });
344+
this.processedSnapshots.push({ name: snapshot?.name, error: error.message });
342345
}
343346
// Close open browser contexts and pages
344347
if (this.ctx.browser) {

src/tasks/finalizeBuild.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
2222
throw new Error('Finalize build failed');
2323
}
2424

25-
if (ctx.buildToSnapshotCountMap) {
26-
for (const [buildId, totalSnapshots] of ctx.buildToSnapshotCountMap.entries()) {
27-
try {
28-
// Fetch projectToken from buildToProjectTokenMap
29-
const projectToken = ctx.buildToProjectTokenMap?.get(buildId) || '';
30-
await ctx.client.finalizeBuildForCapsWithToken(buildId, totalSnapshots, projectToken, ctx.log);
31-
} catch (error: any) {
32-
ctx.log.debug(`Error finalizing build ${buildId}: ${error.message}`);
33-
}
25+
for (const [buildId, totalSnapshots] of ctx.buildToSnapshotCountMap.entries()) {
26+
27+
try {
28+
// Fetch projectToken from buildToProjectTokenMap
29+
const projectToken = ctx.buildToProjectTokenMap?.get(buildId) || '';
30+
await ctx.client.finalizeBuildForCapsWithToken(buildId, totalSnapshots, projectToken, ctx.log);
31+
} catch (error: any) {
32+
ctx.log.debug(`Error finalizing build ${buildId}: ${error.message}`);
3433
}
3534
}
35+
3636

3737
task.output = chalk.gray(`build url: ${ctx.build.url}`);
3838
task.title = 'Finalized build';

0 commit comments

Comments
 (0)