Compare commits
No commits in common. "feature/258-fix-tests" and "main" have entirely different histories.
feature/25
...
main
|
@ -54,10 +54,11 @@ steps:
|
||||||
- yarn install --frozen-lockfile
|
- yarn install --frozen-lockfile
|
||||||
- yarn build
|
- yarn build
|
||||||
|
|
||||||
- name: test
|
# - name: test
|
||||||
image: node
|
# image: node
|
||||||
commands:
|
# commands:
|
||||||
- yarn test
|
# - yarn install --frozen-lockfile
|
||||||
|
# - yarn test
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
branch:
|
branch:
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
# any secret values.
|
# any secret values.
|
||||||
|
|
||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
BOT_VER=3.3.0
|
BOT_VER=3.2.1
|
||||||
BOT_AUTHOR=Vylpes
|
BOT_AUTHOR=Vylpes
|
||||||
BOT_OWNERID=147392775707426816
|
BOT_OWNERID=147392775707426816
|
||||||
BOT_CLIENTID=682942374040961060
|
BOT_CLIENTID=682942374040961060
|
||||||
|
|
|
@ -12,14 +12,14 @@ jobs:
|
||||||
runs-on: node
|
runs-on: node
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v2
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v1
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: npm ci
|
||||||
- run: yarn build
|
- run: npm run build
|
||||||
- run: yarn test
|
- run: npm test
|
||||||
|
|
||||||
- name: "Copy files over to location"
|
- name: "Copy files over to location"
|
||||||
run: cp -r . ${{ secrets.PROD_REPO_PATH }}
|
run: cp -r . ${{ secrets.PROD_REPO_PATH }}
|
||||||
|
@ -63,5 +63,5 @@ jobs:
|
||||||
&& (pm2 delete vylbot_prod || true) \
|
&& (pm2 delete vylbot_prod || true) \
|
||||||
&& docker compose up -d \
|
&& docker compose up -d \
|
||||||
&& sleep 10 \
|
&& sleep 10 \
|
||||||
&& yarn db:up \
|
&& npm run db:up \
|
||||||
&& pm2 start --name vylbot_prod dist/vylbot.js
|
&& pm2 start --name vylbot_prod dist/vylbot.js
|
|
@ -12,14 +12,14 @@ jobs:
|
||||||
runs-on: node
|
runs-on: node
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v2
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v1
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: npm ci
|
||||||
- run: yarn build
|
- run: npm run build
|
||||||
- run: yarn test
|
- run: npm test
|
||||||
|
|
||||||
- name: "Copy files over to location"
|
- name: "Copy files over to location"
|
||||||
run: cp -r . ${{ secrets.STAGE_REPO_PATH }}
|
run: cp -r . ${{ secrets.STAGE_REPO_PATH }}
|
||||||
|
@ -63,5 +63,5 @@ jobs:
|
||||||
&& (pm2 delete vylbot_stage || true) \
|
&& (pm2 delete vylbot_stage || true) \
|
||||||
&& docker compose up -d \
|
&& docker compose up -d \
|
||||||
&& sleep 10 \
|
&& sleep 10 \
|
||||||
&& yarn db:up \
|
&& npm run db:up \
|
||||||
&& pm2 start --name vylbot_stage dist/vylbot.js
|
&& pm2 start --name vylbot_stage dist/vylbot.js
|
|
@ -14,11 +14,11 @@ jobs:
|
||||||
runs-on: node
|
runs-on: node
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v2
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v1
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: npm ci
|
||||||
- run: yarn build
|
- run: npm run build
|
||||||
- run: yarn test
|
- run: npm test
|
8815
package-lock.json
generated
Normal file
8815
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -8,7 +8,7 @@
|
||||||
"clean": "rm -rf node_modules/ dist/",
|
"clean": "rm -rf node_modules/ dist/",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node ./dist/vylbot",
|
"start": "node ./dist/vylbot",
|
||||||
"test": "jest",
|
"test": "echo true",
|
||||||
"db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js",
|
"db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js",
|
||||||
"db:down": "typeorm migration:revert -d dist/database/dataSources/appDataSource.js",
|
"db:down": "typeorm migration:revert -d dist/database/dataSources/appDataSource.js",
|
||||||
"db:create": "typeorm migration:create ./src/database/migrations",
|
"db:create": "typeorm migration:create ./src/database/migrations",
|
||||||
|
@ -35,7 +35,7 @@
|
||||||
"emoji-regex": "^10.0.0",
|
"emoji-regex": "^10.0.0",
|
||||||
"jest": "^29.0.0",
|
"jest": "^29.0.0",
|
||||||
"jest-mock-extended": "^3.0.0",
|
"jest-mock-extended": "^3.0.0",
|
||||||
"minimatch": "9.0.4",
|
"minimatch": "9.0.3",
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1",
|
||||||
"random-bunny": "^2.1.6",
|
"random-bunny": "^2.1.6",
|
||||||
"ts-jest": "^29.0.0",
|
"ts-jest": "^29.0.0",
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Client, Partials } from "discord.js";
|
import { Client, Partials } from "discord.js";
|
||||||
import * as dotenv from "dotenv";
|
import * as dotenv from "dotenv";
|
||||||
|
import { createConnection } from "typeorm";
|
||||||
import { EventType } from "../constants/EventType";
|
import { EventType } from "../constants/EventType";
|
||||||
import ICommandItem from "../contracts/ICommandItem";
|
import ICommandItem from "../contracts/ICommandItem";
|
||||||
import IEventItem from "../contracts/IEventItem";
|
import IEventItem from "../contracts/IEventItem";
|
||||||
|
@ -13,20 +14,32 @@ import { ButtonEvent } from "../type/buttonEvent";
|
||||||
import CacheHelper from "../helpers/CacheHelper";
|
import CacheHelper from "../helpers/CacheHelper";
|
||||||
|
|
||||||
export class CoreClient extends Client {
|
export class CoreClient extends Client {
|
||||||
public static commandItems: ICommandItem[];
|
private static _commandItems: ICommandItem[];
|
||||||
public static eventItems: IEventItem[];
|
private static _eventItems: IEventItem[];
|
||||||
public static buttonEvents: ButtonEventItem[];
|
private static _buttonEvents: ButtonEventItem[];
|
||||||
|
|
||||||
private _events: Events;
|
private _events: Events;
|
||||||
private _util: Util;
|
private _util: Util;
|
||||||
|
|
||||||
|
public static get commandItems(): ICommandItem[] {
|
||||||
|
return this._commandItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static get eventItems(): IEventItem[] {
|
||||||
|
return this._eventItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static get buttonEvents(): ButtonEventItem[] {
|
||||||
|
return this._buttonEvents;
|
||||||
|
}
|
||||||
|
|
||||||
constructor(intents: number[], partials: Partials[]) {
|
constructor(intents: number[], partials: Partials[]) {
|
||||||
super({ intents: intents, partials: partials });
|
super({ intents: intents, partials: partials });
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
CoreClient.commandItems = [];
|
CoreClient._commandItems = [];
|
||||||
CoreClient.eventItems = [];
|
CoreClient._eventItems = [];
|
||||||
CoreClient.buttonEvents = [];
|
CoreClient._buttonEvents = [];
|
||||||
|
|
||||||
this._events = new Events();
|
this._events = new Events();
|
||||||
this._util = new Util();
|
this._util = new Util();
|
||||||
|
@ -38,12 +51,9 @@ export class CoreClient extends Client {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
await AppDataSource.initialize()
|
||||||
await AppDataSource.initialize();
|
.then(() => console.log("Data Source Initialized"))
|
||||||
} catch (err) {
|
.catch((err) => console.error("Error Initialising Data Source", err));
|
||||||
console.error("Error Initialising Data Source", err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
super.on("interactionCreate", this._events.onInteractionCreate);
|
super.on("interactionCreate", this._events.onInteractionCreate);
|
||||||
super.on("ready", this._events.onReady);
|
super.on("ready", this._events.onReady);
|
||||||
|
@ -54,7 +64,7 @@ export class CoreClient extends Client {
|
||||||
await CacheHelper.UpdateServerCache(guild);
|
await CacheHelper.UpdateServerCache(guild);
|
||||||
});
|
});
|
||||||
|
|
||||||
this._util.loadEvents(this, CoreClient.eventItems);
|
this._util.loadEvents(this, CoreClient._eventItems);
|
||||||
this._util.loadSlashCommands(this);
|
this._util.loadSlashCommands(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +75,7 @@ export class CoreClient extends Client {
|
||||||
ServerId: serverId,
|
ServerId: serverId,
|
||||||
};
|
};
|
||||||
|
|
||||||
CoreClient.commandItems.push(item);
|
CoreClient._commandItems.push(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RegisterEvent(eventType: EventType, func: Function) {
|
public static RegisterEvent(eventType: EventType, func: Function) {
|
||||||
|
@ -74,7 +84,7 @@ export class CoreClient extends Client {
|
||||||
ExecutionFunction: func,
|
ExecutionFunction: func,
|
||||||
};
|
};
|
||||||
|
|
||||||
CoreClient.eventItems.push(item);
|
CoreClient._eventItems.push(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RegisterButtonEvent(buttonId: string, event: ButtonEvent) {
|
public static RegisterButtonEvent(buttonId: string, event: ButtonEvent) {
|
||||||
|
@ -83,6 +93,6 @@ export class CoreClient extends Client {
|
||||||
Event: event,
|
Event: event,
|
||||||
};
|
};
|
||||||
|
|
||||||
CoreClient.buttonEvents.push(item);
|
CoreClient._buttonEvents.push(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,10 @@ export class Events {
|
||||||
|
|
||||||
if (interaction.isChatInputCommand()) {
|
if (interaction.isChatInputCommand()) {
|
||||||
ChatInputCommand.onChatInput(interaction);
|
ChatInputCommand.onChatInput(interaction);
|
||||||
} else if (interaction.isButton()) {
|
}
|
||||||
|
|
||||||
|
if (interaction.isButton()) {
|
||||||
Button.onButtonClicked(interaction);
|
Button.onButtonClicked(interaction);
|
||||||
} else {
|
|
||||||
console.error("Received interaction unable to interact with, ignoring");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,6 @@ export default class AddRole extends Command {
|
||||||
const entity = new eLobby(channel.channel.id, role.role.id, cooldown.value as number, gameName.value as string);
|
const entity = new eLobby(channel.channel.id, role.role.id, cooldown.value as number, gameName.value as string);
|
||||||
await entity.Save(eLobby, entity);
|
await entity.Save(eLobby, entity);
|
||||||
|
|
||||||
await interaction.reply(`Added \`${channel.name}\` as a new lobby channel with a cooldown of \`${cooldown.value} minutes\` and will ping \`${role.name}\` on use`);
|
await interaction.reply(`Added \`${channel.name}\` as a new lobby channel with a cooldown of \`${cooldown.value} minutes \` and will ping \`${role.name}\` on use`);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import { CacheType, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
|
import { CacheType, CommandInteraction, EmbedBuilder, GuildBasedChannel, PermissionsBitField, SlashCommandBuilder } from "discord.js";
|
||||||
import { Command } from "../../../type/command";
|
import { Command } from "../../../type/command";
|
||||||
import { default as eLobby } from "../../../database/entities/501231711271780357/Lobby";
|
import { default as eLobby } from "../../../database/entities/501231711271780357/Lobby";
|
||||||
import EmbedColours from "../../../constants/EmbedColours";
|
import EmbedColours from "../../../constants/EmbedColours";
|
||||||
|
|
|
@ -72,18 +72,20 @@ export default class Role extends Command {
|
||||||
|
|
||||||
const userRole = roleManager.cache.find(x => x.name == requestedRole.role!.name);
|
const userRole = roleManager.cache.find(x => x.name == requestedRole.role!.name);
|
||||||
const assignRole = interaction.guild.roles.cache.find(x => x.id == requestedRole.role!.id);
|
const assignRole = interaction.guild.roles.cache.find(x => x.id == requestedRole.role!.id);
|
||||||
|
|
||||||
if (!assignRole!.editable) {
|
if (!assignRole) return;
|
||||||
|
|
||||||
|
if (!assignRole.editable) {
|
||||||
await interaction.reply('Insufficient permissions. Please contact a moderator.');
|
await interaction.reply('Insufficient permissions. Please contact a moderator.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userRole) {
|
if (!userRole) {
|
||||||
await roleManager.add(assignRole!);
|
await roleManager.add(assignRole);
|
||||||
await interaction.reply(`Gave role: \`${assignRole!.name}\``);
|
await interaction.reply(`Gave role: \`${assignRole.name}\``);
|
||||||
} else {
|
} else {
|
||||||
await roleManager.remove(assignRole!);
|
await roleManager.remove(assignRole);
|
||||||
await interaction.reply(`Removed role: \`${assignRole!.name}\``);
|
await interaction.reply(`Removed role: \`${assignRole.name}\``);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,8 @@ export default class Audits extends Command {
|
||||||
{ name: 'Mute', value: AuditType.Mute.toString() },
|
{ name: 'Mute', value: AuditType.Mute.toString() },
|
||||||
{ name: 'Kick', value: AuditType.Kick.toString() },
|
{ name: 'Kick', value: AuditType.Kick.toString() },
|
||||||
{ name: 'Ban', value: AuditType.Ban.toString() },
|
{ name: 'Ban', value: AuditType.Ban.toString() },
|
||||||
))
|
)
|
||||||
|
.setRequired(true))
|
||||||
.addStringOption(option =>
|
.addStringOption(option =>
|
||||||
option
|
option
|
||||||
.setName('reason')
|
.setName('reason')
|
||||||
|
@ -92,7 +93,7 @@ export default class Audits extends Command {
|
||||||
private async SendAuditForUser(interaction: CommandInteraction) {
|
private async SendAuditForUser(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const user = interaction.options.get('target', true).user!;
|
const user = interaction.options.getUser('target');
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
await interaction.reply("User not found.");
|
await interaction.reply("User not found.");
|
||||||
|
@ -190,7 +191,7 @@ export default class Audits extends Command {
|
||||||
private async AddAudit(interaction: CommandInteraction) {
|
private async AddAudit(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const user = interaction.options.get('target', true).user!;
|
const user = interaction.options.getUser('target');
|
||||||
const auditType = interaction.options.get('type');
|
const auditType = interaction.options.get('type');
|
||||||
const reasonInput = interaction.options.get('reason');
|
const reasonInput = interaction.options.get('reason');
|
||||||
|
|
||||||
|
@ -208,4 +209,4 @@ export default class Audits extends Command {
|
||||||
|
|
||||||
await interaction.reply(`Created new audit with ID \`${audit.AuditId}\``);
|
await interaction.reply(`Created new audit with ID \`${audit.AuditId}\``);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -26,7 +26,6 @@ export default class Bunny extends Command {
|
||||||
];
|
];
|
||||||
|
|
||||||
const random = Math.floor(Math.random() * subreddits.length);
|
const random = Math.floor(Math.random() * subreddits.length);
|
||||||
|
|
||||||
const selectedSubreddit = subreddits[random];
|
const selectedSubreddit = subreddits[random];
|
||||||
|
|
||||||
const result = await randomBunny(selectedSubreddit, 'hot');
|
const result = await randomBunny(selectedSubreddit, 'hot');
|
||||||
|
|
|
@ -61,7 +61,7 @@ export default class Config extends Command {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!server) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,13 +70,13 @@ export default class Config extends Command {
|
||||||
await this.SendHelpText(interaction);
|
await this.SendHelpText(interaction);
|
||||||
break;
|
break;
|
||||||
case 'reset':
|
case 'reset':
|
||||||
await this.ResetValue(interaction, server);
|
await this.ResetValue(interaction);
|
||||||
break;
|
break;
|
||||||
case 'get':
|
case 'get':
|
||||||
await this.GetValue(interaction, server);
|
await this.GetValue(interaction);
|
||||||
break;
|
break;
|
||||||
case 'set':
|
case 'set':
|
||||||
await this.SetValue(interaction, server);
|
await this.SetValue(interaction);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
await interaction.reply('Subcommand not found.');
|
await interaction.reply('Subcommand not found.');
|
||||||
|
@ -94,7 +94,9 @@ export default class Config extends Command {
|
||||||
await interaction.reply({ embeds: [ embed ]});
|
await interaction.reply({ embeds: [ embed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async GetValue(interaction: CommandInteraction, server: Server) {
|
private async GetValue(interaction: CommandInteraction) {
|
||||||
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const key = interaction.options.get('key');
|
const key = interaction.options.get('key');
|
||||||
|
|
||||||
if (!key || !key.value) {
|
if (!key || !key.value) {
|
||||||
|
@ -102,6 +104,15 @@ export default class Config extends Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [
|
||||||
|
"Settings",
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!server) {
|
||||||
|
await interaction.reply('Server not found.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
||||||
|
|
||||||
if (setting) {
|
if (setting) {
|
||||||
|
@ -117,7 +128,7 @@ export default class Config extends Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ResetValue(interaction: CommandInteraction, server: Server) {
|
private async ResetValue(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const key = interaction.options.get('key');
|
const key = interaction.options.get('key');
|
||||||
|
@ -127,6 +138,15 @@ export default class Config extends Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [
|
||||||
|
"Settings",
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!server) {
|
||||||
|
await interaction.reply('Server not found.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
||||||
|
|
||||||
if (!setting) {
|
if (!setting) {
|
||||||
|
@ -139,18 +159,34 @@ export default class Config extends Command {
|
||||||
await interaction.reply('The setting has been reset to the default.');
|
await interaction.reply('The setting has been reset to the default.');
|
||||||
}
|
}
|
||||||
|
|
||||||
private async SetValue(interaction: CommandInteraction, server: Server) {
|
private async SetValue(interaction: CommandInteraction) {
|
||||||
const key = interaction.options.get('key', true);
|
if (!interaction.guildId) return;
|
||||||
const value = interaction.options.get('value', true);
|
|
||||||
|
const key = interaction.options.get('key');
|
||||||
|
const value = interaction.options.get('value');
|
||||||
|
|
||||||
|
if (!key || !key.value || !value || !value.value) {
|
||||||
|
await interaction.reply('Fields are required.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const server = await Server.FetchOneById<Server>(Server, interaction.guildId, [
|
||||||
|
"Settings",
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!server) {
|
||||||
|
await interaction.reply('Server not found.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
const setting = server.Settings.filter(x => x.Key == key.value)[0];
|
||||||
|
|
||||||
if (setting) {
|
if (setting) {
|
||||||
setting.UpdateBasicDetails(key.value!.toString(), value.value!.toString());
|
setting.UpdateBasicDetails(key.value.toString(), value.value.toString());
|
||||||
|
|
||||||
await setting.Save(Setting, setting);
|
await setting.Save(Setting, setting);
|
||||||
} else {
|
} else {
|
||||||
const newSetting = new Setting(key.value!.toString(), value.value!.toString());
|
const newSetting = new Setting(key.value.toString(), value.value.toString());
|
||||||
|
|
||||||
await newSetting.Save(Setting, newSetting);
|
await newSetting.Save(Setting, newSetting);
|
||||||
|
|
||||||
|
|
|
@ -48,12 +48,17 @@ export default class Disable extends Command {
|
||||||
private async Add(interaction: CommandInteraction) {
|
private async Add(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const commandName = interaction.options.get('name', true);
|
const commandName = interaction.options.get('name');
|
||||||
|
|
||||||
|
if (!commandName || !commandName.value) {
|
||||||
|
await interaction.reply('Fields are required.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", interaction.guildId);
|
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", interaction.guildId);
|
||||||
const disabledCommands = disabledCommandsString != undefined ? disabledCommandsString?.split(",") : [];
|
const disabledCommands = disabledCommandsString != "" ? disabledCommandsString?.split(",") : [];
|
||||||
|
|
||||||
disabledCommands?.push(commandName.value!.toString());
|
disabledCommands?.push(commandName.value.toString());
|
||||||
|
|
||||||
await SettingsHelper.SetSetting("commands.disabled", interaction.guildId, disabledCommands!.join(","));
|
await SettingsHelper.SetSetting("commands.disabled", interaction.guildId, disabledCommands!.join(","));
|
||||||
|
|
||||||
|
@ -63,18 +68,23 @@ export default class Disable extends Command {
|
||||||
private async Remove(interaction: CommandInteraction) {
|
private async Remove(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const commandName = interaction.options.get('name', true);
|
const commandName = interaction.options.get('name');
|
||||||
|
|
||||||
|
if (!commandName || !commandName.value) {
|
||||||
|
await interaction.reply('Fields are required.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", interaction.guildId);
|
const disabledCommandsString = await SettingsHelper.GetSetting("commands.disabled", interaction.guildId);
|
||||||
const disabledCommands = disabledCommandsString != undefined ? disabledCommandsString?.split(",") : [];
|
const disabledCommands = disabledCommandsString != "" ? disabledCommandsString?.split(",") : [];
|
||||||
|
|
||||||
const disabledCommandsInstance = disabledCommands?.findIndex(x => x == commandName.value!.toString());
|
const disabledCommandsInstance = disabledCommands?.findIndex(x => x == commandName.value!.toString());
|
||||||
|
|
||||||
if (disabledCommandsInstance > -1) {
|
if (disabledCommandsInstance! > -1) {
|
||||||
disabledCommands?.splice(disabledCommandsInstance, 1);
|
disabledCommands?.splice(disabledCommandsInstance!, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
await SettingsHelper.SetSetting("commands.disabled", interaction.guildId, disabledCommands.join(","));
|
await SettingsHelper.SetSetting("commands.disabled", interaction.guildId, disabledCommands!.join(","));
|
||||||
|
|
||||||
await interaction.reply(`Enabled command ${commandName.value}`);
|
await interaction.reply(`Enabled command ${commandName.value}`);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,16 +13,23 @@ export default class Ignore extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
public override async execute(interaction: CommandInteraction) {
|
||||||
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.channelId);
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
|
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.guildId);
|
||||||
|
|
||||||
if (isChannelIgnored) {
|
if (isChannelIgnored) {
|
||||||
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.channelId);
|
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.guildId);
|
||||||
|
|
||||||
await IgnoredChannel.Remove(IgnoredChannel, entity!);
|
if (!entity) {
|
||||||
|
await interaction.reply('Unable to find channel.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await IgnoredChannel.Remove(IgnoredChannel, entity);
|
||||||
|
|
||||||
await interaction.reply('This channel will start being logged again.');
|
await interaction.reply('This channel will start being logged again.');
|
||||||
} else {
|
} else {
|
||||||
const entity = new IgnoredChannel(interaction.channelId);
|
const entity = new IgnoredChannel(interaction.guildId);
|
||||||
|
|
||||||
await entity.Save(IgnoredChannel, entity);
|
await entity.Save(IgnoredChannel, entity);
|
||||||
|
|
||||||
|
|
|
@ -29,17 +29,22 @@ export default class Kick extends Command {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
if (!interaction.guild) return;
|
if (!interaction.guild) return;
|
||||||
|
|
||||||
const targetUser = interaction.options.get('target', true);
|
const targetUser = interaction.options.get('target');
|
||||||
const reasonInput = interaction.options.get('reason');
|
const reasonInput = interaction.options.get('reason');
|
||||||
|
|
||||||
|
if (!targetUser || !targetUser.user || !targetUser.member) {
|
||||||
|
await interaction.reply("User not found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const member = targetUser.member as GuildMember;
|
const member = targetUser.member as GuildMember;
|
||||||
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
|
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
|
||||||
|
|
||||||
const logEmbed = new EmbedBuilder()
|
const logEmbed = new EmbedBuilder()
|
||||||
.setColor(EmbedColours.Ok)
|
.setColor(EmbedColours.Ok)
|
||||||
.setTitle("Member Kicked")
|
.setTitle("Member Kicked")
|
||||||
.setDescription(`<@${targetUser.user!.id}> \`${targetUser.user!.tag}\``)
|
.setDescription(`<@${targetUser.user.id}> \`${targetUser.user.tag}\``)
|
||||||
.setThumbnail(targetUser.user!.avatarURL())
|
.setThumbnail(targetUser.user.avatarURL())
|
||||||
.addFields([
|
.addFields([
|
||||||
{
|
{
|
||||||
name: "Moderator",
|
name: "Moderator",
|
||||||
|
@ -57,19 +62,19 @@ export default class Kick extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
await member.kick();
|
await member.kick();
|
||||||
await interaction.reply(`\`${targetUser.user!.tag}\` has been kicked.`);
|
await interaction.reply(`\`${targetUser.user.tag}\` has been kicked.`);
|
||||||
|
|
||||||
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
||||||
|
|
||||||
if (channelName) {
|
if (!channelName) return;
|
||||||
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
|
||||||
|
|
||||||
if (channel) {
|
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
|
||||||
}
|
if (channel) {
|
||||||
|
await channel.send({ embeds: [ logEmbed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
const audit = new Audit(targetUser.user!.id, AuditType.Kick, reason, interaction.user.id, interaction.guildId);
|
const audit = new Audit(targetUser.user.id, AuditType.Kick, reason, interaction.user.id, interaction.guildId);
|
||||||
await audit.Save(Audit, audit);
|
await audit.Save(Audit, audit);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -40,13 +40,15 @@ export default class Poll extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
public override async execute(interaction: CommandInteraction) {
|
||||||
const title = interaction.options.get('title', true);
|
const title = interaction.options.get('title');
|
||||||
const option1 = interaction.options.get('option1', true);
|
const option1 = interaction.options.get('option1');
|
||||||
const option2 = interaction.options.get('option2', true);
|
const option2 = interaction.options.get('option2');
|
||||||
const option3 = interaction.options.get('option3');
|
const option3 = interaction.options.get('option3');
|
||||||
const option4 = interaction.options.get('option4');
|
const option4 = interaction.options.get('option4');
|
||||||
const option5 = interaction.options.get('option5');
|
const option5 = interaction.options.get('option5');
|
||||||
|
|
||||||
|
if (!title || !option1 || !option2) return;
|
||||||
|
|
||||||
const description = [
|
const description = [
|
||||||
option1.value as string,
|
option1.value as string,
|
||||||
option2.value as string,
|
option2.value as string,
|
||||||
|
@ -56,7 +58,15 @@ export default class Poll extends Command {
|
||||||
]
|
]
|
||||||
.filter(x => x != null);
|
.filter(x => x != null);
|
||||||
|
|
||||||
const reactionEmojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣"];
|
const arrayOfNumbers = [
|
||||||
|
':one:',
|
||||||
|
':two:',
|
||||||
|
':three:',
|
||||||
|
':four:',
|
||||||
|
':five:',
|
||||||
|
];
|
||||||
|
|
||||||
|
const reactionEmojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣"];
|
||||||
|
|
||||||
description.forEach((value, index) => {
|
description.forEach((value, index) => {
|
||||||
description[index] = `${reactionEmojis[index]} ${description[index]}`;
|
description[index] = `${reactionEmojis[index]} ${description[index]}`;
|
||||||
|
@ -72,11 +82,10 @@ export default class Poll extends Command {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const messageResponse = await interaction.reply({ embeds: [ embed ]});
|
const message = await interaction.reply({ embeds: [ embed ]});
|
||||||
const message = await messageResponse.fetch();
|
|
||||||
|
|
||||||
description.forEach(async (value, index) => {
|
description.forEach(async (value, index) => {
|
||||||
await message.react(reactionEmojis[index]);
|
await (await message.fetch()).react(reactionEmojis[index]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -27,16 +27,21 @@ export default class Warn extends Command {
|
||||||
public override async execute(interaction: CommandInteraction) {
|
public override async execute(interaction: CommandInteraction) {
|
||||||
if (!interaction.guild || !interaction.guildId) return;
|
if (!interaction.guild || !interaction.guildId) return;
|
||||||
|
|
||||||
const targetUser = interaction.options.get('target', true).user!;
|
const targetUser = interaction.options.get('target');
|
||||||
const reasonInput = interaction.options.get('reason');
|
const reasonInput = interaction.options.get('reason');
|
||||||
|
|
||||||
|
if (!targetUser || !targetUser.user || !targetUser.member) {
|
||||||
|
await interaction.reply('Fields are required.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
|
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
|
||||||
|
|
||||||
const logEmbed = new EmbedBuilder()
|
const logEmbed = new EmbedBuilder()
|
||||||
.setColor(EmbedColours.Ok)
|
.setColor(EmbedColours.Ok)
|
||||||
.setTitle("Member Warned")
|
.setTitle("Member Warned")
|
||||||
.setDescription(`<@${targetUser.id}> \`${targetUser.tag}\``)
|
.setDescription(`<@${targetUser.user.id}> \`${targetUser.user.tag}\``)
|
||||||
.setThumbnail(targetUser.avatarURL())
|
.setThumbnail(targetUser.user.avatarURL())
|
||||||
.addFields([
|
.addFields([
|
||||||
{
|
{
|
||||||
name: "Moderator",
|
name: "Moderator",
|
||||||
|
@ -50,17 +55,17 @@ export default class Warn extends Command {
|
||||||
|
|
||||||
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
||||||
|
|
||||||
if (channelName) {
|
if (!channelName) return;
|
||||||
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
|
||||||
|
|
||||||
if (channel) {
|
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
|
||||||
}
|
if (channel) {
|
||||||
|
await channel.send({ embeds: [ logEmbed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
const audit = new Audit(targetUser.id, AuditType.Warn, reason, interaction.user.id, interaction.guildId);
|
const audit = new Audit(targetUser.user.id, AuditType.Warn, reason, interaction.user.id, interaction.guildId);
|
||||||
await audit.Save(Audit, audit);
|
await audit.Save(Audit, audit);
|
||||||
|
|
||||||
await interaction.reply('Successfully warned user.');
|
await interaction.reply('Successfully warned user.');
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -46,4 +46,14 @@ export default class SettingsHelper {
|
||||||
await server.Save(Server, server);
|
await server.Save(Server, server);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async GetServerPrefix(serverId: string): Promise<string> {
|
||||||
|
const setting = await this.GetSetting("bot.prefix", serverId);
|
||||||
|
|
||||||
|
if (!setting) {
|
||||||
|
return "v!";
|
||||||
|
}
|
||||||
|
|
||||||
|
return setting;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,155 +0,0 @@
|
||||||
import { ButtonInteraction, GuildMember, Role } from "discord.js";
|
|
||||||
import Verify from "../../src/buttonEvents/verify";
|
|
||||||
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
|
||||||
|
|
||||||
jest.mock("../../src/helpers/SettingsHelper");
|
|
||||||
|
|
||||||
describe("execute", () => {
|
|
||||||
let role: Role;
|
|
||||||
let member: GuildMember;
|
|
||||||
let interaction: ButtonInteraction;
|
|
||||||
|
|
||||||
let entity: Verify;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
entity = new Verify();
|
|
||||||
|
|
||||||
role = {} as Role;
|
|
||||||
|
|
||||||
member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn(),
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
guild: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(role),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
members: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(member),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} as unknown as ButtonInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("roleName");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("EXPECT verification role to be given", async () => {
|
|
||||||
await entity.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith({
|
|
||||||
content: "Given role",
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.role", "guildId");
|
|
||||||
|
|
||||||
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(member.roles.add).toHaveBeenCalledWith(role);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
||||||
interaction.guildId = null;
|
|
||||||
|
|
||||||
await entity.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
|
|
||||||
expect(interaction.guild?.roles.cache.find).not.toHaveBeenCalled();
|
|
||||||
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
|
|
||||||
expect(member.roles.add).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
|
|
||||||
interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
guild: null,
|
|
||||||
} as unknown as ButtonInteraction;
|
|
||||||
|
|
||||||
await entity.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
|
|
||||||
expect(member.roles.add).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN verification.role setting is not found, EXPECT nothing to happen", async () => {
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
|
|
||||||
|
|
||||||
await entity.execute(interaction);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("verification.role", "guildId");
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
expect(interaction.guild?.roles.cache.find).not.toHaveBeenCalled();
|
|
||||||
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
|
|
||||||
expect(member.roles.add).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN role can not be found, EXPECT error", async () => {
|
|
||||||
interaction.guild!.roles.cache.find = jest.fn().mockReturnValue(undefined);
|
|
||||||
|
|
||||||
await entity.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith({
|
|
||||||
content: "Unable to find the role, roleName",
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(interaction.guild?.members.cache.find).not.toHaveBeenCalled();
|
|
||||||
expect(member.roles.add).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN member can not be found, EXPECT error", async () => {
|
|
||||||
interaction.guild!.members.cache.find = jest.fn().mockReturnValue(undefined);
|
|
||||||
|
|
||||||
await entity.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith({
|
|
||||||
content: "Unable to give role to user",
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(member.roles.add).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN member is not manageable, EXPECT error", async () => {
|
|
||||||
member = {
|
|
||||||
manageable: false,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn(),
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
interaction.guild!.members.cache.find = jest.fn().mockReturnValue(member);
|
|
||||||
|
|
||||||
await entity.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith({
|
|
||||||
content: "Unable to give role to user",
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.guild?.members.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(member.roles.add).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,129 +1,153 @@
|
||||||
import { Partials, Client } from "discord.js";
|
import { mock } from "jest-mock-extended";
|
||||||
|
|
||||||
|
const connectionMock = mock<Connection>();
|
||||||
|
const qbuilderMock = mock<SelectQueryBuilder<any>>();
|
||||||
|
|
||||||
|
let repositoryMock = mock<Repository<any>>();
|
||||||
|
let settingMock = mock<Setting>();
|
||||||
|
|
||||||
|
jest.mock('typeorm', () => {
|
||||||
|
qbuilderMock.where.mockReturnThis();
|
||||||
|
qbuilderMock.select.mockReturnThis();
|
||||||
|
repositoryMock.createQueryBuilder.mockReturnValue(qbuilderMock);
|
||||||
|
repositoryMock.findOne.mockImplementation(async () => {
|
||||||
|
return settingMock;
|
||||||
|
});
|
||||||
|
connectionMock.getRepository.mockReturnValue(repositoryMock);
|
||||||
|
|
||||||
|
return {
|
||||||
|
getConnection: () => connectionMock,
|
||||||
|
createConnection: () => connectionMock,
|
||||||
|
|
||||||
|
BaseEntity: class Mock {},
|
||||||
|
ObjectType: () => {},
|
||||||
|
Entity: () => {},
|
||||||
|
InputType: () => {},
|
||||||
|
Index: () => {},
|
||||||
|
PrimaryColumn: () => {},
|
||||||
|
Column: () => {},
|
||||||
|
CreateDateColumn: () => {},
|
||||||
|
UpdateDateColumn: () => {},
|
||||||
|
OneToMany: () => {},
|
||||||
|
ManyToOne: () => {},
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.mock("discord.js");
|
||||||
|
jest.mock("dotenv");
|
||||||
|
jest.mock("../../src/client/events");
|
||||||
|
jest.mock("../../src/client/util");
|
||||||
|
jest.mock("../../src/constants/DefaultValues");
|
||||||
|
|
||||||
import { CoreClient } from "../../src/client/client";
|
import { CoreClient } from "../../src/client/client";
|
||||||
|
|
||||||
|
import { Client } from "discord.js";
|
||||||
import * as dotenv from "dotenv";
|
import * as dotenv from "dotenv";
|
||||||
import AppDataSource from "../../src/database/dataSources/appDataSource";
|
import { Events } from "../../src/client/events";
|
||||||
import { Util } from "../../src/client/util";
|
import { Util } from "../../src/client/util";
|
||||||
import { Command } from "../../src/type/command";
|
import { Command } from "../../src/type/command";
|
||||||
import { EventType } from "../../src/constants/EventType";
|
import { Event } from "../../src/type/event";
|
||||||
import { ButtonEvent } from "../../src/type/buttonEvent";
|
import DefaultValues from "../../src/constants/DefaultValues";
|
||||||
|
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
|
||||||
jest.mock("dotenv");
|
import Setting from "../../src/entity/Setting";
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.resetAllMocks();
|
||||||
|
jest.resetModules();
|
||||||
|
})
|
||||||
|
|
||||||
process.env = {};
|
describe('Constructor', () => {
|
||||||
});
|
test('Expect Successful Initialisation', () => {
|
||||||
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
|
expect(coreClient).toBeInstanceOf(Client);
|
||||||
|
expect(dotenv.config).toBeCalledTimes(1);
|
||||||
|
expect(Events).toBeCalledTimes(1);
|
||||||
|
expect(Util).toBeCalledTimes(1);
|
||||||
|
expect(DefaultValues.useDevPrefix).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
describe('constructor', () => {
|
test('Given devmode parameter is true, Expect devmode prefix to be true', () => {
|
||||||
test("EXPECT properties to be set", () => {
|
const coreClient = new CoreClient(true);
|
||||||
const client = new CoreClient([1, 2, 3], [Partials.User]);
|
|
||||||
|
expect(coreClient).toBeInstanceOf(Client);
|
||||||
expect(dotenv.config).toHaveBeenCalledTimes(1);
|
expect(dotenv.config).toBeCalledTimes(1);
|
||||||
|
expect(Events).toBeCalledTimes(1);
|
||||||
expect(client).toBeDefined();
|
expect(Util).toBeCalledTimes(1);
|
||||||
expect(CoreClient.commandItems).toEqual([]);
|
expect(DefaultValues.useDevPrefix).toBe(true);
|
||||||
expect(CoreClient.eventItems).toEqual([]);
|
|
||||||
expect(CoreClient.buttonEvents).toEqual([]);
|
|
||||||
expect(client).toHaveProperty("_events");
|
|
||||||
expect(client).toHaveProperty("_util");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('start', () => {
|
describe('Start', () => {
|
||||||
test("EXPECT bot to start", async () => {
|
test('Given Env Is Valid, Expect Successful Start', async () => {
|
||||||
process.env.BOT_TOKEN = "token";
|
process.env = {
|
||||||
console.error = jest.fn();
|
BOT_TOKEN: "TOKEN",
|
||||||
AppDataSource.initialize = jest.fn().mockResolvedValue(undefined);
|
};
|
||||||
|
|
||||||
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
const client = new CoreClient([1, 2, 3], [Partials.User]);
|
await coreClient.start();
|
||||||
|
|
||||||
const loadSlashCommands = jest.spyOn(Util.prototype, "loadSlashCommands").mockImplementation();
|
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
|
||||||
const loadEvents = jest.spyOn(Util.prototype, "loadEvents").mockImplementation();
|
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
|
||||||
|
|
||||||
Client.prototype.login = jest.fn();
|
|
||||||
Client.prototype.on = jest.fn();
|
|
||||||
|
|
||||||
await client.start();
|
|
||||||
|
|
||||||
expect(AppDataSource.initialize).toHaveBeenCalledTimes(1);
|
|
||||||
expect(console.error).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(client.login).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.login).toHaveBeenCalledWith("token");
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(2);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("interactionCreate", expect.any(Function));
|
|
||||||
expect(client.on).toHaveBeenCalledWith("ready", expect.any(Function))
|
|
||||||
|
|
||||||
expect(loadSlashCommands).toHaveBeenCalledTimes(1);
|
|
||||||
expect(loadSlashCommands).toHaveBeenCalledWith(client);
|
|
||||||
|
|
||||||
expect(loadEvents).toHaveBeenCalledTimes(1);
|
|
||||||
expect(loadEvents).toHaveBeenCalledWith(client, []);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Given BOT_TOKEN Is Null, Expect Failure', async () => {
|
||||||
|
process.env = {};
|
||||||
|
|
||||||
test("GIVEN BOT_TOKEN is not in env var, EXPECT error", async () => {
|
const consoleError = jest.fn();
|
||||||
console.error = jest.fn();
|
|
||||||
|
|
||||||
const client = new CoreClient([1, 2, 3], [ Partials.User ]);
|
console.error = consoleError;
|
||||||
|
|
||||||
|
const coreClient = new CoreClient();
|
||||||
|
|
||||||
await client.start();
|
await coreClient.start();
|
||||||
|
|
||||||
expect(console.error).toHaveBeenCalledWith("BOT_TOKEN is not defined in .env");
|
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
|
||||||
|
expect(coreClient.on).not.toBeCalled();
|
||||||
|
expect(coreClient.login).not.toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Given BOT_TOKEN Is Empty, Expect Failure', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: '',
|
||||||
|
}
|
||||||
|
|
||||||
|
const consoleError = jest.fn();
|
||||||
|
|
||||||
test("GIVEN database connection can not be initialised, EXPECT error", async () => {
|
console.error = consoleError;
|
||||||
process.env.BOT_TOKEN = "token";
|
|
||||||
console.log = jest.fn();
|
const coreClient = new CoreClient();
|
||||||
console.error = jest.fn();
|
|
||||||
AppDataSource.initialize = jest.fn().mockRejectedValue("error");
|
|
||||||
|
|
||||||
const client = new CoreClient([1, 2, 3], [Partials.User]);
|
await coreClient.start();
|
||||||
|
|
||||||
await client.start();
|
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
|
||||||
|
expect(coreClient.on).not.toBeCalled();
|
||||||
expect(console.log).not.toHaveBeenCalled();
|
expect(coreClient.login).not.toBeCalled();
|
||||||
expect(console.error).toHaveBeenCalledWith("Error Initialising Data Source", "error");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('RegisterCommand', () => {
|
describe('RegisterCommand', () => {
|
||||||
test("EXPECT command to be added to list", () => {
|
test('Expect command added to register', () => {
|
||||||
CoreClient.RegisterCommand("name", {} as Command, "serverId");
|
const cmd = mock<Command>();
|
||||||
|
|
||||||
|
const client = new CoreClient();
|
||||||
|
client.RegisterCommand("test", cmd);
|
||||||
|
|
||||||
expect(CoreClient.commandItems).toStrictEqual([
|
expect(client.commandItems.length).toBe(1);
|
||||||
{
|
expect(client.commandItems[0].Name).toBe("test");
|
||||||
Name: "name",
|
expect(client.commandItems[0].Command).toBe(cmd);
|
||||||
Command: {},
|
|
||||||
ServerId: "serverId",
|
|
||||||
}
|
|
||||||
])
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("RegisterEvent", () => {
|
describe('RegisterEvent', () => {
|
||||||
test("EXPECT event to be added to list", () => {
|
test('Expect event added to register', () => {
|
||||||
CoreClient.RegisterEvent(EventType.ChannelCreate, () => {});
|
const evt = mock<Event>();
|
||||||
|
|
||||||
|
const client = new CoreClient();
|
||||||
|
client.RegisterEvent(evt);
|
||||||
|
|
||||||
expect(CoreClient.eventItems).toStrictEqual([
|
expect(client.eventItems.length).toBe(1);
|
||||||
{
|
expect(client.eventItems[0].Event).toBe(evt);
|
||||||
EventType: EventType.ChannelCreate,
|
|
||||||
ExecutionFunction: expect.any(Function),
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("RegisterButtonEvent", () => {
|
|
||||||
test("EXPECT button event to be added to list", () => {
|
|
||||||
CoreClient.RegisterButtonEvent("buttonId", {} as ButtonEvent);
|
|
||||||
|
|
||||||
expect(CoreClient.buttonEvents).toStrictEqual([
|
|
||||||
{
|
|
||||||
ButtonId: "buttonId",
|
|
||||||
Event: {},
|
|
||||||
}
|
|
||||||
])
|
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -1,89 +1,241 @@
|
||||||
import { Interaction } from "discord.js";
|
|
||||||
import { Events } from "../../src/client/events";
|
import { Events } from "../../src/client/events";
|
||||||
import ChatInputCommand from "../../src/client/interactionCreate/chatInputCommand";
|
import { Message } from "discord.js";
|
||||||
import Button from "../../src/client/interactionCreate/button";
|
import { Util } from "../../src/client/util";
|
||||||
|
import ICommandItem from "../../src/contracts/ICommandItem";
|
||||||
|
import { Command } from "../../src/type/command";
|
||||||
|
import { mock } from "jest-mock-extended";
|
||||||
|
|
||||||
describe('onInteractionCreate', () => {
|
jest.mock("../../src/client/util");
|
||||||
test("GIVEN the interaction is a chat input command, EXPECT chat input command handler to be executed", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "123",
|
|
||||||
} as unknown as Interaction;
|
|
||||||
|
|
||||||
ChatInputCommand.onChatInput = jest.fn();
|
beforeEach(() => {
|
||||||
|
Util.prototype.loadCommand = jest.fn();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('OnMessage', () => {
|
||||||
|
test('Given Message Is Valid Expect Message Sent', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
};
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const cmd = mock<Command>();
|
||||||
|
|
||||||
|
const commandItem: ICommandItem = {
|
||||||
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
const events = new Events();
|
const events = new Events();
|
||||||
await events.onInteractionCreate(interaction);
|
|
||||||
|
const result = await events.onMessage(message, commands);
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
|
expect(result.valid).toBeTruthy();
|
||||||
expect(ChatInputCommand.onChatInput).toHaveBeenCalledTimes(1);
|
|
||||||
expect(ChatInputCommand.onChatInput).toHaveBeenCalledWith(interaction);
|
expect(result.context?.prefix).toBe('!');
|
||||||
|
expect(result.context?.name).toBe('test');
|
||||||
|
expect(result.context?.args.length).toBe(1);
|
||||||
|
expect(result.context?.args[0]).toBe('first');
|
||||||
|
expect(result.context?.message).toBe(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Given Guild Is Null, Expect Failed Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: null,
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
test("GIVEN the interaction is a button, EXPECT button handler to be executed", async () => {
|
const cmd = mock<Command>();
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
|
||||||
isButton: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "123",
|
|
||||||
} as unknown as Interaction;
|
|
||||||
|
|
||||||
Button.onButtonClicked = jest.fn();
|
const commandItem: ICommandItem = {
|
||||||
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
const events = new Events();
|
const events = new Events();
|
||||||
await events.onInteractionCreate(interaction);
|
|
||||||
|
const result = await events.onMessage(message, commands);
|
||||||
expect(interaction.isButton).toHaveBeenCalledTimes(1);
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
expect(Button.onButtonClicked).toHaveBeenCalledTimes(1);
|
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
|
||||||
expect(Button.onButtonClicked).toHaveBeenCalledWith(interaction);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Given Author Is A Bot, Expect Failed Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: true,
|
||||||
|
},
|
||||||
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
test("GIVEN the interaction is not a chat input command or button, EXPECT error", async () => {
|
const cmd = mock<Command>();
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
|
||||||
isButton: jest.fn().mockReturnValue(false),
|
|
||||||
guildId: "123",
|
|
||||||
} as unknown as Interaction;
|
|
||||||
|
|
||||||
console.error = jest.fn();
|
const commandItem: ICommandItem = {
|
||||||
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
const events = new Events();
|
const events = new Events();
|
||||||
await events.onInteractionCreate(interaction);
|
|
||||||
|
const result = await events.onMessage(message, commands);
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.isButton).toHaveBeenCalledTimes(1);
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
||||||
expect(console.error).toHaveBeenCalledWith("Received interaction unable to interact with, ignoring");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Given Message Content Was Not A Command, Expect Failed Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "This is a standard message",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
const cmd = mock<Command>();
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn(),
|
|
||||||
isButton: jest.fn(),
|
|
||||||
guildId: null,
|
|
||||||
} as unknown as Interaction;
|
|
||||||
|
|
||||||
ChatInputCommand.onChatInput = jest.fn();
|
const commandItem: ICommandItem = {
|
||||||
Button.onButtonClicked = jest.fn();
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
const events = new Events();
|
const events = new Events();
|
||||||
await events.onInteractionCreate(interaction);
|
|
||||||
|
const result = await events.onMessage(message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Message was not a command, ignoring.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Message Had No Command Name, Expect Failed Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "!",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
expect(interaction.isChatInputCommand).not.toHaveBeenCalled();
|
const cmd = mock<Command>();
|
||||||
expect(interaction.isButton).not.toHaveBeenCalled();
|
|
||||||
expect(ChatInputCommand.onChatInput).not.toHaveBeenCalled();
|
const commandItem: ICommandItem = {
|
||||||
expect(Button.onButtonClicked).not.toHaveBeenCalled();
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
|
const events = new Events();
|
||||||
|
|
||||||
|
const result = await events.onMessage(message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Command name was not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Command Failed To Execute, Expect Failed Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
},
|
||||||
|
content: "!test first",
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const cmd = mock<Command>();
|
||||||
|
|
||||||
|
const commandItem: ICommandItem = {
|
||||||
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
|
const events = new Events();
|
||||||
|
|
||||||
|
const result = await events.onMessage(message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Command failed");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("onReady", () => {
|
describe('OnReady', () => {
|
||||||
test("EXPECT console to be logged", () => {
|
test('Expect Console Log', () => {
|
||||||
console.log = jest.fn();
|
console.log = jest.fn();
|
||||||
|
|
||||||
const events = new Events();
|
const events = new Events();
|
||||||
|
|
||||||
events.onReady();
|
events.onReady();
|
||||||
|
|
||||||
expect(console.log).toHaveBeenCalledWith("Ready");
|
expect(console.log).toBeCalledWith("Ready");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,70 +0,0 @@
|
||||||
import Button from "../../../src/client/interactionCreate/button";
|
|
||||||
import { CoreClient } from "../../../src/client/client";
|
|
||||||
import ButtonEventItem from "../../../src/contracts/ButtonEventItem";
|
|
||||||
import { ButtonInteraction } from "discord.js";
|
|
||||||
import { ButtonEvent } from "../../../src/type/buttonEvent";
|
|
||||||
|
|
||||||
describe('onButtonClicked', () => {
|
|
||||||
let item: ButtonEventItem;
|
|
||||||
let interaction: ButtonInteraction;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
item = {
|
|
||||||
ButtonId: "buttonId",
|
|
||||||
Event: {
|
|
||||||
execute: jest.fn(),
|
|
||||||
} as unknown as ButtonEvent,
|
|
||||||
} as unknown as ButtonEventItem;
|
|
||||||
|
|
||||||
interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
isButton: true,
|
|
||||||
customId: "buttonId test",
|
|
||||||
} as unknown as ButtonInteraction;
|
|
||||||
|
|
||||||
CoreClient.buttonEvents = [ item ];
|
|
||||||
});
|
|
||||||
|
|
||||||
test("EXPECT button event to be executed", async () => {
|
|
||||||
await Button.onButtonClicked(interaction);
|
|
||||||
|
|
||||||
expect(item.Event.execute).toHaveBeenCalledTimes(1);
|
|
||||||
expect(item.Event.execute).toHaveBeenCalledWith(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction is not a button, EXEPCT nothing to happen", async () => {
|
|
||||||
interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
isButton: false,
|
|
||||||
} as unknown as ButtonInteraction;
|
|
||||||
|
|
||||||
await Button.onButtonClicked(interaction);
|
|
||||||
|
|
||||||
expect(item.Event.execute).not.toHaveBeenCalled();
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN no button event is registered, EXPECT error", async () => {
|
|
||||||
CoreClient.buttonEvents = [];
|
|
||||||
|
|
||||||
await Button.onButtonClicked(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Event not found.");
|
|
||||||
|
|
||||||
expect(item.Event.execute).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN button event is not registered, EXPECT error", async () => {
|
|
||||||
interaction.customId = "anotherButtonId test";
|
|
||||||
|
|
||||||
await Button.onButtonClicked(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Event not found.");
|
|
||||||
|
|
||||||
expect(item.Event.execute).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,168 +0,0 @@
|
||||||
import { ChatInputCommandInteraction } from 'discord.js';
|
|
||||||
import ChatInputComamnd from '../../../src/client/interactionCreate/chatInputCommand';
|
|
||||||
import { CoreClient } from '../../../src/client/client';
|
|
||||||
import ICommandItem from '../../../src/contracts/ICommandItem';
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
CoreClient.commandItems = [];
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('onChatInput', () => {
|
|
||||||
test("GIVEN command is registered globally AND command is found, execute the global command", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
commandName: "test",
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
const testCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const testServerCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
ServerId: "123",
|
|
||||||
};
|
|
||||||
|
|
||||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await ChatInputComamnd.onChatInput(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(testCommand.Command.execute).toHaveBeenCalledTimes(1);
|
|
||||||
expect(testCommand.Command.execute).toHaveBeenCalledWith(interaction);
|
|
||||||
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN command is registered to a single server, execute the server command", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
commandName: "test",
|
|
||||||
guildId: "123",
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
const testCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const testServerCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
ServerId: "123",
|
|
||||||
};
|
|
||||||
|
|
||||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await ChatInputComamnd.onChatInput(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(testServerCommand.Command.execute).toHaveBeenCalledTimes(1);
|
|
||||||
expect(testServerCommand.Command.execute).toHaveBeenCalledWith(interaction);
|
|
||||||
expect(testCommand.Command.execute).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
|
||||||
commandName: "test",
|
|
||||||
guildId: "123",
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
const testCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const testServerCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
ServerId: "123",
|
|
||||||
};
|
|
||||||
|
|
||||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await ChatInputComamnd.onChatInput(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
|
|
||||||
expect(testCommand.Command.execute).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN command is registered globally AND command is not found, EXPECT error", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
commandName: "other",
|
|
||||||
guildId: "123",
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
const testCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const testServerCommand: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: jest.fn(),
|
|
||||||
execute: jest.fn(),
|
|
||||||
},
|
|
||||||
ServerId: "123",
|
|
||||||
};
|
|
||||||
|
|
||||||
CoreClient.commandItems = [ testCommand, testServerCommand ];
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await ChatInputComamnd.onChatInput(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Command not found.");
|
|
||||||
|
|
||||||
expect(testServerCommand.Command.execute).not.toHaveBeenCalled();
|
|
||||||
expect(testCommand.Command.execute).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,333 +1,370 @@
|
||||||
import discordjs, { Client, REST, Routes, SlashCommandBuilder } from "discord.js";
|
|
||||||
import { Util } from "../../src/client/util";
|
import { Util } from "../../src/client/util";
|
||||||
import { CoreClient } from "../../src/client/client";
|
|
||||||
|
import { Client, Message } from "discord.js";
|
||||||
|
import fs from "fs";
|
||||||
|
import { mock } from "jest-mock-extended";
|
||||||
import { Command } from "../../src/type/command";
|
import { Command } from "../../src/type/command";
|
||||||
|
import ICommandItem from "../../src/contracts/ICommandItem";
|
||||||
import IEventItem from "../../src/contracts/IEventItem";
|
import IEventItem from "../../src/contracts/IEventItem";
|
||||||
import { EventType } from "../../src/constants/EventType";
|
import { Event } from "../../src/type/event";
|
||||||
|
|
||||||
|
jest.mock("fs");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.resetAllMocks();
|
fs.existsSync = jest.fn();
|
||||||
jest.resetModules();
|
|
||||||
|
|
||||||
process.env = {};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("loadSlashCommands", () => {
|
describe('LoadCommand', () => {
|
||||||
test("EXPECT slash commands to be loaded to the discord API", () => {
|
test('Given Successful Exection, Expect Successful Result', async () => {
|
||||||
process.env.BOT_TOKEN = "token";
|
process.env = {
|
||||||
process.env.BOT_CLIENTID = "clientid";
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(true),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
const client = {
|
const cmd = mock<Command>();
|
||||||
guilds: {
|
|
||||||
cache: {
|
|
||||||
has: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} as unknown as Client;
|
|
||||||
|
|
||||||
const setTokenPutMock = jest.fn();
|
const commandItem: ICommandItem = {
|
||||||
const setTokenMock = jest.fn().mockReturnValue({
|
Name: "test",
|
||||||
put: setTokenPutMock,
|
Command: cmd
|
||||||
});
|
};
|
||||||
|
|
||||||
REST.prototype.setToken = setTokenMock;
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
|
|
||||||
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeTruthy();
|
||||||
|
expect(cmd.execute).toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Member Is Null, Expect Failed Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: null
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
CoreClient.commandItems = [
|
const cmd = mock<Command>();
|
||||||
{
|
|
||||||
Name: "global",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: {
|
|
||||||
name: "global"
|
|
||||||
} as SlashCommandBuilder,
|
|
||||||
} as Command,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "server-specific",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: {
|
|
||||||
name: "server",
|
|
||||||
} as SlashCommandBuilder,
|
|
||||||
} as Command,
|
|
||||||
ServerId: "123",
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
const commandItem: ICommandItem = {
|
||||||
util.loadSlashCommands(client);
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
expect(setTokenMock).toHaveBeenCalledWith("token");
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Member is not part of message");
|
||||||
|
expect(cmd.execute).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given User Does Have Role, Expect Successful Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(true),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
expect(setTokenPutMock).toHaveBeenCalledTimes(2);
|
const cmd = mock<Command>();
|
||||||
expect(setTokenPutMock).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] });
|
|
||||||
expect(setTokenPutMock).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]});
|
|
||||||
|
|
||||||
expect(client.guilds.cache.has).toHaveBeenCalledTimes(1);
|
const commandItem: ICommandItem = {
|
||||||
expect(client.guilds.cache.has).toHaveBeenCalledWith("123");
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
expect(Routes.applicationCommands).toHaveBeenCalledTimes(1);
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
expect(Routes.applicationCommands).toHaveBeenCalledWith("clientid");
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeTruthy();
|
||||||
|
expect(cmd.execute).toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given User Does Not Have Role, Expect Failed Result', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(false),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
expect(Routes.applicationGuildCommands).toHaveBeenCalledTimes(1);
|
const cmd = mock<Command>();
|
||||||
expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123");
|
cmd._roles = [ "Moderator" ];
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored", () => {
|
const commandItem: ICommandItem = {
|
||||||
process.env.BOT_TOKEN = "token";
|
Name: "test",
|
||||||
process.env.BOT_CLIENTID = "clientid";
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
const client = {
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
guilds: {
|
|
||||||
cache: {
|
const util = new Util();
|
||||||
has: jest.fn().mockReturnValue(false),
|
|
||||||
}
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
}
|
|
||||||
} as unknown as Client;
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("You require the `Moderator` role to run this command");
|
||||||
|
expect(cmd.execute).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
const setTokenPutMock = jest.fn();
|
test('Given command is set to disabled, Expect command to not fire', async () => {
|
||||||
const setTokenMock = jest.fn().mockReturnValue({
|
process.env = {
|
||||||
put: setTokenPutMock,
|
BOT_TOKEN: 'TOKEN',
|
||||||
});
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
COMMANDS_DISABLED: 'test',
|
||||||
|
COMMANDS_DISABLED_MESSAGE: 'disabled',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(true),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
REST.prototype.setToken = setTokenMock;
|
const messageReply = jest.spyOn(message, 'reply');
|
||||||
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
|
|
||||||
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
|
|
||||||
|
|
||||||
CoreClient.commandItems = [
|
const cmd = mock<Command>();
|
||||||
{
|
|
||||||
Name: "server-specific",
|
|
||||||
Command: {
|
|
||||||
CommandBuilder: {
|
|
||||||
name: "server",
|
|
||||||
} as SlashCommandBuilder,
|
|
||||||
} as Command,
|
|
||||||
ServerId: "123",
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
const commandItem: ICommandItem = {
|
||||||
util.loadSlashCommands(client);
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
expect(Routes.applicationGuildCommands).not.toHaveBeenCalled();
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
});
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Command is disabled");
|
||||||
|
expect(messageReply).toBeCalledWith("disabled");
|
||||||
|
expect(cmd.execute).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
COMMANDS_DISABLED: 'test',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(true),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageReply = jest.spyOn(message, 'reply');
|
||||||
|
|
||||||
|
const cmd = mock<Command>();
|
||||||
|
|
||||||
|
const commandItem: ICommandItem = {
|
||||||
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem ];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe("Command is disabled");
|
||||||
|
expect(messageReply).toBeCalledWith("This command is disabled.");
|
||||||
|
expect(cmd.execute).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given a different command is disabled, Expect command to still fire', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
COMMANDS_DISABLED: 'other',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(true),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const cmd = mock<Command>();
|
||||||
|
const otherCmd = mock<Command>();
|
||||||
|
|
||||||
|
const commandItem: ICommandItem = {
|
||||||
|
Name: "test",
|
||||||
|
Command: cmd
|
||||||
|
};
|
||||||
|
|
||||||
|
const otherCommandItem: ICommandItem = {
|
||||||
|
Name: "other",
|
||||||
|
Command: otherCmd,
|
||||||
|
}
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [ commandItem, otherCommandItem ];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeTruthy();
|
||||||
|
expect(cmd.execute).toBeCalled();
|
||||||
|
expect(otherCmd.execute).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given command is not found in register, expect command not found error', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(true),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const commands: ICommandItem[] = [];
|
||||||
|
|
||||||
|
const util = new Util();
|
||||||
|
|
||||||
|
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
||||||
|
|
||||||
|
expect(result.valid).toBeFalsy();
|
||||||
|
expect(result.message).toBe('Command not found');
|
||||||
|
expect(message.reply).toBeCalledWith('Command not found');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("loadEvents", () => {
|
describe('LoadEvents', () => {
|
||||||
test("GIVEN event type is channelCreate, EXPECT event function to be executed", () => {
|
test('Given Events Are Loaded, Expect Successful Result', () => {
|
||||||
const client = {
|
process.env = {
|
||||||
on: jest.fn(),
|
BOT_TOKEN: 'TOKEN',
|
||||||
} as unknown as Client;
|
BOT_PREFIX: '!',
|
||||||
const events: IEventItem[] = [
|
FOLDERS_COMMANDS: 'commands',
|
||||||
{
|
FOLDERS_EVENTS: 'events',
|
||||||
EventType: EventType.ChannelCreate,
|
}
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
const client = {
|
||||||
];
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
|
||||||
const util = new Util();
|
const evt = mock<Event>();
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
const eventItem: IEventItem = {
|
||||||
expect(client.on).toHaveBeenCalledWith("channelCreate", expect.any(Function));
|
Event: evt
|
||||||
});
|
};
|
||||||
|
|
||||||
test("GIVEN event type is channelDelete, EXPECT event function to be executed", () => {
|
const eventItems: IEventItem[] = [ eventItem ];
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
const util = new Util();
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
const result = util.loadEvents(client, eventItems);
|
||||||
{
|
|
||||||
EventType: EventType.ChannelDelete,
|
const clientOn = jest.spyOn(client, 'on');
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
expect(result.valid).toBeTruthy();
|
||||||
];
|
expect(clientOn).toBeCalledTimes(13);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given No Events Found, Expect Successful Result', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_TOKEN: 'TOKEN',
|
||||||
|
BOT_PREFIX: '!',
|
||||||
|
FOLDERS_COMMANDS: 'commands',
|
||||||
|
FOLDERS_EVENTS: 'events',
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
|
||||||
const util = new Util();
|
const eventItems: IEventItem[] = [];
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
const util = new Util();
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("channelDelete", expect.any(Function));
|
const result = util.loadEvents(client, eventItems);
|
||||||
});
|
|
||||||
|
const clientOn = jest.spyOn(client, 'on');
|
||||||
test("GIVEN event type is channelUpdate, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
expect(result.valid).toBeTruthy();
|
||||||
on: jest.fn(),
|
expect(clientOn).toBeCalledTimes(0);
|
||||||
} as unknown as Client;
|
});
|
||||||
const events: IEventItem[] = [
|
});
|
||||||
{
|
|
||||||
EventType: EventType.ChannelUpdate,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("channelUpdate", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is guildBanAdd, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.GuildBanAdd,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("guildBanAdd", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is guildBanRemove, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.GuildBanRemove,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("guildBanRemove", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is guildCreate, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.GuildCreate,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("guildCreate", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is guildMemberAdd, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.GuildMemberAdd,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("guildMemberAdd", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is guildMemberRemove, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.GuildMemberRemove,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("guildMemberRemove", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is messageCreate, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.MessageCreate,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("messageCreate", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is messageDelete, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.MessageDelete,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("messageDelete", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is messageUpdate, EXPECT event function to be executed", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.MessageUpdate,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(client.on).toHaveBeenCalledTimes(1);
|
|
||||||
expect(client.on).toHaveBeenCalledWith("messageUpdate", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN event type is not implemented, EXPECT error", () => {
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
const events: IEventItem[] = [
|
|
||||||
{
|
|
||||||
EventType: EventType.Ready,
|
|
||||||
ExecutionFunction: jest.fn(),
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
console.error = jest.fn();
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
util.loadEvents(client, events);
|
|
||||||
|
|
||||||
expect(console.error).toHaveBeenCalledWith("Event not implemented.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
|
@ -1,391 +0,0 @@
|
||||||
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandChannelOption, SlashCommandNumberOption, SlashCommandRoleOption, SlashCommandStringOption } from "discord.js";
|
|
||||||
import Add from "../../../../src/commands/501231711271780357/Lobby/add";
|
|
||||||
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
|
|
||||||
|
|
||||||
describe('constuctor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const add = new Add();
|
|
||||||
|
|
||||||
expect(add.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandbuilder = add.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandbuilder.name).toBe("addlobby");
|
|
||||||
expect(commandbuilder.description).toBe("Add lobby channel");
|
|
||||||
expect(commandbuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
|
|
||||||
|
|
||||||
expect(commandbuilder.options.length).toBe(4);
|
|
||||||
|
|
||||||
const channelOption = commandbuilder.options[0] as SlashCommandChannelOption;
|
|
||||||
|
|
||||||
expect(channelOption.name).toBe("channel");
|
|
||||||
expect(channelOption.description).toBe("The channel");
|
|
||||||
expect(channelOption.required).toBe(true);
|
|
||||||
|
|
||||||
const roleOption = commandbuilder.options[1] as SlashCommandRoleOption;
|
|
||||||
|
|
||||||
expect(roleOption.name).toBe("role");
|
|
||||||
expect(roleOption.description).toBe("The role to ping on request");
|
|
||||||
expect(roleOption.required).toBe(true);
|
|
||||||
|
|
||||||
const cooldownOption = commandbuilder.options[2] as SlashCommandNumberOption;
|
|
||||||
|
|
||||||
expect(cooldownOption.name).toBe("cooldown");
|
|
||||||
expect(cooldownOption.description).toBe("The cooldown in minutes");
|
|
||||||
expect(cooldownOption.required).toBe(true);
|
|
||||||
|
|
||||||
const nameOption = commandbuilder.options[3] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(nameOption.name).toBe("name");
|
|
||||||
expect(nameOption.description).toBe("The game name");
|
|
||||||
expect(nameOption.required).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('execute', () => {
|
|
||||||
test("EXPECT channel to be added to the lobby database table", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {
|
|
||||||
id: "channelId",
|
|
||||||
},
|
|
||||||
name: "channelName",
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockReturnValue(null);
|
|
||||||
Lobby.prototype.Save = jest.fn();
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(4);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("channel");
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("role");
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("cooldown");
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("name");
|
|
||||||
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
|
|
||||||
|
|
||||||
expect(Lobby.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Added `channelName` as a new lobby channel with a cooldown of `5 minutes` and will ping `roleName` on use");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel is null, EXPECT error", async () => {
|
|
||||||
const channel = null;
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel.channel is undefined, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: undefined,
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN role is null, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {},
|
|
||||||
};
|
|
||||||
const role = null;
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN role.role is undefined, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {},
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: undefined
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN cooldown is null, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {},
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = null;
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN cooldown.value is undefined, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {},
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: undefined,
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN gameName is null, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {},
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = null;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN gameName.value is undefined, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {},
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel has already been set up in the database, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {
|
|
||||||
id: "channelId",
|
|
||||||
},
|
|
||||||
name: "channelName",
|
|
||||||
};
|
|
||||||
const role = {
|
|
||||||
role: {},
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
const cooldown = {
|
|
||||||
value: "5",
|
|
||||||
};
|
|
||||||
const gameName = {
|
|
||||||
value: "test",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(channel)
|
|
||||||
.mockReturnValueOnce(role)
|
|
||||||
.mockReturnValueOnce(cooldown)
|
|
||||||
.mockReturnValueOnce(gameName),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockReturnValue({});
|
|
||||||
Lobby.prototype.Save = jest.fn();
|
|
||||||
|
|
||||||
const add = new Add();
|
|
||||||
await add.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("This channel has already been setup.");
|
|
||||||
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Lobby.prototype.Save).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,82 +0,0 @@
|
||||||
import { APIEmbedField, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
|
|
||||||
import List from "../../../../src/commands/501231711271780357/Lobby/list";
|
|
||||||
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
|
|
||||||
import EmbedColours from "../../../../src/constants/EmbedColours";
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const list = new List();
|
|
||||||
|
|
||||||
expect(list.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBulder = list.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBulder.name).toBe("listlobby");
|
|
||||||
expect(commandBulder.description).toBe("Lists all channels set up as lobbies");
|
|
||||||
expect(commandBulder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("execute", () => {
|
|
||||||
test('EXPECT list of lobby channels to be sent', async () => {
|
|
||||||
let repliedWith;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
map: jest.fn().mockReturnValue([{
|
|
||||||
id: "channelId",
|
|
||||||
}]),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
reply: jest.fn().mockImplementation((options) => {
|
|
||||||
repliedWith = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockReturnValue({
|
|
||||||
Name: "lobbyName",
|
|
||||||
LastUsed: "lastUsed",
|
|
||||||
});
|
|
||||||
|
|
||||||
const list = new List();
|
|
||||||
await list.execute(interaction);
|
|
||||||
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(repliedWith).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const repliedEmbed: EmbedBuilder = repliedWith!.embeds[0];
|
|
||||||
|
|
||||||
expect(repliedEmbed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(repliedEmbed.data.title).toBe("Lobbies");
|
|
||||||
expect(repliedEmbed.data.description).toBe("Channels: 1");
|
|
||||||
expect(repliedEmbed.data.fields).toBeDefined();
|
|
||||||
expect(repliedEmbed.data.fields!.length).toBe(1);
|
|
||||||
|
|
||||||
const repliedEmbedField1: APIEmbedField = repliedEmbed.data.fields![0];
|
|
||||||
|
|
||||||
expect(repliedEmbedField1.name).toBe("# lobbyName");
|
|
||||||
expect(repliedEmbedField1.value).toBe("Last Used: lastUsed");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guild is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guild: null,
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const list = new List();
|
|
||||||
await list.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Guild not found.");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,112 +0,0 @@
|
||||||
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
|
|
||||||
import Command from "../../../../src/commands/501231711271780357/Lobby/lobby";
|
|
||||||
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const lobby = new Command();
|
|
||||||
|
|
||||||
expect(lobby.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = lobby.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("lobby");
|
|
||||||
expect(commandBuilder.description).toBe("Attempt to organise a lobby");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("execute", () => {
|
|
||||||
test("EXPECT lobby command to announce a lobby setup", async () => {
|
|
||||||
const interaction = {
|
|
||||||
user: "user",
|
|
||||||
channelId: "channelId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const markAsUsed = jest.fn();
|
|
||||||
const lobbySave = jest.fn();
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue({
|
|
||||||
Cooldown: 5,
|
|
||||||
LastUsed: new Date(2 * 60 * 1000),
|
|
||||||
Name: "gameName",
|
|
||||||
RoleId: "roleId",
|
|
||||||
MarkAsUsed: markAsUsed,
|
|
||||||
Save: lobbySave,
|
|
||||||
});
|
|
||||||
|
|
||||||
Date.now = jest.fn().mockReturnValue(10 * 60 * 1000);
|
|
||||||
|
|
||||||
const lobby = new Command();
|
|
||||||
await lobby.execute(interaction);
|
|
||||||
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
|
|
||||||
|
|
||||||
expect(Date.now).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(markAsUsed).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(lobbySave).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("user would like to organise a lobby of **gameName**! <@&roleId>");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.channelId is null, EXPECT nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
channelId: null,
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const lobby = new Command();
|
|
||||||
await lobby.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel is not setup in the database as a lobby, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
channelId: "channelId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue(null);
|
|
||||||
|
|
||||||
const lobby = new Command();
|
|
||||||
await lobby.execute(interaction);
|
|
||||||
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("This channel is disabled from using the lobby command.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN lobby command was last used within the cooldown, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
user: "user",
|
|
||||||
channelId: "channelId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const markAsUsed = jest.fn();
|
|
||||||
const lobbySave = jest.fn();
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue({
|
|
||||||
Cooldown: 5,
|
|
||||||
LastUsed: new Date(2 * 60 * 1000),
|
|
||||||
Name: "gameName",
|
|
||||||
RoleId: "roleId",
|
|
||||||
MarkAsUsed: markAsUsed,
|
|
||||||
Save: lobbySave,
|
|
||||||
});
|
|
||||||
|
|
||||||
Date.now = jest.fn().mockReturnValue(5 * 60 * 1000);
|
|
||||||
|
|
||||||
const lobby = new Command();
|
|
||||||
await lobby.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Requesting a lobby for this game is on cooldown! Please try again in **2 minutes**.");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,116 +0,0 @@
|
||||||
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandChannelOption } from "discord.js";
|
|
||||||
import Remove from "../../../../src/commands/501231711271780357/Lobby/remove";
|
|
||||||
import Lobby from "../../../../src/database/entities/501231711271780357/Lobby";
|
|
||||||
import BaseEntity from "../../../../src/contracts/BaseEntity";
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const remove = new Remove();
|
|
||||||
|
|
||||||
expect(remove.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = remove.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("removelobby");
|
|
||||||
expect(commandBuilder.description).toBe("Remove a lobby channel");
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
|
|
||||||
expect(commandBuilder.options.length).toBe(1);
|
|
||||||
|
|
||||||
const channelOption = commandBuilder.options[0] as SlashCommandChannelOption;
|
|
||||||
|
|
||||||
expect(channelOption.name).toBe("channel");
|
|
||||||
expect(channelOption.description).toBe("The channel");
|
|
||||||
expect(channelOption.required).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("execute", () => {
|
|
||||||
test("EXPECT channel to be removed from database", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {
|
|
||||||
id: "channelId",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(channel),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue({});
|
|
||||||
BaseEntity.Remove = jest.fn();
|
|
||||||
|
|
||||||
const remove = new Remove();
|
|
||||||
await remove.execute(interaction);
|
|
||||||
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Lobby.FetchOneByChannelId).toHaveBeenCalledWith("channelId");
|
|
||||||
|
|
||||||
expect(BaseEntity.Remove).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("channel");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Removed <#channelId> from the list of lobby channels");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(null),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const remove = new Remove();
|
|
||||||
await remove.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Channel is required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel.channel is undefined, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(channel),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const remove = new Remove();
|
|
||||||
await remove.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Channel is required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel is not set up as a lobby, EXPECT error", async () => {
|
|
||||||
const channel = {
|
|
||||||
channel: {
|
|
||||||
id: "channelId",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(channel),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Lobby.FetchOneByChannelId = jest.fn().mockResolvedValue(null);
|
|
||||||
|
|
||||||
const remove = new Remove();
|
|
||||||
await remove.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Channel not found.");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,111 +0,0 @@
|
||||||
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
|
|
||||||
import Entry from "../../../src/commands/501231711271780357/entry";
|
|
||||||
import SettingsHelper from "../../../src/helpers/SettingsHelper";
|
|
||||||
import EmbedColours from "../../../src/constants/EmbedColours";
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const entry = new Entry();
|
|
||||||
|
|
||||||
expect(entry.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = entry.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("entry");
|
|
||||||
expect(commandBuilder.description).toBe("Sends the entry embed");
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("execute", () => {
|
|
||||||
test("EXPECT entry embed to be sent", async () => {
|
|
||||||
let sentWith;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
channel: {
|
|
||||||
send: jest.fn().mockImplementation((options) => {
|
|
||||||
sentWith = options;
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("channelId");
|
|
||||||
|
|
||||||
const entry = new Entry();
|
|
||||||
await entry.execute(interaction);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("channels.rules", "guildId");
|
|
||||||
|
|
||||||
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
|
|
||||||
expect(sentWith).toBeDefined();
|
|
||||||
expect(sentWith!.embeds).toBeDefined();
|
|
||||||
expect(sentWith!.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embed = sentWith!.embeds[0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(embed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(embed.data.title).toBe("Welcome");
|
|
||||||
expect(embed.data.description).toBe("Welcome to the server! Please make sure to read the rules in the <#channelId> channel and type the code found there in here to proceed to the main part of the server.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: null,
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn();
|
|
||||||
|
|
||||||
const entry = new Entry();
|
|
||||||
await entry.execute(interaction);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.channel is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
channel: null,
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn();
|
|
||||||
|
|
||||||
const entry = new Entry();
|
|
||||||
await entry.execute(interaction);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channels.rules setting is not set, EXPECT channel id to be defaulted", async () => {
|
|
||||||
let sentWith;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
channel: {
|
|
||||||
send: jest.fn().mockImplementation((options) => {
|
|
||||||
sentWith = options;
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
|
|
||||||
|
|
||||||
const entry = new Entry();
|
|
||||||
await entry.execute(interaction);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("channels.rules", "guildId");
|
|
||||||
|
|
||||||
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
|
|
||||||
expect(sentWith).toBeDefined();
|
|
||||||
expect(sentWith!.embeds).toBeDefined();
|
|
||||||
expect(sentWith!.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embed = sentWith!.embeds[0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(embed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(embed.data.title).toBe("Welcome");
|
|
||||||
expect(embed.data.description).toBe("Welcome to the server! Please make sure to read the rules in the <#rules> channel and type the code found there in here to proceed to the main part of the server.");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,227 +0,0 @@
|
||||||
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandRoleOption } from "discord.js";
|
|
||||||
import Config from "../../../src/commands/Role/config";
|
|
||||||
import Role from "../../../src/database/entities/Role";
|
|
||||||
import Server from "../../../src/database/entities/Server";
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test('EXPECT properties to be set', () => {
|
|
||||||
const config = new Config();
|
|
||||||
|
|
||||||
expect(config.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = config.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("configrole");
|
|
||||||
expect(commandBuilder.description).toBe("Toggle your roles");
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ManageRoles.toString());
|
|
||||||
expect(commandBuilder.options.length).toBe(1);
|
|
||||||
|
|
||||||
const roleOption = commandBuilder.options[0] as SlashCommandRoleOption;
|
|
||||||
|
|
||||||
expect(roleOption.name).toBe("role");
|
|
||||||
expect(roleOption.description).toBe("The role name");
|
|
||||||
expect(roleOption.required).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("execute", () => {
|
|
||||||
test("GIVEN role is marked as assignable, EXPECT role to be removed", async () => {
|
|
||||||
const role = {
|
|
||||||
role: {
|
|
||||||
id: "roleId",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {},
|
|
||||||
member: {},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(role),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchOneByRoleId = jest.fn().mockResolvedValue({});
|
|
||||||
Role.Remove = jest.fn();
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(Role.FetchOneByRoleId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Role.FetchOneByRoleId).toHaveBeenCalledWith("roleId");
|
|
||||||
|
|
||||||
expect(Role.Remove).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("role");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Removed role from configuration.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN role is not marked as assignable, EXPECT role to be added", async () => {
|
|
||||||
let newRole: Role | undefined;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
role: {
|
|
||||||
id: "roleId",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {},
|
|
||||||
member: {},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(role),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchOneByRoleId = jest.fn().mockResolvedValue(null);
|
|
||||||
Role.prototype.SetServer = jest.fn();
|
|
||||||
Role.prototype.Save = jest.fn().mockImplementation((_, role) => {
|
|
||||||
newRole = role;
|
|
||||||
});
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({});
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(Role.FetchOneByRoleId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Role.FetchOneByRoleId).toHaveBeenCalledWith("roleId");
|
|
||||||
|
|
||||||
expect(Role.prototype.SetServer).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(Role.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
expect(newRole).toBeDefined();
|
|
||||||
expect(newRole!.RoleId).toBe("roleId");
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("role");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Added role to configuration.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: null,
|
|
||||||
options: {
|
|
||||||
get: jest.fn(),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.options.get).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guild is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: null,
|
|
||||||
options: {
|
|
||||||
get: jest.fn(),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.options.get).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.member is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {},
|
|
||||||
member: null,
|
|
||||||
options: {
|
|
||||||
get: jest.fn(),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.options.get).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN role is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {},
|
|
||||||
member: {},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(null),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN role.role is undefined, EXPECT error", async () => {
|
|
||||||
const role = {
|
|
||||||
role: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {},
|
|
||||||
member: {},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(role),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN server is not configured in the database, EXPECT error", async () => {
|
|
||||||
let newRole: Role | undefined;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
role: {
|
|
||||||
id: "roleId",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {},
|
|
||||||
member: {},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(role),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchOneByRoleId = jest.fn().mockResolvedValue(null);
|
|
||||||
Role.prototype.SetServer = jest.fn();
|
|
||||||
Role.prototype.Save = jest.fn().mockImplementation((_, role) => {
|
|
||||||
newRole = role;
|
|
||||||
});
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue(null);
|
|
||||||
|
|
||||||
const config = new Config();
|
|
||||||
await config.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("This server has not been setup.");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,475 +0,0 @@
|
||||||
import { CommandInteraction, GuildMemberRoleManager, SlashCommandBuilder, SlashCommandRoleOption, SlashCommandSubcommandBuilder } from "discord.js";
|
|
||||||
import Command from "../../../src/commands/Role/role";
|
|
||||||
import Role from "../../../src/database/entities/Role";
|
|
||||||
import { EmbedBuilder } from "@discordjs/builders";
|
|
||||||
import EmbedColours from "../../../src/constants/EmbedColours";
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('EXPECT properties are set', () => {
|
|
||||||
const command = new Command();
|
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("role");
|
|
||||||
expect(commandBuilder.description).toBe("Toggle your roles");
|
|
||||||
expect(commandBuilder.options.length).toBe(2);
|
|
||||||
|
|
||||||
const toggleSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(toggleSubcommand.name).toBe("toggle");
|
|
||||||
expect(toggleSubcommand.description).toBe("Toggle your role");
|
|
||||||
expect(toggleSubcommand.options.length).toBe(1);
|
|
||||||
|
|
||||||
const toggleRoleOption = toggleSubcommand.options[0] as SlashCommandRoleOption;
|
|
||||||
|
|
||||||
expect(toggleRoleOption.name).toBe("role");
|
|
||||||
expect(toggleRoleOption.description).toBe("The role name");
|
|
||||||
expect(toggleRoleOption.required).toBe(true);
|
|
||||||
|
|
||||||
const listSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(listSubcommand.name).toBe("list");
|
|
||||||
expect(listSubcommand.description).toBe("List togglable roles");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN an invalid subcommand is given, EXPECT not found error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("other"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Subcommand not found.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("toggle", () => {
|
|
||||||
test("GIVEN user has the role, EXPECT role to be removed", async () => {
|
|
||||||
const role = {
|
|
||||||
id: "roleId",
|
|
||||||
name: "roleName",
|
|
||||||
editable: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const requestedRole = {
|
|
||||||
role: {
|
|
||||||
name: "roleName",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const userRole = {};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(role),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(userRole),
|
|
||||||
},
|
|
||||||
remove: jest.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(requestedRole),
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("toggle"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(Role.FetchAllByServerId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Role.FetchAllByServerId).toHaveBeenCalledWith("guildId");
|
|
||||||
|
|
||||||
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(2);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("role");
|
|
||||||
|
|
||||||
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
|
|
||||||
|
|
||||||
expect(roleManager.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
expect(roleManager.remove).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Removed role: `roleName`");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN user does not have the role, EXPECT role to be added", async () => {
|
|
||||||
const role = {
|
|
||||||
id: "roleId",
|
|
||||||
name: "roleName",
|
|
||||||
editable: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const requestedRole = {
|
|
||||||
role: {
|
|
||||||
name: "roleName",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(role),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(undefined),
|
|
||||||
},
|
|
||||||
add: jest.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(requestedRole),
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("toggle"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(Role.FetchAllByServerId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Role.FetchAllByServerId).toHaveBeenCalledWith("guildId");
|
|
||||||
|
|
||||||
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(2);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("role");
|
|
||||||
|
|
||||||
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
|
|
||||||
|
|
||||||
expect(roleManager.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
expect(roleManager.add).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Gave role: `roleName`");
|
|
||||||
})
|
|
||||||
|
|
||||||
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
|
|
||||||
const role = {
|
|
||||||
id: "roleId",
|
|
||||||
name: "roleName",
|
|
||||||
editable: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const requestedRole = {
|
|
||||||
role: {
|
|
||||||
name: "roleName",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const userRole = {};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: null,
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(userRole),
|
|
||||||
},
|
|
||||||
add: jest.fn(),
|
|
||||||
remove: jest.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(requestedRole),
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("toggle"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
|
|
||||||
|
|
||||||
expect(roleManager.add).not.toHaveBeenCalled();
|
|
||||||
expect(roleManager.remove).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.member is null, EXPECT nothing to happen", async () => {
|
|
||||||
const role = {
|
|
||||||
id: "roleId",
|
|
||||||
name: "roleName",
|
|
||||||
editable: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const requestedRole = {
|
|
||||||
role: {
|
|
||||||
name: "roleName",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const userRole = {};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: null,
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(userRole),
|
|
||||||
},
|
|
||||||
add: jest.fn(),
|
|
||||||
remove: jest.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(requestedRole),
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("toggle"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
const roleManager = interaction.member!.roles as GuildMemberRoleManager;
|
|
||||||
|
|
||||||
expect(roleManager.add).not.toHaveBeenCalled();
|
|
||||||
expect(roleManager.remove).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN requestedRole is null, EXPECT invalid error", async () => {
|
|
||||||
const role = {
|
|
||||||
id: "roleId",
|
|
||||||
name: "roleName",
|
|
||||||
editable: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const userRole = {};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(role),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(userRole),
|
|
||||||
},
|
|
||||||
remove: jest.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(null),
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("toggle"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN requestedRole.role is undefined, EXPECT invalid error", async () => {
|
|
||||||
const role = {
|
|
||||||
id: "roleId",
|
|
||||||
name: "roleName",
|
|
||||||
editable: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const userRole = {};
|
|
||||||
|
|
||||||
const requestedRole = {
|
|
||||||
role: undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(role),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(userRole),
|
|
||||||
},
|
|
||||||
remove: jest.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(requestedRole),
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("toggle"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN assignRole is not editable, EXPECT insufficient permissions error", async () => {
|
|
||||||
const role = {
|
|
||||||
id: "roleId",
|
|
||||||
name: "roleName",
|
|
||||||
editable: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
const userRole = {};
|
|
||||||
|
|
||||||
const requestedRole = {
|
|
||||||
role: {
|
|
||||||
name: "roleName",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(role),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(userRole),
|
|
||||||
},
|
|
||||||
remove: jest.fn(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(requestedRole),
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("toggle"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([{}]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Insufficient permissions. Please contact a moderator.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("list", () => {
|
|
||||||
test("EXPECT role list to be sent", async () => {
|
|
||||||
let repliedWith;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: "roleName",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
guild: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(role),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("list"),
|
|
||||||
},
|
|
||||||
reply: jest.fn().mockImplementation((options) => {
|
|
||||||
repliedWith = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Role.FetchAllByServerId = jest.fn().mockResolvedValue([role]);
|
|
||||||
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(Role.FetchAllByServerId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Role.FetchAllByServerId).toHaveBeenCalledWith("guildId");
|
|
||||||
|
|
||||||
expect(interaction.guild?.roles.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(repliedWith).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embed = repliedWith!.embeds[0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(embed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(embed.data.title).toBe("Roles");
|
|
||||||
expect(embed.data.description).toBe("Roles: 1\n\nroleName");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,32 +0,0 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
||||||
|
|
||||||
exports[`Constructor EXPECT properties to be set 1`] = `
|
|
||||||
{
|
|
||||||
"default_member_permissions": "8",
|
|
||||||
"default_permission": undefined,
|
|
||||||
"description": "Rules-related commands",
|
|
||||||
"description_localizations": undefined,
|
|
||||||
"dm_permission": undefined,
|
|
||||||
"name": "rules",
|
|
||||||
"name_localizations": undefined,
|
|
||||||
"nsfw": undefined,
|
|
||||||
"options": [
|
|
||||||
{
|
|
||||||
"description": "Send the rules embeds for this server",
|
|
||||||
"description_localizations": undefined,
|
|
||||||
"name": "embeds",
|
|
||||||
"name_localizations": undefined,
|
|
||||||
"options": [],
|
|
||||||
"type": 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"description": "Send the server verification embed button",
|
|
||||||
"description_localizations": undefined,
|
|
||||||
"name": "access",
|
|
||||||
"name_localizations": undefined,
|
|
||||||
"options": [],
|
|
||||||
"type": 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
`;
|
|
|
@ -1,38 +0,0 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
||||||
|
|
||||||
exports[`Execute EXPECT user to be warned: savedAudit 1`] = `
|
|
||||||
{
|
|
||||||
"AuditId": Any<String>,
|
|
||||||
"AuditType": 1,
|
|
||||||
"Id": Any<String>,
|
|
||||||
"ModeratorId": "moderatorId",
|
|
||||||
"Reason": "Test reason",
|
|
||||||
"ServerId": "guildId",
|
|
||||||
"UserId": "userId",
|
|
||||||
"WhenCreated": Any<Date>,
|
|
||||||
"WhenUpdated": Any<Date>,
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`Execute EXPECT user to be warned: sentEmbeds 1`] = `
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"color": 3166394,
|
|
||||||
"description": "<@userId> \`userTag\`",
|
|
||||||
"fields": [
|
|
||||||
{
|
|
||||||
"name": "Moderator",
|
|
||||||
"value": "<@moderatorId>",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Reason",
|
|
||||||
"value": "Test reason",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"thumbnail": {
|
|
||||||
"url": "https://google.com/avatar.png",
|
|
||||||
},
|
|
||||||
"title": "Member Warned",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
`;
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder, InteractionReplyOptions,SlashCommandBuilder } from "discord.js";
|
import { Message } from "discord.js";
|
||||||
|
import { mock } from "jest-mock-extended";
|
||||||
import About from "../../src/commands/about";
|
import About from "../../src/commands/about";
|
||||||
import EmbedColours from "../../src/constants/EmbedColours";
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
|
import PublicEmbed from "../../src/helpers/embeds/PublicEmbed";
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env = {};
|
process.env = {};
|
||||||
|
@ -8,169 +10,143 @@ beforeEach(() => {
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test('Expect values set', () => {
|
test('Expect values set', () => {
|
||||||
const command = new About();
|
const about = new About();
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
expect(about._category).toBe("General");
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("about");
|
|
||||||
expect(commandBuilder.description).toBe("About VylBot");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Execute', () => {
|
describe('Execute', () => {
|
||||||
test('GIVEN ABOUT_FUNDING and ABOUT_REPO are NOT present in env var, EXPECT embed to be sent without buttons', async () => {
|
test('Expect embed to be made and sent to the current channel', async () => {
|
||||||
// Setup
|
process.env = {
|
||||||
let replyOptions: InteractionReplyOptions | undefined;
|
BOT_VER: "BOT_VER",
|
||||||
|
BOT_AUTHOR: "BOT_AUTHOR",
|
||||||
|
BOT_DATE: "BOT_DATE"
|
||||||
|
};
|
||||||
|
|
||||||
const interaction = {
|
const message = mock<Message>();
|
||||||
reply: jest.fn((options: InteractionReplyOptions) => {
|
message.channel.send = jest.fn();
|
||||||
replyOptions = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
// Execution
|
const context: ICommandContext = {
|
||||||
process.env.BOT_VER = "VERSION";
|
name: "about",
|
||||||
process.env.BOT_AUTHOR = "AUTHOR";
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
const command = new About();
|
const about = new About();
|
||||||
|
|
||||||
await command.execute(interaction);
|
const result = await about.execute(context);
|
||||||
|
|
||||||
// Assertion
|
expect(message.channel.send).toBeCalledTimes(1);
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(replyOptions).toBeDefined();
|
|
||||||
|
|
||||||
expect(replyOptions?.embeds).toBeDefined();
|
|
||||||
expect(replyOptions?.embeds?.length).toBe(1);
|
|
||||||
|
|
||||||
expect(replyOptions?.components).toBeDefined();
|
|
||||||
expect(replyOptions?.components?.length).toBe(0);
|
|
||||||
|
|
||||||
const repliedEmbed = replyOptions?.embeds![0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(repliedEmbed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(repliedEmbed.data.title).toBe("About");
|
|
||||||
expect(repliedEmbed.data.description).toBe("Discord Bot made by Vylpes");
|
|
||||||
expect(repliedEmbed.data.fields?.length).toBe(2);
|
|
||||||
|
|
||||||
const repliedEmbedVersionField = repliedEmbed.data.fields![0];
|
|
||||||
|
|
||||||
expect(repliedEmbedVersionField.name).toBe("Version");
|
|
||||||
expect(repliedEmbedVersionField.value).toBe("VERSION");
|
|
||||||
expect(repliedEmbedVersionField.inline).toBeTruthy();
|
|
||||||
|
|
||||||
const repliedEmbedAuthorField = repliedEmbed.data.fields![1];
|
|
||||||
|
|
||||||
expect(repliedEmbedAuthorField.name).toBe("Author");
|
|
||||||
expect(repliedEmbedAuthorField.value).toBe("AUTHOR");
|
|
||||||
expect(repliedEmbedAuthorField.inline).toBeTruthy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('GIVEN ABOUT_FUNDING is present in env var, EXPECT funding button to be sent', async () => {
|
test('Expect embed send to have values', async () => {
|
||||||
// Setup
|
process.env = {
|
||||||
let replyOptions: InteractionReplyOptions | undefined;
|
BOT_VER: "BOT_VER",
|
||||||
|
BOT_AUTHOR: "BOT_AUTHOR",
|
||||||
|
BOT_DATE: "BOT_DATE"
|
||||||
|
};
|
||||||
|
|
||||||
const interaction = {
|
const message = mock<Message>();
|
||||||
reply: jest.fn((options: InteractionReplyOptions) => {
|
message.channel.send = jest.fn();
|
||||||
replyOptions = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
// Execution
|
const context: ICommandContext = {
|
||||||
process.env.BOT_VER = "VERSION";
|
name: "about",
|
||||||
process.env.BOT_AUTHOR = "AUTHOR";
|
args: [],
|
||||||
process.env.ABOUT_FUNDING = "https://ko-fi.com/vylpes";
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
const command = new About();
|
const about = new About();
|
||||||
|
|
||||||
await command.execute(interaction);
|
const result = await about.execute(context);
|
||||||
|
|
||||||
// Assertion
|
expect(result.embeds.length).toBe(1);
|
||||||
expect(replyOptions?.components?.length).toBe(1);
|
|
||||||
|
|
||||||
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
expect(repliedRow.components?.length).toBe(1);
|
expect(embed.title).toBe('About');
|
||||||
|
expect(embed.description).toBe('');
|
||||||
const repliedRowFundingComponent = repliedRow.components![0] as any;
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
expect(repliedRowFundingComponent.data.url).toBe("https://ko-fi.com/vylpes");
|
|
||||||
expect(repliedRowFundingComponent.data.label).toBe("Funding");
|
|
||||||
expect(repliedRowFundingComponent.data.style).toBe(ButtonStyle.Link);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('GIVEN ABOUT_REPO is present in env var, EXPECT funding button to be sent', async () => {
|
test('Expect version field to have values', async () => {
|
||||||
// Setup
|
process.env = {
|
||||||
let replyOptions: InteractionReplyOptions | undefined;
|
BOT_VER: "BOT_VER",
|
||||||
|
BOT_AUTHOR: "BOT_AUTHOR",
|
||||||
|
BOT_DATE: "BOT_DATE"
|
||||||
|
};
|
||||||
|
|
||||||
const interaction = {
|
const message = mock<Message>();
|
||||||
reply: jest.fn((options: InteractionReplyOptions) => {
|
message.channel.send = jest.fn();
|
||||||
replyOptions = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
// Execution
|
const context: ICommandContext = {
|
||||||
process.env.BOT_VER = "VERSION";
|
name: "about",
|
||||||
process.env.BOT_AUTHOR = "AUTHOR";
|
args: [],
|
||||||
process.env.ABOUT_REPO = "https://gitea.vylpes.xyz/rabbitlabs/vylbot-app";
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
const command = new About();
|
const about = new About();
|
||||||
|
|
||||||
await command.execute(interaction);
|
const result = await about.execute(context);
|
||||||
|
|
||||||
// Assertion
|
const embed = result.embeds[0];
|
||||||
expect(replyOptions?.components?.length).toBe(1);
|
const field = embed.fields[0];
|
||||||
|
|
||||||
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
|
expect(field.name).toBe('Version');
|
||||||
|
expect(field.value).toBe('BOT_VER');
|
||||||
expect(repliedRow.components?.length).toBe(1);
|
|
||||||
|
|
||||||
const repliedRowRepoComponent = repliedRow.components![0] as any;
|
|
||||||
|
|
||||||
expect(repliedRowRepoComponent.data.url).toBe("https://gitea.vylpes.xyz/rabbitlabs/vylbot-app");
|
|
||||||
expect(repliedRowRepoComponent.data.label).toBe("Repo");
|
|
||||||
expect(repliedRowRepoComponent.data.style).toBe(ButtonStyle.Link);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('GIVEN ABOUT_REPO AND ABOUT_FUNDING is present in env var, EXPECT funding button to be sent', async () => {
|
test('Expect author field to have values', async () => {
|
||||||
// Setup
|
process.env = {
|
||||||
let replyOptions: InteractionReplyOptions | undefined;
|
BOT_VER: "BOT_VER",
|
||||||
|
BOT_AUTHOR: "BOT_AUTHOR",
|
||||||
|
BOT_DATE: "BOT_DATE"
|
||||||
|
};
|
||||||
|
|
||||||
const interaction = {
|
const message = mock<Message>();
|
||||||
reply: jest.fn((options: InteractionReplyOptions) => {
|
message.channel.send = jest.fn();
|
||||||
replyOptions = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
// Execution
|
const context: ICommandContext = {
|
||||||
process.env.BOT_VER = "VERSION";
|
name: "about",
|
||||||
process.env.BOT_AUTHOR = "AUTHOR";
|
args: [],
|
||||||
process.env.ABOUT_REPO = "https://gitea.vylpes.xyz/rabbitlabs/vylbot-app";
|
message: message
|
||||||
process.env.ABOUT_FUNDING = "https://ko-fi.com/vylpes";
|
};
|
||||||
|
|
||||||
const command = new About();
|
const about = new About();
|
||||||
|
|
||||||
await command.execute(interaction);
|
const result = await about.execute(context);
|
||||||
|
|
||||||
// Assertion
|
const embed = result.embeds[0];
|
||||||
expect(replyOptions?.components?.length).toBe(1);
|
const field = embed.fields[1];
|
||||||
|
|
||||||
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
|
expect(field.name).toBe('Author');
|
||||||
|
expect(field.value).toBe('BOT_AUTHOR');
|
||||||
|
});
|
||||||
|
|
||||||
expect(repliedRow.components?.length).toBe(2);
|
test('Expect version field to have values', async () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_VER: "BOT_VER",
|
||||||
|
BOT_AUTHOR: "BOT_AUTHOR",
|
||||||
|
BOT_DATE: "BOT_DATE"
|
||||||
|
};
|
||||||
|
|
||||||
|
const message = mock<Message>();
|
||||||
|
message.channel.send = jest.fn();
|
||||||
|
|
||||||
const repliedRowRepoComponent = repliedRow.components![0] as any;
|
const context: ICommandContext = {
|
||||||
|
name: "about",
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
expect(repliedRowRepoComponent.data.url).toBe("https://gitea.vylpes.xyz/rabbitlabs/vylbot-app");
|
const about = new About();
|
||||||
expect(repliedRowRepoComponent.data.label).toBe("Repo");
|
|
||||||
expect(repliedRowRepoComponent.data.style).toBe(ButtonStyle.Link);
|
|
||||||
|
|
||||||
const repliedRowFundingComponent = repliedRow.components![1] as any;
|
const result = await about.execute(context);
|
||||||
|
|
||||||
expect(repliedRowFundingComponent.data.url).toBe("https://ko-fi.com/vylpes");
|
const embed = result.embeds[0];
|
||||||
expect(repliedRowFundingComponent.data.label).toBe("Funding");
|
const field = embed.fields[2];
|
||||||
expect(repliedRowFundingComponent.data.style).toBe(ButtonStyle.Link);
|
|
||||||
|
expect(field.name).toBe('Date');
|
||||||
|
expect(field.value).toBe('BOT_DATE');
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -1,792 +0,0 @@
|
||||||
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder, SlashCommandUserOption } from "discord.js";
|
|
||||||
import Audits from "../../src/commands/audits";
|
|
||||||
import Audit from "../../src/database/entities/Audit";
|
|
||||||
import { AuditType } from "../../src/constants/AuditType";
|
|
||||||
import AuditTools from "../../src/helpers/AuditTools";
|
|
||||||
import EmbedColours from "../../src/constants/EmbedColours";
|
|
||||||
import StringTools from "../../src/helpers/StringTools";
|
|
||||||
|
|
||||||
describe("constructor", () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const audits = new Audits();
|
|
||||||
|
|
||||||
expect(audits.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = audits.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("audits");
|
|
||||||
expect(commandBuilder.description).toBe("View audits of a particular user in the server");
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
|
|
||||||
expect(commandBuilder.options.length).toBe(4);
|
|
||||||
|
|
||||||
const userSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(userSubcommand.name).toBe("user");
|
|
||||||
expect(userSubcommand.description).toBe("View all audits done against a user");
|
|
||||||
expect(userSubcommand.options.length).toBe(1);
|
|
||||||
|
|
||||||
const userSubcommandUserOption = userSubcommand.options[0] as SlashCommandUserOption;
|
|
||||||
|
|
||||||
expect(userSubcommandUserOption.name).toBe("target");
|
|
||||||
expect(userSubcommandUserOption.description).toBe("The user");
|
|
||||||
expect(userSubcommandUserOption.required).toBe(true);
|
|
||||||
|
|
||||||
const viewSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(viewSubcommand.name).toBe("view");
|
|
||||||
expect(viewSubcommand.description).toBe("View a particular audit");
|
|
||||||
expect(viewSubcommand.options.length).toBe(1);
|
|
||||||
|
|
||||||
const viewSubcommandAuditIdOption = viewSubcommand.options[0] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(viewSubcommandAuditIdOption.name).toBe("auditid");
|
|
||||||
expect(viewSubcommandAuditIdOption.description).toBe("The audit id in caps");
|
|
||||||
expect(viewSubcommandAuditIdOption.required).toBe(true);
|
|
||||||
|
|
||||||
const clearSubcommand = commandBuilder.options[2] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(clearSubcommand.name).toBe("clear");
|
|
||||||
expect(clearSubcommand.description).toBe("Clears an audit from a user");
|
|
||||||
expect(clearSubcommand.options.length).toBe(1);
|
|
||||||
|
|
||||||
const clearSubcommandAuditIdOption = clearSubcommand.options[0] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(clearSubcommandAuditIdOption.name).toBe("auditid");
|
|
||||||
expect(clearSubcommandAuditIdOption.description).toBe("The audit id in caps");
|
|
||||||
expect(clearSubcommandAuditIdOption.required).toBe(true);
|
|
||||||
|
|
||||||
const addSubcommand = commandBuilder.options[3] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(addSubcommand.name).toBe("add");
|
|
||||||
expect(addSubcommand.description).toBe("Manually add an audit");
|
|
||||||
expect(addSubcommand.options.length).toBe(3);
|
|
||||||
|
|
||||||
const addSubcommandUserOption = addSubcommand.options[0] as SlashCommandUserOption;
|
|
||||||
|
|
||||||
expect(addSubcommandUserOption.name).toBe("target");
|
|
||||||
expect(addSubcommandUserOption.description).toBe("The user");
|
|
||||||
expect(addSubcommandUserOption.required).toBe(true);
|
|
||||||
|
|
||||||
const addSubcommandTypeOption = addSubcommand.options[1] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(addSubcommandTypeOption.name).toBe("type");
|
|
||||||
expect(addSubcommandTypeOption.description).toBe("The type of audit");
|
|
||||||
expect(addSubcommandTypeOption.required).toBe(true);
|
|
||||||
expect(addSubcommandTypeOption.choices).toBeDefined();
|
|
||||||
expect(addSubcommandTypeOption.choices!.length).toBe(5);
|
|
||||||
|
|
||||||
const addSubcommandReasonOption = addSubcommand.options[2] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(addSubcommandReasonOption.name).toBe("reason");
|
|
||||||
expect(addSubcommandReasonOption.description).toBe("The reason");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('execute', () => {
|
|
||||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN subcommand is invalid, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("invalid"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Subcommand doesn't exist.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("user", () => {
|
|
||||||
test("EXPECT audits for user to be sent", async () => {
|
|
||||||
let repliedWith;
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
id: "userId",
|
|
||||||
};
|
|
||||||
const audit = {
|
|
||||||
AuditId: "auditId",
|
|
||||||
AuditType: AuditType.Warn,
|
|
||||||
WhenCreated: new Date(5 * 60 * 1000),
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("user"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
user: user
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
guildId: "guildId",
|
|
||||||
reply: jest.fn().mockImplementation((options) => {
|
|
||||||
repliedWith = options;
|
|
||||||
})
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditsByUserId = jest.fn().mockReturnValue([ audit ]);
|
|
||||||
AuditTools.TypeToFriendlyText = jest.fn().mockReturnValue("Warn");
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledWith("userId", "guildId");
|
|
||||||
|
|
||||||
expect(AuditTools.TypeToFriendlyText).toHaveBeenCalledTimes(1);
|
|
||||||
expect(AuditTools.TypeToFriendlyText).toHaveBeenCalledWith(AuditType.Warn);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("target", true);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(repliedWith).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embed = repliedWith!.embeds[0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(embed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(embed.data.title).toBe("Audits");
|
|
||||||
expect(embed.data.description).toBe("Audits: 1");
|
|
||||||
expect(embed.data.fields).toBeDefined();
|
|
||||||
expect(embed.data.fields?.length).toBe(1);
|
|
||||||
|
|
||||||
const embedField = embed.data.fields![0];
|
|
||||||
|
|
||||||
expect(embedField.name).toBe("auditId // Warn");
|
|
||||||
expect(embedField.value).toBe(new Date(5 * 60 * 1000).toString());
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("user"),
|
|
||||||
},
|
|
||||||
guildId: null,
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN user is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("user"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
user: null
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
guildId: "guildId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("User not found.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN audits null, EXPECT no audits to be displayed", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("user"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
user: {},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
guildId: "guildId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditsByUserId = jest.fn().mockResolvedValue(null);
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("There are no audits for this user.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN audits length is 0, EXPECT no audits to be displayed", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("user"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
user: {},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
guildId: "guildId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditsByUserId = jest.fn().mockResolvedValue([]);
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.FetchAuditsByUserId).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("There are no audits for this user.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("view", () => {
|
|
||||||
test("EXPECT specific audit defaults to be sent", async () => {
|
|
||||||
let repliedWith;
|
|
||||||
|
|
||||||
const auditOption = {
|
|
||||||
value: "auditId",
|
|
||||||
};
|
|
||||||
|
|
||||||
const audit = {
|
|
||||||
Reason: "Test reason",
|
|
||||||
AuditType: AuditType.Warn,
|
|
||||||
ModeratorId: "moderatorId",
|
|
||||||
AuditId: "auditId",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("view"),
|
|
||||||
get: jest.fn().mockReturnValue(auditOption),
|
|
||||||
},
|
|
||||||
reply: jest.fn().mockImplementation((options) => {
|
|
||||||
repliedWith = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditByAuditId = jest.fn().mockResolvedValue(audit);
|
|
||||||
AuditTools.TypeToFriendlyText = jest.fn().mockReturnValue("Warn");
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledWith("AUDITID", "guildId");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(repliedWith).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embed = repliedWith!.embeds[0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(embed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(embed.data.title).toBe("Audit");
|
|
||||||
expect(embed.data.description).toBe("AUDITID");
|
|
||||||
expect(embed.data.fields).toBeDefined();
|
|
||||||
expect(embed.data.fields!.length).toBe(3);
|
|
||||||
|
|
||||||
const embedReasonField = embed.data.fields![0];
|
|
||||||
|
|
||||||
expect(embedReasonField.name).toBe("Reason");
|
|
||||||
expect(embedReasonField.value).toBe("Test reason");
|
|
||||||
expect(embedReasonField.inline).toBe(true);
|
|
||||||
|
|
||||||
const embedTypeField = embed.data.fields![1];
|
|
||||||
|
|
||||||
expect(embedTypeField.name).toBe("Type");
|
|
||||||
expect(embedTypeField.value).toBe("Warn");
|
|
||||||
expect(embedTypeField.inline).toBe(true);
|
|
||||||
|
|
||||||
const embedModeratorField = embed.data.fields![2];
|
|
||||||
|
|
||||||
expect(embedModeratorField.name).toBe("Moderator");
|
|
||||||
expect(embedModeratorField.value).toBe("<@moderatorId>");
|
|
||||||
expect(embedModeratorField.inline).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, expect nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("view"),
|
|
||||||
},
|
|
||||||
guildId: null,
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN auditId is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("view"),
|
|
||||||
get: jest.fn().mockReturnValue(null),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN auditId.value is undefined, EXPECT error", async () => {
|
|
||||||
const audit = {
|
|
||||||
value: undefined,
|
|
||||||
}
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("view"),
|
|
||||||
get: jest.fn().mockReturnValue(audit),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN audit is not in database, EXPECT error", async () => {
|
|
||||||
const audit = {
|
|
||||||
value: "auditId",
|
|
||||||
}
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("view"),
|
|
||||||
get: jest.fn().mockReturnValue(audit),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditByAuditId = jest.fn().mockResolvedValue(null);
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Audit not found.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN audit.Reason was not supplied, EXPECT reason to be defaulted", async () => {
|
|
||||||
let repliedWith;
|
|
||||||
|
|
||||||
const auditOption = {
|
|
||||||
value: "auditId",
|
|
||||||
};
|
|
||||||
|
|
||||||
const audit = {
|
|
||||||
Reason: undefined,
|
|
||||||
AuditType: AuditType.Warn,
|
|
||||||
ModeratorId: "moderatorId",
|
|
||||||
AuditId: "auditId",
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("view"),
|
|
||||||
get: jest.fn().mockReturnValue(auditOption),
|
|
||||||
},
|
|
||||||
reply: jest.fn().mockImplementation((options) => {
|
|
||||||
repliedWith = options;
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditByAuditId = jest.fn().mockResolvedValue(audit);
|
|
||||||
AuditTools.TypeToFriendlyText = jest.fn().mockReturnValue("Warn");
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
const embed = repliedWith!.embeds[0] as EmbedBuilder;
|
|
||||||
|
|
||||||
const embedReasonField = embed.data.fields![0];
|
|
||||||
|
|
||||||
expect(embedReasonField.name).toBe("Reason");
|
|
||||||
expect(embedReasonField.value).toBe("*none*");
|
|
||||||
expect(embedReasonField.inline).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("clear", () => {
|
|
||||||
test("EXPECT audit to be cleared", async () => {
|
|
||||||
const auditOption = {
|
|
||||||
value: "auditId",
|
|
||||||
}
|
|
||||||
|
|
||||||
const audit = {};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("clear"),
|
|
||||||
get: jest.fn().mockReturnValue(auditOption),
|
|
||||||
},
|
|
||||||
guildId: "guildId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditByAuditId = jest.fn().mockReturnValue(audit);
|
|
||||||
Audit.Remove = jest.fn();
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledWith("AUDITID", "guildId");
|
|
||||||
|
|
||||||
expect(Audit.Remove).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("auditid");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Audit cleared.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("clear"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN auditId is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("clear"),
|
|
||||||
get: jest.fn().mockReturnValue(null),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN auditId.value is undefined, EXPECT error", async () => {
|
|
||||||
const auditOption = {
|
|
||||||
value: undefined,
|
|
||||||
}
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("clear"),
|
|
||||||
get: jest.fn().mockReturnValue(auditOption),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("AuditId not found.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN audit is not found, EXPECT error", async () => {
|
|
||||||
const auditOption = {
|
|
||||||
value: "auditId",
|
|
||||||
}
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("clear"),
|
|
||||||
get: jest.fn().mockReturnValue(auditOption),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.FetchAuditByAuditId = jest.fn().mockReturnValue(null);
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.FetchAuditByAuditId).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Audit not found.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("add", () => {
|
|
||||||
test("EXPECT audit to be added", async () => {
|
|
||||||
const user = {
|
|
||||||
id: "userId",
|
|
||||||
};
|
|
||||||
|
|
||||||
const type = {
|
|
||||||
value: AuditType.Warn,
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce({ user })
|
|
||||||
.mockReturnValueOnce(type)
|
|
||||||
.mockReturnValue(reason),
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn();
|
|
||||||
StringTools.RandomString = jest.fn().mockReturnValue("AUDITID");
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Created new audit with ID `AUDITID`");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: null,
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN user is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce({ user: null })
|
|
||||||
.mockReturnValue({}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Invalid input.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN auditType is null, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce({ user: null })
|
|
||||||
.mockReturnValueOnce(null)
|
|
||||||
.mockReturnValue({}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Invalid input.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN auditType.value is undefined, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce({ user: null })
|
|
||||||
.mockReturnValueOnce({
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
.mockReturnValue({}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Invalid input.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN reasonInput is null, EXPECT reason to be empty", async ()=> {
|
|
||||||
let savedAudit: Audit | undefined;
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
id: "userId",
|
|
||||||
};
|
|
||||||
|
|
||||||
const type = {
|
|
||||||
value: AuditType.Warn,
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce({ user })
|
|
||||||
.mockReturnValueOnce(type)
|
|
||||||
.mockReturnValue(null),
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
|
||||||
savedAudit = audit;
|
|
||||||
});
|
|
||||||
StringTools.RandomString = jest.fn().mockReturnValue("AUDITID");
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(savedAudit).toBeDefined();
|
|
||||||
expect(savedAudit!.Reason).toBe("");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN reasonType.value is undefined, EXPECT reason to be empty", async () => {
|
|
||||||
let savedAudit: Audit | undefined;
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
id: "userId",
|
|
||||||
};
|
|
||||||
|
|
||||||
const type = {
|
|
||||||
value: AuditType.Warn,
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce({ user })
|
|
||||||
.mockReturnValueOnce(type)
|
|
||||||
.mockReturnValue(reason),
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
|
||||||
savedAudit = audit;
|
|
||||||
});
|
|
||||||
StringTools.RandomString = jest.fn().mockReturnValue("AUDITID");
|
|
||||||
|
|
||||||
const audits = new Audits();
|
|
||||||
await audits.execute(interaction);
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(savedAudit).toBeDefined();
|
|
||||||
expect(savedAudit!.Reason).toBe("");
|
|
||||||
});
|
|
||||||
});
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,125 +0,0 @@
|
||||||
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
|
|
||||||
import Bunny from "../../src/commands/bunny";
|
|
||||||
import * as randomBunny from "random-bunny";
|
|
||||||
import { EmbedBuilder } from "@discordjs/builders";
|
|
||||||
import EmbedColours from "../../src/constants/EmbedColours";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
jest.resetAllMocks();
|
|
||||||
jest.resetModules();
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const bunny = new Bunny();
|
|
||||||
|
|
||||||
expect(bunny.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = bunny.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("bunny");
|
|
||||||
expect(commandBuilder.description).toBe("Get a random picture of a rabbit.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('execute', () => {
|
|
||||||
test("EXPECT embed to be sent", async () => {
|
|
||||||
let repliedWith;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
editReply: jest.fn().mockImplementation((options) => {
|
|
||||||
repliedWith = options;
|
|
||||||
}),
|
|
||||||
deferReply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
jest.spyOn(randomBunny, "default").mockResolvedValue({
|
|
||||||
IsSuccess: true,
|
|
||||||
Result: {
|
|
||||||
Title: "title",
|
|
||||||
Permalink: "/r/permalink",
|
|
||||||
Url: "https://permalink.com/url.png",
|
|
||||||
Ups: 1,
|
|
||||||
Archived: false,
|
|
||||||
Downs: 1,
|
|
||||||
Hidden: false,
|
|
||||||
Subreddit: "rabbits",
|
|
||||||
SubredditSubscribers: 1,
|
|
||||||
},
|
|
||||||
Error: undefined,
|
|
||||||
Query: {
|
|
||||||
subreddit: "rabbits",
|
|
||||||
sortBy: "hot",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Math.floor = jest.fn().mockReturnValue(0);
|
|
||||||
|
|
||||||
const bunny = new Bunny();
|
|
||||||
await bunny.execute(interaction);
|
|
||||||
|
|
||||||
expect(randomBunny.default).toHaveBeenCalledTimes(1);
|
|
||||||
expect(randomBunny.default).toHaveBeenCalledWith("rabbits", "hot");
|
|
||||||
|
|
||||||
expect(interaction.editReply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.deferReply).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(repliedWith).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds).toBeDefined();
|
|
||||||
expect(repliedWith!.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embed = repliedWith!.embeds[0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(embed.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(embed.data.title).toBe("title");
|
|
||||||
expect(embed.data.description).toBe("/r/permalink");
|
|
||||||
expect(embed.data.image).toBeDefined();
|
|
||||||
expect(embed.data.image!.url).toBe("https://permalink.com/url.png");
|
|
||||||
expect(embed.data.url).toBe("https://reddit.com/r/permalink");
|
|
||||||
expect(embed.data.footer).toBeDefined();
|
|
||||||
expect(embed.data.footer!.text).toBe("r/rabbits · 1 upvotes");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const bunny = new Bunny();
|
|
||||||
await bunny.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN a result from random-bunny is failure, EXPECT error", async () => {
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
editReply: jest.fn(),
|
|
||||||
deferReply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
jest.spyOn(randomBunny, "default").mockResolvedValue({
|
|
||||||
IsSuccess: false,
|
|
||||||
Result: undefined,
|
|
||||||
Error: undefined,
|
|
||||||
Query: {
|
|
||||||
subreddit: "rabbits",
|
|
||||||
sortBy: "hot",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Math.floor = jest.fn().mockReturnValue(0);
|
|
||||||
|
|
||||||
const bunny = new Bunny();
|
|
||||||
await bunny.execute(interaction);
|
|
||||||
|
|
||||||
expect(randomBunny.default).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.editReply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.editReply).toHaveBeenCalledWith("There was an error running this command.");
|
|
||||||
|
|
||||||
expect(interaction.deferReply).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,216 +1,178 @@
|
||||||
import { ChatInputCommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandNumberOption, TextChannel } from "discord.js";
|
import { Message } from "discord.js";
|
||||||
import Command from "../../src/commands/clear";
|
import Clear from "../../src/commands/clear";
|
||||||
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env = {};
|
process.env = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test("EXPECT values to be set", () => {
|
test('Expect values to be set', () => {
|
||||||
const command = new Command();
|
process.env = {
|
||||||
|
ROLES_MODERATOR: "Moderator"
|
||||||
|
};
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
const clear = new Clear();
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
expect(clear._category).toBe('Moderation');
|
||||||
|
expect(clear._roles.length).toBe(1);
|
||||||
expect(commandBuilder.name).toBe("clear");
|
expect(clear._roles[0]).toBe('Moderator');
|
||||||
expect(commandBuilder.description).toBe("Clears the channel of messages");
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ManageMessages.toString());
|
|
||||||
expect(commandBuilder.options.length).toBe(1);
|
|
||||||
|
|
||||||
const commandBuilderCountOption = commandBuilder.options[0] as SlashCommandNumberOption;
|
|
||||||
|
|
||||||
expect(commandBuilderCountOption.name).toBe("count");
|
|
||||||
expect(commandBuilderCountOption.description).toBe("The amount to delete");
|
|
||||||
expect(commandBuilderCountOption.required).toBe(true);
|
|
||||||
expect(commandBuilderCountOption.min_value).toBe(1);
|
|
||||||
expect(commandBuilderCountOption.max_value).toBe(100);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Execute', () => {
|
describe('Execute', () => {
|
||||||
test("GIVEN input is valid, EXPECT messages to be cleared", async () => {
|
test('Given valid arguments, expect messages to be cleared', async () => {
|
||||||
// Arrange
|
const messageChannelSend = jest.fn();
|
||||||
const channel = {
|
const messageChannelBulkDelete = jest.fn();
|
||||||
manageable: true,
|
|
||||||
bulkDelete: jest.fn(),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
const message = {
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
channel: {
|
||||||
channel: channel,
|
send: messageChannelSend,
|
||||||
options: {
|
bulkDelete: messageChannelBulkDelete
|
||||||
getNumber: jest.fn().mockReturnValue(50),
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const context: ICommandContext = {
|
||||||
const command = new Command();
|
name: 'clear',
|
||||||
await command.execute(interaction);
|
args: ['5'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
// Assert
|
const clear = new Clear();
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
const result = await clear.execute(context);
|
||||||
|
|
||||||
expect(interaction.options.getNumber).toHaveBeenCalledTimes(1);
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
expect(interaction.options.getNumber).toHaveBeenCalledWith("count");
|
expect(messageChannelBulkDelete).toBeCalledWith(5);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
expect(channel.bulkDelete).toHaveBeenCalledTimes(1);
|
// PublicEmbed
|
||||||
expect(channel.bulkDelete).toHaveBeenCalledWith(50);
|
const publicEmbed = result.embeds[0];
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
expect(publicEmbed.title).toBe('');
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("50 message(s) were removed.");
|
expect(publicEmbed.description).toBe('5 message(s) were removed');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN interaction is NOT a chat input command, EXPECT nothing to happen", async () => {
|
test('Given argument is not given, expect error embed to be sent', async () => {
|
||||||
// Arrange
|
const messageChannelSend = jest.fn();
|
||||||
const channel = {
|
const messageChannelBulkDelete = jest.fn();
|
||||||
manageable: true,
|
|
||||||
bulkDelete: jest.fn(),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
const message = {
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
channel: {
|
||||||
channel: channel,
|
send: messageChannelSend,
|
||||||
options: {
|
bulkDelete: messageChannelBulkDelete
|
||||||
getNumber: jest.fn().mockReturnValue(50),
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const context: ICommandContext = {
|
||||||
const command = new Command();
|
name: 'clear',
|
||||||
await command.execute(interaction);
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
// Assert
|
const clear = new Clear();
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
const result = await clear.execute(context);
|
||||||
expect(channel.bulkDelete).not.toHaveBeenCalled();
|
|
||||||
|
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 interaction.channel is null, EXPECT nothing to happen", async () => {
|
test('Given argument is not a number, expect error embed to be sent', async () => {
|
||||||
// Arrange
|
const messageChannelSend = jest.fn();
|
||||||
const interaction = {
|
const messageChannelBulkDelete = jest.fn();
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
channel: null,
|
|
||||||
options: {
|
|
||||||
getNumber: jest.fn().mockReturnValue(50),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const message = {
|
||||||
const command = new Command();
|
channel: {
|
||||||
await command.execute(interaction);
|
send: messageChannelSend,
|
||||||
|
bulkDelete: messageChannelBulkDelete
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
// Assert
|
const context: ICommandContext = {
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
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 totalClear input is NOT supplied, EXPECT invalid error", async () => {
|
test('Given argument is less than 1, expect error embed to be sent', async () => {
|
||||||
// Arrange
|
const messageChannelSend = jest.fn();
|
||||||
const channel = {
|
const messageChannelBulkDelete = jest.fn();
|
||||||
manageable: true,
|
|
||||||
bulkDelete: jest.fn(),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
const message = {
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
channel: {
|
||||||
channel: channel,
|
send: messageChannelSend,
|
||||||
options: {
|
bulkDelete: messageChannelBulkDelete
|
||||||
getNumber: jest.fn().mockReturnValue(null),
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const context: ICommandContext = {
|
||||||
const command = new Command();
|
name: 'clear',
|
||||||
await command.execute(interaction);
|
args: ['0'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
// Assert
|
const clear = new Clear();
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
const result = await clear.execute(context);
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
|
|
||||||
|
|
||||||
expect(channel.bulkDelete).not.toHaveBeenCalled();
|
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 totalClear is less than or equal to 0, EXPECT invalid error", async () => {
|
test('Given argument is more than 100, expect error embed to be sent', async () => {
|
||||||
// Arrange
|
const messageChannelSend = jest.fn();
|
||||||
const channel = {
|
const messageChannelBulkDelete = jest.fn();
|
||||||
manageable: true,
|
|
||||||
bulkDelete: jest.fn(),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
const message = {
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
channel: {
|
||||||
channel: channel,
|
send: messageChannelSend,
|
||||||
options: {
|
bulkDelete: messageChannelBulkDelete
|
||||||
getNumber: jest.fn().mockReturnValue(0),
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const context: ICommandContext = {
|
||||||
const command = new Command();
|
name: 'clear',
|
||||||
await command.execute(interaction);
|
args: ['101'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
// Assert
|
const clear = new Clear();
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
const result = await clear.execute(context);
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
|
|
||||||
|
|
||||||
expect(channel.bulkDelete).not.toHaveBeenCalled();
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
});
|
expect(messageChannelBulkDelete).not.toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
test("GIVEN totalClear is greater than 100, EXPECT invalid error", async () => {
|
// ErrorEmbed
|
||||||
// Arrange
|
const errorEmbed = result.embeds[0];
|
||||||
const channel = {
|
|
||||||
manageable: true,
|
|
||||||
bulkDelete: jest.fn(),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
expect(errorEmbed.title).toBeNull();
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
|
||||||
channel: channel,
|
|
||||||
options: {
|
|
||||||
getNumber: jest.fn().mockReturnValue(101),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
|
|
||||||
|
|
||||||
expect(channel.bulkDelete).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel is NOT manageable, EXPECT insufficient permissions error", async () => {
|
|
||||||
// Arrange
|
|
||||||
const channel = {
|
|
||||||
manageable: false,
|
|
||||||
bulkDelete: jest.fn(),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
channel: channel,
|
|
||||||
options: {
|
|
||||||
getNumber: jest.fn().mockReturnValue(50),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Insufficient permissions. Please contact a moderator.");
|
|
||||||
|
|
||||||
expect(channel.bulkDelete).not.toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -1,261 +0,0 @@
|
||||||
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("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("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("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;
|
|
||||||
|
|
||||||
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("EXPECT embed to be sent", 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("12345");
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// 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,612 +0,0 @@
|
||||||
import { ChatInputCommandInteraction, CommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from "discord.js";
|
|
||||||
import Command from "../../src/commands/config";
|
|
||||||
import Server from "../../src/database/entities/Server";
|
|
||||||
import fs from "fs";
|
|
||||||
import EmbedColours from "../../src/constants/EmbedColours";
|
|
||||||
import Setting from "../../src/database/entities/Setting";
|
|
||||||
import DefaultValues from "../../src/constants/DefaultValues";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.cwd = jest.fn().mockReturnValue("/cwd");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("constructor", () => {
|
|
||||||
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("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;
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.getSubcommand).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
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", () => {
|
|
||||||
test("EXPECT help text to be sent", async () => {
|
|
||||||
let repliedWith: any;
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("list"),
|
|
||||||
},
|
|
||||||
reply: jest.fn().mockImplementation((options: any) => {
|
|
||||||
repliedWith = options.embeds[0];
|
|
||||||
}),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({});
|
|
||||||
|
|
||||||
const readFileSyncMock = jest.spyOn(fs, "readFileSync").mockReturnValue("Example config text");
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(readFileSyncMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(readFileSyncMock).toHaveBeenCalledWith("/cwd/data/usage/config.txt");
|
|
||||||
|
|
||||||
expect(repliedWith).toBeDefined();
|
|
||||||
expect(repliedWith.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(repliedWith.data.title).toBe("Config");
|
|
||||||
expect(repliedWith.data.description).toBe("Example config text");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("reset", () => {
|
|
||||||
test("EXPECT setting value to be set to default", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("reset"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "test.key",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [
|
|
||||||
{
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("key");
|
|
||||||
|
|
||||||
expect(Server.FetchOneById).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Server.FetchOneById).toHaveBeenCalledWith(Server, "guildId", [ "Settings" ]);
|
|
||||||
|
|
||||||
expect(Setting.Remove).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Setting.Remove).toHaveBeenCalledWith(Setting, {
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("The setting has been reset to the default.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN key is null, EXPECT error", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("reset"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: undefined,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [
|
|
||||||
{
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN key.value is undefined, EXPECT error", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("reset"),
|
|
||||||
get: jest.fn().mockReturnValue(null),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [
|
|
||||||
{
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN setting is not found, EXPECT error", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("reset"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "test.key",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [],
|
|
||||||
});
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Setting not found.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("get", () => {
|
|
||||||
test("EXPECT setting value to be sent", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("get"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "test.key",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [
|
|
||||||
{
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("`test.key`: `12345`");
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("key");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN key is null, EXPECT error", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("get"),
|
|
||||||
get: jest.fn().mockReturnValue(null),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [
|
|
||||||
{
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN key.value is undefined, EXPECT error", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("get"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: undefined,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [
|
|
||||||
{
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Fields are required.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN setting can not be found AND a default value is found, EXPECT default to be shown", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("get"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "test.key",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [],
|
|
||||||
});
|
|
||||||
|
|
||||||
DefaultValues.GetValue = jest.fn().mockReturnValue("Test");
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("`test.key`: `Test` <DEFAULT>");
|
|
||||||
|
|
||||||
expect(DefaultValues.GetValue).toHaveBeenCalledTimes(1);
|
|
||||||
expect(DefaultValues.GetValue).toHaveBeenCalledWith("test.key");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN setting can not be found AND a default value is not found, EXPECT none to be shown", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("get"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "test.key",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue({
|
|
||||||
Settings: [],
|
|
||||||
});
|
|
||||||
|
|
||||||
DefaultValues.GetValue = jest.fn().mockReturnValue(undefined);
|
|
||||||
|
|
||||||
Setting.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("`test.key`: <NONE>");
|
|
||||||
|
|
||||||
expect(DefaultValues.GetValue).toHaveBeenCalledTimes(1);
|
|
||||||
expect(DefaultValues.GetValue).toHaveBeenCalledWith("test.key");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("set", () => {
|
|
||||||
test("GIVEN setting already is set, EXPECT setting to be updated", async () => {
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("set"),
|
|
||||||
get: jest.fn().mockReturnValueOnce({
|
|
||||||
value: "test.key",
|
|
||||||
}).mockReturnValue({
|
|
||||||
value: "54321",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const server = {
|
|
||||||
Settings: [
|
|
||||||
{
|
|
||||||
Key: "test.key",
|
|
||||||
Value: "12345",
|
|
||||||
UpdateBasicDetails: jest.fn(),
|
|
||||||
Save: jest.fn(),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
} as unknown as Server;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue(server);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Setting has been set.");
|
|
||||||
|
|
||||||
expect(server.Settings[0].UpdateBasicDetails).toHaveBeenCalledTimes(1);
|
|
||||||
expect(server.Settings[0].UpdateBasicDetails).toHaveBeenCalledWith("test.key", "54321");
|
|
||||||
|
|
||||||
expect(server.Settings[0].Save).toHaveBeenCalledTimes(1);
|
|
||||||
expect(server.Settings[0].Save).toHaveBeenCalledWith(Setting, server.Settings[0]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN setting is not set, EXPECT setting to be added", async () => {
|
|
||||||
let savedSetting: Setting | undefined;
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
guildId: "guildId",
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("set"),
|
|
||||||
get: jest.fn().mockReturnValueOnce({
|
|
||||||
value: "test.key",
|
|
||||||
}).mockReturnValue({
|
|
||||||
value: "54321",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const server = {
|
|
||||||
Settings: [],
|
|
||||||
AddSettingToServer: jest.fn(),
|
|
||||||
Save: jest.fn(),
|
|
||||||
} as unknown as Server;
|
|
||||||
|
|
||||||
Server.FetchOneById = jest.fn().mockResolvedValue(server);
|
|
||||||
|
|
||||||
Setting.prototype.Save = jest.fn().mockImplementation((_, setting: Setting) => {
|
|
||||||
savedSetting = setting;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(Setting.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
expect(Setting.prototype.Save).toHaveBeenCalledWith(Setting, expect.any(Setting));
|
|
||||||
|
|
||||||
expect(server.AddSettingToServer).toHaveBeenCalledTimes(1);
|
|
||||||
expect(server.AddSettingToServer).toHaveBeenCalledWith(expect.any(Setting));
|
|
||||||
|
|
||||||
expect(server.Save).toHaveBeenCalledTimes(1);
|
|
||||||
expect(server.Save).toHaveBeenCalledWith(Server, server);
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Setting has been set.");
|
|
||||||
|
|
||||||
expect(savedSetting).toBeDefined();
|
|
||||||
expect(savedSetting!.Key).toBe("test.key");
|
|
||||||
expect(savedSetting!.Value).toBe("54321");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,285 +0,0 @@
|
||||||
import { ChatInputCommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from "discord.js";
|
|
||||||
import Command from "../../src/commands/disable";
|
|
||||||
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test('EXPECT properties to be set', () => {
|
|
||||||
const command = new Command();
|
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("disable");
|
|
||||||
expect(commandBuilder.description).toBe("Disables a command");
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.Administrator.toString());
|
|
||||||
expect(commandBuilder.options.length).toBe(2);
|
|
||||||
|
|
||||||
const commandBuilderAddSubcommand = commandBuilder.options[0] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilderAddSubcommand.name).toBe("add");
|
|
||||||
expect(commandBuilderAddSubcommand.description).toBe("Disables a command for the server");
|
|
||||||
expect(commandBuilderAddSubcommand.options.length).toBe(1);
|
|
||||||
|
|
||||||
const commandBuilderAddSubcommandNameOption = commandBuilderAddSubcommand.options[0] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderAddSubcommandNameOption.name).toBe("name");
|
|
||||||
expect(commandBuilderAddSubcommandNameOption.description).toBe("The name of the command");
|
|
||||||
expect(commandBuilderAddSubcommandNameOption.required).toBe(true);
|
|
||||||
|
|
||||||
const commandBuilderRemoveSubcommand = commandBuilder.options[1] as SlashCommandSubcommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilderRemoveSubcommand.name).toBe("remove");
|
|
||||||
expect(commandBuilderRemoveSubcommand.description).toBe("Enables a command for the server");
|
|
||||||
expect(commandBuilderRemoveSubcommand.options.length).toBe(1);
|
|
||||||
|
|
||||||
const commandBuilderRemoveSubcommandNameOption = commandBuilderRemoveSubcommand.options[0] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderRemoveSubcommandNameOption.name).toBe("name");
|
|
||||||
expect(commandBuilderRemoveSubcommandNameOption.description).toBe("The name of the command");
|
|
||||||
expect(commandBuilderRemoveSubcommandNameOption.required).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('execute', () => {
|
|
||||||
test("GIVEN interaction is not a chat input command, EXPECT nothing to happen", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(false),
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN subcommand is invalid, EXPECT error", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("invalid"),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
// 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('add', () => {
|
|
||||||
test("EXPECT command to be added to disabled list", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "testCommand",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand");
|
|
||||||
SettingsHelper.SetSetting = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("name", true);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("commands.disabled", "guildId");
|
|
||||||
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "otherCommand,testCommand");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Disabled command testCommand");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: null,
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "testCommand",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand");
|
|
||||||
SettingsHelper.SetSetting = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
expect(SettingsHelper.SetSetting).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN disabledCommandsString is undefined, EXPECT empty disabledCommands array to be used", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("add"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "testCommand",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
|
|
||||||
SettingsHelper.SetSetting = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "testCommand");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("remove", () => {
|
|
||||||
test("EXPECT command to be removed from disabled list", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("remove"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "testCommand",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand,testCommand");
|
|
||||||
SettingsHelper.SetSetting = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("name", true);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("commands.disabled", "guildId");
|
|
||||||
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "otherCommand");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Enabled command testCommand");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: null,
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("remove"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "testCommand",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand,testCommand");
|
|
||||||
SettingsHelper.SetSetting = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
expect(SettingsHelper.SetSetting).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN disabledCommandsString is undefined, EXPECT empty disabledCommands array to be used", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("remove"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "testCommand",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
|
|
||||||
SettingsHelper.SetSetting = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN instance of commandName is not found in disabledCommands array, EXPECT it not to try to remove it", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
isChatInputCommand: jest.fn().mockReturnValue(true),
|
|
||||||
options: {
|
|
||||||
getSubcommand: jest.fn().mockReturnValue("remove"),
|
|
||||||
get: jest.fn().mockReturnValue({
|
|
||||||
value: "testCommand",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as ChatInputCommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("otherCommand");
|
|
||||||
SettingsHelper.SetSetting = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.SetSetting).toHaveBeenCalledWith("commands.disabled", "guildId", "otherCommand");
|
|
||||||
});
|
|
||||||
});
|
|
136
tests/commands/eval.test.ts
Normal file
136
tests/commands/eval.test.ts
Normal file
|
@ -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');
|
||||||
|
});
|
||||||
|
});
|
267
tests/commands/help.test.ts
Normal file
267
tests/commands/help.test.ts
Normal file
|
@ -0,0 +1,267 @@
|
||||||
|
import Help, { ICommandData } from "../../src/commands/help";
|
||||||
|
import { Message } from "discord.js";
|
||||||
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
|
|
||||||
|
const oldCwd = process.cwd();
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties to be set', () => {
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
expect(help._category).toBe('General');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Execute', () => {
|
||||||
|
test('Given no arguments were given, expect SendAll to be executed', () => {
|
||||||
|
const message = {} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'help',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
help.SendAll = jest.fn();
|
||||||
|
help.SendSingle = jest.fn();
|
||||||
|
|
||||||
|
help.execute(context);
|
||||||
|
|
||||||
|
expect(help.SendAll).toBeCalled();
|
||||||
|
expect(help.SendSingle).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given an argument was given, expect SendSingle to be executed', () => {
|
||||||
|
const message = {} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'help',
|
||||||
|
args: ['about'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
help.SendAll = jest.fn();
|
||||||
|
help.SendSingle = jest.fn();
|
||||||
|
|
||||||
|
help.execute(context);
|
||||||
|
|
||||||
|
expect(help.SendAll).not.toBeCalled();
|
||||||
|
expect(help.SendSingle).toBeCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendAll', () => {
|
||||||
|
test('Expect embed with all commands to be sent', () => {
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'help',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
const commandData0: ICommandData = {
|
||||||
|
Exists: true,
|
||||||
|
Name: 'about',
|
||||||
|
Category: 'general',
|
||||||
|
Roles: []
|
||||||
|
};
|
||||||
|
|
||||||
|
const commandData1: ICommandData = {
|
||||||
|
Exists: true,
|
||||||
|
Name: 'role',
|
||||||
|
Category: 'general',
|
||||||
|
Roles: []
|
||||||
|
};
|
||||||
|
|
||||||
|
help.GetAllCommandData = jest.fn()
|
||||||
|
.mockReturnValue([commandData0, commandData1]);
|
||||||
|
|
||||||
|
const result = help.SendAll(context);
|
||||||
|
|
||||||
|
expect(help.GetAllCommandData).toBeCalled();
|
||||||
|
expect(messageChannelSend).toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// PublicEmbed
|
||||||
|
const publicEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(publicEmbed.fields.length).toBe(1);
|
||||||
|
|
||||||
|
// PublicEmbed -> GeneralCategory Field
|
||||||
|
const publicEmbedFieldGeneral = publicEmbed.fields[0];
|
||||||
|
|
||||||
|
expect(publicEmbedFieldGeneral.name).toBe('General');
|
||||||
|
expect(publicEmbedFieldGeneral.value).toBe('about, role');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendSingle', () => {
|
||||||
|
test('Given command exists, expect embed to be sent with command fields', () => {
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'help',
|
||||||
|
args: ['about'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const commandData: ICommandData = {
|
||||||
|
Exists: true,
|
||||||
|
Name: 'about',
|
||||||
|
Category: 'general',
|
||||||
|
Roles: ['role1', 'role2']
|
||||||
|
};
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
help.GetCommandData = jest.fn()
|
||||||
|
.mockReturnValue(commandData);
|
||||||
|
|
||||||
|
const result = help.SendSingle(context);
|
||||||
|
|
||||||
|
expect(help.GetCommandData).toBeCalledWith('about');
|
||||||
|
expect(messageChannelSend).toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// PublicEmbed
|
||||||
|
const publicEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(publicEmbed.title).toBe('About');
|
||||||
|
expect(publicEmbed.description).toBe('');
|
||||||
|
expect(publicEmbed.fields.length).toBe(2);
|
||||||
|
|
||||||
|
// PublicEmbed -> Category Field
|
||||||
|
const fieldCategory = publicEmbed.fields[0];
|
||||||
|
|
||||||
|
expect(fieldCategory.name).toBe('Category');
|
||||||
|
expect(fieldCategory.value).toBe('General');
|
||||||
|
|
||||||
|
// PublicEmbed -> RequiredRoles Field
|
||||||
|
const fieldRoles = publicEmbed.fields[1];
|
||||||
|
|
||||||
|
expect(fieldRoles.name).toBe('Required Roles');
|
||||||
|
expect(fieldRoles.value).toBe('Role1, Role2');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given command does not exist, expect error embed to be sent', () => {
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'help',
|
||||||
|
args: ['about'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const commandData: ICommandData = {
|
||||||
|
Exists: false
|
||||||
|
};
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
help.GetCommandData = jest.fn()
|
||||||
|
.mockReturnValue(commandData);
|
||||||
|
|
||||||
|
const result = help.SendSingle(context);
|
||||||
|
|
||||||
|
expect(help.GetCommandData).toBeCalledWith('about');
|
||||||
|
expect(messageChannelSend).toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// ErrorEmbed
|
||||||
|
const errorEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(errorEmbed.description).toBe('Command does not exist');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GetAllCommandData', () => {
|
||||||
|
test('Expect array of command data to be returned', () => {
|
||||||
|
process.env = {
|
||||||
|
FOLDERS_COMMANDS: "commands"
|
||||||
|
};
|
||||||
|
|
||||||
|
process.cwd = jest.fn()
|
||||||
|
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
const result = help.GetAllCommandData();
|
||||||
|
|
||||||
|
expect(result.length).toBe(1);
|
||||||
|
|
||||||
|
// Mock Command
|
||||||
|
const mockCommand = result[0];
|
||||||
|
|
||||||
|
expect(mockCommand.Exists).toBeTruthy();
|
||||||
|
expect(mockCommand.Name).toBe("mockCmd");
|
||||||
|
expect(mockCommand.Category).toBe("General");
|
||||||
|
|
||||||
|
expect(mockCommand.Roles!.length).toBe(1);
|
||||||
|
expect(mockCommand.Roles![0]).toBe("Moderator");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GetCommandData', () => {
|
||||||
|
test('Given command exists, expect data to be returned', () => {
|
||||||
|
process.env = {
|
||||||
|
FOLDERS_COMMANDS: "commands"
|
||||||
|
};
|
||||||
|
|
||||||
|
process.cwd = jest.fn()
|
||||||
|
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
const result = help.GetCommandData('mockCmd');
|
||||||
|
|
||||||
|
expect(result.Exists).toBeTruthy();
|
||||||
|
expect(result.Name).toBe("mockCmd");
|
||||||
|
expect(result.Category).toBe("General");
|
||||||
|
|
||||||
|
expect(result.Roles!.length).toBe(1);
|
||||||
|
expect(result.Roles![0]).toBe("Moderator");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given command does not exist, expect exists false to be returned', () => {
|
||||||
|
process.env = {
|
||||||
|
FOLDERS_COMMANDS: "commands"
|
||||||
|
};
|
||||||
|
|
||||||
|
const oldCwd = process.cwd();
|
||||||
|
|
||||||
|
process.cwd = jest.fn()
|
||||||
|
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
||||||
|
|
||||||
|
const help = new Help();
|
||||||
|
|
||||||
|
const result = help.GetCommandData('none');
|
||||||
|
|
||||||
|
expect(result.Exists).toBeFalsy();
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,79 +0,0 @@
|
||||||
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js";
|
|
||||||
import Command from "../../src/commands/ignore";
|
|
||||||
import IgnoredChannel from "../../src/database/entities/IgnoredChannel";
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
const command = new Command();
|
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("ignore");
|
|
||||||
expect(commandBuilder.description).toBe("Ignore events in this channel");
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.Administrator.toString());
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("execute", () => {
|
|
||||||
test("GIVEN channel is currently ignored, EXPECT channel to be removed from list", async () => {
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
channelId: "channelId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
IgnoredChannel.IsChannelIgnored = jest.fn().mockResolvedValue(true);
|
|
||||||
IgnoredChannel.FetchOneById = jest.fn().mockResolvedValue({});
|
|
||||||
IgnoredChannel.Remove = jest.fn();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(IgnoredChannel.IsChannelIgnored).toHaveBeenCalledTimes(1);
|
|
||||||
expect(IgnoredChannel.IsChannelIgnored).toHaveBeenCalledWith("channelId");
|
|
||||||
|
|
||||||
expect(IgnoredChannel.FetchOneById).toHaveBeenCalledTimes(1);
|
|
||||||
expect(IgnoredChannel.FetchOneById).toHaveBeenCalledWith(IgnoredChannel, "channelId");
|
|
||||||
|
|
||||||
expect(IgnoredChannel.Remove).toHaveBeenCalledTimes(1);
|
|
||||||
expect(IgnoredChannel.Remove).toHaveBeenCalledWith(IgnoredChannel, {});
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("This channel will start being logged again.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN channel is not currently ignored, EXPECT channel to be added to list", async () => {
|
|
||||||
let savedChannel: IgnoredChannel | undefined;
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
const interaction = {
|
|
||||||
guildId: "guildId",
|
|
||||||
channelId: "channelId",
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
IgnoredChannel.IsChannelIgnored = jest.fn().mockResolvedValue(false);
|
|
||||||
IgnoredChannel.prototype.Save = jest.fn().mockImplementation((_, channel: IgnoredChannel) => {
|
|
||||||
savedChannel = channel;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(IgnoredChannel.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
expect(IgnoredChannel.prototype.Save).toHaveBeenCalledWith(IgnoredChannel, expect.any(IgnoredChannel));
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("This channel will now be ignored from logging.");
|
|
||||||
|
|
||||||
expect(savedChannel).toBeDefined();
|
|
||||||
expect(savedChannel!.Id).toBe("channelId");
|
|
||||||
});
|
|
||||||
});
|
|
File diff suppressed because it is too large
Load diff
815
tests/commands/mute.test.ts
Normal file
815
tests/commands/mute.test.ts
Normal file
|
@ -0,0 +1,815 @@
|
||||||
|
import { mock } from "jest-mock-extended";
|
||||||
|
|
||||||
|
import { GuildMember, Message, Role, TextChannel, User } from "discord.js";
|
||||||
|
import Mute from "../../src/commands/mute";
|
||||||
|
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 Mute();
|
||||||
|
|
||||||
|
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: {
|
||||||
|
add: 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 Mute();
|
||||||
|
|
||||||
|
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.add).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: Test Reason');
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(2);
|
||||||
|
|
||||||
|
// Log Embed
|
||||||
|
const logEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(logEmbed.title).toBe('Member Muted');
|
||||||
|
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`');
|
||||||
|
|
||||||
|
// Log Embed -> Reason Field
|
||||||
|
const logEmbedFieldReason = logEmbed.fields[2];
|
||||||
|
|
||||||
|
expect(logEmbedFieldReason.name).toBe('Reason');
|
||||||
|
expect(logEmbedFieldReason.value).toBe('Test Reason');
|
||||||
|
|
||||||
|
// Public Embed
|
||||||
|
const publicEmbed = result.embeds[1];
|
||||||
|
|
||||||
|
expect(publicEmbed.title).toBe('');
|
||||||
|
expect(publicEmbed.description).toBe('[object Object] has been muted');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given moderator did not supply a reason, expect default reason used', 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: {
|
||||||
|
add: 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'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const mute = new Mute();
|
||||||
|
|
||||||
|
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.add).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: *none*');
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(2);
|
||||||
|
|
||||||
|
// Log Embed
|
||||||
|
const logEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(logEmbed.title).toBe('Member Muted');
|
||||||
|
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`');
|
||||||
|
|
||||||
|
// Log Embed -> Reason Field
|
||||||
|
const logEmbedFieldReason = logEmbed.fields[2];
|
||||||
|
|
||||||
|
expect(logEmbedFieldReason.name).toBe('Reason');
|
||||||
|
expect(logEmbedFieldReason.value).toBe('*none*');
|
||||||
|
|
||||||
|
// Public Embed
|
||||||
|
const publicEmbed = result.embeds[1];
|
||||||
|
|
||||||
|
expect(publicEmbed.title).toBe('');
|
||||||
|
expect(publicEmbed.description).toBe('[object Object] has been muted');
|
||||||
|
});
|
||||||
|
|
||||||
|
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: {
|
||||||
|
add: 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 Mute();
|
||||||
|
|
||||||
|
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: {
|
||||||
|
add: 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 Mute();
|
||||||
|
|
||||||
|
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: {
|
||||||
|
add: 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 Mute();
|
||||||
|
|
||||||
|
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: {
|
||||||
|
add: 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 Mute();
|
||||||
|
|
||||||
|
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: {
|
||||||
|
add: 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 Mute();
|
||||||
|
|
||||||
|
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');
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,242 +1,262 @@
|
||||||
import { CommandInteraction, EmbedBuilder, InteractionResponse, Message, SlashCommandBuilder, SlashCommandStringOption } from "discord.js";
|
import { Message, MessageEmbed } from "discord.js";
|
||||||
import Command from "../../src/commands/poll";
|
import Poll from "../../src/commands/poll";
|
||||||
import EmbedColours from "../../src/constants/EmbedColours";
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test('EXPECT properties to be set', () => {
|
test('Expect properties to be set', () => {
|
||||||
const command = new Command();
|
const poll = new Poll();
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
expect(poll._category).toBe('General');
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("poll");
|
|
||||||
expect(commandBuilder.description).toBe("Run a poll, automatically adding reaction emojis as options");
|
|
||||||
expect(commandBuilder.options.length).toBe(6);
|
|
||||||
|
|
||||||
const commandBuilderTitleOption = commandBuilder.options[0] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderTitleOption.name).toBe("title");
|
|
||||||
expect(commandBuilderTitleOption.description).toBe("Title of the poll");
|
|
||||||
expect(commandBuilderTitleOption.required).toBe(true);
|
|
||||||
|
|
||||||
const commandBuilderOption1Option = commandBuilder.options[1] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderOption1Option.name).toBe("option1");
|
|
||||||
expect(commandBuilderOption1Option.description).toBe("Option 1");
|
|
||||||
expect(commandBuilderOption1Option.required).toBe(true);
|
|
||||||
|
|
||||||
const commandBuilderOption2Option = commandBuilder.options[2] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderOption2Option.name).toBe("option2");
|
|
||||||
expect(commandBuilderOption2Option.description).toBe("Option 2");
|
|
||||||
expect(commandBuilderOption2Option.required).toBe(true);
|
|
||||||
|
|
||||||
const commandBuilderOption3Option = commandBuilder.options[3] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderOption3Option.name).toBe("option3");
|
|
||||||
expect(commandBuilderOption3Option.description).toBe("Option 3");
|
|
||||||
|
|
||||||
const commandBuilderOption4Option = commandBuilder.options[4] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderOption4Option.name).toBe("option4");
|
|
||||||
expect(commandBuilderOption4Option.description).toBe("Option 4");
|
|
||||||
|
|
||||||
const commandBuilderOption5Option = commandBuilder.options[5] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderOption5Option.name).toBe("option5");
|
|
||||||
expect(commandBuilderOption5Option.description).toBe("Option 5");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Execute', () => {
|
describe('Execute', () => {
|
||||||
test("EXPECT a poll to be created", async () => {
|
test('Given input is valid, expect poll to be generated', async () => {
|
||||||
let sentEmbed: EmbedBuilder | undefined;
|
const returnMessageReact = jest.fn();
|
||||||
|
|
||||||
|
const returnMessage = {
|
||||||
|
react: returnMessageReact
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn()
|
||||||
|
.mockReturnValue(returnMessage);
|
||||||
|
const messageDelete = jest.fn();
|
||||||
|
|
||||||
// Arrange
|
|
||||||
const message = {
|
const message = {
|
||||||
react: jest.fn(),
|
channel: {
|
||||||
} as unknown as Message<boolean>;
|
send: messageChannelSend
|
||||||
|
|
||||||
const response = {
|
|
||||||
fetch: jest.fn().mockResolvedValue(message),
|
|
||||||
} as unknown as InteractionResponse<boolean>;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValueOnce({ value: "Title" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 1" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 2" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 3" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 4" })
|
|
||||||
.mockReturnValue({ value: "Option 5" }),
|
|
||||||
},
|
},
|
||||||
reply: jest.fn().mockImplementation((options: any) => {
|
delete: messageDelete,
|
||||||
sentEmbed = options.embeds[0];
|
deletable: true
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
return response;
|
const context: ICommandContext = {
|
||||||
}),
|
name: 'poll',
|
||||||
user: {
|
args: ['Test', 'title;', 'one;', 'two'],
|
||||||
username: "username",
|
message: message
|
||||||
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
|
};
|
||||||
},
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const poll = new Poll();
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
const result = await poll.execute(context);
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(6);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("title", true);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("option1", true);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("option2", true);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("option3");
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("option4");
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("option5");
|
|
||||||
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(messageDelete).toBeCalledTimes(1);
|
||||||
|
expect(returnMessageReact).toBeCalledTimes(2);
|
||||||
|
|
||||||
expect(sentEmbed).toBeDefined();
|
expect(result.embeds.length).toBe(1);
|
||||||
expect(sentEmbed!.data.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(sentEmbed!.data.title).toBe("Title");
|
|
||||||
expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2\n3️⃣ Option 3\n4️⃣ Option 4\n5️⃣ Option 5");
|
|
||||||
expect(sentEmbed!.data.footer).toBeDefined();
|
|
||||||
expect(sentEmbed!.data.footer!.text).toBe("Poll by username");
|
|
||||||
expect(sentEmbed!.data.footer!.icon_url).toBe("https://avatarurl.com/user.png");
|
|
||||||
|
|
||||||
expect(interaction.user.avatarURL).toHaveBeenCalledTimes(1);
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
expect(response.fetch).toHaveBeenCalledTimes(1);
|
expect(embed.title).toBe('Test title');
|
||||||
|
expect(embed.description).toBe(':one: one\n:two: two');
|
||||||
expect(message.react).toHaveBeenCalledTimes(5);
|
|
||||||
expect(message.react).toHaveBeenCalledWith("1️⃣");
|
|
||||||
expect(message.react).toHaveBeenCalledWith("2️⃣");
|
|
||||||
expect(message.react).toHaveBeenCalledWith("3️⃣");
|
|
||||||
expect(message.react).toHaveBeenCalledWith("4️⃣");
|
|
||||||
expect(message.react).toHaveBeenCalledWith("5️⃣");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN option3 is not supplied, EXPECT a 2 option poll to be created", async () => {
|
test('Given message is not deletable by client, expect it not to attempt deletion', async () => {
|
||||||
let sentEmbed: EmbedBuilder | undefined;
|
const returnMessageReact = jest.fn();
|
||||||
|
|
||||||
|
const returnMessage = {
|
||||||
|
react: returnMessageReact
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn()
|
||||||
|
.mockReturnValue(returnMessage);
|
||||||
|
const messageDelete = jest.fn();
|
||||||
|
|
||||||
// Arrange
|
|
||||||
const message = {
|
const message = {
|
||||||
react: jest.fn(),
|
channel: {
|
||||||
} as unknown as Message<boolean>;
|
send: messageChannelSend
|
||||||
|
|
||||||
const response = {
|
|
||||||
fetch: jest.fn().mockResolvedValue(message),
|
|
||||||
} as unknown as InteractionResponse<boolean>;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValueOnce({ value: "Title" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 1" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 2" }),
|
|
||||||
},
|
},
|
||||||
reply: jest.fn().mockImplementation((options: any) => {
|
delete: messageDelete,
|
||||||
sentEmbed = options.embeds[0];
|
deletable: false
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
return response;
|
const context: ICommandContext = {
|
||||||
}),
|
name: 'poll',
|
||||||
user: {
|
args: ['Test', 'title;', 'one;', 'two'],
|
||||||
username: "username",
|
message: message
|
||||||
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
|
};
|
||||||
},
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const poll = new Poll();
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
const result = await poll.execute(context);
|
||||||
expect(sentEmbed).toBeDefined();
|
|
||||||
expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2");
|
|
||||||
|
|
||||||
expect(message.react).toHaveBeenCalledTimes(2);
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(messageDelete).not.toBeCalled();
|
||||||
|
expect(returnMessageReact).toBeCalledTimes(2);
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Test title');
|
||||||
|
expect(embed.description).toBe(':one: one\n:two: two');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN option4 is not supplied, EXPECT a 3 option poll to be created", async () => {
|
test('Given no arguments, expect error embed', async () => {
|
||||||
let sentEmbed: EmbedBuilder | undefined;
|
const returnMessageReact = jest.fn();
|
||||||
|
|
||||||
|
const returnMessage = {
|
||||||
|
react: returnMessageReact
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn()
|
||||||
|
.mockReturnValue(returnMessage);
|
||||||
|
const messageDelete = jest.fn();
|
||||||
|
|
||||||
// Arrange
|
|
||||||
const message = {
|
const message = {
|
||||||
react: jest.fn(),
|
channel: {
|
||||||
} as unknown as Message<boolean>;
|
send: messageChannelSend
|
||||||
|
|
||||||
const response = {
|
|
||||||
fetch: jest.fn().mockResolvedValue(message),
|
|
||||||
} as unknown as InteractionResponse<boolean>;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValueOnce({ value: "Title" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 1" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 2" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 3" }),
|
|
||||||
},
|
},
|
||||||
reply: jest.fn().mockImplementation((options: any) => {
|
delete: messageDelete,
|
||||||
sentEmbed = options.embeds[0];
|
deletable: true
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
return response;
|
const context: ICommandContext = {
|
||||||
}),
|
name: 'poll',
|
||||||
user: {
|
args: [],
|
||||||
username: "username",
|
message: message
|
||||||
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
|
};
|
||||||
},
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
// Act
|
const poll = new Poll();
|
||||||
const command = new Command();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
const result = await poll.execute(context);
|
||||||
expect(sentEmbed).toBeDefined();
|
|
||||||
expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2\n3️⃣ Option 3");
|
|
||||||
|
|
||||||
expect(message.react).toHaveBeenCalledTimes(3);
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(messageDelete).not.toBeCalled();
|
||||||
|
expect(returnMessageReact).not.toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Error Embed
|
||||||
|
const errorEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN option5 is not supplied, EXPECT a 4 option poll to be created", async () => {
|
test('Given only 1 option, expect error embed', async () => {
|
||||||
let sentEmbed: EmbedBuilder | undefined;
|
const returnMessageReact = jest.fn();
|
||||||
|
|
||||||
|
const returnMessage = {
|
||||||
|
react: returnMessageReact
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn()
|
||||||
|
.mockReturnValue(returnMessage);
|
||||||
|
const messageDelete = jest.fn();
|
||||||
|
|
||||||
// Arrange
|
|
||||||
const message = {
|
const message = {
|
||||||
react: jest.fn(),
|
channel: {
|
||||||
} as unknown as Message<boolean>;
|
send: messageChannelSend
|
||||||
|
|
||||||
const response = {
|
|
||||||
fetch: jest.fn().mockResolvedValue(message),
|
|
||||||
} as unknown as InteractionResponse<boolean>;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValueOnce({ value: "Title" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 1" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 2" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 3" })
|
|
||||||
.mockReturnValueOnce({ value: "Option 4" }),
|
|
||||||
},
|
},
|
||||||
reply: jest.fn().mockImplementation((options: any) => {
|
delete: messageDelete,
|
||||||
sentEmbed = options.embeds[0];
|
deletable: true
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
return response;
|
const context: ICommandContext = {
|
||||||
}),
|
name: 'poll',
|
||||||
user: {
|
args: ['Test', 'title;', 'one'],
|
||||||
username: "username",
|
message: message
|
||||||
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
|
};
|
||||||
|
|
||||||
|
const poll = new Poll();
|
||||||
|
|
||||||
|
const result = await poll.execute(context);
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(messageDelete).not.toBeCalled();
|
||||||
|
expect(returnMessageReact).not.toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Error Embed
|
||||||
|
const errorEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given 9 options, expect poll to be generated', async () => {
|
||||||
|
const returnMessageReact = jest.fn();
|
||||||
|
|
||||||
|
const returnMessage = {
|
||||||
|
react: returnMessageReact
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn()
|
||||||
|
.mockReturnValue(returnMessage);
|
||||||
|
const messageDelete = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
},
|
},
|
||||||
} as unknown as CommandInteraction;
|
delete: messageDelete,
|
||||||
|
deletable: true
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
// Act
|
const context: ICommandContext = {
|
||||||
const command = new Command();
|
name: 'poll',
|
||||||
await command.execute(interaction);
|
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
// Assert
|
const poll = new Poll();
|
||||||
expect(sentEmbed).toBeDefined();
|
|
||||||
expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2\n3️⃣ Option 3\n4️⃣ Option 4");
|
|
||||||
|
|
||||||
expect(message.react).toHaveBeenCalledTimes(4);
|
const result = await poll.execute(context);
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(messageDelete).toBeCalledTimes(1);
|
||||||
|
expect(returnMessageReact).toBeCalledTimes(9);
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Test title');
|
||||||
|
expect(embed.description).toBe(':one: one\n:two: two\n:three: three\n:four: four\n:five: five\n:six: six\n:seven: seven\n:eight: eight\n:nine: nine');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given 10 options, expect error embed', async () => {
|
||||||
|
const returnMessageReact = jest.fn();
|
||||||
|
|
||||||
|
const returnMessage = {
|
||||||
|
react: returnMessageReact
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn()
|
||||||
|
.mockReturnValue(returnMessage);
|
||||||
|
const messageDelete = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
delete: messageDelete,
|
||||||
|
deletable: true
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'poll',
|
||||||
|
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine;', 'ten'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const poll = new Poll();
|
||||||
|
|
||||||
|
const result = await poll.execute(context);
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(messageDelete).not.toBeCalled();
|
||||||
|
expect(returnMessageReact).not.toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Error Embed
|
||||||
|
const errorEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
||||||
});
|
});
|
||||||
});
|
});
|
411
tests/commands/role.test.ts
Normal file
411
tests/commands/role.test.ts
Normal file
|
@ -0,0 +1,411 @@
|
||||||
|
import { GuildMemberRoleManager, Message, Role as DiscordRole } from "discord.js";
|
||||||
|
import { mock } from "jest-mock-extended";
|
||||||
|
import Role from "../../src/commands/role";
|
||||||
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties are set', () => {
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
expect(role._category).toBe("General");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Execute', () => {
|
||||||
|
test('Given no arguments were given, expect SendRolesList to be executed', async () => {
|
||||||
|
process.env = {
|
||||||
|
COMMANDS_ROLE_ROLES: 'One,Two'
|
||||||
|
};
|
||||||
|
|
||||||
|
const message = {} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
role.SendRolesList = jest.fn();
|
||||||
|
role.ToggleRole = jest.fn();
|
||||||
|
|
||||||
|
await role.execute(context);
|
||||||
|
|
||||||
|
expect(role.SendRolesList).toBeCalledWith(context, ['One', 'Two']);
|
||||||
|
expect(role.ToggleRole).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given an argument was given, expect ToggleRole to be executed', async () => {
|
||||||
|
process.env = {
|
||||||
|
COMMANDS_ROLE_ROLES: 'One,Two'
|
||||||
|
};
|
||||||
|
|
||||||
|
const message = {} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: ['One'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
role.SendRolesList = jest.fn();
|
||||||
|
role.ToggleRole = jest.fn();
|
||||||
|
|
||||||
|
await role.execute(context);
|
||||||
|
|
||||||
|
expect(role.SendRolesList).not.toBeCalled();
|
||||||
|
expect(role.ToggleRole).toBeCalledWith(context, ['One', 'Two']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendRolesList', () => {
|
||||||
|
test('Expect embed with roles to be sent to the current channel', () => {
|
||||||
|
process.env = {
|
||||||
|
BOT_PREFIX: '!'
|
||||||
|
};
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const roles = ['One', 'Two'];
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
const result = role.SendRolesList(context, roles);
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Roles');
|
||||||
|
expect(embed.description).toBe('Do !role <role> to get the role!\nOne\nTwo');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ToggleRole', () => {
|
||||||
|
test('Given role name is a valid role AND user does not have the role, expect role to be added', async () => {
|
||||||
|
const discordRole = {} as unknown as DiscordRole;
|
||||||
|
|
||||||
|
const messageMemberRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(undefined);
|
||||||
|
const messageGuildRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(discordRole);
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageMemberRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guild: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageGuildRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: ['One'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const roles = ['One', 'Two'];
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
role.AddRole = jest.fn();
|
||||||
|
role.RemoveRole = jest.fn();
|
||||||
|
|
||||||
|
const result = await role.ToggleRole(context, roles);
|
||||||
|
|
||||||
|
expect(messageMemberRolesCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageChannelSend).not.toBeCalled();
|
||||||
|
expect(role.AddRole).toBeCalledWith(context, discordRole);
|
||||||
|
expect(role.RemoveRole).not.toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given role name is a valid role AND user has the role, expect role to be removed', async () => {
|
||||||
|
const discordRole = {} as unknown as DiscordRole;
|
||||||
|
|
||||||
|
const messageMemberRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(discordRole);
|
||||||
|
const messageGuildRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(discordRole);
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageMemberRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guild: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageGuildRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: ['One'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const roles = ['One', 'Two'];
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
role.AddRole = jest.fn();
|
||||||
|
role.RemoveRole = jest.fn();
|
||||||
|
|
||||||
|
const result = await role.ToggleRole(context, roles);
|
||||||
|
|
||||||
|
expect(messageMemberRolesCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageChannelSend).not.toBeCalled();
|
||||||
|
expect(role.AddRole).not.toBeCalled();
|
||||||
|
expect(role.RemoveRole).toBeCalledWith(context, discordRole);
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given role requested is not in the roles array, expect role not assignable error', async () => {
|
||||||
|
const discordRole = {} as unknown as DiscordRole;
|
||||||
|
|
||||||
|
const messageMemberRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(undefined);
|
||||||
|
const messageGuildRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(discordRole);
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageMemberRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guild: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageGuildRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: ['Three'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const roles = ['One', 'Two'];
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
role.AddRole = jest.fn();
|
||||||
|
role.RemoveRole = jest.fn();
|
||||||
|
|
||||||
|
const result = await role.ToggleRole(context, roles);
|
||||||
|
|
||||||
|
expect(messageMemberRolesCacheFind).not.toBeCalled();
|
||||||
|
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(role.AddRole).not.toBeCalled();
|
||||||
|
expect(role.RemoveRole).not.toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Error Embed
|
||||||
|
const errorEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(errorEmbed.description).toBe("This role isn't marked as assignable, to see a list of assignable roles, run this command without any parameters");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given the role is not in the guild, expect error', async () => {
|
||||||
|
const discordRole = {} as unknown as DiscordRole;
|
||||||
|
|
||||||
|
const messageMemberRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(undefined);
|
||||||
|
const messageGuildRolesCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(undefined);
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageMemberRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guild: {
|
||||||
|
roles: {
|
||||||
|
cache: {
|
||||||
|
find: messageGuildRolesCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: ['One'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const roles = ['One', 'Two'];
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
role.AddRole = jest.fn();
|
||||||
|
role.RemoveRole = jest.fn();
|
||||||
|
|
||||||
|
const result = await role.ToggleRole(context, roles);
|
||||||
|
|
||||||
|
expect(messageMemberRolesCacheFind).not.toBeCalled();
|
||||||
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(role.AddRole).not.toBeCalled();
|
||||||
|
expect(role.RemoveRole).not.toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Error Embed
|
||||||
|
const errorEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(errorEmbed.description).toBe("The current server doesn't have this role. Please contact the server's moderators");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('AddRole', () => {
|
||||||
|
test('Expect role to be added to user', async () => {
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const guildMemberRoleManager = mock<GuildMemberRoleManager>();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: guildMemberRoleManager
|
||||||
|
},
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: ['One'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const discordRole = {
|
||||||
|
name: 'One'
|
||||||
|
} as unknown as DiscordRole;
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
const result = await role.AddRole(context, discordRole);
|
||||||
|
|
||||||
|
expect(guildMemberRoleManager.add).toBeCalledWith(discordRole, "Toggled with role command");
|
||||||
|
expect(messageChannelSend).toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('');
|
||||||
|
expect(embed.description).toBe('Gave role: One');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('RemoveRole', () => {
|
||||||
|
test('Expect role to be removed from user', async () => {
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const guildMemberRoleManager = mock<GuildMemberRoleManager>();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
member: {
|
||||||
|
roles: guildMemberRoleManager
|
||||||
|
},
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'role',
|
||||||
|
args: ['One'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const discordRole = {
|
||||||
|
name: 'One'
|
||||||
|
} as unknown as DiscordRole;
|
||||||
|
|
||||||
|
const role = new Role();
|
||||||
|
|
||||||
|
const result = await role.RemoveRole(context, discordRole);
|
||||||
|
|
||||||
|
expect(guildMemberRoleManager.remove).toBeCalledWith(discordRole, "Toggled with role command");
|
||||||
|
expect(messageChannelSend).toBeCalled();
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('');
|
||||||
|
expect(embed.description).toBe('Removed role: One');
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,25 +1,106 @@
|
||||||
import Command from "../../src/commands/rules";
|
import { Message } from "discord.js";
|
||||||
|
import Rules from "../../src/commands/rules";
|
||||||
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
|
|
||||||
|
const oldCwd = process.cwd();
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env = {};
|
process.env = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test('EXPECT properties to be set', () => {
|
test('Expect properties to be set', () => {
|
||||||
const command = new Command();
|
process.env = {
|
||||||
|
ROLES_MODERATOR: "Moderator"
|
||||||
|
};
|
||||||
|
|
||||||
expect(command.CommandBuilder).toMatchSnapshot();
|
const rules = new Rules();
|
||||||
|
|
||||||
|
expect(rules._category).toBe("Admin");
|
||||||
|
expect(rules._roles.length).toBe(1);
|
||||||
|
expect(rules._roles[0]).toBe("Moderator");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Execute', () => {
|
describe('Execute', () => {
|
||||||
test.todo("GIVEN interaction is not a chat input command, EXPECT nothing to happen");
|
test('Given rules exist, expect rules to be sent to current channel', () => {
|
||||||
|
process.env = {
|
||||||
|
COMMANDS_RULES_FILE: 'rules/rules.json'
|
||||||
|
};
|
||||||
|
|
||||||
test.todo("GIVEN invalid subcommand is given, EXPECT invalid error");
|
process.cwd = jest.fn()
|
||||||
});
|
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
||||||
|
|
||||||
describe("access", () => {
|
const messageChannelSend = jest.fn();
|
||||||
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN rules.access.label setting is not supplied, EXPECT label to be defaulted");
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'rules',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const rules = new Rules();
|
||||||
|
|
||||||
|
const result = rules.execute(context);
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(2);
|
||||||
|
expect(result.embeds.length).toBe(2);
|
||||||
|
|
||||||
|
// Header Embed
|
||||||
|
const embedHeader = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embedHeader.title).toBe("");
|
||||||
|
expect(embedHeader.description).toBe("");
|
||||||
|
expect(embedHeader.image?.url).toBe("IMAGEURL");
|
||||||
|
expect(embedHeader.footer?.text).toBe("");
|
||||||
|
|
||||||
|
// Main Embed
|
||||||
|
const embedMain = result.embeds[1];
|
||||||
|
|
||||||
|
expect(embedMain.title).toBe("TITLE 1");
|
||||||
|
expect(embedMain.description).toBe("DESCRIPTION 1A\nDESCRIPTION 1B");
|
||||||
|
expect(embedMain.image?.url).toBe("");
|
||||||
|
expect(embedMain.footer?.text).toBe("FOOTER 1");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given rules file does not exist, expect does not exist error', () => {
|
||||||
|
process.env = {
|
||||||
|
COMMANDS_RULES_FILE: 'rules/none.json'
|
||||||
|
};
|
||||||
|
|
||||||
|
process.cwd = jest.fn()
|
||||||
|
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'rules',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const rules = new Rules();
|
||||||
|
|
||||||
|
const result = rules.execute(context);
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Error Embed
|
||||||
|
const errorEmbed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(errorEmbed.description).toBe("Rules file doesn't exist");
|
||||||
|
});
|
||||||
});
|
});
|
|
@ -1,11 +0,0 @@
|
||||||
describe("constructor", () => {
|
|
||||||
test.todo("EXPECT properties to be set");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('execute', () => {
|
|
||||||
test.todo("EXPECT server to be setup in database");
|
|
||||||
|
|
||||||
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN server is found in the database, EXPECT error");
|
|
||||||
});
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, EmbedBuilder, EmbedField, Guild, GuildMember, InteractionReplyOptions, JSONEncodable, Message, 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";
|
||||||
|
@ -44,8 +44,8 @@ describe('execute', () => {
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
const timeoutFunc = jest.fn();
|
||||||
|
|
||||||
let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
|
|
||||||
const dmChannel = {
|
const dmChannel = {
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||||
|
@ -58,7 +58,6 @@ describe('execute', () => {
|
||||||
id: 'userId',
|
id: 'userId',
|
||||||
tag: 'userTag',
|
tag: 'userTag',
|
||||||
createDM: jest.fn().mockResolvedValue(dmChannel),
|
createDM: jest.fn().mockResolvedValue(dmChannel),
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
|
|
||||||
} as unknown as User,
|
} as unknown as User,
|
||||||
member: {
|
member: {
|
||||||
manageable: true,
|
manageable: true,
|
||||||
|
@ -107,7 +106,7 @@ describe('execute', () => {
|
||||||
await command.execute(interaction);
|
await command.execute(interaction);
|
||||||
|
|
||||||
// EXPECT user to be timed out
|
// EXPECT user to be timed out
|
||||||
expect(timeoutFunc).toHaveBeenCalledWith(1000, 'Test reason');
|
expect(timeoutFunc).toBeCalledWith(1000, 'Test reason');
|
||||||
|
|
||||||
// EXPECT embeds to be sent
|
// EXPECT embeds to be sent
|
||||||
expect(embeds).toBeDefined();
|
expect(embeds).toBeDefined();
|
||||||
|
@ -117,7 +116,6 @@ describe('execute', () => {
|
||||||
const resultEmbed = embeds![0] as EmbedBuilder;
|
const resultEmbed = embeds![0] as EmbedBuilder;
|
||||||
|
|
||||||
expect(resultEmbed.data.description).toBe('<@userId> has been timed out');
|
expect(resultEmbed.data.description).toBe('<@userId> has been timed out');
|
||||||
|
|
||||||
expect(resultEmbed.data.fields).toBeDefined();
|
expect(resultEmbed.data.fields).toBeDefined();
|
||||||
expect(resultEmbed.data.fields!.length).toBe(1);
|
expect(resultEmbed.data.fields!.length).toBe(1);
|
||||||
|
|
||||||
|
@ -156,7 +154,6 @@ describe('execute', () => {
|
||||||
|
|
||||||
expect(logsChannelSentEmbed.color).toBe(EmbedColours.Ok);
|
expect(logsChannelSentEmbed.color).toBe(EmbedColours.Ok);
|
||||||
expect(logsChannelSentEmbed.title).toBe("Member Timed Out");
|
expect(logsChannelSentEmbed.title).toBe("Member Timed Out");
|
||||||
expect(logsChannelSentEmbed.thumbnail.url).toBe("https://google.com/test.png");
|
|
||||||
expect(logsChannelSentEmbed.description).toBe("<@userId> `userTag`");
|
expect(logsChannelSentEmbed.description).toBe("<@userId> `userTag`");
|
||||||
expect(logsChannelSentEmbed.fields?.length).toBe(4);
|
expect(logsChannelSentEmbed.fields?.length).toBe(4);
|
||||||
|
|
||||||
|
@ -418,8 +415,7 @@ describe('execute', () => {
|
||||||
user: {
|
user: {
|
||||||
id: 'userId',
|
id: 'userId',
|
||||||
tag: 'userTag',
|
tag: 'userTag',
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
|
} as User,
|
||||||
} as unknown as User,
|
|
||||||
member: {
|
member: {
|
||||||
manageable: false,
|
manageable: false,
|
||||||
} as GuildMember
|
} as GuildMember
|
||||||
|
@ -486,9 +482,8 @@ describe('execute', () => {
|
||||||
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
||||||
|
|
||||||
return mock<Message<false>>();
|
return mock<Message<false>>();
|
||||||
}),
|
})
|
||||||
}) as unknown as DMChannel,
|
}) as unknown as DMChannel,
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
|
|
||||||
} as unknown as User,
|
} as unknown as User,
|
||||||
member: {
|
member: {
|
||||||
manageable: true,
|
manageable: true,
|
||||||
|
@ -572,7 +567,6 @@ describe('execute', () => {
|
||||||
return mock<Message<false>>();
|
return mock<Message<false>>();
|
||||||
})
|
})
|
||||||
}) as unknown as DMChannel,
|
}) as unknown as DMChannel,
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
|
|
||||||
} as unknown as User,
|
} as unknown as User,
|
||||||
member: {
|
member: {
|
||||||
manageable: true,
|
manageable: true,
|
||||||
|
@ -647,7 +641,6 @@ describe('execute', () => {
|
||||||
return mock<Message<false>>();
|
return mock<Message<false>>();
|
||||||
})
|
})
|
||||||
}) as unknown as DMChannel,
|
}) as unknown as DMChannel,
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
|
|
||||||
} as unknown as User,
|
} as unknown as User,
|
||||||
member: {
|
member: {
|
||||||
manageable: true,
|
manageable: true,
|
||||||
|
@ -697,8 +690,8 @@ describe('execute', () => {
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
const timeoutFunc = jest.fn();
|
||||||
|
|
||||||
let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
|
|
||||||
const dmChannel = {
|
const dmChannel = {
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||||
|
@ -711,7 +704,6 @@ describe('execute', () => {
|
||||||
id: 'userId',
|
id: 'userId',
|
||||||
tag: 'userTag',
|
tag: 'userTag',
|
||||||
createDM: jest.fn().mockRejectedValue(undefined),
|
createDM: jest.fn().mockRejectedValue(undefined),
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
|
|
||||||
} as unknown as User,
|
} as unknown as User,
|
||||||
member: {
|
member: {
|
||||||
manageable: true,
|
manageable: true,
|
||||||
|
@ -771,4 +763,4 @@ describe('execute', () => {
|
||||||
expect(resultEmbedDMField.name).toBe("DM Sent");
|
expect(resultEmbedDMField.name).toBe("DM Sent");
|
||||||
expect(resultEmbedDMField.value).toBe("false");
|
expect(resultEmbedDMField.value).toBe("false");
|
||||||
});
|
});
|
||||||
});
|
});
|
813
tests/commands/unmute.test.ts
Normal file
813
tests/commands/unmute.test.ts
Normal file
|
@ -0,0 +1,813 @@
|
||||||
|
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, 'Moderator: AUTHORTAG, Reason: 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`');
|
||||||
|
|
||||||
|
// Log Embed -> Reason Field
|
||||||
|
const logEmbedFieldReason = logEmbed.fields[2];
|
||||||
|
|
||||||
|
expect(logEmbedFieldReason.name).toBe('Reason');
|
||||||
|
expect(logEmbedFieldReason.value).toBe('Test Reason');
|
||||||
|
|
||||||
|
// Public Embed
|
||||||
|
const publicEmbed = result.embeds[1];
|
||||||
|
|
||||||
|
expect(publicEmbed.title).toBe('');
|
||||||
|
expect(publicEmbed.description).toBe('[object Object] has been unmuted');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given moderator did not supply a reason, expect default reason is used', 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'],
|
||||||
|
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, 'Moderator: AUTHORTAG, Reason: *none*');
|
||||||
|
|
||||||
|
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`');
|
||||||
|
|
||||||
|
// Log Embed -> Reason Field
|
||||||
|
const logEmbedFieldReason = logEmbed.fields[2];
|
||||||
|
|
||||||
|
expect(logEmbedFieldReason.name).toBe('Reason');
|
||||||
|
expect(logEmbedFieldReason.value).toBe('*none*');
|
||||||
|
|
||||||
|
// 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');
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,494 +1,485 @@
|
||||||
import {APIEmbed, APIEmbedField, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandUserOption, TextChannel} from "discord.js";
|
import { GuildMember, Message, TextChannel, User } from "discord.js";
|
||||||
import Warn from "../../src/commands/warn";
|
import Warn from "../../src/commands/warn";
|
||||||
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
||||||
import EmbedColours from "../../src/constants/EmbedColours";
|
|
||||||
import Audit from "../../src/database/entities/Audit";
|
|
||||||
import {AuditType} from "../../src/constants/AuditType";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env = {};
|
process.env = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test('EXPECT values to be set', () => {
|
test('Expect values to be set', () => {
|
||||||
const command = new Warn();
|
process.env.ROLES_MODERATOR = 'Moderator';
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
const warn = new Warn();
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
expect(warn._category).toBe('Moderation');
|
||||||
|
expect(warn._roles.length).toBe(1);
|
||||||
expect(commandBuilder.name).toBe("warn");
|
expect(warn._roles[0]).toBe('Moderator');
|
||||||
expect(commandBuilder.description).toBe("Warns a member in the server with an optional reason")
|
|
||||||
expect(commandBuilder.default_member_permissions).toBe(PermissionsBitField.Flags.ModerateMembers.toString());
|
|
||||||
expect(commandBuilder.options.length).toBe(2);
|
|
||||||
|
|
||||||
const commandBuilderUserOption = commandBuilder.options[0] as SlashCommandUserOption;
|
|
||||||
|
|
||||||
expect(commandBuilderUserOption.name).toBe("target");
|
|
||||||
expect(commandBuilderUserOption.description).toBe("The user");
|
|
||||||
expect(commandBuilderUserOption.required).toBe(true);
|
|
||||||
|
|
||||||
const commandBuilderReasonOption = commandBuilder.options[1] as SlashCommandStringOption;
|
|
||||||
|
|
||||||
expect(commandBuilderReasonOption.name).toBe("reason");
|
|
||||||
expect(commandBuilderReasonOption.description).toBe("The reason");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Execute', () => {
|
describe('Execute', () => {
|
||||||
test("EXPECT user to be warned", async () => {
|
test('Given user has permission, expect user to be warnned', async () => {
|
||||||
let sentEmbeds: EmbedBuilder[] | undefined;
|
process.env = {
|
||||||
let savedAudit: Audit | undefined;
|
ROLES_MODERATOR: 'Moderator',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
// Arrange
|
|
||||||
const targetUser = {
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
tag: "userTag",
|
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
|
|
||||||
},
|
|
||||||
member: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: "Test reason",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mentionedUser = {
|
||||||
|
displayAvatarURL: jest.fn(),
|
||||||
|
tag: 'USERTAG'
|
||||||
|
} as unknown as User;
|
||||||
|
const mentionedMember = {
|
||||||
|
warnnable: true,
|
||||||
|
warn: jest.fn()
|
||||||
|
} as unknown as GuildMember;
|
||||||
const logChannel = {
|
const logChannel = {
|
||||||
send: jest.fn().mockImplementation((opts: any) => {
|
name: 'mod-logs',
|
||||||
sentEmbeds = opts.embeds;
|
send: jest.fn()
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const interaction = {
|
const messageChannelSend = jest.fn();
|
||||||
reply: jest.fn(),
|
const messageMentionsUsersFirst = jest.fn()
|
||||||
options: {
|
.mockReturnValue(mentionedUser);
|
||||||
get: jest.fn()
|
const messageGuildMember = jest.fn()
|
||||||
.mockReturnValueOnce(targetUser)
|
.mockReturnValue(mentionedMember);
|
||||||
.mockReturnValue(reason),
|
const messageGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockImplementation((callback): TextChannel | undefined => {
|
||||||
|
const result = callback(logChannel);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logChannel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
mentions: {
|
||||||
|
users: {
|
||||||
|
first: messageMentionsUsersFirst
|
||||||
|
}
|
||||||
},
|
},
|
||||||
guild: {
|
guild: {
|
||||||
|
member: messageGuildMember ,
|
||||||
channels: {
|
channels: {
|
||||||
cache: {
|
cache: {
|
||||||
find: jest.fn().mockReturnValue(logChannel),
|
find: messageGuildChannelsCacheFind
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
|
available: true
|
||||||
},
|
},
|
||||||
guildId: "guildId",
|
author: {
|
||||||
user: {
|
tag: 'AUTHORTAG'
|
||||||
id: "moderatorId",
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
const context: ICommandContext = {
|
||||||
|
name: 'warn',
|
||||||
|
args: ['warn', 'Test', 'Reason'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
const warn = new Warn();
|
||||||
savedAudit = audit;
|
|
||||||
});
|
const result = await warn.execute(context);
|
||||||
|
|
||||||
// Act
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
const command = new Warn();
|
expect(logChannel.send).toBeCalledTimes(1);
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
|
|
||||||
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledTimes(2);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("target", true);
|
|
||||||
expect(interaction.options.get).toHaveBeenCalledWith("reason");
|
|
||||||
|
|
||||||
expect(interaction.guild!.channels.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledTimes(1);
|
|
||||||
expect(SettingsHelper.GetSetting).toHaveBeenCalledWith("channels.logs.mod", "guildId");
|
|
||||||
|
|
||||||
expect(targetUser.user.avatarURL).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(logChannel.send).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(sentEmbeds).toBeDefined();
|
|
||||||
expect(sentEmbeds).toMatchSnapshot("sentEmbeds");
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).toHaveBeenCalledWith(Audit, expect.any(Audit));
|
|
||||||
|
|
||||||
expect(savedAudit).toMatchSnapshot({
|
|
||||||
Id: expect.any(String),
|
|
||||||
AuditId: expect.any(String),
|
|
||||||
WhenCreated: expect.any(Date),
|
|
||||||
WhenUpdated: expect.any(Date)
|
|
||||||
}, "savedAudit");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
|
test('Given user has permissions, expect embeds to be correct', async () => {
|
||||||
let sentEmbeds: EmbedBuilder[] | undefined;
|
process.env = {
|
||||||
let savedAudit: Audit | undefined;
|
ROLES_MODERATOR: 'Moderator',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
// Arrange
|
|
||||||
const targetUser = {
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
tag: "userTag",
|
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
|
|
||||||
},
|
|
||||||
member: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: "Test reason",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mentionedUser = {
|
||||||
|
displayAvatarURL: jest.fn(),
|
||||||
|
tag: 'USERTAG'
|
||||||
|
} as unknown as User;
|
||||||
|
const mentionedMember = {
|
||||||
|
warnnable: true,
|
||||||
|
warn: jest.fn()
|
||||||
|
} as unknown as GuildMember;
|
||||||
const logChannel = {
|
const logChannel = {
|
||||||
send: jest.fn().mockImplementation((opts: any) => {
|
name: 'mod-logs',
|
||||||
sentEmbeds = opts.embeds;
|
send: jest.fn()
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const interaction = {
|
const messageChannelSend = jest.fn();
|
||||||
reply: jest.fn(),
|
const messageMentionsUsersFirst = jest.fn()
|
||||||
options: {
|
.mockReturnValue(mentionedUser);
|
||||||
get: jest.fn()
|
const messageGuildMember = jest.fn()
|
||||||
.mockReturnValueOnce(targetUser)
|
.mockReturnValue(mentionedMember);
|
||||||
.mockReturnValue(reason),
|
const messageGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockImplementation((callback): TextChannel | undefined => {
|
||||||
|
const result = callback(logChannel);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logChannel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
},
|
},
|
||||||
guildId: "guildId",
|
mentions: {
|
||||||
user: {
|
users: {
|
||||||
id: "moderatorId",
|
first: messageMentionsUsersFirst
|
||||||
},
|
}
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
|
||||||
savedAudit = audit;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Warn();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
||||||
let sentEmbeds: EmbedBuilder[] | undefined;
|
|
||||||
let savedAudit: Audit | undefined;
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
const targetUser = {
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
tag: "userTag",
|
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
|
|
||||||
},
|
|
||||||
member: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: "Test reason",
|
|
||||||
};
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
send: jest.fn().mockImplementation((opts: any) => {
|
|
||||||
sentEmbeds = opts.embeds;
|
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(targetUser)
|
|
||||||
.mockReturnValue(reason),
|
|
||||||
},
|
},
|
||||||
guild: {
|
guild: {
|
||||||
|
member: messageGuildMember ,
|
||||||
channels: {
|
channels: {
|
||||||
cache: {
|
cache: {
|
||||||
find: jest.fn().mockReturnValue(logChannel),
|
find: messageGuildChannelsCacheFind
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
|
available: true
|
||||||
},
|
},
|
||||||
user: {
|
author: {
|
||||||
id: "moderatorId",
|
tag: 'AUTHORTAG'
|
||||||
},
|
}
|
||||||
} as unknown as CommandInteraction;
|
} as unknown as Message;
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
const context: ICommandContext = {
|
||||||
|
name: 'warn',
|
||||||
|
args: ['warn', 'Test', 'Reason'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
const warn = new Warn();
|
||||||
savedAudit = audit;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Warn();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
const result = await warn.execute(context);
|
||||||
expect(interaction.reply).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).not.toHaveBeenCalled();
|
expect(result.embeds.length).toBe(2);
|
||||||
|
|
||||||
|
const logEmbed = result.embeds[0];
|
||||||
|
const publicEmbed = result.embeds[1];
|
||||||
|
|
||||||
|
expect(logEmbed.title).toBe('Member Warned');
|
||||||
|
expect(publicEmbed.title).toBe("");
|
||||||
|
expect(publicEmbed.description).toBe('[object Object] has been warned');
|
||||||
|
expect(logEmbed.fields.length).toBe(3);
|
||||||
|
expect(publicEmbed.fields.length).toBe(1);
|
||||||
|
expect(publicEmbed.fields[0].name).toBe('Reason');
|
||||||
|
expect(publicEmbed.fields[0].value).toBe('Test Reason');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN reasonInput is null, EXPECT reason to be defaulted", async () => {
|
test('Given user has permission, expect logEmbed fields to be correct', async () => {
|
||||||
let sentEmbeds: EmbedBuilder[] | undefined;
|
process.env = {
|
||||||
let savedAudit: Audit | undefined;
|
ROLES_MODERATOR: 'Moderator',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
// Arrange
|
|
||||||
const targetUser = {
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
tag: "userTag",
|
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
|
|
||||||
},
|
|
||||||
member: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: "Test reason",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mentionedUser = {
|
||||||
|
displayAvatarURL: jest.fn().mockReturnValue('URL'),
|
||||||
|
tag: 'USERTAG'
|
||||||
|
} as unknown as User;
|
||||||
|
const mentionedMember = {
|
||||||
|
warnnable: true,
|
||||||
|
warn: jest.fn()
|
||||||
|
} as unknown as GuildMember;
|
||||||
const logChannel = {
|
const logChannel = {
|
||||||
send: jest.fn().mockImplementation((opts: any) => {
|
name: 'mod-logs',
|
||||||
sentEmbeds = opts.embeds;
|
send: jest.fn()
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const interaction = {
|
const messageChannelSend = jest.fn();
|
||||||
reply: jest.fn(),
|
const messageMentionsUsersFirst = jest.fn()
|
||||||
options: {
|
.mockReturnValue(mentionedUser);
|
||||||
get: jest.fn()
|
const messageGuildMember = jest.fn()
|
||||||
.mockReturnValueOnce(targetUser)
|
.mockReturnValue(mentionedMember);
|
||||||
.mockReturnValue(null),
|
const messageGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockImplementation((callback): TextChannel | undefined => {
|
||||||
|
const result = callback(logChannel);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logChannel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
mentions: {
|
||||||
|
users: {
|
||||||
|
first: messageMentionsUsersFirst
|
||||||
|
}
|
||||||
},
|
},
|
||||||
guild: {
|
guild: {
|
||||||
|
member: messageGuildMember ,
|
||||||
channels: {
|
channels: {
|
||||||
cache: {
|
cache: {
|
||||||
find: jest.fn().mockReturnValue(logChannel),
|
find: messageGuildChannelsCacheFind
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
|
available: true
|
||||||
},
|
},
|
||||||
guildId: "guildId",
|
author: {
|
||||||
user: {
|
tag: 'AUTHORTAG'
|
||||||
id: "moderatorId",
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
const context: ICommandContext = {
|
||||||
|
name: 'warn',
|
||||||
|
args: ['warn', 'Test', 'Reason'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
const warn = new Warn();
|
||||||
savedAudit = audit;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const command = new Warn();
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
const result = await warn.execute(context);
|
||||||
expect(sentEmbeds).toBeDefined();
|
|
||||||
|
|
||||||
expect(sentEmbeds![0].data.fields).toBeDefined();
|
const logEmbed = result.embeds[0];
|
||||||
expect(sentEmbeds![0].data.fields!.length).toBe(2);
|
|
||||||
|
|
||||||
const logEmbedReasonField = sentEmbeds![0].data.fields!.find(x => x.name == "Reason");
|
const fieldUser = logEmbed.fields[0];
|
||||||
|
const fieldModerator = logEmbed.fields[1];
|
||||||
|
const fieldReason = logEmbed.fields[2];
|
||||||
|
|
||||||
expect(logEmbedReasonField).toBeDefined();
|
expect(fieldUser.name).toBe("User");
|
||||||
expect(logEmbedReasonField!.value).toBe("*none*");
|
expect(fieldUser.value).toBe("[object Object] `USERTAG`");
|
||||||
|
expect(logEmbed.thumbnail?.url).toBe("URL");
|
||||||
|
|
||||||
|
expect(fieldModerator.name).toBe('Moderator');
|
||||||
|
expect(fieldModerator.value).toBe('[object Object] `AUTHORTAG`');
|
||||||
|
|
||||||
|
expect(fieldReason.name).toBe('Reason');
|
||||||
|
expect(fieldReason.value).toBe('Test Reason');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN reasonInput.value is undefined, EXPECT reason to be defaulted", async () => {
|
test('Given user is not mentioned, expect error embed to be sent', async () => {
|
||||||
let sentEmbeds: EmbedBuilder[] | undefined;
|
process.env = {
|
||||||
let savedAudit: Audit | undefined;
|
ROLES_MODERATOR: 'Moderator',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
// Arrange
|
|
||||||
const targetUser = {
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
tag: "userTag",
|
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
|
|
||||||
},
|
|
||||||
member: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: undefined,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mentionedMember = {
|
||||||
|
warnnable: true,
|
||||||
|
warn: jest.fn()
|
||||||
|
} as unknown as GuildMember;
|
||||||
const logChannel = {
|
const logChannel = {
|
||||||
send: jest.fn().mockImplementation((opts: any) => {
|
name: 'mod-logs',
|
||||||
sentEmbeds = opts.embeds;
|
send: jest.fn()
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const interaction = {
|
const messageChannelSend = jest.fn();
|
||||||
reply: jest.fn(),
|
const messageMentionsUsersFirst = jest.fn()
|
||||||
options: {
|
.mockReturnValue(null);
|
||||||
get: jest.fn()
|
const messageGuildMember = jest.fn()
|
||||||
.mockReturnValueOnce(targetUser)
|
.mockReturnValue(mentionedMember);
|
||||||
.mockReturnValue(reason),
|
const messageGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockImplementation((callback): TextChannel | undefined => {
|
||||||
|
const result = callback(logChannel);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logChannel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
mentions: {
|
||||||
|
users: {
|
||||||
|
first: messageMentionsUsersFirst
|
||||||
|
}
|
||||||
},
|
},
|
||||||
guild: {
|
guild: {
|
||||||
|
member: messageGuildMember ,
|
||||||
channels: {
|
channels: {
|
||||||
cache: {
|
cache: {
|
||||||
find: jest.fn().mockReturnValue(logChannel),
|
find: messageGuildChannelsCacheFind
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
|
available: true
|
||||||
},
|
},
|
||||||
guildId: "guildId",
|
author: {
|
||||||
user: {
|
tag: 'AUTHORTAG'
|
||||||
id: "moderatorId",
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
const context: ICommandContext = {
|
||||||
|
name: 'warn',
|
||||||
|
args: ['warn', 'Test', 'Reason'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
const warn = new Warn();
|
||||||
savedAudit = audit;
|
|
||||||
});
|
const result = await warn.execute(context);
|
||||||
|
|
||||||
// Act
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
const command = new Warn();
|
expect(logChannel.send).not.toBeCalled();
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
expect(result.embeds.length).toBe(1);
|
||||||
expect(sentEmbeds).toBeDefined();
|
|
||||||
|
|
||||||
expect(sentEmbeds![0].data.fields).toBeDefined();
|
const embedError = result.embeds[0];
|
||||||
expect(sentEmbeds![0].data.fields!.length).toBe(2);
|
|
||||||
|
|
||||||
const logEmbedReasonField = sentEmbeds![0].data.fields!.find(x => x.name == "Reason");
|
expect(embedError.description).toBe('User does not exist');
|
||||||
|
|
||||||
expect(logEmbedReasonField).toBeDefined();
|
|
||||||
expect(logEmbedReasonField!.value).toBe("*none*");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN channels.logs.mod setting is not found, EXPECT command to return", async () => {
|
test('Given member is not in server, expect error embed to be sent', async () => {
|
||||||
let sentEmbeds: EmbedBuilder[] | undefined;
|
process.env = {
|
||||||
let savedAudit: Audit | undefined;
|
ROLES_MODERATOR: 'Moderator',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
// Arrange
|
|
||||||
const targetUser = {
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
tag: "userTag",
|
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
|
|
||||||
},
|
|
||||||
member: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: "Test reason",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mentionedUser = {
|
||||||
|
displayAvatarURL: jest.fn(),
|
||||||
|
tag: 'USERTAG'
|
||||||
|
} as unknown as User;
|
||||||
|
const mentionedMember = {
|
||||||
|
warnnable: true,
|
||||||
|
warn: jest.fn()
|
||||||
|
} as unknown as GuildMember;
|
||||||
const logChannel = {
|
const logChannel = {
|
||||||
send: jest.fn().mockImplementation((opts: any) => {
|
name: 'mod-logs',
|
||||||
sentEmbeds = opts.embeds;
|
send: jest.fn()
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const interaction = {
|
const messageChannelSend = jest.fn();
|
||||||
reply: jest.fn(),
|
const messageMentionsUsersFirst = jest.fn()
|
||||||
options: {
|
.mockReturnValue(mentionedUser);
|
||||||
get: jest.fn()
|
const messageGuildMember = jest.fn()
|
||||||
.mockReturnValueOnce(targetUser)
|
.mockReturnValue(null);
|
||||||
.mockReturnValue(reason),
|
const messageGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockImplementation((callback): TextChannel | undefined => {
|
||||||
|
const result = callback(logChannel);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logChannel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
mentions: {
|
||||||
|
users: {
|
||||||
|
first: messageMentionsUsersFirst
|
||||||
|
}
|
||||||
},
|
},
|
||||||
guild: {
|
guild: {
|
||||||
|
member: messageGuildMember ,
|
||||||
channels: {
|
channels: {
|
||||||
cache: {
|
cache: {
|
||||||
find: jest.fn().mockReturnValue(logChannel),
|
find: messageGuildChannelsCacheFind
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
|
available: true
|
||||||
},
|
},
|
||||||
guildId: "guildId",
|
author: {
|
||||||
user: {
|
tag: 'AUTHORTAG'
|
||||||
id: "moderatorId",
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
|
const context: ICommandContext = {
|
||||||
|
name: 'warn',
|
||||||
|
args: ['warn', 'Test', 'Reason'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
const warn = new Warn();
|
||||||
savedAudit = audit;
|
|
||||||
});
|
const result = await warn.execute(context);
|
||||||
|
|
||||||
// Act
|
expect(messageChannelSend).toBeCalledTimes(1);
|
||||||
const command = new Warn();
|
expect(logChannel.send).not.toBeCalled();
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// Assert
|
expect(result.embeds.length).toBe(1);
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
|
const embedError = result.embeds[0];
|
||||||
|
|
||||||
expect(logChannel.send).not.toHaveBeenCalled();
|
expect(embedError.description).toBe('User is not in this server');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("GIVEN channel is not found, EXPECT logEmbed to not be sent", async () => {
|
test('Given guild is unavailable, expect return and do nothing', async () => {
|
||||||
let sentEmbeds: EmbedBuilder[] | undefined;
|
process.env = {
|
||||||
let savedAudit: Audit | undefined;
|
ROLES_MODERATOR: 'Moderator',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
// Arrange
|
|
||||||
const targetUser = {
|
|
||||||
user: {
|
|
||||||
id: "userId",
|
|
||||||
tag: "userTag",
|
|
||||||
avatarURL: jest.fn().mockReturnValue("https://google.com/avatar.png"),
|
|
||||||
},
|
|
||||||
member: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
const reason = {
|
|
||||||
value: "Test reason",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mentionedUser = {
|
||||||
|
displayAvatarURL: jest.fn(),
|
||||||
|
tag: 'USERTAG'
|
||||||
|
} as unknown as User;
|
||||||
|
const mentionedMember = {
|
||||||
|
warnnable: true,
|
||||||
|
warn: jest.fn()
|
||||||
|
} as unknown as GuildMember;
|
||||||
const logChannel = {
|
const logChannel = {
|
||||||
send: jest.fn().mockImplementation((opts: any) => {
|
name: 'mod-logs',
|
||||||
sentEmbeds = opts.embeds;
|
send: jest.fn()
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const interaction = {
|
const messageChannelSend = jest.fn();
|
||||||
reply: jest.fn(),
|
const messageMentionsUsersFirst = jest.fn()
|
||||||
options: {
|
.mockReturnValue(mentionedUser);
|
||||||
get: jest.fn()
|
const messageGuildMember = jest.fn()
|
||||||
.mockReturnValueOnce(targetUser)
|
.mockReturnValue(mentionedMember);
|
||||||
.mockReturnValue(reason),
|
const messageGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockImplementation((callback): TextChannel | undefined => {
|
||||||
|
const result = callback(logChannel);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return logChannel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
mentions: {
|
||||||
|
users: {
|
||||||
|
first: messageMentionsUsersFirst
|
||||||
|
}
|
||||||
},
|
},
|
||||||
guild: {
|
guild: {
|
||||||
|
member: messageGuildMember ,
|
||||||
channels: {
|
channels: {
|
||||||
cache: {
|
cache: {
|
||||||
find: jest.fn().mockReturnValue(undefined),
|
find: messageGuildChannelsCacheFind
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
|
available: false
|
||||||
},
|
},
|
||||||
guildId: "guildId",
|
author: {
|
||||||
user: {
|
tag: 'AUTHORTAG'
|
||||||
id: "moderatorId",
|
}
|
||||||
},
|
} as unknown as Message;
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
const context: ICommandContext = {
|
||||||
|
name: 'warn',
|
||||||
|
args: ['warn', 'Test', 'Reason'],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
const warn = new Warn();
|
||||||
savedAudit = audit;
|
|
||||||
});
|
const result = await warn.execute(context);
|
||||||
|
|
||||||
// Act
|
expect(messageChannelSend).not.toBeCalled();
|
||||||
const command = new Warn();
|
expect(logChannel.send).not.toBeCalled();
|
||||||
await command.execute(interaction);
|
expect(result.embeds.length).toBe(0);
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
|
|
||||||
|
|
||||||
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(interaction.guild!.channels.cache.find).toHaveBeenCalledTimes(1);
|
|
||||||
|
|
||||||
expect(logChannel.send).not.toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -1,53 +0,0 @@
|
||||||
import BaseEntity from "../../src/contracts/BaseEntity";
|
|
||||||
import uuid from "uuid";
|
|
||||||
|
|
||||||
jest.mock("uuid", () => {
|
|
||||||
return {
|
|
||||||
v4: () => "uuidv4",
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
jest.useFakeTimers();
|
|
||||||
|
|
||||||
describe('constructor', () => {
|
|
||||||
test("EXPECT properties to be set", () => {
|
|
||||||
// Arrange
|
|
||||||
const systemTime = new Date("2024-06-29T00:00:00.000Z");
|
|
||||||
|
|
||||||
jest.setSystemTime(systemTime);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const entity = new BaseEntity();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(entity.Id).toBe("uuidv4");
|
|
||||||
expect(entity.WhenCreated).toStrictEqual(systemTime);
|
|
||||||
expect(entity.WhenUpdated).toStrictEqual(systemTime);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Save", () => {
|
|
||||||
test.todo("EXPECT entity to be saved");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Remove", () => {
|
|
||||||
test.todo("EXPECT entity to be removed");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchAll", () => {
|
|
||||||
test.todo("EXPECT all entities to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchOneById", () => {
|
|
||||||
test.todo("EXPECT one entity to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Any", () => {
|
|
||||||
test.todo("GIVEN at least 1 entity is found, EXPECT true to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN no entities are found, EXPECT false to be returned");
|
|
||||||
});
|
|
|
@ -1,13 +0,0 @@
|
||||||
describe("constructor", () => {
|
|
||||||
test.todo("EXPECT properties to be set");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("MarkAsUsed", () => {
|
|
||||||
test.todo("EXPECT LastUsed to be updated");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchOneByChannelId", () => {
|
|
||||||
test.todo("EXPECT entity with channel id to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
|
|
||||||
});
|
|
|
@ -1,11 +0,0 @@
|
||||||
describe("constructor", () => {
|
|
||||||
test.todo("EXPECT properties to be set");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchAuditsByUserId", () => {
|
|
||||||
test.todo("EXPECT all entities with the user id and server id to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchAuditByAuditId", () => {
|
|
||||||
test.todo("EXPECT audit with audit id to be returned");
|
|
||||||
});
|
|
|
@ -1,7 +0,0 @@
|
||||||
describe("constructor", () => {
|
|
||||||
test.todo("EXPECT properties to be defined");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("IsChannelIgnored", () => {
|
|
||||||
test.todo("EXPECT entity with channel id to be returned");
|
|
||||||
});
|
|
|
@ -1,13 +0,0 @@
|
||||||
describe("constructor", () => {
|
|
||||||
test.todo("EXPECT properties to be set");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchOneByRoleId", () => {
|
|
||||||
test.todo("EXPECT entity with role id to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchAllByServerId", () => {
|
|
||||||
test.todo("EXPECT all entities with server id to be returned");
|
|
||||||
});
|
|
|
@ -1,11 +0,0 @@
|
||||||
describe("constructor", () => {
|
|
||||||
test.todo("EXPECT properties to be set");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("AddSettingToServer", () => {
|
|
||||||
test.todo("EXPECT setting to be pushed to server relationship");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("AddRoleToServer", () => {
|
|
||||||
test.todo("EXPECT role to be pushed to server relationship");
|
|
||||||
});
|
|
|
@ -1,13 +0,0 @@
|
||||||
describe("constructor", () => {
|
|
||||||
test.todo("EXPECT properties to be set");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("UpdateBasicDetails", () => {
|
|
||||||
test.todo("EXPECT properties to be updated");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("FetchOneByKey", () => {
|
|
||||||
test.todo("EXPECT setting with key to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN relations parameter is not supplied, EXPECT relations to be defaulted");
|
|
||||||
});
|
|
164
tests/events/MemberEvents.test.ts
Normal file
164
tests/events/MemberEvents.test.ts
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
import { GuildMember, TextChannel, User } from "discord.js";
|
||||||
|
import MemberEvents from "../../src/events/MemberEvents";
|
||||||
|
import GuildMemberUpdate from "../../src/events/MemberEvents/GuildMemberUpdate";
|
||||||
|
|
||||||
|
describe('GuildMemberAdd', () => {
|
||||||
|
test('When event is fired, expect embed to be sent to logs channel', async () => {
|
||||||
|
const currentDate = new Date();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
send: jest.fn()
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const userDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const guildUser = {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
createdAt: currentDate,
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: userDisplayAvatarURL
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const guildMember = {
|
||||||
|
user: guildUser,
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = await memberEvents.guildMemberAdd(guildMember);
|
||||||
|
|
||||||
|
expect(textChannel.send).toBeCalledTimes(1);
|
||||||
|
expect(userDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe("Member Joined");
|
||||||
|
expect(embed.footer?.text).toBe("Id: USERID");
|
||||||
|
expect(embed.fields.length).toBe(2);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe("User");
|
||||||
|
expect(embedFieldUser.value).toBe("[object Object] `USERTAG`");
|
||||||
|
expect(embedFieldUser.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Created Field
|
||||||
|
const embedFieldCreated = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldCreated.name).toBe("Created");
|
||||||
|
expect(embedFieldCreated.value).toBe(currentDate.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GuildMemberRemove', () => {
|
||||||
|
test('When event is fired, expect embed to be sent to logs channel', async () => {
|
||||||
|
const currentDate = new Date();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
send: jest.fn()
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const userDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const guildUser = {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
createdAt: currentDate,
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: userDisplayAvatarURL
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const guildMember = {
|
||||||
|
user: guildUser,
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
joinedAt: currentDate
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = await memberEvents.guildMemberRemove(guildMember);
|
||||||
|
|
||||||
|
expect(textChannel.send).toBeCalledTimes(1);
|
||||||
|
expect(userDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe("Member Left");
|
||||||
|
expect(embed.footer?.text).toBe("Id: USERID");
|
||||||
|
expect(embed.fields.length).toBe(2);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe("User");
|
||||||
|
expect(embedFieldUser.value).toBe("[object Object] `USERTAG`");
|
||||||
|
expect(embedFieldUser.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Joined Field
|
||||||
|
const embedFieldJoined = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldJoined.name).toBe("Joined");
|
||||||
|
expect(embedFieldJoined.value).toBe(currentDate.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GuildMemberUpdate', () => {
|
||||||
|
test('Given nicknames are the same, expect NicknameChanged NOT to be called', async () => {
|
||||||
|
const member = {
|
||||||
|
nickname: 'member'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const nicknameChanged = jest.fn();
|
||||||
|
|
||||||
|
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = await memberEvents.guildMemberUpdate(member, member);
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
expect(nicknameChanged).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given nicknames are the different, expect NicknameChanged to be called', async () => {
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'oldMember'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'newMember'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const nicknameChanged = jest.fn();
|
||||||
|
|
||||||
|
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
|
||||||
|
|
||||||
|
const memberEvents = new MemberEvents();
|
||||||
|
|
||||||
|
const result = await memberEvents.guildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
expect(nicknameChanged).toBeCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,13 +0,0 @@
|
||||||
describe('GuildMemberAdd', () => {
|
|
||||||
test.todo('EXPECT embed to be logged');
|
|
||||||
|
|
||||||
test.todo("GIVEN member.guild is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.member.add.enabled setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.member.add.channel setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
|
|
||||||
});
|
|
|
@ -1,15 +0,0 @@
|
||||||
describe('GuildMemberRemove', () => {
|
|
||||||
test.todo("EXPECT embed to be logged");
|
|
||||||
|
|
||||||
test.todo("GIVEN member.guild is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.member.remove.enabled setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN member.joinedAt is null, EXPECT value to be defaulted");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.member.remove.channel setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
|
|
||||||
});
|
|
|
@ -1,5 +1,235 @@
|
||||||
describe('GuildMemberUpdate', () => {
|
import { GuildMember, TextChannel } from "discord.js";
|
||||||
test.todo("GIVEN user nickname has changed, EXPECT function to be executed");
|
import GuildMemberUpdate from "../../../src/events/MemberEvents/GuildMemberUpdate";
|
||||||
|
|
||||||
test.todo("GIVEN nickname is the same, EXPECT nothing to happen");
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties are set', () => {
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'Old Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'New Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
expect(guildMemberUpdate.oldMember).toBe(oldMember);
|
||||||
|
expect(guildMemberUpdate.newMember).toBe(newMember);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('NicknameChanged', () => {
|
||||||
|
test('Given nickname has changed from one to another, expect embed to be sent with both', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const memberUserDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'Old Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'New Nickname',
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: memberUserDisplayAvatarURL
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
const result = await guildMemberUpdate.NicknameChanged();
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Nickname Changed');
|
||||||
|
expect(embed.footer?.text).toBe('Id: USERID');
|
||||||
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('Old Nickname');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('New Nickname');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given old nickname was null, expect embed to say old nickname was none', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const memberUserDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMember = {} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
nickname: 'New Nickname',
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: memberUserDisplayAvatarURL
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
const result = await guildMemberUpdate.NicknameChanged();
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Nickname Changed');
|
||||||
|
expect(embed.footer?.text).toBe('Id: USERID');
|
||||||
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('*none*');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('New Nickname');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given new nickname was null, expect embed to say new nickname was none', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const memberUserDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMember = {
|
||||||
|
nickname: 'Old Nickname'
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const newMember = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
id: 'USERID',
|
||||||
|
displayAvatarURL: memberUserDisplayAvatarURL
|
||||||
|
}
|
||||||
|
} as unknown as GuildMember;
|
||||||
|
|
||||||
|
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
||||||
|
|
||||||
|
const result = await guildMemberUpdate.NicknameChanged();
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Nickname Changed');
|
||||||
|
expect(embed.footer?.text).toBe('Id: USERID');
|
||||||
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('Old Nickname');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('*none*');
|
||||||
|
});
|
||||||
});
|
});
|
|
@ -1,15 +0,0 @@
|
||||||
describe('NicknameChanged', () => {
|
|
||||||
test.todo('EXPECT embed to be logged');
|
|
||||||
|
|
||||||
test.todo("GIVEN event.member.update.enabled setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN oldNickname is null, EXPECT oldNickname to be defaulted");
|
|
||||||
|
|
||||||
test.todo("GIVEN newNickname is null, EXPECT newNickname to be defaulted");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.member.update.channel setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN log channel can not be found, EXPECT nothing to happen");
|
|
||||||
});
|
|
648
tests/events/MessageEvents.test.ts
Normal file
648
tests/events/MessageEvents.test.ts
Normal file
|
@ -0,0 +1,648 @@
|
||||||
|
import { Collection, Message, MessageAttachment, TextChannel } from "discord.js";
|
||||||
|
import MessageEvents from "../../src/events/MessageEvents";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('MessageDelete', () => {
|
||||||
|
test('Given message was in a guild AND user was NOT a bot, expect message deleted embed to be sent', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const messageAttachments = new Collection<string, MessageAttachment>([
|
||||||
|
[
|
||||||
|
"0",
|
||||||
|
{
|
||||||
|
url: 'image0.png'
|
||||||
|
} as unknown as MessageAttachment
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"1",
|
||||||
|
{
|
||||||
|
url: 'image1.png'
|
||||||
|
} as unknown as MessageAttachment
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
channel: {},
|
||||||
|
content: 'Message Content',
|
||||||
|
attachments: messageAttachments
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageDelete(message);
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Message Deleted');
|
||||||
|
expect(embed.fields.length).toBe(4);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Channel Field
|
||||||
|
const embedFieldChannel = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldChannel.name).toBe('Channel');
|
||||||
|
expect(embedFieldChannel.value).toBe('[object Object]');
|
||||||
|
|
||||||
|
// Embed -> Content Field
|
||||||
|
const embedFieldContent = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldContent.name).toBe('Content');
|
||||||
|
expect(embedFieldContent.value).toBe('```Message Content```');
|
||||||
|
|
||||||
|
// Embed -> Attachments Field
|
||||||
|
const embedFieldAttachments = embed.fields[3];
|
||||||
|
|
||||||
|
expect(embedFieldAttachments.name).toBe('Attachments');
|
||||||
|
expect(embedFieldAttachments.value).toBe('```image0.png\nimage1.png```');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given message was not sent in a guild, expect execution stopped', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const messageAttachments = new Collection<string, MessageAttachment>([
|
||||||
|
[
|
||||||
|
"0",
|
||||||
|
{
|
||||||
|
url: 'image0.png'
|
||||||
|
} as unknown as MessageAttachment
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"1",
|
||||||
|
{
|
||||||
|
url: 'image1.png'
|
||||||
|
} as unknown as MessageAttachment
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
channel: {},
|
||||||
|
content: 'Message Content',
|
||||||
|
attachments: messageAttachments
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageDelete(message);
|
||||||
|
|
||||||
|
expect(channelSend).not.toBeCalled();
|
||||||
|
expect(memberGuildChannelsCacheFind).not.toBeCalled();
|
||||||
|
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given author is a bot, expect execution stopped', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const messageAttachments = new Collection<string, MessageAttachment>([
|
||||||
|
[
|
||||||
|
"0",
|
||||||
|
{
|
||||||
|
url: 'image0.png'
|
||||||
|
} as unknown as MessageAttachment
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"1",
|
||||||
|
{
|
||||||
|
url: 'image1.png'
|
||||||
|
} as unknown as MessageAttachment
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: true,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
channel: {},
|
||||||
|
content: 'Message Content',
|
||||||
|
attachments: messageAttachments
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageDelete(message);
|
||||||
|
|
||||||
|
expect(channelSend).not.toBeCalled();
|
||||||
|
expect(memberGuildChannelsCacheFind).not.toBeCalled();
|
||||||
|
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given message does not contain any attachments, expect attachments field to be omitted', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const messageAttachments = new Collection<string, MessageAttachment>([]);
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
channel: {},
|
||||||
|
content: 'Message Content',
|
||||||
|
attachments: messageAttachments
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageDelete(message);
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Message Deleted');
|
||||||
|
expect(embed.fields.length).toBe(3);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
|
||||||
|
// Embed -> Channel Field
|
||||||
|
const embedFieldChannel = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldChannel.name).toBe('Channel');
|
||||||
|
expect(embedFieldChannel.value).toBe('[object Object]');
|
||||||
|
|
||||||
|
// Embed -> Content Field
|
||||||
|
const embedFieldContent = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldContent.name).toBe('Content');
|
||||||
|
expect(embedFieldContent.value).toBe('```Message Content```');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('MessageUpdate', () => {
|
||||||
|
test('Given message is in a guild AND user is not a bot AND the content has actually changed, e xpect log embed to be sent', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMessage = {
|
||||||
|
content: 'Old Message'
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const newMessage = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
content: 'New Message',
|
||||||
|
channel: {},
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Message Edited');
|
||||||
|
expect(embed.fields.length).toBe(4);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
expect(embedFieldUser.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Channel Field
|
||||||
|
const embedFieldChannel = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldChannel.name).toBe('Channel');
|
||||||
|
expect(embedFieldChannel.value).toBe('[object Object]');
|
||||||
|
expect(embedFieldChannel.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('```Old Message```');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[3];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('```New Message```');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given message was not in a guild, expect execution stopped', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMessage = {
|
||||||
|
content: 'Old Message'
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const newMessage = {
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
content: 'New Message',
|
||||||
|
channel: {},
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
|
||||||
|
|
||||||
|
expect(channelSend).not.toBeCalled();
|
||||||
|
expect(memberGuildChannelsCacheFind).not.toBeCalled();
|
||||||
|
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given author is a bot, expect execution stopped', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMessage = {
|
||||||
|
content: 'Old Message'
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const newMessage = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: true,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
content: 'New Message',
|
||||||
|
channel: {},
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
|
||||||
|
|
||||||
|
expect(channelSend).not.toBeCalled();
|
||||||
|
expect(memberGuildChannelsCacheFind).not.toBeCalled();
|
||||||
|
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given the message contents are the same, expect execution stopped', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMessage = {
|
||||||
|
content: 'Message'
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const newMessage = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
content: 'Message',
|
||||||
|
channel: {},
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
|
||||||
|
|
||||||
|
expect(channelSend).not.toBeCalled();
|
||||||
|
expect(memberGuildChannelsCacheFind).not.toBeCalled();
|
||||||
|
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
|
||||||
|
expect(result.embeds.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given Old Message did not have a content, expect field to account for this', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMessage = {} as unknown as Message;
|
||||||
|
|
||||||
|
const newMessage = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
content: 'New Message',
|
||||||
|
channel: {},
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Message Edited');
|
||||||
|
expect(embed.fields.length).toBe(4);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
expect(embedFieldUser.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Channel Field
|
||||||
|
const embedFieldChannel = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldChannel.name).toBe('Channel');
|
||||||
|
expect(embedFieldChannel.value).toBe('[object Object]');
|
||||||
|
expect(embedFieldChannel.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('```*none*```');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[3];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('```New Message```');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given New Message does not have a content, expect field to account for this', async () => {
|
||||||
|
process.env = {
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const textChannel = {
|
||||||
|
name: 'mod-logs',
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const memberGuildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(textChannel);
|
||||||
|
const messageAuthorDisplayAvatarURL = jest.fn();
|
||||||
|
|
||||||
|
const oldMessage = {
|
||||||
|
content: 'Old Message'
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const newMessage = {
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: memberGuildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
bot: false,
|
||||||
|
displayAvatarURL: messageAuthorDisplayAvatarURL,
|
||||||
|
tag: 'USERTAG'
|
||||||
|
},
|
||||||
|
channel: {},
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const messageEvents = new MessageEvents();
|
||||||
|
|
||||||
|
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
|
||||||
|
|
||||||
|
expect(channelSend).toBeCalledTimes(1);
|
||||||
|
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
|
||||||
|
expect(result.embeds.length).toBe(1);
|
||||||
|
|
||||||
|
// Embed
|
||||||
|
const embed = result.embeds[0];
|
||||||
|
|
||||||
|
expect(embed.title).toBe('Message Edited');
|
||||||
|
expect(embed.fields.length).toBe(4);
|
||||||
|
|
||||||
|
// Embed -> User Field
|
||||||
|
const embedFieldUser = embed.fields[0];
|
||||||
|
|
||||||
|
expect(embedFieldUser.name).toBe('User');
|
||||||
|
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
|
||||||
|
expect(embedFieldUser.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Channel Field
|
||||||
|
const embedFieldChannel = embed.fields[1];
|
||||||
|
|
||||||
|
expect(embedFieldChannel.name).toBe('Channel');
|
||||||
|
expect(embedFieldChannel.value).toBe('[object Object]');
|
||||||
|
expect(embedFieldChannel.inline).toBeTruthy();
|
||||||
|
|
||||||
|
// Embed -> Before Field
|
||||||
|
const embedFieldBefore = embed.fields[2];
|
||||||
|
|
||||||
|
expect(embedFieldBefore.name).toBe('Before');
|
||||||
|
expect(embedFieldBefore.value).toBe('```Old Message```');
|
||||||
|
|
||||||
|
// Embed -> After Field
|
||||||
|
const embedFieldAfter = embed.fields[3];
|
||||||
|
|
||||||
|
expect(embedFieldAfter.name).toBe('After');
|
||||||
|
expect(embedFieldAfter.value).toBe('```*none*```');
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,11 +0,0 @@
|
||||||
describe('MessageCreate', () => {
|
|
||||||
test.todo("GIVEN verification is enabled, EXPECT function to execute");
|
|
||||||
|
|
||||||
test.todo("GIVEN verification is not enabled, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN message.guild is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN message author is a bot, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN verification.enabled is not found, EXPECT nothing to happen");
|
|
||||||
});
|
|
|
@ -1,23 +0,0 @@
|
||||||
describe("VerificationCheck", () => {
|
|
||||||
test.todo("GIVEN validation is correct, EXPECT role given");
|
|
||||||
|
|
||||||
test.todo("GIVEN validation is incorrect, EXPECT only message to be deleted");
|
|
||||||
|
|
||||||
test.todo("GIVEN message.guild is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN verification.channel setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN currentChannel is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN currentChannel is not the verification channel, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN verification.code is null, EXPECT error");
|
|
||||||
|
|
||||||
test.todo("GIVEN verification.code is empty, EXPECT error");
|
|
||||||
|
|
||||||
test.todo("GIVEN verification.role is null, EXPECT error");
|
|
||||||
|
|
||||||
test.todo("GIVEN role is not found, EXPECT error");
|
|
||||||
});
|
|
|
@ -1,21 +0,0 @@
|
||||||
describe('MessageDelete', () => {
|
|
||||||
test.todo('EXPECT embed to be logged');
|
|
||||||
|
|
||||||
test.todo("GIVEN message.guild is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN message author is a bot, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.message.delete.enabled setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN channel is ignored, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN message content is null, EXPECT content to be defaulted");
|
|
||||||
|
|
||||||
test.todo("GIVEN message had at least 1 attachment, EXPECT attachments field to be added");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.message.delete.channel setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
|
|
||||||
});
|
|
|
@ -1,23 +0,0 @@
|
||||||
describe('MessageUpdate', () => {
|
|
||||||
test.todo("EXPECT embed to be logged");
|
|
||||||
|
|
||||||
test.todo("GIVEN newMessage.guild is null, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN message author is a bot, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN message contents are the same, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.message.update.enabled setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN event is not enabled, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN channel is ignored, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN oldMessage.content is null, EXPECT oldMessage to be defaulted");
|
|
||||||
|
|
||||||
test.todo("GIVEN newMessage.content is null, EXPECT newMessage to be defaulted");
|
|
||||||
|
|
||||||
test.todo("GIVEN event.message.delete.channel setting is not found, EXPECT nothing to happen");
|
|
||||||
|
|
||||||
test.todo("GIVEN channel is not found, EXPECT nothing to happen");
|
|
||||||
});
|
|
|
@ -1,31 +0,0 @@
|
||||||
describe('TypeToFriendlyText', () => {
|
|
||||||
test.todo("GIVEN auditType is General, EXPECT General to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN auditType is Warn, EXPECT Warn to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN auditType is Mute, EXPECT Mute to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN auditType is Kick, EXPECT Kick to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN auditType is Ban, EXPECT Ban to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN auditType is Timeout, EXPECT Timeout to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN auditType is none of these, EXPECT Other to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('StringToType', () => {
|
|
||||||
test.todo("GIVEN string is general, EXPECT General to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN string is warn, EXPECT Warn to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN string is mute, EXPECT Mute to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN string is kick, EXPECT Kick to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN string is ban, EXPECT Ban to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN string is timeout, EXPECT Timeout to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN string is none of these, EXPECT General to be returned");
|
|
||||||
});
|
|
|
@ -1,7 +0,0 @@
|
||||||
describe('Up', () => {
|
|
||||||
test.todo("EXPECT up query to be executed");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Down", () => {
|
|
||||||
test.todo("EXPECT down query to be executed");
|
|
||||||
});
|
|
|
@ -1,15 +0,0 @@
|
||||||
describe("GetSetting", () => {
|
|
||||||
test.todo("EXPECT setting value to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN server is not set up, EXPECT default value to be returned");
|
|
||||||
|
|
||||||
test.todo("GIVEN setting is not set, EXPECT default value to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("SetSetting", () => {
|
|
||||||
test.todo("GIVEN setting is not set, EXPECT value to be added");
|
|
||||||
|
|
||||||
test.todo("GIVEN setting is already set, EXPECT value to be updated");
|
|
||||||
|
|
||||||
test.todo("GIVEN server is not set up, EXPECT nothing to happen");
|
|
||||||
});
|
|
|
@ -1,15 +1,11 @@
|
||||||
|
import StringTools from "../../src/helpers/StringTools";
|
||||||
|
|
||||||
describe('Capitalise', () => {
|
describe('Capitalise', () => {
|
||||||
test.todo('EXPECT sentence to be capitalised');
|
test('Expect sentence to be captilised', () => {
|
||||||
});
|
const inputString = 'the big brown fox jumps over the lazy dog';
|
||||||
|
|
||||||
describe('CapitaliseArray', () => {
|
const result = StringTools.Capitalise(inputString);
|
||||||
test.todo("EXPECT words in array to be capitalised");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("RandomString", () => {
|
expect(result).toBe('The Big Brown Fox Jumps Over The Lazy Dog');
|
||||||
test.todo("EXPECT random string of length to be returned");
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe("ReplaceAll", () => {
|
|
||||||
test.todo("EXPECT all instances of substring to be replaced");
|
|
||||||
});
|
});
|
|
@ -1,37 +0,0 @@
|
||||||
describe('constructor', () => {
|
|
||||||
test.todo("EXPECT properties to be set");
|
|
||||||
|
|
||||||
test.todo("GIVEN input has commas, EXPECT these to be removed");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetDays', () => {
|
|
||||||
test.todo("EXPECT day value to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetHours', () => {
|
|
||||||
test.todo("EXPECT hour value to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetMinutes', () => {
|
|
||||||
test.todo("EXPECT minute value to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetSeconds', () => {
|
|
||||||
test.todo("EXPECT second value to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetMiliseconds', () => {
|
|
||||||
test.todo("EXPECT total time returned in miliseconds");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetDateFromNow', () => {
|
|
||||||
test.todo("EXPECT date returned to be value added to now");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GetLength", () => {
|
|
||||||
test.todo("EXPECT stringified value to be returned");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GetLengthShort", () => {
|
|
||||||
test.todo("EXPECT stringified value to be returned");
|
|
||||||
});
|
|
57
tests/helpers/embeds/ErrorEmbed.test.ts
Normal file
57
tests/helpers/embeds/ErrorEmbed.test.ts
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
import { Message } from "discord.js";
|
||||||
|
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||||
|
import ErrorEmbed from "../../../src/helpers/embeds/ErrorEmbed";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties to be set', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR_ERROR: '0xd52803'
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new ErrorEmbed(context, 'Error Message');
|
||||||
|
|
||||||
|
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
|
||||||
|
expect(errorEmbed.description).toBe('Error Message');
|
||||||
|
expect(errorEmbed.context).toBe(context);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToCurrentChannel', () => {
|
||||||
|
test('Expect embed to be sent to the current channel in context', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR_ERROR: '0xd52803'
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new ErrorEmbed(context, 'Error Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToCurrentChannel();
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledWith(errorEmbed);
|
||||||
|
});
|
||||||
|
});
|
292
tests/helpers/embeds/EventEmbed.test.ts
Normal file
292
tests/helpers/embeds/EventEmbed.test.ts
Normal file
|
@ -0,0 +1,292 @@
|
||||||
|
import { Guild, Message, TextChannel, User } from "discord.js";
|
||||||
|
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||||
|
import EventEmbed from "../../../src/helpers/embeds/EventEmbed";
|
||||||
|
import SettingsHelper from "../../../src/helpers/SettingsHelper";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
jest.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties to be set', () => {
|
||||||
|
const guild = {} as unknown as Guild;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
|
||||||
|
expect(errorEmbed.title).toBe('Event Message');
|
||||||
|
expect(errorEmbed.guild).toBe(guild);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('AddUser', () => {
|
||||||
|
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const addField = jest.fn();
|
||||||
|
const setThumbnail = jest.fn();
|
||||||
|
|
||||||
|
const guild = {} as unknown as Guild;
|
||||||
|
|
||||||
|
const user = {
|
||||||
|
tag: 'USERTAG'
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.addField = addField;
|
||||||
|
errorEmbed.setThumbnail = setThumbnail;
|
||||||
|
|
||||||
|
errorEmbed.AddUser('User', user);
|
||||||
|
|
||||||
|
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||||
|
expect(setThumbnail).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const addField = jest.fn();
|
||||||
|
const setThumbnail = jest.fn();
|
||||||
|
const displayAvatarURL = jest.fn()
|
||||||
|
.mockReturnValue('image0.png');
|
||||||
|
|
||||||
|
const guild = {} as unknown as Guild;
|
||||||
|
|
||||||
|
const user = {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
displayAvatarURL: displayAvatarURL
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.addField = addField;
|
||||||
|
errorEmbed.setThumbnail = setThumbnail;
|
||||||
|
|
||||||
|
errorEmbed.AddUser('User', user, true);
|
||||||
|
|
||||||
|
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||||
|
expect(setThumbnail).toBeCalledWith('image0.png');
|
||||||
|
expect(displayAvatarURL).toBeCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('AddReason', () => {
|
||||||
|
test('Given a non-empty string is supplied, expect field with message', () => {
|
||||||
|
const guild = {} as Guild;
|
||||||
|
|
||||||
|
const eventEmbed = new EventEmbed(guild, "Event Embed");
|
||||||
|
|
||||||
|
eventEmbed.addField = jest.fn();
|
||||||
|
|
||||||
|
eventEmbed.AddReason("Test reason");
|
||||||
|
|
||||||
|
expect(eventEmbed.addField).toBeCalledWith("Reason", "Test reason");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given an empty string is supplied, expect field with default message', () => {
|
||||||
|
const guild = {} as Guild;
|
||||||
|
|
||||||
|
const eventEmbed = new EventEmbed(guild, "Event Embed");
|
||||||
|
|
||||||
|
eventEmbed.addField = jest.fn();
|
||||||
|
|
||||||
|
eventEmbed.AddReason("");
|
||||||
|
|
||||||
|
expect(eventEmbed.addField).toBeCalledWith("Reason", "*none*");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToChannel', () => {
|
||||||
|
test('Given channel can be found, expect embed to be sent to that channel', () => {
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const channel = {
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const guildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(channel);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: guildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel('channel-name');
|
||||||
|
|
||||||
|
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(channelSend).toBeCalledWith(errorEmbed);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given channel can NOT be found, expect error logged', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const guildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(null);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: guildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
console.error = jest.fn();
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel('channel-name');
|
||||||
|
|
||||||
|
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(console.error).toBeCalledWith('Unable to find channel channel-name');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToMessageLogsChannel', () => {
|
||||||
|
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue("message-logs");
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await errorEmbed.SendToMessageLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).toBeCalledWith('message-logs');
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setting is not set, expect function to return', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await errorEmbed.SendToMessageLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).not.toBeCalled();
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToMemberLogsChannel', () => {
|
||||||
|
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue("member-logs");
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await errorEmbed.SendToMemberLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).toBeCalledWith('member-logs');
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setting is not set, expect function to return', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await errorEmbed.SendToMemberLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).not.toBeCalled();
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToModLogsChannel', () => {
|
||||||
|
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await errorEmbed.SendToModLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).toBeCalledWith('mod-logs');
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setting is not set, expect function to return', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await errorEmbed.SendToModLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).not.toBeCalled();
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||||
|
});
|
||||||
|
});
|
407
tests/helpers/embeds/LogEmbed.test.ts
Normal file
407
tests/helpers/embeds/LogEmbed.test.ts
Normal file
|
@ -0,0 +1,407 @@
|
||||||
|
import { Guild, Message, TextChannel, User } from "discord.js";
|
||||||
|
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||||
|
import LogEmbed from "../../../src/helpers/embeds/LogEmbed";
|
||||||
|
import SettingsHelper from "../../../src/helpers/SettingsHelper";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
jest.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties to be set', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new LogEmbed(context, 'Log Message');
|
||||||
|
|
||||||
|
expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
|
||||||
|
expect(errorEmbed.title).toBe('Log Message');
|
||||||
|
expect(errorEmbed.context).toBe(context);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('AddUser', () => {
|
||||||
|
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const addField = jest.fn();
|
||||||
|
const setThumbnail = jest.fn();
|
||||||
|
|
||||||
|
const user = {
|
||||||
|
tag: 'USERTAG'
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
author: user
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.addField = addField;
|
||||||
|
errorEmbed.setThumbnail = setThumbnail;
|
||||||
|
|
||||||
|
errorEmbed.AddUser('User', user);
|
||||||
|
|
||||||
|
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||||
|
expect(setThumbnail).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const addField = jest.fn();
|
||||||
|
const setThumbnail = jest.fn();
|
||||||
|
const displayAvatarURL = jest.fn()
|
||||||
|
.mockReturnValue('image0.png');
|
||||||
|
|
||||||
|
const user = {
|
||||||
|
tag: 'USERTAG',
|
||||||
|
displayAvatarURL: displayAvatarURL
|
||||||
|
} as unknown as User;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
author: user
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.addField = addField;
|
||||||
|
errorEmbed.setThumbnail = setThumbnail;
|
||||||
|
|
||||||
|
errorEmbed.AddUser('User', user, true);
|
||||||
|
|
||||||
|
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||||
|
expect(setThumbnail).toBeCalledWith('image0.png');
|
||||||
|
expect(displayAvatarURL).toBeCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToChannel', () => {
|
||||||
|
test('Given channel can be found, expect embed to be sent to that channel', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelSend = jest.fn();
|
||||||
|
|
||||||
|
const channel = {
|
||||||
|
send: channelSend
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
|
const guildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(channel);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: guildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
guild: guild
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel('channel-name');
|
||||||
|
|
||||||
|
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
expect(channelSend).toBeCalledWith(errorEmbed);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given channel can NOT be found, expect error logged', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const guildChannelsCacheFind = jest.fn()
|
||||||
|
.mockReturnValue(null);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: guildChannelsCacheFind
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
console.error = jest.fn();
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
},
|
||||||
|
guild: guild
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
errorEmbed.SendToChannel('channel-name');
|
||||||
|
|
||||||
|
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToMessageLogsChannel', () => {
|
||||||
|
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue("message-logs");
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: guild
|
||||||
|
} as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'log',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
logEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await logEmbed.SendToMessageLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).toBeCalledWith('message-logs');
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setting is not set, expect function to return', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: guild
|
||||||
|
} as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'log',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
logEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await logEmbed.SendToMessageLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).not.toBeCalled();
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToMemberLogsChannel', () => {
|
||||||
|
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue("member-logs");
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: guild
|
||||||
|
} as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'log',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
logEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await logEmbed.SendToMemberLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).toBeCalledWith('member-logs');
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setting is not set, expect function to return', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: guild
|
||||||
|
} as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'log',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
logEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await logEmbed.SendToMemberLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).not.toBeCalled();
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToModLogsChannel', () => {
|
||||||
|
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: guild
|
||||||
|
} as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'log',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
logEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await logEmbed.SendToModLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).toBeCalledWith('mod-logs');
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Given setting is not set, expect function to return', async () => {
|
||||||
|
const sendToChannel = jest.fn();
|
||||||
|
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const guild = {
|
||||||
|
id: "guildId"
|
||||||
|
} as unknown as Guild;
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
guild: guild
|
||||||
|
} as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'log',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
SettingsHelper.GetSetting = getSetting;
|
||||||
|
|
||||||
|
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||||
|
|
||||||
|
logEmbed.SendToChannel = sendToChannel;
|
||||||
|
|
||||||
|
await logEmbed.SendToModLogsChannel();
|
||||||
|
|
||||||
|
expect(sendToChannel).not.toBeCalled();
|
||||||
|
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||||
|
});
|
||||||
|
});
|
67
tests/helpers/embeds/PublicEmbed.test.ts
Normal file
67
tests/helpers/embeds/PublicEmbed.test.ts
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import { Guild, Message, TextChannel, User } from "discord.js";
|
||||||
|
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||||
|
import PublicEmbed from "../../../src/helpers/embeds/PublicEmbed";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = {};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
test('Expect properties to be set', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR: '0xd52803',
|
||||||
|
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||||
|
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||||
|
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new PublicEmbed(context, 'Log Message', 'Log Description');
|
||||||
|
|
||||||
|
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
|
||||||
|
expect(errorEmbed.title).toBe('Log Message');
|
||||||
|
expect(errorEmbed.description).toBe('Log Description');
|
||||||
|
expect(errorEmbed.context).toBe(context);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SendToCurrentChannel', () => {
|
||||||
|
test('Expect embed to be sent to the current channel in context', () => {
|
||||||
|
process.env = {
|
||||||
|
EMBED_COLOUR_ERROR: '0xd52803'
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageChannelSend = jest.fn();
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
channel: {
|
||||||
|
send: messageChannelSend
|
||||||
|
}
|
||||||
|
} as unknown as Message;
|
||||||
|
|
||||||
|
const context: ICommandContext = {
|
||||||
|
name: 'command',
|
||||||
|
args: [],
|
||||||
|
message: message
|
||||||
|
};
|
||||||
|
|
||||||
|
const errorEmbed = new PublicEmbed(context, 'Message', 'Description');
|
||||||
|
|
||||||
|
errorEmbed.SendToCurrentChannel();
|
||||||
|
|
||||||
|
expect(messageChannelSend).toBeCalledWith(errorEmbed);
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,11 +0,0 @@
|
||||||
describe('RegisterCommands', () => {
|
|
||||||
test.todo("EXPECT commands to be registered");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('RegisterEvents', () => {
|
|
||||||
test.todo("EXPECT events to be registered");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('RegisterButtonEvents', () => {
|
|
||||||
test.todo("EXPECT button events to be registered");
|
|
||||||
});
|
|
Loading…
Reference in a new issue