-
Notifications
You must be signed in to change notification settings - Fork 607
/
engineTools.js
169 lines (155 loc) · 5.49 KB
/
engineTools.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @description Retrieves the mismatch threshold based on the given scenario and configuration.
*
* @param {Object} scenario - The scenario object, which may contain a misMatchThreshold property.
* @param {Object} config - The configuration object, which includes misMatchThreshold and defaultMisMatchThreshold properties.
* @returns {number} The mismatch threshold value.
*/
function getMisMatchThreshHold (scenario, config) {
return scenario?.misMatchThreshold || config?.misMatchThreshold || config?.defaultMisMatchThreshold || 0.1;
}
function ensureFileSuffix (filename, suffix) {
const re = new RegExp('\.' + suffix + '$', ''); // eslint-disable-line no-useless-escape
return filename.replace(re, '') + '.' + suffix;
}
// merge both strings while soft-enforcing a single slash between them
function glueStringsWithSlash (stringA, stringB) {
return stringA.replace(/\/$/, '') + '/' + stringB.replace(/^\//, '');
}
function genHash (str) {
let hash = 0;
let i;
let chr;
let len;
if (!str) return hash;
str = str.toString();
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
// return a string and replace a negative sign with a zero
return hash.toString().replace(/^-/, 0);
}
/**
* @description Determines whether the same dimensions are required based on the given scenario and configuration.
*
* @param {Object} scenario - The scenario object, which may contain a requireSameDimensions property.
* @param {Object} config - The configuration object, which includes requireSameDimensions and defaultMisMatchThreshold properties.
* @returns {boolean} True if the same dimensions are required, otherwise false.
*/
function getRequireSameDimensions (scenario, config) {
return scenario?.requireSameDimensions || config?.requireSameDimensions || config?.defaultMisMatchThreshold || true;
}
function getSelectorName (selector) {
return selector.replace(/[^a-z0-9_-]/gi, ''); // remove anything that's not a letter or a number
}
function makeSafe (str) {
return str.replace(/[ /]/g, '_');
}
function getFilename (fileNameTemplate, outputFileFormatSuffix, configId, scenarioIndex, scenarioLabelSafe, selectorIndex, selectorLabel, viewportIndex, viewportLabel) {
let fileName = fileNameTemplate
.replace(/\{configId\}/, configId)
.replace(/\{scenarioIndex\}/, scenarioIndex)
.replace(/\{scenarioLabel\}/, scenarioLabelSafe)
.replace(/\{selectorIndex\}/, selectorIndex)
.replace(/\{selectorLabel\}/, selectorLabel)
.replace(/\{viewportIndex\}/, viewportIndex)
.replace(/\{viewportLabel\}/, makeSafe(viewportLabel))
.replace(/[^a-z0-9_-]/gi, ''); // remove anything that's not a letter or a number or dash or underscore.
const extRegExp = new RegExp(outputFileFormatSuffix + '$', 'i');
if (!extRegExp.test(fileName)) {
fileName = fileName + outputFileFormatSuffix;
}
return fileName;
}
function getEngineOption (config, optionName, fallBack) {
if (typeof config.engineOptions === 'object' && config.engineOptions[optionName]) {
return config.engineOptions[optionName];
}
return fallBack;
}
function getScenarioExpect (scenario) {
let expect = 0;
if (scenario.selectorExpansion && scenario.selectors && scenario.selectors.length && scenario.expect) {
expect = scenario.expect;
}
return expect;
}
function generateTestPair (config, scenario, viewport, variantOrScenarioLabelSafe, scenarioLabelSafe, selectorIndex, selector) {
const cleanedSelectorName = getSelectorName(selector);
const fileName = getFilename(
config._fileNameTemplate,
config._outputFileFormatSuffix,
config._configId,
scenario.sIndex,
variantOrScenarioLabelSafe,
selectorIndex,
cleanedSelectorName,
viewport.vIndex,
viewport.label
);
const testFilePath = config._bitmapsTestPath + '/' + config.screenshotDateTime + '/' + fileName;
const logFileName = getFilename(
config._fileNameTemplate,
'.log.json',
config._configId,
scenario.sIndex,
variantOrScenarioLabelSafe,
selectorIndex,
cleanedSelectorName,
viewport.vIndex,
viewport.label
);
const testLogFilePath = config._bitmapsTestPath + '/' + config.screenshotDateTime + '/' + logFileName;
const referenceFilePath = config._bitmapsReferencePath + '/' + getFilename(
config._fileNameTemplate,
config._outputFileFormatSuffix,
config._configId,
scenario.sIndex,
scenarioLabelSafe,
selectorIndex,
cleanedSelectorName,
viewport.vIndex,
viewport.label
);
const referenceLogFilePath = config._bitmapsReferencePath + '/' + getFilename(
config._fileNameTemplate,
'.log.json',
config._configId,
scenario.sIndex,
scenarioLabelSafe,
selectorIndex,
cleanedSelectorName,
viewport.vIndex,
viewport.label
);
return {
reference: referenceFilePath,
referenceLog: referenceLogFilePath,
test: testFilePath,
testLog: testLogFilePath,
selector,
fileName,
label: scenario.label,
requireSameDimensions: getRequireSameDimensions(scenario, config),
misMatchThreshold: getMisMatchThreshHold(scenario, config),
url: scenario.url,
referenceUrl: scenario.referenceUrl,
expect: getScenarioExpect(scenario),
viewportLabel: viewport.label
};
}
module.exports = {
generateTestPair,
getMisMatchThreshHold,
getRequireSameDimensions,
ensureFileSuffix,
glueStringsWithSlash,
genHash,
makeSafe,
getFilename,
getEngineOption,
getSelectorName,
getScenarioExpect
};