unit test base

This commit is contained in:
Skillz4Killz
2021-10-28 17:56:17 +00:00
committed by GitHub
parent 3d43d7eedf
commit 1a5c85f098
3 changed files with 65 additions and 101 deletions
+46 -99
View File
@@ -1,113 +1,60 @@
import { TOKEN } from "../configs.ts";
import { Bot, createBot, createEventHandlers, startBot, stopBot } from "../mod.ts";
// import { TOKEN } from "../configs.ts";
import { Bot, createBot, createEventHandlers, DiscordChannelTypes, startBot, stopBot } from "../mod.ts";
import { assertEquals, assertExists } from "./deps.ts";
import { delayUntil } from "./utils.ts";
Deno.test("[Bot] - Starting Tests", async (t) => {
const bot = createBot({
token: TOKEN || Deno.env.get("DISCORD_TOKEN"),
// token: TOKEN || Deno.env.get("DISCORD_TOKEN"),
token: Deno.env.get("DISCORD_TOKEN")!,
botId: 675412054529540107n,
events: createEventHandlers({
debug: console.log,
// debug: console.log,
}),
intents: [],
}) as Bot;
await startBot(bot);
console.log("started");
const x = await bot.helpers.sendMessage(bot, 806947972004839444n, "testing");
console.log("x");
// Delay the execution to allow READY events to be processed
await delayUntil(3000, () => true);
console.log("Bot online");
// CREATE ONE GUILD SO WE CAN REUSE LATER TO SAVE RATE LIMITS
const guild = await bot.helpers.createGuild(bot, { name: "Discordeno Test" });
// Assertions
assertExists(guild);
assertExists(guild.id);
// Delay the execution to allow GUILD_CREATE event to be processed
await delayUntil(10000, async () => await bot.cache.guilds.has(guild.id));
// FINAL CHECK TO THROW IF MISSING STILL
if (!(await bot.cache.guilds.has(guild.id))) {
throw new Error(`The guild seemed to be created but it was not cached. ${guild.id.toString()}`);
}
await t.step("Channel related tests", async (t) => {
const channel = await bot.helpers.createChannel(bot, guild.id, { name: "Discordeno-test" });
// Assertions
assertExists(channel);
assertEquals(channel.type, DiscordChannelTypes.GuildText);
await t.step("Message related tests", async (t) => {
const message = await bot.helpers.sendMessage(bot, channel.id, "Testing");
// Assertions
assertExists(message);
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
await delayUntil(10000, async () => await bot.cache.messages.has(message.id));
if (!(await bot.cache.messages.has(message.id))) {
throw new Error("The message seemed to be sent but it was not cached.");
}
});
});
await stopBot(bot);
});
// // THE ORDER OF THE IMPORTS IN THIS FILE MATTER!
// // DO NOT MOVE THEM UNLESS YOU KNOW WHAT YOUR DOING!
// // First complete non-api reliant testing.
// // Don't waste api rate limits if a early test fails.
// // import "./local.ts";
// // API TESTING BELOW
// // First initiate the connection
// import "./ws/start_bot.ts";
// import "./guilds/create_guild.ts";
// // Channel tests
// import "./channels/category_children.ts";
// import "./channels/channel_overwrite_has_permission.ts";
// import "./channels/create_channel.ts";
// import "./channels/clone_channel.ts";
// import "./channels/delete_channel.ts";
// import "./channels/delete_channel_overwrite.ts";
// import "./channels/edit_channel.ts";
// import "./channels/edit_channel_overwrite.ts";
// import "./channels/get_channel.ts";
// import "./channels/get_channels.ts";
// import "./channels/get_pins.ts";
// import "./channels/is_channel_synced.ts";
// import "./channels/start_typing.ts";
// import "./channels/swap_channels.ts";
// // Emojis tests
// import "./emojis/create_emoji.ts";
// import "./emojis/delete_emoji.ts";
// import "./emojis/edit_emoji.ts";
// import "./emojis/get_emoji.ts";
// import "./emojis/get_emojis.ts";
// // Invites tests
// import "./invites/create_invite.ts";
// import "./invites/delete_invite.ts";
// import "./invites/get_channel_invites.ts";
// import "./invites/get_invite.ts";
// import "./invites/get_invites.ts";
// // Messages tests
// import "./messages/add_reaction.ts";
// import "./messages/add_reactions.ts";
// import "./messages/remove_all_reactions.ts";
// import "./messages/remove_reaction.ts";
// import "./messages/remove_reaction_emoji.ts";
// 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/unpin_message.ts";
// // Roles tests
// import "./roles/add_role.ts";
// import "./roles/create_role.ts";
// import "./roles/delete_role.ts";
// import "./roles/edit_role.ts";
// import "./roles/remove_role.ts";
// // Members tests
// import "./members/search_members.ts";
// // Discoveries tests
// import "./discoveries/get_discovery_categories.ts";
// import "./discoveries/valid_discovery_term.ts";
// // Final cleanup
// import "./guilds/delete_guild.ts";
// import "./ws/ws_close.ts";
// import { cache } from "../src/cache.ts";
// import { delay } from "../src/util/utils.ts";
// if (import.meta.main) {
// // clear all the sweeper intervals
// for (const c of Object.values(cache)) {
// if (!(c instanceof Map)) continue;
// c.stopSweeper();
// console.log("Cleaned");
// }
// await delay(3000);
// }
+17
View File
@@ -0,0 +1,17 @@
export function delayUntil(
maxMs: number,
isReady: () => boolean | undefined | Promise<boolean | undefined>,
timeoutTime = 100
): Promise<void> {
const maxTime = Date.now() + maxMs;
async function hackyFix(resolve: () => void) {
if ((await isReady()) || Date.now() >= maxTime) {
resolve();
} else {
setTimeout(() => hackyFix(resolve), timeoutTime);
}
}
return new Promise((resolve) => hackyFix(resolve));
}