@cratis/chronicle 6.57.0 → 6.58.0

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.
Files changed (234) hide show
  1. package/EventStore.ts +2 -2
  2. package/dist/EventStore.js +2 -2
  3. package/dist/EventStore.js.map +1 -1
  4. package/dist/observation/batches/BatchContext.d.ts +43 -0
  5. package/dist/observation/batches/BatchContext.d.ts.map +1 -0
  6. package/dist/observation/batches/BatchContext.js +60 -0
  7. package/dist/observation/batches/BatchContext.js.map +1 -0
  8. package/dist/observation/batches/BatchContextObserver.d.ts +14 -0
  9. package/dist/observation/batches/BatchContextObserver.d.ts.map +1 -0
  10. package/dist/observation/batches/BatchContextObserver.js +4 -0
  11. package/dist/observation/batches/BatchContextObserver.js.map +1 -0
  12. package/dist/observation/batches/BatchContextValues.d.ts +13 -0
  13. package/dist/observation/batches/BatchContextValues.d.ts.map +1 -0
  14. package/dist/observation/batches/BatchContextValues.js +4 -0
  15. package/dist/observation/batches/BatchContextValues.js.map +1 -0
  16. package/dist/observation/batches/BatchMethod.d.ts +9 -0
  17. package/dist/observation/batches/BatchMethod.d.ts.map +1 -0
  18. package/dist/observation/batches/BatchMethod.js +4 -0
  19. package/dist/observation/batches/BatchMethod.js.map +1 -0
  20. package/dist/observation/batches/BatchMethodReturnedAValue.d.ts +12 -0
  21. package/dist/observation/batches/BatchMethodReturnedAValue.d.ts.map +1 -0
  22. package/dist/observation/batches/BatchMethodReturnedAValue.js +20 -0
  23. package/dist/observation/batches/BatchMethodReturnedAValue.js.map +1 -0
  24. package/dist/observation/batches/BatchMethods.d.ts +31 -0
  25. package/dist/observation/batches/BatchMethods.d.ts.map +1 -0
  26. package/dist/observation/batches/BatchMethods.js +81 -0
  27. package/dist/observation/batches/BatchMethods.js.map +1 -0
  28. package/dist/observation/batches/BatchOutcome.d.ts +15 -0
  29. package/dist/observation/batches/BatchOutcome.d.ts.map +1 -0
  30. package/dist/observation/batches/BatchOutcome.js +4 -0
  31. package/dist/observation/batches/BatchOutcome.js.map +1 -0
  32. package/dist/observation/batches/BatchOutcomeKind.d.ts +8 -0
  33. package/dist/observation/batches/BatchOutcomeKind.d.ts.map +1 -0
  34. package/dist/observation/batches/BatchOutcomeKind.js +11 -0
  35. package/dist/observation/batches/BatchOutcomeKind.js.map +1 -0
  36. package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts +13 -0
  37. package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts.map +1 -0
  38. package/dist/observation/batches/DeferralNotSupportedByKernel.js +19 -0
  39. package/dist/observation/batches/DeferralNotSupportedByKernel.js.map +1 -0
  40. package/dist/observation/batches/DeliveredPartition.d.ts +11 -0
  41. package/dist/observation/batches/DeliveredPartition.d.ts.map +1 -0
  42. package/dist/observation/batches/DeliveredPartition.js +4 -0
  43. package/dist/observation/batches/DeliveredPartition.js.map +1 -0
  44. package/dist/observation/batches/EventAndContext.d.ts +28 -0
  45. package/dist/observation/batches/EventAndContext.d.ts.map +1 -0
  46. package/dist/observation/batches/EventAndContext.js +35 -0
  47. package/dist/observation/batches/EventAndContext.js.map +1 -0
  48. package/dist/observation/batches/EventBatch.d.ts +52 -0
  49. package/dist/observation/batches/EventBatch.d.ts.map +1 -0
  50. package/dist/observation/batches/EventBatch.js +92 -0
  51. package/dist/observation/batches/EventBatch.js.map +1 -0
  52. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts +14 -0
  53. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts.map +1 -0
  54. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js +23 -0
  55. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js.map +1 -0
  56. package/dist/observation/batches/InvalidBatchMethod.d.ts +13 -0
  57. package/dist/observation/batches/InvalidBatchMethod.d.ts.map +1 -0
  58. package/dist/observation/batches/InvalidBatchMethod.js +20 -0
  59. package/dist/observation/batches/InvalidBatchMethod.js.map +1 -0
  60. package/dist/observation/batches/ObservationOrigin.d.ts +21 -0
  61. package/dist/observation/batches/ObservationOrigin.d.ts.map +1 -0
  62. package/dist/observation/batches/ObservationOrigin.js +32 -0
  63. package/dist/observation/batches/ObservationOrigin.js.map +1 -0
  64. package/dist/observation/batches/ObservedEventType.d.ts +11 -0
  65. package/dist/observation/batches/ObservedEventType.d.ts.map +1 -0
  66. package/dist/observation/batches/ObservedEventType.js +4 -0
  67. package/dist/observation/batches/ObservedEventType.js.map +1 -0
  68. package/dist/observation/batches/PageMessages.d.ts +22 -0
  69. package/dist/observation/batches/PageMessages.d.ts.map +1 -0
  70. package/dist/observation/batches/PageMessages.js +32 -0
  71. package/dist/observation/batches/PageMessages.js.map +1 -0
  72. package/dist/observation/batches/PartitionFailedInBatch.d.ts +10 -0
  73. package/dist/observation/batches/PartitionFailedInBatch.d.ts.map +1 -0
  74. package/dist/observation/batches/PartitionFailedInBatch.js +16 -0
  75. package/dist/observation/batches/PartitionFailedInBatch.js.map +1 -0
  76. package/dist/observation/batches/PartitionNotInBatch.d.ts +10 -0
  77. package/dist/observation/batches/PartitionNotInBatch.d.ts.map +1 -0
  78. package/dist/observation/batches/PartitionNotInBatch.js +16 -0
  79. package/dist/observation/batches/PartitionNotInBatch.js.map +1 -0
  80. package/dist/observation/batches/PartitionOutcome.d.ts +19 -0
  81. package/dist/observation/batches/PartitionOutcome.d.ts.map +1 -0
  82. package/dist/observation/batches/PartitionOutcome.js +4 -0
  83. package/dist/observation/batches/PartitionOutcome.js.map +1 -0
  84. package/dist/observation/batches/PartitionProblem.d.ts +17 -0
  85. package/dist/observation/batches/PartitionProblem.d.ts.map +1 -0
  86. package/dist/observation/batches/PartitionProblem.js +4 -0
  87. package/dist/observation/batches/PartitionProblem.js.map +1 -0
  88. package/dist/observation/batches/PartitionSettlement.d.ts +29 -0
  89. package/dist/observation/batches/PartitionSettlement.d.ts.map +1 -0
  90. package/dist/observation/batches/PartitionSettlement.js +64 -0
  91. package/dist/observation/batches/PartitionSettlement.js.map +1 -0
  92. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts +12 -0
  93. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts.map +1 -0
  94. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js +20 -0
  95. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js.map +1 -0
  96. package/dist/observation/batches/batchSize.d.ts +42 -0
  97. package/dist/observation/batches/batchSize.d.ts.map +1 -0
  98. package/dist/observation/batches/batchSize.js +55 -0
  99. package/dist/observation/batches/batchSize.js.map +1 -0
  100. package/dist/observation/batches/createBatchContext.d.ts +13 -0
  101. package/dist/observation/batches/createBatchContext.d.ts.map +1 -0
  102. package/dist/observation/batches/createBatchContext.js +27 -0
  103. package/dist/observation/batches/createBatchContext.js.map +1 -0
  104. package/dist/observation/batches/observes.d.ts +39 -0
  105. package/dist/observation/batches/observes.d.ts.map +1 -0
  106. package/dist/observation/batches/observes.js +63 -0
  107. package/dist/observation/batches/observes.js.map +1 -0
  108. package/dist/observation/batches/runConcurrently.d.ts +9 -0
  109. package/dist/observation/batches/runConcurrently.d.ts.map +1 -0
  110. package/dist/observation/batches/runConcurrently.js +22 -0
  111. package/dist/observation/batches/runConcurrently.js.map +1 -0
  112. package/dist/observation/createHandlerInstanceValidator.d.ts +1 -1
  113. package/dist/observation/createHandlerInstanceValidator.d.ts.map +1 -1
  114. package/dist/observation/createHandlerInstanceValidator.js +5 -7
  115. package/dist/observation/createHandlerInstanceValidator.js.map +1 -1
  116. package/dist/observation/getInstanceMethods.d.ts +3 -1
  117. package/dist/observation/getInstanceMethods.d.ts.map +1 -1
  118. package/dist/observation/getInstanceMethods.js +13 -4
  119. package/dist/observation/getInstanceMethods.js.map +1 -1
  120. package/dist/observation/index.d.ts +14 -0
  121. package/dist/observation/index.d.ts.map +1 -1
  122. package/dist/observation/index.js +13 -0
  123. package/dist/observation/index.js.map +1 -1
  124. package/dist/reactors/ReactorDispatcher.d.ts +15 -0
  125. package/dist/reactors/ReactorDispatcher.d.ts.map +1 -1
  126. package/dist/reactors/ReactorDispatcher.js +25 -2
  127. package/dist/reactors/ReactorDispatcher.js.map +1 -1
  128. package/dist/reactors/ReactorObservationTarget.d.ts +45 -0
  129. package/dist/reactors/ReactorObservationTarget.d.ts.map +1 -0
  130. package/dist/reactors/ReactorObservationTarget.js +4 -0
  131. package/dist/reactors/ReactorObservationTarget.js.map +1 -0
  132. package/dist/reactors/ReactorPageDispatcher.d.ts +33 -0
  133. package/dist/reactors/ReactorPageDispatcher.d.ts.map +1 -0
  134. package/dist/reactors/ReactorPageDispatcher.js +293 -0
  135. package/dist/reactors/ReactorPageDispatcher.js.map +1 -0
  136. package/dist/reactors/Reactors.d.ts.map +1 -1
  137. package/dist/reactors/Reactors.js +21 -149
  138. package/dist/reactors/Reactors.js.map +1 -1
  139. package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts +14 -0
  140. package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts.map +1 -0
  141. package/dist/reactors/for_Reactors/given/observe_reactor_messages.js +61 -0
  142. package/dist/reactors/for_Reactors/given/observe_reactor_messages.js.map +1 -0
  143. package/dist/reactors/index.d.ts +2 -0
  144. package/dist/reactors/index.d.ts.map +1 -1
  145. package/dist/reactors/index.js +1 -0
  146. package/dist/reactors/index.js.map +1 -1
  147. package/dist/reducers/ReducerEventDispatcher.d.ts +26 -1
  148. package/dist/reducers/ReducerEventDispatcher.d.ts.map +1 -1
  149. package/dist/reducers/ReducerEventDispatcher.js +35 -3
  150. package/dist/reducers/ReducerEventDispatcher.js.map +1 -1
  151. package/dist/reducers/Reducers.d.ts +2 -0
  152. package/dist/reducers/Reducers.d.ts.map +1 -1
  153. package/dist/reducers/Reducers.js +94 -22
  154. package/dist/reducers/Reducers.js.map +1 -1
  155. package/dist/reducers/ReductionOutcome.d.ts +27 -3
  156. package/dist/reducers/ReductionOutcome.d.ts.map +1 -1
  157. package/dist/reducers/ReductionOutcome.js +51 -7
  158. package/dist/reducers/ReductionOutcome.js.map +1 -1
  159. package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts +19 -0
  160. package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts.map +1 -0
  161. package/dist/reducers/for_Reducers/given/observe_reducer_messages.js +61 -0
  162. package/dist/reducers/for_Reducers/given/observe_reducer_messages.js.map +1 -0
  163. package/dist/reducers/index.d.ts +2 -0
  164. package/dist/reducers/index.d.ts.map +1 -1
  165. package/dist/reducers/index.js +1 -0
  166. package/dist/reducers/index.js.map +1 -1
  167. package/dist/testing/ReactorScenario.d.ts.map +1 -1
  168. package/dist/testing/ReactorScenario.js +4 -1
  169. package/dist/testing/ReactorScenario.js.map +1 -1
  170. package/dist/testing/ReducerReadModelProcessor.d.ts +1 -0
  171. package/dist/testing/ReducerReadModelProcessor.d.ts.map +1 -1
  172. package/dist/testing/ReducerReadModelProcessor.js +45 -0
  173. package/dist/testing/ReducerReadModelProcessor.js.map +1 -1
  174. package/dist/tsconfig.tsbuildinfo +1 -1
  175. package/observation/batches/BatchContext.ts +75 -0
  176. package/observation/batches/BatchContextObserver.ts +16 -0
  177. package/observation/batches/BatchContextValues.ts +16 -0
  178. package/observation/batches/BatchMethod.ts +13 -0
  179. package/observation/batches/BatchMethodReturnedAValue.ts +16 -0
  180. package/observation/batches/BatchMethods.ts +90 -0
  181. package/observation/batches/BatchOutcome.ts +22 -0
  182. package/observation/batches/BatchOutcomeKind.ts +11 -0
  183. package/observation/batches/DeferralNotSupportedByKernel.ts +17 -0
  184. package/observation/batches/DeliveredPartition.ts +16 -0
  185. package/observation/batches/EventAndContext.ts +37 -0
  186. package/observation/batches/EventBatch.ts +105 -0
  187. package/observation/batches/EventTypeObservedByMoreThanOneMethod.ts +17 -0
  188. package/observation/batches/InvalidBatchMethod.ts +16 -0
  189. package/observation/batches/ObservationOrigin.ts +37 -0
  190. package/observation/batches/ObservedEventType.ts +16 -0
  191. package/observation/batches/PageMessages.ts +37 -0
  192. package/observation/batches/PartitionFailedInBatch.ts +14 -0
  193. package/observation/batches/PartitionNotInBatch.ts +14 -0
  194. package/observation/batches/PartitionOutcome.ts +22 -0
  195. package/observation/batches/PartitionProblem.ts +25 -0
  196. package/observation/batches/PartitionSettlement.ts +70 -0
  197. package/observation/batches/ReducerMixesBatchAndPerEventMethods.ts +16 -0
  198. package/observation/batches/batchSize.ts +79 -0
  199. package/observation/batches/createBatchContext.ts +32 -0
  200. package/observation/batches/for_BatchMethods/when_discovering_batch_methods.spec.ts +155 -0
  201. package/observation/batches/for_EventBatch/when_reporting_outcomes.spec.ts +66 -0
  202. package/observation/batches/for_PartitionSettlement/when_settling_a_partition.spec.ts +31 -0
  203. package/observation/batches/for_batchSize/when_declaring_bounds.spec.ts +23 -0
  204. package/observation/batches/for_observes/when_decorating_methods.legacy.spec.ts +28 -0
  205. package/observation/batches/for_observes/when_decorating_methods.spec.ts +33 -0
  206. package/observation/batches/for_runConcurrently/when_running_items.spec.ts +22 -0
  207. package/observation/batches/observes.ts +68 -0
  208. package/observation/batches/runConcurrently.ts +23 -0
  209. package/observation/createHandlerInstanceValidator.ts +5 -7
  210. package/observation/getInstanceMethods.ts +13 -4
  211. package/observation/index.ts +14 -0
  212. package/package.json +1 -1
  213. package/reactors/ReactorDispatcher.ts +27 -2
  214. package/reactors/ReactorObservationTarget.ts +48 -0
  215. package/reactors/ReactorPageDispatcher.ts +321 -0
  216. package/reactors/Reactors.ts +22 -164
  217. package/reactors/for_Reactors/given/observe_reactor_messages.ts +72 -0
  218. package/reactors/for_Reactors/when_delivering_pages/as_a_legacy_single_partition.spec.ts +51 -0
  219. package/reactors/for_Reactors/when_delivering_pages/returning_a_value_from_a_batch_method.spec.ts +34 -0
  220. package/reactors/for_Reactors/when_delivering_pages/with_batch_and_per_event_methods.spec.ts +81 -0
  221. package/reactors/for_Reactors/when_delivering_pages/with_batch_outcomes.spec.ts +86 -0
  222. package/reactors/for_Reactors/when_delivering_pages/with_per_event_handlers.spec.ts +52 -0
  223. package/reactors/index.ts +2 -0
  224. package/reducers/ReducerEventDispatcher.ts +44 -3
  225. package/reducers/Reducers.ts +109 -25
  226. package/reducers/ReductionOutcome.ts +55 -8
  227. package/reducers/for_Reducers/given/observe_reducer_messages.ts +77 -0
  228. package/reducers/for_Reducers/when_delivering_pages/to_a_batch_reducer.spec.ts +111 -0
  229. package/reducers/for_Reducers/when_delivering_pages/to_a_per_event_reducer.spec.ts +53 -0
  230. package/reducers/index.ts +2 -0
  231. package/testing/ReactorScenario.ts +5 -1
  232. package/testing/ReducerReadModelProcessor.ts +44 -0
  233. package/testing/for_ReactorScenario/when_the_reactor_has_a_batch_method.spec.ts +26 -0
  234. package/testing/for_ReadModelScenario/when_reducing_with_a_batch_method/with_events_for_two_event_sources.spec.ts +43 -0
