forked from cadence/out-of-your-element
Add tests for reactions interaction
This commit is contained in:
parent
61803c3838
commit
33915a595d
6 changed files with 121 additions and 14 deletions
|
@ -1,11 +1,8 @@
|
|||
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")
|
||||
const {_interact} = 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({
|
||||
data: {
|
||||
target_id: "0"
|
||||
|
|
|
@ -8,19 +8,22 @@ const api = sync.require("../../matrix/api")
|
|||
/** @type {import("../../m2d/converters/utils")} */
|
||||
const utils = sync.require("../../m2d/converters/utils")
|
||||
|
||||
/** @param {DiscordTypes.APIContextMenuGuildInteraction} interaction */
|
||||
/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */
|
||||
async function interact({id, token, data}) {
|
||||
/**
|
||||
* @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction
|
||||
* @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")
|
||||
.select("event_id", "room_id").where({message_id: data.target_id}).get()
|
||||
if (!row) {
|
||||
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 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) {
|
||||
return discord.snow.interaction.createInteractionResponse(id, token, {
|
||||
return {
|
||||
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: "Nobody from Matrix reacted to this message.",
|
||||
flags: DiscordTypes.MessageFlags.Ephemeral
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return discord.snow.interaction.createInteractionResponse(id, token, {
|
||||
return {
|
||||
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: [...inverted.entries()].map(([key, value]) => `${key} ⮞ ${value.join(" ⬩ ")}`).join("\n"),
|
||||
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
|
||||
|
|
87
src/discord/interactions/reactions.test.js
Normal file
87
src/discord/interactions/reactions.test.js
Normal 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)
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue