@cratis/chronicle 6.71.0 → 6.71.2

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 (39) hide show
  1. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.d.ts +142 -0
  2. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.d.ts.map +1 -0
  3. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.js +142 -0
  4. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.js.map +1 -0
  5. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.d.ts +24 -0
  6. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.d.ts.map +1 -0
  7. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.js +34 -0
  8. package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.js.map +1 -0
  9. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.d.ts +42 -0
  10. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.d.ts.map +1 -0
  11. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.js +115 -0
  12. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.js.map +1 -0
  13. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.d.ts +20 -0
  14. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.d.ts.map +1 -0
  15. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.js +20 -0
  16. package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.js.map +1 -0
  17. package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.d.ts +17 -0
  18. package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.d.ts.map +1 -0
  19. package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js +74 -0
  20. package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js.map +1 -0
  21. package/dist/testing/projections/ProjectionRootCapabilities.d.ts.map +1 -1
  22. package/dist/testing/projections/ProjectionRootCapabilities.js +8 -3
  23. package/dist/testing/projections/ProjectionRootCapabilities.js.map +1 -1
  24. package/dist/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +2 -2
  26. package/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.ts +71 -0
  27. package/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.ts +38 -0
  28. package/testing/for_ReadModelScenario/when_adding_with_children/with_a_fluent_projection.spec.ts +64 -0
  29. package/testing/for_ReadModelScenario/when_adding_with_children/with_legacy_decorators.legacy.spec.ts +41 -0
  30. package/testing/for_ReadModelScenario/when_adding_with_children/with_standard_decorators.standard.spec.ts +42 -0
  31. package/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.ts +55 -0
  32. package/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.ts +24 -0
  33. package/testing/for_ReadModelScenario/when_counting_with_children/with_a_fluent_projection.spec.ts +94 -0
  34. package/testing/for_ReadModelScenario/when_counting_with_children/with_legacy_decorators.legacy.spec.ts +39 -0
  35. package/testing/for_ReadModelScenario/when_counting_with_children/with_standard_decorators.standard.spec.ts +40 -0
  36. package/testing/projections/ProjectionArithmeticChildrenCapabilities.ts +82 -0
  37. package/testing/projections/ProjectionRootCapabilities.ts +6 -3
  38. package/testing/projections/for_ProjectionCapabilities/when_validating_add_with_children.spec.ts +91 -0
  39. package/testing/projections/for_ProjectionCapabilities/when_validating_count_with_children.spec.ts +97 -0