@@ -24,7 +24,7 @@ import { EventStoreName } from '../EventStoreName.js';
24
24
  import { EventStoreNamespaceName } from '../EventStoreNamespaceName.js';
25
25
  import type { IEventStore } from '../IEventStore.js';
26
26
  import type { IReadModels } from '../readModels/IReadModels.js';
27
- import { getReactorEventTypes, invokeReactorHandler, selectReactorHandler } from '../reactors/ReactorDispatcher.js';
27
+ import { getReactorBatchMethods, getReactorEventTypes, invokeReactorHandler, selectReactorHandler } from '../reactors/ReactorDispatcher.js';
28
28
  import { createHandlerInstanceValidator } from '../observation/createHandlerInstanceValidator.js';
29
29
  import { getReactorMetadata } from '../reactors/reactor.js';
30
30
  import { normalizeReactorSideEffects } from '../reactors/ReactorSideEffects.js';
@@ -89,6 +89,10 @@ export class ReactorScenario {
89
89
  eventTypeMigrations: eventTypeMigrations === undefined ? undefined : [...eventTypeMigrations]
90
90
  } });
91
91
  this._entries = getReactorEventTypes(_reactor, this._eventTypes);
92
+ if (getReactorBatchMethods(_reactor, this._eventTypes).length > 0) {
93
+ throw new UnsupportedReactorOperation('reactor.batchMethod', _reactor.name,
94
+ 'Batch methods (@observes) are not fixture-backed yet.');
95
+ }
92
96
  this._validateInstance = createHandlerInstanceValidator(_reactor,
93
97
  this._entries.flatMap(entry => [entry.methodName, entry.replayMethodName].filter((name): name is string => !!name)));
