From ceb5f6c148d8d74df8b21b3a85328acfa37644f6 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 28 Dec 2021 21:22:49 +0000 Subject: [PATCH] eval command tests --- src/commands/clear.ts | 19 +++- src/commands/eval.ts | 18 +++- tests/commands/ban.test.ts | 3 +- tests/commands/clear.test.ts | 178 +++++++++++++++++++++++++++++++++++ tests/commands/eval.test.ts | 136 ++++++++++++++++++++++++++ 5 files changed, 347 insertions(+), 7 deletions(-) create mode 100644 tests/commands/clear.test.ts create mode 100644 tests/commands/eval.test.ts diff --git a/src/commands/clear.ts b/src/commands/clear.ts index cf940a7..4f8f893 100644 --- a/src/commands/clear.ts +++ b/src/commands/clear.ts @@ -3,6 +3,7 @@ import { TextChannel } from "discord.js"; import PublicEmbed from "../helpers/embeds/PublicEmbed"; import { Command } from "../type/command"; import { ICommandContext } from "../contracts/ICommandContext"; +import ICommandReturnContext from "../contracts/ICommandReturnContext"; export default class Clear extends Command { constructor() { @@ -14,11 +15,15 @@ export default class Clear extends Command { ]; } - public override async execute(context: ICommandContext) { + public override async execute(context: ICommandContext): Promise { if (context.args.length == 0) { const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); errorEmbed.SendToCurrentChannel(); - return; + + return { + commandContext: context, + embeds: [errorEmbed] + }; } const totalToClear = Number.parseInt(context.args[0]); @@ -26,12 +31,20 @@ export default class Clear extends Command { if (!totalToClear || totalToClear <= 0 || totalToClear > 100) { const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); errorEmbed.SendToCurrentChannel(); - return; + return { + commandContext: context, + embeds: [errorEmbed] + }; } await (context.message.channel as TextChannel).bulkDelete(totalToClear); const embed = new PublicEmbed(context, "", `${totalToClear} message(s) were removed`); embed.SendToCurrentChannel(); + + return { + commandContext: context, + embeds: [embed] + }; } } \ No newline at end of file diff --git a/src/commands/eval.ts b/src/commands/eval.ts index 29e940c..222367d 100644 --- a/src/commands/eval.ts +++ b/src/commands/eval.ts @@ -1,4 +1,5 @@ import { ICommandContext } from "../contracts/ICommandContext"; +import ICommandReturnContext from "../contracts/ICommandReturnContext"; import ErrorEmbed from "../helpers/embeds/ErrorEmbed"; import PublicEmbed from "../helpers/embeds/PublicEmbed"; import { Command } from "../type/command"; @@ -10,9 +11,12 @@ export default class Evaluate extends Command { super._category = "Owner"; } - public override execute(context: ICommandContext) { + public override execute(context: ICommandContext): ICommandReturnContext { if (context.message.author.id != process.env.BOT_OWNERID) { - return; + return { + commandContext: context, + embeds: [] + }; } const stmt = context.args; @@ -24,10 +28,20 @@ export default class Evaluate extends Command { const embed = new PublicEmbed(context, "", result); embed.SendToCurrentChannel(); + + return { + commandContext: context, + embeds: [embed] + }; } catch (err: any) { const errorEmbed = new ErrorEmbed(context, err); errorEmbed.SendToCurrentChannel(); + + return { + commandContext: context, + embeds: [errorEmbed] + }; } } } \ No newline at end of file diff --git a/tests/commands/ban.test.ts b/tests/commands/ban.test.ts index eb621b0..6ed09e6 100644 --- a/tests/commands/ban.test.ts +++ b/tests/commands/ban.test.ts @@ -1,5 +1,4 @@ -import { Collection, Guild, GuildChannel, GuildChannelManager, GuildManager, GuildMember, Message, MessageEmbed, TextChannel, User } from "discord.js"; -import { mock } from "jest-mock-extended"; +import { GuildMember, Message, TextChannel, User } from "discord.js"; import Ban from "../../src/commands/ban"; import { ICommandContext } from "../../src/contracts/ICommandContext"; diff --git a/tests/commands/clear.test.ts b/tests/commands/clear.test.ts new file mode 100644 index 0000000..ad27d62 --- /dev/null +++ b/tests/commands/clear.test.ts @@ -0,0 +1,178 @@ +import { Message } from "discord.js"; +import Clear from "../../src/commands/clear"; +import { ICommandContext } from "../../src/contracts/ICommandContext"; + +beforeEach(() => { + process.env = {}; +}); + +describe('Constructor', () => { + test('Expect values to be set', () => { + process.env = { + ROLES_MODERATOR: "Moderator" + }; + + const clear = new Clear(); + + expect(clear._category).toBe('Moderation'); + expect(clear._roles.length).toBe(1); + expect(clear._roles[0]).toBe('Moderator'); + }); +}); + +describe('Execute', () => { + test('Given valid arguments, expect messages to be cleared', async () => { + const messageChannelSend = jest.fn(); + const messageChannelBulkDelete = jest.fn(); + + const message = { + channel: { + send: messageChannelSend, + bulkDelete: messageChannelBulkDelete + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'clear', + args: ['5'], + message: message + }; + + const clear = new Clear(); + const result = await clear.execute(context); + + expect(messageChannelSend).toBeCalledTimes(1); + expect(messageChannelBulkDelete).toBeCalledWith(5); + expect(result.embeds.length).toBe(1); + + // PublicEmbed + const publicEmbed = result.embeds[0]; + + expect(publicEmbed.title).toBe(''); + expect(publicEmbed.description).toBe('5 message(s) were removed'); + }); + + test('Given argument is not given, expect error embed to be sent', async () => { + const messageChannelSend = jest.fn(); + const messageChannelBulkDelete = jest.fn(); + + const message = { + channel: { + send: messageChannelSend, + bulkDelete: messageChannelBulkDelete + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'clear', + args: [], + message: message + }; + + const clear = new Clear(); + const result = await clear.execute(context); + + expect(messageChannelSend).toBeCalledTimes(1); + expect(messageChannelBulkDelete).not.toBeCalled(); + expect(result.embeds.length).toBe(1); + + // ErrorEmbed + const errorEmbed = result.embeds[0]; + + expect(errorEmbed.title).toBeNull(); + expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100'); + }); + + test('Given argument is not a number, expect error embed to be sent', async () => { + const messageChannelSend = jest.fn(); + const messageChannelBulkDelete = jest.fn(); + + const message = { + channel: { + send: messageChannelSend, + bulkDelete: messageChannelBulkDelete + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'clear', + args: ['A'], + message: message + }; + + const clear = new Clear(); + const result = await clear.execute(context); + + expect(messageChannelSend).toBeCalledTimes(1); + expect(messageChannelBulkDelete).not.toBeCalled(); + expect(result.embeds.length).toBe(1); + + // ErrorEmbed + const errorEmbed = result.embeds[0]; + + expect(errorEmbed.title).toBeNull(); + expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100'); + }); + + test('Given argument is less than 1, expect error embed to be sent', async () => { + const messageChannelSend = jest.fn(); + const messageChannelBulkDelete = jest.fn(); + + const message = { + channel: { + send: messageChannelSend, + bulkDelete: messageChannelBulkDelete + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'clear', + args: ['0'], + message: message + }; + + const clear = new Clear(); + const result = await clear.execute(context); + + expect(messageChannelSend).toBeCalledTimes(1); + expect(messageChannelBulkDelete).not.toBeCalled(); + expect(result.embeds.length).toBe(1); + + // ErrorEmbed + const errorEmbed = result.embeds[0]; + + expect(errorEmbed.title).toBeNull(); + expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100'); + }); + + test('Given argument is more than 100, expect error embed to be sent', async () => { + const messageChannelSend = jest.fn(); + const messageChannelBulkDelete = jest.fn(); + + const message = { + channel: { + send: messageChannelSend, + bulkDelete: messageChannelBulkDelete + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'clear', + args: ['101'], + message: message + }; + + const clear = new Clear(); + const result = await clear.execute(context); + + expect(messageChannelSend).toBeCalledTimes(1); + expect(messageChannelBulkDelete).not.toBeCalled(); + expect(result.embeds.length).toBe(1); + + // ErrorEmbed + const errorEmbed = result.embeds[0]; + + expect(errorEmbed.title).toBeNull(); + expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100'); + }); +}); \ No newline at end of file diff --git a/tests/commands/eval.test.ts b/tests/commands/eval.test.ts new file mode 100644 index 0000000..a9c4929 --- /dev/null +++ b/tests/commands/eval.test.ts @@ -0,0 +1,136 @@ +import { Message } from "discord.js"; +import Evaluate from "../../src/commands/eval"; +import { ICommandContext } from "../../src/contracts/ICommandContext"; + +beforeEach(() => { + process.env = {}; +}); + +describe('Constructor', () => { + test('Expect values to be set', () => { + const evaluate = new Evaluate(); + + expect(evaluate._category).toBe('Owner'); + }); +}); + +describe('Execute', () => { + test('Given user has permission, expect eval statement ran', () => { + process.env = { + BOT_OWNERID: 'OWNERID' + }; + + console.log = jest.fn(); + global.eval = jest.fn() + .mockReturnValue('General Kenobi'); + + const messageChannelSend = jest.fn(); + + const message = { + author: { + id: 'OWNERID' + }, + channel: { + send: messageChannelSend + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'eval', + args: ['echo', 'Hello', 'there'], + message: message + }; + + const evaluate = new Evaluate(); + + const result = evaluate.execute(context); + + expect(console.log).toBeCalledWith('Eval Statement: echo Hello there'); + expect(global.eval).toBeCalledWith('echo Hello there'); + expect(result.embeds.length).toBe(1); + + // PublicEmbed + const publicEmbed = result.embeds[0]; + + expect(publicEmbed.title).toBe(''); + expect(publicEmbed.description).toBe('General Kenobi'); + }); + + test('Given user does not have permission, expect nothing to occur', () => { + process.env = { + BOT_OWNERID: 'DIFFERENT' + }; + + console.log = jest.fn(); + global.eval = jest.fn() + .mockReturnValue('General Kenobi'); + + const messageChannelSend = jest.fn(); + + const message = { + author: { + id: 'OWNERID' + }, + channel: { + send: messageChannelSend + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'eval', + args: ['echo', 'Hello', 'there'], + message: message + }; + + const evaluate = new Evaluate(); + + const result = evaluate.execute(context); + + expect(console.log).not.toBeCalled(); + expect(global.eval).not.toBeCalled(); + expect(result.embeds.length).toBe(0); + }); + + test('Given eval failed, expect error embed to be sent', () => { + process.env = { + BOT_OWNERID: 'OWNERID' + }; + + console.log = jest.fn(); + global.eval = jest.fn() + .mockImplementation(() => { + throw new Error('Error message'); + }); + + const messageChannelSend = jest.fn(); + + const message = { + author: { + id: 'OWNERID' + }, + channel: { + send: messageChannelSend + } + } as unknown as Message; + + const context: ICommandContext = { + name: 'eval', + args: ['echo', 'Hello', 'there'], + message: message + }; + + const evaluate = new Evaluate(); + + const result = evaluate.execute(context); + + expect(console.log).toBeCalledWith('Eval Statement: echo Hello there'); + expect(global.eval).toBeCalledWith('echo Hello there'); + expect(result.embeds.length).toBe(1); + + // ErrorEmbed + const errorEmbed = result.embeds[0]; + + expect(errorEmbed.title).toBeNull(); + expect(errorEmbed.description).toBe('Error: Error message'); + }); +}); \ No newline at end of file