Compare commits
2 commits
7e625b232e
...
166bd80113
Author | SHA1 | Date | |
---|---|---|---|
166bd80113 | |||
fa7e35b4dd |
3 changed files with 393 additions and 15 deletions
|
@ -61,7 +61,7 @@ export default class Config extends Command {
|
|||
]);
|
||||
|
||||
if (!server) {
|
||||
await interaction.reply('Server not setup. Please use the setup command,');
|
||||
await interaction.reply('Server not setup. Please use the setup command.');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,25 +1,261 @@
|
|||
import { APIEmbed, APIVersion, ChatInputCommandInteraction, CommandInteraction, SlashCommandBuilder, SlashCommandSubcommandBuilder } from "discord.js";
|
||||
import Command from "../../src/commands/code";
|
||||
import StringTools from "../../src/helpers/StringTools";
|
||||
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
||||
|
||||
describe('constructor', () => {
|
||||
test.todo("EXPECT properties to be set");
|
||||
test("EXPECT properties to be set", () => {
|
||||
const command = new Command();
|
||||
|
||||
expect(command.CommandBuilder).toBeDefined();
|
||||
|
||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
||||
|
||||
expect(commandBuilder.name).toBe("code");
|
||||
expect(commandBuilder.description).toBe("Manage the verification code of the server");
|
||||
expect(commandBuilder.options.length).toBe(2);
|
||||
|
||||
const commandBuilderRandomiseSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
|
||||
|
||||
expect(commandBuilderRandomiseSubcommand.name).toBe("randomise");
|
||||
expect(commandBuilderRandomiseSubcommand.description).toBe("Regenerates the verification code for this server");
|
||||
|
||||
const commandBuilderEmbedSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
|
||||
|
||||
expect(commandBuilderEmbedSubcommand.name).toBe("embed");
|
||||
expect(commandBuilderEmbedSubcommand.description).toBe("Sends the embed with the current code to the current channel");
|
||||
});
|
||||
});
|
||||
|
||||
describe("execute", () => {
|
||||
test.todo("GIVEN interaction is not a chat input command, EXPECT nothing to happen");
|
||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
||||
// Assert
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
||||
reply: jest.fn(),
|
||||
} as unknown as CommandInteraction;
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("randomise", () => {
|
||||
test.todo("EXPECT entry code to be randomised");
|
||||
test("EXPECT entry code to be randomised", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("randomise"),
|
||||
},
|
||||
reply: jest.fn(),
|
||||
guildId: "guildId",
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
|
||||
StringTools.RandomString = jest.fn().mockReturnValue("12345");
|
||||
|
||||
SettingsHelper.SetSetting = jest.fn();
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(StringTools.RandomString).toHaveBeenCalledTimes(1);
|
||||
expect(StringTools.RandomString).toHaveBeenCalledWith(5);
|
||||
|
||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
|
||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("verification.code", "guildId", "12345");
|
||||
|
||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).toHaveBeenCalledWith("Entry code has been set to `12345`");
|
||||
});
|
||||
|
||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("randomise"),
|
||||
},
|
||||
reply: jest.fn(),
|
||||
guildId: null,
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
StringTools.RandomString = jest.fn().mockReturnValue("12345");
|
||||
|
||||
SettingsHelper.SetSetting = jest.fn();
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.reply).not.toHaveBeenCalled();
|
||||
expect(SettingsHelper.SetSetting).not.toHaveBeenCalled();
|
||||
expect(StringTools.RandomString).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("embed", () => {
|
||||
test.todo("EXPECT embed to be sent");
|
||||
test("EXPECT embed to be sent", async () => {
|
||||
let repliedWithEmbed: any;
|
||||
|
||||
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("embed"),
|
||||
},
|
||||
reply: jest.fn(),
|
||||
guildId: "guildId",
|
||||
channel: {
|
||||
send: jest.fn().mockImplementation((options: any) => {
|
||||
repliedWithEmbed = options.embeds[0];
|
||||
}),
|
||||
},
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
test.todo("GIVEN interaction.channel is null, EXPECT nothing to happen");
|
||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("12345");
|
||||
|
||||
test.todo("GIVEN verification.code setting is null, EXPECT error");
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
test.todo("GIVEN verification.code setting is empty, EXPECT error");
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
|
||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.code", "guildId");
|
||||
|
||||
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(repliedWithEmbed).toBeDefined();
|
||||
expect(repliedWithEmbed.data.title).toBe("Entry Code");
|
||||
expect(repliedWithEmbed.data.description).toBe("12345");
|
||||
});
|
||||
|
||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
||||
let repliedWithEmbed: any;
|
||||
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("embed"),
|
||||
},
|
||||
reply: jest.fn(),
|
||||
guildId: null,
|
||||
channel: {
|
||||
send: jest.fn().mockImplementation((options: any) => {
|
||||
repliedWithEmbed = options.embeds[0];
|
||||
}),
|
||||
},
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("12345");
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.channel!.send).not.toHaveBeenCalled();
|
||||
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
|
||||
|
||||
expect(repliedWithEmbed).toBeUndefined();
|
||||
});
|
||||
|
||||
test("GIVEN interaction.channel is null, EXPECT nothing to happen", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("embed"),
|
||||
},
|
||||
reply: jest.fn(),
|
||||
guildId: "guildId",
|
||||
channel: null,
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("12345");
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN verification.code setting is undefined, EXPECT error", async () => {
|
||||
let repliedWithEmbed: any;
|
||||
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("embed"),
|
||||
},
|
||||
reply: jest.fn(),
|
||||
guildId: "guildId",
|
||||
channel: {
|
||||
send: jest.fn().mockImplementation((options: any) => {
|
||||
repliedWithEmbed = options.embeds[0];
|
||||
}),
|
||||
},
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).toHaveBeenCalledWith("There is no code for this server setup.");
|
||||
|
||||
expect(interaction.channel! .send).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN verification.code setting is empty, EXPECT error", async () => {
|
||||
let repliedWithEmbed: any;
|
||||
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("embed"),
|
||||
},
|
||||
reply: jest.fn(),
|
||||
guildId: "guildId",
|
||||
channel: {
|
||||
send: jest.fn().mockImplementation((options: any) => {
|
||||
repliedWithEmbed = options.embeds[0];
|
||||
}),
|
||||
},
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("");
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).toHaveBeenCalledWith("There is no code for this server setup.");
|
||||
|
||||
expect(interaction.channel!.send).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
|
@ -1,15 +1,157 @@
|
|||
import { ChatInputCommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from "discord.js";
|
||||
import Command from "../../src/commands/config";
|
||||
import Server from "../../src/database/entities/Server";
|
||||
|
||||
describe("constructor", () => {
|
||||
test.todo("EXPECT properties to be set");
|
||||
test("EXPECT properties to be set", () => {
|
||||
const command = new Command();
|
||||
|
||||
expect(command.CommandBuilder).toBeDefined();
|
||||
|
||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
||||
|
||||
expect(commandBuilder.name).toBe("config");
|
||||
expect(commandBuilder.description).toBe("Configure the current server");
|
||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.Administrator.toString());
|
||||
expect(commandBuilder.options.length).toBe(4);
|
||||
|
||||
const commandBuilderResetSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
|
||||
|
||||
expect(commandBuilderResetSubcommand.name).toBe("reset");
|
||||
expect(commandBuilderResetSubcommand.description).toBe("Reset a setting to the default");
|
||||
expect(commandBuilderResetSubcommand.options.length).toBe(1);
|
||||
|
||||
const commandBuilderResetSubcommandKeyOption = commandBuilderResetSubcommand.options[0] as SlashCommandStringOption;
|
||||
|
||||
expect(commandBuilderResetSubcommandKeyOption.name).toBe("key");
|
||||
expect(commandBuilderResetSubcommandKeyOption.description).toBe("The key");
|
||||
expect(commandBuilderResetSubcommandKeyOption.required).toBe(true);
|
||||
|
||||
const commandBuilderGetSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
|
||||
|
||||
expect(commandBuilderGetSubcommand.name).toBe("get");
|
||||
expect(commandBuilderGetSubcommand.description).toBe("Gets a setting for the server");
|
||||
expect(commandBuilderGetSubcommand.options.length).toBe(1);
|
||||
|
||||
const commandBuilderGetSubcommandKeyOption = commandBuilderGetSubcommand.options[0] as SlashCommandStringOption;
|
||||
|
||||
expect(commandBuilderGetSubcommandKeyOption.name).toBe("key");
|
||||
expect(commandBuilderGetSubcommandKeyOption.description).toBe("The key");
|
||||
expect(commandBuilderGetSubcommandKeyOption.required).toBe(true);
|
||||
|
||||
const commandBuilderSetSubcommand = commandBuilder.options[2] as SlashCommandSubcommandBuilder;
|
||||
|
||||
expect(commandBuilderSetSubcommand.name).toBe("set");
|
||||
expect(commandBuilderSetSubcommand.description).toBe("Sets a setting to a specified value");
|
||||
expect(commandBuilderSetSubcommand.options.length).toBe(2);
|
||||
|
||||
const commandBuilderSetSubcommandKeyOption = commandBuilderSetSubcommand.options[0] as SlashCommandStringOption;
|
||||
|
||||
expect(commandBuilderSetSubcommandKeyOption.name).toBe("key");
|
||||
expect(commandBuilderSetSubcommandKeyOption.description).toBe("The key");
|
||||
expect(commandBuilderSetSubcommandKeyOption.required).toBe(true);
|
||||
|
||||
const commandBuilderSetSubcommandValueOption = commandBuilderSetSubcommand.options[1] as SlashCommandStringOption;
|
||||
|
||||
expect(commandBuilderSetSubcommandValueOption.name).toBe("value");
|
||||
expect(commandBuilderSetSubcommandValueOption.description).toBe("The value");
|
||||
expect(commandBuilderSetSubcommandValueOption.required).toBe(true);
|
||||
|
||||
const commandBuilderListSubcommand = commandBuilder.options[3] as SlashCommandSubcommandBuilder;
|
||||
|
||||
expect(commandBuilderListSubcommand.name).toBe("list");
|
||||
expect(commandBuilderListSubcommand.description).toBe("Lists all settings");
|
||||
});
|
||||
});
|
||||
|
||||
describe("execute", () => {
|
||||
test.todo("GIVEN interaction is not a chat input command, EXPECT nothing to happen");
|
||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
||||
options: {
|
||||
getSubcommand: jest.fn(),
|
||||
},
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
test.todo("GIVEN server is not set up in the database, EXPECT error");
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.options.getSubcommand).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test.todo("GIVEN subcommand is invalid, EXPECT error");
|
||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn(),
|
||||
},
|
||||
guildId: null,
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.options.getSubcommand).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN server is not set up in the database, EXPECT error", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn(),
|
||||
},
|
||||
guildId: "guildId",
|
||||
reply: jest.fn(),
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
Server.FetchOneById = jest.fn().mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(Server.FetchOneById).toHaveBeenCalledTimes(1);
|
||||
expect(Server.FetchOneById).toHaveBeenCalledWith(Server, "guildId", [ "Settings" ]);
|
||||
|
||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).toHaveBeenCalledWith("Server not setup. Please use the setup command.");
|
||||
|
||||
expect(interaction.options.getSubcommand).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN subcommand is invalid, EXPECT error", async () => {
|
||||
// Arrange
|
||||
const interaction = {
|
||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||
options: {
|
||||
getSubcommand: jest.fn().mockReturnValue("invalid"),
|
||||
},
|
||||
guildId: "guildId",
|
||||
reply: jest.fn(),
|
||||
} as unknown as ChatInputCommandInteraction;
|
||||
|
||||
Server.FetchOneById = jest.fn().mockResolvedValue({});
|
||||
|
||||
// Act
|
||||
const command = new Command();
|
||||
await command.execute(interaction);
|
||||
|
||||
// Assert
|
||||
expect(interaction.options.getSubcommand).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||
expect(interaction.reply).toHaveBeenCalledWith("Subcommand not found.");
|
||||
});
|
||||
});
|
||||
|
||||
describe("list", () => {
|
||||
|
|
Loading…
Reference in a new issue