94
98
  // Every unsupported event-sequence operation is latched at its source, whichever result or stream carries it.
@@ -3,6 +3,11 @@
3
3
 
4
4
  import type { Constructor } from '@cratis/fundamentals';
5
5
  import { ReducerEventDispatcher } from '../reducers/ReducerEventDispatcher.js';
6
+ import { BatchContext } from '../observation/batches/BatchContext.js';
7
+ import { EventAndContext } from '../observation/batches/EventAndContext.js';
8
+ import { EventBatch } from '../observation/batches/EventBatch.js';
9
+ import { ObservationOrigin } from '../observation/batches/ObservationOrigin.js';
10
+ import { PartitionFailedInBatch } from '../observation/batches/PartitionFailedInBatch.js';
6
11
  import { getEventSourceTypeFor } from '../events/eventSourceTypeDecorator.js';
7
12
  import { getEventStreamTypeFor } from '../events/eventStreamTypeDecorator.js';
8
13
  import { getFilterTagsFor } from '../events/filterEventsByTagDecorator.js';
@@ -32,6 +37,10 @@ export class ReducerReadModelProcessor<TReadModel extends object> implements IRe
32
37
  const results = new Map<string, ReadModelState<TReadModel>>();
33
38
  const reducer = new (this._reducerType as new () => Record<string, Function>)();
