mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-02 00:40:07 +00:00
refactor: base complete for unit tests
This commit is contained in:
@@ -131,7 +131,7 @@ export async function createGuildStruct(
|
||||
} = snakeKeysToCamelCase(data) as Guild;
|
||||
|
||||
const roles = await Promise.all(
|
||||
data.roles.map((role) =>
|
||||
(data.roles || []).map((role) =>
|
||||
structures.createRoleStruct({ role, guild_id: rest.id })
|
||||
),
|
||||
);
|
||||
@@ -163,7 +163,7 @@ export async function createGuildStruct(
|
||||
),
|
||||
memberCount: createNewProp(memberCount),
|
||||
emojis: createNewProp(
|
||||
new Collection(emojis.map((emoji) => [emoji.id ?? emoji.name, emoji])),
|
||||
new Collection((emojis || []).map((emoji) => [emoji.id ?? emoji.name, emoji])),
|
||||
),
|
||||
voiceStates: createNewProp(
|
||||
new Collection(
|
||||
|
||||
@@ -74,8 +74,8 @@ function isObject(obj: unknown) {
|
||||
);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function camelKeysToSnakeCase<T>(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
obj: Record<string, any> | Record<string, any>[],
|
||||
): T {
|
||||
if (isObject(obj)) {
|
||||
@@ -96,8 +96,8 @@ export function camelKeysToSnakeCase<T>(
|
||||
return obj as T;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function snakeKeysToCamelCase<T>(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
obj: Record<string, any> | Record<string, any>[],
|
||||
): T {
|
||||
if (isObject(obj)) {
|
||||
|
||||
23
tests/README.md
Normal file
23
tests/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Discordeno Unit Test Guideline
|
||||
|
||||
Unit tests are MANDATORY!
|
||||
|
||||
Every time you create a new function in the library, you must also add a unit test for it. A PR should/will not be merged without a valid unit test for it. If you are unable to create a unit test, please leave a comment in your PR asking for help.
|
||||
|
||||
## Test Locally
|
||||
|
||||
You do not need to push to the github repo to have the CI do the tests for you. You can test them locally by doing the following:
|
||||
|
||||
```shell
|
||||
DISCORD_TOKEN=YOUR_BOT_TOKEN_HERE deno test --no-check -A tests/mod.ts
|
||||
```
|
||||
|
||||
> Please note that the token you use should be for a trivial unused bot. Never use your main bot tokens for this.
|
||||
|
||||
## Ordering
|
||||
|
||||
The order of unit tests is very important. Please do not move/change the order of the tests unless you know what you are doing. Certain tests depend on other previous tests. You may add a test but becareful where you add it.
|
||||
|
||||
## Naming
|
||||
|
||||
Each function should have it's own separate file for it's tests. The file should be organized under it's main category which will be the `[]` portion of the tests name. For example, `[guild] create a new guild` will be found in `tests/guilds/create_guild.ts`
|
||||
23
tests/guilds/create_guild.ts
Normal file
23
tests/guilds/create_guild.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { cache, createGuild, delay } from "../../mod.ts";
|
||||
import { tempData, defaultTestOptions } from "../ws/start_bot.ts";
|
||||
import { assertExists, assertEquals } from "../deps.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "[guild] create a new guild",
|
||||
async fn() {
|
||||
const guild = await createGuild({
|
||||
name: "Discordeno Test",
|
||||
});
|
||||
console.log(guild);
|
||||
|
||||
// Assertions
|
||||
assertExists(guild);
|
||||
|
||||
tempData.guildId = guild.id;
|
||||
assertEquals(tempData.guildId, guild.id);
|
||||
|
||||
// Delay the execution by 5 seconds to allow GUILD_CREATE event to be processed
|
||||
await delay(5000);
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
19
tests/guilds/delete_server.ts
Normal file
19
tests/guilds/delete_server.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { cache, deleteServer, delay } from "../../mod.ts";
|
||||
import { tempData, defaultTestOptions } from "../ws/start_bot.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "[guild] delete a guild",
|
||||
async fn() {
|
||||
if (!tempData.guildId)
|
||||
throw new Error("The guild id was not available to be deleted.");
|
||||
if (!cache.guilds.has(tempData.guildId))
|
||||
throw new Error("The guild was not cached so impossible to delete.");
|
||||
|
||||
await deleteServer(tempData.guildId);
|
||||
await delay(3000);
|
||||
|
||||
if (cache.guilds.has(tempData.guildId))
|
||||
throw new Error("The guild was not able to be deleted.");
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
14
tests/mod.ts
Normal file
14
tests/mod.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
// 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 "./util/utils.ts";
|
||||
|
||||
// API TESTING BELOW
|
||||
|
||||
// First initate the connection
|
||||
import "./ws/start_bot.ts";
|
||||
import "./guilds/create_guild.ts";
|
||||
import "./guilds/delete_server.ts";
|
||||
import "./ws/ws_close.ts";
|
||||
@@ -1,5 +1,7 @@
|
||||
import { botId, delay, startBot, ws } from "../mod.ts";
|
||||
import { assertExists } from "./deps.ts";
|
||||
import { startBot, botId } from "../../src/bot.ts";
|
||||
import { delay } from "../../src/util/utils.ts";
|
||||
import { ws } from "../../src/ws/ws.ts";
|
||||
import { assertExists } from "../deps.ts";
|
||||
|
||||
// Set necessary settings
|
||||
// Disables the logger which logs everything
|
||||
@@ -23,9 +25,8 @@ export const tempData = {
|
||||
messageId: "",
|
||||
};
|
||||
|
||||
// Main
|
||||
Deno.test({
|
||||
name: "[main] connect to gateway",
|
||||
name: "[ws] connect to gateway",
|
||||
async fn() {
|
||||
const token = Deno.env.get("DISCORD_TOKEN");
|
||||
if (!token) throw new Error("Token is not provided");
|
||||
@@ -44,26 +45,6 @@ Deno.test({
|
||||
...defaultTestOptions,
|
||||
});
|
||||
|
||||
// Guild
|
||||
|
||||
// Deno.test({
|
||||
// name: "[guild] create a new guild",
|
||||
// async fn() {
|
||||
// const guild = await createGuild({
|
||||
// name: "Discordeno Test",
|
||||
// }) as Guild;
|
||||
|
||||
// // Assertions
|
||||
// assertExists(guild);
|
||||
|
||||
// tempData.guildId = guild.id;
|
||||
|
||||
// // Delay the execution by 5 seconds to allow GUILD_CREATE event to be processed
|
||||
// await delay(5000);
|
||||
// },
|
||||
// ...defaultTestOptions,
|
||||
// });
|
||||
|
||||
// // Role
|
||||
|
||||
// Deno.test({
|
||||
@@ -297,30 +278,3 @@ Deno.test({
|
||||
// },
|
||||
// ...defaultTestOptions,
|
||||
// });
|
||||
|
||||
// Deno.test({
|
||||
// name: "[guild] delete a guild",
|
||||
// async fn() {
|
||||
// await deleteServer(tempData.guildId);
|
||||
|
||||
// // TODO(ayntee): remove this weird shit lol
|
||||
// // TODO(ayntee): check if the GUILD_DELETE event is fired
|
||||
// tempData.guildId = "";
|
||||
// assertEquals(tempData.guildId, "");
|
||||
// },
|
||||
// ...defaultTestOptions,
|
||||
// });
|
||||
|
||||
// Exit the Deno process once all tests are done.
|
||||
Deno.test({
|
||||
name: "[main] Close all shards manually.",
|
||||
async fn() {
|
||||
ws.shards.forEach((shard) => {
|
||||
clearInterval(shard.heartbeat.intervalId);
|
||||
shard.ws.close(3064, "Discordeno Testing Finished! Do Not RESUME!");
|
||||
});
|
||||
|
||||
await delay(3000);
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
16
tests/ws/ws_close.ts
Normal file
16
tests/ws/ws_close.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { ws, delay } from "../../mod.ts";
|
||||
import { defaultTestOptions } from "./start_bot.ts";
|
||||
|
||||
// Exit the Deno process once all tests are done.
|
||||
Deno.test({
|
||||
name: "[ws] Close all shards manually.",
|
||||
async fn() {
|
||||
ws.shards.forEach((shard) => {
|
||||
clearInterval(shard.heartbeat.intervalId);
|
||||
shard.ws.close(3064, "Discordeno Testing Finished! Do Not RESUME!");
|
||||
});
|
||||
|
||||
await delay(3000);
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
Reference in New Issue
Block a user