Skip to content

Commit db96d96

Browse files
fetchresults support
1 parent 4f83ff8 commit db96d96

File tree

8 files changed

+50
-20
lines changed

8 files changed

+50
-20
lines changed

src/lib/ctx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default (options: Record<string, string>): Context => {
5353
process.exit(1);
5454
}
5555
fetchResultObj = true
56-
fetchResultsFileObj = options.fetchResults === true ? 'results.json' : options.fetchResults;
56+
fetchResultsFileObj = options.fetchResults === true ? '' : options.fetchResults;
5757
} else {
5858
fetchResultObj = false
5959
fetchResultsFileObj = ''

src/lib/httpClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,12 @@ export default class httpClient {
157157
}, log)
158158
}
159159

160-
getScreenshotData(buildId: string, baseline: boolean, log: Logger) {
160+
getScreenshotData(buildId: string, baseline: boolean, log: Logger, projectToken: string) {
161161
return this.request({
162162
url: '/screenshot',
163163
method: 'GET',
164-
params: { buildId, baseline }
164+
params: { buildId, baseline },
165+
headers: {projectToken: projectToken}
165166
}, log);
166167
}
167168

src/lib/processSnapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
278278
}
279279
}
280280

281-
// process for every viewport
281+
// process for every viewport
282282
let navigated: boolean = false;
283283
let previousDeviceType: string | null = null;
284284

src/lib/server.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Server, IncomingMessage, ServerResponse } from 'http';
22
import path from 'path';
33
import fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify';
4-
import { readFileSync } from 'fs'
4+
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';
89

