@reventlessdev/reventless-gwt 1.0.0-alpha.291 → 1.0.0-alpha.293

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.
@@ -6,7 +6,6 @@
6
6
  // Also exercises DCB tag isolation: a second order is placed successfully even
7
7
  // though a first order is already on the shared log, because each command step
8
8
  // filters the log by its command's tags.
9
- // See `docs/plans/done/gwt-flow-and-extension-test-kinds.md` Phase 2.
10
9
 
11
10
  open Flow_GWT
12
11
 
@@ -49,11 +48,7 @@ module PlaceOrderDecision = {
49
48
 
50
49
  module AutoShipSlice = {
51
50
  let name = "AutoShip"
52
-
53
- @schema
54
- type consumedEvent =
55
- | OrderPlaced({orderId: string})
56
- | OrderShipped({orderId: string})
51
+ let moduleUrl = ""
57
52
 
58
53
  @schema
59
54
  type todoItem = {orderId: string}
@@ -61,19 +56,48 @@ module AutoShipSlice = {
61
56
  @schema
62
57
  type command = ShipOrder({orderId: string})
63
58
 
64
- let collect = event =>
65
- switch event {
66
- | OrderPlaced({orderId}) => [(orderId, ({orderId: orderId}: todoItem))]
67
- | OrderShipped(_) => []
68
- }
59
+ let maxRetries = 3
60
+ let targetName = "Order"
61
+ let schedule = None
62
+ }
69
63
 
70
- let resolve = event =>
71
- switch event {
72
- | OrderShipped({orderId}) => Some(orderId)
73
- | OrderPlaced(_) => None
74
- }
64
+ module AutoShipAutomation = {
65
+ module M = Reventless.AutomationSlice.Mappings.Make(AutoShipSlice)
66
+ module type Mapping = M.Mapping
67
+ let moduleUrl = ""
68
+
69
+ module Orders = {
70
+ module Id = Reventless.Id.String
71
+ let name = "Orders"
72
+ @schema
73
+ type event =
74
+ | OrderPlaced({orderId: string})
75
+ | OrderShipped({orderId: string})
76
+ }
77
+
78
+ module FromOrders = Reventless.AutomationSlice.Mapping.Make(
79
+ Orders,
80
+ AutoShipSlice,
81
+ {
82
+ let collect = (event, ~sourceId as _, _ctx) =>
83
+ switch event {
84
+ | Orders.OrderPlaced({orderId}) => [(orderId, ({orderId: orderId}: AutoShipSlice.todoItem))]
85
+ | OrderShipped(_) => []
86
+ }
87
+ let resolve = event =>
88
+ switch event {
89
+ | Orders.OrderShipped({orderId}) => Some(orderId)
90
+ | OrderPlaced(_) => None
91
+ }
92
+ let reschedule = _ => []
93
+ },
94
+ )
75
95
 
76
- let process = (id, _todo, ~now as _) => Some((id, ShipOrder({orderId: id})))
96
+ let mappings: array<module(Mapping)> = [module(FromOrders)]
97
+ let process = (id, _todo: AutoShipSlice.todoItem, ~now as _) => Some((
98
+ id,
99
+ AutoShipSlice.ShipOrder({orderId: id}),
100
+ ))
77
101
  let onExhausted = (_id, _todo, ~lastError as _) => None
78
102
  }
79
103
 
@@ -149,6 +173,7 @@ module OrdersViewProjection = {
149
173
 
150
174
  module ConfirmSlice = {
151
175
  let name = "Confirm"
176
+ let moduleUrl = ""
152
177
 
153
178
  @schema
154
179
  type consumedEvent = OrderPlaced({orderId: string})
@@ -159,33 +184,166 @@ module ConfirmSlice = {
159
184
  @schema
160
185
  type inboundCommand = unit
161
186
 
187
+ let maxRetries = 1
188
+ let targetName = None
189
+ let sourceNames = []
190
+ let externalSystem = None
191
+ let capabilityNeeds = []
192
+ let traits = []
193
+ }
194
+
195
+ module ConfirmTranslation = {
196
+ let moduleUrl = ""
197
+
162
198
  let collect = (event, ~sourceId as _) =>
163
199
  switch event {
164
- | OrderPlaced({orderId}) => [(orderId, ({orderId: orderId}: outboundItem))]
200
+ | ConfirmSlice.OrderPlaced({orderId}) => [
201
+ (orderId, ({orderId: orderId}: ConfirmSlice.outboundItem)),
202
+ ]
165
203
  }
204
+
205
+ let translate = async (_id, _item, ~capabilities as _) => Ok(None)
206
+ let onExhausted = (_id, _item, ~lastError as _) => None
207
+ }
208
+
209
+ // The same automation, whose command names its field `order` where the decision
210
+ // that runs it says `orderId`: a flow that types each side's command passes.
211
+ module AutoShipByOrderSlice = {
212
+ let name = "AutoShipByOrder"
213
+ let moduleUrl = ""
214
+
215
+ @schema
216
+ type todoItem = {orderId: string}
217
+
218
+ @schema
219
+ type command = ShipOrder({order: string})
220
+
221
+ let maxRetries = 3
222
+ let targetName = "Order"
223
+ let schedule = None
224
+ }
225
+
226
+ module AutoShipByOrderAutomation = {
227
+ module M = Reventless.AutomationSlice.Mappings.Make(AutoShipByOrderSlice)
228
+ module type Mapping = M.Mapping
229
+ let moduleUrl = ""
230
+
231
+ module FromOrders = Reventless.AutomationSlice.Mapping.Make(
232
+ AutoShipAutomation.Orders,
233
+ AutoShipByOrderSlice,
234
+ {
235
+ let collect = (event, ~sourceId as _, _ctx) =>
236
+ switch event {
237
+ | AutoShipAutomation.Orders.OrderPlaced({orderId}) => [
238
+ (orderId, ({orderId: orderId}: AutoShipByOrderSlice.todoItem)),
239
+ ]
240
+ | OrderShipped(_) => []
241
+ }
242
+ let resolve = event =>
243
+ switch event {
244
+ | AutoShipAutomation.Orders.OrderShipped({orderId}) => Some(orderId)
245
+ | OrderPlaced(_) => None
246
+ }
247
+ let reschedule = _ => []
248
+ },
249
+ )
250
+
251
+ let mappings: array<module(Mapping)> = [module(FromOrders)]
252
+ let process = (id, _todo: AutoShipByOrderSlice.todoItem, ~now as _) => Some((
253
+ id,
254
+ AutoShipByOrderSlice.ShipOrder({order: id}),
255
+ ))
256
+ let onExhausted = (_id, _todo, ~lastError as _) => None
257
+ }
258
+
259
+ // A reminder an hour after an order is placed, unless it ships first: the
260
+ // schedule is set by the event, keyed by the order, and its tick makes the item.
261
+ module RemindSlice = {
262
+ let name = "Remind"
263
+ let moduleUrl = ""
264
+
265
+ @schema
266
+ type todoItem = {orderId: string}
267
+
268
+ @schema
269
+ type command = RemindCustomer({orderId: string})
270
+
271
+ let maxRetries = 3
272
+ let targetName = "Order"
273
+ let schedule = None
274
+ }
275
+
276
+ module RemindAutomation = {
277
+ module M = Reventless.AutomationSlice.Mappings.Make(RemindSlice)
278
+ module type Mapping = M.Mapping
279
+ let moduleUrl = ""
280
+
281
+ module FromClock = Reventless.AutomationSlice.Mapping.Make(
282
+ Reventless.Clock,
283
+ RemindSlice,
284
+ {
285
+ open Reventless.Clock
286
+ let collect = (Tick({key, at}), ~sourceId as _, _ctx) => [
287
+ (itemId(key, at), ({orderId: key}: RemindSlice.todoItem)),
288
+ ]
289
+ let resolve = _ => None
290
+ let reschedule = _ => []
291
+ },
292
+ )
293
+
294
+ module FromOrders = Reventless.AutomationSlice.Mapping.Make(
295
+ AutoShipAutomation.Orders,
296
+ RemindSlice,
297
+ {
298
+ let collect = (_, ~sourceId as _, _ctx) => []
299
+ let resolve = _ => None
300
+ let reschedule = event =>
301
+ switch event {
302
+ | AutoShipAutomation.Orders.OrderPlaced({orderId}) => [
303
+ Reventless.AutomationSlice.Set({
304
+ key: orderId,
305
+ time: Once({moment: After(Hours(1))}),
306
+ }),
307
+ ]
308
+ | OrderShipped({orderId}) => [Clear({key: orderId})]
309
+ }
310
+ },
311
+ )
312
+
313
+ let mappings: array<module(Mapping)> = [module(FromClock), module(FromOrders)]
314
+ let process = (id, item: RemindSlice.todoItem, ~now as _) => Some((
315
+ id,
316
+ RemindSlice.RemindCustomer({orderId: item.orderId}),
317
+ ))
318
+ let onExhausted = (_id, _item, ~lastError as _) => None
166
319
  }
167
320
 
168
321
  // -- Step modules ------------------------------------------------------------
169
322
 
170
- module Place = CommandStep(PlaceOrderSlice, PlaceOrderDecision)
171
- module Auto = AutomationStep(AutoShipSlice)
172
- module Ship = CommandStep(ShipOrderSlice, ShipOrderDecision)
323
+ module Place = DecisionStep(PlaceOrderSlice, PlaceOrderDecision)
324
+ module Auto = AutomationStep(AutoShipSlice, AutoShipAutomation)
325
+ module Ship = DecisionStep(ShipOrderSlice, ShipOrderDecision)
173
326
  module View = ViewStep(OrdersViewSlice, OrdersViewProjection)
174
- module Confirm = OutboundStep(ConfirmSlice)
327
+ module Confirm = OutboundStep(ConfirmSlice, ConfirmTranslation)
328
+ module AutoByOrder = AutomationStep(AutoShipByOrderSlice, AutoShipByOrderAutomation)
329
+ module Remind = AutomationStep(RemindSlice, RemindAutomation)
175
330
 
176
331
  // -- Flows -------------------------------------------------------------------
177
332
 
178
333
  describe("Order flow (single plugin)", () => {
179
- test("place → auto-ship → ship → view Shipped → confirmation fired", () =>
180
- start
181
- ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
182
- ->Place.thenEvents([PlaceOrderSlice.OrderPlaced({orderId: "o1", item: "book"})])
183
- ->Auto.whenReacts
184
- ->Auto.thenIssuesCommand(AutoShipSlice.ShipOrder({orderId: "o1"}))
185
- ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
186
- ->Ship.thenEvents([ShipOrderSlice.OrderShipped({orderId: "o1"})])
187
- ->View.thenViewState("o1", {OrdersViewSlice.orderId: "o1", item: "book", status: "Shipped"})
188
- ->Confirm.thenOutbound([("o1", {ConfirmSlice.orderId: "o1"})])
334
+ test(
335
+ "place → auto-ship → ship → view Shipped → confirmation fired",
336
+ ~tells="An order ships",
337
+ () =>
338
+ start
339
+ ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
340
+ ->Place.thenEvents([PlaceOrderSlice.OrderPlaced({orderId: "o1", item: "book"})])
341
+ ->Auto.whenReacts
342
+ ->Auto.thenIssuesCommand(AutoShipSlice.ShipOrder({orderId: "o1"}))
343
+ ->Ship.whenIssued
344
+ ->Ship.thenEvents([ShipOrderSlice.OrderShipped({orderId: "o1"})])
345
+ ->View.thenRow("o1", {OrdersViewSlice.orderId: "o1", item: "book", status: "Shipped"})
346
+ ->Confirm.thenOutbound([("o1", {ConfirmSlice.orderId: "o1"})]),
189
347
  )
190
348
 
191
349
  test("automation does not re-issue ShipOrder once the order is shipped", () =>
@@ -216,7 +374,377 @@ describe("Order flow (single plugin)", () => {
216
374
  ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
217
375
  ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o2", item: "pen"}))
218
376
  ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
219
- ->View.thenViewState("o1", {OrdersViewSlice.orderId: "o1", item: "book", status: "Shipped"})
220
- ->View.thenViewState("o2", {OrdersViewSlice.orderId: "o2", item: "pen", status: "Placed"})
377
+ ->View.thenRow("o1", {OrdersViewSlice.orderId: "o1", item: "book", status: "Shipped"})
378
+ ->View.thenRow("o2", {OrdersViewSlice.orderId: "o2", item: "pen", status: "Placed"})
221
379
  )
222
380
  })
381
+
382
+ // -- A failure names its step ------------------------------------------------
383
+
384
+ describe("a flow that fails at its sixth step", () => {
385
+ let failed = async () =>
386
+ (
387
+ await start
388
+ ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
389
+ ->Place.thenEvents([PlaceOrderSlice.OrderPlaced({orderId: "o1", item: "book"})])
390
+ ->Auto.whenReacts
391
+ ->Auto.thenIssuesCommand(AutoShipSlice.ShipOrder({orderId: "o1"}))
392
+ ->Ship.whenIssued
393
+ // Wrong on purpose: the order shipped is o1.
394
+ ->Ship.thenEvent(ShipOrderSlice.OrderShipped({orderId: "o2"}))
395
+ ->View.thenRow("o1", {OrdersViewSlice.orderId: "o1", item: "book", status: "Placed"})
396
+ ->Confirm.thenOutbound([("o1", {ConfirmSlice.orderId: "o1"})])
397
+ )->outcomeOf
398
+
399
+ let mismatchOf = async () =>
400
+ switch await failed() {
401
+ | Error(m) => m
402
+ | Ok() => JsError.throwWithMessage("the flow was meant to fail")
403
+ }
404
+
405
+ JestGlobals.testPromise("the mismatch carries the step, not a later one", async () => {
406
+ let m = await mismatchOf()
407
+ JestGlobals.expect(Outcome.stepOf(m))->JestGlobals.toEqual(
408
+ Some({
409
+ Outcome.number: 6,
410
+ total: 8,
411
+ verb: "thenEvent",
412
+ part: {Outcome.kind: Decision, name: "ShipOrder"},
413
+ }),
414
+ )
415
+ })
416
+
417
+ JestGlobals.testPromise("the hint names the step and points at its part", async () => {
418
+ let hint = Hint.forMismatch(~slice="Flow", await mismatchOf())
419
+ JestGlobals.expect(hint.locus)->JestGlobals.toBe("ShipOrder.decide")
420
+ JestGlobals.expect(
421
+ hint->Hint.format->String.startsWith("At step 6 of 8, ShipOrder.thenEvent.\n"),
422
+ )->JestGlobals.toBe(true)
423
+ })
424
+
425
+ JestGlobals.testPromise("the JSON hint carries the step", async () => {
426
+ let json = Hint.forMismatch(~slice="Flow", await mismatchOf())->Hint.toJson
427
+ JestGlobals.expect(
428
+ JSON.stringify(
429
+ json,
430
+ )->String.includes(`"step":{"number":6,"total":8,"verb":"thenEvent","part":{"kind":"Decision","name":"ShipOrder"}}`),
431
+ )->JestGlobals.toBe(true)
432
+ })
433
+
434
+ JestGlobals.testPromise("jest's failure message says the step", async () => {
435
+ let outcome = await failed()
436
+ let message = switch JestBind.assertOutcome(~slice="Flow", outcome) {
437
+ | () => ""
438
+ | exception JsExn(e) => e->JsExn.message->Option.getOr("")
439
+ }
440
+ JestGlobals.expect(
441
+ message->String.includes("At step 6 of 8, ShipOrder.thenEvent."),
442
+ )->JestGlobals.toBe(true)
443
+ })
444
+
445
+ JestGlobals.testPromise("a flow that passes has no step to name", async () => {
446
+ let outcome = (
447
+ await start
448
+ ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
449
+ ->Place.thenEvent(PlaceOrderSlice.OrderPlaced({orderId: "o1", item: "book"}))
450
+ )->outcomeOf
451
+ JestGlobals.expect(outcome)->JestGlobals.toEqual(Outcome.pass)
452
+ })
453
+ })
454
+
455
+ // -- What was sent is what runs ----------------------------------------------
456
+
457
+ let failureOf = async (flowP: flow) =>
458
+ switch (await flowP)->outcomeOf {
459
+ | Error(m) => Some(m)
460
+ | Ok() => None
461
+ }
462
+
463
+ let placed = () =>
464
+ start
465
+ ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
466
+ ->Place.thenEvent(PlaceOrderSlice.OrderPlaced({orderId: "o1", item: "book"}))
467
+
468
+ describe("a command sent by one part and run by the next", () => {
469
+ JestGlobals.testPromise("typed twice, each side to its own type, it passes", async () => {
470
+ let failure = await placed()
471
+ ->AutoByOrder.whenReacts
472
+ ->AutoByOrder.thenIssuesCommand(AutoShipByOrderSlice.ShipOrder({order: "o1"}))
473
+ ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
474
+ ->Ship.thenEvent(ShipOrderSlice.OrderShipped({orderId: "o1"}))
475
+ ->failureOf
476
+ JestGlobals.expect(failure)->JestGlobals.toEqual(None)
477
+ })
478
+
479
+ JestGlobals.testPromise("run as sent, the field the decision cannot read fails it", async () => {
480
+ let failure = await placed()
481
+ ->AutoByOrder.whenReacts
482
+ ->AutoByOrder.thenIssuesCommand(AutoShipByOrderSlice.ShipOrder({order: "o1"}))
483
+ ->Ship.whenIssued
484
+ ->Ship.thenEvent(ShipOrderSlice.OrderShipped({orderId: "o1"}))
485
+ ->failureOf
486
+ switch failure {
487
+ | Some(TranslateError({actual: Some(reason), ?step})) => {
488
+ JestGlobals.expect(
489
+ reason->String.startsWith(`ShipOrder cannot read {"TAG":"ShipOrder","order":"o1"}`),
490
+ )->JestGlobals.toBe(true)
491
+ JestGlobals.expect(step->Option.map(s => s.verb))->JestGlobals.toEqual(Some("whenIssued"))
492
+ }
493
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
494
+ }
495
+ })
496
+
497
+ JestGlobals.testPromise("~only past what was sent fails, naming how many were", async () => {
498
+ let failure = await placed()->Auto.whenReacts->Ship.whenIssued(~only=2)->failureOf
499
+ switch failure {
500
+ | Some(TranslateError({actual: Some(reason)})) =>
501
+ JestGlobals.expect(reason)->JestGlobals.toBe("~only=2, but 1 command(s) were sent")
502
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
503
+ }
504
+ })
505
+
506
+ JestGlobals.testPromise("nothing sent is nothing to run", async () => {
507
+ let failure = await start->Ship.whenIssued->failureOf
508
+ switch failure {
509
+ | Some(TranslateError({actual: Some(reason)})) =>
510
+ JestGlobals.expect(reason)->JestGlobals.toBe("the step before sent no command")
511
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
512
+ }
513
+ })
514
+ })
515
+
516
+ // -- The expectations a flow lacked ------------------------------------------
517
+
518
+ describe("what a flow can expect besides whole events and rows", () => {
519
+ test("a decision that records nothing", () =>
520
+ placed()
521
+ ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
522
+ ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
523
+ ->Ship.thenNoEvent
524
+ )
525
+
526
+ test("a decision that did not refuse, its events not compared", () =>
527
+ placed()->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))->Ship.thenAccepted
528
+ )
529
+
530
+ test("no row for an order never placed", () => placed()->View.thenNoRow("o9"))
531
+
532
+ test("some fields of the row", () =>
533
+ placed()->View.thenRowShows("o1", row => {"status": row.status}, {"status": "Placed"})
534
+ )
535
+
536
+ JestGlobals.testPromise("a field picked and wrong fails, showing what was picked", async () => {
537
+ let failure = await placed()
538
+ ->View.thenRowShows("o1", row => {"status": row.status}, {"status": "Shipped"})
539
+ ->failureOf
540
+ switch failure {
541
+ | Some(StateMismatch({key, expected, actual})) => {
542
+ JestGlobals.expect(key)->JestGlobals.toBe("o1")
543
+ JestGlobals.expect(expected->Option.map(j => JSON.stringify(j)))->JestGlobals.toEqual(
544
+ Some(`[{"status":"Shipped"}]`),
545
+ )
546
+ JestGlobals.expect(actual->Option.map(j => JSON.stringify(j)))->JestGlobals.toEqual(
547
+ Some(`[{"status":"Placed"}]`),
548
+ )
549
+ }
550
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
551
+ }
552
+ })
553
+
554
+ JestGlobals.testPromise("thenAccepted fails on a refusal", async () => {
555
+ let failure = await start
556
+ ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
557
+ ->Ship.thenAccepted
558
+ ->failureOf
559
+ JestGlobals.expect(failure->Option.map(Outcome.kindName))->JestGlobals.toEqual(
560
+ Some("ErrorMismatch"),
561
+ )
562
+ })
563
+ })
564
+
565
+ // -- A flow keeps time -------------------------------------------------------
566
+
567
+ let placedAtNine = () =>
568
+ start->Place.whenCommand(
569
+ ~at="2026-03-02T09:00:00Z",
570
+ PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}),
571
+ )
572
+
573
+ describe("a flow keeps time", () => {
574
+ test("a schedule an event set fires an hour later, and not before", () =>
575
+ placedAtNine()
576
+ ->Remind.whenTimePasses(~until="2026-03-02T09:59:00Z")
577
+ ->Remind.thenIssuesNoCommand
578
+ ->Remind.whenTimePasses(~until="2026-03-02T10:00:00Z")
579
+ ->Remind.thenIssuesCommand(RemindSlice.RemindCustomer({orderId: "o1"}))
580
+ // Once only: it does not fire again.
581
+ ->Remind.whenTimePasses(~until="2026-03-03T10:00:00Z")
582
+ ->Remind.thenIssuesNoCommand
583
+ )
584
+
585
+ test("an event that clears the schedule before it is due stops it", () =>
586
+ placedAtNine()
587
+ ->Ship.whenCommand(~at="2026-03-02T09:30:00Z", ShipOrderSlice.ShipOrder({orderId: "o1"}))
588
+ ->Remind.whenTimePasses(~until="2026-03-02T11:00:00Z")
589
+ ->Remind.thenIssuesNoCommand
590
+ )
591
+
592
+ JestGlobals.testPromise("time only moves forward", async () => {
593
+ let failure = await placedAtNine()
594
+ ->Ship.whenCommand(~at="2026-03-02T08:00:00Z", ShipOrderSlice.ShipOrder({orderId: "o1"}))
595
+ ->failureOf
596
+ switch failure {
597
+ | Some(Throw({error, ?step})) => {
598
+ JestGlobals.expect(
599
+ error,
600
+ )->JestGlobals.toBe(`~at="2026-03-02T08:00:00Z" is before the flow's time, 2026-03-02T09:00:00Z: time only moves forward`)
601
+ JestGlobals.expect(step->Option.map(s => s.number))->JestGlobals.toEqual(Some(2))
602
+ }
603
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
604
+ }
605
+ })
606
+ })
607
+
608
+ // -- Read and acted as a person ----------------------------------------------
609
+
610
+ describe("a flow's caller", () => {
611
+ test("a signed-out caller reads no row of a view that wants a signed-in one", () =>
612
+ placed()->asCaller(Caller.anonymous)->View.thenNoRow("o1")
613
+ )
614
+
615
+ JestGlobals.testPromise("a signed-in caller reads the row", async () => {
616
+ let failure = await placed()->asCaller(Caller.owner("c1"))->View.thenNoRow("o1")->failureOf
617
+ JestGlobals.expect(failure->Option.map(Outcome.kindName))->JestGlobals.toEqual(
618
+ Some("StateMismatch"),
619
+ )
620
+ })
621
+
622
+ test("a signed-out caller's command is refused before its decision runs", () =>
623
+ start
624
+ ->asCaller(Caller.anonymous)
625
+ ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
626
+ ->Place.thenForbidden
627
+ // Refused, so nothing was recorded: the row is not there.
628
+ ->View.thenNoRow("o1")
629
+ )
630
+
631
+ JestGlobals.testPromise("thenForbidden fails when the command was decided", async () => {
632
+ let failure = await start
633
+ ->asCaller(Caller.owner("c1"))
634
+ ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
635
+ ->Place.thenForbidden
636
+ ->failureOf
637
+ JestGlobals.expect(failure->Option.map(Outcome.kindName))->JestGlobals.toEqual(
638
+ Some("ErrorMismatch"),
639
+ )
640
+ })
641
+ })
642
+
643
+ // -- What reacts reacts once -------------------------------------------------
644
+
645
+ // The order's parts, as a plugin's generated `Flow` module lists them.
646
+ let orderParts: parts = {
647
+ reacting: [Auto.reacting, AutoByOrder.reacting],
648
+ deciding: [Place.deciding, Ship.deciding],
649
+ }
650
+ let shippingParts: parts = {reacting: [Auto.reacting], deciding: [Place.deciding, Ship.deciding]}
651
+
652
+ describe("a reaction step called twice", () => {
653
+ test("reports only what is new since that automation's last step", () =>
654
+ placed()
655
+ ->Auto.whenReacts
656
+ ->Auto.thenIssuesCommand(AutoShipSlice.ShipOrder({orderId: "o1"}))
657
+ ->Auto.whenReacts
658
+ ->Auto.thenIssuesNoCommand
659
+ )
660
+ })
661
+
662
+ describe("nothing else reacts", () => {
663
+ JestGlobals.testPromise(
664
+ "an automation the flow did not name, that would send, fails it",
665
+ async () => {
666
+ let failure = await placed()
667
+ ->Auto.whenReacts
668
+ ->Auto.thenIssuesCommand(AutoShipSlice.ShipOrder({orderId: "o1"}))
669
+ ->thenNothingElseReacts([orderParts])
670
+ ->failureOf
671
+ switch failure {
672
+ | Some(PublishedActionsMismatch({actual})) =>
673
+ JestGlobals.expect(actual->Array.map(j => JSON.stringify(j)))->JestGlobals.toEqual([
674
+ `{"reacts":"AutoShipByOrder","sends":{"TAG":"ShipOrder","order":"o1"}}`,
675
+ ])
676
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
677
+ }
678
+ },
679
+ )
680
+
681
+ test("with both named, nothing else reacts", () =>
682
+ placed()
683
+ ->Auto.whenReacts
684
+ ->Auto.thenIssuesCommand(AutoShipSlice.ShipOrder({orderId: "o1"}))
685
+ ->AutoByOrder.whenReacts
686
+ ->AutoByOrder.thenIssuesCommand(AutoShipByOrderSlice.ShipOrder({order: "o1"}))
687
+ ->thenNothingElseReacts([orderParts])
688
+ )
689
+ })
690
+
691
+ describe("the app does the rest", () => {
692
+ test("the automation ships the order and the decision records it", () =>
693
+ placed()
694
+ ->whenSettled([shippingParts])
695
+ ->View.thenRow("o1", {OrdersViewSlice.orderId: "o1", item: "book", status: "Shipped"})
696
+ ->thenNothingElseReacts([shippingParts])
697
+ )
698
+
699
+ JestGlobals.testPromise("a command no decision reads stops it, named", async () => {
700
+ let failure = await placed()->whenSettled([orderParts])->failureOf
701
+ switch failure {
702
+ | Some(TranslateError({actual: Some(reason)})) =>
703
+ JestGlobals.expect(
704
+ reason,
705
+ )->JestGlobals.toBe(`no decision reads {"TAG":"ShipOrder","order":"o1"}`)
706
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
707
+ }
708
+ })
709
+ })
710
+
711
+ // -- A flow starts after another ---------------------------------------------
712
+
713
+ describe("a flow that starts after another", () => {
714
+ test("an order is placed", () => placed())
715
+
716
+ test("it is shipped, on the record the placing left", () =>
717
+ after("an order is placed")
718
+ ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
719
+ ->Ship.thenEvent(ShipOrderSlice.OrderShipped({orderId: "o1"}))
720
+ )
721
+
722
+ // A failing flow, kept out of jest's list so that it fails only here.
723
+ flows->Dict.set("an order placed twice", () =>
724
+ placed()
725
+ ->Place.whenCommand(PlaceOrderSlice.PlaceOrder({orderId: "o1", item: "book"}))
726
+ ->Place.thenEvent(PlaceOrderSlice.OrderPlaced({orderId: "o1", item: "book"}))
727
+ )
728
+
729
+ JestGlobals.testPromise("after a failing flow, it is not run, and says so", async () => {
730
+ let failure = await after("an order placed twice")
731
+ ->Ship.whenCommand(ShipOrderSlice.ShipOrder({orderId: "o1"}))
732
+ ->Ship.thenEvent(ShipOrderSlice.OrderShipped({orderId: "o1"}))
733
+ ->failureOf
734
+ switch failure {
735
+ | Some(Throw({error, ?step})) => {
736
+ JestGlobals.expect(
737
+ error,
738
+ )->JestGlobals.toBe(`not run: the flow it starts after, "an order placed twice", failed`)
739
+ // Not a step of its own: nothing it did failed.
740
+ JestGlobals.expect(step)->JestGlobals.toEqual(None)
741
+ }
742
+ | other => JestGlobals.expect(other)->JestGlobals.toEqual(None)
743
+ }
744
+ })
745
+
746
+ JestGlobals.testPromise("after a title no flow has, it is not run", async () => {
747
+ let failure = await after("no such flow")->failureOf
748
+ JestGlobals.expect(failure->Option.map(Outcome.kindName))->JestGlobals.toEqual(Some("Throw"))
749
+ })
750
+ })