@adviser/cement 0.5.11 → 0.5.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/evento.cjs +138 -22
- package/cjs/evento.cjs.map +1 -1
- package/cjs/evento.d.ts +53 -7
- package/cjs/evento.d.ts.map +1 -1
- package/cjs/evento.test.cjs +505 -18
- package/cjs/evento.test.cjs.map +1 -1
- package/cjs/version.cjs +1 -1
- package/deno.json +1 -1
- package/esm/evento.d.ts +53 -7
- package/esm/evento.d.ts.map +1 -1
- package/esm/evento.js +135 -21
- package/esm/evento.js.map +1 -1
- package/esm/evento.test.js +506 -19
- package/esm/evento.test.js.map +1 -1
- package/esm/version.js +1 -1
- package/package.json +1 -1
- package/src/evento.ts +200 -41
- package/ts/cjs/evento.d.ts +53 -7
- package/ts/cjs/evento.d.ts.map +1 -1
- package/ts/cjs/evento.js +138 -22
- package/ts/cjs/evento.js.map +1 -1
- package/ts/cjs/evento.test.js +505 -18
- package/ts/cjs/evento.test.js.map +1 -1
- package/ts/cjs/version.js +1 -1
- package/ts/esm/evento.d.ts +53 -7
- package/ts/esm/evento.d.ts.map +1 -1
- package/ts/esm/evento.js +135 -21
- package/ts/esm/evento.js.map +1 -1
- package/ts/esm/evento.test.js +506 -19
- package/ts/esm/evento.test.js.map +1 -1
- package/ts/esm/version.js +1 -1
package/esm/evento.test.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
-
import { Evento, EventoResult, EventoOp, EventoType, } from "./evento.js";
|
|
2
|
+
import { Evento, EventoResult, EventoOp, EventoType, EventoSend, } from "./evento.js";
|
|
3
3
|
import { Result } from "./result.js";
|
|
4
4
|
import { Option } from "./option.js";
|
|
5
|
+
import { sleep } from "./promise-sleep.js";
|
|
5
6
|
class ReqResEventoEnDecoder {
|
|
6
7
|
async encode(args) {
|
|
7
8
|
const body = (await args.json());
|
|
@@ -136,6 +137,7 @@ describe("Evento", () => {
|
|
|
136
137
|
const reqRes = new ReqResEventoEnDecoder();
|
|
137
138
|
const evo = new Evento(reqRes);
|
|
138
139
|
const send = new TestSend();
|
|
140
|
+
const sendEvent = new EventoSend(send);
|
|
139
141
|
evo.push({
|
|
140
142
|
type: EventoType.WildCard,
|
|
141
143
|
hash: "wildcard-handler-first",
|
|
@@ -157,7 +159,8 @@ describe("Evento", () => {
|
|
|
157
159
|
req: ctx.validated,
|
|
158
160
|
})
|
|
159
161
|
.then((rv) => {
|
|
160
|
-
const
|
|
162
|
+
const { item } = rv.Ok();
|
|
163
|
+
const v = item.Ok();
|
|
161
164
|
let ret = Result.Ok(EventoResult.Continue);
|
|
162
165
|
if (v.req.stop) {
|
|
163
166
|
ret = Result.Ok(EventoResult.Stop);
|
|
@@ -188,7 +191,7 @@ describe("Evento", () => {
|
|
|
188
191
|
req: ctx.validated,
|
|
189
192
|
})
|
|
190
193
|
.then((rv) => {
|
|
191
|
-
const v = rv.Ok();
|
|
194
|
+
const { item: v } = rv.Ok();
|
|
192
195
|
const ret = Result.Ok(EventoResult.Continue);
|
|
193
196
|
send.fn("wildcard-second-handle", ctx, v, ret);
|
|
194
197
|
return ret;
|
|
@@ -215,7 +218,8 @@ describe("Evento", () => {
|
|
|
215
218
|
req: ctx.validated,
|
|
216
219
|
})
|
|
217
220
|
.then((rv) => {
|
|
218
|
-
const
|
|
221
|
+
const { item } = rv.Ok();
|
|
222
|
+
const v = item.Ok();
|
|
219
223
|
let ret = Result.Ok(EventoResult.Continue);
|
|
220
224
|
if (v.req.stop) {
|
|
221
225
|
ret = Result.Ok(EventoResult.Stop);
|
|
@@ -245,7 +249,8 @@ describe("Evento", () => {
|
|
|
245
249
|
req: ctx.validated,
|
|
246
250
|
})
|
|
247
251
|
.then((rv) => {
|
|
248
|
-
const
|
|
252
|
+
const { item } = rv.Ok();
|
|
253
|
+
const v = item.Ok();
|
|
249
254
|
const ret = Result.Ok(EventoResult.Continue);
|
|
250
255
|
send.fn("regular-second-handle", ctx, v, ret);
|
|
251
256
|
return ret;
|
|
@@ -278,10 +283,11 @@ describe("Evento", () => {
|
|
|
278
283
|
"wildcard-first-validate",
|
|
279
284
|
expect.objectContaining({
|
|
280
285
|
ctx: expect.anything(),
|
|
286
|
+
stats: expect.anything(),
|
|
281
287
|
enRequest: { x: 1 },
|
|
282
288
|
encoder: reqRes,
|
|
283
289
|
request,
|
|
284
|
-
send,
|
|
290
|
+
send: sendEvent,
|
|
285
291
|
}),
|
|
286
292
|
Result.Ok(Option.Some({ validated: true, x: 1, step: "wildcard-first" })),
|
|
287
293
|
]);
|
|
@@ -289,10 +295,11 @@ describe("Evento", () => {
|
|
|
289
295
|
"wildcard-first-handle",
|
|
290
296
|
expect.objectContaining({
|
|
291
297
|
ctx: expect.anything(),
|
|
298
|
+
stats: expect.anything(),
|
|
292
299
|
enRequest: { x: 1 },
|
|
293
300
|
encoder: reqRes,
|
|
294
301
|
request,
|
|
295
|
-
send,
|
|
302
|
+
send: sendEvent,
|
|
296
303
|
validated: { validated: true, x: 1, step: "wildcard-first" },
|
|
297
304
|
}),
|
|
298
305
|
{
|
|
@@ -310,10 +317,11 @@ describe("Evento", () => {
|
|
|
310
317
|
"wildcard-second-validate",
|
|
311
318
|
expect.objectContaining({
|
|
312
319
|
ctx: expect.anything(),
|
|
320
|
+
stats: expect.anything(),
|
|
313
321
|
enRequest: { x: 1 },
|
|
314
322
|
encoder: reqRes,
|
|
315
323
|
request,
|
|
316
|
-
send,
|
|
324
|
+
send: sendEvent,
|
|
317
325
|
}),
|
|
318
326
|
Result.Ok(Option.Some({ validated: true, x: 1, step: "wildcard-second" })),
|
|
319
327
|
]);
|
|
@@ -321,13 +329,14 @@ describe("Evento", () => {
|
|
|
321
329
|
"wildcard-second-handle",
|
|
322
330
|
expect.objectContaining({
|
|
323
331
|
ctx: expect.anything(),
|
|
332
|
+
stats: expect.anything(),
|
|
324
333
|
enRequest: { x: 1 },
|
|
325
334
|
encoder: reqRes,
|
|
326
335
|
request,
|
|
327
|
-
send,
|
|
336
|
+
send: sendEvent,
|
|
328
337
|
validated: { validated: true, x: 1, step: "wildcard-second" },
|
|
329
338
|
}),
|
|
330
|
-
{
|
|
339
|
+
Result.Ok({
|
|
331
340
|
encoderInfo: "test-encoder",
|
|
332
341
|
req: {
|
|
333
342
|
step: "wildcard-second",
|
|
@@ -335,7 +344,7 @@ describe("Evento", () => {
|
|
|
335
344
|
x: 1,
|
|
336
345
|
},
|
|
337
346
|
response: "ok",
|
|
338
|
-
},
|
|
347
|
+
}),
|
|
339
348
|
Result.Ok(EventoResult.Continue),
|
|
340
349
|
]);
|
|
341
350
|
});
|
|
@@ -358,10 +367,11 @@ describe("Evento", () => {
|
|
|
358
367
|
"wildcard-first-validate",
|
|
359
368
|
expect.objectContaining({
|
|
360
369
|
ctx: expect.anything(),
|
|
370
|
+
stats: expect.anything(),
|
|
361
371
|
enRequest: { x: 1, stop: true },
|
|
362
372
|
encoder: reqRes,
|
|
363
373
|
request,
|
|
364
|
-
send,
|
|
374
|
+
send: sendEvent,
|
|
365
375
|
}),
|
|
366
376
|
Result.Ok(Option.Some({ validated: true, x: 1, stop: true, step: "wildcard-first" })),
|
|
367
377
|
]);
|
|
@@ -369,10 +379,11 @@ describe("Evento", () => {
|
|
|
369
379
|
"wildcard-first-handle",
|
|
370
380
|
expect.objectContaining({
|
|
371
381
|
ctx: expect.anything(),
|
|
382
|
+
stats: expect.anything(),
|
|
372
383
|
enRequest: { x: 1, stop: true },
|
|
373
384
|
encoder: reqRes,
|
|
374
385
|
request,
|
|
375
|
-
send,
|
|
386
|
+
send: sendEvent,
|
|
376
387
|
validated: { validated: true, x: 1, stop: true, step: "wildcard-first" },
|
|
377
388
|
}),
|
|
378
389
|
{
|
|
@@ -408,10 +419,11 @@ describe("Evento", () => {
|
|
|
408
419
|
"regular-first-validate",
|
|
409
420
|
expect.objectContaining({
|
|
410
421
|
ctx: expect.anything(),
|
|
422
|
+
stats: expect.anything(),
|
|
411
423
|
enRequest: { x: 2 },
|
|
412
424
|
encoder: reqRes,
|
|
413
425
|
request,
|
|
414
|
-
send,
|
|
426
|
+
send: sendEvent,
|
|
415
427
|
}),
|
|
416
428
|
Result.Ok(Option.Some({ validated: true, x: 2, step: "regular-first" })),
|
|
417
429
|
]);
|
|
@@ -419,10 +431,11 @@ describe("Evento", () => {
|
|
|
419
431
|
"regular-first-handle",
|
|
420
432
|
expect.objectContaining({
|
|
421
433
|
ctx: expect.anything(),
|
|
434
|
+
stats: expect.anything(),
|
|
422
435
|
enRequest: { x: 2 },
|
|
423
436
|
encoder: reqRes,
|
|
424
437
|
request,
|
|
425
|
-
send,
|
|
438
|
+
send: sendEvent,
|
|
426
439
|
validated: { validated: true, x: 2, step: "regular-first" },
|
|
427
440
|
}),
|
|
428
441
|
{
|
|
@@ -440,10 +453,11 @@ describe("Evento", () => {
|
|
|
440
453
|
"regular-second-validate",
|
|
441
454
|
expect.objectContaining({
|
|
442
455
|
ctx: expect.anything(),
|
|
456
|
+
stats: expect.anything(),
|
|
443
457
|
enRequest: { x: 2 },
|
|
444
458
|
encoder: reqRes,
|
|
445
459
|
request,
|
|
446
|
-
send,
|
|
460
|
+
send: sendEvent,
|
|
447
461
|
}),
|
|
448
462
|
Result.Ok(Option.Some({ validated: true, x: 2, step: "regular-second" })),
|
|
449
463
|
]);
|
|
@@ -451,10 +465,11 @@ describe("Evento", () => {
|
|
|
451
465
|
"regular-second-handle",
|
|
452
466
|
expect.objectContaining({
|
|
453
467
|
ctx: expect.anything(),
|
|
468
|
+
stats: expect.anything(),
|
|
454
469
|
enRequest: { x: 2 },
|
|
455
470
|
encoder: reqRes,
|
|
456
471
|
request,
|
|
457
|
-
send,
|
|
472
|
+
send: sendEvent,
|
|
458
473
|
validated: { validated: true, x: 2, step: "regular-second" },
|
|
459
474
|
}),
|
|
460
475
|
{
|
|
@@ -486,10 +501,11 @@ describe("Evento", () => {
|
|
|
486
501
|
"regular-first-validate",
|
|
487
502
|
expect.objectContaining({
|
|
488
503
|
ctx: expect.anything(),
|
|
504
|
+
stats: expect.anything(),
|
|
489
505
|
enRequest: { x: 2, stop: true },
|
|
490
506
|
encoder: reqRes,
|
|
491
507
|
request,
|
|
492
|
-
send,
|
|
508
|
+
send: sendEvent,
|
|
493
509
|
}),
|
|
494
510
|
Result.Ok(Option.Some({ validated: true, x: 2, stop: true, step: "regular-first" })),
|
|
495
511
|
]);
|
|
@@ -497,10 +513,11 @@ describe("Evento", () => {
|
|
|
497
513
|
"regular-first-handle",
|
|
498
514
|
expect.objectContaining({
|
|
499
515
|
ctx: expect.anything(),
|
|
516
|
+
stats: expect.anything(),
|
|
500
517
|
enRequest: { x: 2, stop: true },
|
|
501
518
|
encoder: reqRes,
|
|
502
519
|
request,
|
|
503
|
-
send,
|
|
520
|
+
send: sendEvent,
|
|
504
521
|
validated: { validated: true, x: 2, stop: true, step: "regular-first" },
|
|
505
522
|
}),
|
|
506
523
|
{
|
|
@@ -516,5 +533,475 @@ describe("Evento", () => {
|
|
|
516
533
|
Result.Ok(EventoResult.Stop),
|
|
517
534
|
]);
|
|
518
535
|
});
|
|
536
|
+
it("validate throw encode error trigger", async () => {
|
|
537
|
+
class ErrorEncoder {
|
|
538
|
+
encode(_args) {
|
|
539
|
+
throw new Error("test encode error");
|
|
540
|
+
}
|
|
541
|
+
decode(data) {
|
|
542
|
+
return Promise.resolve(Result.Ok(JSON.stringify(data)));
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
const evo = new Evento(new ErrorEncoder());
|
|
546
|
+
const send = new TestSend();
|
|
547
|
+
const sendEvent = new EventoSend(send);
|
|
548
|
+
evo.push({
|
|
549
|
+
type: EventoType.Error,
|
|
550
|
+
hash: "error-handler-0",
|
|
551
|
+
handle: async (ctx) => {
|
|
552
|
+
send.fn("error-handler-0-handle", ctx);
|
|
553
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
554
|
+
},
|
|
555
|
+
});
|
|
556
|
+
evo.push({
|
|
557
|
+
type: EventoType.Error,
|
|
558
|
+
hash: "error-handler-1",
|
|
559
|
+
handle: async (ctx) => {
|
|
560
|
+
send.fn("error-handler-1-handle", ctx);
|
|
561
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
562
|
+
},
|
|
563
|
+
});
|
|
564
|
+
evo.push({
|
|
565
|
+
hash: "regular-handler-0",
|
|
566
|
+
handle: async (ctx) => {
|
|
567
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
568
|
+
return Promise.reject(new Error("test error"));
|
|
569
|
+
},
|
|
570
|
+
});
|
|
571
|
+
const request = new Request("http://example.com", { method: "GET" });
|
|
572
|
+
await evo.trigger({
|
|
573
|
+
send,
|
|
574
|
+
request,
|
|
575
|
+
});
|
|
576
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual(["error-handler-0-handle", "error-handler-1-handle"]);
|
|
577
|
+
expect(send.fn.mock.calls[0][1].error.message).toContain("test encode error");
|
|
578
|
+
expect(send.fn.mock.calls[0]).toEqual([
|
|
579
|
+
"error-handler-0-handle",
|
|
580
|
+
expect.objectContaining({
|
|
581
|
+
ctx: expect.anything(),
|
|
582
|
+
stats: expect.anything(),
|
|
583
|
+
encoder: reqRes,
|
|
584
|
+
request,
|
|
585
|
+
error: send.fn.mock.calls[0][1].error,
|
|
586
|
+
send: sendEvent,
|
|
587
|
+
}),
|
|
588
|
+
]);
|
|
589
|
+
});
|
|
590
|
+
it("validate result encode error trigger", async () => {
|
|
591
|
+
class ErrorEncoder {
|
|
592
|
+
encode(_args) {
|
|
593
|
+
return Promise.resolve(Result.Err(new Error("test encode error")));
|
|
594
|
+
}
|
|
595
|
+
decode(data) {
|
|
596
|
+
return Promise.resolve(Result.Ok(JSON.stringify(data)));
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
const evo = new Evento(new ErrorEncoder());
|
|
600
|
+
const send = new TestSend();
|
|
601
|
+
const sendEvent = new EventoSend(send);
|
|
602
|
+
evo.push({
|
|
603
|
+
type: EventoType.Error,
|
|
604
|
+
hash: "error-handler-0",
|
|
605
|
+
handle: async (ctx) => {
|
|
606
|
+
send.fn("error-handler-0-handle", ctx);
|
|
607
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
evo.push({
|
|
611
|
+
type: EventoType.Error,
|
|
612
|
+
hash: "error-handler-1",
|
|
613
|
+
handle: async (ctx) => {
|
|
614
|
+
send.fn("error-handler-1-handle", ctx);
|
|
615
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
616
|
+
},
|
|
617
|
+
});
|
|
618
|
+
evo.push({
|
|
619
|
+
hash: "regular-handler-0",
|
|
620
|
+
handle: async (ctx) => {
|
|
621
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
622
|
+
return Promise.reject(new Error("test error"));
|
|
623
|
+
},
|
|
624
|
+
});
|
|
625
|
+
const request = new Request("http://example.com", { method: "GET" });
|
|
626
|
+
await evo.trigger({
|
|
627
|
+
send,
|
|
628
|
+
request,
|
|
629
|
+
});
|
|
630
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual(["error-handler-0-handle", "error-handler-1-handle"]);
|
|
631
|
+
expect(send.fn.mock.calls[0][1].error.message).toContain("test encode error");
|
|
632
|
+
expect(send.fn.mock.calls[0]).toEqual([
|
|
633
|
+
"error-handler-0-handle",
|
|
634
|
+
expect.objectContaining({
|
|
635
|
+
ctx: expect.anything(),
|
|
636
|
+
stats: expect.anything(),
|
|
637
|
+
encoder: reqRes,
|
|
638
|
+
request,
|
|
639
|
+
error: send.fn.mock.calls[0][1].error,
|
|
640
|
+
send: sendEvent,
|
|
641
|
+
}),
|
|
642
|
+
]);
|
|
643
|
+
});
|
|
644
|
+
it("validate throw error trigger", async () => {
|
|
645
|
+
const evo = new Evento(reqRes);
|
|
646
|
+
const send = new TestSend();
|
|
647
|
+
const sendEvent = new EventoSend(send);
|
|
648
|
+
evo.push({
|
|
649
|
+
type: EventoType.Error,
|
|
650
|
+
hash: "error-handler-0",
|
|
651
|
+
handle: async (ctx) => {
|
|
652
|
+
send.fn("error-handler-0-handle", ctx);
|
|
653
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
654
|
+
},
|
|
655
|
+
});
|
|
656
|
+
evo.push({
|
|
657
|
+
type: EventoType.Error,
|
|
658
|
+
hash: "error-handler-1",
|
|
659
|
+
handle: async (ctx) => {
|
|
660
|
+
send.fn("error-handler-1-handle", ctx);
|
|
661
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
662
|
+
},
|
|
663
|
+
});
|
|
664
|
+
const error = new Error("test validate error");
|
|
665
|
+
evo.push({
|
|
666
|
+
hash: "regular-handler-0",
|
|
667
|
+
validate: async (ctx) => {
|
|
668
|
+
send.fn("regular-handler-0-validate", ctx);
|
|
669
|
+
return Promise.reject(error);
|
|
670
|
+
},
|
|
671
|
+
handle: async (ctx) => {
|
|
672
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
673
|
+
return Promise.reject(new Error("test error"));
|
|
674
|
+
},
|
|
675
|
+
});
|
|
676
|
+
const request = new Request("http://example.com", { method: "POST", body: JSON.stringify({ x: 2, stop: true }) });
|
|
677
|
+
await evo.trigger({
|
|
678
|
+
send,
|
|
679
|
+
request,
|
|
680
|
+
});
|
|
681
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual([
|
|
682
|
+
"regular-handler-0-validate",
|
|
683
|
+
"error-handler-0-handle",
|
|
684
|
+
"error-handler-1-handle",
|
|
685
|
+
]);
|
|
686
|
+
expect(send.fn.mock.calls[1]).toEqual([
|
|
687
|
+
"error-handler-0-handle",
|
|
688
|
+
expect.objectContaining({
|
|
689
|
+
ctx: expect.anything(),
|
|
690
|
+
stats: expect.anything(),
|
|
691
|
+
encoder: reqRes,
|
|
692
|
+
enRequest: { x: 2, stop: true },
|
|
693
|
+
request,
|
|
694
|
+
error,
|
|
695
|
+
send: sendEvent,
|
|
696
|
+
}),
|
|
697
|
+
]);
|
|
698
|
+
});
|
|
699
|
+
it("validate result error trigger", async () => {
|
|
700
|
+
const evo = new Evento(reqRes);
|
|
701
|
+
const send = new TestSend();
|
|
702
|
+
const sendEvent = new EventoSend(send);
|
|
703
|
+
evo.push({
|
|
704
|
+
type: EventoType.Error,
|
|
705
|
+
hash: "error-handler-0",
|
|
706
|
+
handle: async (ctx) => {
|
|
707
|
+
send.fn("error-handler-0-handle", ctx);
|
|
708
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
709
|
+
},
|
|
710
|
+
});
|
|
711
|
+
evo.push({
|
|
712
|
+
type: EventoType.Error,
|
|
713
|
+
hash: "error-handler-1",
|
|
714
|
+
handle: async (ctx) => {
|
|
715
|
+
send.fn("error-handler-1-handle", ctx);
|
|
716
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
717
|
+
},
|
|
718
|
+
});
|
|
719
|
+
const error = new Error("test validate error");
|
|
720
|
+
evo.push({
|
|
721
|
+
hash: "regular-handler-0",
|
|
722
|
+
validate: async (ctx) => {
|
|
723
|
+
send.fn("regular-handler-0-validate", ctx);
|
|
724
|
+
return Promise.resolve(Result.Err(error));
|
|
725
|
+
},
|
|
726
|
+
handle: async (ctx) => {
|
|
727
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
728
|
+
return Promise.reject(new Error("test error"));
|
|
729
|
+
},
|
|
730
|
+
});
|
|
731
|
+
const request = new Request("http://example.com", { method: "POST", body: JSON.stringify({ x: 2, stop: true }) });
|
|
732
|
+
await evo.trigger({
|
|
733
|
+
send,
|
|
734
|
+
request,
|
|
735
|
+
});
|
|
736
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual([
|
|
737
|
+
"regular-handler-0-validate",
|
|
738
|
+
"error-handler-0-handle",
|
|
739
|
+
"error-handler-1-handle",
|
|
740
|
+
]);
|
|
741
|
+
expect(send.fn.mock.calls[1]).toEqual([
|
|
742
|
+
"error-handler-0-handle",
|
|
743
|
+
expect.objectContaining({
|
|
744
|
+
ctx: expect.anything(),
|
|
745
|
+
stats: expect.anything(),
|
|
746
|
+
encoder: reqRes,
|
|
747
|
+
enRequest: { x: 2, stop: true },
|
|
748
|
+
request,
|
|
749
|
+
error,
|
|
750
|
+
send: sendEvent,
|
|
751
|
+
}),
|
|
752
|
+
]);
|
|
753
|
+
});
|
|
754
|
+
it("handle throw error trigger", async () => {
|
|
755
|
+
const evo = new Evento(reqRes);
|
|
756
|
+
const send = new TestSend();
|
|
757
|
+
const sendEvent = new EventoSend(send);
|
|
758
|
+
evo.push({
|
|
759
|
+
type: EventoType.Error,
|
|
760
|
+
hash: "error-handler-0",
|
|
761
|
+
handle: async (ctx) => {
|
|
762
|
+
send.fn("error-handler-0-handle", ctx);
|
|
763
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
764
|
+
},
|
|
765
|
+
});
|
|
766
|
+
evo.push({
|
|
767
|
+
type: EventoType.Error,
|
|
768
|
+
hash: "error-handler-1",
|
|
769
|
+
handle: async (ctx) => {
|
|
770
|
+
send.fn("error-handler-1-handle", ctx);
|
|
771
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
772
|
+
},
|
|
773
|
+
});
|
|
774
|
+
const error = new Error("test handle error");
|
|
775
|
+
evo.push({
|
|
776
|
+
hash: "regular-handler-0",
|
|
777
|
+
handle: async (ctx) => {
|
|
778
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
779
|
+
return Promise.reject(error);
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
const request = new Request("http://example.com", { method: "POST", body: JSON.stringify({ x: 2, stop: true }) });
|
|
783
|
+
await evo.trigger({
|
|
784
|
+
send,
|
|
785
|
+
request,
|
|
786
|
+
});
|
|
787
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual([
|
|
788
|
+
"start",
|
|
789
|
+
"regular-handler-0-handle",
|
|
790
|
+
"error-handler-0-handle",
|
|
791
|
+
"error-handler-1-handle",
|
|
792
|
+
"done",
|
|
793
|
+
]);
|
|
794
|
+
expect(send.fn.mock.calls[2]).toEqual([
|
|
795
|
+
"error-handler-0-handle",
|
|
796
|
+
expect.objectContaining({
|
|
797
|
+
ctx: expect.anything(),
|
|
798
|
+
stats: expect.anything(),
|
|
799
|
+
encoder: reqRes,
|
|
800
|
+
enRequest: { x: 2, stop: true },
|
|
801
|
+
request,
|
|
802
|
+
error,
|
|
803
|
+
send: sendEvent,
|
|
804
|
+
validated: {
|
|
805
|
+
stop: true,
|
|
806
|
+
x: 2,
|
|
807
|
+
},
|
|
808
|
+
}),
|
|
809
|
+
]);
|
|
810
|
+
});
|
|
811
|
+
it("handle result error trigger", async () => {
|
|
812
|
+
const evo = new Evento(reqRes);
|
|
813
|
+
const send = new TestSend();
|
|
814
|
+
const sendEvent = new EventoSend(send);
|
|
815
|
+
evo.push({
|
|
816
|
+
type: EventoType.Error,
|
|
817
|
+
hash: "error-handler-0",
|
|
818
|
+
handle: async (ctx) => {
|
|
819
|
+
send.fn("error-handler-0-handle", ctx);
|
|
820
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
821
|
+
},
|
|
822
|
+
});
|
|
823
|
+
evo.push({
|
|
824
|
+
type: EventoType.Error,
|
|
825
|
+
hash: "error-handler-1",
|
|
826
|
+
handle: async (ctx) => {
|
|
827
|
+
send.fn("error-handler-1-handle", ctx);
|
|
828
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
829
|
+
},
|
|
830
|
+
});
|
|
831
|
+
const error = new Error("test handle error");
|
|
832
|
+
evo.push({
|
|
833
|
+
hash: "regular-handler-0",
|
|
834
|
+
handle: async (ctx) => {
|
|
835
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
836
|
+
return Promise.resolve(Result.Err(error));
|
|
837
|
+
},
|
|
838
|
+
post: async (ctx) => {
|
|
839
|
+
send.fn("regular-handler-0-post", ctx);
|
|
840
|
+
return Promise.resolve();
|
|
841
|
+
},
|
|
842
|
+
});
|
|
843
|
+
const request = new Request("http://example.com", { method: "POST", body: JSON.stringify({ x: 2, stop: true }) });
|
|
844
|
+
await evo.trigger({
|
|
845
|
+
send,
|
|
846
|
+
request,
|
|
847
|
+
});
|
|
848
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual([
|
|
849
|
+
"start",
|
|
850
|
+
"regular-handler-0-handle",
|
|
851
|
+
"regular-handler-0-post",
|
|
852
|
+
"error-handler-0-handle",
|
|
853
|
+
"error-handler-1-handle",
|
|
854
|
+
"done",
|
|
855
|
+
]);
|
|
856
|
+
expect(send.fn.mock.calls[3]).toEqual([
|
|
857
|
+
"error-handler-0-handle",
|
|
858
|
+
expect.objectContaining({
|
|
859
|
+
ctx: expect.anything(),
|
|
860
|
+
stats: expect.anything(),
|
|
861
|
+
encoder: reqRes,
|
|
862
|
+
enRequest: { x: 2, stop: true },
|
|
863
|
+
request,
|
|
864
|
+
error,
|
|
865
|
+
send: sendEvent,
|
|
866
|
+
validated: {
|
|
867
|
+
stop: true,
|
|
868
|
+
x: 2,
|
|
869
|
+
},
|
|
870
|
+
}),
|
|
871
|
+
]);
|
|
872
|
+
});
|
|
873
|
+
it("call post", async () => {
|
|
874
|
+
const evo = new Evento(reqRes);
|
|
875
|
+
const send = new TestSend();
|
|
876
|
+
evo.push({
|
|
877
|
+
hash: "regular-handler-0",
|
|
878
|
+
handle: async (ctx) => {
|
|
879
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
880
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
881
|
+
},
|
|
882
|
+
post: async (ctx) => {
|
|
883
|
+
send.fn("regular-handler-0-post", ctx);
|
|
884
|
+
return Promise.reject(new Error("test post-0 error"));
|
|
885
|
+
},
|
|
886
|
+
});
|
|
887
|
+
evo.push({
|
|
888
|
+
hash: "regular-handler-1",
|
|
889
|
+
handle: async (ctx) => {
|
|
890
|
+
send.fn("regular-handler-1-handle", ctx);
|
|
891
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
892
|
+
},
|
|
893
|
+
});
|
|
894
|
+
evo.push({
|
|
895
|
+
hash: "regular-handler-2",
|
|
896
|
+
handle: async (ctx) => {
|
|
897
|
+
send.fn("regular-handler-2-handle", ctx);
|
|
898
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
899
|
+
},
|
|
900
|
+
post: async (ctx) => {
|
|
901
|
+
send.fn("regular-handler-2-post", ctx);
|
|
902
|
+
return Promise.resolve();
|
|
903
|
+
},
|
|
904
|
+
});
|
|
905
|
+
const request = new Request("http://example.com", { method: "POST", body: JSON.stringify({ x: 2, stop: true }) });
|
|
906
|
+
await evo.trigger({
|
|
907
|
+
send,
|
|
908
|
+
request,
|
|
909
|
+
});
|
|
910
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual([
|
|
911
|
+
"start",
|
|
912
|
+
"regular-handler-0-handle",
|
|
913
|
+
"regular-handler-1-handle",
|
|
914
|
+
"regular-handler-2-handle",
|
|
915
|
+
"regular-handler-0-post",
|
|
916
|
+
"regular-handler-2-post",
|
|
917
|
+
"done",
|
|
918
|
+
]);
|
|
919
|
+
});
|
|
920
|
+
it("check stat", async () => {
|
|
921
|
+
const evo = new Evento(reqRes);
|
|
922
|
+
const send = new TestSend();
|
|
923
|
+
evo.push({
|
|
924
|
+
hash: "regular-handler-0",
|
|
925
|
+
validate: async (ctx) => {
|
|
926
|
+
send.fn("regular-handler-0-validate", ctx);
|
|
927
|
+
return sleep(10).then(() => Result.Ok(Option.Some({ x: 2, stop: true })));
|
|
928
|
+
},
|
|
929
|
+
handle: async (ctx) => {
|
|
930
|
+
send.fn("regular-handler-0-handle", ctx);
|
|
931
|
+
for (let i = 0; i < 3; i++) {
|
|
932
|
+
await ctx.send.send(ctx, {
|
|
933
|
+
response: "ok0",
|
|
934
|
+
req: ctx.validated,
|
|
935
|
+
cnt: i,
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
939
|
+
},
|
|
940
|
+
post: async (ctx) => {
|
|
941
|
+
send.fn("regular-handler-0-post", ctx);
|
|
942
|
+
return sleep(10).then(() => {
|
|
943
|
+
return;
|
|
944
|
+
});
|
|
945
|
+
},
|
|
946
|
+
});
|
|
947
|
+
evo.push({
|
|
948
|
+
hash: "regular-handler-1",
|
|
949
|
+
handle: async (ctx) => {
|
|
950
|
+
send.fn("regular-handler-1-handle", ctx);
|
|
951
|
+
for (let i = 0; i < 3; i++) {
|
|
952
|
+
await ctx.send.send(ctx, {
|
|
953
|
+
response: "ok1",
|
|
954
|
+
req: ctx.validated,
|
|
955
|
+
cnt: i,
|
|
956
|
+
});
|
|
957
|
+
}
|
|
958
|
+
return Promise.resolve(Result.Ok(EventoResult.Continue));
|
|
959
|
+
},
|
|
960
|
+
});
|
|
961
|
+
const request = new Request("http://example.com", { method: "POST", body: JSON.stringify({ x: 2, stop: true }) });
|
|
962
|
+
await evo.trigger({
|
|
963
|
+
send,
|
|
964
|
+
request,
|
|
965
|
+
});
|
|
966
|
+
expect(send.fn.mock.calls.map((i) => i[0])).toEqual([
|
|
967
|
+
"regular-handler-0-validate",
|
|
968
|
+
"start",
|
|
969
|
+
"regular-handler-0-handle",
|
|
970
|
+
"send",
|
|
971
|
+
"send",
|
|
972
|
+
"send",
|
|
973
|
+
"regular-handler-1-handle",
|
|
974
|
+
"send",
|
|
975
|
+
"send",
|
|
976
|
+
"send",
|
|
977
|
+
"regular-handler-0-post",
|
|
978
|
+
"done",
|
|
979
|
+
]);
|
|
980
|
+
const stats = send.fn.mock.calls[3][1].stats;
|
|
981
|
+
expect(stats.request.doneTime.getTime()).toBeGreaterThanOrEqual(stats.request.startTime.getTime());
|
|
982
|
+
expect(stats.encode.doneTime.getTime()).toBeGreaterThanOrEqual(stats.encode.startTime.getTime());
|
|
983
|
+
expect(stats.handlers.length).toBe(2);
|
|
984
|
+
for (const hdl of stats.handlers) {
|
|
985
|
+
expect(hdl.handled.doneTime.getTime()).toBeGreaterThanOrEqual(hdl.handled.startTime.getTime());
|
|
986
|
+
expect(hdl.handler.hash).toMatch(/regular-handler/);
|
|
987
|
+
expect(hdl.validated.doneTime.getTime()).toBeGreaterThanOrEqual(hdl.validated.startTime.getTime());
|
|
988
|
+
expect(hdl.total.doneTime.getTime()).toBeGreaterThanOrEqual(hdl.total.startTime.getTime());
|
|
989
|
+
}
|
|
990
|
+
expect(stats.send.doneTime.getTime()).toBeGreaterThanOrEqual(stats.send.startTime.getTime());
|
|
991
|
+
expect(stats.send.items.length).toBe(6);
|
|
992
|
+
let sendCount = 0;
|
|
993
|
+
for (const item of stats.send.items) {
|
|
994
|
+
expect(item.doneTime.getTime()).toBeGreaterThanOrEqual(item.startTime.getTime());
|
|
995
|
+
expect(item.item).toEqual(Result.Ok({
|
|
996
|
+
cnt: sendCount++ % 3,
|
|
997
|
+
encoderInfo: "test-encoder",
|
|
998
|
+
req: {
|
|
999
|
+
stop: true,
|
|
1000
|
+
x: 2,
|
|
1001
|
+
},
|
|
1002
|
+
response: `ok${sendCount > 3 ? 1 : 0}`,
|
|
1003
|
+
}));
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
519
1006
|
});
|
|
520
1007
|
//# sourceMappingURL=evento.test.js.map
|