|
| 1 | +const { assert } = require('chai'); |
| 2 | +const { RTMClient } = require('../src/RTMClient'); |
| 3 | +const { LogLevel } = require('../src/logger'); |
| 4 | +const { 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 | +describe('Integration tests with a WebSocket server', () => { |
| 21 | + beforeEach(() => { |
| 22 | + server = createServer((req, res) => { |
| 23 | + res.writeHead(200, { 'content-type': 'application/json' }); |
| 24 | + res.end(JSON.stringify({ |
| 25 | + ok: true, |
| 26 | + url: `ws://localhost:${WSS_PORT}/`, |
| 27 | + self: { id: 'elclassico' }, |
| 28 | + team: { id: 'T12345' }, |
| 29 | + })); |
| 30 | + }); |
| 31 | + server.listen(HTTP_PORT); |
| 32 | + wss = new WebSocketServer({ port: WSS_PORT }); |
| 33 | + wss.on('connection', (ws) => { |
| 34 | + ws.on('error', (err) => { |
| 35 | + assert.fail(err); |
| 36 | + }); |
| 37 | + // Send `Event.ServerHello` |
| 38 | + ws.send(JSON.stringify({type: 'hello'})); |
| 39 | + exposed_ws_connection = ws; |
| 40 | + }); |
| 41 | + }); |
| 42 | + afterEach(() => { |
| 43 | + server.close(); |
| 44 | + server = null; |
| 45 | + wss.close(); |
| 46 | + wss = null; |
| 47 | + exposed_ws_connection = null; |
| 48 | + client = null; |
| 49 | + }); |
| 50 | + describe('establishing connection, receiving valid messages', () => { |
| 51 | + beforeEach(() => { |
| 52 | + client = new RTMClient('token', { |
| 53 | + slackApiUrl: `http://localhost:${HTTP_PORT}/`, |
| 54 | + logLevel: LogLevel.DEBUG, |
| 55 | + }); |
| 56 | + }); |
| 57 | + it('connects to a server via `start()` and gracefully disconnects via `disconnect()`', async () => { |
| 58 | + await client.start(); |
| 59 | + await sleep(50); // TODO: this is due to `start()` resolving once the authenticated event is raised |
| 60 | + // however, the handshake on the WS side still needs to complete at this point, so calling disconnect() |
| 61 | + // will raise an error. |
| 62 | + await client.disconnect(); |
| 63 | + }); |
| 64 | + it('can listen on slack event types and receive payload properties', async () => { |
| 65 | + client.on('connected', () => { |
| 66 | + exposed_ws_connection.send(JSON.stringify({ |
| 67 | + type: 'team_member_joined', |
| 68 | + envelope_id: 12345, |
| 69 | + })); |
| 70 | + }); |
| 71 | + await client.start(); |
| 72 | + await new Promise((res, _rej) => { |
| 73 | + client.on('team_member_joined', (evt) => { |
| 74 | + assert.equal(evt.envelope_id, 12345); |
| 75 | + res(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + await client.disconnect(); |
| 79 | + }); |
| 80 | + it('should not raise an exception if calling disconnect() when already disconnected', async () => { |
| 81 | + // https://github.com/slackapi/node-slack-sdk/issues/842 |
| 82 | + await client.disconnect(); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +function sleep(ms) { |
| 88 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 89 | +} |
0 commit comments