-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathimage-diff-error.ts
More file actions
113 lines (102 loc) · 3.18 KB
/
Copy pathimage-diff-error.ts
File metadata and controls
113 lines (102 loc) · 3.18 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { BaseStateError } from "./base-state-error";
import type { ImageInfo, RefImageInfo } from "../../../../types";
import type { LooksSameOptions, LooksSameResult } from "looks-same";
interface DiffOptions extends LooksSameOptions {
/** Path to the current screenshot */
current: string;
reference: string;
diffColor: string;
}
type DiffAreas = Pick<LooksSameResult, "diffClusters" | "diffBounds">;
type ImageDiffErrorConstructor<T> = new (params: {
stateName: string;
currImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffAreas: DiffAreas;
diffBuffer: Buffer;
differentPixels: number;
diffRatio: number;
}) => T;
interface ImageDiffErrorData {
stateName: string;
currImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffBounds: LooksSameResult["diffBounds"];
diffClusters: LooksSameResult["diffClusters"];
diffBuffer: Buffer;
differentPixels: number;
diffRatio: number;
}
/**
* @category Errors
*/
export class ImageDiffError extends BaseStateError {
message: string;
diffOpts: DiffOptions;
diffBounds?: DiffAreas["diffBounds"];
diffClusters?: DiffAreas["diffClusters"];
diffBuffer: Buffer;
differentPixels: number;
diffRatio: number;
static create<T extends ImageDiffError>(
this: ImageDiffErrorConstructor<T>,
{
stateName,
currImg,
refImg,
diffOpts,
diffAreas = {} as DiffAreas,
diffBuffer,
differentPixels,
diffRatio,
}: {
stateName: string;
currImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffAreas?: DiffAreas;
diffBuffer: Buffer;
differentPixels: number;
diffRatio: number;
},
): T {
return new this({ stateName, currImg, refImg, diffOpts, diffAreas, diffBuffer, differentPixels, diffRatio });
}
static fromObject<T>(this: ImageDiffErrorConstructor<T>, data: ImageDiffErrorData): T {
const { diffBounds, diffClusters, ...rest } = data;
return new this({ ...rest, diffAreas: { diffBounds, diffClusters } });
}
constructor({
stateName,
currImg,
refImg,
diffOpts,
diffAreas: { diffBounds, diffClusters } = {} as DiffAreas,
diffBuffer,
differentPixels,
diffRatio,
}: {
stateName: string;
currImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffAreas?: DiffAreas;
diffBuffer: Buffer;
differentPixels: number;
diffRatio: number;
}) {
super(stateName, currImg, refImg);
this.message = `images are different for "${stateName}" state`;
this.diffOpts = diffOpts;
this.diffBounds = diffBounds;
this.diffClusters = diffClusters;
this.diffBuffer = diffBuffer;
this.differentPixels = differentPixels;
this.diffRatio = diffRatio;
}
saveDiffTo(diffPath: string): Promise<null> {
return import("../../../../image").then(m => m.Image.buildDiff({ diff: diffPath, ...this.diffOpts }));
}
}