/
scenario-utils.js
68 lines (56 loc) · 1.97 KB
/
scenario-utils.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
// From https://stackoverflow.com/a/12646864
// Note that the array is mutated
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function getRandomPath(object) {
const path = [];
let currentObject = object;
while (typeof currentObject === 'object') {
const keys = Object.keys(currentObject);
if (!keys.length) {
// Don't go into empty objects
break;
}
const key = keys[Math.floor(Math.random() * keys.length)];
path.push(key);
currentObject = currentObject[key];
}
return path;
}
// To avoid repeating code between the different scenarios, these functions will generate a bunch of paths exist /
// don't exist on the given object. Each path is returned as an array of strings, which the caller will needs to format
// into whatever structure they're supposed to test.
function generateExistingPaths(object, numExistingPaths) {
const paths = [];
for (let i = 0; i < numExistingPaths; i++) {
paths.push(getRandomPath(object));
}
return paths;
}
function generateMissingPaths(object, numMissingPaths) {
const paths = [];
for (let i = 0; i < numMissingPaths; i++) {
// Start with a valid path...
const path = getRandomPath(object);
// And change one of the keys to something that [almost certainly] doesn't exist
const indexToChange = Math.floor(Math.random() * path.length);
path.splice(indexToChange, 1, `rand${Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)}`);
paths.push(path);
}
return paths;
}
function generatePaths(object, numExistingPaths, numMissingPaths) {
const existingPaths = generateExistingPaths(object, numExistingPaths);
const missingPaths = generateMissingPaths(object, numMissingPaths);
return shuffleArray([...existingPaths, ...missingPaths]);
}
module.exports = {
generateExistingPaths,
generateMissingPaths,
generatePaths,
};