mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
reaction tests
This commit is contained in:
@@ -1,77 +0,0 @@
|
|||||||
import { Bot } from "../../../src/bot.ts";
|
|
||||||
import { assertEquals, assertExists } from "../../deps.ts";
|
|
||||||
import { delayUntil } from "../../utils.ts";
|
|
||||||
|
|
||||||
const reactionCounters = new Map<bigint, number>();
|
|
||||||
|
|
||||||
export async function addReactionTest(
|
|
||||||
bot: Bot,
|
|
||||||
guildId: bigint,
|
|
||||||
channelId: bigint,
|
|
||||||
options: { custom: boolean; single: boolean; ordered: boolean },
|
|
||||||
t: Deno.TestContext
|
|
||||||
) {
|
|
||||||
const message = await bot.helpers.sendMessage(channelId, "Hello World!");
|
|
||||||
|
|
||||||
// Assertions
|
|
||||||
assertExists(message);
|
|
||||||
|
|
||||||
// Delay the execution by 5 seconds 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.");
|
|
||||||
}
|
|
||||||
|
|
||||||
let emojiId = "❤";
|
|
||||||
let emojiIds = ["❤", "😃"];
|
|
||||||
|
|
||||||
if (options.custom) {
|
|
||||||
if (options.single) {
|
|
||||||
emojiId = `<:blamewolf:${
|
|
||||||
(
|
|
||||||
await bot.helpers.createEmoji(guildId, {
|
|
||||||
name: "blamewolf",
|
|
||||||
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
|
||||||
// roles: [],
|
|
||||||
})
|
|
||||||
).id
|
|
||||||
}>`;
|
|
||||||
} else {
|
|
||||||
emojiIds = [
|
|
||||||
`<:blamewolf:${
|
|
||||||
(
|
|
||||||
await bot.helpers.createEmoji(guildId, {
|
|
||||||
name: "blamewolf",
|
|
||||||
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
|
||||||
// roles: [],
|
|
||||||
})
|
|
||||||
).id
|
|
||||||
}>`,
|
|
||||||
`<:blamewolf2:${
|
|
||||||
(
|
|
||||||
await bot.helpers.createEmoji(guildId, {
|
|
||||||
name: "blamewolf2",
|
|
||||||
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
|
||||||
// roles: [],
|
|
||||||
})
|
|
||||||
).id
|
|
||||||
}>`,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
reactionCounters.set(message.id, 0);
|
|
||||||
|
|
||||||
bot.events.reactionAdd = function (bot, payload) {
|
|
||||||
const current = reactionCounters.get(payload.messageId) || 0;
|
|
||||||
reactionCounters.set(payload.messageId, current + 1);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options.single) await bot.helpers.addReaction(message.channelId, message.id, emojiId);
|
|
||||||
else await bot.helpers.addReactions(message.channelId, message.id, emojiIds, options.ordered);
|
|
||||||
|
|
||||||
await delayUntil(10000, () => reactionCounters.get(message.id) === (options.single ? 1 : emojiIds.length));
|
|
||||||
|
|
||||||
assertEquals(reactionCounters.get(message.id), options.single ? 1 : emojiIds.length);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
import { Bot } from "../../../src/bot.ts";
|
||||||
|
import { assertEquals, assertExists } from "../../deps.ts";
|
||||||
|
import { delayUntil } from "../../utils.ts";
|
||||||
|
|
||||||
|
const reactionCounters = new Map<bigint, number>();
|
||||||
|
|
||||||
|
export async function addReactionTest(
|
||||||
|
bot: Bot,
|
||||||
|
guildId: bigint,
|
||||||
|
channelId: bigint,
|
||||||
|
options: { custom: boolean; single: boolean; ordered: boolean },
|
||||||
|
t: Deno.TestContext
|
||||||
|
) {
|
||||||
|
const message = await bot.helpers.sendMessage(channelId, "Hello World!");
|
||||||
|
|
||||||
|
// Assertions
|
||||||
|
assertExists(message);
|
||||||
|
|
||||||
|
// Delay the execution to allow 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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let emojiId = "❤";
|
||||||
|
let emojiIds = ["❤", "😃"];
|
||||||
|
|
||||||
|
if (options.custom) {
|
||||||
|
if (options.single) {
|
||||||
|
emojiId = `<:blamewolf:${
|
||||||
|
(
|
||||||
|
await bot.helpers.createEmoji(guildId, {
|
||||||
|
name: "blamewolf",
|
||||||
|
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
||||||
|
// roles: [],
|
||||||
|
})
|
||||||
|
).id
|
||||||
|
}>`;
|
||||||
|
} else {
|
||||||
|
emojiIds = [
|
||||||
|
`<:blamewolf:${
|
||||||
|
(
|
||||||
|
await bot.helpers.createEmoji(guildId, {
|
||||||
|
name: "blamewolf",
|
||||||
|
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
||||||
|
// roles: [],
|
||||||
|
})
|
||||||
|
).id
|
||||||
|
}>`,
|
||||||
|
`<:blamewolf2:${
|
||||||
|
(
|
||||||
|
await bot.helpers.createEmoji(guildId, {
|
||||||
|
name: "blamewolf2",
|
||||||
|
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
|
||||||
|
// roles: [],
|
||||||
|
})
|
||||||
|
).id
|
||||||
|
}>`,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reactionCounters.set(message.id, 0);
|
||||||
|
|
||||||
|
bot.events.reactionAdd = function (bot, payload) {
|
||||||
|
const current = reactionCounters.get(payload.messageId) || 0;
|
||||||
|
reactionCounters.set(payload.messageId, current + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (options.single) await bot.helpers.addReaction(message.channelId, message.id, emojiId);
|
||||||
|
else await bot.helpers.addReactions(message.channelId, message.id, emojiIds, options.ordered);
|
||||||
|
|
||||||
|
await delayUntil(10000, () => reactionCounters.get(message.id) === (options.single ? 1 : emojiIds.length));
|
||||||
|
|
||||||
|
assertEquals(reactionCounters.get(message.id), options.single ? 1 : emojiIds.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeAllReactionTests(bot: Bot, channelId: bigint, t: Deno.TestContext) {
|
||||||
|
const message = await bot.helpers.sendMessage(channelId, "Hello World!");
|
||||||
|
|
||||||
|
// Assertions
|
||||||
|
assertExists(message);
|
||||||
|
|
||||||
|
// Delay the execution to allow 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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
reactionCounters.set(message.id, 0);
|
||||||
|
|
||||||
|
bot.events.reactionRemoveAll = function (bot, payload) {
|
||||||
|
reactionCounters.set(payload.messageId, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
bot.events.reactionAdd = function (bot, payload) {
|
||||||
|
const current = reactionCounters.get(payload.messageId) || 0;
|
||||||
|
reactionCounters.set(payload.messageId, current + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add reactions to the message
|
||||||
|
await bot.helpers.addReactions(message.channelId, message.id, ["❤", "😃", "🤫"]);
|
||||||
|
// Delay the execution to allow event to be processed
|
||||||
|
await delayUntil(10000, () => reactionCounters.get(message.id) === 3);
|
||||||
|
|
||||||
|
// Be sure that the message has the reactions
|
||||||
|
assertEquals(reactionCounters.get(message.id), 3);
|
||||||
|
|
||||||
|
await bot.helpers.removeAllReactions(message.channelId, message.id);
|
||||||
|
|
||||||
|
// Delay the execution to allow event to be processed
|
||||||
|
await delayUntil(10000, () => reactionCounters.get(message.id) === 0);
|
||||||
|
|
||||||
|
// Check if the reactions has been deleted
|
||||||
|
assertEquals(reactionCounters.get(message.id), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeReactionTest(bot: Bot, channelId: bigint, t: Deno.TestContext) {
|
||||||
|
const message = await bot.helpers.sendMessage(channelId, "Hello World!");
|
||||||
|
|
||||||
|
// Assertions
|
||||||
|
assertExists(message);
|
||||||
|
|
||||||
|
// Delay the execution to allow 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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
reactionCounters.set(message.id, 0);
|
||||||
|
|
||||||
|
bot.events.reactionRemove = function (bot, payload) {
|
||||||
|
const current = reactionCounters.get(message.id);
|
||||||
|
reactionCounters.set(payload.messageId, (current || 0) - 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
bot.events.reactionAdd = function (bot, payload) {
|
||||||
|
const current = reactionCounters.get(payload.messageId) || 0;
|
||||||
|
reactionCounters.set(payload.messageId, current + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add reactions to the message
|
||||||
|
await bot.helpers.addReactions(message.channelId, message.id, ["❤", "😃", "🤫"]);
|
||||||
|
// Delay the execution to allow event to be processed
|
||||||
|
await delayUntil(10000, () => reactionCounters.get(message.id) === 3);
|
||||||
|
|
||||||
|
// Be sure that the message has the reactions
|
||||||
|
assertEquals(reactionCounters.get(message.id), 3);
|
||||||
|
|
||||||
|
await bot.helpers.removeReaction(message.channelId, message.id, "😃");
|
||||||
|
|
||||||
|
// Delay the execution to allow event to be processed
|
||||||
|
await delayUntil(10000, () => reactionCounters.get(message.id) === 2);
|
||||||
|
|
||||||
|
// Check if the reactions has been deleted
|
||||||
|
assertEquals(reactionCounters.get(message.id), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeReactionEmojiTest(bot: Bot, channelId: bigint, t: Deno.TestContext) {
|
||||||
|
const message = await bot.helpers.sendMessage(channelId, "Hello World!");
|
||||||
|
|
||||||
|
// Assertions
|
||||||
|
assertExists(message);
|
||||||
|
|
||||||
|
// Delay the execution to allow 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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
reactionCounters.set(message.id, 0);
|
||||||
|
|
||||||
|
bot.events.reactionAdd
|
||||||
|
|
||||||
|
bot.events.reactionRemoveEmoji = function (bot, payload) {
|
||||||
|
reactionCounters.set(payload.messageId, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
bot.events.reactionAdd = function (bot, payload) {
|
||||||
|
const current = reactionCounters.get(payload.messageId) || 0;
|
||||||
|
reactionCounters.set(payload.messageId, current + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add reactions to the message
|
||||||
|
await bot.helpers.addReactions(message.channelId, message.id, ["❤", "😃"]);
|
||||||
|
// Delay the execution to allow event to be processed
|
||||||
|
await delayUntil(10000, () => reactionCounters.get(message.id) === 2);
|
||||||
|
|
||||||
|
// Be sure that the message has the reactions
|
||||||
|
assertEquals(reactionCounters.get(message.id), 2);
|
||||||
|
|
||||||
|
await bot.helpers.removeReactionEmoji(message.channelId, message.id, "😃");
|
||||||
|
|
||||||
|
// Delay the execution to allow event to be processed
|
||||||
|
await delayUntil(10000, () => reactionCounters.get(message.id) === 0);
|
||||||
|
|
||||||
|
// Check if the reactions has been deleted
|
||||||
|
assertEquals(reactionCounters.get(message.id), 0);
|
||||||
|
}
|
||||||
+23
-1
@@ -14,10 +14,11 @@ import {
|
|||||||
// CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST
|
// CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST
|
||||||
import "./local.ts";
|
import "./local.ts";
|
||||||
import { getMessageTest } from "./helpers/messages/getMessage.ts";
|
import { getMessageTest } from "./helpers/messages/getMessage.ts";
|
||||||
import { addReactionTest } from "./helpers/messages/addReaction.ts";
|
import { addReactionTest } from "./helpers/messages/reactions.ts";
|
||||||
import { editMessageTest } from "./helpers/messages/editMessage.ts";
|
import { editMessageTest } from "./helpers/messages/editMessage.ts";
|
||||||
import { fetchSingleMemberTest } from "./helpers/members/fetchMembers.ts";
|
import { fetchSingleMemberTest } from "./helpers/members/fetchMembers.ts";
|
||||||
import { pinMessageTests } from "./helpers/messages/pin.ts";
|
import { pinMessageTests } from "./helpers/messages/pin.ts";
|
||||||
|
import { removeAllReactionTests, removeReactionEmojiTest, removeReactionTest } from "./helpers/messages/reactions.ts";
|
||||||
|
|
||||||
Deno.test("[Bot] - Starting Tests", async (t) => {
|
Deno.test("[Bot] - Starting Tests", async (t) => {
|
||||||
// CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS
|
// CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS
|
||||||
@@ -206,6 +207,27 @@ Deno.test("[Bot] - Starting Tests", async (t) => {
|
|||||||
},
|
},
|
||||||
...sanitizeMode,
|
...sanitizeMode,
|
||||||
}),
|
}),
|
||||||
|
t.step({
|
||||||
|
name: "[message] remove a reaction.",
|
||||||
|
fn: async (t) => {
|
||||||
|
await removeReactionTest(bot, channel.id, t);
|
||||||
|
},
|
||||||
|
...sanitizeMode,
|
||||||
|
}),
|
||||||
|
t.step({
|
||||||
|
name: "[message] remove all reactions.",
|
||||||
|
fn: async (t) => {
|
||||||
|
await removeAllReactionTests(bot, channel.id, t);
|
||||||
|
},
|
||||||
|
...sanitizeMode,
|
||||||
|
}),
|
||||||
|
t.step({
|
||||||
|
name: "[message] remove emoji reactions.",
|
||||||
|
fn: async (t) => {
|
||||||
|
await removeReactionEmojiTest(bot, channel.id, t);
|
||||||
|
},
|
||||||
|
...sanitizeMode,
|
||||||
|
}),
|
||||||
t.step({
|
t.step({
|
||||||
name: "[message] pin a message",
|
name: "[message] pin a message",
|
||||||
fn: async (t) => {
|
fn: async (t) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user