Compare commits
5 commits
6d2b323338
...
ca64ede1ad
Author | SHA1 | Date | |
---|---|---|---|
ca64ede1ad | |||
fc975f4602 | |||
39b2a495a8 | |||
de5c744feb | |||
a7f8abb8d4 |
4 changed files with 484 additions and 464 deletions
|
@ -47,7 +47,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^20.0.0",
|
"@types/node": "^20.0.0",
|
||||||
"np": "^9.0.0",
|
"np": "^10.0.0",
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,9 @@
|
||||||
import { Client, SlashCommandBuilder } from "discord.js";
|
import discordjs, { Client, REST, Routes, SlashCommandBuilder } from "discord.js";
|
||||||
import { Util } from "../../src/client/util";
|
import { Util } from "../../src/client/util";
|
||||||
import { CoreClient } from "../../src/client/client";
|
import { CoreClient } from "../../src/client/client";
|
||||||
import { Command } from "../../src/type/command";
|
import { Command } from "../../src/type/command";
|
||||||
|
import IEventItem from "../../src/contracts/IEventItem";
|
||||||
jest.mock("discord.js", () => {
|
import { EventType } from "../../src/constants/EventType";
|
||||||
return {
|
|
||||||
Client: jest.fn(),
|
|
||||||
REST: jest.fn().mockImplementation(() => {
|
|
||||||
return {
|
|
||||||
v10: {
|
|
||||||
put: jest.fn(),
|
|
||||||
},
|
|
||||||
setToken: jest.fn(),
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
Routes: {
|
|
||||||
applicationCommands: jest.fn().mockReturnValue("command url"),
|
|
||||||
applicationGuildCommands: jest.fn().mockReturnValue("guild command url"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
import { REST, Routes } from "discord.js";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.resetAllMocks();
|
jest.resetAllMocks();
|
||||||
|
@ -43,6 +25,15 @@ describe("loadSlashCommands", () => {
|
||||||
}
|
}
|
||||||
} as unknown as Client;
|
} as unknown as Client;
|
||||||
|
|
||||||
|
const setTokenPutMock = jest.fn();
|
||||||
|
const setTokenMock = jest.fn().mockReturnValue({
|
||||||
|
put: setTokenPutMock,
|
||||||
|
});
|
||||||
|
|
||||||
|
REST.prototype.setToken = setTokenMock;
|
||||||
|
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
|
||||||
|
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
|
||||||
|
|
||||||
CoreClient.commandItems = [
|
CoreClient.commandItems = [
|
||||||
{
|
{
|
||||||
Name: "global",
|
Name: "global",
|
||||||
|
@ -66,12 +57,11 @@ describe("loadSlashCommands", () => {
|
||||||
const util = new Util();
|
const util = new Util();
|
||||||
util.loadSlashCommands(client);
|
util.loadSlashCommands(client);
|
||||||
|
|
||||||
expect(REST).toHaveBeenCalledWith({ version: '10' });
|
expect(setTokenMock).toHaveBeenCalledWith("token");
|
||||||
expect(REST.prototype.setToken).toHaveBeenCalledWith("token");
|
|
||||||
|
|
||||||
expect(REST.prototype.put).toHaveBeenCalledTimes(2);
|
expect(setTokenPutMock).toHaveBeenCalledTimes(2);
|
||||||
expect(REST.prototype.put).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] });
|
expect(setTokenPutMock).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] });
|
||||||
expect(REST.prototype.put).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]});
|
expect(setTokenPutMock).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]});
|
||||||
|
|
||||||
expect(client.guilds.cache.has).toHaveBeenCalledTimes(1);
|
expect(client.guilds.cache.has).toHaveBeenCalledTimes(1);
|
||||||
expect(client.guilds.cache.has).toHaveBeenCalledWith("123");
|
expect(client.guilds.cache.has).toHaveBeenCalledWith("123");
|
||||||
|
@ -83,31 +73,261 @@ describe("loadSlashCommands", () => {
|
||||||
expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123");
|
expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.todo("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored");
|
test("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored", () => {
|
||||||
|
process.env.BOT_TOKEN = "token";
|
||||||
|
process.env.BOT_CLIENTID = "clientid";
|
||||||
|
|
||||||
|
const client = {
|
||||||
|
guilds: {
|
||||||
|
cache: {
|
||||||
|
has: jest.fn().mockReturnValue(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as Client;
|
||||||
|
|
||||||
|
const setTokenPutMock = jest.fn();
|
||||||
|
const setTokenMock = jest.fn().mockReturnValue({
|
||||||
|
put: setTokenPutMock,
|
||||||
|
});
|
||||||
|
|
||||||
|
REST.prototype.setToken = setTokenMock;
|
||||||
|
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
|
||||||
|
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
|
||||||
|
|
||||||
|
CoreClient.commandItems = [
|
||||||
|
{
|
||||||
|
Name: "server-specific",
|
||||||
|
Command: {
|
||||||
|
CommandBuilder: {
|
||||||
|
name: "server",
|
||||||
|
} as SlashCommandBuilder,
|
||||||
|
} as Command,
|
||||||
|
ServerId: "123",
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadSlashCommands(client);
|
||||||
|
|
||||||
|
expect(Routes.applicationGuildCommands).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("loadEvents", () => {
|
describe("loadEvents", () => {
|
||||||
test.todo("GIVEN event type is channelCreate, EXPECT event function to be executed");
|
test("GIVEN event type is channelCreate, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.ChannelCreate,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
test.todo("GIVEN event type is channelDelete, EXPECT event function to be executed");
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
test.todo("GIVEN event type is channelUpdate, EXPECT event function to be executed");
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("channelCreate", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
test.todo("GIVEN event type is guildBanAdd, EXPECT event function to be executed");
|
test("GIVEN event type is channelDelete, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.ChannelDelete,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
test.todo("GIVEN event type is guildBanRemove, EXPECT event function to be executed");
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
test.todo("GIVEN event type is guildCreate, EXPECT event function to be executed");
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("channelDelete", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
test.todo("GIVEN event type is guildMemberAdd, EXPECT event function to be executed");
|
test("GIVEN event type is channelUpdate, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.ChannelUpdate,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
test.todo("GIVEN event type is guildMemberRemove, EXPECT event function to be executed");
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
test.todo("GIVEN event type is messageCreate, EXPECT event function to be executed");
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("channelUpdate", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
test.todo("GIVEN event type is messageDelete, EXPECT event function to be executed");
|
test("GIVEN event type is guildBanAdd, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.GuildBanAdd,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
test.todo("GIVEN event type is messageUpdate, EXPECT event function to be executed");
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
test.todo("GIVEN event type is not implemented, EXPECT error");
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("guildBanAdd", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is guildBanRemove, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.GuildBanRemove,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("guildBanRemove", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is guildCreate, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.GuildCreate,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("guildCreate", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is guildMemberAdd, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.GuildMemberAdd,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("guildMemberAdd", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is guildMemberRemove, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.GuildMemberRemove,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("guildMemberRemove", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is messageCreate, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.MessageCreate,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("messageCreate", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is messageDelete, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.MessageDelete,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("messageDelete", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is messageUpdate, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.MessageUpdate,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("messageUpdate", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN event type is not implemented, EXPECT error", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.Ready,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
console.error = jest.fn();
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
|
expect(console.error).toHaveBeenCalledWith("Event not implemented.");
|
||||||
|
});
|
||||||
});
|
});
|
|
@ -1,5 +1,44 @@
|
||||||
|
import { PermissionsBitField, SlashCommandBuilder, SlashCommandChannelOption, SlashCommandNumberOption, SlashCommandRoleOption, SlashCommandStringOption } from "discord.js";
|
||||||
|
import Add from "../../../../src/commands/501231711271780357/Lobby/add";
|
||||||
|
|
||||||
describe('constuctor', () => {
|
describe('constuctor', () => {
|
||||||
test.todo("EXPECT properties to be set");
|
test("EXPECT properties to be set", () => {
|
||||||
|
const add = new Add();
|
||||||
|
|
||||||
|
expect(add.CommandBuilder).toBeDefined();
|
||||||
|
|
||||||
|
const commandbuilder = add.CommandBuilder as SlashCommandBuilder;
|
||||||
|
|
||||||
|
expect(commandbuilder.name).toBe("addlobby");
|
||||||
|
expect(commandbuilder.description).toBe("Add lobby channel");
|
||||||
|
expect(commandbuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
|
||||||
|
|
||||||
|
expect(commandbuilder.options.length).toBe(4);
|
||||||
|
|
||||||
|
const channelOption = commandbuilder.options[0] as SlashCommandChannelOption;
|
||||||
|
|
||||||
|
expect(channelOption.name).toBe("channel");
|
||||||
|
expect(channelOption.description).toBe("The channel");
|
||||||
|
expect(channelOption.required).toBe(true);
|
||||||
|
|
||||||
|
const roleOption = commandbuilder.options[1] as SlashCommandRoleOption;
|
||||||
|
|
||||||
|
expect(roleOption.name).toBe("role");
|
||||||
|
expect(roleOption.description).toBe("The role to ping on request");
|
||||||
|
expect(roleOption.required).toBe(true);
|
||||||
|
|
||||||
|
const cooldownOption = commandbuilder.options[2] as SlashCommandNumberOption;
|
||||||
|
|
||||||
|
expect(cooldownOption.name).toBe("cooldown");
|
||||||
|
expect(cooldownOption.description).toBe("The cooldown in minutes");
|
||||||
|
expect(cooldownOption.required).toBe(true);
|
||||||
|
|
||||||
|
const nameOption = commandbuilder.options[3] as SlashCommandStringOption;
|
||||||
|
|
||||||
|
expect(nameOption.name).toBe("name");
|
||||||
|
expect(nameOption.description).toBe("The game name");
|
||||||
|
expect(nameOption.required).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('execute', () => {
|
describe('execute', () => {
|
||||||
|
|
Loading…
Reference in a new issue