34
39
  this._dispatcher.validateInstance(reducer);
40
+ if (this._dispatcher.batchMethod) {
41
+ await this.processBatches(reducer, events, results);
42
+ return results;
43
+ }
35
44
  for (const event of events) {
36
45
  const handler = this._dispatcher.handlerFor(event.context.eventType.id.value);
37
46
  if (!handler) continue;
@@ -50,4 +59,39 @@ export class ReducerReadModelProcessor<TReadModel extends object> implements IRe
50
59
  }
51
60
  return results;
52
61
  }
62
+
63
+ private async processBatches(reducer: Record<string, Function>, events: readonly ScenarioEvent[],
64
+ results: Map<string, ReadModelState<TReadModel>>): Promise<void> {
65
+ const method = this._dispatcher.batchMethod!;
66
+ const bySource = new Map<string, EventAndContext[]>();
67
+ for (const event of events) {
68
+ const observed = method.eventTypes.get(event.context.eventType.id.value);
69
+ if (!observed) continue;
70
+ const sourceEvents = bySource.get(event.sourceId) ?? [];
71
+ sourceEvents.push(new EventAndContext(event.content, event.context, observed.type));
72
+ bySource.set(event.sourceId, sourceEvents);
73
+ }
74
+ const signal = new AbortController().signal;
75
+ for (const [sourceId, sourceEvents] of bySource) {
76
+ const batch = new EventBatch(sourceEvents, false);
77
+ const sequenceNumbers = sourceEvents.map(event => event.context.sequenceNumber);
78
+ const context = new BatchContext({
79
+ observerId: this._reducerType.name, eventStore: sourceEvents[0].context.eventStore ?? '', namespace: sourceEvents[0].context.namespace ?? '',
80
+ eventSequenceId: 'event-log', origin: ObservationOrigin.Unknown, partitions: [sourceId], eventCount: sourceEvents.length,
81
+ firstSequenceNumber: sequenceNumbers[0], lastSequenceNumber: sequenceNumbers.at(-1), deadline: undefined, signal, canDefer: false
82
+ });
83
+ let next: unknown;
84
+ try {
85
+ next = await this._dispatcher.invokeBatch(reducer, batch, undefined, context);
86
+ const [reported] = batch.outcomes;
87
+ if (reported) throw reported.error ?? new PartitionFailedInBatch(sourceId);
88
+ } catch (cause) {
89
+ throw new Error(`Reducer '${this._reducerType.name}' for read model '${this._readModelType.name}' failed`, { cause });
90
+ }
91
+ results.set(sourceId, {
92
+ instance: next === undefined ? null : next as TReadModel | null,
93
+ deleted: next === undefined
94
+ });
95
+ }
96
+ }
53
97
  }