@@ -0,0 +1,97 @@
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 type { ProjectionDefinition } from '@cratis/chronicle.contracts';
5
+ import { EventType } from '../../../events/EventType.js';
6
+ import { beforeEach, chai, describe, it } from 'vitest';
7
+ import type { JsonSchema } from '../../../schemas/JsonSchema.js';
8
+ import { compile, Totals } from '../../for_ReadModelScenario/when_counting_with_children/given/compiled_projection.js';
9
+ import type { ScenarioEvent } from '../../ScenarioEvent.js';
10
+ import { ProjectionReadModelProcessor } from '../ProjectionReadModelProcessor.js';
11
+ import { UnsupportedProjectionOperation } from '../UnsupportedProjectionOperation.js';
12
+
13
+ chai.should();
14
+
15
+ type Candidate = { compiled: ReturnType<typeof compile>; definition: ProjectionDefinition; schema: JsonSchema };
16
+ type Mutation = (candidate: Candidate) => void;
17
+ const boundaries: Array<[string, Mutation]> = [
18
+ ...['$increment', '$decrement', '$subtract(total)'].map(expression => [expression,
19
+ ({ definition }: Candidate) => { definition.From[0].Value.Properties.total = expression; }] as [string, Mutation]),
20
+ ['int32 count', ({ schema }) => { schema.properties!.total = { type: 'integer', format: 'int32' }; }],
21
+ ['float count', ({ schema }) => { schema.properties!.total = { type: 'number', format: 'float' }; }],
22
+ ['GUID root identifier', ({ schema }) => { schema.properties!.id.format = 'uuid'; }],
23
+ ['numeric root identifier', ({ schema }) => { schema.properties!.id = { type: 'number' }; }],
24
+ ['extra root property', ({ schema }) => { schema.properties!.name = { type: 'string' }; }],
25
+ ['extra root mapping', ({ definition }) => { definition.From[0].Value.Properties.id = '$eventSourceId'; }],
26
+ ['multiple root subscriptions', ({ definition }) => { definition.From.push(structuredClone(definition.From[0])); }],
27
+ ['initial values', ({ definition }) => { definition.InitialModelState = '{"total":0}'; }],
28
+ ['root join', ({ definition }) => { definition.Join.push({ Key: definition.From[0].Key, Value: { On: 'id', Properties: {}, Key: '$eventSourceId' } }); }],
29
+ ['multiple child collections', ({ definition }) => { definition.Children.other = structuredClone(definition.Children.items); }],
30
+ ['untyped children', ({ schema }) => { delete schema.properties!.items.items!.properties; }],
31
+ ['numeric child property', ({ schema }) => { schema.properties!.items.items!.properties!.name = { type: 'number' }; }],
32
+ ['formatted child property', ({ schema }) => { schema.properties!.items.items!.properties!.name.format = 'uuid'; }],
33
+ ['numeric child identifier', ({ schema }) => { schema.properties!.items.items!.properties!.itemId = { type: 'number' }; }],
34
+ ['missing child identifier', ({ schema }) => { delete schema.properties!.items.items!.properties!.itemId; }],
35
+ ['child without From', ({ definition }) => { definition.Children.items.From = []; }],
36
+ ['child From without event Key', ({ definition }) => { delete definition.Children.items.From[0].Key; }],
37
+ ['child removal without event Key', ({ definition }) => { delete definition.Children.items.RemovedWith[0].Key; }],
38
+ ['shared root and child From', ({ definition }) => { definition.Children.items.From[0].Key = definition.From[0].Key; }],
39
+ ['shared root From and child removal', ({ definition }) => { definition.Children.items.RemovedWith[0].Key = definition.From[0].Key; }],
40
+ ['shared root removal and child From', ({ definition }) => { definition.Children.items.From[0].Key = definition.RemovedWith[0].Key; }],
41
+ ['shared root and child removal', ({ definition }) => { definition.Children.items.RemovedWith[0].Key = definition.RemovedWith[0].Key; }],
42
+ ['custom root key', ({ definition }) => { definition.From[0].Value.Key = 'total'; }],
43
+ ['custom child parent key', ({ definition }) => { definition.Children.items.From[0].Value.ParentKey = 'itemId'; }]
44
+ ];
45
+
46
+ function candidate(): Candidate {
47
+ const compiled = compile();
48
+ return { compiled, definition: compiled.definitions[0], schema: JSON.parse(compiled.readModels[0].Schema) };
49
+ }
50
+
51
+ // A constructor failure must precede processing even when events are supplied.
52
+ const seeded: ScenarioEvent = {
53
+ sourceId: 'A', content: { total: 9000 },
54
+ context: {
55
+ eventSourceId: 'A', eventType: EventType.parse('Counted+1'), sequenceNumber: 0n,
56
+ occurred: new Date('2025-01-01T00:00:00Z'), correlationId: '00000000-0000-0000-0000-000000000000',
57
+ causation: [], tags: []
58
+ }
59
+ };
60
+ const histories: Array<[string, ScenarioEvent[]]> = [['empty', []], ['seeded', [seeded]]];
61
+ for (const [name, mutate] of boundaries) {
62
+ for (const [historyName, history] of histories) {
63
+ describe(`when rejecting count with children: ${name}, ${historyName} history`, () => {
64
+ let act: () => Promise<unknown>;
65
+ beforeEach(() => {
66
+ const value = candidate();
67
+ mutate(value);
68
+ value.compiled.readModels[0].Schema = JSON.stringify(value.schema);
69
+ act = () => new ProjectionReadModelProcessor(Totals, value.compiled, value.definition).process(history);
70
+ });
71
+ it('should reject before replay', () => act.should.throw(UnsupportedProjectionOperation));
72
+ });
73
+ }
74
+ }
75
+
76
+ describe('when adding the unformatted same-name operand in the previously rejected count boundary', () => {
77
+ it('should admit the fixture-backed addition rather than count or assign', async () => {
78
+ const value = candidate();
79
+ value.definition.From[0].Value.Properties.total = '$add(total)';
80
+ const processor = new ProjectionReadModelProcessor(Totals, value.compiled, value.definition);
81
+ await processor.process([seeded, { ...seeded, context: { ...seeded.context, sequenceNumber: 1n } }]);
82
+ processor.publicRead.A.total.should.equal(18000);
83
+ });
84
+ });
85
+
86
+ for (const format of [undefined, 'double']) {
87
+ describe(`when admitting count with ${format ?? 'unformatted number'} and typed children`, () => {
88
+ let act: () => ProjectionReadModelProcessor<Totals>;
89
+ beforeEach(() => {
90
+ const value = candidate();
91
+ value.schema.properties!.total.format = format;
92
+ value.compiled.readModels[0].Schema = JSON.stringify(value.schema);
93
+ act = () => new ProjectionReadModelProcessor(Totals, value.compiled, value.definition);
94
+ });
95
+ it('should admit the empty-history constructor', () => act.should.not.throw());
96
+ });
97
+ }