-
Notifications
You must be signed in to change notification settings - Fork 79
/
job-validator.js
515 lines (446 loc) · 11.4 KB
/
job-validator.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
const path = require('node:path');
const test = require('ava');
const later = require('@breejs/later');
const jobValidator = require('../src/job-validator');
const root = path.join(__dirname, 'jobs');
const baseConfig = {
root,
defaultExtension: 'js',
acceptedExtensions: ['.js', '.mjs']
};
test('does not throw for valid object job', async (t) => {
await t.notThrowsAsync(
jobValidator({ name: 'basic' }, 0, ['exists'], baseConfig)
);
});
test('does not throw for valid object job with extension on name', async (t) => {
await t.notThrowsAsync(
jobValidator({ name: 'basic.js' }, 0, ['exists'], baseConfig)
);
});
test('does not throw for valid string job', async (t) => {
await t.notThrowsAsync(jobValidator('basic', 1, ['exists'], baseConfig));
});
test('does not throw for valid string job with extension', async (t) => {
await t.notThrowsAsync(jobValidator('basic.js', 1, ['exists'], baseConfig));
});
test('throws for non-unique job name', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{
name: 'exists'
},
0,
['exists'],
{
root: false
}
)
);
t.regex(err.message, /Job #1 has a duplicate job name of exists/);
});
test('constructs default cronValidate configuration', (t) => {
const returned = jobValidator.cronValidateWithSeconds({ name: 'hello' }, {});
const expected = {
override: {
useSeconds: true
},
preset: 'default'
};
t.deepEqual(returned, expected);
});
// TODO: this can be improved
test("prefers job's cronValidate configuration with validateCron", (t) => {
const job = {
hasSeconds: false,
cronValidate: {
preset: 'custom'
}
};
const config = {
cronValidate: {
preset: 'global'
}
};
const returned = jobValidator.validateCron(job, 'Test prefix', config);
const expected = [
new Error(
'Test prefix had an invalid cron pattern: Option preset custom does not exist.'
)
];
t.deepEqual(returned, expected);
});
test("prefers job's cronValidate configuration", (t) => {
const job = {
cronValidate: {
preset: 'custom'
}
};
const config = {
cronValidate: {
preset: 'global'
}
};
const returned = jobValidator.cronValidateWithSeconds(job, config);
const expected = {
override: {
useSeconds: true
},
preset: 'custom'
};
t.deepEqual(returned, expected);
});
test("prefers config's cronValidate if none in job configuration", (t) => {
const job = {};
const config = {
cronValidate: {
preset: 'config-preset'
}
};
const returned = jobValidator.cronValidateWithSeconds(job, config);
const expected = {
override: {
useSeconds: true
},
preset: 'config-preset'
};
t.deepEqual(returned, expected);
});
test("uses confg's override cronValidate if none in job configuration", (t) => {
const job = {};
const config = {
cronValidate: {
override: {
useSeconds: true
}
}
};
const returned = jobValidator.cronValidateWithSeconds(job, config);
const expected = {
override: {
useSeconds: true
},
preset: 'default'
};
t.deepEqual(returned, expected);
});
test("prefers job's override cronValidate if none in job configuration", (t) => {
const job = {
cronValidate: {
override: {
useSeconds: true
}
}
};
const config = {
cronValidate: {
override: {
useSeconds: false
}
}
};
const returned = jobValidator.cronValidateWithSeconds(job, config);
const expected = {
override: {
useSeconds: true
},
preset: 'default'
};
t.deepEqual(returned, expected);
});
test('throws for reserved job.name', async (t) => {
const err = await t.throwsAsync(
jobValidator({ name: 'index' }, 0, ['exists'], baseConfig)
);
t.is(
err.message,
'You cannot use the reserved job name of "index", "index.js", nor "index.mjs"'
);
});
test('throws for reserved job name', async (t) => {
const err = await t.throwsAsync(
jobValidator('index', 0, ['exists'], baseConfig)
);
t.is(
err.message,
'You cannot use the reserved job name of "index", "index.js", nor "index.mjs"'
);
});
test('throws for string job if no root directory', async (t) => {
const err = await t.throwsAsync(
jobValidator('basic', 0, ['exists'], {
defaultExtension: 'js'
})
);
t.is(
err.message,
'Job #1 "basic" requires root directory option to auto-populate path'
);
});
test('throws for object job if no root directory', async (t) => {
const err = await t.throwsAsync(
jobValidator({ name: 'basic' }, 0, ['exists'], {
defaultExtension: 'js'
})
);
t.is(
err.message,
'Job #1 named "basic" requires root directory option to auto-populate path'
);
});
test('does not throw for valid job path', (t) => {
t.notThrows(() =>
jobValidator(
{ name: 'basic', path: root + '/basic.js' },
0,
['exists'],
baseConfig
)
);
});
test('does not throw for path without extension', (t) => {
t.notThrows(() => jobValidator({ name: 'basic' }, 0, ['exists'], baseConfig));
});
test('does not throw for valid function', (t) => {
const fn = () => {
return true;
};
t.notThrows(() => jobValidator(fn, 1, ['exists'], baseConfig));
});
test('throws for bound function', async (t) => {
const fn = () => {
return true;
};
const boundFn = fn.bind(this);
const err = await t.throwsAsync(
jobValidator(boundFn, 1, ['exists'], baseConfig)
);
t.is(err.message, "Job #2 can't be a bound or built-in function");
});
test('does not throw for valid function in job.path', async (t) => {
const fn = () => {
return true;
};
await t.notThrowsAsync(
jobValidator({ path: fn, name: 'fn' }, 1, ['exists'], baseConfig)
);
});
test('throws for bound function in job.path', async (t) => {
const fn = () => {
return true;
};
const boundFn = fn.bind(this);
const err = await t.throwsAsync(
jobValidator({ path: boundFn, name: 'fn' }, 1, ['exists'], baseConfig)
);
t.is(err.message, 'Job #2 named "fn" can\'t be a bound or built-in function');
});
test('does not throw for valid cron without seconds', async (t) => {
await t.notThrowsAsync(
jobValidator(
{ name: 'basic', cron: '* * * * *' },
0,
['exists'],
baseConfig
)
);
});
test('does not throw for valid cron with "L" in day', async (t) => {
await t.notThrowsAsync(
jobValidator({ name: 'basic', cron: '* * L * *' }, 0, ['exists'], {
root,
defaultExtension: 'js',
acceptedExtensions: ['.js', '.mjs'],
cronValidate: {
override: {
useLastDayOfMonth: true
}
}
})
);
});
test('does not throw for valid cron with seconds', async (t) => {
await t.notThrowsAsync(
jobValidator(
{ name: 'basic', cron: '* * * * * *', hasSeconds: true },
0,
['exists'],
baseConfig
)
);
});
test('does not throw for valid cron that is a schedule', async (t) => {
await t.notThrowsAsync(
jobValidator(
{ name: 'basic', cron: later.parse.cron('* * * * *') },
0,
['exists'],
baseConfig
)
);
});
test('throws for invalid cron expression', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', cron: '* * * * * *' },
0,
['exists'],
baseConfig
)
);
t.is(
err.message,
'Job #1 named "basic" had an invalid cron pattern: Expected 5 values, but got 6. (Input cron: \'* * * * * *\')'
);
});
test('throws if no no name exists', async (t) => {
const err = await t.throwsAsync(jobValidator({}, 0, ['exists'], baseConfig));
t.is(err.message, 'Job #1 is missing a name');
});
test('throws if both interval and cron are used', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', cron: '* * * * *', interval: 60 },
0,
['exists'],
baseConfig
)
);
t.is(
err.message,
'Job #1 named "basic" cannot have both interval and cron configuration'
);
});
test('throws if both timeout and date are used', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', timeout: 60, date: new Date('12/30/2020') },
0,
['exists'],
baseConfig
)
);
t.is(err.message, 'Job #1 named "basic" cannot have both timeout and date');
});
test('throws if date is not a Date object', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', date: '12/23/2020' },
0,
['exists'],
baseConfig
)
);
t.is(err.message, 'Job #1 named "basic" had an invalid Date of 12/23/2020');
});
test('throws if timeout is invalid', async (t) => {
const err = await t.throwsAsync(
jobValidator({ name: 'basic', timeout: -1 }, 0, ['exists'], baseConfig)
);
t.regex(err.message, /Job #1 named "basic" had an invalid timeout of -1; */);
});
test('throws if interval is invalid', async (t) => {
const err = await t.throwsAsync(
jobValidator({ name: 'basic', interval: -1 }, 0, ['exists'], baseConfig)
);
t.regex(
err.message,
/Job #1 named "basic" had an invalid interval of undefined; */
);
});
test('throws if hasSeconds is not a boolean', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', hasSeconds: 'test' },
0,
['exists'],
baseConfig
)
);
t.is(
err.message,
'Job #1 named "basic" had hasSeconds value of test (it must be a Boolean)'
);
});
test('throws if cronValidate is not an Object', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', cronValidate: 'test' },
0,
['exists'],
baseConfig
)
);
t.is(
err.message,
'Job #1 named "basic" had cronValidate value set, but it must be an Object'
);
});
test('throws if closeWorkerAfterMs is invalid', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', closeWorkerAfterMs: -1 },
0,
['exists'],
baseConfig
)
);
t.is(
err.message,
'Job #1 named "basic" had an invalid closeWorkersAfterMs value of undefined (it must be a finite number > 0)'
);
});
test('succeeds if job.timezone is valid', async (t) => {
await t.notThrowsAsync(
jobValidator(
{ name: 'basic', timezone: 'America/New_York' },
0,
['exists'],
baseConfig
)
);
});
test('accepts "local" and "system" as valid job.timezone options', async (t) => {
await t.notThrowsAsync(
jobValidator(
{ name: 'basic', timezone: 'local' },
0,
['exists'],
baseConfig
)
);
await t.notThrowsAsync(
jobValidator(
{ name: 'basic', timezone: 'system' },
0,
['exists'],
baseConfig
)
);
});
test('throws if job.timezone is invalid or unsupported', async (t) => {
const err = await t.throwsAsync(
jobValidator(
{ name: 'basic', timezone: 'bogus' },
0,
['exists'],
baseConfig
)
);
t.is(
err.message,
'Job #1 named "basic" had an invalid or unsupported timezone specified: bogus'
);
});
test('throws if path is not a file during object job', async (t) => {
const err = await t.throwsAsync(
jobValidator({ name: 'leroy.js' }, 0, ['exists'], baseConfig)
);
t.regex(err.message, /path missing/);
});
test('throws if path is not a file during string job', async (t) => {
const err = await t.throwsAsync(
jobValidator('leroy.js', 0, ['exists'], baseConfig)
);
t.regex(err.message, /path missing/);
});