[Unit Test] messages: getMessages

This commit is contained in:
Quentin Nicolini
2021-10-30 14:04:45 +02:00
parent 36c92749d7
commit 6a94fe8f71
2 changed files with 40 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
import { Bot } from "../../../src/bot.ts";
import { assertEquals, assertExists } from "../../deps.ts";
import { delayUntil } from "../../utils.ts";
export async function getMessagesTest(bot: Bot, channelId: bigint, t: Deno.TestContext) {
const message = await bot.helpers.sendMessage(channelId, "Hello World!");
const secondMessage = await bot.helpers.sendMessage(channelId, "Hello World 2!");
const thirdMessage = await bot.helpers.sendMessage(channelId, "Hello World 3!");
// Assertions
assertExists(message);
assertExists(secondMessage);
assertExists(thirdMessage);
// 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) && bot.cache.messages.has(thirdMessage.id));
// Make sure the messages was created.
if (
!bot.cache.messages.has(message.id) ||
!bot.cache.messages.has(secondMessage.id) ||
!bot.cache.messages.has(thirdMessage.id)
) {
throw new Error("The message seemed to be sent but it was not cached.");
}
// Fetch the messages
const fetchedMessages = await bot.helpers.getMessages(channelId, {
after: message.id,
limit: 2,
});
// Check if getMessage has worked
assertEquals(fetchedMessages?.length, 2);
}