2021-12-24 14:55:28 +00:00
|
|
|
import { Util } from "../../src/client/util";
|
|
|
|
|
2022-01-30 17:03:36 +00:00
|
|
|
import { Client, Message } from "discord.js";
|
2021-12-24 14:55:28 +00:00
|
|
|
import fs from "fs";
|
2022-01-30 17:03:36 +00:00
|
|
|
import { mock } from "jest-mock-extended";
|
|
|
|
import { Command } from "../../src/type/command";
|
|
|
|
import ICommandItem from "../../src/contracts/ICommandItem";
|
|
|
|
import IEventItem from "../../src/contracts/IEventItem";
|
|
|
|
import { Event } from "../../src/type/event";
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
jest.mock("fs");
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
fs.existsSync = jest.fn();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('LoadCommand', () => {
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given Successful Exection, Expect Successful Result', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const cmd = mock<Command>();
|
|
|
|
|
|
|
|
const commandItem: ICommandItem = {
|
|
|
|
Name: "test",
|
|
|
|
Command: cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(cmd.execute).toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given Member Is Null, Expect Failed Result', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: null
|
|
|
|
} as unknown as Message;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const cmd = mock<Command>();
|
|
|
|
|
|
|
|
const commandItem: ICommandItem = {
|
|
|
|
Name: "test",
|
|
|
|
Command: cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Member is not part of message");
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(cmd.execute).not.toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given User Does Have Role, Expect Successful Result', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const cmd = mock<Command>();
|
|
|
|
|
|
|
|
const commandItem: ICommandItem = {
|
|
|
|
Name: "test",
|
|
|
|
Command: cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(cmd.execute).toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given User Does Not Have Role, Expect Failed Result', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(false),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const cmd = mock<Command>();
|
|
|
|
cmd._roles = [ "Moderator" ];
|
|
|
|
|
|
|
|
const commandItem: ICommandItem = {
|
|
|
|
Name: "test",
|
|
|
|
Command: cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("You require the `Moderator` role to run this command");
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(cmd.execute).not.toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given command is set to disabled, Expect command to not fire', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
2022-01-30 17:03:36 +00:00
|
|
|
COMMANDS_DISABLED: 'test',
|
2021-12-24 14:55:28 +00:00
|
|
|
COMMANDS_DISABLED_MESSAGE: 'disabled',
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const messageReply = jest.spyOn(message, 'reply');
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const cmd = mock<Command>();
|
|
|
|
|
|
|
|
const commandItem: ICommandItem = {
|
|
|
|
Name: "test",
|
|
|
|
Command: cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Command is disabled");
|
|
|
|
expect(messageReply).toBeCalledWith("disabled");
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(cmd.execute).not.toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
2022-01-30 17:03:36 +00:00
|
|
|
COMMANDS_DISABLED: 'test',
|
2021-12-24 14:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const messageReply = jest.spyOn(message, 'reply');
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const cmd = mock<Command>();
|
|
|
|
|
|
|
|
const commandItem: ICommandItem = {
|
|
|
|
Name: "test",
|
|
|
|
Command: cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const commands: ICommandItem[] = [ commandItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Command is disabled");
|
|
|
|
expect(messageReply).toBeCalledWith("This command is disabled.");
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(cmd.execute).not.toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given a different command is disabled, Expect command to still fire', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
2022-01-30 17:03:36 +00:00
|
|
|
COMMANDS_DISABLED: 'other',
|
2021-12-24 14:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const cmd = mock<Command>();
|
|
|
|
const otherCmd = mock<Command>();
|
|
|
|
|
|
|
|
const commandItem: ICommandItem = {
|
|
|
|
Name: "test",
|
|
|
|
Command: cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const otherCommandItem: ICommandItem = {
|
|
|
|
Name: "other",
|
|
|
|
Command: otherCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
const commands: ICommandItem[] = [ commandItem, otherCommandItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(cmd.execute).toBeCalled();
|
|
|
|
expect(otherCmd.execute).not.toBeCalled();
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
test('Given command is not found in register, expect command not found error', async () => {
|
2021-12-24 14:55:28 +00:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(true),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const commands: ICommandItem[] = [];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-03-27 10:12:19 +01:00
|
|
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
2022-01-30 17:03:36 +00:00
|
|
|
expect(result.message).toBe('Command not found');
|
|
|
|
expect(message.reply).toBeCalledWith('Command not found');
|
2021-12-24 14:55:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('LoadEvents', () => {
|
|
|
|
test('Given Events Are Loaded, Expect Successful Result', () => {
|
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
const client = {
|
|
|
|
on: jest.fn(),
|
|
|
|
} as unknown as Client;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const evt = mock<Event>();
|
|
|
|
|
|
|
|
const eventItem: IEventItem = {
|
|
|
|
Event: evt
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventItems: IEventItem[] = [ eventItem ];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-01-30 17:03:36 +00:00
|
|
|
const result = util.loadEvents(client, eventItems);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const clientOn = jest.spyOn(client, 'on');
|
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
|
|
|
expect(clientOn).toBeCalledTimes(13);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Given No Events Found, Expect Successful Result', () => {
|
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
|
|
|
|
|
|
|
const client = {
|
|
|
|
on: jest.fn(),
|
|
|
|
} as unknown as Client;
|
2022-01-30 17:03:36 +00:00
|
|
|
|
|
|
|
const eventItems: IEventItem[] = [];
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2022-01-30 17:03:36 +00:00
|
|
|
const result = util.loadEvents(client, eventItems);
|
2021-12-24 14:55:28 +00:00
|
|
|
|
|
|
|
const clientOn = jest.spyOn(client, 'on');
|
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
|
|
|
expect(clientOn).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
});
|