From 150fc394437b1f35e1cb4693528f718fcf66dc56 Mon Sep 17 00:00:00 2001 From: Quentin Nicolini Date: Sat, 30 Oct 2021 11:21:33 +0200 Subject: [PATCH] [Unit Test] messages: sendMessage --- tests/helpers/messages/sendMessage.ts | 91 +++++++++++++++++++++++++++ tests/mod.ts | 38 +++++++---- 2 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 tests/helpers/messages/sendMessage.ts diff --git a/tests/helpers/messages/sendMessage.ts b/tests/helpers/messages/sendMessage.ts new file mode 100644 index 000000000..29738fea1 --- /dev/null +++ b/tests/helpers/messages/sendMessage.ts @@ -0,0 +1,91 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; +import {CreateMessage} from "../../../src/types/messages/create_message.ts"; +import { + DiscordMessageComponentTypes +} from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/message_component_types.ts"; +import { + DiscordButtonStyles +} from "https://raw.githubusercontent.com/discordeno/discordeno/cool-stuff/src/types/messages/components/button_styles.ts"; + +async function ifItFailsBlameWolf(bot: Bot, channelId: bigint, content: string | CreateMessage) { + const message = await bot.helpers.sendMessage(channelId, content); + // Assertions + assertExists(message); + // Delay the execution by to allow MESSAGE_CREATE event to be processed + await delayUntil(10000, () => bot.cache.messages.has(message.id)); + // Make sure the message was created. + if (!bot.cache.messages.has(message.id)) { + throw new Error("The message seemed to be sent but it was not cached."); + } +} + +export async function sendMessageWithTextTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId, "Hello World!"); +} + +export async function sendMessageWithComponents(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId, { + content: "Hello World!", + components: [ + { + type: DiscordMessageComponentTypes.ActionRow, + components: [ + { + type: DiscordMessageComponentTypes.Button, + label: "Doc", + style: DiscordButtonStyles.Link, + url: `https://discordeno.mod.land/` + }, + { + type: DiscordMessageComponentTypes.Button, + label: "Server", + style: DiscordButtonStyles.Link, + url: `https://discord.gg/ddeno` + }, + ] + }, + { + type: DiscordMessageComponentTypes.ActionRow, + components: [ + { + type: DiscordMessageComponentTypes.Button, + label: "Hi", + customId: `hi`, + style: DiscordButtonStyles.Primary, + }, + ] + } + ] + }); +} + +export async function sendMessageWithEmbedsTest(bot: Bot, channelId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, channelId, { + embeds: [ + { + title: "Hello World", + description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam at cursus libero. Sed egestas nec ligula sit amet sollicitudin. Curabitur.", + color: 0x00ff00, + footer: { + text: "Discordeno Best Lib" + }, + author: { + name: "Cacahe" + } + }, + { + title: "Goodbye World", + description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean libero enim, blandit tincidunt magna non, auctor pellentesque lacus. Nulla diam.", + color: 0x0000ff, + footer: { + text: "Discordeno Best Lib" + }, + author: { + name: "Wolf" + } + } + ] + }); +} diff --git a/tests/mod.ts b/tests/mod.ts index 1a4fa26ac..6604c5cf1 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -4,6 +4,11 @@ import { assertEquals, assertExists } from "./deps.ts"; import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; import { editMessageTest } from "./helpers/messages/editMessage.ts"; import { delayUntil } from "./utils.ts"; +import { + sendMessageWithComponents, + sendMessageWithEmbedsTest, + sendMessageWithTextTest +} from "./helpers/messages/sendMessage.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST import "./local.ts"; @@ -77,20 +82,29 @@ Deno.test("[Bot] - Starting Tests", async (t) => { // ALL MESSAGE RELATED TESTS THAT DEPEND ON AN EXISTING CHANNEL await t.step("Message related tests", async (t) => { - const message = await bot.helpers.sendMessage(channel.id, "Testing"); - - // Assertions - assertExists(message); - - // Delay the execution to allow MESSAGE_CREATE event to be processed - await delayUntil(10000, () => bot.cache.messages.has(message.id)); - - if (!bot.cache.messages.has(message.id)) { - throw new Error("The message seemed to be sent but it was not cached."); - } - // CONDUCT ALL TESTS RELATED TO A MESSAGE HERE await Promise.all([ + t.step({ + name: "[message] send message with text", + fn: async (t) => { + await sendMessageWithTextTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[message] send message with embeds", + fn: async (t) => { + await sendMessageWithEmbedsTest(bot, channel.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[message] send message with components", + fn: async (t) => { + await sendMessageWithComponents(bot, channel.id, t); + }, + ...sanitizeMode, + }), t.step({ name: "[message] delete message without a reason", fn: async (t) => {