Skip to content

Commit

Permalink
Test bot cancels autoRenick on nick change or after cancelAutoRenick
Browse files Browse the repository at this point in the history
  • Loading branch information
Throne3d committed Aug 6, 2017
1 parent 635ebd4 commit cd59d13
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ Client.prototype.activateFloodProtection = function(interval) {
};

Client.prototype.cancelAutoRenick = function() {
if (!this.conn) return;
var oldInterval = this.conn.renickInterval;
clearInterval(this.conn.renickInterval);
this.conn.renickInterval = null;
Expand Down
127 changes: 124 additions & 3 deletions test/test-autorenick.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ test('bot does not try to renick when it gets the chosen nickname', function(t)
}, { autoRenick: true });
});

// TODO: reduce code duplication

test('bot renicks automatically when config enabled', function(t) {
withClient(function(obj) {
var client = obj.client;
Expand Down Expand Up @@ -68,7 +70,6 @@ test('bot renicks automatically when config enabled', function(t) {
mock.on('line', mustRenick);
} else {
mock.send(':testbot2!~testbot@mockhost.com NICK :testbot\r\n');

}
} else if (args[1] === 'testbot1') {
mock.send(':localhost 433 * testbot1 :Nickname is already in use.\r\n');
Expand All @@ -78,7 +79,7 @@ test('bot renicks automatically when config enabled', function(t) {
});

client.on('registered', function() {
t.notEqual(typeof client.conn.renickInterval, 'undefined');
t.ok(client.conn.renickInterval);
});

client.on('nick', function(oldnick, newnick) {
Expand Down Expand Up @@ -133,11 +134,12 @@ test('bot renicks given amount', function(t) {
});

client.on('registered', function() {
t.notEqual(typeof client.conn.renickInterval, 'undefined');
t.ok(client.conn.renickInterval);
});

mock.on('end', function() {
t.deepEqual(actual, expected, 'bot must send right nick commands');
t.equal(client.conn.renickInterval, null);
mock.close(function() {
t.end();
});
Expand Down Expand Up @@ -188,3 +190,122 @@ test('bot only renicks if config enabled', function(t) {
});
}, { autoRenick: false, renickDelay: 50, renickCount: 3 });
});

test('bot does not renick if config enabled and nickinuse handler calls cancelAutoRenick', function(t) {
withClient(function(obj) {
var client = obj.client;
var mock = obj.mock;
t.timeoutAfter(1000);
var expected = [
'NICK testbot',
'NICK testbot1',
'NICK testbot'
];
var actual = [];

var rebuked = false;
mock.on('line', function(line) {
var args = line.split(' ');
if (args[0] !== 'NICK') return;

// ensure bot sends right nick commands
actual.push(line);
if (args[1] === 'testbot') {
if (!rebuked) {
rebuked = true;
mock.send(':localhost 433 * testbot :Nickname is already in use.\r\n');
}
}
if (expected.length === actual.length) {
setTimeout(function() {
client.disconnect();
}, 200);
}
});

client.on('registered', function() {
t.ok(client.conn.renickInterval);
});

client.addListener('raw', function handler(message) {
if (message.command === 'err_nicknameinuse') {
client.removeListener('raw', handler);
setTimeout(function() {
client.cancelAutoRenick();
t.equal(client.conn.renickInterval, null);
}, 75);
}
});

mock.on('end', function() {
t.deepEqual(actual, expected, 'bot must send right nick commands');
t.equal(client.conn.renickInterval, null);
mock.close(function() {
t.end();
});
});
}, { autoRenick: true, renickDelay: 50, renickCount: 3 });
});

test('bot does not renick if it finds it has renicked in the meantime', function(t){
withClient(function(obj) {
var client = obj.client;
var mock = obj.mock;
t.timeoutAfter(1000);
var expected = [
'NICK testbot',
'NICK testbot1'
];
var actual = [];

var rebuked = false;
mock.on('line', function(line) {
var args = line.split(' ');
if (args[0] !== 'NICK') return;

// ensure bot sends right nick commands
actual.push(line);
if (args[1] === 'testbot') {
if (!rebuked) {
rebuked = true;
mock.send(':localhost 433 * testbot :Nickname is already in use.\r\n');
}
}
if (expected.length === actual.length) {
setTimeout(function() {
client.disconnect();
}, 200);
}
});

client.on('registered', function() {
t.ok(client.conn.renickInterval);
});

var receivedNick = false;
var mustReceiveNick = function(oldNick, newNick) {
t.equal(oldNick, 'testbot1');
t.equal(newNick, 'testbot');
t.equal(client.conn.renickInterval, null);
receivedNick = true;
};

client.addListener('raw', function handler(message) {
if (message.command === 'err_nicknameinuse') {
client.removeListener('raw', handler);
t.ok(client.conn.renickInterval);
client.once('nick', mustReceiveNick);
mock.send(':testbot1!~testbot@mockhost.com NICK :testbot\r\n');
}
});

mock.on('end', function() {
t.deepEqual(actual, expected, 'bot must send right nick commands');
t.ok(receivedNick, 'bot must be renicked by server');
t.equal(client.conn.renickInterval, null);
mock.close(function() {
t.end();
});
});
}, { autoRenick: true, renickDelay: 50, renickCount: 3 });
});

0 comments on commit cd59d13

Please sign in to comment.