From 61803c3838d494782ae4b3edea70f72f1bbcb8f3 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 30 Sep 2024 01:18:56 +1300 Subject: [PATCH] Add tests for matrix info interaction --- src/discord/interactions/matrix-info.js | 36 ++++++---- src/discord/interactions/matrix-info.test.js | 76 ++++++++++++++++++++ test/test.js | 1 + 3 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 src/discord/interactions/matrix-info.test.js diff --git a/src/discord/interactions/matrix-info.js b/src/discord/interactions/matrix-info.js index fac38043..f7bc31a6 100644 --- a/src/discord/interactions/matrix-info.js +++ b/src/discord/interactions/matrix-info.js @@ -6,46 +6,58 @@ const {discord, sync, db, select, from} = require("../../passthrough") /** @type {import("../../matrix/api")} */ const api = sync.require("../../matrix/api") -/** @param {DiscordTypes.APIContextMenuGuildInteraction} interaction */ -/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */ -async function interact({id, token, guild_id, channel, data}) { +/** + * @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction + * @param {{api: typeof api}} di + * @returns {Promise} + */ +async function _interact({guild_id, data}, {api}) { const message = from("event_message").join("message_channel", "message_id").join("channel_room", "channel_id") - .select("name", "nick", "source", "room_id", "event_id").where({message_id: data.target_id}).get() + .select("name", "nick", "source", "channel_id", "room_id", "event_id").where({message_id: data.target_id, part: 0}).get() if (!message) { - return discord.snow.interaction.createInteractionResponse(id, token, { + return { type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, data: { content: "This message hasn't been bridged to Matrix.", flags: DiscordTypes.MessageFlags.Ephemeral } - }) + } } const idInfo = `\n-# Room ID: \`${message.room_id}\`\n-# Event ID: \`${message.event_id}\`` + const roomName = message.nick || message.name if (message.source === 1) { // from Discord const userID = data.resolved.messages[data.target_id].author.id - return discord.snow.interaction.createInteractionResponse(id, token, { + return { type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, data: { - content: `Bridged <@${userID}> https://discord.com/channels/${guild_id}/${channel.id}/${data.target_id} on Discord to [${message.nick || message.name}]() on Matrix.` + content: `Bridged <@${userID}> https://discord.com/channels/${guild_id}/${message.channel_id}/${data.target_id} on Discord to [${roomName}]() on Matrix.` + idInfo, flags: DiscordTypes.MessageFlags.Ephemeral } - }) + } } // from Matrix const event = await api.getEvent(message.room_id, message.event_id) - return discord.snow.interaction.createInteractionResponse(id, token, { + return { type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, data: { - content: `Bridged [${event.sender}]()'s message in [${message.nick || message.name}]() on Matrix to https://discord.com/channels/${guild_id}/${channel.id}/${data.target_id} on Discord.` + content: `Bridged [${event.sender}]()'s message in [${roomName}]() on Matrix to https://discord.com/channels/${guild_id}/${message.channel_id}/${data.target_id} on Discord.` + idInfo, flags: DiscordTypes.MessageFlags.Ephemeral } - }) + } +} + +/* c8 ignore start */ + +/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */ +async function interact(interaction) { + await discord.snow.interaction.createInteractionResponse(interaction.id, interaction.token, await _interact(interaction, {api})) } module.exports.interact = interact +module.exports._interact = _interact diff --git a/src/discord/interactions/matrix-info.test.js b/src/discord/interactions/matrix-info.test.js new file mode 100644 index 00000000..f4d0f521 --- /dev/null +++ b/src/discord/interactions/matrix-info.test.js @@ -0,0 +1,76 @@ +const {test} = require("supertape") +const data = require("../../../test/data") +const DiscordTypes = require("discord-api-types/v10") +const {db, discord} = require("../../passthrough") +const {MatrixServerError} = require("../../matrix/mreq") +const {_interact, _interactButton} = require("./matrix-info") + +test("matrix info: checks if message has bridged", async t => { + const msg = await _interact({ + data: { + target_id: "0" + }, + guild_id: "112760669178241024" + }, {}) + t.equal(msg.data.content, "This message hasn't been bridged to Matrix.") +}) + +test("matrix info: shows info for discord source message", async t => { + const msg = await _interact({ + data: { + target_id: "1141619794500649020", + resolved: { + messages: { + "1141619794500649020": data.message_update.edit_by_webhook + } + } + }, + guild_id: "497159726455455754" + }, {}) + t.equal( + msg.data.content, + "Bridged <@700285844094845050> https://discord.com/channels/497159726455455754/497161350934560778/1141619794500649020 on Discord to [amanda-spam]() on Matrix." + + "\n-# Room ID: `!CzvdIdUQXgUjDVKxeU:cadence.moe`" + + "\n-# Event ID: `$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10`" + ) +}) + +test("matrix info: shows info for matrix source message", async t => { + let called = 0 + const msg = await _interact({ + data: { + target_id: "1128118177155526666", + resolved: { + messages: { + "1141501302736695316": data.message.simple_reply_to_matrix_user + } + } + }, + guild_id: "112760669178241024" + }, { + api: { + async getEvent(roomID, eventID) { + called++ + t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe") + t.equal(eventID, "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4") + return { + event_id: eventID, + room_id: roomID, + type: "m.room.message", + content: { + msgtype: "m.text", + body: "so can you reply to my webhook uwu" + }, + sender: "@cadence:cadence.moe" + } + } + } + }) + t.equal( + msg.data.content, + "Bridged [@cadence:cadence.moe]()'s message in [main]() on Matrix to https://discord.com/channels/112760669178241024/112760669178241024/1128118177155526666 on Discord." + + "\n-# Room ID: `!kLRqKKUQXcibIMtOpl:cadence.moe`" + + "\n-# Event ID: `$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4`" + ) + t.equal(called, 1) +}) diff --git a/test/test.js b/test/test.js index 07146ab2..e0736c89 100644 --- a/test/test.js +++ b/test/test.js @@ -137,4 +137,5 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not require("../src/m2d/converters/utils.test") require("../src/m2d/converters/emoji-sheet.test") require("../src/discord/interactions/invite.test") + require("../src/discord/interactions/matrix-info.test") })()