@cratis/chronicle 6.31.0 → 6.31.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 (25) hide show
  1. package/connection/ChronicleConnection.ts +1 -0
  2. package/connection/encodeExplicitFalse.ts +56 -0
  3. package/connection/for_encodeExplicitFalse/when_encoding_reactor_definition.spec.ts +34 -0
  4. package/connection/for_encodeExplicitFalse/when_encoding_webhook_definition.spec.ts +39 -0
  5. package/connection/for_encodeExplicitFalse/wireFields.ts +24 -0
  6. package/dist/connection/ChronicleConnection.d.ts +1 -0
  7. package/dist/connection/ChronicleConnection.d.ts.map +1 -1
  8. package/dist/connection/ChronicleConnection.js +1 -0
  9. package/dist/connection/ChronicleConnection.js.map +1 -1
  10. package/dist/connection/encodeExplicitFalse.d.ts +6 -0
  11. package/dist/connection/encodeExplicitFalse.d.ts.map +1 -0
  12. package/dist/connection/encodeExplicitFalse.js +45 -0
  13. package/dist/connection/encodeExplicitFalse.js.map +1 -0
  14. package/dist/connection/for_encodeExplicitFalse/wireFields.d.ts +6 -0
  15. package/dist/connection/for_encodeExplicitFalse/wireFields.d.ts.map +1 -0
  16. package/dist/connection/for_encodeExplicitFalse/wireFields.js +24 -0
  17. package/dist/connection/for_encodeExplicitFalse/wireFields.js.map +1 -0
  18. package/dist/tsconfig.tsbuildinfo +1 -1
  19. package/events/constraints/for_Constraints/against_a_kernel/when_appending_unique_values_concurrently.integration.spec.ts +189 -0
  20. package/events/migrations/for_EventTypeMigrators/against_a_kernel/when_replaying_an_older_generation.integration.spec.ts +156 -0
  21. package/jobs/for_Jobs/against_a_kernel/when_managing_a_replay_job.integration.spec.ts +114 -0
  22. package/package.json +9 -9
  23. package/reactors/for_Reactors/against_a_kernel/when_replaying_once_only_reactors.integration.spec.ts +111 -0
  24. package/reactors/for_Reactors/against_a_kernel/when_replaying_reactors_with_once_only_handlers.integration.spec.ts +131 -0
  25. package/webhooks/for_Webhooks/against_a_kernel/when_registering_and_removing_a_webhook.integration.spec.ts +88 -0
