Skip to content

Commit

Permalink
socket-mode(fix): do not handle message events twice (#2059)
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj authored Oct 2, 2024
1 parent de54897 commit 9d2fa58
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
4 changes: 1 addition & 3 deletions packages/socket-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
],
"main": "dist/src/index.js",
"types": "./dist/src/index.d.ts",
"files": [
"dist/**/*"
],
"files": ["dist/**/*"],
"engines": {
"node": ">= 18",
"npm": ">= 8.6.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/socket-mode/src/SlackWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class SlackWebSocket {
this.options.client.emit('error', websocketErrorWithOriginal(event.error));
});
ws.on('message', (msg, isBinary) => {
this.options.client.emit('message', msg, isBinary);
this.options.client.emit('ws_message', msg, isBinary);
});
ws.on('close', (code: number, data: Buffer) => {
this.logger.debug(`WebSocket close frame received (code: ${code}, reason: ${data.toString()})`);
Expand Down
80 changes: 70 additions & 10 deletions packages/socket-mode/src/SocketModeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('SocketModeClient', () => {
args.retry_num === undefined &&
args.retry_reason === undefined;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', message, false /* isBinary */);
await sleep(30);
assert.isTrue(commandListenerCalled);
assert.isTrue(slackEventListenerCalled);
Expand All @@ -89,7 +89,7 @@ describe('SocketModeClient', () => {
client.on('slash_commands', async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', message, false /* isBinary */);
await sleep(30);
assert.equal(passedEnvelopeId, envelopeId);
});
Expand All @@ -99,15 +99,15 @@ describe('SocketModeClient', () => {
client.on('slack_event', async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', message, false /* isBinary */);
await sleep(30);
assert.equal(passedEnvelopeId, envelopeId);
});
});

describe('events_api messages', () => {
const envelopeId = 'cda4159a-72a5-4744-aba3-4d66eb52682b';
const message = JSON.stringify({
const appMention = JSON.stringify({
envelope_id: envelopeId,
payload: {
token: 'verification-token',
Expand Down Expand Up @@ -160,6 +160,59 @@ describe('SocketModeClient', () => {
retry_attempt: 2,
retry_reason: 'timeout',
});
const message = JSON.stringify({
envelope_id: envelopeId,
payload: {
token: 'verification-token',
team_id: 'T111',
api_app_id: 'A111',
event: {
client_msg_id: 'f0582a78-72db-4feb-b2f3-1e47d66365c8',
type: 'message',
text: '<@U111>',
user: 'U222',
ts: '1610241741.000200',
team: 'T111',
blocks: [
{
type: 'rich_text',
block_id: 'Sesm',
elements: [
{
type: 'rich_text_section',
elements: [
{
type: 'user',
user_id: 'U111',
},
],
},
],
},
],
channel: 'C111',
event_ts: '1610241741.000200',
},
type: 'event_callback',
event_id: 'Ev111',
event_time: 1610241741,
authorizations: [
{
enterprise_id: null,
team_id: 'T111',
user_id: 'U222',
is_bot: true,
is_enterprise_install: false,
},
],
is_ext_shared_channel: false,
event_context: '1-app_mention-T111-C111',
},
type: 'events_api',
accepts_response_payload: false,
retry_attempt: 2,
retry_reason: 'timeout',
});

it('should be sent to the specific and generic event listeners, and should not trip an unrelated event listener', async () => {
const client = new SocketModeClient({ appToken: 'xapp-' });
Expand All @@ -183,7 +236,7 @@ describe('SocketModeClient', () => {
args.retry_num === 2 &&
args.retry_reason === 'timeout';
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', appMention, false /* isBinary */);
await sleep(30);
assert.isFalse(otherListenerCalled);
assert.isTrue(eventsApiListenerCalled);
Expand All @@ -196,7 +249,7 @@ describe('SocketModeClient', () => {
client.on('app_mention', async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', appMention, false /* isBinary */);
await sleep(30);
assert.equal(passedEnvelopeId, envelopeId);
});
Expand All @@ -206,10 +259,17 @@ describe('SocketModeClient', () => {
client.on('slack_event', async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', appMention, false /* isBinary */);
await sleep(30);
assert.equal(passedEnvelopeId, envelopeId);
});
it('should process message events once', async () => {
const client = new SocketModeClient({ appToken: 'xapp-' });
const spy = sinon.spy();
client.on('message', spy);
client.emit('ws_message', message, false /* isBinary */);
sinon.assert.callCount(spy, 1);
});
});

describe('interactivity messages', () => {
Expand Down Expand Up @@ -252,7 +312,7 @@ describe('SocketModeClient', () => {
client.on('slack_event', async (args) => {
slackEventListenerCalled = args.ack !== undefined && args.body !== undefined;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', message, false /* isBinary */);
await sleep(30);
assert.isFalse(otherListenerCalled);
assert.isTrue(interactiveListenerCalled);
Expand All @@ -265,7 +325,7 @@ describe('SocketModeClient', () => {
client.on('interactive', async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', message, false /* isBinary */);
await sleep(30);
assert.equal(passedEnvelopeId, envelopeId);
});
Expand All @@ -275,7 +335,7 @@ describe('SocketModeClient', () => {
client.on('slack_event', async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
client.emit('message', message, false /* isBinary */);
client.emit('ws_message', message, false /* isBinary */);
await sleep(30);
assert.equal(passedEnvelopeId, envelopeId);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/socket-mode/src/SocketModeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class SocketModeClient extends EventEmitter {
this.emit(State.Disconnected);
}
});
this.on('message', this.onWebSocketMessage.bind(this));
this.on('ws_message', this.onWebSocketMessage.bind(this));
this.logger.debug('The Socket Mode client has successfully initialized');
}

Expand Down
8 changes: 2 additions & 6 deletions packages/socket-mode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
"sourceMap": true
},
"extends": "@tsconfig/recommended/tsconfig.json",
"include": [
"src/**/*"
],
"exclude": [
"src/**/*.spec.*"
],
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.*"],
"jsdoc": {
"out": "support/jsdoc",
"access": "public"
Expand Down

0 comments on commit 9d2fa58

Please sign in to comment.