/ tests / inbound / inbound-processor-errors.test.js
inbound-processor-errors.test.js
 1  /**
 2   * Tests for Inbound Processor - pollAllChannels and processAllReplies error paths
 3   * Uses mock.module to simulate SMS/Email handler failures
 4   */
 5  
 6  import { test, mock } from 'node:test';
 7  import assert from 'node:assert/strict';
 8  
 9  // Mock SMS and email handlers to simulate errors BEFORE importing processor
10  mock.module('../../src/inbound/sms.js', {
11    namedExports: {
12      pollInboundSMS: async () => {
13        throw new Error('SMS connection failed');
14      },
15      processPendingReplies: async () => {
16        throw new Error('SMS replies failed');
17      },
18    },
19  });
20  
21  mock.module('../../src/inbound/email.js', {
22    namedExports: {
23      pollInboundEmails: async () => {
24        throw new Error('Email connection failed');
25      },
26      processPendingReplies: async () => {
27        throw new Error('Email replies failed');
28      },
29    },
30  });
31  
32  const { pollAllChannels, processAllReplies } = await import('../../src/inbound/processor.js');
33  
34  test('pollAllChannels: returns default values when SMS handler throws', async () => {
35    const results = await pollAllChannels();
36    assert.ok(typeof results === 'object', 'Should return object even on error');
37    assert.ok('sms' in results, 'Should have sms key');
38    assert.ok('email' in results, 'Should have email key');
39  });
40  
41  test('pollAllChannels: sms result has zero counts on error', async () => {
42    const results = await pollAllChannels();
43    // When handlers throw, the error is caught and defaults remain
44    assert.ok(typeof results.sms === 'object');
45  });
46  
47  test('pollAllChannels: email result has zero counts on error', async () => {
48    const results = await pollAllChannels();
49    assert.ok(typeof results.email === 'object');
50  });
51  
52  test('pollAllChannels: does not throw when both handlers fail', async () => {
53    await assert.doesNotReject(() => pollAllChannels());
54  });
55  
56  test('processAllReplies: returns default values when SMS handler throws', async () => {
57    const results = await processAllReplies();
58    assert.ok(typeof results === 'object');
59    assert.ok('sms' in results);
60    assert.ok('email' in results);
61  });
62  
63  test('processAllReplies: does not throw when both handlers fail', async () => {
64    await assert.doesNotReject(() => processAllReplies());
65  });
66  
67  test('processAllReplies: sms result is object on error', async () => {
68    const results = await processAllReplies();
69    assert.ok(typeof results.sms === 'object');
70  });
71  
72  test('processAllReplies: email result is object on error', async () => {
73    const results = await processAllReplies();
74    assert.ok(typeof results.email === 'object');
75  });