@@ -0,0 +1,26 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { field } from '@cratis/fundamentals';
5
+ import { chai, describe, it } from 'vitest';
6
+ import { eventType, observes, reactor } from '../../index.js';
7
+ import { ReactorScenario, UnsupportedReactorOperation } from '../index.js';
8
+
9
+ chai.should();
10
+
11
+ @eventType('scenario-batch-shipment-sent')
12
+ class ShipmentSent {
13
+ @field(String) trackingNumber = '';
14
+ }
15
+
16
+ @reactor('scenario-batch-shipments')
17
+ class Shipments {
18
+ @observes(ShipmentSent)
19
+ track() {}
20
+ }
21
+
22
+ describe('when creating a reactor scenario for a reactor with a batch method', () => {
23
+ it('should refuse it instead of skipping its events', () =>
24
+ (() => new ReactorScenario(Shipments, { artifacts: { eventTypes: [ShipmentSent] }, constraints: 'disabled' }))
25
+ .should.throw(UnsupportedReactorOperation, 'reactor.batchMethod'));
26
+ });
@@ -0,0 +1,43 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { chai, describe, it } from 'vitest';
5
+ import { field } from '@cratis/fundamentals';
6
+ import { eventType } from '../../../events/eventTypeDecorator.js';
7
+ import { EventBatch, observes } from '../../../observation/index.js';
8
+ import { reducer } from '../../../reducers/reducer.js';
9
+ import { ReadModelScenario } from '../../index.js';
10
+
11
+ chai.should();
12
+
13
+ @eventType('scenario-batch-points-earned')
14
+ class PointsEarned {
15
+ constructor(readonly points: number = 0) {}
16
+ }
17
+
18
+ class Score {
19
+ @field(Number) points = 0;
20
+ }
21
+
22
+ let calls = 0;
23
+
24
+ @reducer('scenario-batch-scores', undefined, Score)
25
+ class Scores {
26
+ @observes(PointsEarned)
27
+ reduce(events: EventBatch, current: Score | undefined): Score {
28
+ calls++;
29
+ return { points: events.ofType(PointsEarned).reduce((total, entry) => total + entry.event.points, current?.points ?? 0) };
30
+ }
31
+ }
32
+
33
+ describe('when reducing with a batch method in a read model scenario', () => {
34
+ it('should call the batch method once per event source and keep each state', async () => {
35
+ calls = 0;
36
+ const scenario = new ReadModelScenario(Score, { eventTypes: [PointsEarned], reducers: [Scores] });
37
+ scenario.given.forEventSource('player-1').events(new PointsEarned(3), new PointsEarned(4));
38
+ scenario.given.forEventSource('player-2').events(new PointsEarned(10));
39
+ (await scenario.instanceForEventSourceId('player-1'))!.should.deep.equal({ points: 7 });
40
+ (await scenario.instanceForEventSourceId('player-2'))!.should.deep.equal({ points: 10 });
41
+ calls.should.be.greaterThan(0);
42
+ });
43
+ });