@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.
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.d.ts +142 -0
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.js +142 -0
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.d.ts +24 -0
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.js +34 -0
- package/dist/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.d.ts +42 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.js +115 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.js.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.d.ts +20 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.d.ts.map +1 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.js +20 -0
- package/dist/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.js.map +1 -0
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.d.ts +17 -0
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.d.ts.map +1 -0
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js +74 -0
- package/dist/testing/projections/ProjectionArithmeticChildrenCapabilities.js.map +1 -0
- package/dist/testing/projections/ProjectionRootCapabilities.d.ts.map +1 -1
- package/dist/testing/projections/ProjectionRootCapabilities.js +8 -3
- package/dist/testing/projections/ProjectionRootCapabilities.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/testing/for_ReadModelScenario/when_adding_with_children/given/compiled_projection.ts +71 -0
- package/testing/for_ReadModelScenario/when_adding_with_children/given/lifecycle.ts +38 -0
- package/testing/for_ReadModelScenario/when_adding_with_children/with_a_fluent_projection.spec.ts +64 -0
- package/testing/for_ReadModelScenario/when_adding_with_children/with_legacy_decorators.legacy.spec.ts +41 -0
- package/testing/for_ReadModelScenario/when_adding_with_children/with_standard_decorators.standard.spec.ts +42 -0
- package/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.ts +55 -0
- package/testing/for_ReadModelScenario/when_counting_with_children/given/lifecycle.ts +24 -0
- package/testing/for_ReadModelScenario/when_counting_with_children/with_a_fluent_projection.spec.ts +94 -0
- package/testing/for_ReadModelScenario/when_counting_with_children/with_legacy_decorators.legacy.spec.ts +39 -0
- package/testing/for_ReadModelScenario/when_counting_with_children/with_standard_decorators.standard.spec.ts +40 -0
- package/testing/projections/ProjectionArithmeticChildrenCapabilities.ts +82 -0
- package/testing/projections/ProjectionRootCapabilities.ts +6 -3
- package/testing/projections/for_ProjectionCapabilities/when_validating_add_with_children.spec.ts +91 -0
- package/testing/projections/for_ProjectionCapabilities/when_validating_count_with_children.spec.ts +97 -0
package/testing/for_ReadModelScenario/when_adding_with_children/with_a_fluent_projection.spec.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
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 { readFileSync } from 'node:fs';
|
|
5
|
+
import { JsonSerializer } from '@cratis/fundamentals';
|
|
6
|
+
import { chai, describe, it } from 'vitest';
|
|
7
|
+
import { getEventTypeMetadata } from '../../../events/eventTypeDecorator.js';
|
|
8
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
9
|
+
import { artifacts, Totals, variants } from './given/compiled_projection.js';
|
|
10
|
+
import { history, lifecycle } from './given/lifecycle.js';
|
|
11
|
+
|
|
12
|
+
chai.should();
|
|
13
|
+
describe('when adding with independent children through a fluent projection', () => {
|
|
14
|
+
for (const variant of variants) {
|
|
15
|
+
it(`should compile exactly the oracle input for ${variant.name}`, () => {
|
|
16
|
+
const fixture = JSON.parse(readFileSync(new URL(`../../projections/fixtures/${variant.name}.json`, import.meta.url), 'utf8'));
|
|
17
|
+
const compiled = variant.compile();
|
|
18
|
+
const { LastUpdated, ...wire } = compiled.definitions[0];
|
|
19
|
+
LastUpdated!.should.deep.equal(fixture.compilerLastUpdated);
|
|
20
|
+
JSON.parse(JSON.stringify({ ...wire, NoAutoMapProperties: [] })).should.deep.equal(fixture.wireDefinition);
|
|
21
|
+
JSON.parse(compiled.readModels[0].Schema).should.deep.equal(fixture.readModel.schema);
|
|
22
|
+
const schemas = [...compiled.eventSchemas.get(compiled.definitions[0])!.values()].map(entry => {
|
|
23
|
+
entry.eventType.Tombstone.should.be.false;
|
|
24
|
+
const { Tombstone: _falseFlag, ...eventType } = entry.eventType;
|
|
25
|
+
return { ...entry, eventType };
|
|
26
|
+
});
|
|
27
|
+
schemas.should.deep.equal(fixture.eventSchemas);
|
|
28
|
+
const events = history(amount => new variant.event(amount));
|
|
29
|
+
events.length.should.equal(fixture.events.length);
|
|
30
|
+
events.forEach(({ source, event }, index) => {
|
|
31
|
+
const captured = fixture.events[index];
|
|
32
|
+
const type = getEventTypeMetadata(event.constructor)!.eventType;
|
|
33
|
+
captured.context.eventType.should.deep.equal({ Id: type.id.value, Generation: type.generation.value });
|
|
34
|
+
captured.context.eventSourceId.should.equal(source);
|
|
35
|
+
captured.content.should.deep.equal(JSON.parse(JsonSerializer.serialize(event)));
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
it('should accumulate fractions, negative and zero values independently of child updates and removals', async () => {
|
|
41
|
+
const result = await lifecycle(new ReadModelScenario(Totals, artifacts));
|
|
42
|
+
result.first.total.should.equal(1.5);
|
|
43
|
+
result.first.items.should.deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
44
|
+
result.remaining.total.should.equal(1.5);
|
|
45
|
+
result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
46
|
+
(result.removed === null).should.be.true;
|
|
47
|
+
result.surviving.should.deep.equal(result.other);
|
|
48
|
+
result.other.total.should.equal(8);
|
|
49
|
+
result.recreated.total.should.equal(4.5);
|
|
50
|
+
result.recreated.items.should.deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
51
|
+
result.repeated.should.deep.equal(result.recreated);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
for (const variant of variants.slice(1)) {
|
|
55
|
+
it(`should add rather than assign for ${variant.name}`, async () => {
|
|
56
|
+
const scenario = new ReadModelScenario(Totals, variant.artifacts);
|
|
57
|
+
for (const { source, event } of history(amount => new variant.event(amount)).slice(0, 8)) {
|
|
58
|
+
scenario.given.forEventSource(source).events(event);
|
|
59
|
+
}
|
|
60
|
+
(await scenario.instanceForEventSourceId('A'))!.total.should.equal(1.5);
|
|
61
|
+
(await scenario.instanceForEventSourceId('B'))!.total.should.equal(8);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
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 { addFrom } from '../../../projections/modelBound/addFrom.js';
|
|
7
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
8
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
9
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
10
|
+
import { Added, artifacts, ItemChanged, ItemRemoved, Removed } from './given/compiled_projection.js';
|
|
11
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
12
|
+
|
|
13
|
+
chai.should();
|
|
14
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
15
|
+
class Item {
|
|
16
|
+
@field(String) itemId!: string;
|
|
17
|
+
@field(String) name!: string;
|
|
18
|
+
}
|
|
19
|
+
@removedWith(Removed)
|
|
20
|
+
class Totals {
|
|
21
|
+
@field(String) id!: string;
|
|
22
|
+
@field(Number) @addFrom(Added, 'amount') total!: number;
|
|
23
|
+
@field(Array) @childrenFrom(ItemChanged, { childType: Item, key: 'itemId', identifiedBy: 'itemId' })
|
|
24
|
+
items!: Item[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('when adding with independent children through legacy decorators', () => {
|
|
28
|
+
it('should preserve sums through child changes, removal, independent roots and parent-first recreation', async () => {
|
|
29
|
+
const result = await lifecycle(new ReadModelScenario(Totals, { ...artifacts, projections: [], readModels: [Totals] }));
|
|
30
|
+
result.first.total.should.equal(1.5);
|
|
31
|
+
result.first.items.should.deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
32
|
+
result.remaining.total.should.equal(1.5);
|
|
33
|
+
result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
34
|
+
(result.removed === null).should.be.true;
|
|
35
|
+
result.surviving.should.deep.equal(result.other);
|
|
36
|
+
result.other.total.should.equal(8);
|
|
37
|
+
result.recreated.total.should.equal(4.5);
|
|
38
|
+
result.recreated.items.should.deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
39
|
+
result.repeated.should.deep.equal(result.recreated);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { addFrom } from '../../../projections/modelBound/addFrom.js';
|
|
7
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
8
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
9
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
10
|
+
import { Added, artifacts, ItemChanged, ItemRemoved, Removed } from './given/compiled_projection.js';
|
|
11
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
12
|
+
|
|
13
|
+
chai.should();
|
|
14
|
+
function should(value: unknown): Assertion { return (value as { should: Assertion }).should; }
|
|
15
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
16
|
+
class Item {
|
|
17
|
+
@field(String) itemId!: string;
|
|
18
|
+
@field(String) name!: string;
|
|
19
|
+
}
|
|
20
|
+
@removedWith(Removed)
|
|
21
|
+
class Totals {
|
|
22
|
+
@field(String) id!: string;
|
|
23
|
+
@field(Number) @addFrom(Added, 'amount') total!: number;
|
|
24
|
+
@field(Array, { genericArguments: [Item] })
|
|
25
|
+
@childrenFrom(ItemChanged, { key: 'itemId', identifiedBy: 'itemId' }) items!: Item[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('when adding with independent children through standard decorators', () => {
|
|
29
|
+
it('should preserve sums through child changes, removal, independent roots and parent-first recreation', async () => {
|
|
30
|
+
const result = await lifecycle(new ReadModelScenario(Totals, { ...artifacts, projections: [], readModels: [Totals] }));
|
|
31
|
+
should(result.first.total).equal(1.5);
|
|
32
|
+
should(result.first.items).deep.equal([{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }]);
|
|
33
|
+
should(result.remaining.total).equal(1.5);
|
|
34
|
+
should(result.remaining.items).deep.equal([{ itemId: 'two', name: 'second' }]);
|
|
35
|
+
should(result.removed === null).be.true;
|
|
36
|
+
should(result.surviving).deep.equal(result.other);
|
|
37
|
+
should(result.other.total).equal(8);
|
|
38
|
+
should(result.recreated.total).equal(4.5);
|
|
39
|
+
should(result.recreated.items).deep.equal([{ itemId: 'new', name: 'recreated' }]);
|
|
40
|
+
should(result.repeated).deep.equal(result.recreated);
|
|
41
|
+
});
|
|
42
|
+
});
|
package/testing/for_ReadModelScenario/when_counting_with_children/given/compiled_projection.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
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 type { IClientArtifactsProvider } from '../../../../artifacts/IClientArtifactsProvider.js';
|
|
6
|
+
import { eventType } from '../../../../events/eventTypeDecorator.js';
|
|
7
|
+
import type { IProjectionBuilderFor } from '../../../../projections/declarative/IProjectionBuilderFor.js';
|
|
8
|
+
import { projection } from '../../../../projections/declarative/projection.js';
|
|
9
|
+
import { ProjectionDefinitionCompiler } from '../../../../projections/ProjectionDefinitionCompiler.js';
|
|
10
|
+
|
|
11
|
+
@eventType('Counted')
|
|
12
|
+
export class Counted {
|
|
13
|
+
@field(Number) readonly total: number;
|
|
14
|
+
constructor(total: number) { this.total = total; }
|
|
15
|
+
}
|
|
16
|
+
@eventType('ItemChanged')
|
|
17
|
+
export class ItemChanged {
|
|
18
|
+
@field(String) readonly itemId: string;
|
|
19
|
+
@field(String) readonly name: string;
|
|
20
|
+
constructor(itemId: string, name: string) { this.itemId = itemId; this.name = name; }
|
|
21
|
+
}
|
|
22
|
+
@eventType('ItemRemoved')
|
|
23
|
+
export class ItemRemoved {
|
|
24
|
+
@field(String) readonly itemId: string;
|
|
25
|
+
constructor(itemId: string) { this.itemId = itemId; }
|
|
26
|
+
}
|
|
27
|
+
@eventType('Removed')
|
|
28
|
+
export class Removed {}
|
|
29
|
+
export class Item {
|
|
30
|
+
@field(String) itemId!: string;
|
|
31
|
+
@field(String) name!: string;
|
|
32
|
+
}
|
|
33
|
+
export class Totals {
|
|
34
|
+
@field(String) id!: string;
|
|
35
|
+
@field(Number) total!: number;
|
|
36
|
+
@field(Array, { genericArguments: [Item] }) items!: Item[];
|
|
37
|
+
}
|
|
38
|
+
@projection('count-children', Totals)
|
|
39
|
+
export class TotalsProjection {
|
|
40
|
+
define(builder: IProjectionBuilderFor<Totals>): void {
|
|
41
|
+
builder.from(Counted, from => from.count(model => model.total))
|
|
42
|
+
.children<Item>(model => model.items, child => child.identifiedBy(item => item.itemId)
|
|
43
|
+
.from(ItemChanged, from => from.usingKey(event => event.itemId))
|
|
44
|
+
.removedWith(ItemRemoved, removal => removal.usingKey(event => event.itemId)))
|
|
45
|
+
.removedWith(Removed);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export const artifacts = {
|
|
49
|
+
readModels: [Totals], projections: [TotalsProjection], eventTypes: [Counted, ItemChanged, ItemRemoved, Removed],
|
|
50
|
+
globalForHandlers: [], reducers: [], reactors: [], seeders: [], constraints: [], webhooks: [], eventTypeMigrations: []
|
|
51
|
+
} satisfies IClientArtifactsProvider;
|
|
52
|
+
|
|
53
|
+
export function compile() {
|
|
54
|
+
return new ProjectionDefinitionCompiler(artifacts, 'test').compile([TotalsProjection], []);
|
|
55
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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 { ReadModelScenario } from '../../../ReadModelScenario.js';
|
|
5
|
+
import { Counted, ItemChanged, ItemRemoved, Removed } from './compiled_projection.js';
|
|
6
|
+
|
|
7
|
+
type CountedParent = { id: string; total: number; items: Array<{ itemId: string; name: string }> };
|
|
8
|
+
|
|
9
|
+
export async function lifecycle<T extends CountedParent>(scenario: ReadModelScenario<T>) {
|
|
10
|
+
scenario.given.forEventSource('A').events(new Counted(9000), new Counted(8000));
|
|
11
|
+
scenario.given.forEventSource('A').events(new ItemChanged('one', 'first'), new ItemChanged('two', 'second'), new ItemChanged('one', 'updated'), new Counted(7000));
|
|
12
|
+
scenario.given.forEventSource('B').events(new Counted(6000), new ItemChanged('one', 'independent'));
|
|
13
|
+
const first = (await scenario.instanceForEventSourceId('A'))!;
|
|
14
|
+
const other = (await scenario.instanceForEventSourceId('B'))!;
|
|
15
|
+
scenario.given.forEventSource('A').events(new ItemRemoved('one'), new ItemRemoved('unknown'));
|
|
16
|
+
const remaining = (await scenario.instanceForEventSourceId('A'))!;
|
|
17
|
+
scenario.given.forEventSource('A').events(new Removed());
|
|
18
|
+
const removed = await scenario.instanceForEventSourceId('A');
|
|
19
|
+
const surviving = (await scenario.instanceForEventSourceId('B'))!;
|
|
20
|
+
scenario.given.forEventSource('A').events(new Counted(5000), new ItemChanged('new', 'recreated'));
|
|
21
|
+
const recreated = (await scenario.instanceForEventSourceId('A'))!;
|
|
22
|
+
const repeated = (await scenario.instanceForEventSourceId('A'))!;
|
|
23
|
+
return { first, other, remaining, removed, surviving, recreated, repeated };
|
|
24
|
+
}
|
package/testing/for_ReadModelScenario/when_counting_with_children/with_a_fluent_projection.spec.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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 { readFileSync } from 'node:fs';
|
|
5
|
+
import { beforeAll, chai, describe, it } from 'vitest';
|
|
6
|
+
import type { JsonSchema } from '../../../schemas/JsonSchema.js';
|
|
7
|
+
import type { IProjectionBuilderFor } from '../../../projections/declarative/IProjectionBuilderFor.js';
|
|
8
|
+
import { projection } from '../../../projections/declarative/projection.js';
|
|
9
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
10
|
+
import { artifacts, compile, Counted, Item, ItemChanged, ItemRemoved, Removed, Totals } from './given/compiled_projection.js';
|
|
11
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
12
|
+
|
|
13
|
+
chai.should();
|
|
14
|
+
|
|
15
|
+
class MappedProjection {
|
|
16
|
+
define(builder: IProjectionBuilderFor<Totals>): void {
|
|
17
|
+
builder.from(Counted)
|
|
18
|
+
.children<Item>(model => model.items, child => child.identifiedBy(item => item.itemId)
|
|
19
|
+
.from(ItemChanged, from => from.usingKey(event => event.itemId))
|
|
20
|
+
.removedWith(ItemRemoved, removal => removal.usingKey(event => event.itemId)))
|
|
21
|
+
.removedWith(Removed);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
projection('mapped-children', Totals)(MappedProjection);
|
|
25
|
+
|
|
26
|
+
describe('when compiling a root count with typed children', () => {
|
|
27
|
+
let wire: ReturnType<typeof compile>['definitions'][number];
|
|
28
|
+
let schema: JsonSchema;
|
|
29
|
+
beforeAll(() => {
|
|
30
|
+
const compiled = compile();
|
|
31
|
+
wire = compiled.definitions[0];
|
|
32
|
+
schema = JSON.parse(compiled.readModels[0].Schema);
|
|
33
|
+
});
|
|
34
|
+
for (const name of ['count-children-root-first.json', 'count-children-child-first.json']) {
|
|
35
|
+
it(`should match the real compiler wire and schemas in ${name}`, () => {
|
|
36
|
+
const fixture = JSON.parse(readFileSync(new URL(`../../projections/fixtures/${name}`, import.meta.url), 'utf8'));
|
|
37
|
+
const compiled = compile();
|
|
38
|
+
const definition = compiled.definitions[0];
|
|
39
|
+
// Only the oracle input omits registration metadata; client output retains it.
|
|
40
|
+
const { LastUpdated, ...emittedFields } = JSON.parse(JSON.stringify(definition));
|
|
41
|
+
LastUpdated.should.deep.equal(fixture.compilerLastUpdated);
|
|
42
|
+
Object.hasOwn(fixture.wireDefinition, 'LastUpdated').should.be.false;
|
|
43
|
+
const oracleFields = Object.hasOwn(emittedFields, 'NoAutoMapProperties')
|
|
44
|
+
? emittedFields : { ...emittedFields, NoAutoMapProperties: [] };
|
|
45
|
+
oracleFields.should.deep.equal(fixture.wireDefinition);
|
|
46
|
+
definition.LastUpdated!.should.deep.equal(fixture.compilerLastUpdated);
|
|
47
|
+
JSON.parse(compiled.readModels[0].Schema).should.deep.equal(fixture.readModel.schema);
|
|
48
|
+
const emittedSchemas = [...compiled.eventSchemas.get(definition)!.values()];
|
|
49
|
+
const oracleSchemas = emittedSchemas.map(({ eventType, ...fields }) => {
|
|
50
|
+
// The kernel EventType constructor defaults only a false tombstone flag.
|
|
51
|
+
const { Tombstone, ...header } = eventType;
|
|
52
|
+
Tombstone.should.be.false;
|
|
53
|
+
return { ...fields, eventType: header };
|
|
54
|
+
});
|
|
55
|
+
oracleSchemas.should.deep.equal(fixture.eventSchemas);
|
|
56
|
+
emittedSchemas.every(entry => Object.hasOwn(entry.eventType, 'Tombstone') && entry.eventType.Tombstone === false).should.be.true;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
it('should admit count with children through the ordinary public constructor', () => {
|
|
61
|
+
(() => new ReadModelScenario(Totals, artifacts)).should.not.throw();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should preserve the existing non-arithmetic child-before-parent placeholder path', async () => {
|
|
65
|
+
const ordinary = new ReadModelScenario(Totals, { ...artifacts, projections: [MappedProjection] });
|
|
66
|
+
ordinary.given.forEventSource('A').events(new ItemChanged('one', 'first'), new ItemChanged('one', 'updated'));
|
|
67
|
+
(await ordinary.instance)!.items.should.deep.equal([{ itemId: 'one', name: 'updated' }]);
|
|
68
|
+
ordinary.given.forEventSource('A').events(new Counted(9000));
|
|
69
|
+
(await ordinary.instance)!.items.should.deep.equal([{ itemId: 'one', name: 'updated' }]);
|
|
70
|
+
(await ordinary.instance)!.total.should.equal(9000);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should compile one root count subscription', () => wire.From[0].Value.Properties.should.deep.equal({ total: '$count' }));
|
|
74
|
+
it('should compile an explicitly typed child collection', () => schema.properties!.items.should.deep.equal({
|
|
75
|
+
type: 'array', items: {
|
|
76
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema', title: 'Item', type: 'object',
|
|
77
|
+
properties: { itemId: { type: 'string' }, name: { type: 'string' } },
|
|
78
|
+
required: ['itemId', 'name'], additionalProperties: false
|
|
79
|
+
}
|
|
80
|
+
}));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('when counting a parent with fluent children and no initial values', () => {
|
|
84
|
+
let result: Awaited<ReturnType<typeof lifecycle<Totals>>>;
|
|
85
|
+
beforeAll(async () => { result = await lifecycle(new ReadModelScenario(Totals, artifacts)); });
|
|
86
|
+
it('should count root events without assigning their same-named payload', () => result.first.total.should.equal(3));
|
|
87
|
+
it('should update children without changing the root count', () => result.first.items.should.deep.equal([
|
|
88
|
+
{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }
|
|
89
|
+
]));
|
|
90
|
+
it('should remove only the matching child and ignore unknown children', () => result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]));
|
|
91
|
+
it('should leave the other parent independent', () => [result.other.total, result.surviving.id, result.removed].should.deep.equal([1, 'B', null]));
|
|
92
|
+
it('should discard old children and count on root recreation', () => [result.recreated.total, result.recreated.items].should.deep.equal([1, [{ itemId: 'new', name: 'recreated' }]]));
|
|
93
|
+
it('should not count an unchanged repeated read twice', () => result.repeated.should.deep.equal(result.recreated));
|
|
94
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
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 { beforeAll, chai, describe, it } from 'vitest';
|
|
6
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
7
|
+
import { count } from '../../../projections/modelBound/count.js';
|
|
8
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
9
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
10
|
+
import { Counted, ItemChanged, ItemRemoved, Removed } from './given/compiled_projection.js';
|
|
11
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
12
|
+
|
|
13
|
+
chai.should();
|
|
14
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
15
|
+
class Item {
|
|
16
|
+
@field(String) itemId!: string;
|
|
17
|
+
@field(String) name!: string;
|
|
18
|
+
}
|
|
19
|
+
@removedWith(Removed)
|
|
20
|
+
class Totals {
|
|
21
|
+
@field(String) id!: string;
|
|
22
|
+
@field(Number) @count(Counted) total!: number;
|
|
23
|
+
@field(Array) @childrenFrom(ItemChanged, { childType: Item, key: 'itemId', identifiedBy: 'itemId' })
|
|
24
|
+
items!: Item[];
|
|
25
|
+
}
|
|
26
|
+
const artifacts = { eventTypes: [Counted, ItemChanged, ItemRemoved, Removed], readModels: [Totals], reducers: [], projections: [] };
|
|
27
|
+
|
|
28
|
+
describe('when counting a parent with legacy-decorator children and no initial values', () => {
|
|
29
|
+
let result: Awaited<ReturnType<typeof lifecycle<Totals>>>;
|
|
30
|
+
beforeAll(async () => { result = await lifecycle(new ReadModelScenario(Totals, artifacts)); });
|
|
31
|
+
it('should count root events without assigning their same-named payload', () => result.first.total.should.equal(3));
|
|
32
|
+
it('should update children without changing the root count', () => result.first.items.should.deep.equal([
|
|
33
|
+
{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }
|
|
34
|
+
]));
|
|
35
|
+
it('should remove only the matching child and ignore unknown children', () => result.remaining.items.should.deep.equal([{ itemId: 'two', name: 'second' }]));
|
|
36
|
+
it('should leave the other parent independent', () => [result.other.total, result.surviving.id, result.removed].should.deep.equal([1, 'B', null]));
|
|
37
|
+
it('should discard old children and count on root recreation', () => [result.recreated.total, result.recreated.items].should.deep.equal([1, [{ itemId: 'new', name: 'recreated' }]]));
|
|
38
|
+
it('should not count an unchanged repeated read twice', () => result.repeated.should.deep.equal(result.recreated));
|
|
39
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
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 { beforeAll, chai, describe, it, type Assertion } from 'vitest';
|
|
6
|
+
import { childrenFrom } from '../../../projections/modelBound/childrenFrom.js';
|
|
7
|
+
import { count } from '../../../projections/modelBound/count.js';
|
|
8
|
+
import { removedWith } from '../../../projections/modelBound/removedWith.js';
|
|
9
|
+
import { ReadModelScenario } from '../../ReadModelScenario.js';
|
|
10
|
+
import { Counted, ItemChanged, ItemRemoved, Removed } from './given/compiled_projection.js';
|
|
11
|
+
import { lifecycle } from './given/lifecycle.js';
|
|
12
|
+
|
|
13
|
+
chai.should();
|
|
14
|
+
function should(value: unknown): Assertion { return (value as { should: Assertion }).should; }
|
|
15
|
+
@removedWith(ItemRemoved, 'itemId')
|
|
16
|
+
class Item {
|
|
17
|
+
@field(String) itemId!: string;
|
|
18
|
+
@field(String) name!: string;
|
|
19
|
+
}
|
|
20
|
+
@removedWith(Removed)
|
|
21
|
+
class Totals {
|
|
22
|
+
@field(String) id!: string;
|
|
23
|
+
@field(Number) @count(Counted) total!: number;
|
|
24
|
+
@field(Array, { genericArguments: [Item] }) @childrenFrom(ItemChanged, { key: 'itemId', identifiedBy: 'itemId' })
|
|
25
|
+
items!: Item[];
|
|
26
|
+
}
|
|
27
|
+
const artifacts = { eventTypes: [Counted, ItemChanged, ItemRemoved, Removed], readModels: [Totals], reducers: [], projections: [] };
|
|
28
|
+
|
|
29
|
+
describe('when counting a parent with standard-decorator children and no initial values', () => {
|
|
30
|
+
let result: Awaited<ReturnType<typeof lifecycle<Totals>>>;
|
|
31
|
+
beforeAll(async () => { result = await lifecycle(new ReadModelScenario(Totals, artifacts)); });
|
|
32
|
+
it('should count root events without assigning their same-named payload', () => should(result.first.total).equal(3));
|
|
33
|
+
it('should update children without changing the root count', () => should(result.first.items).deep.equal([
|
|
34
|
+
{ itemId: 'one', name: 'updated' }, { itemId: 'two', name: 'second' }
|
|
35
|
+
]));
|
|
36
|
+
it('should remove only the matching child and ignore unknown children', () => should(result.remaining.items).deep.equal([{ itemId: 'two', name: 'second' }]));
|
|
37
|
+
it('should leave the other parent independent', () => should([result.other.total, result.surviving.id, result.removed]).deep.equal([1, 'B', null]));
|
|
38
|
+
it('should discard old children and count on root recreation', () => should([result.recreated.total, result.recreated.items]).deep.equal([1, [{ itemId: 'new', name: 'recreated' }]]));
|
|
39
|
+
it('should not count an unchanged repeated read twice', () => should(result.repeated).deep.equal(result.recreated));
|
|
40
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
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 { ChildrenDefinition } from '@cratis/chronicle.contracts';
|
|
5
|
+
import type { FromRecord, RemovedWithRecord } from '../../projections/declarative/ProjectionBuilderCore.js';
|
|
6
|
+
import type { JsonSchema } from '../../schemas/JsonSchema.js';
|
|
7
|
+
import { eventContractPath } from '../../projections/eventContractPath.js';
|
|
8
|
+
import type { ProjectionEventSchemaResolver } from './ProjectionEventSchemaResolver.js';
|
|
9
|
+
|
|
10
|
+
/** The measured intersection of one root accumulator and independent typed children. */
|
|
11
|
+
export class ProjectionArithmeticChildrenCapabilities {
|
|
12
|
+
static supports(schema: JsonSchema, wire: Record<string, unknown>, require: ProjectionEventSchemaResolver['require']): boolean {
|
|
13
|
+
const from = wire.From as FromRecord[] ?? [];
|
|
14
|
+
const children = Object.entries(wire.Children as Record<string, ChildrenDefinition> ?? {});
|
|
15
|
+
if (from.length !== 1 || children.length !== 1) return false;
|
|
16
|
+
const mappings = Object.entries(from[0].Value.Properties ?? {});
|
|
17
|
+
if (mappings.length !== 1) return false;
|
|
18
|
+
const [accumulator, expression] = mappings[0];
|
|
19
|
+
if (!this.accumulator(schema.properties?.[accumulator], expression, from[0], require)) return false;
|
|
20
|
+
const [collection, child] = children[0];
|
|
21
|
+
return this.rootShape(schema, accumulator, collection)
|
|
22
|
+
&& this.childShape(schema.properties?.[collection], child)
|
|
23
|
+
&& this.independentEvents(from, wire, child) && this.noInteractions(wire);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private static accumulator(target: JsonSchema | undefined, expression: string, from: FromRecord, require: ProjectionEventSchemaResolver['require']): boolean {
|
|
27
|
+
if (expression === '$count') return this.countTarget(target);
|
|
28
|
+
const operand = /^\$add\(([A-Za-z_][A-Za-z0-9]*)\)$/.exec(expression)?.[1];
|
|
29
|
+
if (!operand || !this.unformattedNumber(target)) return false;
|
|
30
|
+
const event = require(from.Key, eventContractPath('From', from.Key));
|
|
31
|
+
return this.unformattedNumber(event.properties?.[operand]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private static unformattedNumber(schema?: JsonSchema): boolean {
|
|
35
|
+
return schema?.type === 'number' && schema.format === undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private static rootShape(schema: JsonSchema, count: string, collection: string): boolean {
|
|
39
|
+
const properties = schema.properties ?? {};
|
|
40
|
+
return this.exactRootMembers(properties, count, collection)
|
|
41
|
+
&& this.plainString(properties.id);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private static exactRootMembers(properties: Record<string, JsonSchema>, count: string, collection: string): boolean {
|
|
45
|
+
const allowed = new Set(['id', count, collection]);
|
|
46
|
+
const names = Object.keys(properties);
|
|
47
|
+
return allowed.size === 3 && names.length === 3 && names.every(property => allowed.has(property));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private static countTarget(schema?: JsonSchema): boolean {
|
|
51
|
+
return schema?.type === 'number' && [undefined, '', 'double'].includes(schema.format);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private static plainString(schema?: JsonSchema): boolean {
|
|
55
|
+
return schema?.type === 'string' && !schema.format;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private static childShape(collection: JsonSchema | undefined, child: ChildrenDefinition): boolean {
|
|
59
|
+
return collection?.type === 'array' && this.stringItems(collection.items, child.IdentifiedBy)
|
|
60
|
+
&& child.From.length > 0 && child.Join.length === 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private static stringItems(items: JsonSchema | undefined, identifier: string): boolean {
|
|
64
|
+
if (items?.type !== 'object' || !items.properties) return false;
|
|
65
|
+
return this.plainString(items.properties[identifier])
|
|
66
|
+
&& Object.values(items.properties).every(property => this.plainString(property));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private static independentEvents(from: FromRecord[], wire: Record<string, unknown>, child: ChildrenDefinition): boolean {
|
|
70
|
+
const removals = wire.RemovedWith as RemovedWithRecord[] ?? [];
|
|
71
|
+
const rootEvents = new Set([...from, ...removals].map(entry => entry.Key.Id));
|
|
72
|
+
return [...child.From, ...child.RemovedWith].every(entry => {
|
|
73
|
+
if (!entry.Key) return false;
|
|
74
|
+
return !rootEvents.has(entry.Key.Id);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private static noInteractions(wire: Record<string, unknown>): boolean {
|
|
79
|
+
return (wire.InitialModelState ?? '{}') === '{}'
|
|
80
|
+
&& (wire.Join as unknown[] ?? []).length === 0;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -6,6 +6,7 @@ import type { FromRecord, RemovedWithRecord } from '../../projections/declarativ
|
|
|
6
6
|
import { eventContractPath } from '../../projections/eventContractPath.js';
|
|
7
7
|
import type { JsonSchema } from '../../schemas/JsonSchema.js';
|
|
8
8
|
import { ProjectionArithmetic } from './ProjectionArithmetic.js';
|
|
9
|
+
import { ProjectionArithmeticChildrenCapabilities } from './ProjectionArithmeticChildrenCapabilities.js';
|
|
9
10
|
import type { ProjectionCapabilityReject } from './ProjectionCapabilityReject.js';
|
|
10
11
|
import type { ProjectionEventSchemaResolver } from './ProjectionEventSchemaResolver.js';
|
|
11
12
|
import { ProjectionMappingCapabilities } from './ProjectionMappingCapabilities.js';
|
|
@@ -24,7 +25,7 @@ export class ProjectionRootCapabilities {
|
|
|
24
25
|
this.checkOverlaps();
|
|
25
26
|
const accumulators = new Set(this.from.flatMap(entry => Object.entries(entry.Value.Properties ?? {})
|
|
26
27
|
.filter(([, expression]) => ProjectionArithmetic.isArithmetic(expression)).map(([property]) => property)));
|
|
27
|
-
this.checkAccumulatorChildren(accumulators);
|
|
28
|
+
this.checkAccumulatorChildren(accumulators, resolver);
|
|
28
29
|
for (const entry of this.from) this.checkClearedAccumulators(entry, accumulators);
|
|
29
30
|
const mappings = new ProjectionMappingCapabilities(this.schema, this.reject, accumulators);
|
|
30
31
|
for (const entry of [...this.from, ...this.removedWith]) this.subscription(entry, resolver, mappings);
|
|
@@ -38,8 +39,10 @@ export class ProjectionRootCapabilities {
|
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
private checkAccumulatorChildren(accumulators: Set<string
|
|
42
|
-
if (accumulators.size
|
|
42
|
+
private checkAccumulatorChildren(accumulators: Set<string>, resolver: ProjectionEventSchemaResolver): void {
|
|
43
|
+
if (!accumulators.size) return;
|
|
44
|
+
if (!Object.keys(this.wire.Children as object ?? {}).length) return;
|
|
45
|
+
if (!ProjectionArithmeticChildrenCapabilities.supports(this.schema, this.wire, resolver.require)) {
|
|
43
46
|
this.reject('Children', 'root arithmetic combined with children requires a kernel-backed test');
|
|
44
47
|
}
|
|
45
48
|
}
|
package/testing/projections/for_ProjectionCapabilities/when_validating_add_with_children.spec.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
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_adding_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(amount)', '$add(amount + 1)', '$add(nested.amount)', '$add($eventSourceId)',
|
|
18
|
+
'$add(1)', '$add(true)', '$add(snake_case)', '$add(missing)', '$add(amount', '$add(_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
|
+
['initial values', ({ definition }) => { definition.InitialModelState = '{"total":0}'; }],
|
|
39
|
+
['root join', ({ definition }) => { definition.Join.push({ Key: definition.From[0].Key, Value: { On: 'id', Properties: {}, Key: '$eventSourceId' } }); }],
|
|
40
|
+
['multiple child collections', ({ definition }) => { definition.Children.other = structuredClone(definition.Children.items); }],
|
|
41
|
+
['untyped children', ({ schema }) => { delete schema.properties!.items.items!.properties; }],
|
|
42
|
+
['numeric child property', ({ schema }) => { schema.properties!.items.items!.properties!.name = { type: 'number' }; }],
|
|
43
|
+
['missing child identifier', ({ schema }) => { delete schema.properties!.items.items!.properties!.itemId; }],
|
|
44
|
+
['child From without event Key', ({ definition }) => { delete definition.Children.items.From[0].Key; }],
|
|
45
|
+
['child removal without event Key', ({ definition }) => { delete definition.Children.items.RemovedWith[0].Key; }],
|
|
46
|
+
['shared root and child From', ({ definition }) => { definition.Children.items.From[0].Key = definition.From[0].Key; }],
|
|
47
|
+
['shared root From and child removal', ({ definition }) => { definition.Children.items.RemovedWith[0].Key = definition.From[0].Key; }],
|
|
48
|
+
['shared root removal and child From', ({ definition }) => { definition.Children.items.From[0].Key = definition.RemovedWith[0].Key; }],
|
|
49
|
+
['shared root and child removal', ({ definition }) => { definition.Children.items.RemovedWith[0].Key = definition.RemovedWith[0].Key; }],
|
|
50
|
+
['shared root From and removal', ({ definition }) => { definition.RemovedWith[0].Key = definition.From[0].Key; }],
|
|
51
|
+
['custom root key', ({ definition }) => { definition.From[0].Value.Key = 'amount'; }],
|
|
52
|
+
['custom root parent key', ({ definition }) => { definition.From[0].Value.ParentKey = 'amount'; }],
|
|
53
|
+
['custom child parent key', ({ definition }) => { definition.Children.items.From[0].Value.ParentKey = 'itemId'; }],
|
|
54
|
+
['child join', ({ definition }) => { definition.Children.items.Join.push({}); }],
|
|
55
|
+
['nested children', ({ definition }) => { definition.Children.items.Children.nested = structuredClone(definition.Children.items); }],
|
|
56
|
+
['value children', ({ definition }) => { definition.Children.items.FromEventProperty = 'items'; }],
|
|
57
|
+
['unknown child expression', ({ definition }) => { definition.Children.items.From[0].Value.Properties.name = '$unknown(name)'; }],
|
|
58
|
+
['unknown root expression', ({ definition }) => { definition.From[0].Value.Properties.total = '$unknown(amount)'; }]
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
function candidate(): Candidate {
|
|
62
|
+
const compiled = compile();
|
|
63
|
+
const definition = compiled.definitions[0];
|
|
64
|
+
const operand = [...compiled.eventSchemas.get(definition)!.values()].find(entry => entry.eventType.Id === 'Added')!.schema.properties!.amount;
|
|
65
|
+
return { compiled, definition, schema: JSON.parse(compiled.readModels[0].Schema), operand };
|
|
66
|
+
}
|
|
67
|
+
const seeded: ScenarioEvent = {
|
|
68
|
+
sourceId: 'A', content: { amount: 2.5 },
|
|
69
|
+
context: {
|
|
70
|
+
eventSourceId: 'A', eventType: EventType.parse('Added+1'), sequenceNumber: 0n,
|
|
71
|
+
occurred: new Date('2025-01-01T00:00:00Z'), correlationId: '00000000-0000-0000-0000-000000000000',
|
|
72
|
+
causation: [], tags: []
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
for (const [name, mutate] of boundaries) {
|
|
76
|
+
for (const [historyName, history] of [['empty', []], ['seeded', [seeded]]] as const) {
|
|
77
|
+
describe(`when rejecting addition with children: ${name}, ${historyName} history`, () => {
|
|
78
|
+
let act: () => Promise<unknown>;
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
const value = candidate();
|
|
81
|
+
mutate(value);
|
|
82
|
+
value.compiled.readModels[0].Schema = JSON.stringify(value.schema);
|
|
83
|
+
act = () => new ProjectionReadModelProcessor(Totals, value.compiled, value.definition).process(history);
|
|
84
|
+
});
|
|
85
|
+
it('should reject before replay with the model and contract path', () => {
|
|
86
|
+
act.should.throw(UnsupportedProjectionOperation).and.satisfy((error: Error) =>
|
|
87
|
+
error.message.includes('Totals') && /Children|From|Join|Schema|Definition/.test(error.message));
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|