From 70292474619cfe14a66c4cf8e194bdd0b47b55d6 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 23 Nov 2023 15:52:41 +1300 Subject: [PATCH] Coverage for role and emoji converter --- d2m/converters/message-to-event.js | 9 ++++----- d2m/converters/message-to-event.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index 5994ff37..8a5ab177 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -52,11 +52,8 @@ function getDiscordParseCallbacks(message, guild, useHTML) { emoji: node => { if (useHTML) { const mxc = select("emoji", "mxc_url", {emoji_id: node.id}).pluck().get() - if (mxc) { + assert(mxc) // All emojis should have been added ahead of time in the messageToEvent function. return `:${node.name}:` - } else { // We shouldn't get here since all emojis should have been added ahead of time in the messageToEvent function. - return `:${node.name}:` - } } else { return `:${node.name}:` } @@ -64,7 +61,9 @@ function getDiscordParseCallbacks(message, guild, useHTML) { role: node => { const role = guild.roles.find(r => r.id === node.id) if (!role) { - return "@&" + node.id // fallback for if the cache breaks. if this happens, fix discord-packets.js to store the role info. + // This fallback should only trigger if somebody manually writes a silly message, or if the cache breaks (hasn't happened yet). + // If the cache breaks, fix discord-packets.js to store role info properly. + return "@&" + node.id } else if (useHTML && role.color) { return `@${role.name}` } else if (useHTML) { diff --git a/d2m/converters/message-to-event.test.js b/d2m/converters/message-to-event.test.js index 8f89a43c..d40dd723 100644 --- a/d2m/converters/message-to-event.test.js +++ b/d2m/converters/message-to-event.test.js @@ -85,6 +85,18 @@ test("message2event: simple role mentions", async t => { }]) }) +test("message2event: manually constructed unknown roles should use fallback", async t => { + const events = await messageToEvent(data.message.unknown_role, data.guild.general, {}) + t.deepEqual(events, [{ + $type: "m.room.message", + "m.mentions": {}, + msgtype: "m.text", + body: "I'm just @&4 testing a few role pings <@&B> don't mind me", + format: "org.matrix.custom.html", + formatted_body: "I'm just @&4 testing a few role pings <@&B> don't mind me" + }]) +}) + test("message2event: simple message link", async t => { const events = await messageToEvent(data.message.simple_message_link, data.guild.general, {}) t.deepEqual(events, [{