@bernierllc/email-webhook-events 1.4.1 → 1.7.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/dist/EmailWebhookEventsService.js +4 -0
- package/dist/EmailWebhookEventsService.js.map +1 -1
- package/dist/analytics/AnalyticsEngine.js +2 -1
- package/dist/analytics/AnalyticsEngine.js.map +1 -1
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +25 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/persistence/InMemoryPersistenceAdapter.js +2 -4
- package/dist/persistence/InMemoryPersistenceAdapter.js.map +1 -1
- package/package.json +6 -2
- package/.eslintrc.cjs +0 -30
- package/CHANGELOG.md +0 -139
- package/__tests__/EmailWebhookEventsService.test.ts +0 -251
- package/__tests__/WebhookEventCallbacks.test.ts +0 -343
- package/__tests__/analytics/AnalyticsEngine.test.ts +0 -250
- package/__tests__/fixtures/sendgrid-webhooks/.gitkeep +0 -0
- package/__tests__/fixtures/sendgrid-webhooks/README.md +0 -63
- package/__tests__/fixtures/sendgrid-webhooks/deferred.json +0 -31
- package/__tests__/fixtures/sendgrid-webhooks/delivered.json +0 -83
- package/__tests__/fixtures/sendgrid-webhooks/open.json +0 -83
- package/__tests__/integration/ngrok-tunnel-manager.ts +0 -88
- package/__tests__/integration/sendgrid-webhook-integration.test.ts +0 -272
- package/__tests__/integration/webhook-server.ts +0 -188
- package/__tests__/mocks/README.md +0 -172
- package/__tests__/mocks/index.ts +0 -94
- package/__tests__/mocks/sendgrid-api-responses.ts +0 -391
- package/__tests__/mocks/sendgrid-webhook-payloads.ts +0 -463
- package/__tests__/normalizers/MailgunNormalizer.test.ts +0 -149
- package/__tests__/normalizers/PostmarkNormalizer.test.ts +0 -112
- package/__tests__/normalizers/SESNormalizer.test.ts +0 -168
- package/__tests__/normalizers/SMTP2GONormalizer.test.ts +0 -83
- package/__tests__/normalizers/SendGridNormalizer.test.ts +0 -181
- package/__tests__/persistence/PersistenceAdapter.test.ts +0 -103
- package/jest.config.cjs +0 -38
- package/src/EmailWebhookEventsService.ts +0 -290
- package/src/analytics/AnalyticsEngine.ts +0 -230
- package/src/index.ts +0 -40
- package/src/normalizers/EventNormalizer.ts +0 -13
- package/src/normalizers/MailgunNormalizer.ts +0 -72
- package/src/normalizers/PostmarkNormalizer.ts +0 -72
- package/src/normalizers/SESNormalizer.ts +0 -117
- package/src/normalizers/SMTP2GONormalizer.ts +0 -54
- package/src/normalizers/SendGridNormalizer.ts +0 -63
- package/src/persistence/InMemoryPersistenceAdapter.ts +0 -68
- package/src/persistence/PersistenceAdapter.ts +0 -15
- package/src/types.ts +0 -261
- package/tsconfig.json +0 -31
|
@@ -1,463 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* SendGrid Webhook Mock Payloads
|
|
11
|
-
*
|
|
12
|
-
* These mocks are generated from REAL SendGrid webhook payloads captured during
|
|
13
|
-
* integration testing. They accurately reflect the actual structure and data
|
|
14
|
-
* that SendGrid sends.
|
|
15
|
-
*
|
|
16
|
-
* Philosophy: "Only mock services that are not owned by us"
|
|
17
|
-
* - SendGrid is external, so we mock it
|
|
18
|
-
* - Our internal packages use real implementations
|
|
19
|
-
*
|
|
20
|
-
* Last captured: 2025-12-24
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
// ============================================================================
|
|
24
|
-
// TYPE DEFINITIONS (based on real payloads)
|
|
25
|
-
// ============================================================================
|
|
26
|
-
|
|
27
|
-
export interface SendGridWebhookHeaders {
|
|
28
|
-
host: string;
|
|
29
|
-
'user-agent': string;
|
|
30
|
-
'content-length': string;
|
|
31
|
-
'accept-encoding': string;
|
|
32
|
-
'content-type': string;
|
|
33
|
-
'x-forwarded-for': string;
|
|
34
|
-
'x-forwarded-host': string;
|
|
35
|
-
'x-forwarded-proto': string;
|
|
36
|
-
'x-twilio-email-event-webhook-signature': string;
|
|
37
|
-
'x-twilio-email-event-webhook-timestamp': string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface SendGridDeliveredEvent {
|
|
41
|
-
email: string;
|
|
42
|
-
event: 'delivered';
|
|
43
|
-
ip: string;
|
|
44
|
-
response: string;
|
|
45
|
-
sg_event_id: string;
|
|
46
|
-
sg_message_id: string;
|
|
47
|
-
'smtp-id': string;
|
|
48
|
-
timestamp: number;
|
|
49
|
-
tls: number;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface SendGridOpenEvent {
|
|
53
|
-
email: string;
|
|
54
|
-
event: 'open';
|
|
55
|
-
ip: string;
|
|
56
|
-
sg_content_type: string;
|
|
57
|
-
sg_event_id: string;
|
|
58
|
-
sg_machine_open: boolean;
|
|
59
|
-
sg_message_id: string;
|
|
60
|
-
timestamp: number;
|
|
61
|
-
useragent: string;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export interface SendGridDeferredEvent {
|
|
65
|
-
attempt: string;
|
|
66
|
-
domain: string;
|
|
67
|
-
email: string;
|
|
68
|
-
event: 'deferred';
|
|
69
|
-
from: string;
|
|
70
|
-
response: string;
|
|
71
|
-
sg_event_id: string;
|
|
72
|
-
sg_message_id: string;
|
|
73
|
-
'smtp-id': string;
|
|
74
|
-
timestamp: number;
|
|
75
|
-
tls: number;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface SendGridBounceEvent {
|
|
79
|
-
email: string;
|
|
80
|
-
event: 'bounce';
|
|
81
|
-
ip: string;
|
|
82
|
-
reason: string;
|
|
83
|
-
sg_event_id: string;
|
|
84
|
-
sg_message_id: string;
|
|
85
|
-
'smtp-id': string;
|
|
86
|
-
status: string;
|
|
87
|
-
timestamp: number;
|
|
88
|
-
tls: number;
|
|
89
|
-
type: 'hard' | 'soft';
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export interface SendGridClickEvent {
|
|
93
|
-
email: string;
|
|
94
|
-
event: 'click';
|
|
95
|
-
ip: string;
|
|
96
|
-
sg_event_id: string;
|
|
97
|
-
sg_message_id: string;
|
|
98
|
-
timestamp: number;
|
|
99
|
-
url: string;
|
|
100
|
-
url_offset: { index: number; type: string };
|
|
101
|
-
useragent: string;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface SendGridSpamReportEvent {
|
|
105
|
-
email: string;
|
|
106
|
-
event: 'spamreport';
|
|
107
|
-
sg_event_id: string;
|
|
108
|
-
sg_message_id: string;
|
|
109
|
-
timestamp: number;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface SendGridUnsubscribeEvent {
|
|
113
|
-
email: string;
|
|
114
|
-
event: 'unsubscribe';
|
|
115
|
-
sg_event_id: string;
|
|
116
|
-
sg_message_id: string;
|
|
117
|
-
timestamp: number;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export interface SendGridDroppedEvent {
|
|
121
|
-
email: string;
|
|
122
|
-
event: 'dropped';
|
|
123
|
-
reason: string;
|
|
124
|
-
sg_event_id: string;
|
|
125
|
-
sg_message_id: string;
|
|
126
|
-
'smtp-id': string;
|
|
127
|
-
timestamp: number;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export type SendGridWebhookEvent =
|
|
131
|
-
| SendGridDeliveredEvent
|
|
132
|
-
| SendGridOpenEvent
|
|
133
|
-
| SendGridDeferredEvent
|
|
134
|
-
| SendGridBounceEvent
|
|
135
|
-
| SendGridClickEvent
|
|
136
|
-
| SendGridSpamReportEvent
|
|
137
|
-
| SendGridUnsubscribeEvent
|
|
138
|
-
| SendGridDroppedEvent;
|
|
139
|
-
|
|
140
|
-
export interface SendGridWebhookPayload {
|
|
141
|
-
headers: SendGridWebhookHeaders;
|
|
142
|
-
body: SendGridWebhookEvent;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// ============================================================================
|
|
146
|
-
// REAL CAPTURED PAYLOADS (from integration tests)
|
|
147
|
-
// ============================================================================
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Example SendGrid headers (sanitized from real captures)
|
|
151
|
-
*
|
|
152
|
-
* NOTE: All IDs, signatures, and identifiable data have been replaced
|
|
153
|
-
* with fake values for security. Structure matches real SendGrid payloads.
|
|
154
|
-
*/
|
|
155
|
-
export const REAL_SENDGRID_HEADERS: SendGridWebhookHeaders = {
|
|
156
|
-
host: 'example-webhook.ngrok.app',
|
|
157
|
-
'user-agent': 'SendGrid Event API',
|
|
158
|
-
'content-length': '405',
|
|
159
|
-
'accept-encoding': 'gzip',
|
|
160
|
-
'content-type': 'application/json;charset=utf-8',
|
|
161
|
-
'x-forwarded-for': '192.0.2.1', // TEST-NET-1 (RFC 5737)
|
|
162
|
-
'x-forwarded-host': 'example-webhook.ngrok.app',
|
|
163
|
-
'x-forwarded-proto': 'https',
|
|
164
|
-
// Fake signature - structure matches real but value is test data
|
|
165
|
-
'x-twilio-email-event-webhook-signature': 'MEUCIQD_FAKE_SIGNATURE_FOR_TESTING_PURPOSES_ONLYAiEA_FAKE_SIG_DO_NOT_USE_IN_PRODUCTION=',
|
|
166
|
-
'x-twilio-email-event-webhook-timestamp': '1700000000'
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Example delivered event (sanitized from real captures)
|
|
171
|
-
*/
|
|
172
|
-
export const REAL_DELIVERED_EVENT: SendGridDeliveredEvent = {
|
|
173
|
-
email: 'test-recipient@example.com',
|
|
174
|
-
event: 'delivered',
|
|
175
|
-
ip: '192.0.2.10', // TEST-NET-1 (RFC 5737)
|
|
176
|
-
response: '250 2.0.0 OK 1700000000 example-mail-server.example.com - gsmtp',
|
|
177
|
-
sg_event_id: 'FAKE_EVENT_ID_ZGVsaXZlcmVkLTAtMTM1ODk2OC0',
|
|
178
|
-
sg_message_id: 'FAKE_MSG_ID.recvd-1234567890-abcde-1-12345678-10.0',
|
|
179
|
-
'smtp-id': '<FAKE_SMTP_ID@geopod-ismtpd-test>',
|
|
180
|
-
timestamp: 1700000000,
|
|
181
|
-
tls: 1
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Example open event (sanitized from real captures)
|
|
186
|
-
*/
|
|
187
|
-
export const REAL_OPEN_EVENT: SendGridOpenEvent = {
|
|
188
|
-
email: 'test-recipient@example.com',
|
|
189
|
-
event: 'open',
|
|
190
|
-
ip: '192.0.2.20', // TEST-NET-1 (RFC 5737)
|
|
191
|
-
sg_content_type: 'html',
|
|
192
|
-
sg_event_id: 'FAKE_OPEN_EVENT_ID_SGhn1bJGTdGTubBQ',
|
|
193
|
-
sg_machine_open: false,
|
|
194
|
-
sg_message_id: 'FAKE_MSG_ID.recvd-1234567890-abcde-1-12345678-1B.0',
|
|
195
|
-
timestamp: 1700000100,
|
|
196
|
-
useragent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Example deferred event (sanitized from real captures)
|
|
201
|
-
*/
|
|
202
|
-
export const REAL_DEFERRED_EVENT: SendGridDeferredEvent = {
|
|
203
|
-
attempt: '2',
|
|
204
|
-
domain: 'example.com',
|
|
205
|
-
email: 'test-recipient@example.com',
|
|
206
|
-
event: 'deferred',
|
|
207
|
-
from: 'Test Sender <sender@example.com>',
|
|
208
|
-
response: 'temporarily unable to deliver - example deferred response',
|
|
209
|
-
sg_event_id: 'FAKE_DEFERRED_EVENT_ID_ZGVmZXJyZWQtMi0',
|
|
210
|
-
sg_message_id: 'FAKE_MSG_ID.recvd-1234567890-zyxwv-1-12345678-14.0',
|
|
211
|
-
'smtp-id': '<FAKE_DEFERRED_SMTP_ID@geopod-ismtpd-test>',
|
|
212
|
-
timestamp: 1700000200,
|
|
213
|
-
tls: 0
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// ============================================================================
|
|
217
|
-
// MOCK FACTORY FUNCTIONS
|
|
218
|
-
// ============================================================================
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Generate a unique SendGrid event ID
|
|
222
|
-
*/
|
|
223
|
-
function generateEventId(): string {
|
|
224
|
-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
225
|
-
let result = '';
|
|
226
|
-
for (let i = 0; i < 22; i++) {
|
|
227
|
-
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
228
|
-
}
|
|
229
|
-
return result;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Generate a unique SendGrid message ID
|
|
234
|
-
*/
|
|
235
|
-
function generateMessageId(): string {
|
|
236
|
-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
237
|
-
let prefix = '';
|
|
238
|
-
for (let i = 0; i < 22; i++) {
|
|
239
|
-
prefix += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
240
|
-
}
|
|
241
|
-
return `${prefix}.recvd-${Date.now()}-1-${Date.now().toString(16)}-10.0`;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Generate mock SendGrid webhook headers
|
|
246
|
-
*/
|
|
247
|
-
export function createMockHeaders(overrides?: Partial<SendGridWebhookHeaders>): SendGridWebhookHeaders {
|
|
248
|
-
return {
|
|
249
|
-
...REAL_SENDGRID_HEADERS,
|
|
250
|
-
'x-twilio-email-event-webhook-timestamp': Math.floor(Date.now() / 1000).toString(),
|
|
251
|
-
...overrides
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Create a mock delivered event
|
|
257
|
-
*/
|
|
258
|
-
export function createDeliveredEvent(overrides?: Partial<SendGridDeliveredEvent>): SendGridDeliveredEvent {
|
|
259
|
-
return {
|
|
260
|
-
email: 'test@example.com',
|
|
261
|
-
event: 'delivered',
|
|
262
|
-
ip: '192.0.2.10', // TEST-NET-1 (RFC 5737)
|
|
263
|
-
response: '250 2.0.0 OK ' + Date.now() + ' mail.example.com - gsmtp',
|
|
264
|
-
sg_event_id: generateEventId(),
|
|
265
|
-
sg_message_id: generateMessageId(),
|
|
266
|
-
'smtp-id': `<${generateEventId()}@geopod-ismtpd-test>`,
|
|
267
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
268
|
-
tls: 1,
|
|
269
|
-
...overrides
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Create a mock open event
|
|
275
|
-
*/
|
|
276
|
-
export function createOpenEvent(overrides?: Partial<SendGridOpenEvent>): SendGridOpenEvent {
|
|
277
|
-
return {
|
|
278
|
-
email: 'test@example.com',
|
|
279
|
-
event: 'open',
|
|
280
|
-
ip: '192.0.2.100', // TEST-NET-1 (RFC 5737)
|
|
281
|
-
sg_content_type: 'html',
|
|
282
|
-
sg_event_id: generateEventId(),
|
|
283
|
-
sg_machine_open: false,
|
|
284
|
-
sg_message_id: generateMessageId(),
|
|
285
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
286
|
-
useragent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|
287
|
-
...overrides
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Create a mock click event
|
|
293
|
-
*/
|
|
294
|
-
export function createClickEvent(overrides?: Partial<SendGridClickEvent>): SendGridClickEvent {
|
|
295
|
-
return {
|
|
296
|
-
email: 'test@example.com',
|
|
297
|
-
event: 'click',
|
|
298
|
-
ip: '192.0.2.101', // TEST-NET-1 (RFC 5737)
|
|
299
|
-
sg_event_id: generateEventId(),
|
|
300
|
-
sg_message_id: generateMessageId(),
|
|
301
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
302
|
-
url: 'https://example.com/clicked-link',
|
|
303
|
-
url_offset: { index: 0, type: 'html' },
|
|
304
|
-
useragent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|
305
|
-
...overrides
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Create a mock bounce event
|
|
311
|
-
*/
|
|
312
|
-
export function createBounceEvent(overrides?: Partial<SendGridBounceEvent>): SendGridBounceEvent {
|
|
313
|
-
return {
|
|
314
|
-
email: 'test@example.com',
|
|
315
|
-
event: 'bounce',
|
|
316
|
-
ip: '192.0.2.11', // TEST-NET-1 (RFC 5737)
|
|
317
|
-
reason: '550 5.1.1 The email account that you tried to reach does not exist',
|
|
318
|
-
sg_event_id: generateEventId(),
|
|
319
|
-
sg_message_id: generateMessageId(),
|
|
320
|
-
'smtp-id': `<${generateEventId()}@geopod-ismtpd-11>`,
|
|
321
|
-
status: '5.1.1',
|
|
322
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
323
|
-
tls: 1,
|
|
324
|
-
type: 'hard',
|
|
325
|
-
...overrides
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Create a mock deferred event
|
|
331
|
-
*/
|
|
332
|
-
export function createDeferredEvent(overrides?: Partial<SendGridDeferredEvent>): SendGridDeferredEvent {
|
|
333
|
-
return {
|
|
334
|
-
attempt: '1',
|
|
335
|
-
domain: 'example.com',
|
|
336
|
-
email: 'test@example.com',
|
|
337
|
-
event: 'deferred',
|
|
338
|
-
from: 'sender@example.com',
|
|
339
|
-
response: 'temporarily rejected',
|
|
340
|
-
sg_event_id: generateEventId(),
|
|
341
|
-
sg_message_id: generateMessageId(),
|
|
342
|
-
'smtp-id': `<${generateEventId()}@geopod-ismtpd-11>`,
|
|
343
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
344
|
-
tls: 1,
|
|
345
|
-
...overrides
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Create a mock spam report event
|
|
351
|
-
*/
|
|
352
|
-
export function createSpamReportEvent(overrides?: Partial<SendGridSpamReportEvent>): SendGridSpamReportEvent {
|
|
353
|
-
return {
|
|
354
|
-
email: 'test@example.com',
|
|
355
|
-
event: 'spamreport',
|
|
356
|
-
sg_event_id: generateEventId(),
|
|
357
|
-
sg_message_id: generateMessageId(),
|
|
358
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
359
|
-
...overrides
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Create a mock unsubscribe event
|
|
365
|
-
*/
|
|
366
|
-
export function createUnsubscribeEvent(overrides?: Partial<SendGridUnsubscribeEvent>): SendGridUnsubscribeEvent {
|
|
367
|
-
return {
|
|
368
|
-
email: 'test@example.com',
|
|
369
|
-
event: 'unsubscribe',
|
|
370
|
-
sg_event_id: generateEventId(),
|
|
371
|
-
sg_message_id: generateMessageId(),
|
|
372
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
373
|
-
...overrides
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Create a mock dropped event
|
|
379
|
-
*/
|
|
380
|
-
export function createDroppedEvent(overrides?: Partial<SendGridDroppedEvent>): SendGridDroppedEvent {
|
|
381
|
-
return {
|
|
382
|
-
email: 'test@example.com',
|
|
383
|
-
event: 'dropped',
|
|
384
|
-
reason: 'Bounced Address',
|
|
385
|
-
sg_event_id: generateEventId(),
|
|
386
|
-
sg_message_id: generateMessageId(),
|
|
387
|
-
'smtp-id': `<${generateEventId()}@geopod-ismtpd-11>`,
|
|
388
|
-
timestamp: Math.floor(Date.now() / 1000),
|
|
389
|
-
...overrides
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* Create a complete webhook payload with headers and body
|
|
395
|
-
*/
|
|
396
|
-
export function createWebhookPayload<T extends SendGridWebhookEvent>(
|
|
397
|
-
event: T,
|
|
398
|
-
headerOverrides?: Partial<SendGridWebhookHeaders>
|
|
399
|
-
): SendGridWebhookPayload {
|
|
400
|
-
return {
|
|
401
|
-
headers: createMockHeaders(headerOverrides),
|
|
402
|
-
body: event
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Create an array of webhook events (SendGrid often batches events)
|
|
408
|
-
*/
|
|
409
|
-
export function createBatchedEvents(
|
|
410
|
-
events: SendGridWebhookEvent[]
|
|
411
|
-
): { headers: SendGridWebhookHeaders; body: SendGridWebhookEvent[] } {
|
|
412
|
-
return {
|
|
413
|
-
headers: createMockHeaders(),
|
|
414
|
-
body: events
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// ============================================================================
|
|
419
|
-
// PRE-BUILT TEST SCENARIOS
|
|
420
|
-
// ============================================================================
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Complete email lifecycle: processed -> delivered -> open -> click
|
|
424
|
-
*/
|
|
425
|
-
export function createEmailLifecycleEvents(email: string = 'test@example.com'): SendGridWebhookEvent[] {
|
|
426
|
-
const messageId = generateMessageId();
|
|
427
|
-
const baseTimestamp = Math.floor(Date.now() / 1000);
|
|
428
|
-
|
|
429
|
-
return [
|
|
430
|
-
createDeliveredEvent({ email, sg_message_id: messageId, timestamp: baseTimestamp }),
|
|
431
|
-
createOpenEvent({ email, sg_message_id: messageId, timestamp: baseTimestamp + 60 }),
|
|
432
|
-
createClickEvent({ email, sg_message_id: messageId, timestamp: baseTimestamp + 120 })
|
|
433
|
-
];
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Failed delivery scenario: deferred -> deferred -> bounce
|
|
438
|
-
*/
|
|
439
|
-
export function createFailedDeliveryEvents(email: string = 'test@example.com'): SendGridWebhookEvent[] {
|
|
440
|
-
const messageId = generateMessageId();
|
|
441
|
-
const baseTimestamp = Math.floor(Date.now() / 1000);
|
|
442
|
-
|
|
443
|
-
return [
|
|
444
|
-
createDeferredEvent({ email, sg_message_id: messageId, attempt: '1', timestamp: baseTimestamp }),
|
|
445
|
-
createDeferredEvent({ email, sg_message_id: messageId, attempt: '2', timestamp: baseTimestamp + 300 }),
|
|
446
|
-
createBounceEvent({ email, sg_message_id: messageId, timestamp: baseTimestamp + 600, type: 'hard' })
|
|
447
|
-
];
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* Spam report scenario
|
|
452
|
-
*/
|
|
453
|
-
export function createSpamScenarioEvents(email: string = 'test@example.com'): SendGridWebhookEvent[] {
|
|
454
|
-
const messageId = generateMessageId();
|
|
455
|
-
const baseTimestamp = Math.floor(Date.now() / 1000);
|
|
456
|
-
|
|
457
|
-
return [
|
|
458
|
-
createDeliveredEvent({ email, sg_message_id: messageId, timestamp: baseTimestamp }),
|
|
459
|
-
createOpenEvent({ email, sg_message_id: messageId, timestamp: baseTimestamp + 30 }),
|
|
460
|
-
createSpamReportEvent({ email, sg_message_id: messageId, timestamp: baseTimestamp + 60 })
|
|
461
|
-
];
|
|
462
|
-
}
|
|
463
|
-
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { MailgunNormalizer } from '../../src/normalizers/MailgunNormalizer';
|
|
10
|
-
import { EmailEventType, EmailProvider, WebhookPayload } from '../../src/types';
|
|
11
|
-
|
|
12
|
-
describe('MailgunNormalizer', () => {
|
|
13
|
-
let normalizer: MailgunNormalizer;
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
normalizer = new MailgunNormalizer();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should normalize delivered event', () => {
|
|
20
|
-
const webhook: WebhookPayload = {
|
|
21
|
-
provider: EmailProvider.MAILGUN,
|
|
22
|
-
signature: 'test-signature',
|
|
23
|
-
payload: {
|
|
24
|
-
'event-data': {
|
|
25
|
-
event: 'delivered',
|
|
26
|
-
recipient: 'user@example.com',
|
|
27
|
-
timestamp: 1234567890,
|
|
28
|
-
id: 'msg-123',
|
|
29
|
-
message: {
|
|
30
|
-
headers: {
|
|
31
|
-
'message-id': 'email-456'
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
headers: {}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const result = normalizer.normalize(webhook);
|
|
40
|
-
|
|
41
|
-
expect(result.type).toBe(EmailEventType.DELIVERED);
|
|
42
|
-
expect(result.recipient).toBe('user@example.com');
|
|
43
|
-
expect(result.provider).toBe(EmailProvider.MAILGUN);
|
|
44
|
-
expect(result.messageId).toBe('msg-123');
|
|
45
|
-
expect(result.emailId).toBe('email-456');
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should normalize bounce event with geolocation', () => {
|
|
49
|
-
const webhook: WebhookPayload = {
|
|
50
|
-
provider: EmailProvider.MAILGUN,
|
|
51
|
-
signature: 'test-signature',
|
|
52
|
-
payload: {
|
|
53
|
-
'event-data': {
|
|
54
|
-
event: 'bounced',
|
|
55
|
-
recipient: 'user@example.com',
|
|
56
|
-
timestamp: 1234567890,
|
|
57
|
-
id: 'msg-123',
|
|
58
|
-
severity: 'permanent',
|
|
59
|
-
'delivery-status': {
|
|
60
|
-
message: 'User unknown',
|
|
61
|
-
code: 550
|
|
62
|
-
},
|
|
63
|
-
message: {
|
|
64
|
-
headers: {
|
|
65
|
-
'message-id': 'email-456'
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
geolocation: {
|
|
69
|
-
country: 'US',
|
|
70
|
-
region: 'CA',
|
|
71
|
-
city: 'San Francisco'
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
headers: {}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const result = normalizer.normalize(webhook);
|
|
79
|
-
|
|
80
|
-
expect(result.type).toBe(EmailEventType.BOUNCED);
|
|
81
|
-
expect(result.metadata.bounceType).toBe('hard');
|
|
82
|
-
expect(result.metadata.bounceReason).toBe('User unknown');
|
|
83
|
-
expect(result.metadata.bounceCode).toBe('550');
|
|
84
|
-
expect(result.metadata.location).toEqual({
|
|
85
|
-
country: 'US',
|
|
86
|
-
region: 'CA',
|
|
87
|
-
city: 'San Francisco'
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should normalize soft bounce', () => {
|
|
92
|
-
const webhook: WebhookPayload = {
|
|
93
|
-
provider: EmailProvider.MAILGUN,
|
|
94
|
-
signature: 'test-signature',
|
|
95
|
-
payload: {
|
|
96
|
-
'event-data': {
|
|
97
|
-
event: 'bounced',
|
|
98
|
-
recipient: 'user@example.com',
|
|
99
|
-
timestamp: 1234567890,
|
|
100
|
-
id: 'msg-123',
|
|
101
|
-
severity: 'temporary',
|
|
102
|
-
message: {
|
|
103
|
-
headers: {
|
|
104
|
-
'message-id': 'email-456'
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
headers: {}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const result = normalizer.normalize(webhook);
|
|
113
|
-
|
|
114
|
-
expect(result.metadata.bounceType).toBe('soft');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should normalize click event', () => {
|
|
118
|
-
const webhook: WebhookPayload = {
|
|
119
|
-
provider: EmailProvider.MAILGUN,
|
|
120
|
-
signature: 'test-signature',
|
|
121
|
-
payload: {
|
|
122
|
-
'event-data': {
|
|
123
|
-
event: 'clicked',
|
|
124
|
-
recipient: 'user@example.com',
|
|
125
|
-
timestamp: 1234567890,
|
|
126
|
-
id: 'msg-123',
|
|
127
|
-
url: 'https://example.com/link',
|
|
128
|
-
'client-info': {
|
|
129
|
-
'client-ip': '192.168.1.1',
|
|
130
|
-
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) mobile'
|
|
131
|
-
},
|
|
132
|
-
message: {
|
|
133
|
-
headers: {
|
|
134
|
-
'message-id': 'email-456'
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
headers: {}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
const result = normalizer.normalize(webhook);
|
|
143
|
-
|
|
144
|
-
expect(result.type).toBe(EmailEventType.CLICKED);
|
|
145
|
-
expect(result.metadata.url).toBe('https://example.com/link');
|
|
146
|
-
expect(result.metadata.ipAddress).toBe('192.168.1.1');
|
|
147
|
-
expect(result.metadata.deviceType).toBe('mobile');
|
|
148
|
-
});
|
|
149
|
-
});
|