Compare commits

..

No commits in common. "ca64ede1adfa75cf81c82b2f3a1fae1e06878e5d" and "6d2b323338f62295116ac1bb7463dfd800cf0acb" have entirely different histories.

4 changed files with 464 additions and 484 deletions

View file

@ -47,7 +47,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.0.0", "@types/node": "^20.0.0",
"np": "^10.0.0", "np": "^9.0.0",
"typescript": "^5.0.0" "typescript": "^5.0.0"
} }
} }

View file

@ -1,9 +1,27 @@
import discordjs, { Client, REST, Routes, SlashCommandBuilder } from "discord.js"; import { Client, 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";
import { EventType } from "../../src/constants/EventType"; jest.mock("discord.js", () => {
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();
@ -25,15 +43,6 @@ 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",
@ -57,11 +66,12 @@ describe("loadSlashCommands", () => {
const util = new Util(); const util = new Util();
util.loadSlashCommands(client); util.loadSlashCommands(client);
expect(setTokenMock).toHaveBeenCalledWith("token"); expect(REST).toHaveBeenCalledWith({ version: '10' });
expect(REST.prototype.setToken).toHaveBeenCalledWith("token");
expect(setTokenPutMock).toHaveBeenCalledTimes(2); expect(REST.prototype.put).toHaveBeenCalledTimes(2);
expect(setTokenPutMock).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] }); expect(REST.prototype.put).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] });
expect(setTokenPutMock).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]}); expect(REST.prototype.put).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");
@ -73,261 +83,31 @@ describe("loadSlashCommands", () => {
expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123"); expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123");
}); });
test("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored", () => { test.todo("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("GIVEN event type is channelCreate, EXPECT event function to be executed", () => { test.todo("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(),
}
];
const util = new Util(); test.todo("GIVEN event type is channelDelete, EXPECT event function to be executed");
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1); test.todo("GIVEN event type is channelUpdate, EXPECT event function to be executed");
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", () => { test.todo("GIVEN event type is guildBanRemove, EXPECT event function to be executed");
const client = {
on: jest.fn(), test.todo("GIVEN event type is guildCreate, EXPECT event function to be executed");
} as unknown as Client;
const events: IEventItem[] = [ test.todo("GIVEN event type is guildMemberAdd, EXPECT event function to be executed");
{
EventType: EventType.ChannelDelete, test.todo("GIVEN event type is guildMemberRemove, EXPECT event function to be executed");
ExecutionFunction: jest.fn(),
} test.todo("GIVEN event type is messageCreate, EXPECT event function to be executed");
];
test.todo("GIVEN event type is messageDelete, EXPECT event function to be executed");
const util = new Util();
util.loadEvents(client, events); test.todo("GIVEN event type is messageUpdate, EXPECT event function to be executed");
expect(client.on).toHaveBeenCalledTimes(1); test.todo("GIVEN event type is not implemented, EXPECT error");
expect(client.on).toHaveBeenCalledWith("channelDelete", expect.any(Function));
});
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(),
}
];
const util = new Util();
util.loadEvents(client, events);
expect(client.on).toHaveBeenCalledTimes(1);
expect(client.on).toHaveBeenCalledWith("channelUpdate", expect.any(Function));
});
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(),
}
];
const util = new Util();
util.loadEvents(client, events);
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.");
});
}); });

View file

@ -1,44 +1,5 @@
import { PermissionsBitField, SlashCommandBuilder, SlashCommandChannelOption, SlashCommandNumberOption, SlashCommandRoleOption, SlashCommandStringOption } from "discord.js";
import Add from "../../../../src/commands/501231711271780357/Lobby/add";
describe('constuctor', () => { describe('constuctor', () => {
test("EXPECT properties to be set", () => { test.todo("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', () => {

607
yarn.lock

File diff suppressed because it is too large Load diff