-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.js
329 lines (284 loc) · 9.61 KB
/
test.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
import parksapi from './lib/index.js';
import {entityType, queueType} from './lib/parks/parkTypes.js';
import moment from 'moment-timezone';
import path from 'path';
import {promises as fs} from 'fs';
const __dirname = path.dirname(process.argv[1]);
const destination = new parksapi.destinations.LotteWorld();
const logSuccess = (...msg) => {
// print green tick
console.log(`[\x1b[32m✓\x1b[0m]`, ...msg);
}
const logError = (...msg) => {
// print red cross
console.log(`[\x1b[31m✗\x1b[0m]`, ...msg);
}
destination.on('error', (id, err, data) => {
logError(`${id}: ${err} ${JSON.stringify(data, null, 4)}`);
debugger;
});
const _requiredFields = [
'timezone',
'_id',
'name',
'entityType',
'location',
];
const requiredFields = {
[entityType.destination]: [
..._requiredFields,
'slug',
],
[entityType.park]: [
..._requiredFields,
'_parentId',
'_destinationId',
],
[entityType.attraction]: [
..._requiredFields,
//'_parkId',
'_parentId',
'_destinationId',
'attractionType',
],
[entityType.show]: [
..._requiredFields,
//'_parkId',
'_parentId',
'_destinationId',
],
[entityType.restaurant]: [
..._requiredFields,
//'_parkId',
'_parentId',
'_destinationId',
],
};
class EntityError extends Error {
constructor(message, entity) {
super(message);
this.name = 'EntityError';
this.entity = JSON.stringify(entity, null, 4);
}
}
function TestEntity(ent) {
if (!ent.entityType) {
throw new EntityError('entityType is required', ent);
}
if (!ent._id) {
throw new EntityError('_id is required', ent);
}
if (typeof ent._id !== 'string') {
throw new EntityError('_id must be a string', ent);
}
if (ent._id === 'resortId') {
throw new EntityError('resortId is default template _id, must change', ent);
}
if (ent.slug === 'resortslug') {
throw new EntityError('resortslug is default template slug, must change', ent);
}
const entityType = ent.entityType;
if (!requiredFields[entityType]) {
throw new EntityError(`Invalid entityType: ${entityType}`, ent);
}
const fields = requiredFields[entityType];
for (const field of fields) {
if (ent[field] === undefined) {
throw new EntityError(`${field} is required`, ent);
}
}
if (entityType == "DESTINATION") {
// destination must not have a parentId or destinationId
if (ent._parentId) {
throw new EntityError('destination must not have a parentId', ent);
}
if (ent._destinationId) {
throw new EntityError('destination must not have a destinationId', ent);
}
if (ent._parkId) {
throw new EntityError('destination must not have a parkId', ent);
}
}
// if our entity has a parentId, and that entity is a park, we should have a parkId too
if (entityType != "DESTINATION" && entityType != "PARK") {
if (ent._parentId) {
const parent = parkEntities.find((x) => x._id === ent._parentId);
if (parent.entityType === 'PARK') {
if (!ent._parkId) {
throw new EntityError('Entity has a park as their parent, but not assigned a _parkId', ent);
}
}
} else {
throw new EntityError('Missing parentId', ent);
}
}
}
function TestLiveData(data, ents) {
if (!data._id) {
throw new EntityError('Missing _id', data);
}
if (typeof data._id !== 'string') {
throw new EntityError('livedata _id must be a string', data);
}
if (!data.status) {
throw new EntityError('Missing status', data);
}
if (data?.queue && data?.queue[queueType.standBy]) {
if (typeof data.queue[queueType.standBy].waitTime != 'number' && data.queue[queueType.standBy].waitTime !== null) {
throw new EntityError('StandbyQueue missing waitTime number', data);
}
}
const ent = ents.find((x) => x._id === data._id);
if (!ent) {
logError(`Missing entity ${data._id} for livedata: ${JSON.stringify(data)}`);
} else {
data._name = ent.name;
}
// logSuccess(`${data._id}: ${JSON.stringify(data)}`);
}
function TestSchedule(scheduleData, entityId) {
const entSchedule = scheduleData.find((x) => x._id === entityId);
if (!entSchedule) {
throw new EntityError(`Missing schedule ${entityId}`, scheduleData);
}
if (entSchedule.schedule.length === 0) {
throw new EntityError(`Schedule ${entityId} is empty`, scheduleData);
}
for (const schedule of entSchedule.schedule) {
if (!schedule.type) {
throw new EntityError('Missing type', schedule);
}
if (!schedule.date) {
throw new EntityError('Missing date', schedule);
}
if (!schedule.openingTime) {
throw new EntityError('Missing openingTime', schedule);
}
if (!schedule.closingTime) {
throw new EntityError('Missing closingTime', schedule);
}
const open = moment(schedule.openingTime);
if (!open.isValid()) {
throw new EntityError('Invalid openingTime', schedule);
}
const close = moment(schedule.closingTime);
if (!close.isValid()) {
throw new EntityError('Invalid closingTime', schedule);
}
}
// check we have some schedule data for the next 2 months
const now = moment();
const nextMonth = moment().add(2, 'month');
let schedulesForNextMonth = 0;
let scheduleDays = 0;
for (const date = now.clone(); date.isBefore(nextMonth); date.add(1, 'day')) {
scheduleDays++;
const schedule = entSchedule.schedule.filter((x) => x.date === date.format('YYYY-MM-DD'));
if (schedule && schedule.length > 0) {
schedulesForNextMonth += schedule.length;
}
}
if (schedulesForNextMonth === 0) {
throw new EntityError(`No schedule data found for next 2 months for ${entityId}`);
}
logSuccess(`${entityId}: ${schedulesForNextMonth} schedules found for next month [${scheduleDays} days]`);
// console.log(entSchedule.schedule);
}
// track park entities for later testing
const parkEntities = [];
async function TestDestination() {
const destinations = [].concat(await destination.buildDestinationEntity());
for (const dest of destinations) {
TestEntity(dest);
}
const _parkEntities = await destination.getParkEntities();
for (const park of _parkEntities) {
parkEntities.push(park);
TestEntity(park);
}
const allEntities = await destination.getAllEntities();
for (const ent of allEntities) {
TestEntity(ent);
}
logSuccess(`${allEntities.length} entities tested`);
const entityTypes = allEntities.reduce((x, ent) => {
x[ent.entityType] = x[ent.entityType] || 0;
x[ent.entityType]++;
return x;
}, {});
Object.keys(entityTypes).forEach((x) => {
console.log(`\t${entityTypes[x]} ${x} entities`);
});
// test for entity id collisions
const entityIds = allEntities.map((x) => x._id);
const duplicateEntityIds = entityIds.filter((x, i) => entityIds.indexOf(x) !== i);
if (duplicateEntityIds.length > 0) {
logError(`${duplicateEntityIds.length} entity ids are duplicated`);
console.log(duplicateEntityIds);
} else {
logSuccess(`No entity ids are duplicated`);
}
// test for entity slug collisions
const entitySlugs = allEntities.map((x) => x.slug).filter((x) => !!x);
const duplicateEntitySlugs = entitySlugs.filter((x, i) => entitySlugs.indexOf(x) !== i);
if (duplicateEntitySlugs.length > 0) {
logError(`${duplicateEntitySlugs.length} entity slugs are duplicated`);
console.log(duplicateEntitySlugs);
} else {
logSuccess(`No entity slugs are duplicated`);
}
// sort entities by _id
allEntities.sort((a, b) => a._id.localeCompare(b._id));
// write all entities to a file
const entityDataFile = path.join(__dirname, 'testout_Entities.json');
await fs.writeFile(entityDataFile, JSON.stringify(allEntities, null, 4));
const schedule = await destination.getEntitySchedules();
// get parks
const parks = await destination.getParkEntities();
for (const park of parks) {
TestSchedule(schedule, park._id);
}
logSuccess(`${parks.length} park schedules tested`);
// write all schedule data to file
const scheduleDataFile = path.join(__dirname, 'testout_Schedules.json');
await fs.writeFile(scheduleDataFile, JSON.stringify(schedule, null, 4));
const liveData = await destination.getEntityLiveData();
// test for duplicate live data entries
const liveDataIds = liveData.map((x) => x._id);
const duplicateLiveDataIds = liveDataIds.filter((x, i) => liveDataIds.indexOf(x) !== i);
if (duplicateLiveDataIds.length > 0) {
logError(`${duplicateLiveDataIds.length} live data ids are duplicated`);
console.log(duplicateLiveDataIds);
} else {
logSuccess(`No live data ids are duplicated`);
}
for (const ent of liveData) {
TestLiveData(ent, allEntities);
}
logSuccess(`${liveData.length} live data tested`);
// write all live data to file
const liveDataFile = path.join(__dirname, 'testout_LiveData.json');
await fs.writeFile(liveDataFile, JSON.stringify(liveData, null, 4));
// TODO - test all entities, if their _parentId is a PARK, they should have a _parkId
// TODO - find all park entity IDs
// TODO - find all entities that have a park as a _parentId
// TODO - error if they don't have a _parkId matching their _parentId
// check for custom test functions
// reflect all functions in the destination object
// check for any functions starting with "unittest_"
// call each function
const customTests = Object.getOwnPropertyNames(Object.getPrototypeOf(destination)).filter((x) => x.startsWith('unittest_'));
for (const test of customTests) {
console.log(`Running custom test: ${test}`);
await destination[test](logSuccess, logError);
}
}
const run = async () => {
try {
await TestDestination();
} catch (err) {
console.error(err);
}
process.exit(0);
};
run();