-
Notifications
You must be signed in to change notification settings - Fork 93
/
common.js
384 lines (340 loc) · 12.8 KB
/
common.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
const _ = require("lodash");
const fs = require("fs");
const net = require('net');
const {
PYTHAGORA_METADATA_DIR,
METADATA_FILENAME,
PYTHAGORA_TESTS_DIR,
PYTHAGORA_DELIMITER
} = require("@pythagora.io/js-code-processing").common;
const path = require('path');
const args = require('./getArgs.js');
let mongodb;
// this is needed so that "mongodb" is not required before mongo patches are applied
const ObjectId = class {
constructor(id) {
if (!mongodb) mongodb = require("mongodb");
return new mongodb.ObjectId(id);
}
static isValid(value) {
if (!mongodb) mongodb = require("mongodb");
return mongodb.ObjectId.isValid(value);
}
};
const objectIdAsStringRegex = /^ObjectId\("([0-9a-fA-F]{24})"\)$/;
const dateAsStringRegex = /^Date\("(.+)"\)$/;
const iso8601DateRegex = /^(\d{4})-(\d{2})-(\d{2})(T(\d{2}):(\d{2}):(\d{2})(\.(\d{1,3}))?(Z|([+\-])(\d{2}):(\d{2})))?$/;
const regExpRegex = /^RegExp\("(.*)"\)$/;
const mongoIdRegex = /^[0-9a-fA-F]{24}$/;
const isObjectId = (value) => {
try {
return (!value || !value.toString || Array.isArray(value)) ? false :
(
value.constructor.name === 'ObjectId' ||
value.constructor.name === 'ObjectID' ||
mongoIdRegex.test(value.toString()) ||
objectIdAsStringRegex.test(value.toString())
);
} catch (e) {
return false;
}
}
const isLegacyObjectId = (value) => {
return !value || !value.constructor ? false : value.constructor.name === 'ObjectID' && isJSONObject(value.id);
}
const cutWithDots = (string, cutAtChar = 100) => {
if (cutAtChar >= string.length) return string;
return (string && string.length > cutAtChar) ? string.slice(0, cutAtChar) + '...' : string;
}
function parseString(s) {
if (typeof s === 'string') {
try {
let tempS = JSON.parse(s);
if (typeof tempS === 'object' || typeof tempS === 'boolean') {
s = tempS;
}
} catch (e) {
//dummy catch
}
}
return s
}
function compareResponse(a, b) {
a = parseString(a);
b = parseString(b);
return typeof a !== typeof b ? false :
typeof a === 'string' && a.toLowerCase().includes('<!doctype html>') && b.toLowerCase().includes('<!doctype html>') ? true : //todo make appropriate check
typeof a === 'object' && typeof b === 'object' ? compareJson(a,b) : a === b;
}
function isDate(date) {
return (typeof date === 'string' && iso8601DateRegex.exec(date) && (new Date(date) !== "Invalid Date") && !isNaN(new Date(date))) ||
(typeof date === 'string' && dateAsStringRegex.exec(date)) ||
(typeof date === 'object' && date instanceof Date);
}
function stringToDate(str) {
const match = dateAsStringRegex.exec(str);
if (match) {
const dateString = match[1];
const date = new Date(dateString);
if (!isNaN(date.getTime())) {
return date;
}
}
return str;
}
function isJSONObject(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
function compareJson(a, b, strict) {
if (a === b) return true;
if (typeof a !== typeof b) return false;
if ((!a && a !== 0) || (!b && b !== 0)) return false;
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false;
return a.every((itemA) => b.some((itemB) => compareJson(itemA, itemB, strict))) &&
b.every((itemB) => a.some((itemA) => compareJson(itemA, itemB, strict)));
}
if (typeof a === 'string' && typeof b === 'string') {
return objectIdAsStringRegex.test(a) && objectIdAsStringRegex.test(b) && !strict ? true : a === b;
}
if (typeof a === 'object' && typeof b === 'object') {
let ignoreKeys = ['stacktrace'];
let aProps = Object.getOwnPropertyNames(a);
let bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) {
return false;
}
for (let i = 0; i < aProps.length; i++) {
let propName = aProps[i];
if (ignoreKeys.includes(propName)) continue;
if (isObjectId(a[propName]) && isObjectId(b[propName])) continue;
// Perform deep comparison first
if (typeof a[propName] === 'object') {
if (!compareJson(a[propName], b[propName], strict))
return false;
} else if (a[propName] !== b[propName]) return false; // If a[propName] and b[propName] are not objects, use !== for comparison
}
return true;
}
// If a and b are neither arrays nor objects, use !== for comparison
return a !== b ? false : true;
}
function compareJsonDetailed(a, b, strict) {
let differencesCapture = {};
let differencesTest = {};
if (a === b) {
return { capture: differencesCapture, test: differencesTest };
} else if (typeof a !== typeof b) {
differencesCapture.type = { a: typeof a, b: typeof b };
differencesTest.type = { a: typeof a, b: typeof b };
} else if (!a || !b) {
differencesCapture.missing = !!a;
differencesTest.missing = !!b;
} else if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
differencesCapture.length = a.length;
differencesTest.length = b.length;
}
// TODO optimize because this is O(n^2)
for (let i = 0; i < a.length; i++) {
const itemDifferences = compareJsonDetailed(a[i], b[i], strict);
if (Object.keys(itemDifferences.capture).length > 0) {
differencesCapture[`[${i}]`] = itemDifferences.capture;
}
if (Object.keys(itemDifferences.test).length > 0) {
differencesTest[`[${i}]`] = itemDifferences.test;
}
}
} else if (typeof a === 'string' && typeof b === 'string') {
if (objectIdAsStringRegex.test(a) && objectIdAsStringRegex.test(b) && !strict) {
return { capture: differencesCapture, test: differencesTest };
} else if (a !== b) {
differencesCapture.value = a;
differencesTest.value = b;
}
} else {
let ignoreKeys = ['stacktrace'];
let aProps = Object.getOwnPropertyNames(a);
let bProps = Object.getOwnPropertyNames(b);
if (aProps.length === 0 && bProps.length === 0 && a !== b) {
differencesCapture.value = a;
differencesTest.value = b;
}
if (aProps.length !== bProps.length) {
differencesCapture.propsLength = aProps.length;
differencesTest.propsLength = bProps.length;
}
for (let i = 0; i < aProps.length; i++) {
let propName = aProps[i];
if (
a[propName] !== b[propName] &&
(!isDate(a[propName]) && !isDate(b[propName])) &&
!ignoreKeys.includes(propName) &&
!(isObjectId(a[propName]) && isObjectId(b[propName]))
) {
if (typeof a[propName] === 'object') {
const propDifferences = compareJsonDetailed(a[propName], b[propName], strict);
if (Object.keys(propDifferences.capture).length > 0) {
differencesCapture[propName] = propDifferences.capture;
}
if (Object.keys(propDifferences.test).length > 0) {
differencesTest[propName] = propDifferences.test;
}
} else {
differencesCapture[propName] = a[propName];
differencesTest[propName] = b[propName];
}
}
}
}
return { capture: differencesCapture, test: differencesTest };
}
function noUndefined(value, replaceValue = {}) {
return value === undefined || value === null ? replaceValue : value;
}
function stringToRegExp(str) {
let idValue = str.match(regExpRegex);
if (idValue && idValue[1]) {
const [, pattern, flags] = idValue[1].match(/^\/(.*)\/([gimuy]+)?$/);
return new RegExp(pattern, flags);
}
return str;
}
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
// Handle the specific types after checking for circular references
if (isLegacyObjectId(value)) value = (new ObjectId(Buffer.from(value.id.data))).toString();
else if (value instanceof RegExp) value = `RegExp("${value.toString()}")`;
else if (Array.isArray(value) && value.find(v => isLegacyObjectId(v))) {
value = value.map(v => isLegacyObjectId(v) ? (new ObjectId(Buffer.from(v.id.data))).toString() : v);
}
return value;
};
};
function getOccurrenceInArray(array, value) {
return array.filter((v) => (v === value)).length;
}
function convertToRegularObject(obj) {
if (obj === null) return obj;
const reviver = (key, value) => {
if (typeof value === 'string') {
if (value.length === 24 && mongoIdRegex.test(value)) return new ObjectId(value);
else if (iso8601DateRegex.test(value)) return new Date(value);
else if (regExpRegex.test(value)) return stringToRegExp(value);
}
return value;
}
let noUndObj = noUndefined(obj);
let stringified = JSON.stringify(noUndObj.toObject ? noUndObj.toObject() : noUndObj, getCircularReplacer());
return JSON.parse(stringified, reviver);
}
function getMetadata() {
let metadata = fs.readFileSync(path.resolve(args.pythagora_root, PYTHAGORA_METADATA_DIR, METADATA_FILENAME));
metadata = JSON.parse(metadata);
return metadata;
}
function delay(ms) {
return new Promise((resolve, reject) => {
if (ms < 0) throw new Error('Delay must be a positive number');
setTimeout(resolve, ms)
});
}
async function isPortTaken(port) {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.once('error', (err) => {
if (err.code === 'EADDRINUSE') {
resolve(true);
} else {
reject(err);
}
});
server.once('listening', () => {
server.close();
resolve(false);
});
server.listen(port);
});
}
async function getFreePortInRange(minPort, maxPort) {
let listenPort = 0;
while (!listenPort) {
listenPort = Math.floor(Math.random() * (maxPort - minPort + 1)) + minPort;
if (await isPortTaken(listenPort)) {
// console.log(`Port ${listenPort} is already in use`);
listenPort = 0;
} else {
// console.log(`Using port ${listenPort}`);
}
}
return listenPort;
}
function getAllGeneratedTests() {
let allTests = [];
let files = fs.readdirSync(path.resolve(args.pythagora_root, PYTHAGORA_TESTS_DIR));
files = files.filter(f => f[0] !== '.' && f.indexOf(PYTHAGORA_DELIMITER) === 0);
for (let file of files) {
let tests = JSON.parse(fs.readFileSync(path.resolve(args.pythagora_root, PYTHAGORA_TESTS_DIR, file)));
allTests = allTests.concat(tests);
}
return allTests;
}
function insertVariablesInText(text, variables) {
let variableNames = Object.keys(variables);
for (let variableName of variableNames) {
let variableValue = typeof variables[variableName] === 'object' ?
JSON.stringify(variables[variableName], null, 2) : variables[variableName];
let variableRegex = new RegExp(`{{${variableName}}}`, 'g');
text = text.replace(variableRegex, variableValue);
}
return text;
}
function updateMetadata(newMetadata) {
const dirPath = path.resolve(args.pythagora_root, PYTHAGORA_METADATA_DIR);
const filePath = path.join(dirPath, METADATA_FILENAME);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
fs.writeFileSync(filePath, JSON.stringify(newMetadata, getCircularReplacer(), 2));
}
function comparePaths(path1, path2) {
// Remove any leading or trailing slashes from both paths
path1 = path1.replace(/^\/+|\/+$/g, '');
path2 = path2.replace(/^\/+|\/+$/g, '');
return path1 === path2;
}
module.exports = {
compareJson,
compareJsonDetailed,
comparePaths,
compareResponse,
convertToRegularObject,
cutWithDots,
dateAsStringRegex,
delay,
getAllGeneratedTests,
getCircularReplacer,
getFreePortInRange,
getMetadata,
getOccurrenceInArray,
insertVariablesInText,
isJSONObject,
isLegacyObjectId,
isObjectId,
isPortTaken,
mongoIdRegex,
noUndefined,
ObjectId,
objectIdAsStringRegex,
regExpRegex,
stringToDate,
stringToRegExp,
updateMetadata,
}