Skip to content

Commit 9faa90b

Browse files
committed
Add tests for partial and overlapping matches
1 parent 8174293 commit 9faa90b

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

lib/bot.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,12 @@ class Bot {
338338
// compare two strings case-insensitively
339339
// for discord mention matching
340340
static caseComp(str1, str2) {
341-
if (str1 === str2) return true;
342-
if (typeof str1 !== 'string' || typeof str2 !== 'string') return false;
343341
return str1.toUpperCase() === str2.toUpperCase();
344342
}
345343

346344
// check if the first string starts with the second case-insensitively
347345
// for discord mention matching
348346
static caseStartsWith(str1, str2) {
349-
if (str1 === str2) return true;
350-
if (typeof str1 !== 'string' || typeof str2 !== 'string') return false;
351347
return str1.toUpperCase().startsWith(str2.toUpperCase());
352348
}
353349

@@ -392,7 +388,8 @@ class Bot {
392388
// this preliminary stuff is ultimately unnecessary
393389
// but might save time over later more complicated calculations
394390
// @nickname => mention, case insensitively
395-
const nickUser = guild.members.find(x => Bot.caseComp(x.nickname, reference));
391+
const nickUser = guild.members.find(x =>
392+
x.nickname !== null && Bot.caseComp(x.nickname, reference));
396393
if (nickUser) return nickUser;
397394

398395
// @username => mention, case insensitively

test/bot.test.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ describe('Bot', function () {
519519
const testUser = this.addUser({ username: 'testuser', id: '123', nickname: 'somenickname' });
520520

521521
const username = 'ircuser';
522-
const text = 'Hello, @somenickname!';
523-
const expected = `**<${username}>** Hello, ${testUser}!`;
522+
const text = 'Hello, @somenickname';
523+
const expected = `**<${username}>** Hello, ${testUser}`;
524524

525525
this.bot.sendToDiscord(username, '#irc', text);
526526
this.sendStub.should.have.been.calledWith(expected);
@@ -531,7 +531,19 @@ describe('Bot', function () {
531531

532532
const username = 'ircuser';
533533
const text = 'Hello, @testuser!';
534-
const expected = `**<${username}>** Hello, <@${testUser.id}>!`;
534+
const expected = `**<${username}>** Hello, ${testUser}!`;
535+
536+
this.bot.sendToDiscord(username, '#irc', text);
537+
this.sendStub.should.have.been.calledWith(expected);
538+
});
539+
540+
it('should convert username-discriminator mentions from IRC properly', function () {
541+
const user1 = this.addUser({ username: 'user', id: '123', discriminator: '9876' });
542+
const user2 = this.addUser({ username: 'user', id: '124', discriminator: '5555', nickname: 'secondUser' });
543+
544+
const username = 'ircuser';
545+
const text = 'hello @user#9876 and @user#5555 and @fakeuser#1234';
546+
const expected = `**<${username}>** hello ${user1} and ${user2} and @fakeuser#1234`;
535547

536548
this.bot.sendToDiscord(username, '#irc', text);
537549
this.sendStub.should.have.been.calledWith(expected);
@@ -599,6 +611,35 @@ describe('Bot', function () {
599611
this.sendStub.should.have.been.calledWith(expected);
600612
});
601613

614+
it('should convert overlapping mentions from IRC properly and case-insensitively', function () {
615+
const user = this.addUser({ username: 'user', id: '111' });
616+
const nickUser = this.addUser({ username: 'user2', id: '112', nickname: 'userTest' });
617+
const nickUserCase = this.addUser({ username: 'user3', id: '113', nickname: 'userTEST' });
618+
const role = this.addRole({ name: 'userTestRole', id: '12345', mentionable: true });
619+
620+
const username = 'ircuser';
621+
const text = 'hello @User, @user, @userTest, @userTEST, @userTestRole and @usertestrole';
622+
const expected = `**<${username}>** hello ${user}, ${user}, ${nickUser}, ${nickUserCase}, ${role} and ${role}`;
623+
624+
this.bot.sendToDiscord(username, '#irc', text);
625+
this.sendStub.should.have.been.calledWith(expected);
626+
});
627+
628+
it('should convert partial matches from IRC properly', function () {
629+
const user = this.addUser({ username: 'user', id: '111' });
630+
const longUser = this.addUser({ username: 'user-punc', id: '112' });
631+
const nickUser = this.addUser({ username: 'user2', id: '113', nickname: 'nick' });
632+
const nickUserCase = this.addUser({ username: 'user3', id: '114', nickname: 'NiCK' });
633+
const role = this.addRole({ name: 'role', id: '12345', mentionable: true });
634+
635+
const username = 'ircuser';
636+
const text = '@user-ific @usermore, @user\'s friend @user-punc, @nicks and @NiCKs @roles';
637+
const expected = `**<${username}>** ${user}-ific ${user}more, ${user}'s friend ${longUser}, ${nickUser}s and ${nickUserCase}s ${role}s`;
638+
639+
this.bot.sendToDiscord(username, '#irc', text);
640+
this.sendStub.should.have.been.calledWith(expected);
641+
});
642+
602643
it('should successfully send messages with default config', function () {
603644
const bot = new Bot(configMsgFormatDefault);
604645
bot.connect();

0 commit comments

Comments
 (0)