@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,565 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CLOCK,
|
|
3
|
+
IClock,
|
|
4
|
+
ID_GENERATOR,
|
|
5
|
+
IIdGenerator,
|
|
6
|
+
} from '@nathapp/nestjs-common';
|
|
7
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
8
|
+
import { OutboxRelay } from '../../lib/provider/outbox-relay';
|
|
9
|
+
import { InMemoryOutboxStore } from '../../lib/provider/in-memory-outbox-store';
|
|
10
|
+
import {
|
|
11
|
+
OUTBOX_STORE,
|
|
12
|
+
OUTBOX_PUBLISHER,
|
|
13
|
+
OUTBOX_MODULE_OPTIONS,
|
|
14
|
+
} from '../../lib/constant/outbox.constants';
|
|
15
|
+
import { OutboxRecord } from '../../lib/interface/outbox-record.interface';
|
|
16
|
+
import { OutboxStatus } from '../../lib/interface/outbox-status.enum';
|
|
17
|
+
import { IOutboxStore } from '../../lib/interface/outbox-store.interface';
|
|
18
|
+
import { IOutboxPublisher } from '../../lib/interface/outbox-publisher.interface';
|
|
19
|
+
import { ResolvedOutboxOptions } from '../../lib/provider/resolve-outbox-options';
|
|
20
|
+
import { OutboxMisconfiguredException } from '../../lib/exception/outbox-misconfigured.exception';
|
|
21
|
+
|
|
22
|
+
const makeRecord = (overrides: Partial<OutboxRecord> = {}): OutboxRecord => ({
|
|
23
|
+
id: 'rec-1',
|
|
24
|
+
type: 'order.created',
|
|
25
|
+
payload: { orderId: 'o-1' },
|
|
26
|
+
status: OutboxStatus.PENDING,
|
|
27
|
+
attempts: 0,
|
|
28
|
+
createdAt: new Date('2026-01-01T00:00:00.000Z'),
|
|
29
|
+
nextAttemptAt: new Date('2026-01-01T00:00:00.000Z'),
|
|
30
|
+
...overrides,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
interface Harness {
|
|
34
|
+
relay: OutboxRelay;
|
|
35
|
+
store: jest.Mocked<IOutboxStore>;
|
|
36
|
+
publisher: IOutboxPublisher;
|
|
37
|
+
clock: IClock;
|
|
38
|
+
idGenerator: IIdGenerator;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const buildRelay = async (options: {
|
|
42
|
+
providePublisher?: boolean;
|
|
43
|
+
resolvedOptions?: Partial<ResolvedOutboxOptions>;
|
|
44
|
+
storeMock?: jest.Mocked<IOutboxStore>;
|
|
45
|
+
publisherMock?: IOutboxPublisher;
|
|
46
|
+
} = {}): Promise<Harness> => {
|
|
47
|
+
const storeMock: jest.Mocked<IOutboxStore> =
|
|
48
|
+
options.storeMock ??
|
|
49
|
+
({
|
|
50
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
51
|
+
claimBatch: jest.fn().mockResolvedValue([]),
|
|
52
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
53
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
54
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
55
|
+
} as unknown as jest.Mocked<IOutboxStore>);
|
|
56
|
+
|
|
57
|
+
const fixedNow = new Date('2026-05-01T00:00:00.000Z');
|
|
58
|
+
const clock: IClock = { now: jest.fn().mockReturnValue(fixedNow) };
|
|
59
|
+
const idGenerator: IIdGenerator = { generate: jest.fn().mockReturnValue('owner-xyz') };
|
|
60
|
+
|
|
61
|
+
const defaultResolved: ResolvedOutboxOptions = {
|
|
62
|
+
store: storeMock,
|
|
63
|
+
publisher: options.providePublisher
|
|
64
|
+
? (options.publisherMock ?? {
|
|
65
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
66
|
+
})
|
|
67
|
+
: undefined,
|
|
68
|
+
relay: {
|
|
69
|
+
enabled: false,
|
|
70
|
+
pollIntervalMs: 5000,
|
|
71
|
+
batchSize: 10,
|
|
72
|
+
leaseMs: 30000,
|
|
73
|
+
maxAttempts: 10,
|
|
74
|
+
backoffBaseMs: 1000,
|
|
75
|
+
backoffCapMs: 300000,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const resolved: ResolvedOutboxOptions = {
|
|
80
|
+
...defaultResolved,
|
|
81
|
+
...options.resolvedOptions,
|
|
82
|
+
relay: {
|
|
83
|
+
...defaultResolved.relay,
|
|
84
|
+
...(options.resolvedOptions?.relay ?? {}),
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const providers: Array<unknown> = [
|
|
89
|
+
OutboxRelay,
|
|
90
|
+
{ provide: OUTBOX_STORE, useValue: storeMock },
|
|
91
|
+
{ provide: CLOCK, useValue: clock },
|
|
92
|
+
{ provide: ID_GENERATOR, useValue: idGenerator },
|
|
93
|
+
{ provide: OUTBOX_MODULE_OPTIONS, useValue: resolved },
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
if (options.providePublisher) {
|
|
97
|
+
providers.push({
|
|
98
|
+
provide: OUTBOX_PUBLISHER,
|
|
99
|
+
useValue: resolved.publisher,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const moduleRef: TestingModule = await Test.createTestingModule({
|
|
104
|
+
providers: providers as never[],
|
|
105
|
+
}).compile();
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
relay: moduleRef.get(OutboxRelay),
|
|
109
|
+
store: storeMock,
|
|
110
|
+
publisher: resolved.publisher as IOutboxPublisher,
|
|
111
|
+
clock,
|
|
112
|
+
idGenerator,
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
describe('OutboxRelay.dispatchPendingBatch', () => {
|
|
117
|
+
describe('AC1: no publisher configured rejects with OutboxMisconfiguredException (500) and claimBatch is never called', () => {
|
|
118
|
+
it('throws OutboxMisconfiguredException with httpStatus 500 and never calls claimBatch', async () => {
|
|
119
|
+
const { relay, store } = await buildRelay({ providePublisher: false });
|
|
120
|
+
|
|
121
|
+
let caught: unknown;
|
|
122
|
+
try {
|
|
123
|
+
await relay.dispatchPendingBatch();
|
|
124
|
+
} catch (e) {
|
|
125
|
+
caught = e;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
expect(caught).toBeInstanceOf(OutboxMisconfiguredException);
|
|
129
|
+
expect((caught as OutboxMisconfiguredException).httpStatus).toBe(500);
|
|
130
|
+
expect(store.claimBatch).not.toHaveBeenCalled();
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('AC2: empty claimBatch — claimBatch called once with correct args, publish/markPublished never called', () => {
|
|
135
|
+
it('calls claimBatch once with batchSize, leaseMs, a Date, and owner; neither markPublished nor publish called', async () => {
|
|
136
|
+
const fixedNow = new Date('2026-05-01T00:00:00.000Z');
|
|
137
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
138
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
139
|
+
claimBatch: jest.fn().mockResolvedValue([]),
|
|
140
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
141
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
142
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
143
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
144
|
+
const publisher: IOutboxPublisher = {
|
|
145
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const { relay } = await buildRelay({
|
|
149
|
+
providePublisher: true,
|
|
150
|
+
storeMock,
|
|
151
|
+
publisherMock: publisher,
|
|
152
|
+
resolvedOptions: {
|
|
153
|
+
store: storeMock,
|
|
154
|
+
publisher,
|
|
155
|
+
relay: {
|
|
156
|
+
enabled: false,
|
|
157
|
+
pollIntervalMs: 5000,
|
|
158
|
+
batchSize: 7,
|
|
159
|
+
leaseMs: 12000,
|
|
160
|
+
maxAttempts: 10,
|
|
161
|
+
backoffBaseMs: 1000,
|
|
162
|
+
backoffCapMs: 300000,
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
await relay.dispatchPendingBatch();
|
|
168
|
+
|
|
169
|
+
expect(storeMock.claimBatch).toHaveBeenCalledTimes(1);
|
|
170
|
+
const [batchSize, leaseMs, nowArg, owner] =
|
|
171
|
+
storeMock.claimBatch.mock.calls[0];
|
|
172
|
+
expect(batchSize).toBe(7);
|
|
173
|
+
expect(leaseMs).toBe(12000);
|
|
174
|
+
expect(nowArg).toBeInstanceOf(Date);
|
|
175
|
+
expect((nowArg as Date).getTime()).toBe(fixedNow.getTime());
|
|
176
|
+
expect(typeof owner).toBe('string');
|
|
177
|
+
expect((publisher.publish as jest.Mock)).not.toHaveBeenCalled();
|
|
178
|
+
expect(storeMock.markPublished).not.toHaveBeenCalled();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('AC3: success path — publish called once with record, markPublished called once with record.id, no markRetry/markDead', () => {
|
|
183
|
+
it('publishes the record and marks it PUBLISHED', async () => {
|
|
184
|
+
const record = makeRecord({ id: 'r-success' });
|
|
185
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
186
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
187
|
+
claimBatch: jest.fn().mockResolvedValue([record]),
|
|
188
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
189
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
190
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
191
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
192
|
+
const publisher: IOutboxPublisher = {
|
|
193
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const { relay } = await buildRelay({
|
|
197
|
+
providePublisher: true,
|
|
198
|
+
storeMock,
|
|
199
|
+
publisherMock: publisher,
|
|
200
|
+
resolvedOptions: {
|
|
201
|
+
store: storeMock,
|
|
202
|
+
publisher,
|
|
203
|
+
relay: {
|
|
204
|
+
enabled: false,
|
|
205
|
+
pollIntervalMs: 5000,
|
|
206
|
+
batchSize: 10,
|
|
207
|
+
leaseMs: 30000,
|
|
208
|
+
maxAttempts: 10,
|
|
209
|
+
backoffBaseMs: 1000,
|
|
210
|
+
backoffCapMs: 300000,
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
await relay.dispatchPendingBatch();
|
|
216
|
+
|
|
217
|
+
expect((publisher.publish as jest.Mock)).toHaveBeenCalledTimes(1);
|
|
218
|
+
expect((publisher.publish as jest.Mock).mock.calls[0][0]).toBe(record);
|
|
219
|
+
expect(storeMock.markPublished).toHaveBeenCalledTimes(1);
|
|
220
|
+
expect(storeMock.markPublished.mock.calls[0][0]).toBe('r-success');
|
|
221
|
+
expect(storeMock.markRetry).not.toHaveBeenCalled();
|
|
222
|
+
expect(storeMock.markDead).not.toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe('AC4: publish rejects with attempts=0, maxAttempts=10 → markRetry with attempts=1 and correct nextAttemptAt', () => {
|
|
227
|
+
it('schedules a retry at now + computeBackoff(1, baseMs, capMs)', async () => {
|
|
228
|
+
const fixedNow = new Date('2026-05-01T00:00:00.000Z');
|
|
229
|
+
const backoffBaseMs = 1000;
|
|
230
|
+
const backoffCapMs = 300000;
|
|
231
|
+
const record = makeRecord({ id: 'r-retry', attempts: 0 });
|
|
232
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
233
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
234
|
+
claimBatch: jest.fn().mockResolvedValue([record]),
|
|
235
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
236
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
237
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
238
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
239
|
+
const publisher: IOutboxPublisher = {
|
|
240
|
+
publish: jest.fn().mockRejectedValue(new Error('publish boom')),
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const { relay } = await buildRelay({
|
|
244
|
+
providePublisher: true,
|
|
245
|
+
storeMock,
|
|
246
|
+
publisherMock: publisher,
|
|
247
|
+
resolvedOptions: {
|
|
248
|
+
store: storeMock,
|
|
249
|
+
publisher,
|
|
250
|
+
relay: {
|
|
251
|
+
enabled: false,
|
|
252
|
+
pollIntervalMs: 5000,
|
|
253
|
+
batchSize: 10,
|
|
254
|
+
leaseMs: 30000,
|
|
255
|
+
maxAttempts: 10,
|
|
256
|
+
backoffBaseMs,
|
|
257
|
+
backoffCapMs,
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
await relay.dispatchPendingBatch();
|
|
263
|
+
|
|
264
|
+
expect(storeMock.markRetry).toHaveBeenCalledTimes(1);
|
|
265
|
+
const [id, attempts, nextAttemptAt] =
|
|
266
|
+
storeMock.markRetry.mock.calls[0];
|
|
267
|
+
expect(id).toBe('r-retry');
|
|
268
|
+
expect(attempts).toBe(1);
|
|
269
|
+
const expectedBackoff = backoffBaseMs * 2 ** (1 - 1);
|
|
270
|
+
expect((nextAttemptAt as Date).getTime()).toBe(
|
|
271
|
+
fixedNow.getTime() + Math.min(backoffCapMs, expectedBackoff),
|
|
272
|
+
);
|
|
273
|
+
expect(storeMock.markDead).not.toHaveBeenCalled();
|
|
274
|
+
expect(storeMock.markPublished).not.toHaveBeenCalled();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
describe('AC5: publish rejects with attempts=9, maxAttempts=10 → markDead with attempts=10, no markRetry/markPublished', () => {
|
|
279
|
+
it('marks the record DEAD after exhausting maxAttempts', async () => {
|
|
280
|
+
const record = makeRecord({ id: 'r-dead', attempts: 9 });
|
|
281
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
282
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
283
|
+
claimBatch: jest.fn().mockResolvedValue([record]),
|
|
284
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
285
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
286
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
287
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
288
|
+
const publisher: IOutboxPublisher = {
|
|
289
|
+
publish: jest.fn().mockRejectedValue(new Error('publish boom')),
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const { relay } = await buildRelay({
|
|
293
|
+
providePublisher: true,
|
|
294
|
+
storeMock,
|
|
295
|
+
publisherMock: publisher,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
await relay.dispatchPendingBatch();
|
|
299
|
+
|
|
300
|
+
expect(storeMock.markDead).toHaveBeenCalledTimes(1);
|
|
301
|
+
const [id, attempts] = storeMock.markDead.mock.calls[0];
|
|
302
|
+
expect(id).toBe('r-dead');
|
|
303
|
+
expect(attempts).toBe(10);
|
|
304
|
+
expect(storeMock.markRetry).not.toHaveBeenCalled();
|
|
305
|
+
expect(storeMock.markPublished).not.toHaveBeenCalled();
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
describe('AC6: per-record isolation — first record fails, second succeeds; markPublished called for second.id', () => {
|
|
310
|
+
it('continues after a failure and publishes the next record', async () => {
|
|
311
|
+
const first = makeRecord({ id: 'r-fail', attempts: 0 });
|
|
312
|
+
const second = makeRecord({ id: 'r-ok', attempts: 0 });
|
|
313
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
314
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
315
|
+
claimBatch: jest.fn().mockResolvedValue([first, second]),
|
|
316
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
317
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
318
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
319
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
320
|
+
const publishMock = jest
|
|
321
|
+
.fn<Promise<void>, [OutboxRecord]>()
|
|
322
|
+
.mockRejectedValueOnce(new Error('first failed'))
|
|
323
|
+
.mockResolvedValueOnce(undefined);
|
|
324
|
+
const publisher: IOutboxPublisher = { publish: publishMock };
|
|
325
|
+
|
|
326
|
+
const { relay } = await buildRelay({
|
|
327
|
+
providePublisher: true,
|
|
328
|
+
storeMock,
|
|
329
|
+
publisherMock: publisher,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
await relay.dispatchPendingBatch();
|
|
333
|
+
|
|
334
|
+
expect(publishMock).toHaveBeenCalledTimes(2);
|
|
335
|
+
expect(storeMock.markPublished).toHaveBeenCalledTimes(1);
|
|
336
|
+
expect(storeMock.markPublished.mock.calls[0][0]).toBe('r-ok');
|
|
337
|
+
expect(storeMock.markRetry).toHaveBeenCalledTimes(1);
|
|
338
|
+
expect(storeMock.markRetry.mock.calls[0][0]).toBe('r-fail');
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe('per-record isolation when store mutations fail', () => {
|
|
343
|
+
it('continues to next record when markRetry throws (publisher failed for first record)', async () => {
|
|
344
|
+
const first = makeRecord({ id: 'r-retry-fail', attempts: 0 });
|
|
345
|
+
const second = makeRecord({ id: 'r-ok', attempts: 0 });
|
|
346
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
347
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
348
|
+
claimBatch: jest.fn().mockResolvedValue([first, second]),
|
|
349
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
350
|
+
markRetry: jest
|
|
351
|
+
.fn()
|
|
352
|
+
.mockRejectedValueOnce(new Error('store down'))
|
|
353
|
+
.mockResolvedValueOnce(undefined),
|
|
354
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
355
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
356
|
+
const publishMock = jest
|
|
357
|
+
.fn<Promise<void>, [OutboxRecord]>()
|
|
358
|
+
.mockRejectedValueOnce(new Error('first failed'))
|
|
359
|
+
.mockResolvedValueOnce(undefined);
|
|
360
|
+
const publisher: IOutboxPublisher = { publish: publishMock };
|
|
361
|
+
|
|
362
|
+
const { relay } = await buildRelay({
|
|
363
|
+
providePublisher: true,
|
|
364
|
+
storeMock,
|
|
365
|
+
publisherMock: publisher,
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
await expect(relay.dispatchPendingBatch()).resolves.toBeUndefined();
|
|
369
|
+
|
|
370
|
+
expect(publishMock).toHaveBeenCalledTimes(2);
|
|
371
|
+
expect(storeMock.markPublished).toHaveBeenCalledTimes(1);
|
|
372
|
+
expect(storeMock.markPublished.mock.calls[0][0]).toBe('r-ok');
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('continues to next record when markDead throws (publisher failed and maxAttempts exhausted)', async () => {
|
|
376
|
+
const first = makeRecord({ id: 'r-dead-fail', attempts: 9 });
|
|
377
|
+
const second = makeRecord({ id: 'r-ok', attempts: 0 });
|
|
378
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
379
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
380
|
+
claimBatch: jest.fn().mockResolvedValue([first, second]),
|
|
381
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
382
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
383
|
+
markDead: jest
|
|
384
|
+
.fn()
|
|
385
|
+
.mockRejectedValueOnce(new Error('store down'))
|
|
386
|
+
.mockResolvedValue(undefined),
|
|
387
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
388
|
+
const publishMock = jest
|
|
389
|
+
.fn<Promise<void>, [OutboxRecord]>()
|
|
390
|
+
.mockRejectedValueOnce(new Error('first failed'))
|
|
391
|
+
.mockResolvedValueOnce(undefined);
|
|
392
|
+
const publisher: IOutboxPublisher = { publish: publishMock };
|
|
393
|
+
|
|
394
|
+
const { relay } = await buildRelay({
|
|
395
|
+
providePublisher: true,
|
|
396
|
+
storeMock,
|
|
397
|
+
publisherMock: publisher,
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
await expect(relay.dispatchPendingBatch()).resolves.toBeUndefined();
|
|
401
|
+
|
|
402
|
+
expect(publishMock).toHaveBeenCalledTimes(2);
|
|
403
|
+
expect(storeMock.markPublished).toHaveBeenCalledTimes(1);
|
|
404
|
+
expect(storeMock.markPublished.mock.calls[0][0]).toBe('r-ok');
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('continues to the next record when markPublished throws after a successful publish (per-record isolation)', async () => {
|
|
408
|
+
const first = makeRecord({ id: 'r-pub-fail', attempts: 0 });
|
|
409
|
+
const second = makeRecord({ id: 'r-next', attempts: 0 });
|
|
410
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
411
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
412
|
+
claimBatch: jest.fn().mockResolvedValue([first, second]),
|
|
413
|
+
markPublished: jest
|
|
414
|
+
.fn()
|
|
415
|
+
.mockRejectedValueOnce(new Error('store down'))
|
|
416
|
+
.mockResolvedValueOnce(undefined),
|
|
417
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
418
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
419
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
420
|
+
const publishMock = jest
|
|
421
|
+
.fn<Promise<void>, [OutboxRecord]>()
|
|
422
|
+
.mockResolvedValue(undefined);
|
|
423
|
+
const publisher: IOutboxPublisher = { publish: publishMock };
|
|
424
|
+
|
|
425
|
+
const { relay } = await buildRelay({
|
|
426
|
+
providePublisher: true,
|
|
427
|
+
storeMock,
|
|
428
|
+
publisherMock: publisher,
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
await expect(relay.dispatchPendingBatch()).resolves.toBeUndefined();
|
|
432
|
+
|
|
433
|
+
expect(publishMock).toHaveBeenCalledTimes(2);
|
|
434
|
+
expect(storeMock.markPublished).toHaveBeenCalledTimes(2);
|
|
435
|
+
expect(storeMock.markPublished.mock.calls[1][0]).toBe('r-next');
|
|
436
|
+
expect(storeMock.markRetry).not.toHaveBeenCalled();
|
|
437
|
+
expect(storeMock.markDead).not.toHaveBeenCalled();
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
describe('AC7: single-flight — second dispatchPendingBatch while first is in-flight does not call claimBatch again', () => {
|
|
442
|
+
it('second call resolves immediately and claimBatch is invoked once', async () => {
|
|
443
|
+
const record = makeRecord({ id: 'r-flight' });
|
|
444
|
+
let resolvePublish!: () => void;
|
|
445
|
+
const publishPromise = new Promise<void>((resolve) => {
|
|
446
|
+
resolvePublish = resolve;
|
|
447
|
+
});
|
|
448
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
449
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
450
|
+
claimBatch: jest.fn().mockResolvedValue([record]),
|
|
451
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
452
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
453
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
454
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
455
|
+
const publisher: IOutboxPublisher = {
|
|
456
|
+
publish: jest.fn().mockReturnValue(publishPromise),
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const { relay } = await buildRelay({
|
|
460
|
+
providePublisher: true,
|
|
461
|
+
storeMock,
|
|
462
|
+
publisherMock: publisher,
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
const first = relay.dispatchPendingBatch();
|
|
466
|
+
await relay.dispatchPendingBatch();
|
|
467
|
+
expect(storeMock.claimBatch).toHaveBeenCalledTimes(1);
|
|
468
|
+
resolvePublish();
|
|
469
|
+
await first;
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
describe('AC8: empty batch — publish, markPublished, markRetry, markDead all never called', () => {
|
|
474
|
+
it('resolves without invoking publish, markPublished, markRetry, or markDead', async () => {
|
|
475
|
+
const storeMock: jest.Mocked<IOutboxStore> = {
|
|
476
|
+
save: jest.fn().mockResolvedValue(undefined),
|
|
477
|
+
claimBatch: jest.fn().mockResolvedValue([]),
|
|
478
|
+
markPublished: jest.fn().mockResolvedValue(undefined),
|
|
479
|
+
markRetry: jest.fn().mockResolvedValue(undefined),
|
|
480
|
+
markDead: jest.fn().mockResolvedValue(undefined),
|
|
481
|
+
} as unknown as jest.Mocked<IOutboxStore>;
|
|
482
|
+
const publisher: IOutboxPublisher = {
|
|
483
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
const { relay } = await buildRelay({
|
|
487
|
+
providePublisher: true,
|
|
488
|
+
storeMock,
|
|
489
|
+
publisherMock: publisher,
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
await expect(relay.dispatchPendingBatch()).resolves.toBeUndefined();
|
|
493
|
+
expect((publisher.publish as jest.Mock)).not.toHaveBeenCalled();
|
|
494
|
+
expect(storeMock.markPublished).not.toHaveBeenCalled();
|
|
495
|
+
expect(storeMock.markRetry).not.toHaveBeenCalled();
|
|
496
|
+
expect(storeMock.markDead).not.toHaveBeenCalled();
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
describe('AC9: future-dated record is skipped while a claimable record is published in the same cycle', () => {
|
|
501
|
+
it('publishes only the claimable record; future-dated record is not in the batch', async () => {
|
|
502
|
+
const fixedNow = new Date('2026-05-01T00:00:00.000Z');
|
|
503
|
+
const futureDated = makeRecord({
|
|
504
|
+
id: 'r-future',
|
|
505
|
+
nextAttemptAt: new Date(fixedNow.getTime() + 60000),
|
|
506
|
+
});
|
|
507
|
+
const claimable = makeRecord({
|
|
508
|
+
id: 'r-now',
|
|
509
|
+
nextAttemptAt: fixedNow,
|
|
510
|
+
});
|
|
511
|
+
const realStore = new InMemoryOutboxStore();
|
|
512
|
+
await realStore.save(futureDated, {});
|
|
513
|
+
await realStore.save(claimable, {});
|
|
514
|
+
|
|
515
|
+
const publisher: IOutboxPublisher = {
|
|
516
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const moduleRef: TestingModule = await Test.createTestingModule({
|
|
520
|
+
providers: [
|
|
521
|
+
OutboxRelay,
|
|
522
|
+
{ provide: OUTBOX_STORE, useValue: realStore },
|
|
523
|
+
{
|
|
524
|
+
provide: CLOCK,
|
|
525
|
+
useValue: { now: jest.fn().mockReturnValue(fixedNow) },
|
|
526
|
+
},
|
|
527
|
+
{
|
|
528
|
+
provide: ID_GENERATOR,
|
|
529
|
+
useValue: { generate: jest.fn().mockReturnValue('owner-x') },
|
|
530
|
+
},
|
|
531
|
+
{
|
|
532
|
+
provide: OUTBOX_MODULE_OPTIONS,
|
|
533
|
+
useValue: {
|
|
534
|
+
store: realStore,
|
|
535
|
+
publisher,
|
|
536
|
+
relay: {
|
|
537
|
+
enabled: false,
|
|
538
|
+
pollIntervalMs: 5000,
|
|
539
|
+
batchSize: 10,
|
|
540
|
+
leaseMs: 30000,
|
|
541
|
+
maxAttempts: 10,
|
|
542
|
+
backoffBaseMs: 1000,
|
|
543
|
+
backoffCapMs: 300000,
|
|
544
|
+
},
|
|
545
|
+
},
|
|
546
|
+
},
|
|
547
|
+
{ provide: OUTBOX_PUBLISHER, useValue: publisher },
|
|
548
|
+
],
|
|
549
|
+
}).compile();
|
|
550
|
+
const relay = moduleRef.get(OutboxRelay);
|
|
551
|
+
|
|
552
|
+
await relay.dispatchPendingBatch();
|
|
553
|
+
|
|
554
|
+
expect((publisher.publish as jest.Mock)).toHaveBeenCalledTimes(1);
|
|
555
|
+
const publishedRecord = (publisher.publish as jest.Mock).mock.calls[0][0];
|
|
556
|
+
expect(publishedRecord.id).toBe('r-now');
|
|
557
|
+
|
|
558
|
+
const internal = (realStore as unknown as { map: Map<string, OutboxRecord> }).map;
|
|
559
|
+
expect(internal.get('r-now')!.status).toBe(OutboxStatus.PUBLISHED);
|
|
560
|
+
expect(internal.get('r-future')!.status).toBe(OutboxStatus.PENDING);
|
|
561
|
+
|
|
562
|
+
await moduleRef.close();
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
});
|