Repository navigation
Expand file tree
/
Copy patheventAuditRepository.test.ts
More file actions
290 lines (244 loc) · 9.44 KB
/
Copy patheventAuditRepository.test.ts
File metadata and controls
290 lines (244 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* @module repository/eventAuditRepository.test
* @description Unit tests for event audit repository with correlation ID support.
*
* Tests:
* - Event processing with correlation ID
* - Event rejection with correlation ID
* - Audit records store correlation ID
* - Duplicate detection with correlation ID propagation
*/
import { EventAuditService, InMemoryEventAuditRepository } from './eventAuditRepository';
import { EventProcessingAudit } from '../events/types';
describe('EventAuditService with correlation ID support', () => {
let service: EventAuditService;
let repository: InMemoryEventAuditRepository;
beforeEach(() => {
repository = new InMemoryEventAuditRepository();
service = new EventAuditService(repository);
});
describe('processEvent with correlation ID', () => {
/**
* Should accept and store correlation ID when provided.
*/
it('should store correlation ID in audit record', async () => {
const event = {
contractId: 'contract-123',
eventId: 'event-456',
sequence: 1,
payload: { action: 'test' },
};
const result = await service.processEvent(event, 'test-type', 'corr-id-789');
expect(result.status).toBe('accepted');
expect(result.deduplicationKey).toBeTruthy();
const audit = await repository.findByDeduplicationKey(result.deduplicationKey);
expect(audit).toBeTruthy();
expect(audit?.correlationId).toBe('corr-id-789');
});
/**
* Should handle events without correlation ID.
*/
it('should process events without correlation ID', async () => {
const event = {
contractId: 'contract-123',
eventId: 'event-456',
sequence: 1,
payload: { action: 'test' },
};
const result = await service.processEvent(event, 'test-type');
expect(result.status).toBe('accepted');
const audit = await repository.findByDeduplicationKey(result.deduplicationKey);
expect(audit?.correlationId).toBeUndefined();
});
/**
* Should include correlation ID in rejected events.
*/
it('should store correlation ID in rejected event audit', async () => {
const event = {
contractId: 'contract-123',
eventId: 'event-789',
sequence: 2,
payload: { invalid: true },
};
const result = await service.rejectEvent(event, 'Invalid payload', 'corr-id-rejected');
expect(result.status).toBe('rejected');
const audit = await repository.findByDeduplicationKey(result.deduplicationKey);
expect(audit?.correlationId).toBe('corr-id-rejected');
expect(audit?.reason).toBe('Invalid payload');
});
/**
* Should detect duplicates regardless of correlation ID.
*/
it('should detect duplicate events even with different correlation IDs', async () => {
const event = {
contractId: 'contract-123',
eventId: 'event-abc',
sequence: 1,
payload: { data: 'same' },
};
// First event with correlation ID
const result1 = await service.processEvent(event, 'test-type', 'corr-id-1');
expect(result1.status).toBe('accepted');
// Duplicate event with different correlation ID
const result2 = await service.processEvent(event, 'test-type', 'corr-id-2');
expect(result2.status).toBe('duplicate');
expect(result2.deduplicationKey).toBe(result1.deduplicationKey);
});
/**
* Should preserve correlation ID in all audit records for retrieval.
*/
it('should retrieve audit records with correlation ID', async () => {
const event1 = {
contractId: 'contract-123',
eventId: 'event-1',
sequence: 1,
payload: { id: 1 },
};
const event2 = {
contractId: 'contract-123',
eventId: 'event-2',
sequence: 2,
payload: { id: 2 },
};
const correlationId = 'trace-multi-event';
await service.processEvent(event1, 'test-type', correlationId);
await service.processEvent(event2, 'test-type', correlationId);
const audits = await repository.findByContractId('contract-123');
expect(audits).toHaveLength(2);
expect(audits.every(a => a.correlationId === correlationId)).toBe(true);
});
/**
* Should handle events with undefined vs no correlation ID parameter.
*/
it('should handle undefined correlation ID parameter', async () => {
const event = {
contractId: 'contract-456',
eventId: 'event-def',
sequence: 1,
payload: { test: true },
};
const result = await service.processEvent(
event,
'test-type',
undefined
);
expect(result.status).toBe('accepted');
const audit = await repository.findByDeduplicationKey(result.deduplicationKey);
expect(audit?.correlationId).toBeUndefined();
});
});
describe('rejectEvent with correlation ID', () => {
/**
* Should store correlation ID in rejection audit record.
*/
it('should store correlation ID in rejection record', async () => {
const event = {
contractId: 'contract-789',
eventId: 'event-rejected',
sequence: 1,
payload: { invalid: 'data' },
};
const result = await service.rejectEvent(
event,
'Validation failed',
'corr-reject-123'
);
expect(result.status).toBe('rejected');
const audit = await repository.findByDeduplicationKey(result.deduplicationKey);
expect(audit?.correlationId).toBe('corr-reject-123');
});
/**
* Should allow rejection without correlation ID.
*/
it('should reject events without correlation ID', async () => {
const event = {
contractId: 'contract-999',
eventId: 'event-reject-no-corr',
sequence: 1,
payload: { invalid: true },
};
const result = await service.rejectEvent(event, 'Bad data');
expect(result.status).toBe('rejected');
const audit = await repository.findByDeduplicationKey(result.deduplicationKey);
expect(audit?.correlationId).toBeUndefined();
});
});
describe('End-to-end correlation ID tracking', () => {
/**
* Should track correlation ID through complete lifecycle.
*/
it('should track correlation ID from event acceptance through retrieval', async () => {
const correlationId = 'e2e-trace-123';
const contractId = 'contract-e2e';
// Process multiple events with same correlation ID
const events = [
{ contractId, eventId: 'e1', sequence: 1, payload: { data: 1 } },
{ contractId, eventId: 'e2', sequence: 2, payload: { data: 2 } },
{ contractId, eventId: 'e3', sequence: 3, payload: { data: 3 } },
];
for (const event of events) {
const result = await service.processEvent(event, 'test-type', correlationId);
expect(result.status).toBe('accepted');
}
// Retrieve and verify correlation ID is preserved
const history = await service.getEventHistory(contractId);
expect(history).toHaveLength(3);
expect(history.every(h => h.correlationId === correlationId)).toBe(true);
});
/**
* Should preserve different correlation IDs for different request batches.
*/
it('should preserve different correlation IDs per request', async () => {
const contractId = 'contract-multi-corr';
// Batch 1
const event1 = { contractId, eventId: 'e1', sequence: 1, payload: { b: 1 } };
await service.processEvent(event1, 'test-type', 'corr-batch-1');
// Batch 2
const event2 = { contractId, eventId: 'e2', sequence: 2, payload: { b: 2 } };
await service.processEvent(event2, 'test-type', 'corr-batch-2');
const history = await service.getEventHistory(contractId);
expect(history).toHaveLength(2);
const audit1 = history.find(h => h.eventId === 'e1');
const audit2 = history.find(h => h.eventId === 'e2');
expect(audit1?.correlationId).toBe('corr-batch-1');
expect(audit2?.correlationId).toBe('corr-batch-2');
});
});
describe('Statistics with correlation ID', () => {
/**
* Should track statistics correctly with correlation ID support.
* Note: When processEvent detects a duplicate, it returns status='duplicate'
* but does NOT create a new audit record. So duplicates count is 0 even if
* we get duplicate results.
*/
it('should maintain accurate statistics with correlation IDs', async () => {
const event = {
contractId: 'contract-stats',
eventId: 'event-stat-1',
sequence: 1,
payload: { data: 'test' },
};
const event2 = {
contractId: 'contract-stats',
eventId: 'event-stat-2',
sequence: 2,
payload: { data: 'test-2' },
};
const event3 = {
contractId: 'contract-stats',
eventId: 'event-stat-3',
sequence: 3,
payload: { data: 'test-3' },
};
await service.processEvent(event, 'test-type', 'corr-stat-1');
await service.processEvent(event, 'test-type', 'corr-stat-2'); // Duplicate result, no new record
await service.processEvent(event2, 'test-type', 'corr-stat-3'); // Accepted
await service.rejectEvent(event3, 'Rejected', 'corr-stat-4'); // Rejected
const stats = await service.getStatistics();
expect(stats.total).toBe(3);
expect(stats.accepted).toBe(2);
expect(stats.duplicates).toBe(0); // No duplicate audit records created
expect(stats.rejected).toBe(1);
});
});
});