910
export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMessage, ServerResponse>> => {
1011

@@ -56,12 +57,20 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
5657
try {
5758
let fetchedCapabilitiesResp = await ctx.client.getSmartUICapabilities(sessionId, ctx.config, ctx.git, ctx.log);
5859
capsBuildId = fetchedCapabilitiesResp?.buildId || ''
59-
console.log(JSON.stringify(fetchedCapabilitiesResp))
6060

6161
if (capsBuildId) {
6262
ctx.sessionCapabilitiesMap.set(sessionId, fetchedCapabilitiesResp);
6363
ctx.sessionToBuildMap.set(sessionId, capsBuildId);
6464
ctx.buildToProjectTokenMap.set(capsBuildId, fetchedCapabilitiesResp?.projectToken || '');
65+
if (ctx.options.fetchResults) {
66+
let is_baseline;
67+
if (fetchedCapabilitiesResp.baseline) {
68+
is_baseline = true;
69+
} else {
70+
is_baseline = false
71+
}
72+
startPolling(ctx, capsBuildId, is_baseline, fetchedCapabilitiesResp.projectToken)
73+
}
6574
}
6675
} catch (error: any) {
6776
// console.log(`Failed to fetch capabilities for sessionId ${sessionId}: ${error.message}`);

src/lib/utils.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import axios from 'axios';
88
import { globalAgent } from 'http';
99
import { promisify } from 'util'
1010
const sleep = promisify(setTimeout);
11+
import { build } from 'tsup';
1112

1213
let isPollingActive = false;
1314
let globalContext: Context;
@@ -227,9 +228,12 @@ process.on('SIGINT', async () => {
227228
});
228229

229230
// Background polling function
230-
export async function startPolling(ctx: Context): Promise<void> {
231-
ctx.log.info('Fetching results in progress....');
232-
ctx.log.debug(ctx.build);
231+
export async function startPolling(ctx: Context, build_id: string, baseline: boolean, projectToken: string): Promise<void> {
232+
if (build_id) {
233+
ctx.log.info(`Fetching results for buildId ${build_id} in progress....`);
234+
} else if (ctx.build && ctx.build.id) {
235+
ctx.log.info(`Fetching results for buildId ${ctx.build.id} in progress....`);
236+
}
233237
isPollingActive = true;
234238

235239
const intervalId = setInterval(async () => {
@@ -239,21 +243,35 @@ export async function startPolling(ctx: Context): Promise<void> {
239243
}
240244

241245
try {
242-
const resp = await ctx.client.getScreenshotData(ctx.build.id, ctx.build.baseline || false, ctx.log);
246+
let resp;
247+
let filename;
248+
if (build_id) {
249+
resp = await ctx.client.getScreenshotData(build_id, baseline, ctx.log, projectToken);
250+
filename = `${build_id}.json`
251+
} else if (ctx.build && ctx.build.id) {
252+
resp = await ctx.client.getScreenshotData(ctx.build.id, ctx.build.baseline, ctx.log, '');
253+
if (ctx.options.fetchResultsFileName) {
254+
filename = ctx.options.fetchResultsFileName
255+
} else {
256+
filename = `${ctx.build.id}.json`
257+
}
258+
} else {
259+
return;
260+
}
243261

244262
if (!resp.build) {
245263
ctx.log.info("Error: Build data is null.");
246264
clearInterval(intervalId);
247-
isPollingActive = false;
265+
// isPollingActive = false;
248266
}
249267

250-
fs.writeFileSync(ctx.options.fetchResultsFileName, JSON.stringify(resp, null, 2));
251-
ctx.log.debug(`Updated results in ${ctx.options.fetchResultsFileName}`);
268+
fs.writeFileSync(filename, JSON.stringify(resp, null, 2));
269+
ctx.log.debug(`Updated results in ${filename}`);
252270

253271
if (resp.build.build_status_ind === constants.BUILD_COMPLETE || resp.build.build_status_ind === constants.BUILD_ERROR) {
254272
clearInterval(intervalId);
255-
ctx.log.info(`Fetching results completed. Final results written to ${ctx.options.fetchResultsFileName}`);
256-
isPollingActive = false;
273+
ctx.log.info(`Fetching results completed. Final results written to ${filename}`);
274+
// isPollingActive = false;
257275

258276

259277
// Evaluating Summary
@@ -284,7 +302,7 @@ export async function startPolling(ctx: Context): Promise<void> {
284302
// Display summary
285303
ctx.log.info(
286304
chalk.green.bold(
287-
`\nSummary of Mismatches:\n` +
305+
`\nSummary of Mismatches for buildId: ${build_id}\n` +
288306
`${chalk.yellow('Total Variants with Mismatches:')} ${chalk.white(totalVariantsWithMismatches)} out of ${chalk.white(totalVariants)}\n` +
289307
`${chalk.yellow('Total Screenshots with Mismatches:')} ${chalk.white(totalScreenshotsWithMismatches)} out of ${chalk.white(totalScreenshots)}\n` +
290308
`${chalk.yellow('Branch Name:')} ${chalk.white(resp.build.branch)}\n` +
@@ -301,7 +319,7 @@ export async function startPolling(ctx: Context): Promise<void> {
301319
ctx.log.error(`Error fetching screenshot data: ${error.message}`);
302320
}
303321
clearInterval(intervalId);
304-
isPollingActive = false;
322+
// isPollingActive = false;
305323
}
306324
}, 5000);
307325
}

src/tasks/captureScreenshots.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1212
try {
1313
ctx.task = task;
1414
if (ctx.options.fetchResults) {
15-
startPolling(ctx);
15+
startPolling(ctx, '', false, '');
1616
}
1717
updateLogContext({task: 'capture'});
1818

src/tasks/exec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1111
task: async (ctx, task): Promise<void> => {
1212

1313
if (ctx.options.fetchResults) {
14-
startPolling(ctx);
14+
if (ctx.build && ctx.build.id) {
15+
startPolling(ctx, '', false, '');
16+
}
1517
}
1618

1719
updateLogContext({task: 'exec'});

src/tasks/uploadScreenshots.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1212
try {
1313
ctx.task = task;
1414
if (ctx.options.fetchResults) {
15-
startPolling(ctx);
15+
startPolling(ctx, '', false, '');
1616
}
1717
updateLogContext({ task: 'upload' });
1818

0 commit comments

Comments
 (0)