Vylpes
f61c4c728a
* Fix tests * Update coverage * Remove unrequired mock files * Add about command test * Update about tests * Ban command tests * eval command tests * Start help command tests * Add help command tests * Add kick command tests * Mute command tests * Poll command tests * Add role command tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add rules command tests * Add unmute command tests * Add warn command tests * Add MemberEvents tests * Add GuildMemberUpdate tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add MessageEvents tests * Add StringTools test Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add embed tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add GitHub Actions Signed-off-by: Ethan Lane <ethan@vylpes.com> * Move to tslint Signed-off-by: Ethan Lane <ethan@vylpes.com> * Remove tslint Signed-off-by: Ethan Lane <ethan@vylpes.com> * Remove linting script Signed-off-by: Ethan Lane <ethan@vylpes.com>
673 lines
No EOL
19 KiB
TypeScript
673 lines
No EOL
19 KiB
TypeScript
import { GuildMember, Message, Role, TextChannel, User } from "discord.js";
|
|
import Unmute from "../../src/commands/unmute";
|
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
|
|
beforeEach(() => {
|
|
process.env = {};
|
|
});
|
|
|
|
describe('Constructor', () => {
|
|
test('Expect properties to be set', () => {
|
|
process.env = {
|
|
ROLES_MODERATOR: 'Moderator'
|
|
};
|
|
|
|
const mute = new Unmute();
|
|
|
|
expect(mute._category).toBe("Moderation");
|
|
expect(mute._roles.length).toBe(1);
|
|
expect(mute._roles[0]).toBe('Moderator');
|
|
});
|
|
});
|
|
|
|
describe('Execute', () => {
|
|
test('Given user has permission, expect user to be given muted role', async () => {
|
|
process.env = {
|
|
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
ROLES_MUTED: 'Muted'
|
|
};
|
|
|
|
const user = {
|
|
displayAvatarURL: jest.fn(),
|
|
tag: 'USERTAG'
|
|
} as unknown as User;
|
|
|
|
const messageAuthor = {
|
|
tag: 'AUTHORTAG'
|
|
} as unknown as User;
|
|
|
|
const member = {
|
|
manageable: true,
|
|
roles: {
|
|
remove: jest.fn()
|
|
}
|
|
} as unknown as GuildMember;
|
|
|
|
const role = {
|
|
name: 'Muted'
|
|
} as unknown as Role;
|
|
|
|
const logChannel = {
|
|
name: 'mod-logs',
|
|
send: jest.fn()
|
|
} as unknown as TextChannel;
|
|
|
|
const messageMentionsUsersFirst = jest.fn()
|
|
.mockReturnValue(user);
|
|
const messageGuildMember = jest.fn()
|
|
.mockReturnValue(member);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockImplementation((callback): Role | undefined => {
|
|
const result = callback(role);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return role;
|
|
});
|
|
const messageChannelSend = jest.fn();
|
|
const messageGuildChannelsCacheFind = jest.fn()
|
|
.mockImplementation((callback): TextChannel | undefined => {
|
|
const result = callback(logChannel);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return logChannel;
|
|
});
|
|
|
|
const message = {
|
|
mentions: {
|
|
users: {
|
|
first: messageMentionsUsersFirst
|
|
}
|
|
},
|
|
guild: {
|
|
member: messageGuildMember,
|
|
available: true,
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
},
|
|
channels: {
|
|
cache: {
|
|
find: messageGuildChannelsCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
},
|
|
author: messageAuthor
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'mute',
|
|
args: ['USER', 'Test', 'Reason'],
|
|
message: message
|
|
};
|
|
|
|
const mute = new Unmute();
|
|
|
|
const result = await mute.execute(context);
|
|
|
|
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
expect(messageGuildMember).toBeCalledWith(user);
|
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
expect(member.roles.remove).toBeCalledWith(role, 'Test Reason');
|
|
|
|
expect(result.embeds.length).toBe(2);
|
|
|
|
// Log Embed
|
|
const logEmbed = result.embeds[0];
|
|
|
|
expect(logEmbed.title).toBe('Member Unmuted');
|
|
expect(logEmbed.fields.length).toBe(3);
|
|
|
|
// Log Embed -> User Field
|
|
const logEmbedUserField = logEmbed.fields[0];
|
|
|
|
expect(logEmbedUserField.name).toBe('User');
|
|
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
|
|
expect(logEmbedUserField.inline).toBeTruthy();
|
|
|
|
// Log Embed -> Moderator Field
|
|
const logEmbedModeratorField = logEmbed.fields[1];
|
|
|
|
expect(logEmbedModeratorField.name).toBe('Moderator');
|
|
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
|
|
|
|
// Public Embed
|
|
const publicEmbed = result.embeds[1];
|
|
|
|
expect(publicEmbed.title).toBe('');
|
|
expect(publicEmbed.description).toBe('[object Object] has been unmuted');
|
|
});
|
|
|
|
test('Given user did not mention a user, expect user not to exist', async () => {
|
|
process.env = {
|
|
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
ROLES_MUTED: 'Muted'
|
|
};
|
|
|
|
const user = {
|
|
displayAvatarURL: jest.fn(),
|
|
tag: 'USERTAG'
|
|
} as unknown as User;
|
|
|
|
const messageAuthor = {
|
|
tag: 'AUTHORTAG'
|
|
} as unknown as User;
|
|
|
|
const member = {
|
|
manageable: true,
|
|
roles: {
|
|
remove: jest.fn()
|
|
}
|
|
} as unknown as GuildMember;
|
|
|
|
const role = {
|
|
name: 'Muted'
|
|
} as unknown as Role;
|
|
|
|
const logChannel = {
|
|
name: 'mod-logs',
|
|
send: jest.fn()
|
|
} as unknown as TextChannel;
|
|
|
|
const messageMentionsUsersFirst = jest.fn()
|
|
.mockReturnValue(null);
|
|
const messageGuildMember = jest.fn()
|
|
.mockReturnValue(member);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockImplementation((callback): Role | undefined => {
|
|
const result = callback(role);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return role;
|
|
});
|
|
const messageChannelSend = jest.fn();
|
|
const messageGuildChannelsCacheFind = jest.fn()
|
|
.mockImplementation((callback): TextChannel | undefined => {
|
|
const result = callback(logChannel);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return logChannel;
|
|
});
|
|
|
|
const message = {
|
|
mentions: {
|
|
users: {
|
|
first: messageMentionsUsersFirst
|
|
}
|
|
},
|
|
guild: {
|
|
member: messageGuildMember,
|
|
available: true,
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
},
|
|
channels: {
|
|
cache: {
|
|
find: messageGuildChannelsCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
},
|
|
author: messageAuthor
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'mute',
|
|
args: ['USER', 'Test', 'Reason'],
|
|
message: message
|
|
};
|
|
|
|
const mute = new Unmute();
|
|
|
|
const result = await mute.execute(context);
|
|
|
|
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
expect(messageGuildMember).not.toBeCalled();
|
|
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Error Embed
|
|
const errorEmbed = result.embeds[0];
|
|
|
|
expect(errorEmbed.description).toBe('User does not exist');
|
|
});
|
|
|
|
test('Given member can not be found from user, expect user to not be in server', async () => {
|
|
process.env = {
|
|
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
ROLES_MUTED: 'Muted'
|
|
};
|
|
|
|
const user = {
|
|
displayAvatarURL: jest.fn(),
|
|
tag: 'USERTAG'
|
|
} as unknown as User;
|
|
|
|
const messageAuthor = {
|
|
tag: 'AUTHORTAG'
|
|
} as unknown as User;
|
|
|
|
const member = {
|
|
manageable: true,
|
|
roles: {
|
|
remove: jest.fn()
|
|
}
|
|
} as unknown as GuildMember;
|
|
|
|
const role = {
|
|
name: 'Muted'
|
|
} as unknown as Role;
|
|
|
|
const logChannel = {
|
|
name: 'mod-logs',
|
|
send: jest.fn()
|
|
} as unknown as TextChannel;
|
|
|
|
const messageMentionsUsersFirst = jest.fn()
|
|
.mockReturnValue(user);
|
|
const messageGuildMember = jest.fn()
|
|
.mockReturnValue(null);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockImplementation((callback): Role | undefined => {
|
|
const result = callback(role);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return role;
|
|
});
|
|
const messageChannelSend = jest.fn();
|
|
const messageGuildChannelsCacheFind = jest.fn()
|
|
.mockImplementation((callback): TextChannel | undefined => {
|
|
const result = callback(logChannel);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return logChannel;
|
|
});
|
|
|
|
const message = {
|
|
mentions: {
|
|
users: {
|
|
first: messageMentionsUsersFirst
|
|
}
|
|
},
|
|
guild: {
|
|
member: messageGuildMember,
|
|
available: true,
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
},
|
|
channels: {
|
|
cache: {
|
|
find: messageGuildChannelsCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
},
|
|
author: messageAuthor
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'mute',
|
|
args: ['USER', 'Test', 'Reason'],
|
|
message: message
|
|
};
|
|
|
|
const mute = new Unmute();
|
|
|
|
const result = await mute.execute(context);
|
|
|
|
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
expect(messageGuildMember).toBeCalledWith(user);
|
|
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Error Embed
|
|
const errorEmbed = result.embeds[0];
|
|
|
|
expect(errorEmbed.description).toBe('User is not in this server');
|
|
});
|
|
|
|
test('Given guild is unavailable, expect execution to stop', async () => {
|
|
process.env = {
|
|
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
ROLES_MUTED: 'Muted'
|
|
};
|
|
|
|
const user = {
|
|
displayAvatarURL: jest.fn(),
|
|
tag: 'USERTAG'
|
|
} as unknown as User;
|
|
|
|
const messageAuthor = {
|
|
tag: 'AUTHORTAG'
|
|
} as unknown as User;
|
|
|
|
const member = {
|
|
manageable: true,
|
|
roles: {
|
|
remove: jest.fn()
|
|
}
|
|
} as unknown as GuildMember;
|
|
|
|
const role = {
|
|
name: 'Muted'
|
|
} as unknown as Role;
|
|
|
|
const logChannel = {
|
|
name: 'mod-logs',
|
|
send: jest.fn()
|
|
} as unknown as TextChannel;
|
|
|
|
const messageMentionsUsersFirst = jest.fn()
|
|
.mockReturnValue(user);
|
|
const messageGuildMember = jest.fn()
|
|
.mockReturnValue(member);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockImplementation((callback): Role | undefined => {
|
|
const result = callback(role);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return role;
|
|
});
|
|
const messageChannelSend = jest.fn();
|
|
const messageGuildChannelsCacheFind = jest.fn()
|
|
.mockImplementation((callback): TextChannel | undefined => {
|
|
const result = callback(logChannel);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return logChannel;
|
|
});
|
|
|
|
const message = {
|
|
mentions: {
|
|
users: {
|
|
first: messageMentionsUsersFirst
|
|
}
|
|
},
|
|
guild: {
|
|
member: messageGuildMember,
|
|
available: false,
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
},
|
|
channels: {
|
|
cache: {
|
|
find: messageGuildChannelsCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
},
|
|
author: messageAuthor
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'mute',
|
|
args: ['USER', 'Test', 'Reason'],
|
|
message: message
|
|
};
|
|
|
|
const mute = new Unmute();
|
|
|
|
const result = await mute.execute(context);
|
|
|
|
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
expect(messageGuildMember).toBeCalledWith(user);
|
|
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
expect(messageChannelSend).not.toBeCalled();
|
|
|
|
expect(result.embeds.length).toBe(0);
|
|
});
|
|
|
|
test('Given client can not manage user, expect insufficient permissions', async () => {
|
|
process.env = {
|
|
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
ROLES_MUTED: 'Muted'
|
|
};
|
|
|
|
const user = {
|
|
displayAvatarURL: jest.fn(),
|
|
tag: 'USERTAG'
|
|
} as unknown as User;
|
|
|
|
const messageAuthor = {
|
|
tag: 'AUTHORTAG'
|
|
} as unknown as User;
|
|
|
|
const member = {
|
|
manageable: false,
|
|
roles: {
|
|
remove: jest.fn()
|
|
}
|
|
} as unknown as GuildMember;
|
|
|
|
const role = {
|
|
name: 'Muted'
|
|
} as unknown as Role;
|
|
|
|
const logChannel = {
|
|
name: 'mod-logs',
|
|
send: jest.fn()
|
|
} as unknown as TextChannel;
|
|
|
|
const messageMentionsUsersFirst = jest.fn()
|
|
.mockReturnValue(user);
|
|
const messageGuildMember = jest.fn()
|
|
.mockReturnValue(member);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockImplementation((callback): Role | undefined => {
|
|
const result = callback(role);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return role;
|
|
});
|
|
const messageChannelSend = jest.fn();
|
|
const messageGuildChannelsCacheFind = jest.fn()
|
|
.mockImplementation((callback): TextChannel | undefined => {
|
|
const result = callback(logChannel);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return logChannel;
|
|
});
|
|
|
|
const message = {
|
|
mentions: {
|
|
users: {
|
|
first: messageMentionsUsersFirst
|
|
}
|
|
},
|
|
guild: {
|
|
member: messageGuildMember,
|
|
available: true,
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
},
|
|
channels: {
|
|
cache: {
|
|
find: messageGuildChannelsCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
},
|
|
author: messageAuthor
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'mute',
|
|
args: ['USER', 'Test', 'Reason'],
|
|
message: message
|
|
};
|
|
|
|
const mute = new Unmute();
|
|
|
|
const result = await mute.execute(context);
|
|
|
|
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
expect(messageGuildMember).toBeCalledWith(user);
|
|
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Error Embed
|
|
const errorEmbed = result.embeds[0];
|
|
|
|
expect(errorEmbed.description).toBe('Unable to do this action, am I missing permissions?');
|
|
});
|
|
|
|
test('Given muted role can not be found, expect role not found', async () => {
|
|
process.env = {
|
|
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
ROLES_MUTED: 'Muted'
|
|
};
|
|
|
|
const user = {
|
|
displayAvatarURL: jest.fn(),
|
|
tag: 'USERTAG'
|
|
} as unknown as User;
|
|
|
|
const messageAuthor = {
|
|
tag: 'AUTHORTAG'
|
|
} as unknown as User;
|
|
|
|
const member = {
|
|
manageable: true,
|
|
roles: {
|
|
remove: jest.fn()
|
|
}
|
|
} as unknown as GuildMember;
|
|
|
|
const role = {
|
|
name: 'Muted'
|
|
} as unknown as Role;
|
|
|
|
const logChannel = {
|
|
name: 'mod-logs',
|
|
send: jest.fn()
|
|
} as unknown as TextChannel;
|
|
|
|
const messageMentionsUsersFirst = jest.fn()
|
|
.mockReturnValue(user);
|
|
const messageGuildMember = jest.fn()
|
|
.mockReturnValue(member);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockReturnValue(undefined);
|
|
const messageChannelSend = jest.fn();
|
|
const messageGuildChannelsCacheFind = jest.fn()
|
|
.mockImplementation((callback): TextChannel | undefined => {
|
|
const result = callback(logChannel);
|
|
|
|
if (!result) {
|
|
return undefined;
|
|
}
|
|
|
|
return logChannel;
|
|
});
|
|
|
|
const message = {
|
|
mentions: {
|
|
users: {
|
|
first: messageMentionsUsersFirst
|
|
}
|
|
},
|
|
guild: {
|
|
member: messageGuildMember,
|
|
available: true,
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
},
|
|
channels: {
|
|
cache: {
|
|
find: messageGuildChannelsCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
},
|
|
author: messageAuthor
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'mute',
|
|
args: ['USER', 'Test', 'Reason'],
|
|
message: message
|
|
};
|
|
|
|
const mute = new Unmute();
|
|
|
|
const result = await mute.execute(context);
|
|
|
|
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
expect(messageGuildMember).toBeCalledWith(user);
|
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Error Embed
|
|
const errorEmbed = result.embeds[0];
|
|
|
|
expect(errorEmbed.description).toBe('Unable to find role');
|
|
});
|
|
}); |