tests: add multiple reactions and delete multiple messages tests

This commit is contained in:
TriForMine
2021-04-10 08:02:58 +02:00
parent 78e5faab6b
commit e9ac9b3c3c
3 changed files with 194 additions and 0 deletions
+133
View File
@@ -0,0 +1,133 @@
import {
addReactions,
cache,
createEmoji,
delay,
DiscordReaction,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false, ordered = false) {
const message = await sendMessage(tempData.channelId, "Hello World!");
// Assertions
assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(5000);
if (!cache.messages.has(message.id)) {
throw new Error(
"The message seemed to be sent but it was not cached.",
);
}
let emojiIds = ["❤","😃"];
if (custom) {
emojiIds = [`<:blamewolf:${
(await createEmoji(
tempData.guildId,
"blamewolf",
"https://cdn.discordapp.com/emojis/814955268123000832.png",
{
name: "blamewolf",
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
roles: [],
},
)).id
}>`,`<:blamewolf2:${
(await createEmoji(
tempData.guildId,
"blamewolf2",
"https://cdn.discordapp.com/emojis/814955268123000832.png",
{
name: "blamewolf2",
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
roles: [],
},
)).id
}>`];
}
if (type === "raw") {
await addReactions(message.channelId, message.id, emojiIds, ordered);
} else {
await message.addReactions(emojiIds, ordered);
}
await delay(5000);
assertEquals(
await cache.messages.get(message.id)?.reactions?.filter((
reaction: DiscordReaction,
) => emojiIds.includes(reaction.emoji?.name)).length,
2,
);
}
Deno.test({
name: "[message] add reactions",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.addReactions()",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] add custom reactions",
async fn() {
await ifItFailsBlameWolf("raw", true);
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.addReactions() with custom reactions",
async fn() {
await ifItFailsBlameWolf("getter", true);
},
...defaultTestOptions,
});
Deno.test({
name: "[message] add ordered reactions",
async fn() {
await ifItFailsBlameWolf("raw", false, true);
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.addReactions() ordered",
async fn() {
await ifItFailsBlameWolf("getter", false, true);
},
...defaultTestOptions,
});
Deno.test({
name: "[message] add ordered custom reactions",
async fn() {
await ifItFailsBlameWolf("raw", true, true);
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.addReactions() with ordered custom reactions",
async fn() {
await ifItFailsBlameWolf("getter", true, true);
},
...defaultTestOptions,
});
+59
View File
@@ -0,0 +1,59 @@
import {cache, delay, deleteMessage, deleteMessages, sendMessage} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists } from "../deps.ts";
async function ifItFailsBlameWolf(reason?: string) {
const message = await sendMessage(tempData.channelId, "Hello World!");
// Assertions
assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(5000);
// Make sure the message was created.
if (!cache.messages.has(message.id)) {
throw new Error(
"The message seemed to be sent but it was not cached.",
);
}
const secondMessage = await sendMessage(tempData.channelId, "Hello World 2!");
// Assertions
assertExists(secondMessage);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(5000);
// Make sure the message was created.
if (!cache.messages.has(secondMessage.id)) {
throw new Error(
"The message seemed to be sent but it was not cached.",
);
}
// Delete the message now
await deleteMessages(tempData.channelId, [message.id,secondMessage.id], reason);
// Wait 5 seconds to give it time for MESSAGE_DELETE event
await delay(5000);
// Make sure it is gone from cache
if (cache.messages.has(message.id) || cache.messages.has(secondMessage.id)) {
throw new Error(
"The message should have been deleted but it is still in cache.",
);
}
}
Deno.test({
name: "[message] delete messages without a reason.",
async fn() {
await ifItFailsBlameWolf();
},
...defaultTestOptions,
});
Deno.test({
name: "[message] delete messages with a reason.",
async fn() {
await ifItFailsBlameWolf( "with a reason");
},
...defaultTestOptions,
});
+2
View File
@@ -17,8 +17,10 @@ import "./channels/delete_channel.ts";
// Messages tests // Messages tests
import "./messages/add_reaction.ts"; import "./messages/add_reaction.ts";
import "./messages/add_reactions.ts";
import "./messages/create_message.ts"; import "./messages/create_message.ts";
import "./messages/delete_message.ts"; import "./messages/delete_message.ts";
import "./messages/delete_messages.ts";
import "./messages/edit_message.ts"; import "./messages/edit_message.ts";
// Final cleanup // Final cleanup