@nathapp/nestjs-outbox 3.3.0 → 4.0.1

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 (42) hide show
  1. package/package.json +11 -11
  2. package/.eslintrc.js +0 -8
  3. package/.nax/features/nestjs-outbox/.nax-acceptance.test.ts +0 -878
  4. package/.turbo/turbo-build.log +0 -4
  5. package/CHANGELOG.md +0 -15
  6. package/lib/constant/index.ts +0 -1
  7. package/lib/constant/outbox.constants.ts +0 -3
  8. package/lib/exception/index.ts +0 -3
  9. package/lib/exception/outbox-exception-code.enum.ts +0 -4
  10. package/lib/exception/outbox-invalid-event.exception.ts +0 -9
  11. package/lib/exception/outbox-misconfigured.exception.ts +0 -9
  12. package/lib/index.ts +0 -6
  13. package/lib/interface/index.ts +0 -18
  14. package/lib/interface/outbox-module-options.interface.ts +0 -18
  15. package/lib/interface/outbox-publisher.interface.ts +0 -5
  16. package/lib/interface/outbox-record.interface.ts +0 -23
  17. package/lib/interface/outbox-status.enum.ts +0 -6
  18. package/lib/interface/outbox-store.interface.ts +0 -23
  19. package/lib/module/index.ts +0 -1
  20. package/lib/module/outbox.module.ts +0 -64
  21. package/lib/provider/in-memory-outbox-store.ts +0 -114
  22. package/lib/provider/index.ts +0 -4
  23. package/lib/provider/outbox-relay.ts +0 -147
  24. package/lib/provider/outbox.service.ts +0 -70
  25. package/lib/provider/resolve-outbox-options.ts +0 -51
  26. package/lib/util/compute-backoff.ts +0 -4
  27. package/lib/util/index.ts +0 -1
  28. package/test/barrel/index.spec.ts +0 -45
  29. package/test/constant/outbox.constants.spec.ts +0 -20
  30. package/test/e2e/outbox.module.spec.ts +0 -371
  31. package/test/exception/outbox.exceptions.spec.ts +0 -38
  32. package/test/interface/outbox-status.enum.spec.ts +0 -19
  33. package/test/jest-nax-setup.js +0 -31
  34. package/test/jest-nax.json +0 -30
  35. package/test/jest-test.json +0 -28
  36. package/test/module/outbox.module.spec.ts +0 -139
  37. package/test/provider/in-memory-outbox-store.spec.ts +0 -216
  38. package/test/provider/outbox-relay.spec.ts +0 -565
  39. package/test/provider/outbox-service.spec.ts +0 -229
  40. package/test/provider/resolve-outbox-options.spec.ts +0 -50
  41. package/test/util/compute-backoff.spec.ts +0 -25
  42. package/tsconfig.json +0 -9
@@ -1,229 +0,0 @@
1
- import {
2
- CLOCK,
3
- ID_GENERATOR,
4
- IClock,
5
- IIdGenerator,
6
- } from '@nathapp/nestjs-common';
7
- import { Test, TestingModule } from '@nestjs/testing';
8
- import { TRANSACTION_MANAGER, ITransactionManager } from '@nathapp/nestjs-data';
9
- import { OutboxService } from '../../lib/provider/outbox.service';
10
- import { OUTBOX_STORE } from '../../lib/constant/outbox.constants';
11
- import {
12
- OutboxRecord,
13
- } from '../../lib/interface/outbox-record.interface';
14
- import { OutboxStatus } from '../../lib/interface/outbox-status.enum';
15
- import { IOutboxStore } from '../../lib/interface/outbox-store.interface';
16
- import { OutboxInvalidEventException } from '../../lib/exception/outbox-invalid-event.exception';
17
-
18
- describe('OutboxService.record (AC1-AC8)', () => {
19
- let module: TestingModule;
20
- let service: OutboxService;
21
-
22
- let storeSave: jest.Mock;
23
- let getClient: jest.Mock;
24
- let clockNow: jest.Mock;
25
- let idGen: jest.Mock;
26
-
27
- let storedRecord: OutboxRecord | undefined;
28
- let storedClient: unknown;
29
- let callCount: number;
30
-
31
- const fixedDate = new Date('2026-05-01T00:00:00.000Z');
32
-
33
- const buildModule = async (options: {
34
- provideClock?: boolean;
35
- provideIdGen?: boolean;
36
- configureStore?: (mock: jest.Mock) => void;
37
- }): Promise<void> => {
38
- storeSave = jest.fn();
39
- if (options.configureStore) {
40
- options.configureStore(storeSave);
41
- }
42
- getClient = jest.fn();
43
- getClient.mockReturnValue({ __txClient: true });
44
-
45
- clockNow = jest.fn();
46
- clockNow.mockReturnValue(fixedDate);
47
-
48
- idGen = jest.fn();
49
- idGen.mockReturnValue('id-123');
50
-
51
- storedRecord = undefined;
52
- storedClient = undefined;
53
- callCount = 0;
54
- storeSave.mockImplementation(async (record, client) => {
55
- storedRecord = record;
56
- storedClient = client;
57
- callCount += 1;
58
- });
59
-
60
- const txManager: ITransactionManager = {
61
- run: jest.fn(),
62
- getClient: getClient as unknown as ITransactionManager['getClient'],
63
- isInTransaction: jest.fn(),
64
- };
65
-
66
- const storeProvider = {
67
- provide: OUTBOX_STORE,
68
- useValue: { save: storeSave } as unknown as IOutboxStore,
69
- };
70
-
71
- const txProvider = {
72
- provide: TRANSACTION_MANAGER,
73
- useValue: txManager,
74
- };
75
-
76
- const providers: any[] = [storeProvider, txProvider, OutboxService];
77
-
78
- if (options.provideClock) {
79
- providers.push({
80
- provide: CLOCK,
81
- useValue: { now: clockNow } as unknown as IClock,
82
- });
83
- }
84
- if (options.provideIdGen) {
85
- providers.push({
86
- provide: ID_GENERATOR,
87
- useValue: { generate: idGen } as unknown as IIdGenerator,
88
- });
89
- }
90
-
91
- module = await Test.createTestingModule({
92
- providers,
93
- }).compile();
94
-
95
- service = module.get(OutboxService);
96
- };
97
-
98
- afterEach(async () => {
99
- if (module) {
100
- await module.close();
101
- }
102
- });
103
-
104
- describe('AC1: empty type rejects with OutboxInvalidEventException, save is never called', () => {
105
- it('throws OutboxInvalidEventException with httpStatus 400 and store.save is never called', async () => {
106
- await buildModule({ provideClock: true, provideIdGen: true });
107
-
108
- let caught: unknown;
109
- try {
110
- await service.record({ type: '', payload: {} });
111
- } catch (e) {
112
- caught = e;
113
- }
114
-
115
- expect(caught).toBeInstanceOf(OutboxInvalidEventException);
116
- expect((caught as OutboxInvalidEventException).httpStatus).toBe(400);
117
- expect(storeSave).not.toHaveBeenCalled();
118
- });
119
- });
120
-
121
- describe('AC2: blank/whitespace-only type rejects with OutboxInvalidEventException, save is never called', () => {
122
- it('throws OutboxInvalidEventException and store.save is never called for " "', async () => {
123
- await buildModule({ provideClock: true, provideIdGen: true });
124
-
125
- await expect(
126
- service.record({ type: ' ', payload: {} }),
127
- ).rejects.toBeInstanceOf(OutboxInvalidEventException);
128
-
129
- expect(storeSave).not.toHaveBeenCalled();
130
- });
131
- });
132
-
133
- describe('AC3: getClient() called and save called once with reference-equal client', () => {
134
- it('invokes the tx manager getClient() and saves exactly once with the client as second arg', async () => {
135
- await buildModule({ provideClock: true, provideIdGen: true });
136
-
137
- const txClient = { __theClient: Symbol('x') };
138
- getClient.mockReturnValue(txClient);
139
-
140
- await service.record({ type: 'user.created', payload: { x: 1 } });
141
-
142
- expect(getClient).toHaveBeenCalledTimes(1);
143
- expect(storeSave).toHaveBeenCalledTimes(1);
144
- expect(storeSave.mock.calls[0][1]).toBe(txClient);
145
- });
146
- });
147
-
148
- describe('AC4: saved record has correct id/type/payload/status/attempts', () => {
149
- it('record passed to save has PENDING status, attempts=0, id=generated, type and payload set', async () => {
150
- await buildModule({ provideClock: true, provideIdGen: true });
151
- idGen.mockReturnValue('gen-id-42');
152
-
153
- await service.record({ type: 'user.created', payload: { x: 1 } });
154
-
155
- expect(storedRecord).toBeDefined();
156
- expect(storedRecord!.status).toBe(OutboxStatus.PENDING);
157
- expect(storedRecord!.attempts).toBe(0);
158
- expect(storedRecord!.id).toBe('gen-id-42');
159
- expect(storedRecord!.type).toBe('user.created');
160
- expect(storedRecord!.payload).toEqual({ x: 1 });
161
- });
162
- });
163
-
164
- describe('AC5: createdAt from clock, nextAttemptAt <= createdAt, record() resolves to saved record', () => {
165
- it('createdAt equals clock.now() and nextAttemptAt is at or before createdAt, returned record matches', async () => {
166
- await buildModule({ provideClock: true, provideIdGen: true });
167
- const t = new Date('2026-05-01T12:34:56.000Z');
168
- clockNow.mockReturnValue(t);
169
-
170
- const returned = await service.record({
171
- type: 'user.created',
172
- payload: { x: 1 },
173
- });
174
-
175
- expect(storedRecord!.createdAt).toBe(t);
176
- expect(storedRecord!.nextAttemptAt.getTime()).toBeLessThanOrEqual(
177
- storedRecord!.createdAt.getTime(),
178
- );
179
- expect(returned).toBe(storedRecord);
180
- });
181
- });
182
-
183
- describe('AC6: optional headers and metadata are propagated to the saved record', () => {
184
- it('saved record carries deep-equal headers and metadata from the input', async () => {
185
- await buildModule({ provideClock: true, provideIdGen: true });
186
-
187
- await service.record({
188
- type: 't',
189
- payload: {},
190
- headers: { 'x-h': '1' },
191
- metadata: { a: 2 },
192
- });
193
-
194
- expect(storedRecord!.headers).toEqual({ 'x-h': '1' });
195
- expect(storedRecord!.metadata).toEqual({ a: 2 });
196
- });
197
- });
198
-
199
- describe('AC7: without CLOCK/ID_GENERATOR provided, id is non-empty string and createdAt is Date', () => {
200
- it('falls back to default SystemClock + UuidIdGenerator and produces a non-empty string id and a Date', async () => {
201
- await buildModule({});
202
-
203
- const returned = await service.record({ type: 't', payload: {} });
204
-
205
- expect(typeof returned.id).toBe('string');
206
- expect(returned.id.length).toBeGreaterThan(0);
207
- expect(returned.createdAt).toBeInstanceOf(Date);
208
- expect(callCount).toBe(1);
209
- expect(getClient).toHaveBeenCalledTimes(1);
210
- });
211
- });
212
-
213
- describe('AC8: error from store.save propagates', () => {
214
- it('record() rejects with the same error that store.save rejects with', async () => {
215
- const boom = new Error('store down');
216
- await buildModule({
217
- provideClock: true,
218
- provideIdGen: true,
219
- configureStore: (m) => {
220
- m.mockRejectedValueOnce(boom);
221
- },
222
- });
223
-
224
- await expect(
225
- service.record({ type: 'user.created', payload: { x: 1 } }),
226
- ).rejects.toBe(boom);
227
- });
228
- });
229
- });
@@ -1,50 +0,0 @@
1
- import { resolveOutboxOptions } from '../../lib/provider/resolve-outbox-options';
2
- import { InMemoryOutboxStore } from '../../lib/provider/in-memory-outbox-store';
3
-
4
- describe('resolveOutboxOptions (AC #2, #3, #4, #5)', () => {
5
- it('resolveOutboxOptions(undefined) returns all default relay values', () => {
6
- const resolved = resolveOutboxOptions(undefined);
7
-
8
- expect(resolved.relay.enabled).toBe(true);
9
- expect(resolved.relay.pollIntervalMs).toBe(5000);
10
- expect(resolved.relay.batchSize).toBe(10);
11
- expect(resolved.relay.leaseMs).toBe(30000);
12
- expect(resolved.relay.maxAttempts).toBe(10);
13
- expect(resolved.relay.backoffBaseMs).toBe(1000);
14
- expect(resolved.relay.backoffCapMs).toBe(300000);
15
- });
16
-
17
- it('resolveOutboxOptions({}) defaults store to InMemoryOutboxStore and leaves publisher undefined', () => {
18
- const resolved = resolveOutboxOptions({});
19
-
20
- expect(resolved.store).toBeInstanceOf(InMemoryOutboxStore);
21
- expect(resolved.publisher).toBeUndefined();
22
- });
23
-
24
- it('partial relay override keeps unspecified fields at defaults', () => {
25
- const resolved = resolveOutboxOptions({
26
- relay: { pollIntervalMs: 100, batchSize: 3, maxAttempts: 2 },
27
- });
28
-
29
- expect(resolved.relay.pollIntervalMs).toBe(100);
30
- expect(resolved.relay.batchSize).toBe(3);
31
- expect(resolved.relay.maxAttempts).toBe(2);
32
-
33
- expect(resolved.relay.leaseMs).toBe(30000);
34
- expect(resolved.relay.backoffBaseMs).toBe(1000);
35
- expect(resolved.relay.backoffCapMs).toBe(300000);
36
- });
37
-
38
- it('preserves caller-supplied store and publisher by reference', () => {
39
- const customStore = new InMemoryOutboxStore();
40
- const customPublisher = { publish: jest.fn() };
41
-
42
- const resolved = resolveOutboxOptions({
43
- store: customStore,
44
- publisher: customPublisher,
45
- });
46
-
47
- expect(resolved.store).toBe(customStore);
48
- expect(resolved.publisher).toBe(customPublisher);
49
- });
50
- });
@@ -1,25 +0,0 @@
1
- import { computeBackoff } from '../../lib/util/compute-backoff';
2
-
3
- describe('computeBackoff (AC #6, #7)', () => {
4
- it('attempts=1 returns baseMs', () => {
5
- expect(computeBackoff(1, 1000, 300000)).toBe(1000);
6
- });
7
-
8
- it('attempts=2 returns baseMs * 2', () => {
9
- expect(computeBackoff(2, 1000, 300000)).toBe(2000);
10
- });
11
-
12
- it('attempts=3 returns baseMs * 4', () => {
13
- expect(computeBackoff(3, 1000, 300000)).toBe(4000);
14
- });
15
-
16
- it('attempts=50 caps at capMs and never exceeds it', () => {
17
- const result = computeBackoff(50, 1000, 300000);
18
- expect(result).toBe(300000);
19
- expect(result).toBeLessThanOrEqual(300000);
20
- });
21
-
22
- it('caps at capMs when 2^(attempts-1) * baseMs would overflow', () => {
23
- expect(computeBackoff(20, 1000, 300000)).toBe(300000);
24
- });
25
- });
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "@nathapp/typescript-config/base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./lib"
6
- },
7
- "include": ["lib/**/*", "../index.ts", "../test/**/*"],
8
- "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
9
- }