tests: add roles tests (#822)

This commit is contained in:
TriForMine
2021-04-11 20:41:15 +02:00
committed by GitHub
parent b357a5fb04
commit 5e6d749ffa
12 changed files with 334 additions and 5 deletions

View File

@@ -8,7 +8,7 @@ export async function handleGuildRoleCreate(data: DiscordGatewayPayload) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (!guild) return;
const role = await structures.createDiscordenoRole(payload.role);
const role = await structures.createDiscordenoRole(payload);
guild.roles = guild.roles.set(payload.role.id, role);
await cacheHandlers.set("guilds", payload.guild_id, guild);

View File

@@ -11,7 +11,7 @@ export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) {
const cachedRole = guild.roles.get(payload.role.id);
if (!cachedRole) return;
const role = await structures.createDiscordenoRole(payload.role);
const role = await structures.createDiscordenoRole(payload);
guild.roles.set(payload.role.id, role);
await cacheHandlers.set("guilds", guild.id, guild);

View File

@@ -22,7 +22,7 @@ export async function createRole(
});
const roleData = result as RoleData;
const role = await structures.createDiscordenoRole(roleData);
const role = await structures.createDiscordenoRole({role: roleData, guild_id: guildId});
const guild = await cacheHandlers.get("guilds", guildId);
guild?.roles.set(role.id, role);

View File

@@ -3,13 +3,13 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. */
export async function swapRoles(guildId: string, rolePositons: PositionSwap) {
export async function swapRoles(guildId: string, rolePositions: PositionSwap) {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
const result = await rest.runMethod(
"patch",
endpoints.GUILD_ROLES(guildId),
rolePositons,
rolePositions,
);
return result;

View File

@@ -40,6 +40,14 @@ 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";
import "./roles/swap_roles.ts";
// Final cleanup
import "./guilds/delete_server.ts";
import "./ws/ws_close.ts";

65
tests/roles/add_role.ts Normal file
View File

@@ -0,0 +1,65 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import {assertEquals, assertExists} from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { delayUntil } from "../util/delay_until.ts";
import {addRole} from "../../src/helpers/roles/add_role.ts";
import {createRole} from "../../src/helpers/roles/create_role.ts";
import {botId} from "../../src/bot.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
const role = await createRole(tempData.guildId, {
name: "hoti",
});
assertExists(role);
// Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed
await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id));
if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) {
throw new Error(`The role seemed to be created but it was not cached.`);
}
if (type === "raw") {
await addRole(tempData.guildId, botId, role.id, reason);
} else {
await cache.members.get(botId).addRole(tempData.guildId, role.id, reason);
}
// Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed
await delayUntil(10000, () => cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id));
assertEquals(cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id), true);
}
Deno.test({
name: "[role] add a role without a reason",
async fn() {
await ifItFailsBlameWolf("raw")
},
...defaultTestOptions,
});
Deno.test({
name: "[role] add a role with a reason",
async fn() {
await ifItFailsBlameWolf("raw", "with a reason")
},
...defaultTestOptions,
});
Deno.test({
name: "[role] member.addRole() without a reason",
async fn() {
await ifItFailsBlameWolf("getter")
},
...defaultTestOptions,
});
Deno.test({
name: "[role] member.addRole() with a reason",
async fn() {
await ifItFailsBlameWolf("getter", "with a reason")
},
...defaultTestOptions,
});

View File

@@ -0,0 +1,36 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import {assertExists} from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { delayUntil } from "../util/delay_until.ts";
import {createRole} from "../../src/helpers/roles/create_role.ts";
async function ifItFailsBlameWolf(reason?: string) {
const role = await createRole(tempData.guildId, {
name: "hoti",
}, reason);
assertExists(role);
// Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed
await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id));
if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) {
throw new Error(`The role seemed to be created but it was not cached.`);
}
}
Deno.test({
name: "[role] create a role without a reason",
async fn() {
await ifItFailsBlameWolf()
},
...defaultTestOptions,
});
Deno.test({
name: "[role] create a role with a reason",
async fn() {
await ifItFailsBlameWolf( "with a reason")
},
...defaultTestOptions,
});

View File

@@ -0,0 +1,46 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import {assertExists} from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { delayUntil } from "../util/delay_until.ts";
import {createRole} from "../../src/helpers/roles/create_role.ts";
import {deleteRole} from "../../src/helpers/roles/delete_role.ts";
async function ifItFailsBlameWolf(reason?: string) {
const role = await createRole(tempData.guildId, {
name: "hoti",
}, reason);
assertExists(role);
// Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed
await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id));
if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) {
throw new Error(`The role seemed to be created but it was not cached.`);
}
await deleteRole(tempData.guildId, role.id);
// Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed
await delayUntil(10000, () => !cache.guilds.get(tempData.guildId)?.roles.has(role.id));
if (cache.guilds.get(tempData.guildId)?.roles.has(role.id)) {
throw new Error(`The role should have been deleted but it is still in cache.`);
}
}
Deno.test({
name: "[role] delete a role without a reason",
async fn() {
await ifItFailsBlameWolf()
},
...defaultTestOptions,
});
Deno.test({
name: "[role] delete a role with a reason",
async fn() {
await ifItFailsBlameWolf( "with a reason")
},
...defaultTestOptions,
});

