@cratis/chronicle 6.70.1 → 6.71.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 (27) hide show
  1. package/dist/projections/IProjections.d.ts +27 -1
  2. package/dist/projections/IProjections.d.ts.map +1 -1
  3. package/dist/projections/ProjectionDefinitionCompiler.d.ts +11 -0
  4. package/dist/projections/ProjectionDefinitionCompiler.d.ts.map +1 -1
  5. package/dist/projections/ProjectionDefinitionCompiler.js +42 -18
  6. package/dist/projections/ProjectionDefinitionCompiler.js.map +1 -1
  7. package/dist/projections/Projections.d.ts +9 -0
  8. package/dist/projections/Projections.d.ts.map +1 -1
  9. package/dist/projections/Projections.js +62 -11
  10. package/dist/projections/Projections.js.map +1 -1
  11. package/dist/projections/RuntimeProjectionDefinitions.d.ts +26 -0
  12. package/dist/projections/RuntimeProjectionDefinitions.d.ts.map +1 -0
  13. package/dist/projections/RuntimeProjectionDefinitions.js +55 -0
  14. package/dist/projections/RuntimeProjectionDefinitions.js.map +1 -0
  15. package/dist/tsconfig.tsbuildinfo +1 -1
  16. package/package.json +1 -1
  17. package/projections/IProjections.ts +30 -1
  18. package/projections/ProjectionDefinitionCompiler.ts +46 -17
  19. package/projections/Projections.ts +72 -12
  20. package/projections/RuntimeProjectionDefinitions.ts +68 -0
  21. package/projections/for_Projections/given/default_runtime_registration.fixture.ts +53 -0
  22. package/projections/for_Projections/given/runtime_registration.fixture.ts +51 -0
  23. package/projections/for_Projections/when_mixing_runtime_registration_styles.spec.ts +73 -0
  24. package/projections/for_Projections/when_reconnecting_with_runtime_projections.spec.ts +36 -0
  25. package/projections/for_Projections/when_registering_at_runtime.spec.ts +72 -0
  26. package/projections/for_Projections/when_registering_with_the_default_provider.spec.ts +116 -0
  27. package/projections/for_Projections/when_runtime_registration_fails.spec.ts +106 -0
