Skip to content

Commit

Permalink
Add zero width space in Telegram usernames sent to IRC (closes #113).
Browse files Browse the repository at this point in the history
Add unit tests for ZWP.
  • Loading branch information
Tjzabel committed Mar 3, 2019
1 parent 5323871 commit 09aa1f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/TelegramHandlers/TgHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports.ResolveUserName = function(from) {
return from.first_name;
}

return from.username;
// Add ZWP to remove IRC highlighting when Telegram + IRC nicks are the same
return from.username.substr(0,1) + "\u200B" + from.username.substr(1);
};

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/TelegramToIrcTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const TeleIrc = require("../lib/TeleIrc");
const TgHelper = require("../lib/TelegramHandlers/TgHelpers.js");

const TEST_SETTINGS = {
token: "EXAMPLE_TOKEN",
Expand Down Expand Up @@ -56,6 +57,15 @@ function createIrcBotMock(assert, expectedMessages) {
};
}

// Override default method to not use zero width spaces
TgHelper.ResolveUserName = function(from) {
if (from.username === undefined) {
return from.first_name;
}

return from.username;
}

exports.TelegramToIrcTests = {
"Forwarding a simple message": function(assert) {
const USERNAME = "TEST_USERNAME";
Expand Down
28 changes: 23 additions & 5 deletions tests/TgMessageHandlerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ let fromWithUserName = {
username : "username"
};

let fromWithZWP = {
first_name : "First Name",
username : "u" + "\u200B" + "sername"
}

// Subset of the IRC config.
let prefixSuffixConfig = {
prefix : "<",
Expand Down Expand Up @@ -40,8 +45,6 @@ exports.TgMessageHandler = {
assert.done();
},
"If user has no username, first name is reported": function(assert) {
var actualMessage = undefined;
var actualUsername = undefined;

let sentMessage = "My Message";
let expectedUsername = fromWithUserName.first_name;
Expand All @@ -59,8 +62,6 @@ exports.TgMessageHandler = {
uut.RelayMessage(fromNoUsername, sentMessage);
},
"Username is reported if it is available": function(assert) {
var actualMessage = undefined;
var actualUsername = undefined;

let sentMessage = "My Message";
let expectedUsername = fromWithUserName.username;
Expand All @@ -77,4 +78,21 @@ exports.TgMessageHandler = {

uut.RelayMessage(fromWithUserName, sentMessage);
},
};
"Zero-width space is inserted into username": function(assert) {

let sentMessage = "My Message";
let expectedUsername = fromWithZWP.username;
let expectedMessage = `<${expectedUsername}> My Message`;

let uut = new TgMessageHandler (
prefixSuffixConfig,
true,
(input) => {
assert.strictEqual(expectedMessage, input);
assert.done();
}
);

uut.RelayMessage(fromWithZWP, sentMessage);
}
};

0 comments on commit 09aa1f1

Please sign in to comment.