mirror of
https://github.com/discordeno/discordeno.git
synced 2026-05-31 07:50:07 +00:00
fix(test): delayUntil use setTimeout instead of while loop & addReaction test (#817)
* fix: guild create tests * fix(test): use setTimeout instead of while loop and fix some tests * fix(test): fix some test and fix maxMs Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
This commit is contained in:
@@ -16,7 +16,7 @@ Deno.test({
|
||||
// Assertions
|
||||
assertExists(category);
|
||||
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.channels.has(category.id));
|
||||
await delayUntil(10000, () => cache.channels.has(category.id));
|
||||
|
||||
if (!cache.channels.has(category.id)) {
|
||||
throw new Error(
|
||||
@@ -34,7 +34,7 @@ Deno.test({
|
||||
),
|
||||
);
|
||||
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
|
||||
delayUntil(10000, () => channels.every((c) => cache.channels.has(c.id)));
|
||||
await delayUntil(10000, () => channels.every((c) => cache.channels.has(c.id)));
|
||||
|
||||
// If every channel is not present in the cache, error out
|
||||
if (!channels.every((c) => cache.channels.has(c.id))) {
|
||||
|
||||
@@ -16,7 +16,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) {
|
||||
if (save) tempData.channelId = channel.id;
|
||||
|
||||
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.channels.has(channel.id));
|
||||
await delayUntil(10000, () => 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.");
|
||||
|
||||
@@ -42,7 +42,7 @@ Deno.test({
|
||||
name: "delete-channel",
|
||||
});
|
||||
// wait 5 seconds to give it time for CHANNEL_CREATE event
|
||||
delayUntil(10000, () => cache.channels.has(channel.id));
|
||||
await delayUntil(10000, () => cache.channels.has(channel.id));
|
||||
// Make sure the channel was created.
|
||||
if (!cache.channels.has(channel.id)) {
|
||||
throw new Error(
|
||||
@@ -53,7 +53,7 @@ Deno.test({
|
||||
// Delete the channel now without a reason
|
||||
await deleteChannel(tempData.guildId, channel.id, "with a reason");
|
||||
// wait 5 seconds to give it time for CHANNEL_DELETE event
|
||||
delayUntil(10000, () => !cache.channels.has(channel.id));
|
||||
await delayUntil(10000, () => !cache.channels.has(channel.id));
|
||||
// Make sure it is gone from cache
|
||||
if (cache.channels.has(channel.id)) {
|
||||
throw new Error(
|
||||
|
||||
@@ -18,7 +18,7 @@ Deno.test({
|
||||
tempData.guildId = guild.id;
|
||||
|
||||
// Delay the execution by 5 seconds to allow GUILD_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.guilds.has(guild.id));
|
||||
await delayUntil(10000, () => 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. ${JSON.stringify(guild)}`);
|
||||
|
||||
@@ -14,7 +14,7 @@ Deno.test({
|
||||
}
|
||||
|
||||
await deleteServer(tempData.guildId);
|
||||
delayUntil(10000, () => cache.guilds.has(tempData.guildId));
|
||||
await delayUntil(10000, () => !cache.guilds.has(tempData.guildId));
|
||||
|
||||
if (cache.guilds.has(tempData.guildId)) {
|
||||
throw new Error("The guild was not able to be deleted.");
|
||||
|
||||
@@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
|
||||
assertExists(message);
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => 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.");
|
||||
@@ -45,9 +45,9 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
|
||||
await message.addReaction(emojiId);
|
||||
}
|
||||
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => (cache.messages.get(message.id)?.reactions?.length || 0) > 0
|
||||
() => cache.messages.get(message.id)?.reactions?.length === 1
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
|
||||
@@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(
|
||||
assertExists(message);
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => 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.");
|
||||
@@ -61,7 +61,7 @@ async function ifItFailsBlameWolf(
|
||||
await message.addReactions(emojiIds, ordered);
|
||||
}
|
||||
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions?.length === 2,
|
||||
);
|
||||
|
||||
@@ -12,7 +12,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
|
||||
assertExists(channel);
|
||||
// Wait few seconds for the channel create event to arrive and cache it
|
||||
delayUntil(10000, () => cache.channels.has(channel.id));
|
||||
await delayUntil(10000, () => cache.channels.has(channel.id));
|
||||
|
||||
const message = type === "raw"
|
||||
? await sendMessage(channel.id, "Hello World!")
|
||||
@@ -22,7 +22,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
assertExists(message);
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => 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.");
|
||||
|
||||
@@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
@@ -25,7 +25,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
|
||||
}
|
||||
|
||||
// Wait 5 seconds to give it time for MESSAGE_DELETE event
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => !cache.messages.has(message.id));
|
||||
// Make sure it is gone from cache
|
||||
if (cache.messages.has(message.id)) {
|
||||
throw new Error(
|
||||
|
||||
@@ -9,7 +9,7 @@ async function ifItFailsBlameWolf(reason?: string) {
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
@@ -20,7 +20,7 @@ async function ifItFailsBlameWolf(reason?: string) {
|
||||
// Assertions
|
||||
assertExists(secondMessage);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(secondMessage.id));
|
||||
await delayUntil(10000, () => cache.messages.has(secondMessage.id));
|
||||
// Make sure the message was created.
|
||||
if (!cache.messages.has(secondMessage.id)) {
|
||||
throw new Error("The message seemed to be sent but it was not cached.");
|
||||
@@ -34,7 +34,7 @@ async function ifItFailsBlameWolf(reason?: string) {
|
||||
);
|
||||
|
||||
// Wait 5 seconds to give it time for MESSAGE_DELETE event
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() =>
|
||||
!cache.messages.has(message.id) && !cache.messages.has(secondMessage.id),
|
||||
|
||||
@@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
@@ -24,7 +24,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
await message.edit("Goodbye World!");
|
||||
}
|
||||
// Wait 5 seconds to give it time for MESSAGE_UPDATE event
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.content === "Goodbye World!",
|
||||
);
|
||||
|
||||
@@ -11,7 +11,7 @@ Deno.test({
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
|
||||
@@ -21,7 +21,7 @@ Deno.test({
|
||||
assertExists(secondMessage);
|
||||
assertExists(thirdMessage);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() =>
|
||||
cache.messages.has(message.id) &&
|
||||
|
||||
@@ -11,7 +11,7 @@ Deno.test({
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
|
||||
@@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
assertExists(message);
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => 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.");
|
||||
|
||||
@@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
@@ -23,7 +23,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// 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
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions?.length === 3,
|
||||
);
|
||||
@@ -38,7 +38,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions === undefined,
|
||||
);
|
||||
|
||||
@@ -9,7 +9,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
@@ -18,7 +18,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// 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
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions?.length === 1,
|
||||
);
|
||||
@@ -33,7 +33,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions === undefined,
|
||||
);
|
||||
|
||||
@@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
@@ -23,7 +23,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// 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
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions?.length === 1,
|
||||
);
|
||||
@@ -38,7 +38,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions === undefined,
|
||||
);
|
||||
|
||||
@@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// 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.");
|
||||
@@ -23,7 +23,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
// 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
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions?.length === 1,
|
||||
);
|
||||
@@ -43,7 +43,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
|
||||
delayUntil(
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions === undefined,
|
||||
);
|
||||
|
||||
@@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
assertExists(message);
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
delayUntil(10000, () => cache.messages.has(message.id));
|
||||
await delayUntil(10000, () => 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.");
|
||||
|
||||
@@ -1,7 +1,17 @@
|
||||
export function delayUntil(maxMs: number, isReady: () => boolean) {
|
||||
const maxed = Date.now() + maxMs;
|
||||
export async function delayUntil(maxMs: number, isReady: () => boolean, timeoutTime= 100): Promise<void> {
|
||||
const maxTime = Date.now() + maxMs;
|
||||
|
||||
while (Date.now() < maxed) {
|
||||
if (isReady()) return;
|
||||
function hackyFix(resolve: () => void) {
|
||||
if (isReady() || Date.now() >= maxTime) {
|
||||
resolve();
|
||||
}
|
||||
else {
|
||||
setTimeout(async () => {
|
||||
await hackyFix(resolve);
|
||||
resolve();
|
||||
}, timeoutTime);
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise(resolve => hackyFix(resolve));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { deleteServer } from "../../src/helpers/guilds/delete_server.ts";
|
||||
import { delay } from "../../src/util/utils.ts";
|
||||
import { ws } from "../../src/ws/ws.ts";
|
||||
import { assertExists } from "../deps.ts";
|
||||
import {delayUntil} from "../util/delay_until.ts";
|
||||
|
||||
// Set necessary settings
|
||||
// Disables the logger which logs everything
|
||||
|
||||
Reference in New Issue
Block a user