@nathapp/nestjs-outbox 3.3.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.
- package/.eslintrc.js +8 -0
- package/.nax/features/nestjs-outbox/.nax-acceptance.test.ts +878 -0
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +15 -0
- package/README.md +50 -0
- package/dist/constant/index.d.ts +1 -0
- package/dist/constant/index.js +18 -0
- package/dist/constant/index.js.map +1 -0
- package/dist/constant/outbox.constants.d.ts +3 -0
- package/dist/constant/outbox.constants.js +7 -0
- package/dist/constant/outbox.constants.js.map +1 -0
- package/dist/exception/index.d.ts +3 -0
- package/dist/exception/index.js +20 -0
- package/dist/exception/index.js.map +1 -0
- package/dist/exception/outbox-exception-code.enum.d.ts +4 -0
- package/dist/exception/outbox-exception-code.enum.js +9 -0
- package/dist/exception/outbox-exception-code.enum.js.map +1 -0
- package/dist/exception/outbox-invalid-event.exception.d.ts +4 -0
- package/dist/exception/outbox-invalid-event.exception.js +13 -0
- package/dist/exception/outbox-invalid-event.exception.js.map +1 -0
- package/dist/exception/outbox-misconfigured.exception.d.ts +4 -0
- package/dist/exception/outbox-misconfigured.exception.js +13 -0
- package/dist/exception/outbox-misconfigured.exception.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/interface/index.d.ts +6 -0
- package/dist/interface/index.js +6 -0
- package/dist/interface/index.js.map +1 -0
- package/dist/interface/outbox-module-options.interface.d.ts +16 -0
- package/dist/interface/outbox-module-options.interface.js +3 -0
- package/dist/interface/outbox-module-options.interface.js.map +1 -0
- package/dist/interface/outbox-publisher.interface.d.ts +4 -0
- package/dist/interface/outbox-publisher.interface.js +3 -0
- package/dist/interface/outbox-publisher.interface.js.map +1 -0
- package/dist/interface/outbox-record.interface.d.ts +21 -0
- package/dist/interface/outbox-record.interface.js +3 -0
- package/dist/interface/outbox-record.interface.js.map +1 -0
- package/dist/interface/outbox-status.enum.d.ts +6 -0
- package/dist/interface/outbox-status.enum.js +11 -0
- package/dist/interface/outbox-status.enum.js.map +1 -0
- package/dist/interface/outbox-store.interface.d.ts +8 -0
- package/dist/interface/outbox-store.interface.js +3 -0
- package/dist/interface/outbox-store.interface.js.map +1 -0
- package/dist/module/index.d.ts +1 -0
- package/dist/module/index.js +18 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/outbox.module.d.ts +10 -0
- package/dist/module/outbox.module.js +63 -0
- package/dist/module/outbox.module.js.map +1 -0
- package/dist/provider/in-memory-outbox-store.d.ts +12 -0
- package/dist/provider/in-memory-outbox-store.js +97 -0
- package/dist/provider/in-memory-outbox-store.js.map +1 -0
- package/dist/provider/index.d.ts +4 -0
- package/dist/provider/index.js +21 -0
- package/dist/provider/index.js.map +1 -0
- package/dist/provider/outbox-relay.d.ts +20 -0
- package/dist/provider/outbox-relay.js +129 -0
- package/dist/provider/outbox-relay.js.map +1 -0
- package/dist/provider/outbox.service.d.ts +12 -0
- package/dist/provider/outbox.service.js +68 -0
- package/dist/provider/outbox.service.js.map +1 -0
- package/dist/provider/resolve-outbox-options.d.ts +29 -0
- package/dist/provider/resolve-outbox-options.js +34 -0
- package/dist/provider/resolve-outbox-options.js.map +1 -0
- package/dist/util/compute-backoff.d.ts +1 -0
- package/dist/util/compute-backoff.js +8 -0
- package/dist/util/compute-backoff.js.map +1 -0
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +18 -0
- package/dist/util/index.js.map +1 -0
- package/lib/constant/index.ts +1 -0
- package/lib/constant/outbox.constants.ts +3 -0
- package/lib/exception/index.ts +3 -0
- package/lib/exception/outbox-exception-code.enum.ts +4 -0
- package/lib/exception/outbox-invalid-event.exception.ts +9 -0
- package/lib/exception/outbox-misconfigured.exception.ts +9 -0
- package/lib/index.ts +6 -0
- package/lib/interface/index.ts +18 -0
- package/lib/interface/outbox-module-options.interface.ts +18 -0
- package/lib/interface/outbox-publisher.interface.ts +5 -0
- package/lib/interface/outbox-record.interface.ts +23 -0
- package/lib/interface/outbox-status.enum.ts +6 -0
- package/lib/interface/outbox-store.interface.ts +23 -0
- package/lib/module/index.ts +1 -0
- package/lib/module/outbox.module.ts +64 -0
- package/lib/provider/in-memory-outbox-store.ts +114 -0
- package/lib/provider/index.ts +4 -0
- package/lib/provider/outbox-relay.ts +147 -0
- package/lib/provider/outbox.service.ts +70 -0
- package/lib/provider/resolve-outbox-options.ts +51 -0
- package/lib/util/compute-backoff.ts +4 -0
- package/lib/util/index.ts +1 -0
- package/package.json +48 -0
- package/test/barrel/index.spec.ts +45 -0
- package/test/constant/outbox.constants.spec.ts +20 -0
- package/test/e2e/outbox.module.spec.ts +371 -0
- package/test/exception/outbox.exceptions.spec.ts +38 -0
- package/test/interface/outbox-status.enum.spec.ts +19 -0
- package/test/jest-nax-setup.js +31 -0
- package/test/jest-nax.json +30 -0
- package/test/jest-test.json +28 -0
- package/test/module/outbox.module.spec.ts +139 -0
- package/test/provider/in-memory-outbox-store.spec.ts +216 -0
- package/test/provider/outbox-relay.spec.ts +565 -0
- package/test/provider/outbox-service.spec.ts +229 -0
- package/test/provider/resolve-outbox-options.spec.ts +50 -0
- package/test/util/compute-backoff.spec.ts +25 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CLOCK,
|
|
3
|
+
IClock,
|
|
4
|
+
ID_GENERATOR,
|
|
5
|
+
} from '@nathapp/nestjs-common';
|
|
6
|
+
import { Logger } from '@nestjs/common';
|
|
7
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
8
|
+
import { TRANSACTION_MANAGER, ITransactionManager } from '@nathapp/nestjs-data';
|
|
9
|
+
import { OutboxModule } from '../../lib/module/outbox.module';
|
|
10
|
+
import { OutboxService } from '../../lib/provider/outbox.service';
|
|
11
|
+
import { OutboxRelay } from '../../lib/provider/outbox-relay';
|
|
12
|
+
import { InMemoryOutboxStore } from '../../lib/provider/in-memory-outbox-store';
|
|
13
|
+
import { IOutboxStore } from '../../lib/interface/outbox-store.interface';
|
|
14
|
+
import { IOutboxPublisher } from '../../lib/interface/outbox-publisher.interface';
|
|
15
|
+
import { OutboxRecord } from '../../lib/interface/outbox-record.interface';
|
|
16
|
+
import { OutboxStatus } from '../../lib/interface/outbox-status.enum';
|
|
17
|
+
import { OutboxMisconfiguredException } from '../../lib/exception/outbox-misconfigured.exception';
|
|
18
|
+
import { ResolvedOutboxOptions } from '../../lib/provider/resolve-outbox-options';
|
|
19
|
+
|
|
20
|
+
const defaultRelay: ResolvedOutboxOptions['relay'] = {
|
|
21
|
+
enabled: false,
|
|
22
|
+
pollIntervalMs: 5000,
|
|
23
|
+
batchSize: 10,
|
|
24
|
+
leaseMs: 30000,
|
|
25
|
+
maxAttempts: 10,
|
|
26
|
+
backoffBaseMs: 0,
|
|
27
|
+
backoffCapMs: 0,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const txManagerStub = (): ITransactionManager => ({
|
|
31
|
+
run: jest.fn(),
|
|
32
|
+
getClient: jest.fn().mockReturnValue({ __txClient: true }),
|
|
33
|
+
isInTransaction: jest.fn().mockReturnValue(false),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const buildIntegrationModule = async (options: {
|
|
37
|
+
store?: IOutboxStore;
|
|
38
|
+
publisher?: IOutboxPublisher;
|
|
39
|
+
relay?: ResolvedOutboxOptions['relay'];
|
|
40
|
+
clock?: IClock;
|
|
41
|
+
}): Promise<TestingModule> => {
|
|
42
|
+
const clock: IClock = options.clock ?? {
|
|
43
|
+
now: jest.fn().mockReturnValue(new Date('2026-05-01T00:00:00.000Z')),
|
|
44
|
+
};
|
|
45
|
+
const idGenerator = (() => {
|
|
46
|
+
let counter = 0;
|
|
47
|
+
return {
|
|
48
|
+
generate: jest.fn(() => `id-${++counter}`),
|
|
49
|
+
};
|
|
50
|
+
})();
|
|
51
|
+
|
|
52
|
+
return Test.createTestingModule({
|
|
53
|
+
imports: [
|
|
54
|
+
OutboxModule.register({
|
|
55
|
+
store: options.store,
|
|
56
|
+
publisher: options.publisher,
|
|
57
|
+
relay: options.relay ?? defaultRelay,
|
|
58
|
+
}),
|
|
59
|
+
],
|
|
60
|
+
providers: [
|
|
61
|
+
{ provide: TRANSACTION_MANAGER, useValue: txManagerStub() },
|
|
62
|
+
{ provide: CLOCK, useValue: clock },
|
|
63
|
+
{ provide: ID_GENERATOR, useValue: idGenerator },
|
|
64
|
+
],
|
|
65
|
+
}).compile();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const tryBuild = async (
|
|
69
|
+
build: () => Promise<TestingModule>,
|
|
70
|
+
): Promise<{ moduleRef?: TestingModule; error?: unknown }> => {
|
|
71
|
+
try {
|
|
72
|
+
const moduleRef = await build();
|
|
73
|
+
return { moduleRef };
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return { error };
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const safeGet = <T>(moduleRef: TestingModule, token: unknown): T | undefined => {
|
|
80
|
+
try {
|
|
81
|
+
return moduleRef.get(token as Parameters<TestingModule['get']>[0]) as T;
|
|
82
|
+
} catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
describe('OutboxModule — integration: record + dispatch + lifecycle', () => {
|
|
88
|
+
describe('AC6: record() × 3 then dispatchPendingBatch() — fake publisher receives all three, store reports each as PUBLISHED', () => {
|
|
89
|
+
it('AC6: three record() calls + one dispatchPendingBatch() — publisher receives all three and the store reports each as OutboxStatus.PUBLISHED', async () => {
|
|
90
|
+
const store = new InMemoryOutboxStore();
|
|
91
|
+
const publishMock = jest.fn().mockResolvedValue(undefined);
|
|
92
|
+
const publisher: IOutboxPublisher = { publish: publishMock };
|
|
93
|
+
|
|
94
|
+
const { moduleRef, error } = await tryBuild(() =>
|
|
95
|
+
buildIntegrationModule({
|
|
96
|
+
store,
|
|
97
|
+
publisher,
|
|
98
|
+
relay: { ...defaultRelay },
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
expect(error).toBeUndefined();
|
|
103
|
+
expect(moduleRef).toBeDefined();
|
|
104
|
+
if (moduleRef === undefined) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const service = safeGet<OutboxService>(moduleRef, OutboxService);
|
|
109
|
+
const relay = safeGet<OutboxRelay>(moduleRef, OutboxRelay);
|
|
110
|
+
|
|
111
|
+
expect(service).toBeDefined();
|
|
112
|
+
expect(relay).toBeDefined();
|
|
113
|
+
if (service === undefined || relay === undefined) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const rec1 = await service.record({ type: 'order.created', payload: { n: 1 } });
|
|
118
|
+
const rec2 = await service.record({ type: 'order.created', payload: { n: 2 } });
|
|
119
|
+
const rec3 = await service.record({ type: 'order.created', payload: { n: 3 } });
|
|
120
|
+
|
|
121
|
+
await relay.dispatchPendingBatch();
|
|
122
|
+
|
|
123
|
+
expect(publishMock).toHaveBeenCalledTimes(3);
|
|
124
|
+
const publishedIds = publishMock.mock.calls
|
|
125
|
+
.map((c) => (c[0] as OutboxRecord).id)
|
|
126
|
+
.sort();
|
|
127
|
+
expect(publishedIds).toEqual([rec1.id, rec2.id, rec3.id].sort());
|
|
128
|
+
|
|
129
|
+
const internal = (store as unknown as { map: Map<string, OutboxRecord> }).map;
|
|
130
|
+
expect(internal.get(rec1.id)!.status).toBe(OutboxStatus.PUBLISHED);
|
|
131
|
+
expect(internal.get(rec2.id)!.status).toBe(OutboxStatus.PUBLISHED);
|
|
132
|
+
expect(internal.get(rec3.id)!.status).toBe(OutboxStatus.PUBLISHED);
|
|
133
|
+
|
|
134
|
+
await moduleRef.close();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('AC6 (default-store path): record() + dispatchPendingBatch() with NO custom store — the default InMemoryOutboxStore is shared between OutboxService and OutboxRelay so the publisher receives the record', async () => {
|
|
138
|
+
const publishMock = jest.fn().mockResolvedValue(undefined);
|
|
139
|
+
const publisher: IOutboxPublisher = { publish: publishMock };
|
|
140
|
+
|
|
141
|
+
const { moduleRef, error } = await tryBuild(() =>
|
|
142
|
+
buildIntegrationModule({
|
|
143
|
+
publisher,
|
|
144
|
+
relay: { ...defaultRelay },
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect(error).toBeUndefined();
|
|
149
|
+
expect(moduleRef).toBeDefined();
|
|
150
|
+
if (moduleRef === undefined) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const service = safeGet<OutboxService>(moduleRef, OutboxService);
|
|
155
|
+
const relay = safeGet<OutboxRelay>(moduleRef, OutboxRelay);
|
|
156
|
+
expect(service).toBeDefined();
|
|
157
|
+
expect(relay).toBeDefined();
|
|
158
|
+
if (service === undefined || relay === undefined) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const rec = await service.record({ type: 'order.created', payload: { n: 1 } });
|
|
163
|
+
|
|
164
|
+
await relay.dispatchPendingBatch();
|
|
165
|
+
|
|
166
|
+
expect(publishMock).toHaveBeenCalledTimes(1);
|
|
167
|
+
const publishedRecord = publishMock.mock.calls[0][0] as OutboxRecord;
|
|
168
|
+
expect(publishedRecord.id).toBe(rec.id);
|
|
169
|
+
|
|
170
|
+
await moduleRef.close();
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('AC7: always-rejecting publisher + maxAttempts=3 + backoffBaseMs=0 — record then dispatchPendingBatch() × 3 across clock advance reaches DEAD', () => {
|
|
175
|
+
it('AC7: after three dispatchPendingBatch() invocations with the clock advancing past each nextAttemptAt, the record is DEAD with attempts=3', async () => {
|
|
176
|
+
const store = new InMemoryOutboxStore();
|
|
177
|
+
const publisher: IOutboxPublisher = {
|
|
178
|
+
publish: jest.fn().mockRejectedValue(new Error('broker down')),
|
|
179
|
+
};
|
|
180
|
+
const clockDates: Date[] = [
|
|
181
|
+
new Date('2026-05-01T00:00:00.000Z'),
|
|
182
|
+
new Date('2026-05-01T00:00:10.000Z'),
|
|
183
|
+
new Date('2026-05-01T00:00:20.000Z'),
|
|
184
|
+
new Date('2026-05-01T00:00:30.000Z'),
|
|
185
|
+
];
|
|
186
|
+
let clockIdx = 0;
|
|
187
|
+
const clock: IClock = {
|
|
188
|
+
now: jest.fn(() => clockDates[clockIdx++] ?? clockDates[clockDates.length - 1]),
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const { moduleRef, error } = await tryBuild(() =>
|
|
192
|
+
buildIntegrationModule({
|
|
193
|
+
store,
|
|
194
|
+
publisher,
|
|
195
|
+
clock,
|
|
196
|
+
relay: { ...defaultRelay, maxAttempts: 3, backoffBaseMs: 0, backoffCapMs: 0 },
|
|
197
|
+
}),
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
expect(error).toBeUndefined();
|
|
201
|
+
expect(moduleRef).toBeDefined();
|
|
202
|
+
if (moduleRef === undefined) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const service = safeGet<OutboxService>(moduleRef, OutboxService);
|
|
207
|
+
const relay = safeGet<OutboxRelay>(moduleRef, OutboxRelay);
|
|
208
|
+
|
|
209
|
+
expect(service).toBeDefined();
|
|
210
|
+
expect(relay).toBeDefined();
|
|
211
|
+
if (service === undefined || relay === undefined) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const rec = await service.record({ type: 't', payload: {} });
|
|
216
|
+
|
|
217
|
+
await relay.dispatchPendingBatch();
|
|
218
|
+
await relay.dispatchPendingBatch();
|
|
219
|
+
await relay.dispatchPendingBatch();
|
|
220
|
+
|
|
221
|
+
const internal = (store as unknown as { map: Map<string, OutboxRecord> }).map;
|
|
222
|
+
const final = internal.get(rec.id);
|
|
223
|
+
expect(final).toBeDefined();
|
|
224
|
+
expect(final!.status).toBe(OutboxStatus.DEAD);
|
|
225
|
+
expect(final!.attempts).toBe(3);
|
|
226
|
+
|
|
227
|
+
await moduleRef.close();
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe('AC8: relay.enabled=false — module built and timers advance, store.claimBatch is never invoked', () => {
|
|
232
|
+
it('AC8: with relay.enabled=false, timers advance but the relay does not invoke claimBatch', async () => {
|
|
233
|
+
const claimBatch = jest.fn().mockResolvedValue([] as OutboxRecord[]);
|
|
234
|
+
const store: IOutboxStore = {
|
|
235
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
236
|
+
claimBatch,
|
|
237
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
238
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
239
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
240
|
+
};
|
|
241
|
+
const publisher: IOutboxPublisher = { publish: jest.fn().mockResolvedValue(undefined) };
|
|
242
|
+
|
|
243
|
+
const { moduleRef, error } = await tryBuild(() =>
|
|
244
|
+
buildIntegrationModule({
|
|
245
|
+
store,
|
|
246
|
+
publisher,
|
|
247
|
+
relay: { ...defaultRelay, pollIntervalMs: 25 },
|
|
248
|
+
}),
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
expect(error).toBeUndefined();
|
|
252
|
+
expect(moduleRef).toBeDefined();
|
|
253
|
+
if (moduleRef === undefined) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
await moduleRef.init();
|
|
258
|
+
await new Promise((r) => setTimeout(r, 120));
|
|
259
|
+
await moduleRef.close();
|
|
260
|
+
|
|
261
|
+
const relay = safeGet<OutboxRelay>(moduleRef, OutboxRelay);
|
|
262
|
+
expect(relay).toBeDefined();
|
|
263
|
+
const internalInterval = (relay as unknown as { interval: ReturnType<typeof setInterval> | null } | undefined)
|
|
264
|
+
?.interval;
|
|
265
|
+
expect(internalInterval).toBeNull();
|
|
266
|
+
|
|
267
|
+
expect(claimBatch).not.toHaveBeenCalled();
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
describe('AC9: relay.enabled=true with no publisher configured — onApplicationBootstrap throws OutboxMisconfiguredException', () => {
|
|
272
|
+
it('AC9: bootstrapping a module with relay.enabled=true and no publisher throws OutboxMisconfiguredException and does not start the polling interval', async () => {
|
|
273
|
+
const store: IOutboxStore = {
|
|
274
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
275
|
+
claimBatch: jest.fn().mockResolvedValue([] as OutboxRecord[]),
|
|
276
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
277
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
278
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const { moduleRef, error: buildError } = await tryBuild(() =>
|
|
282
|
+
Test.createTestingModule({
|
|
283
|
+
imports: [
|
|
284
|
+
OutboxModule.register({
|
|
285
|
+
store,
|
|
286
|
+
publisher: undefined,
|
|
287
|
+
relay: { ...defaultRelay, enabled: true, pollIntervalMs: 50 },
|
|
288
|
+
}),
|
|
289
|
+
],
|
|
290
|
+
providers: [
|
|
291
|
+
{ provide: TRANSACTION_MANAGER, useValue: txManagerStub() },
|
|
292
|
+
{ provide: CLOCK, useValue: { now: jest.fn().mockReturnValue(new Date()) } },
|
|
293
|
+
{ provide: ID_GENERATOR, useValue: { generate: jest.fn().mockReturnValue('id-1') } },
|
|
294
|
+
],
|
|
295
|
+
}).compile(),
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
expect(buildError).toBeUndefined();
|
|
299
|
+
expect(moduleRef).toBeDefined();
|
|
300
|
+
if (moduleRef === undefined) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
let caught: unknown;
|
|
305
|
+
try {
|
|
306
|
+
await moduleRef.init();
|
|
307
|
+
} catch (e) {
|
|
308
|
+
caught = e;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
expect(caught).toBeInstanceOf(OutboxMisconfiguredException);
|
|
312
|
+
expect((caught as OutboxMisconfiguredException).httpStatus).toBe(500);
|
|
313
|
+
|
|
314
|
+
// Wait long enough for any accidentally-started interval (pollIntervalMs: 50) to fire
|
|
315
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
316
|
+
|
|
317
|
+
// AC9 requires the interval is never started before throwing
|
|
318
|
+
const relay = safeGet<OutboxRelay>(moduleRef, OutboxRelay);
|
|
319
|
+
expect(relay).toBeDefined();
|
|
320
|
+
const internalInterval = (relay as unknown as { interval: ReturnType<typeof setInterval> | null } | undefined)
|
|
321
|
+
?.interval;
|
|
322
|
+
expect(internalInterval).toBeNull();
|
|
323
|
+
expect(store.claimBatch).not.toHaveBeenCalled();
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
describe('AC10: interval-driven cycle with claimBatch rejection — relay logs the error and remains running for later polls', () => {
|
|
328
|
+
it('AC10: when claimBatch rejects at a poll tick, the relay logs the cycle error and the application stays running so a later poll can be attempted', async () => {
|
|
329
|
+
const claimBatch = jest
|
|
330
|
+
.fn<Promise<OutboxRecord[]>, [number, number, Date, string]>()
|
|
331
|
+
.mockRejectedValueOnce(new Error('store temporarily unavailable'))
|
|
332
|
+
.mockResolvedValueOnce([] as OutboxRecord[]);
|
|
333
|
+
const store: IOutboxStore = {
|
|
334
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
335
|
+
claimBatch,
|
|
336
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
337
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
338
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
339
|
+
};
|
|
340
|
+
const publisher: IOutboxPublisher = { publish: jest.fn().mockResolvedValue(undefined) };
|
|
341
|
+
|
|
342
|
+
const { moduleRef, error } = await tryBuild(() =>
|
|
343
|
+
buildIntegrationModule({
|
|
344
|
+
store,
|
|
345
|
+
publisher,
|
|
346
|
+
relay: { ...defaultRelay, enabled: true, pollIntervalMs: 75 },
|
|
347
|
+
}),
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
expect(error).toBeUndefined();
|
|
351
|
+
expect(moduleRef).toBeDefined();
|
|
352
|
+
if (moduleRef === undefined) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const loggerErrorSpy = jest.spyOn(Logger.prototype, 'error');
|
|
357
|
+
|
|
358
|
+
await moduleRef.init();
|
|
359
|
+
await new Promise((r) => setTimeout(r, 175));
|
|
360
|
+
await moduleRef.close();
|
|
361
|
+
|
|
362
|
+
expect(claimBatch).toHaveBeenCalledTimes(2);
|
|
363
|
+
expect(loggerErrorSpy).toHaveBeenCalledWith(
|
|
364
|
+
expect.stringContaining('store temporarily unavailable'),
|
|
365
|
+
expect.anything(),
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
loggerErrorSpy.mockRestore();
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AppException } from '@nathapp/nestjs-common';
|
|
2
|
+
import { OutboxExceptionCode } from '../../lib/exception/outbox-exception-code.enum';
|
|
3
|
+
import { OutboxInvalidEventException } from '../../lib/exception/outbox-invalid-event.exception';
|
|
4
|
+
import { OutboxMisconfiguredException } from '../../lib/exception/outbox-misconfigured.exception';
|
|
5
|
+
|
|
6
|
+
describe('OutboxInvalidEventException (AC #8)', () => {
|
|
7
|
+
it('is an instance of AppException', () => {
|
|
8
|
+
const e = new OutboxInvalidEventException();
|
|
9
|
+
expect(e).toBeInstanceOf(AppException);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('has httpStatus 400', () => {
|
|
13
|
+
const e = new OutboxInvalidEventException();
|
|
14
|
+
expect(e.httpStatus).toBe(400);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('has code equal to OutboxExceptionCode.INVALID_EVENT_TYPE', () => {
|
|
18
|
+
const e = new OutboxInvalidEventException();
|
|
19
|
+
expect(e.code).toBe(OutboxExceptionCode.INVALID_EVENT_TYPE);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('OutboxMisconfiguredException (AC #9)', () => {
|
|
24
|
+
it('is an instance of AppException', () => {
|
|
25
|
+
const e = new OutboxMisconfiguredException();
|
|
26
|
+
expect(e).toBeInstanceOf(AppException);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('has httpStatus 500', () => {
|
|
30
|
+
const e = new OutboxMisconfiguredException();
|
|
31
|
+
expect(e.httpStatus).toBe(500);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('has code equal to OutboxExceptionCode.PUBLISHER_NOT_CONFIGURED', () => {
|
|
35
|
+
const e = new OutboxMisconfiguredException();
|
|
36
|
+
expect(e.code).toBe(OutboxExceptionCode.PUBLISHER_NOT_CONFIGURED);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { OutboxStatus } from '../../lib/interface/outbox-status.enum';
|
|
2
|
+
|
|
3
|
+
describe('OutboxStatus enum (AC #1)', () => {
|
|
4
|
+
it('PENDING equals "pending"', () => {
|
|
5
|
+
expect(OutboxStatus.PENDING).toBe('pending');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('PROCESSING equals "processing"', () => {
|
|
9
|
+
expect(OutboxStatus.PROCESSING).toBe('processing');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('PUBLISHED equals "published"', () => {
|
|
13
|
+
expect(OutboxStatus.PUBLISHED).toBe('published');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('DEAD equals "dead"', () => {
|
|
17
|
+
expect(OutboxStatus.DEAD).toBe('dead');
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Setup file for the nax acceptance test suite.
|
|
2
|
+
// Patches missing @nestjs APP_* token re-exports and Node crypto onto globalThis.
|
|
3
|
+
// No supertest wrapper: @nathapp/nestjs-outbox has no HTTP surface.
|
|
4
|
+
const nestCommon = require('@nestjs/common');
|
|
5
|
+
const coreConstants = require('@nestjs/core/constants');
|
|
6
|
+
const appTokens = {
|
|
7
|
+
APP_INTERCEPTOR: coreConstants.APP_INTERCEPTOR,
|
|
8
|
+
APP_FILTER: coreConstants.APP_FILTER,
|
|
9
|
+
APP_GUARD: coreConstants.APP_GUARD,
|
|
10
|
+
APP_PIPE: coreConstants.APP_PIPE,
|
|
11
|
+
};
|
|
12
|
+
for (const [key, value] of Object.entries(appTokens)) {
|
|
13
|
+
if (!(key in nestCommon)) {
|
|
14
|
+
Object.defineProperty(nestCommon, key, {
|
|
15
|
+
value,
|
|
16
|
+
writable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
enumerable: true,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const nodeCrypto = require('crypto');
|
|
24
|
+
if (typeof globalThis.crypto !== 'undefined' && typeof globalThis.crypto.createHash !== 'function') {
|
|
25
|
+
Object.defineProperty(globalThis.crypto, 'createHash', {
|
|
26
|
+
value: nodeCrypto.createHash.bind(nodeCrypto),
|
|
27
|
+
writable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
enumerable: false,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"moduleFileExtensions": ["js", "json", "ts", "d.ts"],
|
|
3
|
+
"rootDir": "../",
|
|
4
|
+
"testEnvironment": "node",
|
|
5
|
+
"testMatch": ["**/.nax/**/*.test.ts"],
|
|
6
|
+
"setupFiles": ["<rootDir>/test/jest-nax-setup.js"],
|
|
7
|
+
"transform": {
|
|
8
|
+
"^.+\\.(t)s$": [
|
|
9
|
+
"ts-jest",
|
|
10
|
+
{
|
|
11
|
+
"diagnostics": {
|
|
12
|
+
"ignoreCodes": [2305, 2322, 2339, 2345]
|
|
13
|
+
},
|
|
14
|
+
"tsconfig": {
|
|
15
|
+
"esModuleInterop": true,
|
|
16
|
+
"emitDecoratorMetadata": true,
|
|
17
|
+
"experimentalDecorators": true
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
"collectCoverageFrom": [
|
|
23
|
+
"lib/**/*.ts",
|
|
24
|
+
"!lib/**/*.spec.ts",
|
|
25
|
+
"!lib/**/*.d.ts",
|
|
26
|
+
"!**/node_modules/**"
|
|
27
|
+
],
|
|
28
|
+
"forceExit": true,
|
|
29
|
+
"maxWorkers": 1
|
|
30
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"moduleFileExtensions": [
|
|
3
|
+
"js",
|
|
4
|
+
"json",
|
|
5
|
+
"ts",
|
|
6
|
+
"d.ts"
|
|
7
|
+
],
|
|
8
|
+
"rootDir": "../",
|
|
9
|
+
"testEnvironment": "node",
|
|
10
|
+
"testRegex": ".spec.ts$",
|
|
11
|
+
"transform": {
|
|
12
|
+
"^.+\\.(t)s$": [
|
|
13
|
+
"ts-jest",
|
|
14
|
+
{
|
|
15
|
+
"diagnostics": true
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"collectCoverageFrom": [
|
|
20
|
+
"lib/**/*.ts",
|
|
21
|
+
"!lib/**/*.spec.ts",
|
|
22
|
+
"!lib/**/*.d.ts",
|
|
23
|
+
"!lib/**/index.ts",
|
|
24
|
+
"!lib/**/*.interface.ts",
|
|
25
|
+
"!**/node_modules/**"
|
|
26
|
+
],
|
|
27
|
+
"coverageProvider": "v8"
|
|
28
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Test } from '@nestjs/testing';
|
|
2
|
+
import { OutboxModule } from '../../lib/module/outbox.module';
|
|
3
|
+
import { OutboxService } from '../../lib/provider/outbox.service';
|
|
4
|
+
import { OutboxRelay } from '../../lib/provider/outbox-relay';
|
|
5
|
+
import {
|
|
6
|
+
OUTBOX_MODULE_OPTIONS,
|
|
7
|
+
OUTBOX_STORE,
|
|
8
|
+
} from '../../lib/constant/outbox.constants';
|
|
9
|
+
import { InMemoryOutboxStore } from '../../lib/provider/in-memory-outbox-store';
|
|
10
|
+
import { IOutboxStore } from '../../lib/interface/outbox-store.interface';
|
|
11
|
+
import { IOutboxPublisher } from '../../lib/interface/outbox-publisher.interface';
|
|
12
|
+
|
|
13
|
+
const stubPublisher = (): IOutboxPublisher => ({
|
|
14
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const safeGet = <T>(moduleRef: { get: (token: unknown) => T }, token: unknown): T | undefined => {
|
|
18
|
+
try {
|
|
19
|
+
return moduleRef.get(token);
|
|
20
|
+
} catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
describe('OutboxModule — register / registerAsync', () => {
|
|
26
|
+
describe('AC1: register() returns a global dynamic module keyed to OutboxModule', () => {
|
|
27
|
+
it('AC1: register({ publisher }) returns a DynamicModule with global === true and module === OutboxModule', () => {
|
|
28
|
+
const publisher = stubPublisher();
|
|
29
|
+
const dynamicModule = OutboxModule.register({ publisher }) as {
|
|
30
|
+
global?: boolean;
|
|
31
|
+
module?: unknown;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
expect(dynamicModule).toBeDefined();
|
|
35
|
+
expect(dynamicModule.global).toBe(true);
|
|
36
|
+
expect(dynamicModule.module).toBe(OutboxModule);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('AC1 (boundary): register() with no arguments also returns global === true and module === OutboxModule', () => {
|
|
40
|
+
const dynamicModule = OutboxModule.register() as {
|
|
41
|
+
global?: boolean;
|
|
42
|
+
module?: unknown;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
expect(dynamicModule.global).toBe(true);
|
|
46
|
+
expect(dynamicModule.module).toBe(OutboxModule);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('AC2: register({ publisher }) resolves OUTBOX_STORE to an InMemoryOutboxStore', () => {
|
|
51
|
+
it('AC2: a testing module built from register({ publisher }) resolves OUTBOX_STORE to InMemoryOutboxStore', async () => {
|
|
52
|
+
const publisher = stubPublisher();
|
|
53
|
+
|
|
54
|
+
const moduleRef = await Test.createTestingModule({
|
|
55
|
+
imports: [OutboxModule.register({ publisher })],
|
|
56
|
+
}).compile();
|
|
57
|
+
|
|
58
|
+
const store = safeGet<IOutboxStore>(moduleRef, OUTBOX_STORE);
|
|
59
|
+
expect(store).toBeDefined();
|
|
60
|
+
expect(store).toBeInstanceOf(InMemoryOutboxStore);
|
|
61
|
+
|
|
62
|
+
await moduleRef.close();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('AC3: register({ store, publisher }) resolves OUTBOX_STORE to the custom instance (reference-equal)', () => {
|
|
67
|
+
it('AC3: a custom store passed via register({ store, publisher }) is exposed under OUTBOX_STORE reference-equal', async () => {
|
|
68
|
+
const customStore: IOutboxStore = {
|
|
69
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
70
|
+
claimBatch: jest.fn().mockResolvedValue([]),
|
|
71
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
72
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
73
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
74
|
+
};
|
|
75
|
+
const publisher = stubPublisher();
|
|
76
|
+
|
|
77
|
+
const moduleRef = await Test.createTestingModule({
|
|
78
|
+
imports: [OutboxModule.register({ store: customStore, publisher })],
|
|
79
|
+
}).compile();
|
|
80
|
+
|
|
81
|
+
const resolved = safeGet<IOutboxStore>(moduleRef, OUTBOX_STORE);
|
|
82
|
+
expect(resolved).toBe(customStore);
|
|
83
|
+
|
|
84
|
+
await moduleRef.close();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('Module exports: register() and registerAsync() expose OutboxService and OutboxRelay so importing modules can inject them', () => {
|
|
89
|
+
it('register({ publisher }) returns a DynamicModule whose exports include OutboxService and OutboxRelay', () => {
|
|
90
|
+
const publisher = stubPublisher();
|
|
91
|
+
const dynamicModule = OutboxModule.register({ publisher }) as {
|
|
92
|
+
exports?: unknown[];
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const exports = dynamicModule.exports ?? [];
|
|
96
|
+
expect(exports).toContain(OutboxService);
|
|
97
|
+
expect(exports).toContain(OutboxRelay);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('registerAsync({ useFactory }) returns a DynamicModule whose exports include OutboxService and OutboxRelay', () => {
|
|
101
|
+
const publisher = stubPublisher();
|
|
102
|
+
const dynamicModule = OutboxModule.registerAsync({
|
|
103
|
+
inject: [],
|
|
104
|
+
useFactory: () => ({ publisher }),
|
|
105
|
+
}) as { exports?: unknown[] };
|
|
106
|
+
|
|
107
|
+
const exports = dynamicModule.exports ?? [];
|
|
108
|
+
expect(exports).toContain(OutboxService);
|
|
109
|
+
expect(exports).toContain(OutboxRelay);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('AC4: registerAsync({ useFactory }) builds and the resolved module options expose factory-supplied values', () => {
|
|
114
|
+
it('AC4: registerAsync with useFactory returning { publisher, relay: { pollIntervalMs: 111 } } builds and OUTBOX_MODULE_OPTIONS.relay.pollIntervalMs === 111', async () => {
|
|
115
|
+
const publisher = stubPublisher();
|
|
116
|
+
|
|
117
|
+
const moduleRef = await Test.createTestingModule({
|
|
118
|
+
imports: [
|
|
119
|
+
OutboxModule.registerAsync({
|
|
120
|
+
inject: [],
|
|
121
|
+
useFactory: () => ({
|
|
122
|
+
publisher,
|
|
123
|
+
relay: { pollIntervalMs: 111 },
|
|
124
|
+
}),
|
|
125
|
+
}),
|
|
126
|
+
],
|
|
127
|
+
}).compile();
|
|
128
|
+
|
|
129
|
+
const resolved = safeGet<{ relay: { pollIntervalMs?: number } }>(
|
|
130
|
+
moduleRef,
|
|
131
|
+
OUTBOX_MODULE_OPTIONS,
|
|
132
|
+
);
|
|
133
|
+
expect(resolved).toBeDefined();
|
|
134
|
+
expect(resolved!.relay.pollIntervalMs).toBe(111);
|
|
135
|
+
|
|
136
|
+
await moduleRef.close();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|