mirror of
https://gitdab.com/cadence/out-of-your-element.git
synced 2025-09-11 12:43:01 +02:00
Test invite interaction & code coverage
This commit is contained in:
parent
65170c1282
commit
bad8c5b8c2
15 changed files with 407 additions and 161 deletions
228
src/discord/interactions/invite.test.js
Normal file
228
src/discord/interactions/invite.test.js
Normal file
|
@ -0,0 +1,228 @@
|
|||
const {test} = require("supertape")
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const {db, discord} = require("../../passthrough")
|
||||
const {MatrixServerError} = require("../../matrix/mreq")
|
||||
const {_interact, _interactButton} = require("./invite")
|
||||
|
||||
test("invite: checks for missing matrix ID", async t => {
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: []
|
||||
},
|
||||
channel: discord.channels.get("0"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {})
|
||||
t.equal(msg.data.content, "You have to say the Matrix ID of the person you want to invite. Matrix IDs look like this: `@username:example.org`")
|
||||
})
|
||||
|
||||
test("invite: checks for invalid matrix ID", async t => {
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: [{
|
||||
name: "user",
|
||||
type: DiscordTypes.ApplicationCommandOptionType.String,
|
||||
value: "@cadence"
|
||||
}]
|
||||
},
|
||||
channel: discord.channels.get("0"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {})
|
||||
t.equal(msg.data.content, "You have to say the Matrix ID of the person you want to invite. Matrix IDs look like this: `@username:example.org`")
|
||||
})
|
||||
|
||||
test("invite: checks if channel exists or is autocreatable", async t => {
|
||||
db.prepare("UPDATE guild_active SET autocreate = 0").run()
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: [{
|
||||
name: "user",
|
||||
type: DiscordTypes.ApplicationCommandOptionType.String,
|
||||
value: "@cadence:cadence.moe"
|
||||
}]
|
||||
},
|
||||
channel: discord.channels.get("498323546729086986"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {})
|
||||
t.equal(msg.data.content, "This channel isn't bridged, so you can't invite Matrix users yet. Try turning on automatic room-creation or link a Matrix room in the website.")
|
||||
db.prepare("UPDATE guild_active SET autocreate = 1").run()
|
||||
})
|
||||
|
||||
test("invite: checks if user is already invited to space", async t => {
|
||||
let called = 0
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: [{
|
||||
name: "user",
|
||||
type: DiscordTypes.ApplicationCommandOptionType.String,
|
||||
value: "@cadence:cadence.moe"
|
||||
}]
|
||||
},
|
||||
channel: discord.channels.get("112760669178241024"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {
|
||||
api: {
|
||||
getStateEvent: async (roomID, type, stateKey) => {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
return {
|
||||
displayname: "cadence",
|
||||
membership: "invite"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(msg.data.content, "`@cadence:cadence.moe` already has an invite, which they haven't accepted yet.")
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("invite: invites if user is not in space", async t => {
|
||||
let called = 0
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: [{
|
||||
name: "user",
|
||||
type: DiscordTypes.ApplicationCommandOptionType.String,
|
||||
value: "@cadence:cadence.moe"
|
||||
}]
|
||||
},
|
||||
channel: discord.channels.get("112760669178241024"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {
|
||||
api: {
|
||||
getStateEvent: async (roomID, type, stateKey) => {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
throw new MatrixServerError("State event doesn't exist or something")
|
||||
},
|
||||
inviteToRoom: async (roomID, mxid) => {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(mxid, "@cadence:cadence.moe")
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(msg.data.content, "You invited `@cadence:cadence.moe` to the server.")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("invite: prompts to invite to room (if never joined)", async t => {
|
||||
let called = 0
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: [{
|
||||
name: "user",
|
||||
type: DiscordTypes.ApplicationCommandOptionType.String,
|
||||
value: "@cadence:cadence.moe"
|
||||
}]
|
||||
},
|
||||
channel: discord.channels.get("112760669178241024"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {
|
||||
api: {
|
||||
getStateEvent: async (roomID, type, stateKey) => {
|
||||
called++
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
if (roomID === "!jjWAGMeQdNrVZSSfvz:cadence.moe") { // space ID
|
||||
return {
|
||||
displayname: "cadence",
|
||||
membership: "join"
|
||||
}
|
||||
} else {
|
||||
throw new MatrixServerError("State event doesn't exist or something")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(msg.data.content, "`@cadence:cadence.moe` is already in this server. Would you like to additionally invite them to this specific channel?")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("invite: prompts to invite to room (if left)", async t => {
|
||||
let called = 0
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: [{
|
||||
name: "user",
|
||||
type: DiscordTypes.ApplicationCommandOptionType.String,
|
||||
value: "@cadence:cadence.moe"
|
||||
}]
|
||||
},
|
||||
channel: discord.channels.get("112760669178241024"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {
|
||||
api: {
|
||||
getStateEvent: async (roomID, type, stateKey) => {
|
||||
called++
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
if (roomID === "!jjWAGMeQdNrVZSSfvz:cadence.moe") { // space ID
|
||||
return {
|
||||
displayname: "cadence",
|
||||
membership: "join"
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
displayname: "cadence",
|
||||
membership: "leave"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(msg.data.content, "`@cadence:cadence.moe` is already in this server. Would you like to additionally invite them to this specific channel?")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("invite button: invites to room when button clicked", async t => {
|
||||
let called = 0
|
||||
const msg = await _interactButton({
|
||||
channel: discord.channels.get("112760669178241024"),
|
||||
message: {
|
||||
content: "`@cadence:cadence.moe` is already in this server. Would you like to additionally invite them to this specific channel?"
|
||||
}
|
||||
}, {
|
||||
api: {
|
||||
inviteToRoom: async (roomID, mxid) => {
|
||||
called++
|
||||
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe") // room ID
|
||||
t.equal(mxid, "@cadence:cadence.moe")
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(msg.data.content, "You invited `@cadence:cadence.moe` to the channel.")
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("invite: no-op if in room and space", async t => {
|
||||
let called = 0
|
||||
const msg = await _interact({
|
||||
data: {
|
||||
options: [{
|
||||
name: "user",
|
||||
type: DiscordTypes.ApplicationCommandOptionType.String,
|
||||
value: "@cadence:cadence.moe"
|
||||
}]
|
||||
},
|
||||
channel: discord.channels.get("112760669178241024"),
|
||||
guild_id: "112760669178241024"
|
||||
}, {
|
||||
api: {
|
||||
getStateEvent: async (roomID, type, stateKey) => {
|
||||
called++
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
return {
|
||||
displayname: "cadence",
|
||||
membership: "join"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(msg.data.content, "`@cadence:cadence.moe` is already in this server and this channel.")
|
||||
t.equal(called, 2)
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue