[Unit Test] messages: deleteMessages

This commit is contained in:
Quentin Nicolini
2021-10-30 11:35:34 +02:00
parent 36c92749d7
commit 4720fd0c77
2 changed files with 51 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
import { Bot } from "../../../src/bot.ts";
import { assertExists } from "../../deps.ts";
import { delayUntil } from "../../utils.ts";
async function ifItFailsBlameWolf(bot: Bot, channelId: bigint, reason?: string) {
const message = await bot.helpers.sendMessage(channelId, "Hello World!");
const secondMessage = await bot.helpers.sendMessage(channelId, "Hello World 2!");
// Assertions
assertExists(message);
assertExists(secondMessage);
// Delay the execution by to allow MESSAGE_CREATE event to be processed
await delayUntil(10000, () => bot.cache.messages.has(message.id) && bot.cache.messages.has(secondMessage.id));
// Make sure the message was created.
if (!bot.cache.messages.has(message.id) || !bot.cache.messages.has(secondMessage.id)) {
throw new Error(`The message seemed to be sent but it was not cached. Reason: ${reason}`);
}
// Delete the message now
await bot.helpers.deleteMessages(channelId, [message.id, secondMessage.id], reason);
// Wait to give it time for MESSAGE_DELETE event
await delayUntil(10000, () => !bot.cache.messages.has(message.id) && !bot.cache.messages.has(secondMessage.id));
// Make sure it is gone from cache
if (bot.cache.messages.has(message.id) || bot.cache.messages.has(secondMessage.id)) {
throw new Error("The messages should have been deleted but they are still in cache.");
}
}
export async function deleteMessagesWithoutReasonTest(bot: Bot, channelId: bigint, t: Deno.TestContext) {
await ifItFailsBlameWolf(bot, channelId);
}
export async function deleteMessagesWithReasonTest(bot: Bot, channelId: bigint, t: Deno.TestContext) {
await ifItFailsBlameWolf(bot, channelId, "with a reason");
}
+15
View File
@@ -2,6 +2,7 @@ import { TOKEN } from "../configs.ts";
import { createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts"; import { createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts";
import { assertEquals, assertExists } from "./deps.ts"; import { assertEquals, assertExists } from "./deps.ts";
import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts";
import { deleteMessagesWithoutReasonTest, deleteMessagesWithReasonTest } from "./helpers/messages/deleteMessages.ts";
import { delayUntil } from "./utils.ts"; import { delayUntil } from "./utils.ts";
// CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST
@@ -104,6 +105,20 @@ Deno.test("[Bot] - Starting Tests", async (t) => {
}, },
...sanitizeMode, ...sanitizeMode,
}), }),
t.step({
name: "[message] delete messages without a reason",
fn: async (t) => {
await deleteMessagesWithoutReasonTest(bot, channel.id, t);
},
...sanitizeMode,
}),
t.step({
name: "[message] delete messages with a reason",
fn: async (t) => {
await deleteMessagesWithReasonTest(bot, channel.id, t);
},
...sanitizeMode,
}),
]); ]);
}); });
}); });