-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathbot-events.test.js
More file actions
463 lines (420 loc) · 19.7 KB
/
Copy pathbot-events.test.js
File metadata and controls
463 lines (420 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* eslint-disable no-unused-expressions, prefer-arrow-callback */
import chai from 'chai';
import sinonChai from 'sinon-chai';
import sinon from 'sinon';
import irc from 'irc-upd';
import discord from 'discord.js';
import Bot from '../lib/bot';
import logger from '../lib/logger';
import createDiscordStub from './stubs/discord-stub';
import ClientStub from './stubs/irc-client-stub';
import config from './fixtures/single-test-config.json';
chai.should();
chai.use(sinonChai);
describe('Bot Events', function () {
const sandbox = sinon.createSandbox({
useFakeTimers: false,
useFakeServer: false
});
const createBot = (optConfig = null) => {
const useConfig = optConfig || config;
const bot = new Bot(useConfig);
bot.sendToIRC = sandbox.stub();
bot.sendToDiscord = sandbox.stub();
bot.sendExactToDiscordByIrcChannel = sandbox.stub();
bot.sendExactToDiscordByDiscordChannel = sandbox.stub();
return bot;
};
beforeEach(function () {
this.infoSpy = sandbox.stub(logger, 'info');
this.debugSpy = sandbox.stub(logger, 'debug');
this.warnSpy = sandbox.stub(logger, 'warn');
this.errorSpy = sandbox.stub(logger, 'error');
this.sendStub = sandbox.stub();
this.getUserStub = sandbox.stub();
irc.Client = ClientStub;
discord.Client = createDiscordStub(this.sendStub, this.getUserStub);
ClientStub.prototype.send = sandbox.stub();
ClientStub.prototype.join = sandbox.stub();
this.bot = createBot();
this.bot.connect();
});
afterEach(function () {
sandbox.restore();
});
it('should log on discord ready event', function () {
this.bot.discord.emit('ready');
this.infoSpy.should.have.been.calledWithExactly('Connected to Discord');
});
it('should log on irc registered event', function () {
const message = 'registered';
this.bot.ircClient.emit('registered', message);
this.infoSpy.should.have.been.calledWithExactly('Connected to IRC');
this.debugSpy.should.have.been.calledWithExactly('Registered event: ', message);
});
it('should try to send autoSendCommands on registered IRC event', function () {
this.bot.ircClient.emit('registered');
ClientStub.prototype.send.should.have.been.calledTwice;
ClientStub.prototype.send.getCall(0)
.args.should.deep.equal(config.autoSendCommands[0]);
ClientStub.prototype.send.getCall(1)
.args.should.deep.equal(config.autoSendCommands[1]);
});
it('should error log on error events', function () {
const discordError = new Error('discord');
const ircError = new Error('irc');
this.bot.discord.emit('error', discordError);
this.bot.ircClient.emit('error', ircError);
this.errorSpy.getCall(0).args[0].should.equal('Received error event from Discord');
this.errorSpy.getCall(0).args[1].should.equal(discordError);
this.errorSpy.getCall(1).args[0].should.equal('Received error event from IRC');
this.errorSpy.getCall(1).args[1].should.equal(ircError);
});
it('should warn log on warn events from discord', function () {
const discordError = new Error('discord');
this.bot.discord.emit('warn', discordError);
const [message, error] = this.warnSpy.firstCall.args;
message.should.equal('Received warn event from Discord');
error.should.equal(discordError);
});
it('should send messages to irc if correct', function () {
const message = {
type: 'message'
};
this.bot.discord.emit('message', message);
this.bot.sendToIRC.should.have.been.calledWithExactly(message);
});
it('should send messages to discord', function () {
const channel = '#channel';
const author = 'user';
const text = 'hi';
this.bot.ircClient.emit('message', author, channel, text);
this.bot.sendToDiscord.should.have.been.calledWithExactly(author, channel, text);
});
it('should send notices to discord', function () {
const channel = '#channel';
const author = 'user';
const text = 'hi';
const formattedText = `*${text}*`;
this.bot.ircClient.emit('notice', author, channel, text);
this.bot.sendToDiscord.should.have.been.calledWithExactly(author, channel, formattedText);
});
it('should not send name change event to discord', function () {
const channel = '#channel';
const oldnick = 'user1';
const newnick = 'user2';
this.bot.ircClient.emit('nick', oldnick, newnick, [channel]);
this.bot.sendExactToDiscordByIrcChannel.should.not.have.been.called;
});
it('should send name change event to discord', function () {
const channel1 = '#channel1';
const channel2 = '#channel2';
const channel3 = '#channel3';
const oldNick = 'user1';
const newNick = 'user2';
const user3 = 'user3';
const bot = createBot({ ...config, ircStatusNotices: true });
const staticChannel = new Set([bot.nickname, user3]);
bot.connect();
bot.ircClient.emit('names', channel1, { [bot.nickname]: '', [oldNick]: '' });
bot.ircClient.emit('names', channel2, { [bot.nickname]: '', [user3]: '' });
const channelNicksPre = new Set([bot.nickname, oldNick]);
bot.channelUsers.should.deep.equal({ '#channel1': channelNicksPre, '#channel2': staticChannel });
const formattedText = `*${oldNick}* is now known as ${newNick}`;
const channelNicksAfter = new Set([bot.nickname, newNick]);
bot.ircClient.emit('nick', oldNick, newNick, [channel1, channel2, channel3]);
bot.sendExactToDiscordByIrcChannel.should.have.been.calledWithExactly(channel1, formattedText);
bot.channelUsers.should.deep.equal({ '#channel1': channelNicksAfter, '#channel2': staticChannel });
});
it('should send name change event to specified discord channel', function () {
const channel1 = '#channel1';
const channel2 = '#channel2';
const channel3 = '#channel3';
const oldNick = 'user1';
const newNick = 'user2';
const user3 = 'user3';
const notifyChannel = '#joins-and-leaves';
const bot = createBot({ ...config, ircStatusNotices: notifyChannel });
const staticChannel = new Set([bot.nickname, user3]);
bot.connect();
bot.ircClient.emit('names', channel1, { [bot.nickname]: '', [oldNick]: '' });
bot.ircClient.emit('names', channel2, { [bot.nickname]: '', [user3]: '' });
const channelNicksPre = new Set([bot.nickname, oldNick]);
bot.channelUsers.should.deep.equal({ '#channel1': channelNicksPre, '#channel2': staticChannel });
const formattedText = `*${oldNick}* is now known as ${newNick}`;
const channelNicksAfter = new Set([bot.nickname, newNick]);
bot.ircClient.emit('nick', oldNick, newNick, [channel1, channel2, channel3]);
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledOnce;
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledWithExactly(
notifyChannel,
formattedText
);
bot.channelUsers.should.deep.equal({ '#channel1': channelNicksAfter, '#channel2': staticChannel });
});
it('should send actions to discord', function () {
const channel = '#channel';
const author = 'user';
const text = 'hi';
const formattedText = '_hi_';
const message = {};
this.bot.ircClient.emit('action', author, channel, text, message);
this.bot.sendToDiscord.should.have.been.calledWithExactly(author, channel, formattedText);
});
it('should keep track of users through names event when irc status notices enabled', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
bot.channelUsers.should.be.an('object');
const channel = '#channel';
// nick => '' means the user is not a special user
const nicks = {
[bot.nickname]: '', user: '', user2: '@', user3: '+'
};
bot.ircClient.emit('names', channel, nicks);
const channelNicks = new Set([bot.nickname, 'user', 'user2', 'user3']);
bot.channelUsers.should.deep.equal({ '#channel': channelNicks });
});
it('should lowercase the channelUsers mapping', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel = '#channelName';
const nicks = { [bot.nickname]: '' };
bot.ircClient.emit('names', channel, nicks);
const channelNicks = new Set([bot.nickname]);
bot.channelUsers.should.deep.equal({ '#channelname': channelNicks });
});
it('should send join messages to discord when config enabled', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel = '#channel';
bot.ircClient.emit('names', channel, { [bot.nickname]: '' });
const nick = 'user';
const text = `*${nick}* has joined the channel`;
bot.ircClient.emit('join', channel, nick);
bot.sendExactToDiscordByIrcChannel.should.have.been.calledWithExactly(channel, text);
const channelNicks = new Set([bot.nickname, nick]);
bot.channelUsers.should.deep.equal({ '#channel': channelNicks });
});
it('should send join messages to specified discord channel when config enabled', function () {
const notifyChannel = '#joins-and-leaves';
const bot = createBot({ ...config, ircStatusNotices: notifyChannel });
bot.connect();
const channel = '#channel';
bot.ircClient.emit('names', channel, { [bot.nickname]: '' });
const nick = 'user';
const text = `*${nick}* has joined IRC`;
bot.ircClient.emit('join', channel, nick);
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledOnce;
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledWithExactly(notifyChannel, text);
const channelNicks = new Set([bot.nickname, nick]);
bot.channelUsers.should.deep.equal({ '#channel': channelNicks });
});
it('should send single join message to specified discord channel when config enabled and multiple channels are joined', function () {
const notifyChannel = '#joins-and-leaves';
const bot = createBot({ ...config, ircStatusNotices: notifyChannel });
bot.connect();
const channel1 = '#channel1';
const channel2 = '#channel2';
bot.ircClient.emit('names', channel1, { [bot.nickname]: '' });
bot.ircClient.emit('names', channel2, { [bot.nickname]: '' });
const nick = 'user';
const text = `*${nick}* has joined IRC`;
bot.ircClient.emit('join', channel1, nick);
bot.ircClient.emit('join', channel2, nick);
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledOnce;
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledWithExactly(notifyChannel, text);
});
it('should not announce itself joining by default', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel = '#channel';
bot.ircClient.emit('names', channel, { [bot.nickname]: '' });
const nick = bot.nickname;
bot.ircClient.emit('join', channel, nick);
bot.sendExactToDiscordByIrcChannel.should.not.have.been.called;
const channelNicks = new Set([bot.nickname]);
bot.channelUsers.should.deep.equal({ '#channel': channelNicks });
});
it('should announce the bot itself when config enabled', function () {
// self-join is announced before names (which includes own nick)
// hence don't trigger a names and don't expect anything of bot.channelUsers
const bot = createBot({ ...config, ircStatusNotices: true, announceSelfJoin: true });
bot.connect();
const channel = '#channel';
const nick = this.bot.nickname;
const text = `*${nick}* has joined the channel`;
bot.ircClient.emit('join', channel, nick);
bot.sendExactToDiscordByIrcChannel.should.have.been.calledWithExactly(channel, text);
});
it('should send part messages to discord when config enabled', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel = '#channel';
const nick = 'user';
bot.ircClient.emit('names', channel, { [bot.nickname]: '', [nick]: '' });
const originalNicks = new Set([bot.nickname, nick]);
bot.channelUsers.should.deep.equal({ '#channel': originalNicks });
const reason = 'Leaving';
const text = `*${nick}* has left the channel (${reason})`;
bot.ircClient.emit('part', channel, nick, reason);
bot.sendExactToDiscordByIrcChannel.should.have.been.calledWithExactly(channel, text);
// it should remove the nickname from the channelUsers list
const channelNicks = new Set([bot.nickname]);
bot.channelUsers.should.deep.equal({ '#channel': channelNicks });
});
it('should send part messages to specified discord channel when config enabled', function () {
const notifyChannel = '#joins-and-leaves';
const bot = createBot({ ...config, ircStatusNotices: notifyChannel });
bot.connect();
const channel = '#channel';
const nick = 'user';
bot.ircClient.emit('names', channel, { [bot.nickname]: '', [nick]: '' });
const originalNicks = new Set([bot.nickname, nick]);
bot.channelUsers.should.deep.equal({ '#channel': originalNicks });
const reason = 'Leaving';
const text = `*${nick}* has left ${channel} (${reason})`;
bot.ircClient.emit('part', channel, nick, reason);
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledOnce;
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledWithExactly(notifyChannel, text);
// it should remove the nickname from the channelUsers list
const channelNicks = new Set([bot.nickname]);
bot.channelUsers.should.deep.equal({ '#channel': channelNicks });
});
it('should not announce itself leaving a channel', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel = '#channel';
bot.ircClient.emit('names', channel, { [bot.nickname]: '', user: '' });
const originalNicks = new Set([bot.nickname, 'user']);
bot.channelUsers.should.deep.equal({ '#channel': originalNicks });
const reason = 'Leaving';
bot.ircClient.emit('part', channel, bot.nickname, reason);
bot.sendExactToDiscordByIrcChannel.should.not.have.been.called;
// it should remove the nickname from the channelUsers list
bot.channelUsers.should.deep.equal({});
});
it('should only send quit messages to discord for channels the user is tracked in', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel1 = '#channel1';
const channel2 = '#channel2';
const channel3 = '#channel3';
const nick = 'user';
bot.ircClient.emit('names', channel1, { [bot.nickname]: '', [nick]: '' });
bot.ircClient.emit('names', channel2, { [bot.nickname]: '' });
bot.ircClient.emit('names', channel3, { [bot.nickname]: '', [nick]: '' });
const reason = 'Quit: Leaving';
const text = `*${nick}* has quit (${reason})`;
// send quit message for all channels on server, as the node-irc library does
bot.ircClient.emit('quit', nick, reason, [channel1, channel2, channel3]);
bot.sendExactToDiscordByIrcChannel.should.have.been.calledTwice;
bot.sendExactToDiscordByIrcChannel.getCall(0).args.should.deep.equal([channel1, text]);
bot.sendExactToDiscordByIrcChannel.getCall(1).args.should.deep.equal([channel3, text]);
});
it('should send quit messages to a specified discord channel when config enabled', function () {
const notifyChannel = '#joins-and-leaves';
const bot = createBot({ ...config, ircStatusNotices: notifyChannel });
bot.connect();
const channel1 = '#channel1';
const channel2 = '#channel2';
const channel3 = '#channel3';
const nick = 'user';
bot.ircClient.emit('names', channel1, { [bot.nickname]: '', [nick]: '' });
bot.ircClient.emit('names', channel2, { [bot.nickname]: '' });
bot.ircClient.emit('names', channel3, { [bot.nickname]: '', [nick]: '' });
const reason = 'Quit: Leaving';
const text = `*${nick}* has quit (${reason})`;
// send quit message for all channels on server, as the node-irc library does
bot.ircClient.emit('quit', nick, reason, [channel1, channel2, channel3]);
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledOnce;
bot.sendExactToDiscordByDiscordChannel.should.have.been.calledWithExactly(notifyChannel, text);
});
it('should not crash with join/part/quit messages and weird channel casing', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
function wrap() {
const nick = 'user';
const reason = 'Leaving';
bot.ircClient.emit('names', '#Channel', { [bot.nickname]: '' });
bot.ircClient.emit('join', '#cHannel', nick);
bot.ircClient.emit('part', '#chAnnel', nick, reason);
bot.ircClient.emit('join', '#chaNnel', nick);
bot.ircClient.emit('quit', nick, reason, ['#chanNel']);
}
(wrap).should.not.throw();
});
it('should be possible to disable join/part/quit messages', function () {
const bot = createBot({ ...config, ircStatusNotices: false });
bot.connect();
const channel = '#channel';
const nick = 'user';
const reason = 'Leaving';
bot.ircClient.emit('names', channel, { [bot.nickname]: '' });
bot.ircClient.emit('join', channel, nick);
bot.ircClient.emit('part', channel, nick, reason);
bot.ircClient.emit('join', channel, nick);
bot.ircClient.emit('quit', nick, reason, [channel]);
bot.sendExactToDiscordByIrcChannel.should.not.have.been.called;
});
it('should warn if it receives a part/quit before a names event', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel = '#channel';
const reason = 'Leaving';
bot.ircClient.emit('part', channel, 'user1', reason);
bot.ircClient.emit('quit', 'user2', reason, [channel]);
this.warnSpy.should.have.been.calledTwice;
this.warnSpy.getCall(0).args.should.deep.equal([`No channelUsers found for ${channel} when user1 parted.`]);
this.warnSpy.getCall(1).args.should.deep.equal([`No channelUsers found for ${channel} when user2 quit, ignoring.`]);
});
it('should not crash if it uses a different name from config', function () {
// this can happen when a user with the same name is already connected
const bot = createBot({ ...config, nickname: 'testbot' });
bot.connect();
const newName = 'testbot1';
bot.ircClient.nick = newName;
function wrap() {
bot.ircClient.emit('join', '#channel', newName);
}
(wrap).should.not.throw;
});
it('should not listen to discord debug messages in production', function () {
logger.level = 'info';
const bot = createBot();
bot.connect();
const listeners = bot.discord.listeners('debug');
listeners.length.should.equal(0);
});
it('should listen to discord debug messages in development', function () {
logger.level = 'debug';
const bot = createBot();
bot.connect();
const listeners = bot.discord.listeners('debug');
listeners.length.should.equal(1);
});
it('should join channels when invited', function () {
const channel = '#irc';
const author = 'user';
this.bot.ircClient.emit('invite', channel, author);
const firstCall = this.debugSpy.getCall(1);
firstCall.args[0].should.equal('Received invite:');
firstCall.args[1].should.equal(channel);
firstCall.args[2].should.equal(author);
ClientStub.prototype.join.should.have.been.calledWith(channel);
const secondCall = this.debugSpy.getCall(2);
secondCall.args[0].should.equal('Joining channel:');
secondCall.args[1].should.equal(channel);
});
it('should not join channels that aren\'t in the channel mapping', function () {
const channel = '#wrong';
const author = 'user';
this.bot.ircClient.emit('invite', channel, author);
const firstCall = this.debugSpy.getCall(1);
firstCall.args[0].should.equal('Received invite:');
firstCall.args[1].should.equal(channel);
firstCall.args[2].should.equal(author);
ClientStub.prototype.join.should.not.have.been.called;
const secondCall = this.debugSpy.getCall(2);
secondCall.args[0].should.equal('Channel not found in config, not joining:');
secondCall.args[1].should.equal(channel);
});
});