tests: use delayUntil instead of delay

This commit is contained in:
TriForMine
2021-04-10 23:37:24 +02:00
parent e163e2d109
commit 0739826e7a
20 changed files with 138 additions and 120 deletions
+7 -7
View File
@@ -3,7 +3,7 @@ import { assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { categoryChildren, createChannel } from "../../src/helpers/mod.ts"; import { categoryChildren, createChannel } from "../../src/helpers/mod.ts";
import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts"; import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts";
import { delay } from "../../src/util/utils.ts"; import { delayUntil } from "../util/delay_until.ts";
Deno.test({ Deno.test({
name: "[channel] category channel ids", name: "[channel] category channel ids",
@@ -16,11 +16,11 @@ Deno.test({
// Assertions // Assertions
assertExists(category); assertExists(category);
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.channels.has(category.id));
if (!cache.channels.has(category.id)) { if (!cache.channels.has(category.id)) {
throw new Error( throw new Error(
"The channel seemed to be created but it was not cached." "The channel seemed to be created but it was not cached.",
); );
} }
@@ -31,15 +31,15 @@ Deno.test({
name: `Discordeno-test-${num}`, name: `Discordeno-test-${num}`,
parentId: category.id, parentId: category.id,
}) })
) ),
); );
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
await delay(3000); delayUntil(3000, () => channels.every((c) => cache.channels.has(c.id)));
// If every channel is not present in the cache, error out // If every channel is not present in the cache, error out
if (!channels.every((c) => cache.channels.has(c.id))) { if (!channels.every((c) => cache.channels.has(c.id))) {
throw new Error( throw new Error(
"The channels seemed to be created but it was not cached." "The channels seemed to be created but it was not cached.",
); );
} }
@@ -49,7 +49,7 @@ Deno.test({
!channels.every((c) => ids.has(c.id)) !channels.every((c) => ids.has(c.id))
) { ) {
throw new Error( throw new Error(
"The category channel ids did not match with the category channels." "The category channel ids did not match with the category channels.",
); );
} }
}, },
+9 -9
View File
@@ -3,8 +3,8 @@ import { assertEquals, assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts"; import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts";
import { CreateGuildChannel } from "../../src/types/guilds/create_guild_channel.ts"; import { CreateGuildChannel } from "../../src/types/guilds/create_guild_channel.ts";
import { delay } from "../../src/util/utils.ts";
import { createChannel } from "../../src/helpers/channels/create_channel.ts"; import { createChannel } from "../../src/helpers/channels/create_channel.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) { async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
const channel = await createChannel(tempData.guildId, options); const channel = await createChannel(tempData.guildId, options);
@@ -16,7 +16,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
if (save) tempData.channelId = channel.id; if (save) tempData.channelId = channel.id;
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.channels.has(channel.id));
if (!cache.channels.has(channel.id)) { if (!cache.channels.has(channel.id)) {
throw new Error("The channel seemed to be created but it was not cached."); throw new Error("The channel seemed to be created but it was not cached.");
@@ -24,13 +24,13 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
if (options.topic && channel.topic !== options.topic) { if (options.topic && channel.topic !== options.topic) {
throw new Error( throw new Error(
"The channel was supposed to have a topic but it does not appear to be the same topic." "The channel was supposed to have a topic but it does not appear to be the same topic.",
); );
} }
if (options.bitrate && channel.bitrate !== options.bitrate) { if (options.bitrate && channel.bitrate !== options.bitrate) {
throw new Error( throw new Error(
"The channel was supposed to have a bitrate but it does not appear to be the same bitrate." "The channel was supposed to have a bitrate but it does not appear to be the same bitrate.",
); );
} }
} }
@@ -51,7 +51,7 @@ Deno.test({
name: "Discordeno-test", name: "Discordeno-test",
type: DiscordChannelTypes.GUILD_CATEGORY, type: DiscordChannelTypes.GUILD_CATEGORY,
}, },
true true,
); );
}, },
...defaultTestOptions, ...defaultTestOptions,
@@ -81,7 +81,7 @@ Deno.test({
name: "Discordeno-test", name: "Discordeno-test",
type: DiscordChannelTypes.GUILD_VOICE, type: DiscordChannelTypes.GUILD_VOICE,
}, },
true true,
); );
}, },
...defaultTestOptions, ...defaultTestOptions,
@@ -96,7 +96,7 @@ Deno.test({
type: DiscordChannelTypes.GUILD_VOICE, type: DiscordChannelTypes.GUILD_VOICE,
bitrate: 32000, bitrate: 32000,
}, },
true true,
); );
}, },
...defaultTestOptions, ...defaultTestOptions,
@@ -111,7 +111,7 @@ Deno.test({
type: DiscordChannelTypes.GUILD_VOICE, type: DiscordChannelTypes.GUILD_VOICE,
userLimit: 32, userLimit: 32,
}, },
true true,
); );
}, },
...defaultTestOptions, ...defaultTestOptions,
@@ -125,7 +125,7 @@ Deno.test({
name: "Discordeno-test", name: "Discordeno-test",
rateLimitPerUser: 2423, rateLimitPerUser: 2423,
}, },
true true,
); );
}, },
...defaultTestOptions, ...defaultTestOptions,
+9 -9
View File
@@ -1,8 +1,8 @@
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { createChannel } from "../../src/helpers/channels/create_channel.ts"; import { createChannel } from "../../src/helpers/channels/create_channel.ts";
import { deleteChannel } from "../../src/helpers/channels/delete_channel.ts"; import { deleteChannel } from "../../src/helpers/channels/delete_channel.ts";
import { delay } from "../../src/util/utils.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { delayUntil } from "../util/delay_until.ts";
Deno.test({ Deno.test({
name: "[channel] delete a channel without a reason.", name: "[channel] delete a channel without a reason.",
@@ -12,22 +12,22 @@ Deno.test({
name: "delete-channel", name: "delete-channel",
}); });
// wait 5 seconds to give it time for CHANNEL_CREATE event // wait 5 seconds to give it time for CHANNEL_CREATE event
await delay(3000); delayUntil(3000, () => cache.channels.has(channel.id));
// Make sure the channel was created. // Make sure the channel was created.
if (!cache.channels.has(channel.id)) { if (!cache.channels.has(channel.id)) {
throw new Error( throw new Error(
"The channel should have been created but it is not in the cache." "The channel should have been created but it is not in the cache.",
); );
} }
// Delete the channel now without a reason // Delete the channel now without a reason
await deleteChannel(tempData.guildId, channel.id); await deleteChannel(tempData.guildId, channel.id);
// wait 5 seconds to give it time for CHANNEL_DELETE event // wait 5 seconds to give it time for CHANNEL_DELETE event
await delay(3000); delayUntil(3000, () => !cache.channels.has(channel.id));
// Make sure it is gone from cache // Make sure it is gone from cache
if (cache.channels.has(channel.id)) { if (cache.channels.has(channel.id)) {
throw new Error( throw new Error(
"The channel should have been deleted but it is still in cache." "The channel should have been deleted but it is still in cache.",
); );
} }
}, },
@@ -42,22 +42,22 @@ Deno.test({
name: "delete-channel", name: "delete-channel",
}); });
// wait 5 seconds to give it time for CHANNEL_CREATE event // wait 5 seconds to give it time for CHANNEL_CREATE event
await delay(3000); delayUntil(3000, () => cache.channels.has(channel.id));
// Make sure the channel was created. // Make sure the channel was created.
if (!cache.channels.has(channel.id)) { if (!cache.channels.has(channel.id)) {
throw new Error( throw new Error(
"The channel should have been created but it is not in the cache." "The channel should have been created but it is not in the cache.",
); );
} }
// Delete the channel now without a reason // Delete the channel now without a reason
await deleteChannel(tempData.guildId, channel.id, "with a reason"); await deleteChannel(tempData.guildId, channel.id, "with a reason");
// wait 5 seconds to give it time for CHANNEL_DELETE event // wait 5 seconds to give it time for CHANNEL_DELETE event
await delay(3000); delayUntil(3000, () => !cache.channels.has(channel.id));
// Make sure it is gone from cache // Make sure it is gone from cache
if (cache.channels.has(channel.id)) { if (cache.channels.has(channel.id)) {
throw new Error( throw new Error(
"The channel should have been deleted but it is still in cache." "The channel should have been deleted but it is still in cache.",
); );
} }
}, },
+2 -2
View File
@@ -1,8 +1,8 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists } from "../deps.ts"; import { assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { delay } from "../../src/util/utils.ts";
import { createGuild } from "../../src/helpers/guilds/create_guild.ts"; import { createGuild } from "../../src/helpers/guilds/create_guild.ts";
import { delayUntil } from "../util/delay_until.ts";
Deno.test({ Deno.test({
name: "[guild] create a new guild", name: "[guild] create a new guild",
@@ -17,7 +17,7 @@ Deno.test({
tempData.guildId = guild.id; tempData.guildId = guild.id;
// Delay the execution by 5 seconds to allow GUILD_CREATE event to be processed // Delay the execution by 5 seconds to allow GUILD_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.guilds.has(guild.id));
if (!cache.guilds.has(guild.id)) { if (!cache.guilds.has(guild.id)) {
throw new Error("The guild seemed to be created but it was not cached."); throw new Error("The guild seemed to be created but it was not cached.");
+2 -2
View File
@@ -1,7 +1,7 @@
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { deleteServer } from "../../src/helpers/guilds/delete_server.ts"; import { deleteServer } from "../../src/helpers/guilds/delete_server.ts";
import { delay } from "../../src/util/utils.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { delayUntil } from "../util/delay_until.ts";
Deno.test({ Deno.test({
name: "[guild] delete a guild", name: "[guild] delete a guild",
@@ -14,7 +14,7 @@ Deno.test({
} }
await deleteServer(tempData.guildId); await deleteServer(tempData.guildId);
await delay(3000); delayUntil(3000, () => cache.guilds.has(tempData.guildId));
if (cache.guilds.has(tempData.guildId)) { if (cache.guilds.has(tempData.guildId)) {
throw new Error("The guild was not able to be deleted."); throw new Error("The guild was not able to be deleted.");
+6 -6
View File
@@ -2,10 +2,10 @@ import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { DiscordReaction } from "../../src/types/messages/reaction.ts"; import { DiscordReaction } from "../../src/types/messages/reaction.ts";
import { delay } from "../../src/util/utils.ts";
import { sendMessage } from "../../src/helpers/messages/send_message.ts"; import { sendMessage } from "../../src/helpers/messages/send_message.ts";
import { addReaction } from "../../src/helpers/messages/add_reaction.ts"; import { addReaction } from "../../src/helpers/messages/add_reaction.ts";
import { createEmoji } from "../../src/helpers/emojis/create_emoji.ts"; import { createEmoji } from "../../src/helpers/emojis/create_emoji.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -33,7 +33,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
name: "blamewolf", name: "blamewolf",
image: "https://cdn.discordapp.com/emojis/814955268123000832.png", image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
roles: [], roles: [],
} },
) )
).id ).id
}>`; }>`;
@@ -45,16 +45,16 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
await message.addReaction(emojiId); await message.addReaction(emojiId);
} }
await delay(3000); delayUntil(3000, () => cache.messages.get(message.id)?.reactions?.length > 0);
assertEquals( assertEquals(
await cache.messages await cache.messages
.get(message.id) .get(message.id)
?.reactions?.filter( ?.reactions?.filter(
(reaction: DiscordReaction) => (reaction: DiscordReaction) =>
reaction.emoji?.name === (custom ? "blamewolf" : "❤") reaction.emoji?.name === (custom ? "blamewolf" : "❤"),
).length, ).length,
1 1,
); );
} }
+7 -12
View File
@@ -1,17 +1,12 @@
import { import { addReactions, cache, createEmoji, sendMessage } from "../../mod.ts";
addReactions,
cache,
createEmoji,
delay,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf( async function ifItFailsBlameWolf(
type: "getter" | "raw", type: "getter" | "raw",
custom = false, custom = false,
ordered = false ordered = false,
) { ) {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -19,7 +14,7 @@ async function ifItFailsBlameWolf(
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -39,7 +34,7 @@ async function ifItFailsBlameWolf(
name: "blamewolf", name: "blamewolf",
image: "https://cdn.discordapp.com/emojis/814955268123000832.png", image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
roles: [], roles: [],
} },
) )
).id ).id
}>`, }>`,
@@ -53,7 +48,7 @@ async function ifItFailsBlameWolf(
name: "blamewolf2", name: "blamewolf2",
image: "https://cdn.discordapp.com/emojis/814955268123000832.png", image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
roles: [], roles: [],
} },
) )
).id ).id
}>`, }>`,
@@ -66,7 +61,7 @@ async function ifItFailsBlameWolf(
await message.addReactions(emojiIds, ordered); await message.addReactions(emojiIds, ordered);
} }
await delay(3000); delayUntil(3000, () => cache.messages.get(message.id)?.reactions?.length > 0);
assertEquals(await cache.messages.get(message.id)?.reactions?.length, 2); assertEquals(await cache.messages.get(message.id)?.reactions?.length, 2);
} }
+4 -5
View File
@@ -1,9 +1,9 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists } from "../deps.ts"; import { assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { delay } from "../../src/util/utils.ts";
import { sendMessage } from "../../src/helpers/messages/send_message.ts"; import { sendMessage } from "../../src/helpers/messages/send_message.ts";
import { createChannel } from "../../src/helpers/channels/create_channel.ts"; import { createChannel } from "../../src/helpers/channels/create_channel.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const channel = await createChannel(tempData.guildId, { const channel = await createChannel(tempData.guildId, {
@@ -12,10 +12,9 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
assertExists(channel); assertExists(channel);
// Wait few seconds for the channel create event to arrive and cache it // Wait few seconds for the channel create event to arrive and cache it
await delay(3000); delayUntil(3000, () => cache.channels.has(channel.id));
const message = const message = type === "raw"
type === "raw"
? await sendMessage(channel.id, "Hello World!") ? await sendMessage(channel.id, "Hello World!")
: await channel.send("Hello World!"); : await channel.send("Hello World!");
@@ -23,7 +22,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
+4 -4
View File
@@ -1,9 +1,9 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists } from "../deps.ts"; import { assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { delay } from "../../src/util/utils.ts";
import { sendMessage } from "../../src/helpers/messages/send_message.ts"; import { sendMessage } from "../../src/helpers/messages/send_message.ts";
import { deleteMessage } from "../../src/helpers/messages/delete_message.ts"; import { deleteMessage } from "../../src/helpers/messages/delete_message.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -25,11 +25,11 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
} }
// Wait 5 seconds to give it time for MESSAGE_DELETE event // Wait 5 seconds to give it time for MESSAGE_DELETE event
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure it is gone from cache // Make sure it is gone from cache
if (cache.messages.has(message.id)) { if (cache.messages.has(message.id)) {
throw new Error( throw new Error(
"The message should have been deleted but it is still in cache." "The message should have been deleted but it is still in cache.",
); );
} }
} }
+11 -12
View File
@@ -1,12 +1,7 @@
import { import { cache, deleteMessages, sendMessage } from "../../mod.ts";
cache,
delay,
deleteMessage,
deleteMessages,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists } from "../deps.ts"; import { assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(reason?: string) { async function ifItFailsBlameWolf(reason?: string) {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -14,7 +9,7 @@ async function ifItFailsBlameWolf(reason?: string) {
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -25,7 +20,7 @@ async function ifItFailsBlameWolf(reason?: string) {
// Assertions // Assertions
assertExists(secondMessage); assertExists(secondMessage);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(secondMessage.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(secondMessage.id)) { if (!cache.messages.has(secondMessage.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -35,15 +30,19 @@ async function ifItFailsBlameWolf(reason?: string) {
await deleteMessages( await deleteMessages(
tempData.channelId, tempData.channelId,
[message.id, secondMessage.id], [message.id, secondMessage.id],
reason reason,
); );
// Wait 5 seconds to give it time for MESSAGE_DELETE event // Wait 5 seconds to give it time for MESSAGE_DELETE event
await delay(3000); delayUntil(
3000,
() =>
!cache.messages.has(message.id) && !cache.messages.has(secondMessage.id),
);
// Make sure it is gone from cache // Make sure it is gone from cache
if (cache.messages.has(message.id) || cache.messages.has(secondMessage.id)) { if (cache.messages.has(message.id) || cache.messages.has(secondMessage.id)) {
throw new Error( throw new Error(
"The message should have been deleted but it is still in cache." "The message should have been deleted but it is still in cache.",
); );
} }
} }
+6 -3
View File
@@ -1,9 +1,9 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { cache } from "../../src/cache.ts"; import { cache } from "../../src/cache.ts";
import { delay } from "../../src/util/utils.ts";
import { sendMessage } from "../../src/helpers/messages/send_message.ts"; import { sendMessage } from "../../src/helpers/messages/send_message.ts";
import { editMessage } from "../../src/helpers/messages/edit_message.ts"; import { editMessage } from "../../src/helpers/messages/edit_message.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -24,7 +24,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
await message.edit("Goodbye World!"); await message.edit("Goodbye World!");
} }
// Wait 5 seconds to give it time for MESSAGE_UPDATE event // Wait 5 seconds to give it time for MESSAGE_UPDATE event
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.content === "Goodbye World!",
);
// Make sure it has been modified in cache // Make sure it has been modified in cache
assertEquals(cache.messages.get(message.id)?.content, "Goodbye World!"); assertEquals(cache.messages.get(message.id)?.content, "Goodbye World!");
+3 -2
View File
@@ -1,6 +1,7 @@
import { cache, delay, getMessage, sendMessage } from "../../mod.ts"; import { cache, getMessage, sendMessage } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
Deno.test({ Deno.test({
name: "[message] fetch a message", name: "[message] fetch a message",
@@ -10,7 +11,7 @@ Deno.test({
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
+11 -4
View File
@@ -1,6 +1,7 @@
import { cache, delay, getMessages, sendMessage } from "../../mod.ts"; import { cache, getMessages, sendMessage } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
Deno.test({ Deno.test({
name: "[message] fetch messages", name: "[message] fetch messages",
@@ -8,11 +9,11 @@ Deno.test({
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
const secondMessage = await sendMessage( const secondMessage = await sendMessage(
tempData.channelId, tempData.channelId,
"Hello World 2!" "Hello World 2!",
); );
const thirdMessage = await sendMessage( const thirdMessage = await sendMessage(
tempData.channelId, tempData.channelId,
"Hello World 3!" "Hello World 3!",
); );
// Assertions // Assertions
@@ -20,7 +21,13 @@ Deno.test({
assertExists(secondMessage); assertExists(secondMessage);
assertExists(thirdMessage); assertExists(thirdMessage);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(
3000,
() =>
cache.messages.has(message.id) &&
cache.messages.has(secondMessage.id) &&
cache.messages.has(thirdMessage.id),
);
// Make sure the message was created. // Make sure the message was created.
if ( if (
!cache.messages.has(message.id) || !cache.messages.has(message.id) ||
+4 -9
View File
@@ -1,12 +1,7 @@
import { import { addReaction, cache, getReactions, sendMessage } from "../../mod.ts";
addReaction,
cache,
delay,
getReactions,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
Deno.test({ Deno.test({
name: "[message] fetch reactions", name: "[message] fetch reactions",
@@ -16,7 +11,7 @@ Deno.test({
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -28,7 +23,7 @@ Deno.test({
const fetchedReactions = await getReactions( const fetchedReactions = await getReactions(
tempData.channelId, tempData.channelId,
message.id, message.id,
"❤" "❤",
); );
// Check if getMessage has worked // Check if getMessage has worked
assertEquals(fetchedReactions.size, 1); assertEquals(fetchedReactions.size, 1);
+4 -3
View File
@@ -1,7 +1,8 @@
import { cache, delay, getPins, pin, sendMessage } from "../../mod.ts"; import { cache, getPins, pin, sendMessage } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { DiscordenoMessage } from "../../src/structures/message.ts"; import { DiscordenoMessage } from "../../src/structures/message.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -10,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -25,7 +26,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
const pins = await getPins(tempData.channelId); const pins = await getPins(tempData.channelId);
assertEquals( assertEquals(
pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length, pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length,
1 1,
); );
} }
+10 -4
View File
@@ -1,12 +1,12 @@
import { import {
addReactions, addReactions,
cache, cache,
delay,
removeAllReactions, removeAllReactions,
sendMessage, sendMessage,
} from "../../mod.ts"; } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -23,7 +23,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Add reactions to the message // Add reactions to the message
await addReactions(message.channelId, message.id, ["❤", "😃", "🤫"]); await addReactions(message.channelId, message.id, ["❤", "😃", "🤫"]);
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions?.length === 3,
);
// Be sure that the message has the reactions // Be sure that the message has the reactions
assertEquals(await cache.messages.get(message.id)?.reactions?.length, 3); assertEquals(await cache.messages.get(message.id)?.reactions?.length, 3);
@@ -35,7 +38,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
} }
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions === undefined,
);
// Check if the reactions has been deleted // Check if the reactions has been deleted
assertEquals(await cache.messages.get(message.id)?.reactions, undefined); assertEquals(await cache.messages.get(message.id)?.reactions, undefined);
+11 -10
View File
@@ -1,12 +1,7 @@
import { import { addReaction, cache, removeReaction, sendMessage } from "../../mod.ts";
addReaction,
cache,
delay,
removeReaction,
sendMessage,
} from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -14,7 +9,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -23,7 +18,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Add reactions to the message // Add reactions to the message
await addReaction(message.channelId, message.id, "❤"); await addReaction(message.channelId, message.id, "❤");
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions?.length === 1,
);
// Be sure that the message has the reactions // Be sure that the message has the reactions
assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1); assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1);
@@ -35,7 +33,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
} }
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions === undefined,
);
// Check if the reactions has been deleted // Check if the reactions has been deleted
assertEquals(await cache.messages.get(message.id)?.reactions, undefined); assertEquals(await cache.messages.get(message.id)?.reactions, undefined);
+10 -5
View File
@@ -1,13 +1,12 @@
import { import {
addReaction, addReaction,
cache, cache,
delay,
removeReaction,
removeReactionEmoji, removeReactionEmoji,
sendMessage, sendMessage,
} from "../../mod.ts"; } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -15,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -24,7 +23,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Add reactions to the message // Add reactions to the message
await addReaction(message.channelId, message.id, "❤"); await addReaction(message.channelId, message.id, "❤");
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions?.length === 1,
);
// Be sure that the message has the reactions // Be sure that the message has the reactions
assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1); assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1);
@@ -36,7 +38,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
} }
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions === undefined,
);
// Check if the reactions has been deleted // Check if the reactions has been deleted
assertEquals(cache.messages.get(message.id)?.reactions, undefined); assertEquals(cache.messages.get(message.id)?.reactions, undefined);
+11 -6
View File
@@ -1,13 +1,12 @@
import { import {
addReaction, addReaction,
cache, cache,
delay,
removeReaction,
removeUserReaction, removeUserReaction,
sendMessage, sendMessage,
} from "../../mod.ts"; } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -15,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Assertions // Assertions
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
// Make sure the message was created. // Make sure the message was created.
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -24,7 +23,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
// Add reactions to the message // Add reactions to the message
await addReaction(message.channelId, message.id, "❤"); await addReaction(message.channelId, message.id, "❤");
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions?.length === 1,
);
// Be sure that the message has the reactions // Be sure that the message has the reactions
assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1); assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1);
@@ -34,14 +36,17 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
message.channelId, message.channelId,
message.id, message.id,
"❤", "❤",
message.author.id message.author.id,
); );
} else { } else {
//await message.removeUserReaction("❤", message.author.id); //await message.removeUserReaction("❤", message.author.id);
} }
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
await delay(3000); delayUntil(
3000,
() => cache.messages.get(message.id)?.reactions === undefined,
);
// Check if the reactions has been deleted // Check if the reactions has been deleted
assertEquals(await cache.messages.get(message.id)?.reactions, undefined); assertEquals(await cache.messages.get(message.id)?.reactions, undefined);
+5 -4
View File
@@ -1,7 +1,8 @@
import { cache, delay, getPins, pin, sendMessage, unpin } from "../../mod.ts"; import { cache, getPins, pin, sendMessage, unpin } from "../../mod.ts";
import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertEquals, assertExists } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
import { DiscordenoMessage } from "../../src/structures/message.ts"; import { DiscordenoMessage } from "../../src/structures/message.ts";
import { delayUntil } from "../util/delay_until.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw") { async function ifItFailsBlameWolf(type: "getter" | "raw") {
const message = await sendMessage(tempData.channelId, "Hello World!"); const message = await sendMessage(tempData.channelId, "Hello World!");
@@ -10,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
assertExists(message); assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delay(3000); delayUntil(3000, () => cache.messages.has(message.id));
if (!cache.messages.has(message.id)) { if (!cache.messages.has(message.id)) {
throw new Error("The message seemed to be sent but it was not cached."); throw new Error("The message seemed to be sent but it was not cached.");
@@ -22,7 +23,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
const pins = await getPins(tempData.channelId); const pins = await getPins(tempData.channelId);
assertEquals( assertEquals(
pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length, pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length,
1 1,
); );
if (type === "raw") { if (type === "raw") {
@@ -36,7 +37,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
assertEquals( assertEquals(
removedPins.filter((msg: DiscordenoMessage) => msg.id === message.id) removedPins.filter((msg: DiscordenoMessage) => msg.id === message.id)
.length, .length,
0 0,
); );
} }