|
| 1 | +const { assert } = require('chai'); |
| 2 | +const { SocketModeClient} = require('../src/SocketModeClient'); |
| 3 | +const { LogLevel } = require('../src/logger'); |
| 4 | +const { Server: WebSocketServer } = require('ws'); |
| 5 | +const { createServer } = require('http'); |
| 6 | +const sinon = require('sinon'); |
| 7 | + |
| 8 | +const HTTP_PORT = 12345; |
| 9 | +const WSS_PORT = 23456; |
| 10 | +// Basic HTTP server to 'fake' behaviour of `apps.connections.open` endpoint |
| 11 | +let server = null; |
| 12 | + |
| 13 | +// Basic WS server to fake slack WS endpoint |
| 14 | +let wss = null; |
| 15 | +let exposed_ws_connection = null; |
| 16 | + |
| 17 | +// Socket mode client pointing to the above two posers |
| 18 | +let client = null; |
| 19 | + |
| 20 | + |
| 21 | +const DISCONNECT_REASONS = ['warning', 'refresh_requested', 'too_many_websockets']; |
| 22 | + |
| 23 | +describe('Integration tests with a WebSocket server', () => { |
| 24 | + beforeEach(() => { |
| 25 | + server = createServer((_req, res) => { |
| 26 | + res.writeHead(200, { 'content-type': 'application/json' }); |
| 27 | + res.end(JSON.stringify({ |
| 28 | + ok: true, |
| 29 | + url: `ws://localhost:${WSS_PORT}/`, |
| 30 | + })); |
| 31 | + }); |
| 32 | + server.listen(HTTP_PORT); |
| 33 | + wss = new WebSocketServer({ port: WSS_PORT }); |
| 34 | + wss.on('connection', (ws) => { |
| 35 | + ws.on('error', (err) => { |
| 36 | + assert.fail(err); |
| 37 | + }); |
| 38 | + // Send `Event.ServerHello` |
| 39 | + ws.send(JSON.stringify({type: 'hello'})); |
| 40 | + exposed_ws_connection = ws; |
| 41 | + }); |
| 42 | + }); |
| 43 | + afterEach(() => { |
| 44 | + server.close(); |
| 45 | + server = null; |
| 46 | + wss.close(); |
| 47 | + wss = null; |
| 48 | + exposed_ws_connection = null; |
| 49 | + client = null; |
| 50 | + }); |
| 51 | + describe('establishing connection, receiving valid messages', () => { |
| 52 | + beforeEach(() => { |
| 53 | + client = new SocketModeClient({ appToken: 'whatever', logLevel: LogLevel.ERROR, clientOptions: { |
| 54 | + slackApiUrl: `http://localhost:${HTTP_PORT}/` |
| 55 | + }}); |
| 56 | + }); |
| 57 | + it('connects to a server via `start()` and gracefully disconnects via `disconnect()`', async () => { |
| 58 | + const connected = new Promise((res) => client.once('connected', res)); // due to legacy reasons, await start() does not wait for Connected state, so add additional check here for it |
| 59 | + await client.start(); |
| 60 | + await connected; |
| 61 | + await client.disconnect(); |
| 62 | + }); |
| 63 | + it('can call `disconnect()` even if already disconnected without issue', async () => { |
| 64 | + await client.disconnect(); |
| 65 | + }); |
| 66 | + it('can listen on random event types and receive payload properties', async () => { |
| 67 | + client.on('connected', () => { |
| 68 | + exposed_ws_connection.send(JSON.stringify({ |
| 69 | + type: 'integration-test', |
| 70 | + envelope_id: 12345, |
| 71 | + })); |
| 72 | + }); |
| 73 | + await client.start(); |
| 74 | + await new Promise((res, _rej) => { |
| 75 | + client.on('integration-test', (evt) => { |
| 76 | + assert.equal(evt.envelope_id, 12345); |
| 77 | + res(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + await client.disconnect(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + describe('catastrophic server behaviour', () => { |
| 84 | + beforeEach(() => { |
| 85 | + client = new SocketModeClient({ appToken: 'whatever', logLevel: LogLevel.ERROR, clientOptions: { |
| 86 | + slackApiUrl: `http://localhost:${HTTP_PORT}/` |
| 87 | + }, clientPingTimeout: 25}); |
| 88 | + }); |
| 89 | + it('should retry if retrieving a WSS URL fails', async () => { |
| 90 | + // Shut down the main WS-endpoint-retrieval server - we will customize its behaviour for this test |
| 91 | + server.close(); |
| 92 | + let num_attempts = 0; |
| 93 | + server = createServer((_req, res) => { |
| 94 | + num_attempts += 1; |
| 95 | + res.writeHead(200, { 'content-type': 'application/json' }); |
| 96 | + if (num_attempts < 3) { |
| 97 | + res.end(JSON.stringify({ |
| 98 | + ok: false, |
| 99 | + error: "fatal_error", |
| 100 | + })); |
| 101 | + } else { |
| 102 | + res.end(JSON.stringify({ |
| 103 | + ok: true, |
| 104 | + url: `ws://localhost:${WSS_PORT}/`, |
| 105 | + })); |
| 106 | + } |
| 107 | + }); |
| 108 | + server.listen(HTTP_PORT); |
| 109 | + await client.start(); |
| 110 | + assert.equal(num_attempts, 3); |
| 111 | + await client.disconnect(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + describe('failure modes / unexpected messages sent to client', () => { |
| 115 | + let loggerSpy = sinon.stub(); |
| 116 | + const noop = () => {}; |
| 117 | + beforeEach(() => { |
| 118 | + client = new SocketModeClient({ appToken: 'whatever', clientOptions: { |
| 119 | + slackApiUrl: `http://localhost:${HTTP_PORT}/` |
| 120 | + }, logger: { |
| 121 | + debug: noop, |
| 122 | + info: noop, |
| 123 | + error: loggerSpy, |
| 124 | + getLevel: () => 'error', |
| 125 | + }}); |
| 126 | + }); |
| 127 | + afterEach(() => { |
| 128 | + loggerSpy.resetHistory(); |
| 129 | + }); |
| 130 | + it('should error-log that a malformed JSON message was received', async () => { |
| 131 | + client.on('connected', () => { |
| 132 | + exposed_ws_connection.send(''); |
| 133 | + }); |
| 134 | + await client.start(); |
| 135 | + await sleep(10); |
| 136 | + assert.isTrue(loggerSpy.calledWith(sinon.match('Unable to parse an incoming WebSocket message'))); |
| 137 | + await client.disconnect(); |
| 138 | + }); |
| 139 | + }); |
| 140 | + describe('lifecycle events', () => { |
| 141 | + beforeEach(() => { |
| 142 | + client = new SocketModeClient({ appToken: 'whatever', logLevel: LogLevel.ERROR, clientOptions: { |
| 143 | + slackApiUrl: `http://localhost:${HTTP_PORT}/` |
| 144 | + }}); |
| 145 | + }); |
| 146 | + it('raises connecting event during `start()`', async () => { |
| 147 | + let raised = false; |
| 148 | + client.on('connecting', () => { raised = true; }); |
| 149 | + const connected = new Promise((res) => client.once('connected', res)); // due to legacy reasons, await start() does not wait for Connected state, so add additional check here for it |
| 150 | + await client.start(); |
| 151 | + await connected; |
| 152 | + assert.isTrue(raised); |
| 153 | + await client.disconnect(); |
| 154 | + }); |
| 155 | + it('raises authenticated event during `start()`', async () => { |
| 156 | + let raised = false; |
| 157 | + client.on('authenticated', () => { raised = true; }); |
| 158 | + const connected = new Promise((res) => client.once('connected', res)); // due to legacy reasons, await start() does not wait for Connected state, so add additional check here for it |
| 159 | + await client.start(); |
| 160 | + await connected; |
| 161 | + assert.isTrue(raised); |
| 162 | + await client.disconnect(); |
| 163 | + }); |
| 164 | + it('raises disconnecting event during `disconnect()`', async () => { |
| 165 | + let raised = false; |
| 166 | + const connected = new Promise((res) => client.once('connected', res)); // due to legacy reasons, await start() does not wait for Connected state, so add additional check here for it |
| 167 | + client.on('disconnecting', () => { raised = true; }); |
| 168 | + await client.start(); |
| 169 | + await connected; |
| 170 | + await client.disconnect(); |
| 171 | + assert.isTrue(raised); |
| 172 | + }); |
| 173 | + it('raises disconnected event after `disconnect()`', async () => { |
| 174 | + let raised = false; |
| 175 | + const connected = new Promise((res) => client.once('connected', res)); // due to legacy reasons, await start() does not wait for Connected state, so add additional check here for it |
| 176 | + client.on('disconnected', () => { raised = true; }); |
| 177 | + await client.start(); |
| 178 | + await connected; |
| 179 | + await client.disconnect(); |
| 180 | + assert.isTrue(raised); |
| 181 | + }); |
| 182 | + describe('slack_event', () => { |
| 183 | + beforeEach(() => { |
| 184 | + // Disable auto reconnect for these tests |
| 185 | + client = new SocketModeClient({ appToken: 'whatever', logLevel: LogLevel.ERROR, autoReconnectEnabled: false, clientOptions: { |
| 186 | + slackApiUrl: `http://localhost:${HTTP_PORT}/` |
| 187 | + }}); |
| 188 | + }); |
| 189 | + afterEach(async () => { |
| 190 | + await client.disconnect(); |
| 191 | + }); |
| 192 | + // These tests check that specific type:disconnect events, of various reasons, sent by Slack backend are not raised as slack_events for apps to consume. |
| 193 | + DISCONNECT_REASONS.forEach((reason) => { |
| 194 | + it(`should not raise a type:disconnect reason:${reason} message as a slack_event`, async () => { |
| 195 | + let raised = false; |
| 196 | + client.on('connected', () => { |
| 197 | + exposed_ws_connection.send(JSON.stringify({type:'disconnect', reason})); |
| 198 | + }); |
| 199 | + client.on('slack_event', () => { |
| 200 | + raised = true; |
| 201 | + }); |
| 202 | + await client.start(); |
| 203 | + await sleep(10); |
| 204 | + assert.isFalse(raised); |
| 205 | + }); |
| 206 | + }); |
| 207 | + }); |
| 208 | + describe('including reconnection ability', () => { |
| 209 | + it('raises reconnecting event after peer disconnects underlying WS connection', async () => { |
| 210 | + const reconnectingWaiter = new Promise((res) => client.on('reconnecting', res)); |
| 211 | + const connected = new Promise((res) => client.once('connected', res)); // due to legacy reasons, await start() does not wait for Connected state, so add additional check here for it |
| 212 | + await client.start(); |
| 213 | + await connected; |
| 214 | + // force a WS disconnect from the server |
| 215 | + exposed_ws_connection.terminate(); |
| 216 | + // create another waiter for post-reconnect connected event |
| 217 | + const reconnectedWaiter = new Promise((res) => client.on('connected', res)); |
| 218 | + // if we pass the point where the reconnectingWaiter succeeded, then we have verified the reconnecting event is raised |
| 219 | + // and this test can be considered passing. if we time out here, then that is an indication of a failure. |
| 220 | + await reconnectingWaiter; |
| 221 | + await reconnectedWaiter; // wait for this to ensure we dont raise an unexpected error by calling `disconnect` mid-reconnect. |
| 222 | + await client.disconnect(); |
| 223 | + }); |
| 224 | + DISCONNECT_REASONS.forEach((reason) => { |
| 225 | + it(`should reconnect gracefully if server sends a disconnect (reason: ${reason}) message`, async () => { |
| 226 | + const connected = new Promise((res) => client.once('connected', res)); // due to legacy reasons, await start() does not wait for Connected state, so add additional check here for it |
| 227 | + await client.start(); |
| 228 | + await connected; |
| 229 | + // force a WS disconnect from the server |
| 230 | + exposed_ws_connection.send(JSON.stringify({type:"disconnect", reason})); |
| 231 | + // create a waiter for post-reconnect connected event |
| 232 | + const reconnectedWaiter = new Promise((res) => client.on('connected', res)); |
| 233 | + // if we pass the point where the reconnectedWaiter succeeded, then we have verified the reconnection succeeded |
| 234 | + // and this test can be considered passing. if we time out here, then that is an indication of a failure. |
| 235 | + await reconnectedWaiter; |
| 236 | + await client.disconnect(); |
| 237 | + }); |
| 238 | + }); |
| 239 | + }); |
| 240 | + }); |
| 241 | +}); |
| 242 | + |
| 243 | +function sleep(ms) { |
| 244 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 245 | +} |
0 commit comments