@cratis/chronicle 6.72.0 → 6.72.1
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/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.d.ts +145 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.js +142 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.d.ts +24 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.js +34 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.d.ts +7 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.js +33 -0
- package/dist/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.js.map +1 -0
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js +1 -1
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/testing/for_ReadModelScenario/when_subtracting_with_children/given/compiled_projection.ts +71 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/given/lifecycle.ts +38 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/given/oracle_input.ts +36 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/with_a_fluent_projection.spec.ts +40 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/with_legacy_decorators.legacy.spec.ts +50 -0
- package/testing/for_ReadModelScenario/when_subtracting_with_children/with_standard_decorators.standard.spec.ts +51 -0
- package/testing/projections/ProjectionArithmeticChildrenCapabilities.ts +1 -1
- package/testing/projections/for_ProjectionCapabilities/when_validating_add_with_children.spec.ts +1 -1
- package/testing/projections/for_ProjectionCapabilities/when_validating_count_with_children.spec.ts +11 -9
- package/testing/projections/for_ProjectionCapabilities/when_validating_subtract_with_children.spec.ts +105 -0
|
@@ -0,0 +1,51 @@
|
|
|
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, type Assertion } from 'vitest';
|
|
6
|
+
import { ProjectionDefinitionCompiler } from '../../../projections/ProjectionDefinitionCompiler.js';
|
|
7
|
+
import { subtractFrom } from '../../../projections/modelBound/subtractFrom.js';
|
|
8
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
9
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
10
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
11
|
+
import { Subtracted, artifacts, ItemChanged, ItemRemoved, Removed } from './given/compiled_projection.js';
|
|
12
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
13
|
+
import { assertOracleInput } from './given/oracle_input.js';
|
|
14
|
+
|
|
15
|
+
chai.should();
|
|
16
|
+
function should(value: unknown): Assertion { return (value as { should: Assertion }).should; }
|
|
17
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
18
|
+
class Item {
|
|
19
|
+
@field(String) itemId!: string;
|
|
20
|
+
@field(String) name!: string;
|
|
21
|
+
}
|
|
22
|
+
@removedWith(Removed)
|
|
23
|
+
class Totals {
|
|
24
|
+
@field(String) id!: string;
|
|
25
|
+
@field(Number) @subtractFrom(Subtracted, 'amount') total!: number;
|
|
26
|
+
@field(Array, { genericArguments: [Item] })
|
|
27
|
+
@childrenFrom(ItemChanged, { key: 'itemId', identifiedBy: 'itemId' }) items!: Item[];
|
|
28
|
+
}
|
|
29
|
+
const modelArtifacts = { ...artifacts, projections: [], readModels: [Totals] };
|
|
30
|
+
export const candidate = {
|
|
31
|
+
name: 'subtract-children-standard', event: Subtracted, model: Totals, artifacts: modelArtifacts,
|
|
32
|
+
compile: () => new ProjectionDefinitionCompiler(modelArtifacts, 'test').compile([], [Totals])
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe('when subtracting with independent children through standard decorators', () => {
|
|
36
|
+
it('should compile exactly the oracle input', () => assertOracleInput(candidate));
|
|
37
|
+
|
|
38
|
+
it('should preserve subtraction through child changes, removal, independent roots and parent-first recreation', async () => {
|
|
39
|
+
const result = await lifecycle(new ReadModelScenario(Totals, modelArtifacts));
|
|
40
|
+
should(result.first.total).equal(-1.5);
|
|
41
|
+
should(result.first.items).deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
42
|
+
should(result.remaining.total).equal(-1.5);
|
|
43
|
+
should(result.remaining.items).deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
44
|
+
should(result.removed === null).be.true;
|
|
45
|
+
should(result.surviving).deep.equal(result.other);
|
|
46
|
+
should(result.other.total).equal(-8);
|
|
47
|
+
should(result.recreated.total).equal(-4.5);
|
|
48
|
+
should(result.recreated.items).deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
49
|
+
should(result.repeated).deep.equal(result.recreated);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -25,7 +25,7 @@ export class ProjectionArithmeticChildrenCapabilities {
|
|
|
25
25
|
|
|
26
26
|
private static accumulator(target: JsonSchema | undefined, expression: string, from: FromRecord, require: ProjectionEventSchemaResolver['require']): boolean {
|
|
27
27
|
if (expression === '$count') return this.countTarget(target);
|
|
28
|
-
const operand = /^\$add\(([A-Za-z_][A-Za-z0-9]*)\)$/.exec(expression)?.[1];
|
|
28
|
+
const operand = /^\$(?:add|subtract)\(([A-Za-z_][A-Za-z0-9]*)\)$/.exec(expression)?.[1];
|
|
29
29
|
if (!operand || !this.unformattedNumber(target)) return false;
|
|
30
30
|
const event = require(from.Key, eventContractPath('From', from.Key));
|
|
31
31
|
return this.unformattedNumber(event.properties?.[operand]);
|
package/testing/projections/for_ProjectionCapabilities/when_validating_add_with_children.spec.ts
CHANGED
|
@@ -14,7 +14,7 @@ chai.should();
|
|
|
14
14
|
type Candidate = { compiled: ReturnType<typeof compile>; definition: ProjectionDefinition; schema: JsonSchema; operand: JsonSchema };
|
|
15
15
|
type Mutation = (candidate: Candidate) => void;
|
|
16
16
|
const boundaries: Array<[string, Mutation]> = [
|
|
17
|
-
...['$increment', '$decrement', '$
|
|
17
|
+
...['$increment', '$decrement', '$add(amount + 1)', '$add(nested.amount)', '$add($eventSourceId)',
|
|
18
18
|
'$add(1)', '$add(true)', '$add(snake_case)', '$add(missing)', '$add(amount', '$add(_snake_case)'].map(expression =>
|
|
19
19
|
[expression, ({ definition }: Candidate) => { definition.From[0].Value.Properties.total = expression; }] as [string, Mutation]),
|
|
20
20
|
...['int32', 'uint32', 'float', 'double', ''].flatMap(format => [
|
package/testing/projections/for_ProjectionCapabilities/when_validating_count_with_children.spec.ts
CHANGED
|
@@ -15,7 +15,7 @@ chai.should();
|
|
|
15
15
|
type Candidate = { compiled: ReturnType<typeof compile>; definition: ProjectionDefinition; schema: JsonSchema };
|
|
16
16
|
type Mutation = (candidate: Candidate) => void;
|
|
17
17
|
const boundaries: Array<[string, Mutation]> = [
|
|
18
|
-
...['$increment', '$decrement'
|
|
18
|
+
...['$increment', '$decrement'].map(expression => [expression,
|
|
19
19
|
({ definition }: Candidate) => { definition.From[0].Value.Properties.total = expression; }] as [string, Mutation]),
|
|
20
20
|
['int32 count', ({ schema }) => { schema.properties!.total = { type: 'integer', format: 'int32' }; }],
|
|
21
21
|
['float count', ({ schema }) => { schema.properties!.total = { type: 'number', format: 'float' }; }],
|
|
@@ -73,15 +73,17 @@ for (const [name, mutate] of boundaries) {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
for (const [operation, expected] of [['add', 18000], ['subtract', -18000]] as const) {
|
|
77
|
+
describe(`when applying ${operation} to the unformatted same-name operand in the count boundary`, () => {
|
|
78
|
+
it('should admit the fixture-backed arithmetic rather than count or assign', async () => {
|
|
79
|
+
const value = candidate();
|
|
80
|
+
value.definition.From[0].Value.Properties.total = `$${operation}(total)`;
|
|
81
|
+
const processor = new ProjectionReadModelProcessor(Totals, value.compiled, value.definition);
|
|
82
|
+
await processor.process([seeded, { ...seeded, context: { ...seeded.context, sequenceNumber: 1n } }]);
|
|
83
|
+
processor.publicRead.A.total.should.equal(expected);
|
|
84
|
+
});
|
|
83
85
|
});
|
|
84
|
-
}
|
|
86
|
+
}
|
|
85
87
|
|
|
86
88
|
for (const format of [undefined, 'double']) {
|
|
87
89
|
describe(`when admitting count with ${format ?? 'unformatted number'} and typed children`, () => {
|
|
@@ -0,0 +1,105 @@
|
|
|
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 { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import type { ProjectionDefinition } from '@cratis/chronicle.contracts';
|
|
6
|
+
import { EventType } from '../../../events/EventType.js';
|
|
7
|
+
import type { JsonSchema } from '../../../schemas/JsonSchema.js';
|
|
8
|
+
import { compile, Totals } from '../../for_ReadModelScenario/when_subtracting_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
|
+
type Candidate = { compiled: ReturnType<typeof compile>; definition: ProjectionDefinition; schema: JsonSchema; operand: JsonSchema };
|
|
15
|
+
type Mutation = (candidate: Candidate) => void;
|
|
16
|
+
const boundaries: Array<[string, Mutation]> = [
|
|
17
|
+
...['$increment', '$decrement', '$subtract()', '$subtract(amount + 1)', '$subtract(nested.amount)', '$subtract($eventSourceId)',
|
|
18
|
+
'$subtract(1)', '$subtract(true)', '$subtract(snake_case)', '$subtract(missing)', '$subtract(amount', '$subtract(_snake_case)'].map(expression =>
|
|
19
|
+
[expression, ({ definition }: Candidate) => { definition.From[0].Value.Properties.total = expression; }] as [string, Mutation]),
|
|
20
|
+
...['int32', 'uint32', 'float', 'double', ''].flatMap(format => [
|
|
21
|
+
[`${format || 'empty'} operand format`, ({ operand }: Candidate) => { operand.format = format; }],
|
|
22
|
+
[`${format || 'empty'} target format`, ({ schema }: Candidate) => { schema.properties!.total.format = format; }]
|
|
23
|
+
] as Array<[string, Mutation]>),
|
|
24
|
+
['integer operand', ({ operand }) => { operand.type = 'integer'; operand.format = 'int32'; }],
|
|
25
|
+
['integer target', ({ schema }) => { schema.properties!.total = { type: 'integer', format: 'int32' }; }],
|
|
26
|
+
['non-numeric operand', ({ operand }) => { operand.type = 'string'; }],
|
|
27
|
+
['non-numeric target', ({ schema }) => { schema.properties!.total = { type: 'string' }; }],
|
|
28
|
+
['nullable operand', ({ operand }) => { Object.assign(operand, { type: ['number', 'null'] }); }],
|
|
29
|
+
['nullable target', ({ schema }) => { Object.assign(schema.properties!.total, { type: ['number', 'null'] }); }],
|
|
30
|
+
['protected operand', ({ operand }) => { operand.security = [{ metadataType: 'EncryptedSubject', details: '' }]; }],
|
|
31
|
+
['compliance operand', ({ operand }) => { operand.compliance = [{ metadataType: 'PII', details: '' }]; }],
|
|
32
|
+
['protected target', ({ schema }) => { schema.properties!.total.security = [{ metadataType: 'EncryptedSubject', details: '' }]; }],
|
|
33
|
+
['compliance target', ({ schema }) => { schema.properties!.total.compliance = [{ metadataType: 'PII', details: '' }]; }],
|
|
34
|
+
['GUID root identifier', ({ schema }) => { schema.properties!.id.format = 'uuid'; }],
|
|
35
|
+
['extra root property', ({ schema }) => { schema.properties!.name = { type: 'string' }; }],
|
|
36
|
+
['extra root mapping', ({ definition }) => { definition.From[0].Value.Properties.id = '$eventSourceId'; }],
|
|
37
|
+
['multiple root subscriptions', ({ definition }) => { definition.From.push(structuredClone(definition.From[0])); }],
|
|
38
|
+
...['add-first', 'subtract-first'].flatMap(order => [
|
|
39
|
+
[`mixed add/subtract subscriptions, ${order}`, ({ definition }: Candidate) => {
|
|
40
|
+
const added = structuredClone(definition.From[0]);
|
|
41
|
+
added.Value.Properties.total = '$add(amount)';
|
|
42
|
+
if (order === 'add-first') definition.From.unshift(added);
|
|
43
|
+
else definition.From.push(added);
|
|
44
|
+
}],
|
|
45
|
+
[`mixed add/subtract targets, ${order}`, ({ definition, schema }: Candidate) => {
|
|
46
|
+
schema.properties!.other = { type: 'number' };
|
|
47
|
+
definition.From[0].Value.Properties = order === 'add-first'
|
|
48
|
+
? { other: '$add(amount)', total: '$subtract(amount)' }
|
|
49
|
+
: { total: '$subtract(amount)', other: '$add(amount)' };
|
|
50
|
+
}]
|
|
51
|
+
] as Array<[string, Mutation]>),
|
|
52
|
+
['initial values', ({ definition }) => { definition.InitialModelState = '{"total":0}'; }],
|
|
53
|
+
['root join', ({ definition }) => { definition.Join.push({ Key: definition.From[0].Key, Value: { On: 'id', Properties: {}, Key: '$eventSourceId' } }); }],
|
|
54
|
+
['multiple child collections', ({ definition }) => { definition.Children.other = structuredClone(definition.Children.items); }],
|
|
55
|
+
['untyped children', ({ schema }) => { delete schema.properties!.items.items!.properties; }],
|
|
56
|
+
['numeric child property', ({ schema }) => { schema.properties!.items.items!.properties!.name = { type: 'number' }; }],
|
|
57
|
+
['missing child identifier', ({ schema }) => { delete schema.properties!.items.items!.properties!.itemId; }],
|
|
58
|
+
['child From without event Key', ({ definition }) => { delete definition.Children.items.From[0].Key; }],
|
|
59
|
+
['child removal without event Key', ({ definition }) => { delete definition.Children.items.RemovedWith[0].Key; }],
|
|
60
|
+
['shared root and child From', ({ definition }) => { definition.Children.items.From[0].Key = definition.From[0].Key; }],
|
|
61
|
+
['shared root From and child removal', ({ definition }) => { definition.Children.items.RemovedWith[0].Key = definition.From[0].Key; }],
|
|
62
|
+
['shared root removal and child From', ({ definition }) => { definition.Children.items.From[0].Key = definition.RemovedWith[0].Key; }],
|
|
63
|
+
['shared root and child removal', ({ definition }) => { definition.Children.items.RemovedWith[0].Key = definition.RemovedWith[0].Key; }],
|
|
64
|
+
['shared root From and removal', ({ definition }) => { definition.RemovedWith[0].Key = definition.From[0].Key; }],
|
|
65
|
+
['custom root key', ({ definition }) => { definition.From[0].Value.Key = 'amount'; }],
|
|
66
|
+
['custom root parent key', ({ definition }) => { definition.From[0].Value.ParentKey = 'amount'; }],
|
|
67
|
+
['custom child parent key', ({ definition }) => { definition.Children.items.From[0].Value.ParentKey = 'itemId'; }],
|
|
68
|
+
['child join', ({ definition }) => { definition.Children.items.Join.push({}); }],
|
|
69
|
+
['nested children', ({ definition }) => { definition.Children.items.Children.nested = structuredClone(definition.Children.items); }],
|
|
70
|
+
['value children', ({ definition }) => { definition.Children.items.FromEventProperty = 'items'; }],
|
|
71
|
+
['unknown child expression', ({ definition }) => { definition.Children.items.From[0].Value.Properties.name = '$unknown(name)'; }],
|
|
72
|
+
['unknown root expression', ({ definition }) => { definition.From[0].Value.Properties.total = '$unknown(amount)'; }]
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
function candidate(): Candidate {
|
|
76
|
+
const compiled = compile();
|
|
77
|
+
const definition = compiled.definitions[0];
|
|
78
|
+
const operand = [...compiled.eventSchemas.get(definition)!.values()].find(entry => entry.eventType.Id === 'Subtracted')!.schema.properties!.amount;
|
|
79
|
+
return { compiled, definition, schema: JSON.parse(compiled.readModels[0].Schema), operand };
|
|
80
|
+
}
|
|
81
|
+
const seeded: ScenarioEvent = {
|
|
82
|
+
sourceId: 'A', content: { amount: 2.5 },
|
|
83
|
+
context: {
|
|
84
|
+
eventSourceId: 'A', eventType: EventType.parse('Subtracted+1'), sequenceNumber: 0n,
|
|
85
|
+
occurred: new Date('2025-01-01T00:00:00Z'), correlationId: '00000000-0000-0000-0000-000000000000',
|
|
86
|
+
causation: [], tags: []
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
for (const [name, mutate] of boundaries) {
|
|
90
|
+
for (const [historyName, history] of [['empty', []], ['seeded', [seeded]]] as const) {
|
|
91
|
+
describe(`when rejecting subtraction with children: ${name}, ${historyName} history`, () => {
|
|
92
|
+
let act: () => Promise<unknown>;
|
|
93
|
+
beforeEach(() => {
|
|
94
|
+
const value = candidate();
|
|
95
|
+
mutate(value);
|
|
96
|
+
value.compiled.readModels[0].Schema = JSON.stringify(value.schema);
|
|
97
|
+
act = () => new ProjectionReadModelProcessor(Totals, value.compiled, value.definition).process(history);
|
|
98
|
+
});
|
|
99
|
+
it('should reject before replay with the model and contract path', () => {
|
|
100
|
+
act.should.throw(UnsupportedProjectionOperation).and.satisfy((error: Error) =>
|
|
101
|
+
error.message.includes('Totals') && /Children|From|Join|Schema|Definition/.test(error.message));
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|