-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
666 lines (665 loc) · 19.3 KB
/
test.ts
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import { Viae } from './index';
const DELAY_MILLIS = 1;
describe('Viae', () => {
it('should throw with node name containing errors', () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value(' foo ', 1)).toThrowError(
'Node name cannot contain whitespace'
);
});
it('should throw with node name is empty', () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value('', 1)).toThrowError('Node name cannot be empty');
});
it('should throw if node name already registered', () => {
expect.assertions(1);
const g = new Viae();
g.value('foo', 1);
expect(() => g.value('foo', 2)).toThrowError(`'foo' is already registered`);
});
it('should throw if node name is invalid variable name', () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value('f oo', 1)).toThrowError(
`'f oo' is not a valid variable name`
);
});
it(`should throw if node name is the reserved keyword 'function'`, () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value('function', 1)).toThrowError(
`'function' is not a valid variable name`
);
});
it(`should throw if node name is the reserved keyword 'var'`, () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value('var', 1)).toThrowError(
`'var' is not a valid variable name`
);
});
it(`should throw if node name is an invalid variable name starting with '.'`, () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value('.foo', 1)).toThrowError(
`'.foo' is not a valid variable name`
);
});
it(`should not throw if node name starts with '$' which is a valid variable
name`, () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value('$foo', 1)).not.toThrow();
});
it(`should not throw if node name starts with '_' which is a valid variable
name`, () => {
expect.assertions(1);
const g = new Viae();
expect(() => g.value('_foo', 1)).not.toThrow();
});
it('should allow ES6 arrow function', () => {
expect.assertions(2);
const g = new Viae();
g.value('num', 1);
g.async('foo', num => Promise.resolve(num + 1));
return g.entryPoint((num, foo) => {
expect(num).toBe(1);
expect(foo).toBe(2);
});
});
it('should strip comments in function declarations', () => {
expect.assertions(3);
const g = new Viae();
g.value('x', 1);
g.async('xToString', function (x) {
return Promise.resolve(x + '');
});
g.async('xIsOdd', (/** @number */ x) => Promise.resolve(x % 2 == 1));
return g.entryPoint((
/** @number */ x,
/** @boolean **/ xIsOdd /** @string*/,
xToString
) => {
expect(xToString).toBe('1');
expect(x).toBe(1);
expect(xIsOdd).toBe(true);
});
});
it('should resolve data value with primitive', () => {
expect.assertions(1);
const g = new Viae();
g.value('foo', 1);
return g.entryPoint(function (foo) {
expect(foo).toBe(1);
});
});
it('should throw if data dependency not defined', () => {
expect.assertions(1);
const g = new Viae();
g.value('foo', 1);
try {
g.entryPoint(function (bar) {
fail();
});
} catch (e) {
expect(e.message).toEqual(`Node 'bar' was not found in graph.`);
}
});
it('should resolve data value with same object', () => {
const g = new Viae();
const originalObject = {
id: 123,
phone: 911,
};
g.value('bar', originalObject);
return g.entryPoint(function (bar) {
expect(bar).toBe(originalObject);
});
});
it('should resolve multiple data values independent of registration order', () => {
const g = new Viae();
g.value('age', 1337);
g.value('name', 'Tuan');
g.value('data', {
id: 123,
phone: 911,
});
return g.entryPoint(function (name, data, age) {
expect(age).toBe(1337);
expect(name).toBe('Tuan');
expect(data).toEqual({
phone: 911,
id: 123,
});
});
});
it('should resolve async function', () => {
const g = new Viae();
g.value('jwt', 1);
g.async('account', function (jwt) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ id: jwt });
}, DELAY_MILLIS);
});
});
return g.entryPoint(function (account) {
expect(account).toEqual({
id: 1,
});
});
});
it('should resolve no-arg async function', () => {
expect.assertions(1);
const g = new Viae();
g.async('x', function () {
return Promise.resolve(1);
});
return g.entryPoint(x => {
expect(x).toBe(1);
});
});
it('should resolve function declared in variable', () => {
expect.assertions(1);
const g = new Viae();
g.value('x', 1);
const f = function (x) {
return Promise.resolve(x + 1);
};
g.async('y', f);
return g.entryPoint(y => {
expect(y).toBe(2);
});
});
it('should resolve functions in their lexical closure', () => {
expect.assertions(1);
const g = new Viae();
g.value('x', 1);
const y = 2;
const f = function (x) {
return Promise.resolve(x + y);
};
g.async('y', f);
return g.entryPoint(y => {
expect(y).toBe(3);
});
});
it('should throw if async function references non-existent dependency', () => {
expect.assertions(1);
const g = new Viae();
g.async('account', function (jwt) {
return Promise.resolve(123);
});
try {
g.entryPoint(function (account) {
fail();
});
} catch (e) {
expect(e.message).toBe(`Node 'jwt' was not found in graph.`);
}
});
it('should resolve async function with nodes registered out of order', () => {
const g = new Viae();
g.async('account', function (jwt) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ id: jwt });
}, DELAY_MILLIS);
});
});
g.value('jwt', 1);
return g.entryPoint(function (account) {
expect(account).toEqual({
id: 1,
});
});
});
it('should resolve async function that depends on async function', () => {
const g = new Viae();
g.value('jwt', 1);
g.async('account', function (jwt) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ id: jwt });
}, DELAY_MILLIS);
});
});
g.async('campaign', function (account) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
id: 123,
accountId: account.id,
});
}, DELAY_MILLIS);
});
});
return g.entryPoint(function (campaign) {
expect(campaign).toEqual({
id: 123,
accountId: 1,
});
});
});
it('should detect a cycle with distance 2', () => {
expect.assertions(1);
const g = new Viae();
g.async('chicken', function (egg) {
return Promise.resolve('Cluck');
});
g.async('egg', function (chicken) {
return Promise.resolve('...');
});
try {
return g.entryPoint(function (egg) {
fail();
});
} catch (e) {
expect(e.message).toBe(
`Node 'chicken' depends on 'egg' which is actually` +
' an ancestor: root->egg->chicken->egg'
);
}
});
it('should detect a long cycle', () => {
expect.assertions(1);
const g = new Viae();
g.async('google', function (goobuntu) {
return Promise.resolve();
});
g.async('goobuntu', function (ubuntu) {
return Promise.resolve();
});
g.async('ubuntu', function (linuxKernel) {
return Promise.resolve();
});
g.async('linuxKernel', function (gcc) {
return Promise.resolve();
});
g.async('gcc', function (glibc) {
return Promise.resolve();
});
g.async('glibc', function (c) {
return Promise.resolve();
});
g.async('c', function (dennisRitchie, kenThompson) {
return Promise.resolve();
});
g.value('unix', 0);
g.async('kenThompson', function (goobuntu) {
return Promise.resolve();
});
g.async('dennisRitchie', function (unix) {
return Promise.resolve();
});
try {
return g.entryPoint(function (google) {
fail();
});
} catch (e) {
expect(e.message).toBe(
`Node 'kenThompson' depends on 'goobuntu' which is` +
' actually an ancestor: root->google->goobuntu->ubuntu->' +
'linuxKernel->gcc->glibc->c->kenThompson->goobuntu'
);
}
});
it('should resolve async function and async function it depends on', () => {
const g = new Viae();
g.value('jwt', 1);
g.async('account', function (jwt) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ id: jwt });
}, DELAY_MILLIS);
});
});
g.async('campaign', function (account) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
id: 123,
accountId: account.id,
});
}, DELAY_MILLIS);
});
});
return g.entryPoint(function (campaign, account) {
expect(account).toEqual({
id: 1,
});
expect(campaign).toEqual({
id: 123,
accountId: 1,
});
});
});
it('should resolve async function and async function that depends on it', () => {
const g = new Viae();
g.value('jwt', 1);
g.async('account', function (jwt) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ id: jwt });
}, DELAY_MILLIS);
});
});
g.async('campaign', function (account) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
id: 123,
accountId: account.id,
});
}, DELAY_MILLIS);
});
});
return g.entryPoint(function (account, campaign) {
expect(account).toEqual({
id: 1,
});
expect(campaign).toEqual({
id: 123,
accountId: 1,
});
});
});
it('should reject if async function throws', () => {
expect.assertions(1);
const g = new Viae();
g.value('jwt', 1);
g.async('campaign', function (jwt) {
throw new Error('Foo');
});
return g
.entryPoint(function (campaign) {
fail();
})
.catch(e => {
expect(e).toEqual(
new Error(
`Error on path root->campaign. ` +
`call campaign(jwt=1) threw error: (Error: Foo)`
)
);
});
});
it('should reject if async function returns rejected Promise', () => {
expect.assertions(1);
const g = new Viae();
g.value('jwt', 1);
g.async('campaign', function (jwt) {
return Promise.resolve().then(() => {
throw new Error('Foo');
});
});
return g
.entryPoint(function (campaign) {
fail();
})
.catch(e => {
expect(e).toEqual(
new Error(
`Error on path root->campaign. ` +
`call campaign(jwt=1) ` +
`returned Promise rejected with (Error: Foo)`
)
);
});
});
it('should wrap in promise if Function produces a value that is not a Promise', () => {
expect.assertions(1);
const g = new Viae();
g.value('x', 1);
g.async('y', function (x) {
return x + 1;
});
return g.entryPoint(function (y) {
expect(y).toBe(2);
});
});
it('should give precedence to missing node error if async function throws', () => {
expect.assertions(1);
const g = new Viae();
g.async('campaign', function (jw) {
throw new Error('Foo');
});
try {
g.entryPoint(function (campaign) {
fail();
}).catch(e => {
fail();
});
} catch (e) {
expect(e.message).toBe(`Node 'jw' was not found in graph.`);
}
});
it('should resolve async functions with shared data dependency', () => {
const g = new Viae();
g.value('num', 10);
g.async('double', function (num) {
return Promise.resolve(num * 2);
});
g.async('half', function (num) {
return Promise.resolve(num / 2);
});
return g.entryPoint(function (double, half) {
expect(double).toEqual(20);
expect(half).toEqual(5);
});
});
it('should invoke async function node once per execution', () => {
expect.assertions(5);
let bCount = 0;
let cCount = 0;
const g = new Viae();
g.value('a', 2);
g.async('b', function (a) {
bCount++;
return Promise.resolve(3 * a);
});
g.async('c', function (b) {
cCount++;
return Promise.resolve(5 * b);
});
const executionA = g.entryPoint(function (b, c) {
expect(b).toBe(3 * 2);
expect(c).toBe(5 * (3 * 2));
});
const executionB = g.entryPoint(function (b) {
expect(b).toBe(3 * 2);
});
return Promise.all([executionA, executionB]).then(() => {
expect(bCount).toBe(2);
expect(cCount).toBe(1);
});
});
it('should resolve async functions with shared async function dependency', () => {
const executions = 2;
const expectations = 3;
expect.assertions(executions * expectations);
const g = new Viae();
g.value('num', 10);
g.async('add2', function (num) {
return Promise.resolve(num + 2);
});
g.async('double', function (add2) {
return Promise.resolve(add2 * 2);
});
g.async('half', function (add2) {
return Promise.resolve(add2 / 2);
});
const executionA = g.entryPoint(function (add2, double, half) {
expect(add2).toEqual(12);
expect(double).toEqual(24);
expect(half).toEqual(6);
});
const executionB = g.entryPoint(function (double, add2, half) {
expect(add2).toEqual(12);
expect(double).toEqual(24);
expect(half).toEqual(6);
});
return Promise.all([executionA, executionB]);
});
it('should resolve permutations of bcd with g: d->c, c->b, b->a', () => {
const permutations = 6;
const expectsPerPermutation = 3;
expect.assertions(expectsPerPermutation * permutations);
const g = new Viae();
g.value('a', 2);
g.async('b', function (a) {
return Promise.resolve(3 * a);
});
g.async('c', function (b) {
return Promise.resolve(5 * b);
});
g.async('d', function (c) {
return Promise.resolve(7 * c);
});
const expectedB = 3 * 2;
const expectedC = 5 * (3 * 2);
const expectedD = 7 * (5 * 3 * 2);
const bcd = g.entryPoint(function (b, c, d) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const bdc = g.entryPoint(function (b, d, c) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const cbd = g.entryPoint(function (c, b, d) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const cdb = g.entryPoint(function (c, d, b) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const dbc = g.entryPoint(function (d, b, c) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const dcb = g.entryPoint(function (d, c, b) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
return Promise.all([bcd, bdc, cbd, cdb, dbc, dcb]);
});
it('should resolve permutations of bcd with graph d->[b, c], c->b, b->a', () => {
expect.assertions(3 * 6);
const g = new Viae();
g.value('a', 2);
g.async('b', function (a) {
return Promise.resolve(3 * a);
});
g.async('c', function (b) {
return Promise.resolve(5 * b);
});
g.async('d', function (b, c) {
return Promise.resolve(7 * b * c);
});
const expectedB = 3 * 2;
const expectedC = 5 * (3 * 2);
const expectedD = 7 * (5 * 3 * 2) * (3 * 2);
const bcd = g.entryPoint(function (b, c, d) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const bdc = g.entryPoint(function (b, d, c) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const cbd = g.entryPoint(function (c, b, d) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const cdb = g.entryPoint(function (c, d, b) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const dbc = g.entryPoint(function (d, b, c) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const dcb = g.entryPoint(function (d, c, b) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
return Promise.all([bcd, bdc, cbd, cdb, dbc, dcb]);
});
it('should resolve permutations of bcd with graph d->[c, b], c->b, b->a', () => {
expect.assertions(3 * 6);
const g = new Viae();
g.value('a', 2);
g.async('b', function (a) {
return Promise.resolve(3 * a);
});
g.async('c', function (b) {
return Promise.resolve(5 * b);
});
g.async('d', function (c, b) {
return Promise.resolve(7 * b * c);
});
const expectedB = 3 * 2;
const expectedC = 5 * (3 * 2);
const expectedD = 7 * (5 * 3 * 2) * (3 * 2);
const bcd = g.entryPoint(function (b, c, d) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const bdc = g.entryPoint(function (b, d, c) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const cbd = g.entryPoint(function (c, b, d) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const cdb = g.entryPoint(function (c, d, b) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const dbc = g.entryPoint(function (d, b, c) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
const dcb = g.entryPoint(function (d, c, b) {
expect(b).toEqual(expectedB);
expect(c).toEqual(expectedC);
expect(d).toEqual(expectedD);
});
return Promise.all([bcd, bdc, cbd, cdb, dbc, dcb]);
});
test.todo('should support configuring delay between nodes');
test.todo('should support node retry to smooth over transient errors');
test.todo('should reject with path leading up tonode failure');
test.todo('should fail because of global timeout');
test.todo('should dump debug information for failure');
test.todo('should create copy of object values before injecting');
test.todo('should error if destructuring parameter arguments');
// TODO: consider not allowing nodes registered out of order,
// complain if dependency not yet registered. This also allows easy
// enforcement of cycle detection, and that graph is an D.A.G.
// TODO: consider pruning library interace. remove data node, and let
// all nodes be async functions. The base case would be a function
// that has no dependencies.
});