Compare commits
2 commits
c387eb5b02
...
6af92ad1c7
Author | SHA1 | Date | |
---|---|---|---|
6af92ad1c7 | |||
177f7a3072 |
1 changed files with 330 additions and 7 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, MessageCreateOptions, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, EmbedField, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, MessagePayload, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
||||||
import { mock } from "jest-mock-extended";
|
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 SettingsHelper from "../../src/helpers/SettingsHelper";
|
||||||
|
@ -194,7 +194,19 @@ describe('execute', () => {
|
||||||
expect(interaction.reply).not.toBeCalled();
|
expect(interaction.reply).not.toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
test.todo('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen');
|
test('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen', async () => {
|
||||||
|
const command = new Timeout();
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
guild: mock<Guild>(),
|
||||||
|
guildId: null,
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
|
expect(interaction.reply).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
// Validation
|
// Validation
|
||||||
test('GIVEN targetUser IS NULL, EXPECT validation error', async () => {
|
test('GIVEN targetUser IS NULL, EXPECT validation error', async () => {
|
||||||
|
@ -392,15 +404,326 @@ describe('execute', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reason variable
|
// Reason variable
|
||||||
test.todo('GIVEN reason IS NOT NULL, EXPECT to be ran with reason set');
|
test('GIVEN reason IS NULL, EXPECT to be ran with empty string', async () => {
|
||||||
|
const command = new Timeout();
|
||||||
|
|
||||||
test.todo('GIVEN reason IS NULL, EXPECT to be ran with empty string');
|
let savedAudit: DeepPartial<Audit> | undefined;
|
||||||
|
|
||||||
|
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||||
|
savedAudit = entity;
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
const timeoutFunc = jest.fn();
|
||||||
|
|
||||||
|
const sentEmbeds: EmbedBuilder[] = [];
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn(),
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(mock<TextChannel>()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guildId: 'guildId',
|
||||||
|
user: {
|
||||||
|
id: 'moderatorId',
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
||||||
|
switch (value) {
|
||||||
|
case 'target':
|
||||||
|
return {
|
||||||
|
user: {
|
||||||
|
id: 'userId',
|
||||||
|
tag: 'userTag',
|
||||||
|
createDM: jest.fn().mockReturnValue({
|
||||||
|
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
||||||
|
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
||||||
|
|
||||||
|
return mock<Message<false>>();
|
||||||
|
})
|
||||||
|
}) as unknown as DMChannel,
|
||||||
|
} as unknown as User,
|
||||||
|
member: {
|
||||||
|
manageable: true,
|
||||||
|
timeout: timeoutFunc,
|
||||||
|
} as unknown as GuildMember
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
case 'length':
|
||||||
|
return {
|
||||||
|
value: '1m'
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
case 'reason':
|
||||||
|
return {
|
||||||
|
value: undefined,
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
|
expect(timeoutFunc).toBeCalledWith(1000 * 60 * 1, "");
|
||||||
|
expect(savedAudit?.Reason).toBe("*none*");
|
||||||
|
|
||||||
|
const dmEmbed = (sentEmbeds[0] as any).data;
|
||||||
|
const dmEmbedReasonField = dmEmbed.fields![0] as EmbedField;
|
||||||
|
|
||||||
|
expect(dmEmbedReasonField.value).toBe("*none*");
|
||||||
|
});
|
||||||
|
|
||||||
// Log embed
|
// Log embed
|
||||||
test.todo('GIVEN channelName IS NULL, EXPECT execution to return');
|
test('GIVEN channelName IS NULL, EXPECT execution to return', async () => {
|
||||||
|
const command = new Timeout();
|
||||||
|
|
||||||
test.todo('GIVEN channel IS NULL, EXPECT embed to not be sent');
|
let savedAudit: DeepPartial<Audit> | undefined;
|
||||||
|
|
||||||
|
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||||
|
savedAudit = entity;
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const timeoutFunc = jest.fn();
|
||||||
|
|
||||||
|
const sentEmbeds: EmbedBuilder[] = [];
|
||||||
|
|
||||||
|
const logChannelSendFunc = jest.fn();
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn(),
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue({
|
||||||
|
send: logChannelSendFunc,
|
||||||
|
} as unknown as TextChannel),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guildId: 'guildId',
|
||||||
|
user: {
|
||||||
|
id: 'moderatorId',
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
||||||
|
switch (value) {
|
||||||
|
case 'target':
|
||||||
|
return {
|
||||||
|
user: {
|
||||||
|
id: 'userId',
|
||||||
|
tag: 'userTag',
|
||||||
|
createDM: jest.fn().mockReturnValue({
|
||||||
|
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
||||||
|
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
||||||
|
|
||||||
|
return mock<Message<false>>();
|
||||||
|
})
|
||||||
|
}) as unknown as DMChannel,
|
||||||
|
} as unknown as User,
|
||||||
|
member: {
|
||||||
|
manageable: true,
|
||||||
|
timeout: timeoutFunc,
|
||||||
|
} as unknown as GuildMember
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
case 'length':
|
||||||
|
return {
|
||||||
|
value: '1m'
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
case 'reason':
|
||||||
|
return {
|
||||||
|
value: 'Test reason',
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
|
expect(timeoutFunc).toBeCalled();
|
||||||
|
expect(sentEmbeds.length).toBe(0);
|
||||||
|
expect(logChannelSendFunc).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GIVEN channel IS NULL, EXPECT embed to not be sent', async () => {
|
||||||
|
const command = new Timeout();
|
||||||
|
|
||||||
|
let savedAudit: DeepPartial<Audit> | undefined;
|
||||||
|
|
||||||
|
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||||
|
savedAudit = entity;
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
||||||
|
|
||||||
|
const timeoutFunc = jest.fn();
|
||||||
|
|
||||||
|
const sentEmbeds: EmbedBuilder[] = [];
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn(),
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(undefined),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guildId: 'guildId',
|
||||||
|
user: {
|
||||||
|
id: 'moderatorId',
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
||||||
|
switch (value) {
|
||||||
|
case 'target':
|
||||||
|
return {
|
||||||
|
user: {
|
||||||
|
id: 'userId',
|
||||||
|
tag: 'userTag',
|
||||||
|
createDM: jest.fn().mockReturnValue({
|
||||||
|
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
||||||
|
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
||||||
|
|
||||||
|
return mock<Message<false>>();
|
||||||
|
})
|
||||||
|
}) as unknown as DMChannel,
|
||||||
|
} as unknown as User,
|
||||||
|
member: {
|
||||||
|
manageable: true,
|
||||||
|
timeout: timeoutFunc,
|
||||||
|
} as unknown as GuildMember
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
case 'length':
|
||||||
|
return {
|
||||||
|
value: '1m'
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
case 'reason':
|
||||||
|
return {
|
||||||
|
value: 'Test reason',
|
||||||
|
} as CommandInteractionOption;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
|
expect(timeoutFunc).toBeCalled();
|
||||||
|
expect(sentEmbeds.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
// DM user
|
// DM user
|
||||||
test.todo('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"');
|
test('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"', async () => {
|
||||||
|
let embeds: APIEmbed[] | undefined;
|
||||||
|
|
||||||
|
const command = new Timeout();
|
||||||
|
|
||||||
|
const interactionReply = jest.fn((options: InteractionReplyOptions) => {
|
||||||
|
embeds = options.embeds as APIEmbed[];
|
||||||
|
});
|
||||||
|
|
||||||
|
let savedAudit: DeepPartial<Audit> | undefined;
|
||||||
|
|
||||||
|
const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
||||||
|
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
||||||
|
savedAudit = entity;
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
const timeoutFunc = jest.fn();
|
||||||
|
|
||||||
|
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
|
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
|
|
||||||
|
const dmChannel = {
|
||||||
|
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||||
|
dmChannelSentEmbeds = options.embeds;
|
||||||
|
}),
|
||||||
|
} as unknown as DMChannel;
|
||||||
|
|
||||||
|
const userInput = {
|
||||||
|
user: {
|
||||||
|
id: 'userId',
|
||||||
|
tag: 'userTag',
|
||||||
|
createDM: jest.fn().mockRejectedValue(undefined),
|
||||||
|
} as unknown 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().mockImplementation((options: MessageCreateOptions) => {
|
||||||
|
logsChannelSentEmbeds = options.embeds;
|
||||||
|
}),
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn()
|
||||||
|
.mockReturnValue(logsChannel),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
name: "Test Guild",
|
||||||
|
} 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 embeds to be sent
|
||||||
|
expect(embeds).toBeDefined();
|
||||||
|
expect(embeds!.length).toBe(1);
|
||||||
|
|
||||||
|
const resultEmbed = embeds![0] as EmbedBuilder;
|
||||||
|
|
||||||
|
// EXPECT DM field to be configured
|
||||||
|
const resultEmbedDMField = resultEmbed.data.fields![0];
|
||||||
|
|
||||||
|
expect(resultEmbedDMField.name).toBe("DM Sent");
|
||||||
|
expect(resultEmbedDMField.value).toBe("false");
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in a new issue