34
tests/roles/edit_role.ts Normal file
View File

@@ -0,0 +1,34 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import {assertEquals, assertExists} from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { delayUntil } from "../util/delay_until.ts";
import {createRole} from "../../src/helpers/roles/create_role.ts";
import {editRole} from "../../src/helpers/roles/edit_role.ts";
Deno.test({
name: "[role] edit a role",
async fn() {
const role = await createRole(tempData.guildId, {
name: "hoti",
});
assertExists(role);
// Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed
await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id));
if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) {
throw new Error(`The role seemed to be created but it was not cached.`);
}
await editRole(tempData.guildId, role.id, {
name: "#rememberAyntee"
});
// Delay the execution by 5 seconds to allow GUILD_ROLE_UPDATE event to be processed
await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.get(role.id)?.name === "#rememberAyntee");
assertEquals(cache.guilds.get(tempData.guildId)?.roles.get(role.id)?.name === "#rememberAyntee", true);
},
...defaultTestOptions,
});

20
tests/roles/get_roles.ts Normal file
View File

@@ -0,0 +1,20 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import {assertEquals} from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { delayUntil } from "../util/delay_until.ts";
import {botId} from "../../src/bot.ts";
import {getRoles} from "../../src/helpers/roles/get_roles.ts";
Deno.test({
name: "[role] get roles",
async fn() {
cache.guilds.get(tempData.guildId)?.roles.clear();
await getRoles(tempData.guildId);
await delayUntil(10000, () => cache.members.get(botId)?.guilds.get(tempData.guildId).roles.length > 0);
assertEquals(cache.members.get(botId)?.guilds.get(tempData.guildId).roles.length > 0, true);
},
...defaultTestOptions,
});

View File

@@ -0,0 +1,75 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import {assertEquals, assertExists} from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { delayUntil } from "../util/delay_until.ts";
import {addRole} from "../../src/helpers/roles/add_role.ts";
import {createRole} from "../../src/helpers/roles/create_role.ts";
import {botId} from "../../src/bot.ts";
import {removeRole} from "../../src/helpers/roles/remove_role.ts";
async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) {
const role = await createRole(tempData.guildId, {
name: "hoti",
});
assertExists(role);
// Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed
await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id));
if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id)) {
throw new Error(`The role seemed to be created but it was not cached.`);
}
if (type === "raw") {
await addRole(tempData.guildId, botId, role.id, reason);
} else {
await cache.members.get(botId).addRole(tempData.guildId, role.id, reason);
}
// Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed
await delayUntil(10000, () => cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id));
if (type === "raw") {
await removeRole(tempData.guildId, botId, role.id, reason);
} else {
await cache.members.get(botId).removeRole(tempData.guildId, role.id, reason);
}
// Delay the execution by 5 seconds to allow GUILD_MEMBER_UPDATE event to be processed
await delayUntil(10000, () => !cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id));
assertEquals(cache.members.get(botId)?.guilds.get(tempData.guildId).roles.includes(role.id), false);
}
Deno.test({
name: "[role] remove a role without a reason",
async fn() {
await ifItFailsBlameWolf("raw")
},
...defaultTestOptions,
});
Deno.test({
name: "[role] remove a role with a reason",
async fn() {
await ifItFailsBlameWolf("raw", "with a reason")
},
...defaultTestOptions,
});
Deno.test({
name: "[role] member.removeRole() without a reason",
async fn() {
await ifItFailsBlameWolf("getter")
},
...defaultTestOptions,
});
Deno.test({
name: "[role] member.removeRole() with a reason",
async fn() {
await ifItFailsBlameWolf("getter", "with a reason")
},
...defaultTestOptions,
});

45
tests/roles/swap_roles.ts Normal file
View File

@@ -0,0 +1,45 @@
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import {assertEquals, assertExists} from "../deps.ts";
import { cache } from "../../src/cache.ts";
import { delayUntil } from "../util/delay_until.ts";
import {addRole} from "../../src/helpers/roles/add_role.ts";
import {createRole} from "../../src/helpers/roles/create_role.ts";
import {botId} from "../../src/bot.ts";
import {swapRoles} from "../../src/helpers/roles/swap_roles.ts";
import {delay} from "../../src/util/utils.ts";
Deno.test({
name: "[role] swap roles",
async fn() {
const role = await createRole(tempData.guildId, {
name: "hoti",
});
assertExists(role);
const secondRole = await createRole(tempData.guildId, {
name: "not hoti",
});
assertExists(secondRole);
// Delay the execution by 5 seconds to allow GUILD_ROLE_CREATE event to be processed
await delayUntil(10000, () => cache.guilds.get(tempData.guildId)?.roles.has(role.id) && cache.guilds.get(tempData.guildId)?.roles.has(secondRole.id));
if (!cache.guilds.get(tempData.guildId)?.roles.has(role.id) || !cache.guilds.get(tempData.guildId)?.roles.has(secondRole.id)) {
throw new Error(`The role seemed to be created but it was not cached.`);
}
const result = await swapRoles(tempData.guildId, [
{
id: role.id,
},
{
id: secondRole.id,
},
]);
assertExists(result);
},
...defaultTestOptions,
});