@@ -0,0 +1,106 @@
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 { describe, expect, it } from 'vitest';
5
+ import { Added, BoundModel, defineRuntime, RuntimeModel, runtime_registration } from './given/runtime_registration.fixture.js';
6
+
7
+ function expectNotRetained(context: ReturnType<typeof runtime_registration>): void {
8
+ expect(context.projections.hasFor('runtime-model')).toBe(false);
9
+ expect(context.projections.hasForModel(RuntimeModel)).toBe(false);
10
+ expect(() => context.projections.getProjectionIdFor(RuntimeModel)).toThrow('No projection found for read model');
11
+ }
12
+
13
+ describe('when runtime registration conflicts with discovery', () => {
14
+ it('should reject a runtime identifier already used by a discovered model-bound projection', async () => {
15
+ const context = runtime_registration();
16
+ context.artifacts.readModels.push(BoundModel);
17
+ await expect(context.projections.registerFor(RuntimeModel, defineRuntime, 'BoundModel')).rejects.toThrow("Projection id 'BoundModel' is already discovered");
18
+ expect(context.registerReadModel).not.toHaveBeenCalled();
19
+ expect(context.registerProjection).not.toHaveBeenCalled();
20
+ });
21
+
22
+ it('should reject a discovery refresh that tries to overwrite a runtime identifier', async () => {
23
+ const context = runtime_registration();
24
+ await context.projections.registerFor(RuntimeModel, defineRuntime, 'BoundModel');
25
+ context.artifacts.readModels.push(BoundModel);
26
+ await expect(context.projections.discover()).rejects.toThrow("Projection id 'BoundModel' is already registered at runtime");
27
+ expect(context.projections.getProjectionIdFor(RuntimeModel).value).toBe('BoundModel');
28
+ });
29
+ });
30
+
31
+ describe('when runtime identifiers conflict', () => {
32
+ it('should reject a second registration without replacing the retained definition', async () => {
33
+ const context = runtime_registration();
34
+ await context.projections.registerFor(RuntimeModel, defineRuntime);
35
+ await expect(context.projections.registerFor(RuntimeModel, builder => builder.passive())).rejects.toThrow('already registered at runtime');
36
+ await context.projections.register();
37
+ expect(context.registerProjection).toHaveBeenCalledTimes(2);
38
+ expect(context.registerProjection.mock.calls[1][0]).toEqual(context.registerProjection.mock.calls[0][0]);
39
+ });
40
+
41
+ it('should reserve in-flight identifiers and release them on failure', async () => {
42
+ const context = runtime_registration();
43
+ let rejectRegistration!: (error: Error) => void;
44
+ let started!: () => void;
45
+ const pending = new Promise<void>(resolve => { started = resolve; });
46
+ context.registerProjection.mockImplementationOnce(() => {
47
+ started();
48
+ return new Promise((_resolve, reject) => { rejectRegistration = reject; });
49
+ });
50
+ const registration = context.projections.registerFor(RuntimeModel, defineRuntime).catch(error => error);
51
+ await pending;
52
+ expectNotRetained(context);
53
+ await expect(context.projections.registerFor(RuntimeModel, defineRuntime)).rejects.toThrow('already registered at runtime');
54
+ rejectRegistration(new Error('Rejected declaration'));
55
+ expect(await registration).toBeInstanceOf(Error);
56
+ await context.projections.registerFor(RuntimeModel, defineRuntime);
57
+ expect(context.projections.hasForModel(RuntimeModel)).toBe(true);
58
+ });
59
+
60
+ it('should refuse a second projection for the same read model', async () => {
61
+ const context = runtime_registration();
62
+ await context.projections.registerFor(RuntimeModel, defineRuntime);
63
+ await expect(context.projections.registerFor(RuntimeModel, defineRuntime, 'another')).rejects.toThrow('already has a projection');
64
+ expect(context.registerProjection).toHaveBeenCalledTimes(1);
65
+ });
66
+ });
67
+
68
+ describe('when the kernel rejects runtime registration', () => {
69
+ it('should surface the failure without retaining a definition, and allow a later retry', async () => {
70
+ const context = runtime_registration();
71
+ context.registerProjection.mockRejectedValueOnce(new Error('Invalid declaration'));
72
+ await expect(context.projections.registerFor(RuntimeModel, defineRuntime)).rejects.toThrow('Invalid declaration');
73
+ expectNotRetained(context);
74
+ await context.projections.register();
75
+ expect(context.registerProjection).toHaveBeenCalledTimes(1);
76
+ await context.projections.registerFor(RuntimeModel, defineRuntime);
77
+ expect(context.projections.hasForModel(RuntimeModel)).toBe(true);
78
+ });
79
+
80
+ it('should not retain anything when read model registration fails', async () => {
81
+ const context = runtime_registration();
82
+ context.registerReadModel.mockRejectedValueOnce(new Error('Invalid schema'));
83
+ await expect(context.projections.registerFor(RuntimeModel, defineRuntime)).rejects.toThrow('Invalid schema');
84
+ expectNotRetained(context);
85
+ expect(context.registerProjection).not.toHaveBeenCalled();
86
+ });
87
+ });
88
+
89
+ describe('when validating an explicit runtime projection', () => {
90
+ it('should surface existing builder validation before contacting the kernel', async () => {
91
+ const context = runtime_registration();
92
+ await expect(context.projections.registerFor(RuntimeModel, builder => {
93
+ builder.from(Added, from => from.usingKeyFromContext('unknownContextProperty'));
94
+ })).rejects.toThrow('unknownContextProperty');
95
+ expectNotRetained(context);
96
+ expect(context.registerReadModel).not.toHaveBeenCalled();
97
+ expect(context.registerProjection).not.toHaveBeenCalled();
98
+ });
99
+
100
+ it('should refuse a type without model-bound decorators', async () => {
101
+ const context = runtime_registration();
102
+ await expect(context.projections.registerModelBound(RuntimeModel)).rejects.toThrow('has no model-bound projection');
103
+ expectNotRetained(context);
104
+ expect(context.registerReadModel).not.toHaveBeenCalled();
105
+ });
106
+ });