-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisual-snapshots-api.ts
86 lines (73 loc) · 2.01 KB
/
visual-snapshots-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { DiffingMethod, VisualApi } from "@saucelabs/visual";
export interface CreateVisualSnapshotsParams {
branch: string;
buildName: string;
defaultBranch: string;
project: string;
customId: string;
buildId: string;
}
export class VisualSnapshotsApi {
private api: VisualApi;
constructor(api: VisualApi) {
this.api = api;
}
public async generateAndSendPdfFilSnapshots(
pdfFilePages: AsyncGenerator<Buffer>,
params: CreateVisualSnapshotsParams,
) {
const buildId = await this.createBuild(params);
let pageNumber = 1;
for await (const pdfPageImage of pdfFilePages) {
await this.uploadImageAndCreateSnapshot(
pdfPageImage,
buildId,
pageNumber,
);
pageNumber++;
}
await this.finishBuild(buildId);
}
private async createBuild(
params: CreateVisualSnapshotsParams,
): Promise<string> {
const build = await this.api.createBuild({
name: params.buildName,
branch: params.branch,
defaultBranch: params.defaultBranch,
project: params.project,
customId: params.customId,
});
console.info(`Build ${build.id} created.`);
return build.id;
}
private async uploadImageAndCreateSnapshot(
snapshot: Buffer,
buildId: string,
snapshotId: number,
) {
const uploadId = await this.api.uploadSnapshot({
buildId,
image: { data: snapshot },
});
console.info(`Uploaded image to build ${buildId}: upload id=${uploadId}.`);
const snapshotName = `page-${snapshotId}`;
const snapshotMetadata = {
diffingMethod: DiffingMethod.Balanced,
buildUuid: buildId,
name: snapshotName,
};
await this.api.createSnapshot({
...snapshotMetadata,
buildUuid: buildId,
uploadUuid: uploadId,
});
console.info(`Created a snapshot ${snapshotName} for build ${buildId}.`);
}
private async finishBuild(buildId: string) {
await this.api.finishBuild({
uuid: buildId,
});
console.info(`Build ${buildId} finished.`);
}
}