Create timeout command #302

Merged
Vylpes merged 16 commits from feature/98-timeout-command into develop 2023-06-16 18:01:46 +01:00
Showing only changes of commit 05650ea6c5 - Show all commits

View file

@ -1,8 +1,11 @@
import { PermissionsBitField, SlashCommandBuilder } from "discord.js"; import { APIEmbed, CommandInteraction, CommandInteractionOption, Embed, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, SlashCommandBuilder, TextChannel, User } from "discord.js";
import { mock } from "jest-mock-extended";
import Timeout from "../../src/commands/timeout"; import Timeout from "../../src/commands/timeout";
import SettingsHelper from "../../src/helpers/SettingsHelper";
import Audit from "../../src/entity/Audit";
describe('Constructor', () => { describe('Constructor', () => {
test('Expect CommandBuilder to be configured', () => { test('EXPECT CommandBuilder to be configured', () => {
const command = new Timeout(); const command = new Timeout();
expect(command.CommandBuilder).toBeDefined(); expect(command.CommandBuilder).toBeDefined();
@ -16,130 +19,190 @@ describe('Constructor', () => {
}); });
describe('execute', () => { describe('execute', () => {
test.todo('Given targetUser is null, Expect validation error'); // Happy flow
test.todo('Given targetUser.user is null, Expect validation error'); test('GIVEN all checks have passed, EXPECT user to be timed out', async () => {
test.todo('Given targetUser.member is null, Expect validation error'); let embeds: APIEmbed[] | undefined;
describe('Null checks', () => { const command = new Timeout();
describe('GIVEN interaction.guild IS NULL', () => {
test.todo('EXPECT nothing to happen'); const interactionReply = jest.fn((options: InteractionReplyOptions) => {
embeds = options.embeds as APIEmbed[];
}); });
describe('GIVEN interaction.guildId IS NULL', () => { // SettingsHelper.GetSetting = jest.fn().mockResolvedValue('mod-logs');
test.todo('EXPECT nothing to happen'); const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
}); const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation();
const timeoutFunc = jest.fn();
const userInput = {
user: {
id: 'userId',
tag: 'userTag',
} as User,
member: {
manageable: true,
timeout: timeoutFunc,
} as unknown as GuildMember,
} as CommandInteractionOption;
const lengthInput = {
value: '1s',
} as CommandInteractionOption;
const reasonInput = {
value: 'Test reason',
} as CommandInteractionOption;
const logsChannel = {
name: 'mod-logs',
send: jest.fn(),
} as unknown as TextChannel;
const interaction = {
guild: {
channels: {
cache: {
find: jest.fn()
.mockReturnValue(logsChannel),
}
}
} as unknown as Guild,
guildId: 'guildId',
reply: interactionReply,
options: {
get: jest.fn()
.mockReturnValueOnce(userInput)
.mockReturnValueOnce(lengthInput)
.mockReturnValue(reasonInput),
},
user: {
id: 'moderatorId'
}
} as unknown as CommandInteraction;
await command.execute(interaction);
// EXPECT user to be timed out
expect(timeoutFunc).toBeCalledWith(1000, 'Test reason');
// EXPECT embeds to be sent
expect(embeds).toBeDefined();
expect(embeds!.length).toBe(1);
// EXPECT resultEmbed to be correctly configured
const resultEmbed = embeds![0];
expect(resultEmbed.description).toBe('<@userId> has been timed out');
expect(resultEmbed.fields).toBeDefined();
expect(resultEmbed.fields!.length).toBe(1);
// EXPECT DM field to be configured
const resultEmbedDMField = resultEmbed.fields![0];
expect(resultEmbedDMField.name).toBe("DM Sent");
expect(resultEmbedDMField.value).toBe("true");
// TODO: User was timed out
// TODO: DM Embed check
// TODO: Log Embed check
// TODO: Audit check
}); });
describe('Validation checks', () => { // Null checks
describe('targetUser', () => { test('GIVEN interaction.guild IS NULL, EXPECT nothing to happen', async () => {
describe('GIVEN targetUser IS NULL', () => { const command = new Timeout();
test.todo('EXPECT validation error');
const interaction = {
guild: null,
reply: jest.fn(),
} as unknown as CommandInteraction;
await command.execute(interaction);
expect(interaction.reply).not.toBeCalled();
}); });
describe('GIVEN targetUser.user IS NULL', () => { test.todo('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen');
test.todo('EXPECT validation error');
// Validation
test('GIVEN targetUser IS NULL, EXPECT validation error', async () => {
const command = new Timeout();
const interaction = {
reply: jest.fn(),
guild: mock<Guild>(),
guildId: 'guildId',
options: {
get: jest.fn().mockReturnValue(undefined),
}
} as unknown as CommandInteraction;
await command.execute(interaction);
expect(interaction.reply).toBeCalledWith('Fields are required.');
}); });
describe('GIVEN targetUser.member IS NULL', () => { test('GIVEN targetUser.user IS NULL, EXPECT validation error', async () => {
test.todo('EXPECT validation error'); const command = new Timeout();
});
const interactionOption = {
user: undefined,
member: {} as GuildMember
} as CommandInteractionOption;
const interaction = {
reply: jest.fn(),
guild: mock<Guild>(),
guildId: 'guildId',
options: {
get: jest.fn().mockReturnValue(interactionOption),
}
} as unknown as CommandInteraction;
await command.execute(interaction);
expect(interaction.reply).toBeCalledWith('Fields are required.');
}); });
describe('lengthInput', () => { test('GIVEN targetUser.member IS NULL, EXPECT validation error', async () => {
describe('GIVEN lengthInput IS NULL', () => { const command = new Timeout();
test.todo('EXPECT validation error');
const interactionOption = {
user: {} as User,
member: undefined
} as CommandInteractionOption;
const interaction = {
reply: jest.fn(),
guild: mock<Guild>(),
guildId: 'guildId',
options: {
get: jest.fn().mockReturnValue(interactionOption),
}
} as unknown as CommandInteraction;
await command.execute(interaction);
expect(interaction.reply).toBeCalledWith('Fields are required.');
}); });
describe('GIVEN lengthInput.value IS NULL', () => { test.todo('GIVEN lengthInput IS NULL, EXPECT validation error');
test.todo('EXPECT validation error');
});
});
});
describe('GIVEN targetMember IS NOT manageable by the bot', () => { test.todo('GIVEN lengthInput.value IS NULL, EXPECT validation error');
test.todo('EXPECT insufficient permissions error');
});
describe('targetMember.timeout', () => { test.todo('GIVEN targetMember IS NOT manageable by the bot, EXPECT insufficient permissions error');
test.todo('EXPECT to be ran with time length');
describe('GIVEN reason IS NOT NULL', () => { // Reason variable
test.todo('EXPECT to be ran with reason set'); test.todo('GIVEN reason IS NOT NULL, EXPECT to be ran with reason set');
});
describe('GIVEN reason IS NULL', () => { test.todo('GIVEN reason IS NULL, EXPECT to be ran with empty string');
test.todo('EXPECT to be ran with empty string');
});
});
describe('Log Embed', () => { // Log embed
test.todo('EXPECT moderator to be current user'); test.todo('GIVEN channelName IS NULL, EXPECT execution to return');
test.todo('EXPECT length to be length'); test.todo('GIVEN channel IS NULL, EXPECT embed to not be sent');
test.todo('EXPECT until to be until date'); // DM user
test.todo('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"');
describe('GIVEN reason IS NULL', () => {
test.todo('EXPECT reason to be "*none*"');
});
describe('GIVEN reason IS NOT NULL', () => {
test.todo('EXPECT reason to be set to reason');
});
describe('GIVEN channelName IS NULL', () => {
test.todo('EXPECT execution to return');
});
describe('GIVEN channel IS NULL', () => {
test.todo('EXPECT embed to not be sent');
});
describe('GIVEN channel IS NOT NULL', () => {
test.todo('EXPECT logEmbed to be sent to channel');
});
});
describe('Audit', () => {
test.todo('EXPECT audit to be saved');
describe('GIVEN reason IS NULL', () => {
test.todo('EXPECT audit entity to set reason to "*none*"');
});
describe('GIVEN reason IS NOT NULL', () => {
test.todo('EXPECT audit entity to set reason to reason');
});
});
describe('DM User', () => {
describe('GIVEN user can be messaged', () => {
test.todo('EXPECT embed to be sent');
test.todo('EXPECT length to be in fields');
test.todo('EXPECT until to be in fields');
test.todo('EXPECT resultEmbed to contain "DM Sent = true"');
describe('GIVEN reason IS NULL', () => {
test.todo('EXPECT reason to be "*none*"');
});
describe('GIVEN reason IS NOT NULL', () => {
test.todo('EXPECT reason to be set to reason');
});
});
describe('GIVEN user can NOT be messaged', () => {
test.todo('EXPECT resultEmbed to contain "DM Sent = false"');
});
});
describe('Result Embed', () => {
test.todo('EXPECT resultEmbed to be sent to current channel');
test.todo('EXPECT embed description to be set to "You have been timed out in (GUILD NAME)');
});
}); });