tests: add all messages tests

This commit is contained in:
TriForMine
2021-04-10 10:54:30 +02:00
parent 69b8fe4545
commit 63675aa818
10 changed files with 499 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
import { cache, delay, getMessage, sendMessage } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
Deno.test({
name: "[message] fetch a message",
async fn() {
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.",
);
}
// Fetch the message
const fetchedMessage = await getMessage(tempData.channelId, message.id);
// Check if getMessage has worked
assertEquals(fetchedMessage.id, message.id);
assertEquals(fetchedMessage.content, message.content);
},
...defaultTestOptions,
});
+44
View File
@@ -0,0 +1,44 @@
import { cache, delay, getMessages, sendMessage } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
Deno.test({
name: "[message] fetch messages",
async fn() {
const message = await sendMessage(tempData.channelId, "Hello World!");
const secondMessage = await sendMessage(
tempData.channelId,
"Hello World 2!",
);
const thirdMessage = await sendMessage(
tempData.channelId,
"Hello World 3!",
);
// Assertions
assertExists(message);
assertExists(secondMessage);
assertExists(thirdMessage);
// 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) ||
!cache.messages.has(secondMessage.id) ||
!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 getMessages(tempData.channelId, {
after: message.id,
limit: 2,
});
// Check if getMessages has worked
assertEquals(fetchedMessages.length, 2);
},
...defaultTestOptions,
});
+39
View File
@@ -0,0 +1,39 @@
import {
addReaction,
cache,
delay,
getReactions,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
Deno.test({
name: "[message] fetch reactions",
async fn() {
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.",
);
}
await addReaction(message.channelId, message.id, "❤");
// Fetch the message
const fetchedReactions = await getReactions(
tempData.channelId,
message.id,
"❤",
);
// Check if getMessage has worked
assertEquals(fetchedReactions.size, 1);
},
...defaultTestOptions,
});
+48
View File
@@ -0,0 +1,48 @@
import { cache, delay, getPins, pin, sendMessage } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
import { DiscordenoMessage } from "../../src/structures/message.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") {
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.",
);
}
if (type === "raw") {
await pin(message.channelId, message.id);
} else {
await message.pin();
}
const pins = await getPins(tempData.channelId);
assertEquals(
pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length,
1,
);
}
Deno.test({
name: "[message] pin a message",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.pin()",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});
+66
View File
@@ -0,0 +1,66 @@
import {
addReactions,
cache,
delay,
removeAllReactions,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") {
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.",
);
}
// Add reactions to the message
await addReactions(message.channelId, message.id, ["❤", "😃", "🤫"]);
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(5000);
// Be sure that the message has the reactions
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
3,
);
if (type === "raw") {
await removeAllReactions(message.channelId, message.id);
} else {
await message.removeAllReactions();
}
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(5000);
// Check if the reactions has been deleted
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
0,
);
}
Deno.test({
name: "[message] remove all reactions",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.removeAllReactions()",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});
+66
View File
@@ -0,0 +1,66 @@
import {
addReaction,
cache,
delay,
removeReaction,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") {
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.",
);
}
// Add reactions to the message
await addReaction(message.channelId, message.id, "❤");
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(5000);
// Be sure that the message has the reactions
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
1,
);
if (type === "raw") {
await removeReaction(message.channelId, message.id, "❤");
} else {
await message.removeReaction("❤");
}
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(5000);
// Check if the reactions has been deleted
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
0,
);
}
Deno.test({
name: "[message] remove a reaction",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.removeReaction()",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});
+67
View File
@@ -0,0 +1,67 @@
import {
addReaction,
cache,
delay,
removeReaction,
removeReactionEmoji,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") {
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.",
);
}
// Add reactions to the message
await addReaction(message.channelId, message.id, "❤");
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(5000);
// Be sure that the message has the reactions
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
1,
);
if (type === "raw") {
await removeReactionEmoji(message.channelId, message.id, "❤");
} else {
await message.removeReactionEmoji("❤");
}
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(5000);
// Check if the reactions has been deleted
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
0,
);
}
Deno.test({
name: "[message] remove a reaction emoji",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
Deno.test({
name: "[message] message.removeReactionEmoji()",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});
+72
View File
@@ -0,0 +1,72 @@
import {
addReaction,
cache,
delay,
removeReaction,
removeUserReaction,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") {
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.",
);
}
// Add reactions to the message
await addReaction(message.channelId, message.id, "❤");
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(5000);
// Be sure that the message has the reactions
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
1,
);
if (type === "raw") {
await removeUserReaction(
message.channelId,
message.id,
"❤",
message.author.id,
);
} else {
//await message.removeUserReaction("❤", message.author.id);
}
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(5000);
// Check if the reactions has been deleted
assertEquals(
await cache.messages.get(message.id)?.reactions?.length,
0,
);
}
Deno.test({
name: "[message] remove a user reaction",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
/*
Deno.test({
name: "[message] message.removeReaction()",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});*/
+60
View File
@@ -0,0 +1,60 @@
import { cache, delay, getPins, pin, sendMessage, unpin } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts";
import { DiscordenoMessage } from "../../src/structures/message.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") {
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.",
);
}
await pin(message.channelId, message.id);
// Be sure that the pin got added
const pins = await getPins(tempData.channelId);
assertEquals(
pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length,
1,
);
if (type === "raw") {
await unpin(message.channelId, message.id);
} else {
//await message.unpin();
}
// Be sure that the pin got removed
const removedPins = await getPins(tempData.channelId);
assertEquals(
removedPins.filter((msg: DiscordenoMessage) => msg.id === message.id)
.length,
0,
);
}
Deno.test({
name: "[message] unpin a message",
async fn() {
await ifItFailsBlameWolf("raw");
},
...defaultTestOptions,
});
/*
Deno.test({
name: "[message] message.unpin()",
async fn() {
await ifItFailsBlameWolf("getter");
},
...defaultTestOptions,
});*/
+9
View File
@@ -22,6 +22,15 @@ import "./messages/create_message.ts";
import "./messages/delete_message.ts";
import "./messages/delete_messages.ts";
import "./messages/edit_message.ts";
import "./messages/get_message.ts";
import "./messages/get_messages.ts";
import "./messages/get_reactions.ts";
import "./messages/pin_message.ts";
import "./messages/remove_all_reactions.ts";
import "./messages/remove_reaction.ts";
import "./messages/remove_reaction_emoji.ts";
import "./messages/remove_user_reaction.ts";
import "./messages/unpin_message.ts";
// Final cleanup
import "./guilds/delete_server.ts";