webhook-model.test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import assert from 'node:assert/strict';
  2. import crypto from 'node:crypto';
  3. import { test } from 'node:test';
  4. import {
  5. WEBHOOK_EVENTS,
  6. TERMINAL_WEBHOOK_EVENTS,
  7. MAX_WEBHOOK_ATTEMPTS,
  8. WEBHOOK_LEASE_MS,
  9. eventTypeForStatus,
  10. resolveWebhooksForEvent,
  11. buildWebhookPayload,
  12. signWebhookBody,
  13. nextBackoffMs,
  14. isTerminalWebhookStatus,
  15. normalizeWebhookEvents,
  16. parseWebhookEventsJson
  17. } from '../src/webhook-model.js';
  18. test('maps terminal statuses to email.* types', () => {
  19. assert.equal(eventTypeForStatus('sent'), 'email.sent');
  20. assert.equal(eventTypeForStatus('bounced'), 'email.bounced');
  21. assert.equal(eventTypeForStatus('failed'), 'email.failed');
  22. assert.equal(eventTypeForStatus('opened'), 'email.opened');
  23. assert.equal(eventTypeForStatus('clicked'), 'email.clicked');
  24. assert.equal(eventTypeForStatus('queued'), null);
  25. assert.equal(eventTypeForStatus('deferred'), null);
  26. });
  27. test('supports opened and clicked subscriptions without making them delivery terminal statuses', () => {
  28. assert.deepEqual(WEBHOOK_EVENTS, ['sent', 'bounced', 'failed', 'opened', 'clicked']);
  29. assert.equal(isTerminalWebhookStatus('opened'), false);
  30. assert.equal(isTerminalWebhookStatus('clicked'), false);
  31. assert.deepEqual(normalizeWebhookEvents(['clicked', 'opened', 'sent']), ['sent', 'opened', 'clicked']);
  32. });
  33. test('isTerminalWebhookStatus matches terminal set', () => {
  34. assert.equal(isTerminalWebhookStatus('sent'), true);
  35. assert.equal(isTerminalWebhookStatus('bounced'), true);
  36. assert.equal(isTerminalWebhookStatus('failed'), true);
  37. assert.equal(isTerminalWebhookStatus('queued'), false);
  38. assert.equal(isTerminalWebhookStatus('processing'), false);
  39. assert.deepEqual(TERMINAL_WEBHOOK_EVENTS, ['sent', 'bounced', 'failed']);
  40. assert.equal(MAX_WEBHOOK_ATTEMPTS, 8);
  41. assert.equal(WEBHOOK_LEASE_MS, 2 * 60 * 1000);
  42. });
  43. test('domain webhooks override account for the same event', () => {
  44. const account = [
  45. { id: 1, domainId: null, enabled: true, events: ['sent', 'failed'] },
  46. { id: 2, domainId: null, enabled: true, events: ['bounced'] }
  47. ];
  48. const domain = [
  49. { id: 3, domainId: 9, enabled: true, events: ['sent'] }
  50. ];
  51. const resolved = resolveWebhooksForEvent({
  52. accountWebhooks: account,
  53. domainWebhooks: domain,
  54. eventType: 'sent'
  55. });
  56. assert.deepEqual(resolved.map((w) => w.id), [3]);
  57. });
  58. test('falls back to account when domain has no matching enabled subscription', () => {
  59. const resolved = resolveWebhooksForEvent({
  60. accountWebhooks: [{ id: 1, domainId: null, enabled: true, events: ['failed'] }],
  61. domainWebhooks: [{ id: 3, domainId: 9, enabled: true, events: ['sent'] }],
  62. eventType: 'failed'
  63. });
  64. assert.deepEqual(resolved.map((w) => w.id), [1]);
  65. });
  66. test('skips disabled webhooks and unsubscribed events', () => {
  67. const resolved = resolveWebhooksForEvent({
  68. accountWebhooks: [
  69. { id: 1, domainId: null, enabled: false, events: ['sent'] },
  70. { id: 2, domainId: null, enabled: 'false', events: ['sent'] },
  71. { id: 3, domainId: null, enabled: true, events: ['bounced'] },
  72. { id: 4, domainId: null, enabled: true, events: ['sent'] }
  73. ],
  74. domainWebhooks: [],
  75. eventType: 'sent'
  76. });
  77. assert.deepEqual(resolved.map((w) => w.id), [4]);
  78. });
  79. test('builds webhook payload for real and test deliveries', () => {
  80. const real = buildWebhookPayload({
  81. deliveryId: 42,
  82. eventType: 'email.sent',
  83. createdAt: '2026-07-09T12:00:00.000Z',
  84. sendEvent: {
  85. id: 7,
  86. status: 'sent',
  87. queueId: 'A1B2C3',
  88. domain: 'example.com',
  89. sender: 'noreply@example.com',
  90. recipients: ['user@example.com'],
  91. subject: 'Hello',
  92. detail: 'ok',
  93. deliveredAt: '2026-07-09T12:00:01.000Z'
  94. }
  95. });
  96. assert.equal(real.id, 'whd_42');
  97. assert.equal(real.type, 'email.sent');
  98. assert.equal(real.created_at, '2026-07-09T12:00:00.000Z');
  99. assert.equal(real.data.message_id, 'mh-7');
  100. assert.equal(real.data.send_event_id, 7);
  101. assert.equal(real.data.queue_id, 'A1B2C3');
  102. assert.equal(real.data.test, undefined);
  103. const synthetic = buildWebhookPayload({
  104. deliveryId: 1,
  105. eventType: 'email.failed',
  106. createdAt: '2026-07-09T12:00:00.000Z',
  107. sendEvent: {
  108. id: 0,
  109. status: 'failed',
  110. domain: 'example.com',
  111. sender: 'noreply@example.com',
  112. recipients: ['user@example.com'],
  113. subject: 'Test'
  114. },
  115. test: true
  116. });
  117. assert.equal(synthetic.data.test, true);
  118. assert.equal(synthetic.data.message_id, 'mh-test');
  119. assert.equal(synthetic.data.send_event_id, 0);
  120. assert.equal(synthetic.type, 'email.failed');
  121. });
  122. test('builds private engagement webhook payloads without full click destinations', () => {
  123. const payload = buildWebhookPayload({
  124. deliveryId: 51,
  125. eventType: 'email.clicked',
  126. createdAt: '2026-07-09T12:00:00.000Z',
  127. sendEvent: {
  128. id: 8,
  129. status: 'sent',
  130. domain: 'example.com',
  131. sender: 'noreply@example.com',
  132. recipients: ['reader@example.net'],
  133. subject: 'Tracked'
  134. },
  135. engagement: {
  136. type: 'click',
  137. occurredAt: '2026-07-09T12:00:00.000Z',
  138. source: 'direct',
  139. linkId: 4,
  140. targetOrigin: 'https://example.net'
  141. }
  142. });
  143. assert.equal(payload.type, 'email.clicked');
  144. assert.deepEqual(payload.data.engagement, {
  145. type: 'click',
  146. occurred_at: '2026-07-09T12:00:00.000Z',
  147. source: 'direct',
  148. link_id: 4,
  149. target_origin: 'https://example.net'
  150. });
  151. assert.equal(JSON.stringify(payload).includes('token='), false);
  152. });
  153. test('signs body with Stripe-style t and v1', () => {
  154. const body = '{"id":"whd_1"}';
  155. const secret = 'secret';
  156. const t = 1_700_000_000;
  157. const header = signWebhookBody(body, secret, t);
  158. assert.equal(header.startsWith('t=1700000000,v1='), true);
  159. assert.match(header, /^t=\d+,v1=[0-9a-f]{64}$/);
  160. const expected = crypto.createHmac('sha256', secret).update(`${t}.${body}`).digest('hex');
  161. assert.equal(header, `t=${t},v1=${expected}`);
  162. });
  163. test('backoff grows then caps', () => {
  164. assert.equal(nextBackoffMs(1), 60_000);
  165. assert.equal(nextBackoffMs(2), 300_000);
  166. assert.equal(nextBackoffMs(3), 1_800_000);
  167. assert.equal(nextBackoffMs(4), 7_200_000);
  168. assert.equal(nextBackoffMs(5), 21_600_000);
  169. assert.equal(nextBackoffMs(6), 43_200_000);
  170. assert.ok(nextBackoffMs(1) < nextBackoffMs(2));
  171. assert.equal(nextBackoffMs(6), nextBackoffMs(7));
  172. assert.equal(nextBackoffMs(10), nextBackoffMs(20));
  173. });
  174. test('normalizeWebhookEvents accepts non-empty subset of terminal events', () => {
  175. assert.deepEqual(normalizeWebhookEvents(['failed', 'sent', 'sent']), ['sent', 'failed']);
  176. assert.deepEqual(normalizeWebhookEvents(['bounced']), ['bounced']);
  177. assert.throws(() => normalizeWebhookEvents([]), /events/i);
  178. assert.throws(() => normalizeWebhookEvents(['queued']), /events/i);
  179. assert.throws(() => normalizeWebhookEvents(null), /events/i);
  180. });
  181. test('parseWebhookEventsJson parses JSON array of events', () => {
  182. assert.deepEqual(parseWebhookEventsJson('["sent","bounced"]'), ['sent', 'bounced']);
  183. assert.throws(() => parseWebhookEventsJson('not-json'), /events/i);
  184. assert.throws(() => parseWebhookEventsJson('[]'), /events/i);
  185. });