Skip to content

Commit

Permalink
sushi-bot: Add reaction to add x-logo on 'twitter'
Browse files Browse the repository at this point in the history
  • Loading branch information
hakatashi committed Jul 31, 2023
1 parent ece7962 commit 2d83201
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
49 changes: 46 additions & 3 deletions lib/slackMock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
/* eslint-env node, jest */

const EventEmitter = require('events');
const noop = require('lodash/noop');
const last = require('lodash/last');

// https://jestjs.io/docs/mock-function-api
const mockMethodCalls = [
'mockImplementation',
'mockImplementationOnce',
'mockReturnThis',
'mockReturnValue',
'mockReturnValueOnce',
'mockResolvedValue',
'mockResolvedValueOnce',
'mockRejectedValue',
'mockRejectedValueOnce',
'mockRestore',
'mockClear',
'mockReset',
'mockName',
];

const createWebClient = (callback) => {
const createWebClient = (fallbackFn, registeredMocks) => {
const handler = (stack) => {
return new Proxy(
(...args) => callback(stack, ...args),
(...args) => {
const path = stack.join('.');
if (registeredMocks.has(path)) {
return registeredMocks.get(path)(...args);
}
if (mockMethodCalls.includes(last(stack))) {
const mock = jest.fn();
registeredMocks.set(stack.slice(0, -1).join('.'), mock);
return mock[last(stack)](...args);
}
return fallbackFn(stack, ...args)
},
{
get: (name, property, receiver) => {
const path = [...stack, property].join('.');
if (registeredMocks.has(path)) {
return registeredMocks.get(path);
}
if (typeof property === 'string' && property !== 'then') {
return handler([...stack, property])
}
Expand All @@ -26,7 +61,8 @@ module.exports = class SlackMock extends EventEmitter {
this.fakeUser = 'U00000000';
this.fakeTimestamp = '1234567890.123456';
this.eventClient = new EventEmitter();
this.webClient = createWebClient((...args) => this.handleWebcall(...args));
this.registeredMocks = new Map();
this.webClient = createWebClient((...args) => this.handleWebcall(...args), this.registeredMocks);
this.messageClient = {
action: noop,
viewSubmission: noop,
Expand Down Expand Up @@ -95,4 +131,11 @@ module.exports = class SlackMock extends EventEmitter {
this.postMessage(message, options);
return res;
}

// Not recommended. Instanciate a new SlackMock instead.
reset() {
this.eventClient.removeAllListeners();
this.removeAllListeners();
this.registeredMocks.clear();
}
};
46 changes: 46 additions & 0 deletions sushi-bot/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,49 @@ it('reacts to "エクササイズランキング 確認"', () => new Promise((re
})();
}));

it('reacts to "twitter" with :x-logo:', () => new Promise((resolve) => {
slack.on('reactions.add', ({name, channel, timestamp}) => {
expect(name).toBe('x-logo');
expect(channel).toBe(slack.fakeChannel);
expect(timestamp).toBe(slack.fakeTimestamp);
resolve();
});

slack.eventClient.emit('message', {
channel: slack.fakeChannel,
text: '私のtwitterアカウントは@hakatashiです',
user: slack.fakeUser,
ts: slack.fakeTimestamp,
});
}));

it('reacts to "twitter" with :x-logo: case-insensitively', () => new Promise((resolve) => {
slack.on('reactions.add', ({name, channel, timestamp}) => {
expect(name).toBe('x-logo');
expect(channel).toBe(slack.fakeChannel);
expect(timestamp).toBe(slack.fakeTimestamp);
resolve();
});

slack.eventClient.emit('message', {
channel: slack.fakeChannel,
text: '私のTwIttERアカウントは@hakatashiです',
user: slack.fakeUser,
ts: slack.fakeTimestamp,
});
}));

it('does not react to "twitter.com"', async () => {
slack.webClient.reactions.add.mockReturnValue(null);

slack.eventClient.emit('message', {
channel: slack.fakeChannel,
text: '<https://twitter.com/hakatashi/status/1539187440476246017>',
user: slack.fakeUser,
ts: slack.fakeTimestamp,
});

await new Promise((resolve) => process.nextTick(resolve));

expect(slack.webClient.reactions.add).not.toHaveBeenCalled();
});
6 changes: 6 additions & 0 deletions sushi-bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ export default async function ({eventClient, webClient: slack}: SlackInterface)
}
}
}

{
if (text.match(/twitter(?!\.com)/i)) {
slack.reactions.add({name: 'x-logo', channel, timestamp})
}
}
});

schedule.scheduleJob('0 19 * * *', async (date) => {
Expand Down

0 comments on commit 2d83201

Please sign in to comment.