@@ -0,0 +1,131 @@
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 { randomUUID } from 'node:crypto';
5
+ import { afterAll, beforeAll, chai, describe, it } from 'vitest';
6
+ import { field } from '@cratis/fundamentals';
7
+ import { ChronicleClient } from '../../../ChronicleClient.js';
8
+ import { ChronicleOptions } from '../../../ChronicleOptions.js';
9
+ import type { IClientArtifactsProvider } from '../../../artifacts/index.js';
10
+ import type { ChronicleConnection } from '../../../connection/index.js';
11
+ import { eventType } from '../../../events/index.js';
12
+ import type { IEventStore } from '../../../IEventStore.js';
13
+ import { onceOnly } from '../../onceOnly.js';
14
+ import { reactor } from '../../reactor.js';
15
+ import { replay } from '../../replay.js';
16
+
17
+ chai.should();
18
+
19
+ // Runs against a real Chronicle kernel; skipped locally unless the connection string is set (see vitest.integration.config.ts).
20
+ // In CI it never skips, so a missing connection string or unreachable kernel fails instead of passing vacuously.
21
+ const connectionString = process.env.CHRONICLE_INTEGRATION_CONNECTION_STRING;
22
+
23
+ @eventType()
24
+ class MethodOnceOnlyHappened {
25
+ @field(String) name = '';
26
+ }
27
+
28
+ @eventType()
29
+ class MethodReplayableHappened {
30
+ @field(String) name = '';
31
+ }
32
+
33
+ @eventType()
34
+ class MethodReplayOnceOnlyHappened {
35
+ @field(String) name = '';
36
+ }
37
+
38
+ // The client constructs reactor instances; share a sink for assertions across that boundary.
39
+ const invocations: string[] = [];
40
+
41
+ @reactor()
42
+ class MixedOnceOnlyReactor {
43
+ @onceOnly()
44
+ methodOnceOnlyHappened(event: MethodOnceOnlyHappened) { invocations.push(`once-only:${event.name}`); }
45
+
46
+ methodReplayableHappened(event: MethodReplayableHappened) { invocations.push(`replayable:${event.name}`); }
47
+
48
+ methodReplayOnceOnlyHappened(event: MethodReplayOnceOnlyHappened) { invocations.push(`live:${event.name}`); }
49
+
50
+ @replay()
51
+ @onceOnly()
52
+ replayMethodReplayOnceOnlyHappened(event: MethodReplayOnceOnlyHappened) { invocations.push(`replay:${event.name}`); }
53
+ }
54
+
55
+ const artifacts: IClientArtifactsProvider = {
56
+ eventTypes: [MethodOnceOnlyHappened, MethodReplayableHappened, MethodReplayOnceOnlyHappened],
57
+ readModels: [],
58
+ reactors: [MixedOnceOnlyReactor],
59
+ reducers: [],
60
+ seeders: [],
61
+ constraints: [],
62
+ projections: [],
63
+ webhooks: [],
64
+ eventTypeMigrations: [],
65
+ globalForHandlers: []
66
+ };
67
+
68
+ async function eventually(accept: () => boolean, timeoutMs = 30_000): Promise<void> {
69
+ const deadline = Date.now() + timeoutMs;
70
+ while (!accept()) {
71
+ if (Date.now() > deadline) throw new Error('Timed out waiting for the kernel');
72
+ await new Promise(resolve => setTimeout(resolve, 250));
73
+ }
74
+ }
75
+
76
+ const isEmptyJobId = (jobId: string | undefined) => /^[0-]*$/.test(jobId ?? '');
77
+
78
+ describe.skipIf(!connectionString && !process.env.CI)('when replaying a reactor with once-only handlers against a kernel', () => {
79
+ const storeName = `Method${randomUUID().replaceAll('-', '').slice(0, 12)}`;
80
+ let client: ChronicleClient;
81
+ let store: IEventStore;
82
+ let connection: ChronicleConnection;
83
+ let jobId: string | undefined;
84
+ let invocationsBeforeReplay: string[];
85
+
86
+ beforeAll(async () => {
87
+ client = new ChronicleClient(ChronicleOptions.fromConnectionString(connectionString!, {
88
+ discoveryPatterns: [],
89
+ clientArtifactsProvider: artifacts
90
+ }));
91
+ store = await client.getEventStore(storeName);
92
+ connection = (store as unknown as { _connection: ChronicleConnection })._connection;
93
+
94
+ (await store.eventLog.append(randomUUID(), Object.assign(new MethodOnceOnlyHappened(), { name: 'a' }))).isSuccess.should.be.true;
95
+ (await store.eventLog.append(randomUUID(), Object.assign(new MethodReplayableHappened(), { name: 'b' }))).isSuccess.should.be.true;
96
+ (await store.eventLog.append(randomUUID(), Object.assign(new MethodReplayOnceOnlyHappened(), { name: 'c' }))).isSuccess.should.be.true;
97
+ await eventually(() => invocations.length === 3);
98
+ invocationsBeforeReplay = [...invocations];
99
+
100
+ jobId = (await connection.observers.replay({
101
+ EventStore: storeName,
102
+ Namespace: 'Default',
103
+ ObserverId: 'MixedOnceOnlyReactor',
104
+ EventSequenceId: 'event-log'
105
+ })).JobId;
106
+ await eventually(() => invocations.includes('replayable:b') && invocations.filter(_ => _ === 'replayable:b').length === 2);
107
+ await new Promise(resolve => setTimeout(resolve, 3_000));
108
+ });
109
+
110
+ afterAll(() => client?.dispose());
111
+
112
+ it('should run every handler once before the replay', () => {
113
+ invocationsBeforeReplay.sort().should.deep.equal(['live:c', 'once-only:a', 'replayable:b']);
114
+ });
115
+
116
+ it('should start the replay', () => {
117
+ isEmptyJobId(jobId).should.be.false;
118
+ });
119
+
120
+ it('should run the handler that is not once-only again', () => {
121
+ invocations.filter(_ => _ === 'replayable:b').length.should.equal(2);
122
+ });
123
+
124
+ it('should not run the once-only handler again', () => {
125
+ invocations.filter(_ => _ === 'once-only:a').length.should.equal(1);
126
+ });
127
+
128
+ it('should run neither the live nor the once-only replay handler again', () => {
129
+ invocations.filter(_ => _.endsWith(':c')).sort().should.deep.equal(['live:c']);
130
+ });
131
+ });
@@ -0,0 +1,88 @@
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 { randomUUID } from 'node:crypto';
5
+ import { afterAll, beforeAll, chai, describe, it } from 'vitest';
6
+ import type { WebhookDefinition } from '@cratis/chronicle.contracts';
7
+ import { field } from '@cratis/fundamentals';
8
+ import { ChronicleClient } from '../../../ChronicleClient.js';
9
+ import { ChronicleOptions } from '../../../ChronicleOptions.js';
10
+ import type { IClientArtifactsProvider } from '../../../artifacts/index.js';
11
+ import { eventType } from '../../../events/index.js';
12
+ import type { IEventStore } from '../../../IEventStore.js';
13
+
14
+ chai.should();
15
+
16
+ // Runs against a real Chronicle kernel; skipped locally unless the connection string is set (see vitest.integration.config.ts).
17
+ // In CI it never skips, so a missing connection string or unreachable kernel fails instead of passing vacuously.
18
+ const connectionString = process.env.CHRONICLE_INTEGRATION_CONNECTION_STRING;
19
+
20
+ @eventType()
21
+ class OrderShipped {
22
+ @field(String) orderId = '';
23
+ }
24
+
25
+ const artifacts: IClientArtifactsProvider = {
26
+ eventTypes: [OrderShipped],
27
+ readModels: [],
28
+ reactors: [],
29
+ reducers: [],
30
+ seeders: [],
31
+ constraints: [],
32
+ projections: [],
33
+ webhooks: [],
34
+ eventTypeMigrations: [],
35
+ globalForHandlers: []
36
+ };
37
+
38
+ async function eventually<T>(read: () => Promise<T>, accept: (value: T) => boolean, timeoutMs = 20_000): Promise<T> {
39
+ const deadline = Date.now() + timeoutMs;
40
+ let value = await read();
41
+ while (!accept(value)) {
42
+ if (Date.now() > deadline) throw new Error('Timed out waiting for the kernel');
43
+ await new Promise(resolve => setTimeout(resolve, 100));
44
+ value = await read();
45
+ }
46
+ return value;
47
+ }
48
+
49
+ describe.skipIf(!connectionString && !process.env.CI)('when registering and removing a webhook against a kernel', () => {
50
+ const webhookId = randomUUID();
51
+ const targetUrl = 'http://localhost:9/orders';
52
+ let client: ChronicleClient;
53
+ let store: IEventStore;
54
+ let registered: WebhookDefinition | undefined;
55
+ let afterRemove: WebhookDefinition[];
56
+ let registerWithoutIdentifier: unknown;
57
+
58
+ const findRegistered = async () => (await store.webhooks.getWebhooks()).find(_ => _.Identifier === webhookId);
59
+
60
+ beforeAll(async () => {
61
+ client = new ChronicleClient(ChronicleOptions.fromConnectionString(connectionString!, {
62
+ discoveryPatterns: [],
63
+ clientArtifactsProvider: artifacts
64
+ }));
65
+ store = await client.getEventStore(`WebhooksEndToEnd${randomUUID().replaceAll('-', '')}`);
66
+
67
+ await store.webhooks.register(webhookId, targetUrl, _ => _
68
+ .withEventType(OrderShipped)
69
+ .withHeader('X-Source', 'specs'));
70
+
71
+ // The kernel records the registration as events and builds its webhook list from them, so it shows up eventually.
72
+ registered = await eventually(findRegistered, _ => _ !== undefined);
73
+
74
+ await store.webhooks.remove(webhookId);
75
+ afterRemove = await eventually(() => store.webhooks.getWebhooks(), _ => !_.some(webhook => webhook.Identifier === webhookId));
76
+
77
+ registerWithoutIdentifier = await store.webhooks.register('', targetUrl, _ => _.withEventType(OrderShipped)).catch((error: unknown) => error);
78
+ });
79
+
80
+ afterAll(() => client?.dispose());
81
+
82
+ it('should list the webhook', () => registered!.Identifier.should.equal(webhookId));
83
+ it('should keep the target url', () => registered!.Target!.Url.should.equal(targetUrl));
84
+ it('should keep the header', () => registered!.Target!.Headers['X-Source'].should.equal('specs'));
85
+ it('should keep the event type', () => registered!.EventTypes.map(_ => _.Id).should.deep.equal(['OrderShipped']));
86
+ it('should no longer list the removed webhook', () => afterRemove.some(_ => _.Identifier === webhookId).should.be.false);
87
+ it('should report a registration the kernel rejects', () => registerWithoutIdentifier.should.be.instanceOf(Error));
88
+ });