Add tests for reactions interaction

This commit is contained in:
Cadence Ember 2024-09-30 01:42:12 +13:00
parent 61803c3838
commit 33915a595d
6 changed files with 121 additions and 14 deletions

View file

@ -9,6 +9,7 @@ function addbot() {
return `Open this link to add the bot to a Discord server:\nhttps://discord.com/oauth2/authorize?client_id=${id}&scope=bot&permissions=1610883072 ` return `Open this link to add the bot to a Discord server:\nhttps://discord.com/oauth2/authorize?client_id=${id}&scope=bot&permissions=1610883072 `
} }
/* c8 ignore next 3 */
if (process.argv.find(a => a.endsWith("addbot") || a.endsWith("addbot.js"))) { if (process.argv.find(a => a.endsWith("addbot") || a.endsWith("addbot.js"))) {
console.log(addbot()) console.log(addbot())
} }

View file

@ -1,11 +1,8 @@
const {test} = require("supertape") const {test} = require("supertape")
const data = require("../../../test/data") const data = require("../../../test/data")
const DiscordTypes = require("discord-api-types/v10") const {_interact} = require("./matrix-info")
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 => { test("matrix info: checks if message is bridged", async t => {
const msg = await _interact({ const msg = await _interact({
data: { data: {
target_id: "0" target_id: "0"

View file

@ -8,19 +8,22 @@ const api = sync.require("../../matrix/api")
/** @type {import("../../m2d/converters/utils")} */ /** @type {import("../../m2d/converters/utils")} */
const utils = sync.require("../../m2d/converters/utils") const utils = sync.require("../../m2d/converters/utils")
/** @param {DiscordTypes.APIContextMenuGuildInteraction} interaction */ /**
/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */ * @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction
async function interact({id, token, data}) { * @param {{api: typeof api}} di
* @returns {Promise<DiscordTypes.APIInteractionResponse>}
*/
async function _interact({data}, {api}) {
const row = from("event_message").join("message_channel", "message_id").join("channel_room", "channel_id") const row = from("event_message").join("message_channel", "message_id").join("channel_room", "channel_id")
.select("event_id", "room_id").where({message_id: data.target_id}).get() .select("event_id", "room_id").where({message_id: data.target_id}).get()
if (!row) { if (!row) {
return discord.snow.interaction.createInteractionResponse(id, token, { return {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: { data: {
content: "This message hasn't been bridged to Matrix.", content: "This message hasn't been bridged to Matrix.",
flags: DiscordTypes.MessageFlags.Ephemeral flags: DiscordTypes.MessageFlags.Ephemeral
} }
}) }
} }
const reactions = await api.getFullRelations(row.room_id, row.event_id, "m.annotation") const reactions = await api.getFullRelations(row.room_id, row.event_id, "m.annotation")
@ -37,22 +40,30 @@ async function interact({id, token, data}) {
} }
if (inverted.size === 0) { if (inverted.size === 0) {
return discord.snow.interaction.createInteractionResponse(id, token, { return {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: { data: {
content: "Nobody from Matrix reacted to this message.", content: "Nobody from Matrix reacted to this message.",
flags: DiscordTypes.MessageFlags.Ephemeral flags: DiscordTypes.MessageFlags.Ephemeral
} }
}) }
} }
return discord.snow.interaction.createInteractionResponse(id, token, { return {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: { data: {
content: [...inverted.entries()].map(([key, value]) => `${key}${value.join(" ⬩ ")}`).join("\n"), content: [...inverted.entries()].map(([key, value]) => `${key}${value.join(" ⬩ ")}`).join("\n"),
flags: DiscordTypes.MessageFlags.Ephemeral 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
module.exports._interact = _interact

View file

@ -0,0 +1,87 @@
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} = require("./reactions")
test("reactions: checks if message is bridged", async t => {
const msg = await _interact({
data: {
target_id: "0"
}
}, {})
t.equal(msg.data.content, "This message hasn't been bridged to Matrix.")
})
test("reactions: different response if nobody reacted", async t => {
const msg = await _interact({
data: {
target_id: "1126786462646550579"
}
}, {
api: {
async getFullRelations(roomID, eventID) {
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
t.equal(eventID, "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg")
return []
}
}
})
t.equal(msg.data.content, "Nobody from Matrix reacted to this message.")
})
test("reactions: shows reactions if there are some, ignoring discord users", async t => {
let called = 1
const msg = await _interact({
data: {
target_id: "1126786462646550579"
}
}, {
api: {
async getFullRelations(roomID, eventID) {
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
t.equal(eventID, "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg")
return [{
sender: "@cadence:cadence.moe",
content: {
"m.relates_to": {
key: "🐈",
rel_type: "m.annotation"
}
}
}, {
sender: "@rnl:cadence.moe",
content: {
"m.relates_to": {
key: "🐈",
rel_type: "m.annotation"
}
}
}, {
sender: "@cadence:cadence.moe",
content: {
"m.relates_to": {
key: "🐈‍⬛",
rel_type: "m.annotation"
}
}
}, {
sender: "@_ooye_rnl:cadence.moe",
content: {
"m.relates_to": {
key: "🐈",
rel_type: "m.annotation"
}
}
}]
}
}
})
t.equal(
msg.data.content,
"🐈 ⮞ cadence [they] ⬩ @rnl:cadence.moe"
+ "\n🐈⬛ ⮞ cadence [they]"
)
t.equal(called, 1)
})

8
test/addbot.test.js Normal file
View file

@ -0,0 +1,8 @@
// @ts-check
const {addbot} = require("../addbot")
const {test} = require("supertape")
test("addbot: returns message and invite link", t => {
t.equal(addbot(), `Open this link to add the bot to a Discord server:\nhttps://discord.com/oauth2/authorize?client_id=684280192553844747&scope=bot&permissions=1610883072 `)
})

View file

@ -17,6 +17,7 @@ const passthrough = require("../src/passthrough")
const db = new sqlite(":memory:") const db = new sqlite(":memory:")
const {reg} = require("../src/matrix/read-registration") const {reg} = require("../src/matrix/read-registration")
reg.ooye.discord_token = "Njg0MjgwMTkyNTUzODQ0NzQ3.Xl3zlw.baby"
reg.ooye.server_origin = "https://matrix.cadence.moe" // so that tests will pass even when hard-coded reg.ooye.server_origin = "https://matrix.cadence.moe" // so that tests will pass even when hard-coded
reg.ooye.server_name = "cadence.moe" reg.ooye.server_name = "cadence.moe"
reg.id = "baby" // don't actually take authenticated actions on the server reg.id = "baby" // don't actually take authenticated actions on the server
@ -113,6 +114,7 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not
db.exec(fs.readFileSync(join(__dirname, "ooye-test-data.sql"), "utf8")) db.exec(fs.readFileSync(join(__dirname, "ooye-test-data.sql"), "utf8"))
require("./addbot.test")
require("../src/db/orm.test") require("../src/db/orm.test")
require("../src/discord/utils.test") require("../src/discord/utils.test")
require("../src/matrix/kstate.test") require("../src/matrix/kstate.test")
@ -138,4 +140,5 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not
require("../src/m2d/converters/emoji-sheet.test") require("../src/m2d/converters/emoji-sheet.test")
require("../src/discord/interactions/invite.test") require("../src/discord/interactions/invite.test")
require("../src/discord/interactions/matrix-info.test") require("../src/discord/interactions/matrix-info.test")
require("../src/discord/interactions/reactions.test")
})() })()