Compare commits
53 commits
main
...
feature/25
Author | SHA1 | Date | |
---|---|---|---|
Ethan Lane | 451c53f2cd | ||
Ethan Lane | 345a9efbc2 | ||
Ethan Lane | 97bd3d675b | ||
Ethan Lane | 42ec4a99db | ||
Ethan Lane | 739bd0713d | ||
Ethan Lane | 16bc43c30e | ||
RenovateBot | 7ccfa34562 | ||
RenovateBot | f1c7cdc6d6 | ||
RenovateBot | 1371a30eee | ||
RenovateBot | 34398e5657 | ||
RenovateBot | cd0737cf3b | ||
RenovateBot | 5f22a31398 | ||
Ethan Lane | 581c275adf | ||
Ethan Lane | 3e198308b9 | ||
Ethan Lane | 39231ddc16 | ||
Ethan Lane | 91bc444e71 | ||
Ethan Lane | d0a8db718e | ||
2841ed5463 | |||
ac51eb7f40 | |||
bd6858daa3 | |||
Ethan Lane | 2377ec165f | ||
Ethan Lane | 3d8b5a8347 | ||
Ethan Lane | 3afc28e02b | ||
RenovateBot | 932d3ac3d5 | ||
RenovateBot | 6412ad0ad4 | ||
Ethan Lane | 166bd80113 | ||
Ethan Lane | fa7e35b4dd | ||
Ethan Lane | 7e625b232e | ||
Ethan Lane | 6323398e48 | ||
Ethan Lane | 60972e5782 | ||
Ethan Lane | 74cdd04d27 | ||
RenovateBot | f1c513bd16 | ||
RenovateBot | 942fea12bf | ||
Ethan Lane | ffff5e8c7f | ||
Ethan Lane | 5409ecaffc | ||
Ethan Lane | e860a19bfe | ||
Ethan Lane | e7fde3a2ea | ||
Ethan Lane | d5a5282371 | ||
Ethan Lane | 00f6a60a43 | ||
Ethan Lane | 946422b2ac | ||
Ethan Lane | 5e30962053 | ||
Ethan Lane | f770e3f4fc | ||
Ethan Lane | 53a084b3b1 | ||
Ethan Lane | ca64ede1ad | ||
Ethan Lane | fc975f4602 | ||
Ethan Lane | 39b2a495a8 | ||
Ethan Lane | 6d2b323338 | ||
Ethan Lane | 88d229b709 | ||
Ethan Lane | 9c13ee6099 | ||
Ethan Lane | 0f0dbee7de | ||
Ethan Lane | 4e9ec89108 | ||
Ethan Lane | 69ef94dbb5 | ||
Ethan Lane | ea6bac2af1 |
|
@ -54,11 +54,10 @@ steps:
|
||||||
- yarn install --frozen-lockfile
|
- yarn install --frozen-lockfile
|
||||||
- yarn build
|
- yarn build
|
||||||
|
|
||||||
# - name: test
|
- name: test
|
||||||
# image: node
|
image: node
|
||||||
# commands:
|
commands:
|
||||||
# - yarn install --frozen-lockfile
|
- yarn test
|
||||||
# - yarn test
|
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
branch:
|
branch:
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
# any secret values.
|
# any secret values.
|
||||||
|
|
||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
BOT_VER=3.2.1
|
BOT_VER=3.3.0
|
||||||
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@v2
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
- run: npm ci
|
- run: yarn install --frozen-lockfile
|
||||||
- run: npm run build
|
- run: yarn build
|
||||||
- run: npm test
|
- run: yarn 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 \
|
||||||
&& npm run db:up \
|
&& yarn 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@v2
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
- run: npm ci
|
- run: yarn install --frozen-lockfile
|
||||||
- run: npm run build
|
- run: yarn build
|
||||||
- run: npm test
|
- run: yarn 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 \
|
||||||
&& npm run db:up \
|
&& yarn 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@v2
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
- run: npm ci
|
- run: yarn install --frozen-lockfile
|
||||||
- run: npm run build
|
- run: yarn build
|
||||||
- run: npm test
|
- run: yarn test
|
||||||
|
|
8815
package-lock.json
generated
8815
package-lock.json
generated
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": "echo true",
|
"test": "jest",
|
||||||
"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.3",
|
"minimatch": "9.0.4",
|
||||||
"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,6 +1,5 @@
|
||||||
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";
|
||||||
|
@ -14,32 +13,20 @@ 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 {
|
||||||
private static _commandItems: ICommandItem[];
|
public static commandItems: ICommandItem[];
|
||||||
private static _eventItems: IEventItem[];
|
public static eventItems: IEventItem[];
|
||||||
private static _buttonEvents: ButtonEventItem[];
|
public 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();
|
||||||
|
@ -51,9 +38,12 @@ export class CoreClient extends Client {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await AppDataSource.initialize()
|
try {
|
||||||
.then(() => console.log("Data Source Initialized"))
|
await AppDataSource.initialize();
|
||||||
.catch((err) => console.error("Error Initialising Data Source", err));
|
} catch (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);
|
||||||
|
@ -64,7 +54,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +65,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) {
|
||||||
|
@ -84,7 +74,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) {
|
||||||
|
@ -93,6 +83,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, GuildBasedChannel, PermissionsBitField, SlashCommandBuilder } from "discord.js";
|
import { CacheType, CommandInteraction, EmbedBuilder, 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";
|
||||||
|
|
|
@ -73,19 +73,17 @@ 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) return;
|
if (!assignRole!.editable) {
|
||||||
|
|
||||||
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,8 +60,7 @@ 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')
|
||||||
|
@ -93,7 +92,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.getUser('target');
|
const user = interaction.options.get('target', true).user!;
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
await interaction.reply("User not found.");
|
await interaction.reply("User not found.");
|
||||||
|
@ -191,7 +190,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.getUser('target');
|
const user = interaction.options.get('target', true).user!;
|
||||||
const auditType = interaction.options.get('type');
|
const auditType = interaction.options.get('type');
|
||||||
const reasonInput = interaction.options.get('reason');
|
const reasonInput = interaction.options.get('reason');
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ 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);
|
await this.ResetValue(interaction, server);
|
||||||
break;
|
break;
|
||||||
case 'get':
|
case 'get':
|
||||||
await this.GetValue(interaction);
|
await this.GetValue(interaction, server);
|
||||||
break;
|
break;
|
||||||
case 'set':
|
case 'set':
|
||||||
await this.SetValue(interaction);
|
await this.SetValue(interaction, server);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
await interaction.reply('Subcommand not found.');
|
await interaction.reply('Subcommand not found.');
|
||||||
|
@ -94,9 +94,7 @@ export default class Config extends Command {
|
||||||
await interaction.reply({ embeds: [ embed ]});
|
await interaction.reply({ embeds: [ embed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async GetValue(interaction: CommandInteraction) {
|
private async GetValue(interaction: CommandInteraction, server: Server) {
|
||||||
if (!interaction.guildId) return;
|
|
||||||
|
|
||||||
const key = interaction.options.get('key');
|
const key = interaction.options.get('key');
|
||||||
|
|
||||||
if (!key || !key.value) {
|
if (!key || !key.value) {
|
||||||
|
@ -104,15 +102,6 @@ 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) {
|
||||||
|
@ -128,7 +117,7 @@ export default class Config extends Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ResetValue(interaction: CommandInteraction) {
|
private async ResetValue(interaction: CommandInteraction, server: Server) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const key = interaction.options.get('key');
|
const key = interaction.options.get('key');
|
||||||
|
@ -138,15 +127,6 @@ 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) {
|
||||||
|
@ -159,34 +139,18 @@ 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) {
|
private async SetValue(interaction: CommandInteraction, server: Server) {
|
||||||
if (!interaction.guildId) return;
|
const key = interaction.options.get('key', true);
|
||||||
|
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,17 +48,12 @@ 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');
|
const commandName = interaction.options.get('name', true);
|
||||||
|
|
||||||
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 != "" ? disabledCommandsString?.split(",") : [];
|
const disabledCommands = disabledCommandsString != undefined ? 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(","));
|
||||||
|
|
||||||
|
@ -68,23 +63,18 @@ 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');
|
const commandName = interaction.options.get('name', true);
|
||||||
|
|
||||||
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 != "" ? disabledCommandsString?.split(",") : [];
|
const disabledCommands = disabledCommandsString != undefined ? 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,23 +13,16 @@ export default class Ignore extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
public override async execute(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.channelId);
|
||||||
|
|
||||||
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.guildId);
|
|
||||||
|
|
||||||
if (isChannelIgnored) {
|
if (isChannelIgnored) {
|
||||||
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.guildId);
|
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.channelId);
|
||||||
|
|
||||||
if (!entity) {
|
await IgnoredChannel.Remove(IgnoredChannel, 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.guildId);
|
const entity = new IgnoredChannel(interaction.channelId);
|
||||||
|
|
||||||
await entity.Save(IgnoredChannel, entity);
|
await entity.Save(IgnoredChannel, entity);
|
||||||
|
|
||||||
|
|
|
@ -29,22 +29,17 @@ 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');
|
const targetUser = interaction.options.get('target', true);
|
||||||
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",
|
||||||
|
@ -62,19 +57,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) return;
|
if (channelName) {
|
||||||
|
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
||||||
|
|
||||||
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
if (channel) {
|
||||||
|
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,15 +40,13 @@ export default class Poll extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
public override async execute(interaction: CommandInteraction) {
|
||||||
const title = interaction.options.get('title');
|
const title = interaction.options.get('title', true);
|
||||||
const option1 = interaction.options.get('option1');
|
const option1 = interaction.options.get('option1', true);
|
||||||
const option2 = interaction.options.get('option2');
|
const option2 = interaction.options.get('option2', true);
|
||||||
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,
|
||||||
|
@ -58,15 +56,7 @@ export default class Poll extends Command {
|
||||||
]
|
]
|
||||||
.filter(x => x != null);
|
.filter(x => x != null);
|
||||||
|
|
||||||
const arrayOfNumbers = [
|
const reactionEmojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣"];
|
||||||
':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]}`;
|
||||||
|
@ -82,10 +72,11 @@ export default class Poll extends Command {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const message = await interaction.reply({ embeds: [ embed ]});
|
const messageResponse = await interaction.reply({ embeds: [ embed ]});
|
||||||
|
const message = await messageResponse.fetch();
|
||||||
|
|
||||||
description.forEach(async (value, index) => {
|
description.forEach(async (value, index) => {
|
||||||
await (await message.fetch()).react(reactionEmojis[index]);
|
await message.react(reactionEmojis[index]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -27,21 +27,16 @@ 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');
|
const targetUser = interaction.options.get('target', true).user!;
|
||||||
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.user.id}> \`${targetUser.user.tag}\``)
|
.setDescription(`<@${targetUser.id}> \`${targetUser.tag}\``)
|
||||||
.setThumbnail(targetUser.user.avatarURL())
|
.setThumbnail(targetUser.avatarURL())
|
||||||
.addFields([
|
.addFields([
|
||||||
{
|
{
|
||||||
name: "Moderator",
|
name: "Moderator",
|
||||||
|
@ -55,15 +50,15 @@ 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) return;
|
if (channelName) {
|
||||||
|
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
||||||
|
|
||||||
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
if (channel) {
|
||||||
|
await channel.send({ embeds: [ logEmbed ]});
|
||||||
if (channel) {
|
}
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const audit = new Audit(targetUser.user.id, AuditType.Warn, reason, interaction.user.id, interaction.guildId);
|
const audit = new Audit(targetUser.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,14 +46,4 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
155
tests/buttonEvents/verify.test.ts
Normal file
155
tests/buttonEvents/verify.test.ts
Normal file
|
@ -0,0 +1,155 @@
|
||||||
|
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,153 +1,129 @@
|
||||||
import { mock } from "jest-mock-extended";
|
import { Partials, Client } from "discord.js";
|
||||||
|
|
||||||
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 { Events } from "../../src/client/events";
|
import AppDataSource from "../../src/database/dataSources/appDataSource";
|
||||||
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 { Event } from "../../src/type/event";
|
import { EventType } from "../../src/constants/EventType";
|
||||||
import DefaultValues from "../../src/constants/DefaultValues";
|
import { ButtonEvent } from "../../src/type/buttonEvent";
|
||||||
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
|
|
||||||
import Setting from "../../src/entity/Setting";
|
jest.mock("dotenv");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.resetAllMocks();
|
jest.clearAllMocks();
|
||||||
jest.resetModules();
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
process.env = {};
|
||||||
test('Expect Successful Initialisation', () => {
|
});
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
expect(coreClient).toBeInstanceOf(Client);
|
describe('constructor', () => {
|
||||||
expect(dotenv.config).toBeCalledTimes(1);
|
test("EXPECT properties to be set", () => {
|
||||||
expect(Events).toBeCalledTimes(1);
|
const client = new CoreClient([1, 2, 3], [Partials.User]);
|
||||||
expect(Util).toBeCalledTimes(1);
|
|
||||||
expect(DefaultValues.useDevPrefix).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given devmode parameter is true, Expect devmode prefix to be true', () => {
|
expect(dotenv.config).toHaveBeenCalledTimes(1);
|
||||||
const coreClient = new CoreClient(true);
|
|
||||||
|
|
||||||
expect(coreClient).toBeInstanceOf(Client);
|
expect(client).toBeDefined();
|
||||||
expect(dotenv.config).toBeCalledTimes(1);
|
expect(CoreClient.commandItems).toEqual([]);
|
||||||
expect(Events).toBeCalledTimes(1);
|
expect(CoreClient.eventItems).toEqual([]);
|
||||||
expect(Util).toBeCalledTimes(1);
|
expect(CoreClient.buttonEvents).toEqual([]);
|
||||||
expect(DefaultValues.useDevPrefix).toBe(true);
|
expect(client).toHaveProperty("_events");
|
||||||
|
expect(client).toHaveProperty("_util");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Start', () => {
|
describe('start', () => {
|
||||||
test('Given Env Is Valid, Expect Successful Start', async () => {
|
test("EXPECT bot to start", async () => {
|
||||||
process.env = {
|
process.env.BOT_TOKEN = "token";
|
||||||
BOT_TOKEN: "TOKEN",
|
console.error = jest.fn();
|
||||||
};
|
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();
|
||||||
|
const loadEvents = jest.spyOn(Util.prototype, "loadEvents").mockImplementation();
|
||||||
|
|
||||||
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
|
Client.prototype.login = jest.fn();
|
||||||
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
|
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 () => {
|
test("GIVEN BOT_TOKEN is not in env var, EXPECT error", async () => {
|
||||||
process.env = {};
|
console.error = jest.fn();
|
||||||
|
|
||||||
const consoleError = jest.fn();
|
const client = new CoreClient([1, 2, 3], [ Partials.User ]);
|
||||||
|
|
||||||
console.error = consoleError;
|
await client.start();
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
expect(console.error).toHaveBeenCalledWith("BOT_TOKEN is not defined in .env");
|
||||||
|
|
||||||
await coreClient.start();
|
|
||||||
|
|
||||||
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 () => {
|
test("GIVEN database connection can not be initialised, EXPECT error", async () => {
|
||||||
process.env = {
|
process.env.BOT_TOKEN = "token";
|
||||||
BOT_TOKEN: '',
|
console.log = jest.fn();
|
||||||
}
|
console.error = jest.fn();
|
||||||
|
AppDataSource.initialize = jest.fn().mockRejectedValue("error");
|
||||||
|
|
||||||
const consoleError = jest.fn();
|
const client = new CoreClient([1, 2, 3], [Partials.User]);
|
||||||
|
|
||||||
console.error = consoleError;
|
await client.start();
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
expect(console.log).not.toHaveBeenCalled();
|
||||||
|
expect(console.error).toHaveBeenCalledWith("Error Initialising Data Source", "error");
|
||||||
await coreClient.start();
|
|
||||||
|
|
||||||
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
|
|
||||||
expect(coreClient.on).not.toBeCalled();
|
|
||||||
expect(coreClient.login).not.toBeCalled();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('RegisterCommand', () => {
|
describe('RegisterCommand', () => {
|
||||||
test('Expect command added to register', () => {
|
test("EXPECT command to be added to list", () => {
|
||||||
const cmd = mock<Command>();
|
CoreClient.RegisterCommand("name", {} as Command, "serverId");
|
||||||
|
|
||||||
const client = new CoreClient();
|
expect(CoreClient.commandItems).toStrictEqual([
|
||||||
client.RegisterCommand("test", cmd);
|
{
|
||||||
|
Name: "name",
|
||||||
expect(client.commandItems.length).toBe(1);
|
Command: {},
|
||||||
expect(client.commandItems[0].Name).toBe("test");
|
ServerId: "serverId",
|
||||||
expect(client.commandItems[0].Command).toBe(cmd);
|
}
|
||||||
|
])
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('RegisterEvent', () => {
|
describe("RegisterEvent", () => {
|
||||||
test('Expect event added to register', () => {
|
test("EXPECT event to be added to list", () => {
|
||||||
const evt = mock<Event>();
|
CoreClient.RegisterEvent(EventType.ChannelCreate, () => {});
|
||||||
|
|
||||||
const client = new CoreClient();
|
expect(CoreClient.eventItems).toStrictEqual([
|
||||||
client.RegisterEvent(evt);
|
{
|
||||||
|
EventType: EventType.ChannelCreate,
|
||||||
expect(client.eventItems.length).toBe(1);
|
ExecutionFunction: expect.any(Function),
|
||||||
expect(client.eventItems[0].Event).toBe(evt);
|
}
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("RegisterButtonEvent", () => {
|
||||||
|
test("EXPECT button event to be added to list", () => {
|
||||||
|
CoreClient.RegisterButtonEvent("buttonId", {} as ButtonEvent);
|
||||||
|
|
||||||
|
expect(CoreClient.buttonEvents).toStrictEqual([
|
||||||
|
{
|
||||||
|
ButtonId: "buttonId",
|
||||||
|
Event: {},
|
||||||
|
}
|
||||||
|
])
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -1,241 +1,89 @@
|
||||||
|
import { Interaction } from "discord.js";
|
||||||
import { Events } from "../../src/client/events";
|
import { Events } from "../../src/client/events";
|
||||||
import { Message } from "discord.js";
|
import ChatInputCommand from "../../src/client/interactionCreate/chatInputCommand";
|
||||||
import { Util } from "../../src/client/util";
|
import Button from "../../src/client/interactionCreate/button";
|
||||||
import ICommandItem from "../../src/contracts/ICommandItem";
|
|
||||||
import { Command } from "../../src/type/command";
|
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
|
|
||||||
jest.mock("../../src/client/util");
|
describe('onInteractionCreate', () => {
|
||||||
|
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;
|
||||||
|
|
||||||
beforeEach(() => {
|
ChatInputCommand.onChatInput = jest.fn();
|
||||||
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 () => {
|
test("GIVEN the interaction is a button, EXPECT button handler to be executed", async () => {
|
||||||
process.env = {
|
const interaction = {
|
||||||
BOT_TOKEN: 'TOKEN',
|
isChatInputCommand: jest.fn().mockReturnValue(false),
|
||||||
BOT_PREFIX: '!',
|
isButton: jest.fn().mockReturnValue(true),
|
||||||
FOLDERS_COMMANDS: 'commands',
|
guildId: "123",
|
||||||
FOLDERS_EVENTS: 'events',
|
} as unknown as Interaction;
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
Button.onButtonClicked = jest.fn();
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: null,
|
|
||||||
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.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 () => {
|
test("GIVEN the interaction is not a chat input command or button, EXPECT error", async () => {
|
||||||
process.env = {
|
const interaction = {
|
||||||
BOT_TOKEN: 'TOKEN',
|
isChatInputCommand: jest.fn().mockReturnValue(false),
|
||||||
BOT_PREFIX: '!',
|
isButton: jest.fn().mockReturnValue(false),
|
||||||
FOLDERS_COMMANDS: 'commands',
|
guildId: "123",
|
||||||
FOLDERS_EVENTS: 'events',
|
} as unknown as Interaction;
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
console.error = jest.fn();
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: true,
|
|
||||||
},
|
|
||||||
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(interaction.isButton).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
expect(console.error).toHaveBeenCalledWith("Received interaction unable to interact with, ignoring");
|
||||||
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Given Message Content Was Not A Command, Expect Failed Result', async () => {
|
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
||||||
process.env = {
|
const interaction = {
|
||||||
BOT_TOKEN: 'TOKEN',
|
isChatInputCommand: jest.fn(),
|
||||||
BOT_PREFIX: '!',
|
isButton: jest.fn(),
|
||||||
FOLDERS_COMMANDS: 'commands',
|
guildId: null,
|
||||||
FOLDERS_EVENTS: 'events',
|
} as unknown as Interaction;
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
ChatInputCommand.onChatInput = jest.fn();
|
||||||
|
Button.onButtonClicked = jest.fn();
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "This is a standard message",
|
|
||||||
} 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).not.toHaveBeenCalled();
|
||||||
|
expect(interaction.isButton).not.toHaveBeenCalled();
|
||||||
expect(result.valid).toBeFalsy();
|
expect(ChatInputCommand.onChatInput).not.toHaveBeenCalled();
|
||||||
expect(result.message).toBe("Message was not a command, ignoring.");
|
expect(Button.onButtonClicked).not.toHaveBeenCalled();
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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 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 Log', () => {
|
test("EXPECT console to be logged", () => {
|
||||||
console.log = jest.fn();
|
console.log = jest.fn();
|
||||||
|
|
||||||
const events = new Events();
|
const events = new Events();
|
||||||
|
|
||||||
events.onReady();
|
events.onReady();
|
||||||
|
|
||||||
expect(console.log).toBeCalledWith("Ready");
|
expect(console.log).toHaveBeenCalledWith("Ready");
|
||||||
});
|
});
|
||||||
});
|
});
|
70
tests/client/interactionCreate/button.test.ts
Normal file
70
tests/client/interactionCreate/button.test.ts
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
168
tests/client/interactionCreate/chatInputCommand.test.ts
Normal file
168
tests/client/interactionCreate/chatInputCommand.test.ts
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
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,370 +1,333 @@
|
||||||
|
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 { Event } from "../../src/type/event";
|
import { EventType } from "../../src/constants/EventType";
|
||||||
|
|
||||||
jest.mock("fs");
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fs.existsSync = jest.fn();
|
jest.resetAllMocks();
|
||||||
|
jest.resetModules();
|
||||||
|
|
||||||
|
process.env = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('LoadCommand', () => {
|
describe("loadSlashCommands", () => {
|
||||||
test('Given Successful Exection, Expect Successful Result', async () => {
|
test("EXPECT slash commands to be loaded to the discord API", () => {
|
||||||
process.env = {
|
process.env.BOT_TOKEN = "token";
|
||||||
BOT_TOKEN: 'TOKEN',
|
process.env.BOT_CLIENTID = "clientid";
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
const client = {
|
||||||
member: {
|
guilds: {
|
||||||
roles: {
|
cache: {
|
||||||
cache: {
|
has: jest.fn().mockReturnValue(true),
|
||||||
find: jest.fn().mockReturnValue(true),
|
}
|
||||||
}
|
}
|
||||||
},
|
} as unknown as Client;
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
const setTokenPutMock = jest.fn();
|
||||||
|
const setTokenMock = jest.fn().mockReturnValue({
|
||||||
|
put: setTokenPutMock,
|
||||||
|
});
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
REST.prototype.setToken = setTokenMock;
|
||||||
Name: "test",
|
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
|
||||||
Command: cmd
|
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
CoreClient.commandItems = [
|
||||||
|
{
|
||||||
|
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 util = new Util();
|
||||||
|
util.loadSlashCommands(client);
|
||||||
|
|
||||||
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
expect(setTokenMock).toHaveBeenCalledWith("token");
|
||||||
|
|
||||||
expect(result.valid).toBeTruthy();
|
expect(setTokenPutMock).toHaveBeenCalledTimes(2);
|
||||||
expect(cmd.execute).toBeCalled();
|
expect(setTokenPutMock).toHaveBeenCalledWith("command url", { body: [ CoreClient.commandItems[0].Command.CommandBuilder ] });
|
||||||
});
|
expect(setTokenPutMock).toHaveBeenCalledWith("guild command url", { body: [ CoreClient.commandItems[1].Command.CommandBuilder ]});
|
||||||
|
|
||||||
test('Given Member Is Null, Expect Failed Result', async () => {
|
expect(client.guilds.cache.has).toHaveBeenCalledTimes(1);
|
||||||
process.env = {
|
expect(client.guilds.cache.has).toHaveBeenCalledWith("123");
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
expect(Routes.applicationCommands).toHaveBeenCalledTimes(1);
|
||||||
member: null
|
expect(Routes.applicationCommands).toHaveBeenCalledWith("clientid");
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
expect(Routes.applicationGuildCommands).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Routes.applicationGuildCommands).toHaveBeenCalledWith("clientid", "123");
|
||||||
|
});
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
test("GIVEN bot is not in a guild for a server command, EXPECT this to be ignored", () => {
|
||||||
Name: "test",
|
process.env.BOT_TOKEN = "token";
|
||||||
Command: cmd
|
process.env.BOT_CLIENTID = "clientid";
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
const client = {
|
||||||
|
guilds: {
|
||||||
|
cache: {
|
||||||
|
has: jest.fn().mockReturnValue(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as Client;
|
||||||
|
|
||||||
const util = new Util();
|
const setTokenPutMock = jest.fn();
|
||||||
|
const setTokenMock = jest.fn().mockReturnValue({
|
||||||
|
put: setTokenPutMock,
|
||||||
|
});
|
||||||
|
|
||||||
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
REST.prototype.setToken = setTokenMock;
|
||||||
|
Routes.applicationCommands = jest.fn().mockReturnValue("command url");
|
||||||
|
Routes.applicationGuildCommands = jest.fn().mockReturnValue("guild command url");
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
CoreClient.commandItems = [
|
||||||
expect(result.message).toBe("Member is not part of message");
|
{
|
||||||
expect(cmd.execute).not.toBeCalled();
|
Name: "server-specific",
|
||||||
});
|
Command: {
|
||||||
|
CommandBuilder: {
|
||||||
|
name: "server",
|
||||||
|
} as SlashCommandBuilder,
|
||||||
|
} as Command,
|
||||||
|
ServerId: "123",
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
test('Given User Does Have Role, Expect Successful Result', async () => {
|
const util = new Util();
|
||||||
process.env = {
|
util.loadSlashCommands(client);
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
expect(Routes.applicationGuildCommands).not.toHaveBeenCalled();
|
||||||
member: {
|
});
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
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).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;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
cmd._roles = [ "Moderator" ];
|
|
||||||
|
|
||||||
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("You require the `Moderator` role to run this command");
|
|
||||||
expect(cmd.execute).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given command is set to disabled, Expect command to not fire', async () => {
|
|
||||||
process.env = {
|
|
||||||
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;
|
|
||||||
|
|
||||||
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("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 Events Are Loaded, Expect Successful Result', () => {
|
test("GIVEN event type is channelCreate, EXPECT event function to be executed", () => {
|
||||||
process.env = {
|
const client = {
|
||||||
BOT_TOKEN: 'TOKEN',
|
on: jest.fn(),
|
||||||
BOT_PREFIX: '!',
|
} as unknown as Client;
|
||||||
FOLDERS_COMMANDS: 'commands',
|
const events: IEventItem[] = [
|
||||||
FOLDERS_EVENTS: 'events',
|
{
|
||||||
}
|
EventType: EventType.ChannelCreate,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
const client = {
|
const util = new Util();
|
||||||
on: jest.fn(),
|
util.loadEvents(client, events);
|
||||||
} as unknown as Client;
|
|
||||||
|
|
||||||
const evt = mock<Event>();
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("channelCreate", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
const eventItem: IEventItem = {
|
test("GIVEN event type is channelDelete, EXPECT event function to be executed", () => {
|
||||||
Event: evt
|
const client = {
|
||||||
};
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.ChannelDelete,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
const eventItems: IEventItem[] = [ eventItem ];
|
const util = new Util();
|
||||||
|
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);
|
test("GIVEN event type is channelUpdate, EXPECT event function to be executed", () => {
|
||||||
|
const client = {
|
||||||
|
on: jest.fn(),
|
||||||
|
} as unknown as Client;
|
||||||
|
const events: IEventItem[] = [
|
||||||
|
{
|
||||||
|
EventType: EventType.ChannelUpdate,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
const clientOn = jest.spyOn(client, 'on');
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
expect(result.valid).toBeTruthy();
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
expect(clientOn).toBeCalledTimes(13);
|
expect(client.on).toHaveBeenCalledWith("channelUpdate", expect.any(Function));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Given No Events Found, Expect Successful Result', () => {
|
test("GIVEN event type is guildBanAdd, EXPECT event function to be executed", () => {
|
||||||
process.env = {
|
const client = {
|
||||||
BOT_TOKEN: 'TOKEN',
|
on: jest.fn(),
|
||||||
BOT_PREFIX: '!',
|
} as unknown as Client;
|
||||||
FOLDERS_COMMANDS: 'commands',
|
const events: IEventItem[] = [
|
||||||
FOLDERS_EVENTS: 'events',
|
{
|
||||||
}
|
EventType: EventType.GuildBanAdd,
|
||||||
|
ExecutionFunction: jest.fn(),
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
const client = {
|
const util = new Util();
|
||||||
on: jest.fn(),
|
util.loadEvents(client, events);
|
||||||
} as unknown as Client;
|
|
||||||
|
|
||||||
const eventItems: IEventItem[] = [];
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("guildBanAdd", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
const util = new Util();
|
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 result = util.loadEvents(client, eventItems);
|
const util = new Util();
|
||||||
|
util.loadEvents(client, events);
|
||||||
|
|
||||||
const clientOn = jest.spyOn(client, 'on');
|
expect(client.on).toHaveBeenCalledTimes(1);
|
||||||
|
expect(client.on).toHaveBeenCalledWith("guildBanRemove", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
expect(result.valid).toBeTruthy();
|
test("GIVEN event type is guildCreate, EXPECT event function to be executed", () => {
|
||||||
expect(clientOn).toBeCalledTimes(0);
|
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.");
|
||||||
|
});
|
||||||
});
|
});
|
391
tests/commands/501231711271780357/Lobby/add.test.ts
Normal file
391
tests/commands/501231711271780357/Lobby/add.test.ts
Normal file
|
@ -0,0 +1,391 @@
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
82
tests/commands/501231711271780357/Lobby/list.test.ts
Normal file
82
tests/commands/501231711271780357/Lobby/list.test.ts
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
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.");
|
||||||
|
});
|
||||||
|
});
|
112
tests/commands/501231711271780357/Lobby/lobby.test.ts
Normal file
112
tests/commands/501231711271780357/Lobby/lobby.test.ts
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
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**.");
|
||||||
|
});
|
||||||
|
});
|
116
tests/commands/501231711271780357/Lobby/remove.test.ts
Normal file
116
tests/commands/501231711271780357/Lobby/remove.test.ts
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
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.");
|
||||||
|
});
|
||||||
|
});
|
111
tests/commands/501231711271780357/entry.test.ts
Normal file
111
tests/commands/501231711271780357/entry.test.ts
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
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.");
|
||||||
|
});
|
||||||
|
});
|
227
tests/commands/Role/config.test.ts
Normal file
227
tests/commands/Role/config.test.ts
Normal file
|
@ -0,0 +1,227 @@
|
||||||
|
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.");
|
||||||
|
});
|
||||||
|
});
|
475
tests/commands/Role/role.test.ts
Normal file
475
tests/commands/Role/role.test.ts
Normal file
|
@ -0,0 +1,475 @@
|
||||||
|
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");
|
||||||
|
});
|
||||||
|
});
|
32
tests/commands/__snapshots__/rules.test.ts.snap
Normal file
32
tests/commands/__snapshots__/rules.test.ts.snap
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// 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,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
`;
|
38
tests/commands/__snapshots__/warn.test.ts.snap
Normal file
38
tests/commands/__snapshots__/warn.test.ts.snap
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
// 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,8 +1,6 @@
|
||||||
import { Message } from "discord.js";
|
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder, InteractionReplyOptions,SlashCommandBuilder } from "discord.js";
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
import About from "../../src/commands/about";
|
import About from "../../src/commands/about";
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
import EmbedColours from "../../src/constants/EmbedColours";
|
||||||
import PublicEmbed from "../../src/helpers/embeds/PublicEmbed";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env = {};
|
process.env = {};
|
||||||
|
@ -10,143 +8,169 @@ beforeEach(() => {
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test('Expect values set', () => {
|
test('Expect values set', () => {
|
||||||
const about = new About();
|
const command = new About();
|
||||||
|
|
||||||
expect(about._category).toBe("General");
|
expect(command.CommandBuilder).toBeDefined();
|
||||||
|
|
||||||
|
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
||||||
|
|
||||||
|
expect(commandBuilder.name).toBe("about");
|
||||||
|
expect(commandBuilder.description).toBe("About VylBot");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Execute', () => {
|
describe('Execute', () => {
|
||||||
test('Expect embed to be made and sent to the current channel', async () => {
|
test('GIVEN ABOUT_FUNDING and ABOUT_REPO are NOT present in env var, EXPECT embed to be sent without buttons', async () => {
|
||||||
process.env = {
|
// Setup
|
||||||
BOT_VER: "BOT_VER",
|
let replyOptions: InteractionReplyOptions | undefined;
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
const interaction = {
|
||||||
message.channel.send = jest.fn();
|
reply: jest.fn((options: InteractionReplyOptions) => {
|
||||||
|
replyOptions = options;
|
||||||
|
}),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Execution
|
||||||
name: "about",
|
process.env.BOT_VER = "VERSION";
|
||||||
args: [],
|
process.env.BOT_AUTHOR = "AUTHOR";
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
const command = new About();
|
||||||
|
|
||||||
const result = await about.execute(context);
|
await command.execute(interaction);
|
||||||
|
|
||||||
expect(message.channel.send).toBeCalledTimes(1);
|
// Assertion
|
||||||
|
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('Expect embed send to have values', async () => {
|
test('GIVEN ABOUT_FUNDING is present in env var, EXPECT funding button to be sent', async () => {
|
||||||
process.env = {
|
// Setup
|
||||||
BOT_VER: "BOT_VER",
|
let replyOptions: InteractionReplyOptions | undefined;
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
const interaction = {
|
||||||
message.channel.send = jest.fn();
|
reply: jest.fn((options: InteractionReplyOptions) => {
|
||||||
|
replyOptions = options;
|
||||||
|
}),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Execution
|
||||||
name: "about",
|
process.env.BOT_VER = "VERSION";
|
||||||
args: [],
|
process.env.BOT_AUTHOR = "AUTHOR";
|
||||||
message: message
|
process.env.ABOUT_FUNDING = "https://ko-fi.com/vylpes";
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
const command = new About();
|
||||||
|
|
||||||
const result = await about.execute(context);
|
await command.execute(interaction);
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
// Assertion
|
||||||
|
expect(replyOptions?.components?.length).toBe(1);
|
||||||
|
|
||||||
const embed = result.embeds[0];
|
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
|
||||||
|
|
||||||
expect(embed.title).toBe('About');
|
expect(repliedRow.components?.length).toBe(1);
|
||||||
expect(embed.description).toBe('');
|
|
||||||
expect(embed.fields.length).toBe(3);
|
const repliedRowFundingComponent = repliedRow.components![0] as any;
|
||||||
|
|
||||||
|
expect(repliedRowFundingComponent.data.url).toBe("https://ko-fi.com/vylpes");
|
||||||
|
expect(repliedRowFundingComponent.data.label).toBe("Funding");
|
||||||
|
expect(repliedRowFundingComponent.data.style).toBe(ButtonStyle.Link);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Expect version field to have values', async () => {
|
test('GIVEN ABOUT_REPO is present in env var, EXPECT funding button to be sent', async () => {
|
||||||
process.env = {
|
// Setup
|
||||||
BOT_VER: "BOT_VER",
|
let replyOptions: InteractionReplyOptions | undefined;
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
const interaction = {
|
||||||
message.channel.send = jest.fn();
|
reply: jest.fn((options: InteractionReplyOptions) => {
|
||||||
|
replyOptions = options;
|
||||||
|
}),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Execution
|
||||||
name: "about",
|
process.env.BOT_VER = "VERSION";
|
||||||
args: [],
|
process.env.BOT_AUTHOR = "AUTHOR";
|
||||||
message: message
|
process.env.ABOUT_REPO = "https://gitea.vylpes.xyz/rabbitlabs/vylbot-app";
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
const command = new About();
|
||||||
|
|
||||||
const result = await about.execute(context);
|
await command.execute(interaction);
|
||||||
|
|
||||||
const embed = result.embeds[0];
|
// Assertion
|
||||||
const field = embed.fields[0];
|
expect(replyOptions?.components?.length).toBe(1);
|
||||||
|
|
||||||
expect(field.name).toBe('Version');
|
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
|
||||||
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('Expect author field to have values', async () => {
|
test('GIVEN ABOUT_REPO AND ABOUT_FUNDING is present in env var, EXPECT funding button to be sent', async () => {
|
||||||
process.env = {
|
// Setup
|
||||||
BOT_VER: "BOT_VER",
|
let replyOptions: InteractionReplyOptions | undefined;
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
const interaction = {
|
||||||
message.channel.send = jest.fn();
|
reply: jest.fn((options: InteractionReplyOptions) => {
|
||||||
|
replyOptions = options;
|
||||||
|
}),
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Execution
|
||||||
name: "about",
|
process.env.BOT_VER = "VERSION";
|
||||||
args: [],
|
process.env.BOT_AUTHOR = "AUTHOR";
|
||||||
message: message
|
process.env.ABOUT_REPO = "https://gitea.vylpes.xyz/rabbitlabs/vylbot-app";
|
||||||
};
|
process.env.ABOUT_FUNDING = "https://ko-fi.com/vylpes";
|
||||||
|
|
||||||
const about = new About();
|
const command = new About();
|
||||||
|
|
||||||
const result = await about.execute(context);
|
await command.execute(interaction);
|
||||||
|
|
||||||
const embed = result.embeds[0];
|
// Assertion
|
||||||
const field = embed.fields[1];
|
expect(replyOptions?.components?.length).toBe(1);
|
||||||
|
|
||||||
expect(field.name).toBe('Author');
|
const repliedRow = replyOptions?.components![0] as ActionRowBuilder<ButtonBuilder>;
|
||||||
expect(field.value).toBe('BOT_AUTHOR');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Expect version field to have values', async () => {
|
expect(repliedRow.components?.length).toBe(2);
|
||||||
process.env = {
|
|
||||||
BOT_VER: "BOT_VER",
|
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
const repliedRowRepoComponent = repliedRow.components![0] as any;
|
||||||
message.channel.send = jest.fn();
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
expect(repliedRowRepoComponent.data.url).toBe("https://gitea.vylpes.xyz/rabbitlabs/vylbot-app");
|
||||||
name: "about",
|
expect(repliedRowRepoComponent.data.label).toBe("Repo");
|
||||||
args: [],
|
expect(repliedRowRepoComponent.data.style).toBe(ButtonStyle.Link);
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
const repliedRowFundingComponent = repliedRow.components![1] as any;
|
||||||
|
|
||||||
const result = await about.execute(context);
|
expect(repliedRowFundingComponent.data.url).toBe("https://ko-fi.com/vylpes");
|
||||||
|
expect(repliedRowFundingComponent.data.label).toBe("Funding");
|
||||||
const embed = result.embeds[0];
|
expect(repliedRowFundingComponent.data.style).toBe(ButtonStyle.Link);
|
||||||
const field = embed.fields[2];
|
|
||||||
|
|
||||||
expect(field.name).toBe('Date');
|
|
||||||
expect(field.value).toBe('BOT_DATE');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
792
tests/commands/audits.test.ts
Normal file
792
tests/commands/audits.test.ts
Normal file
|
@ -0,0 +1,792 @@
|
||||||
|
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
125
tests/commands/bunny.test.ts
Normal file
125
tests/commands/bunny.test.ts
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
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,178 +1,216 @@
|
||||||
import { Message } from "discord.js";
|
import { ChatInputCommandInteraction, PermissionsBitField, SlashCommandBuilder, SlashCommandNumberOption, TextChannel } from "discord.js";
|
||||||
import Clear from "../../src/commands/clear";
|
import Command 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", () => {
|
||||||
process.env = {
|
const command = new Command();
|
||||||
ROLES_MODERATOR: "Moderator"
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
expect(command.CommandBuilder).toBeDefined();
|
||||||
|
|
||||||
expect(clear._category).toBe('Moderation');
|
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
||||||
expect(clear._roles.length).toBe(1);
|
|
||||||
expect(clear._roles[0]).toBe('Moderator');
|
expect(commandBuilder.name).toBe("clear");
|
||||||
|
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 valid arguments, expect messages to be cleared', async () => {
|
test("GIVEN input is valid, EXPECT messages to be cleared", async () => {
|
||||||
const messageChannelSend = jest.fn();
|
// Arrange
|
||||||
const messageChannelBulkDelete = jest.fn();
|
const channel = {
|
||||||
|
manageable: true,
|
||||||
|
bulkDelete: jest.fn(),
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const message = {
|
const interaction = {
|
||||||
channel: {
|
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||||
send: messageChannelSend,
|
channel: channel,
|
||||||
bulkDelete: messageChannelBulkDelete
|
options: {
|
||||||
}
|
getNumber: jest.fn().mockReturnValue(50),
|
||||||
} as unknown as Message;
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as ChatInputCommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Act
|
||||||
name: 'clear',
|
const command = new Command();
|
||||||
args: ['5'],
|
await command.execute(interaction);
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
// Assert
|
||||||
const result = await clear.execute(context);
|
expect(interaction.isChatInputCommand).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
expect(interaction.options.getNumber).toHaveBeenCalledTimes(1);
|
||||||
expect(messageChannelBulkDelete).toBeCalledWith(5);
|
expect(interaction.options.getNumber).toHaveBeenCalledWith("count");
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// PublicEmbed
|
expect(channel.bulkDelete).toHaveBeenCalledTimes(1);
|
||||||
const publicEmbed = result.embeds[0];
|
expect(channel.bulkDelete).toHaveBeenCalledWith(50);
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('');
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
expect(publicEmbed.description).toBe('5 message(s) were removed');
|
expect(interaction.reply).toHaveBeenCalledWith("50 message(s) were removed.");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Given argument is not given, expect error embed to be sent', async () => {
|
test("GIVEN interaction is NOT a chat input command, EXPECT nothing to happen", async () => {
|
||||||
const messageChannelSend = jest.fn();
|
// Arrange
|
||||||
const messageChannelBulkDelete = jest.fn();
|
const channel = {
|
||||||
|
manageable: true,
|
||||||
|
bulkDelete: jest.fn(),
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const message = {
|
const interaction = {
|
||||||
channel: {
|
isChatInputCommand: jest.fn().mockReturnValue(false),
|
||||||
send: messageChannelSend,
|
channel: channel,
|
||||||
bulkDelete: messageChannelBulkDelete
|
options: {
|
||||||
}
|
getNumber: jest.fn().mockReturnValue(50),
|
||||||
} as unknown as Message;
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as ChatInputCommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Act
|
||||||
name: 'clear',
|
const command = new Command();
|
||||||
args: [],
|
await command.execute(interaction);
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
// Assert
|
||||||
const result = await clear.execute(context);
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
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 argument is not a number, expect error embed to be sent', async () => {
|
test("GIVEN interaction.channel is null, EXPECT nothing to happen", async () => {
|
||||||
const messageChannelSend = jest.fn();
|
// Arrange
|
||||||
const messageChannelBulkDelete = jest.fn();
|
const interaction = {
|
||||||
|
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||||
|
channel: null,
|
||||||
|
options: {
|
||||||
|
getNumber: jest.fn().mockReturnValue(50),
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as ChatInputCommandInteraction;
|
||||||
|
|
||||||
const message = {
|
// Act
|
||||||
channel: {
|
const command = new Command();
|
||||||
send: messageChannelSend,
|
await command.execute(interaction);
|
||||||
bulkDelete: messageChannelBulkDelete
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Assert
|
||||||
name: 'clear',
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
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 argument is less than 1, expect error embed to be sent', async () => {
|
test("GIVEN totalClear input is NOT supplied, EXPECT invalid error", async () => {
|
||||||
const messageChannelSend = jest.fn();
|
// Arrange
|
||||||
const messageChannelBulkDelete = jest.fn();
|
const channel = {
|
||||||
|
manageable: true,
|
||||||
|
bulkDelete: jest.fn(),
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const message = {
|
const interaction = {
|
||||||
channel: {
|
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||||
send: messageChannelSend,
|
channel: channel,
|
||||||
bulkDelete: messageChannelBulkDelete
|
options: {
|
||||||
}
|
getNumber: jest.fn().mockReturnValue(null),
|
||||||
} as unknown as Message;
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as ChatInputCommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Act
|
||||||
name: 'clear',
|
const command = new Command();
|
||||||
args: ['0'],
|
await command.execute(interaction);
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
// Assert
|
||||||
const result = await clear.execute(context);
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
expect(channel.bulkDelete).not.toHaveBeenCalled();
|
||||||
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 argument is more than 100, expect error embed to be sent', async () => {
|
test("GIVEN totalClear is less than or equal to 0, EXPECT invalid error", async () => {
|
||||||
const messageChannelSend = jest.fn();
|
// Arrange
|
||||||
const messageChannelBulkDelete = jest.fn();
|
const channel = {
|
||||||
|
manageable: true,
|
||||||
|
bulkDelete: jest.fn(),
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const message = {
|
const interaction = {
|
||||||
channel: {
|
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||||
send: messageChannelSend,
|
channel: channel,
|
||||||
bulkDelete: messageChannelBulkDelete
|
options: {
|
||||||
}
|
getNumber: jest.fn().mockReturnValue(0),
|
||||||
} as unknown as Message;
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
} as unknown as ChatInputCommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Act
|
||||||
name: 'clear',
|
const command = new Command();
|
||||||
args: ['101'],
|
await command.execute(interaction);
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
// Assert
|
||||||
const result = await clear.execute(context);
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Please specify an amount between 1 and 100.");
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
expect(channel.bulkDelete).not.toHaveBeenCalled();
|
||||||
expect(messageChannelBulkDelete).not.toBeCalled();
|
});
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// ErrorEmbed
|
test("GIVEN totalClear is greater than 100, EXPECT invalid error", async () => {
|
||||||
const errorEmbed = result.embeds[0];
|
// Arrange
|
||||||
|
const channel = {
|
||||||
|
manageable: true,
|
||||||
|
bulkDelete: jest.fn(),
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
expect(errorEmbed.title).toBeNull();
|
const interaction = {
|
||||||
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
|
isChatInputCommand: jest.fn().mockReturnValue(true),
|
||||||
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
261
tests/commands/code.test.ts
Normal file
261
tests/commands/code.test.ts
Normal file
|
@ -0,0 +1,261 @@
|
||||||
|
import { APIEmbed, APIVersion, ChatInputCommandInteraction, CommandInteraction, SlashCommandBuilder, SlashCommandSubcommandBuilder } from "discord.js";
|
||||||
|
import Command from "../../src/commands/code";
|
||||||
|
import StringTools from "../../src/helpers/StringTools";
|
||||||
|
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
||||||
|
|
||||||
|
describe('constructor', () => {
|
||||||
|
test("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();
|
||||||
|
});
|
||||||
|
});
|
612
tests/commands/config.test.ts
Normal file
612
tests/commands/config.test.ts
Normal file
|
@ -0,0 +1,612 @@
|
||||||
|
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");
|
||||||
|
});
|
||||||
|
});
|
285
tests/commands/disable.test.ts
Normal file
285
tests/commands/disable.test.ts
Normal file
|
@ -0,0 +1,285 @@
|
||||||
|
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");
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,136 +0,0 @@
|
||||||
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');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,267 +0,0 @@
|
||||||
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();
|
|
||||||
});
|
|
||||||
});
|
|
79
tests/commands/ignore.test.ts
Normal file
79
tests/commands/ignore.test.ts
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
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
|
@ -1,815 +0,0 @@
|
||||||
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,262 +1,242 @@
|
||||||
import { Message, MessageEmbed } from "discord.js";
|
import { CommandInteraction, EmbedBuilder, InteractionResponse, Message, SlashCommandBuilder, SlashCommandStringOption } from "discord.js";
|
||||||
import Poll from "../../src/commands/poll";
|
import Command from "../../src/commands/poll";
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
import EmbedColours from "../../src/constants/EmbedColours";
|
||||||
|
|
||||||
describe('Constructor', () => {
|
describe('Constructor', () => {
|
||||||
test('Expect properties to be set', () => {
|
test('EXPECT properties to be set', () => {
|
||||||
const poll = new Poll();
|
const command = new Command();
|
||||||
|
|
||||||
expect(poll._category).toBe('General');
|
expect(command.CommandBuilder).toBeDefined();
|
||||||
|
|
||||||
|
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('Given input is valid, expect poll to be generated', async () => {
|
test("EXPECT a poll to be created", async () => {
|
||||||
const returnMessageReact = jest.fn();
|
let sentEmbed: EmbedBuilder | undefined;
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
|
// Arrange
|
||||||
const message = {
|
const message = {
|
||||||
channel: {
|
react: jest.fn(),
|
||||||
send: messageChannelSend
|
} as unknown as Message<boolean>;
|
||||||
|
|
||||||
|
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" }),
|
||||||
},
|
},
|
||||||
delete: messageDelete,
|
reply: jest.fn().mockImplementation((options: any) => {
|
||||||
deletable: true
|
sentEmbed = options.embeds[0];
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
return response;
|
||||||
name: 'poll',
|
}),
|
||||||
args: ['Test', 'title;', 'one;', 'two'],
|
user: {
|
||||||
message: message
|
username: "username",
|
||||||
};
|
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
|
||||||
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const poll = new Poll();
|
// Act
|
||||||
|
const command = new Command();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
// Assert
|
||||||
|
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(messageChannelSend).toBeCalledTimes(1);
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
expect(messageDelete).toBeCalledTimes(1);
|
|
||||||
expect(returnMessageReact).toBeCalledTimes(2);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
expect(sentEmbed).toBeDefined();
|
||||||
|
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");
|
||||||
|
|
||||||
// Embed
|
expect(interaction.user.avatarURL).toHaveBeenCalledTimes(1);
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('Test title');
|
expect(response.fetch).toHaveBeenCalledTimes(1);
|
||||||
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 message is not deletable by client, expect it not to attempt deletion', async () => {
|
test("GIVEN option3 is not supplied, EXPECT a 2 option poll to be created", async () => {
|
||||||
const returnMessageReact = jest.fn();
|
let sentEmbed: EmbedBuilder | undefined;
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
|
// Arrange
|
||||||
const message = {
|
const message = {
|
||||||
channel: {
|
react: jest.fn(),
|
||||||
send: messageChannelSend
|
} as unknown as Message<boolean>;
|
||||||
|
|
||||||
|
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" }),
|
||||||
},
|
},
|
||||||
delete: messageDelete,
|
reply: jest.fn().mockImplementation((options: any) => {
|
||||||
deletable: false
|
sentEmbed = options.embeds[0];
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
return response;
|
||||||
name: 'poll',
|
}),
|
||||||
args: ['Test', 'title;', 'one;', 'two'],
|
user: {
|
||||||
message: message
|
username: "username",
|
||||||
};
|
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
|
||||||
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const poll = new Poll();
|
// Act
|
||||||
|
const command = new Command();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
// Assert
|
||||||
|
expect(sentEmbed).toBeDefined();
|
||||||
|
expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2");
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
expect(message.react).toHaveBeenCalledTimes(2);
|
||||||
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 no arguments, expect error embed', async () => {
|
test("GIVEN option4 is not supplied, EXPECT a 3 option poll to be created", async () => {
|
||||||
const returnMessageReact = jest.fn();
|
let sentEmbed: EmbedBuilder | undefined;
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
|
// Arrange
|
||||||
const message = {
|
const message = {
|
||||||
channel: {
|
react: jest.fn(),
|
||||||
send: messageChannelSend
|
} as unknown as Message<boolean>;
|
||||||
|
|
||||||
|
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" }),
|
||||||
},
|
},
|
||||||
delete: messageDelete,
|
reply: jest.fn().mockImplementation((options: any) => {
|
||||||
deletable: true
|
sentEmbed = options.embeds[0];
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
return response;
|
||||||
name: 'poll',
|
}),
|
||||||
args: [],
|
user: {
|
||||||
message: message
|
username: "username",
|
||||||
};
|
avatarURL: jest.fn().mockReturnValue("https://avatarurl.com/user.png"),
|
||||||
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const poll = new Poll();
|
// Act
|
||||||
|
const command = new Command();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
// Assert
|
||||||
|
expect(sentEmbed).toBeDefined();
|
||||||
|
expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2\n3️⃣ Option 3");
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
expect(message.react).toHaveBeenCalledTimes(3);
|
||||||
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 only 1 option, expect error embed', async () => {
|
test("GIVEN option5 is not supplied, EXPECT a 4 option poll to be created", async () => {
|
||||||
const returnMessageReact = jest.fn();
|
let sentEmbed: EmbedBuilder | undefined;
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
|
// Arrange
|
||||||
const message = {
|
const message = {
|
||||||
channel: {
|
react: jest.fn(),
|
||||||
send: messageChannelSend
|
} as unknown as Message<boolean>;
|
||||||
|
|
||||||
|
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" }),
|
||||||
},
|
},
|
||||||
delete: messageDelete,
|
reply: jest.fn().mockImplementation((options: any) => {
|
||||||
deletable: true
|
sentEmbed = options.embeds[0];
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
return response;
|
||||||
name: 'poll',
|
}),
|
||||||
args: ['Test', 'title;', 'one'],
|
user: {
|
||||||
message: message
|
username: "username",
|
||||||
};
|
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
|
|
||||||
},
|
},
|
||||||
delete: messageDelete,
|
} as unknown as CommandInteraction;
|
||||||
deletable: true
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
// Act
|
||||||
name: 'poll',
|
const command = new Command();
|
||||||
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine'],
|
await command.execute(interaction);
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const poll = new Poll();
|
// Assert
|
||||||
|
expect(sentEmbed).toBeDefined();
|
||||||
|
expect(sentEmbed!.data.description).toBe("1️⃣ Option 1\n2️⃣ Option 2\n3️⃣ Option 3\n4️⃣ Option 4");
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
expect(message.react).toHaveBeenCalledTimes(4);
|
||||||
|
|
||||||
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');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -1,411 +0,0 @@
|
||||||
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,106 +1,25 @@
|
||||||
import { Message } from "discord.js";
|
import Command from "../../src/commands/rules";
|
||||||
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', () => {
|
||||||
process.env = {
|
const command = new Command();
|
||||||
ROLES_MODERATOR: "Moderator"
|
|
||||||
};
|
|
||||||
|
|
||||||
const rules = new Rules();
|
expect(command.CommandBuilder).toMatchSnapshot();
|
||||||
|
|
||||||
expect(rules._category).toBe("Admin");
|
|
||||||
expect(rules._roles.length).toBe(1);
|
|
||||||
expect(rules._roles[0]).toBe("Moderator");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Execute', () => {
|
describe('Execute', () => {
|
||||||
test('Given rules exist, expect rules to be sent to current channel', () => {
|
test.todo("GIVEN interaction is not a chat input command, EXPECT nothing to happen");
|
||||||
process.env = {
|
|
||||||
COMMANDS_RULES_FILE: 'rules/rules.json'
|
|
||||||
};
|
|
||||||
|
|
||||||
process.cwd = jest.fn()
|
test.todo("GIVEN invalid subcommand is given, EXPECT invalid error");
|
||||||
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
});
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
describe("access", () => {
|
||||||
|
test.todo("GIVEN interaction.guildId is null, EXPECT nothing to happen");
|
||||||
const message = {
|
|
||||||
channel: {
|
test.todo("GIVEN rules.access.label setting is not supplied, EXPECT label to be defaulted");
|
||||||
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");
|
|
||||||
});
|
|
||||||
});
|
});
|
11
tests/commands/setup.test.ts
Normal file
11
tests/commands/setup.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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, Embed, EmbedBuilder, EmbedField, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, MessagePayload, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, EmbedBuilder, EmbedField, Guild, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, 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: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
|
|
||||||
const dmChannel = {
|
const dmChannel = {
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||||
|
@ -58,6 +58,7 @@ 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,
|
||||||
|
@ -106,7 +107,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).toBeCalledWith(1000, 'Test reason');
|
expect(timeoutFunc).toHaveBeenCalledWith(1000, 'Test reason');
|
||||||
|
|
||||||
// EXPECT embeds to be sent
|
// EXPECT embeds to be sent
|
||||||
expect(embeds).toBeDefined();
|
expect(embeds).toBeDefined();
|
||||||
|
@ -116,6 +117,7 @@ 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);
|
||||||
|
|
||||||
|
@ -154,6 +156,7 @@ 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);
|
||||||
|
|
||||||
|
@ -415,7 +418,8 @@ describe('execute', () => {
|
||||||
user: {
|
user: {
|
||||||
id: 'userId',
|
id: 'userId',
|
||||||
tag: 'userTag',
|
tag: 'userTag',
|
||||||
} as User,
|
avatarURL: jest.fn().mockReturnValue("https://google.com/test.png"),
|
||||||
|
} as unknown as User,
|
||||||
member: {
|
member: {
|
||||||
manageable: false,
|
manageable: false,
|
||||||
} as GuildMember
|
} as GuildMember
|
||||||
|
@ -482,8 +486,9 @@ 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,
|
||||||
|
@ -567,6 +572,7 @@ 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,
|
||||||
|
@ -641,6 +647,7 @@ 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,
|
||||||
|
@ -690,8 +697,8 @@ describe('execute', () => {
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
const timeoutFunc = jest.fn();
|
||||||
|
|
||||||
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let dmChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
let logsChannelSentEmbeds: readonly (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
||||||
|
|
||||||
const dmChannel = {
|
const dmChannel = {
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||||
|
@ -704,6 +711,7 @@ 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,
|
||||||
|
|
|
@ -1,813 +0,0 @@
|
||||||
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,485 +1,494 @@
|
||||||
import { GuildMember, Message, TextChannel, User } from "discord.js";
|
import {APIEmbed, APIEmbedField, CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandUserOption, TextChannel} from "discord.js";
|
||||||
import Warn from "../../src/commands/warn";
|
import Warn from "../../src/commands/warn";
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
||||||
|
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', () => {
|
||||||
process.env.ROLES_MODERATOR = 'Moderator';
|
const command = new Warn();
|
||||||
|
|
||||||
const warn = new Warn();
|
expect(command.CommandBuilder).toBeDefined();
|
||||||
|
|
||||||
expect(warn._category).toBe('Moderation');
|
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
||||||
expect(warn._roles.length).toBe(1);
|
|
||||||
expect(warn._roles[0]).toBe('Moderator');
|
expect(commandBuilder.name).toBe("warn");
|
||||||
|
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('Given user has permission, expect user to be warnned', async () => {
|
test("EXPECT user to be warned", async () => {
|
||||||
process.env = {
|
let sentEmbeds: EmbedBuilder[] | undefined;
|
||||||
ROLES_MODERATOR: 'Moderator',
|
let savedAudit: Audit | undefined;
|
||||||
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 = {
|
||||||
name: 'mod-logs',
|
send: jest.fn().mockImplementation((opts: any) => {
|
||||||
send: jest.fn()
|
sentEmbeds = opts.embeds;
|
||||||
|
}),
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
const interaction = {
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
reply: jest.fn(),
|
||||||
.mockReturnValue(mentionedUser);
|
options: {
|
||||||
const messageGuildMember = jest.fn()
|
get: jest.fn()
|
||||||
.mockReturnValue(mentionedMember);
|
.mockReturnValueOnce(targetUser)
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
.mockReturnValue(reason),
|
||||||
.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: messageGuildChannelsCacheFind
|
find: jest.fn().mockReturnValue(logChannel),
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
available: true
|
|
||||||
},
|
},
|
||||||
author: {
|
guildId: "guildId",
|
||||||
tag: 'AUTHORTAG'
|
user: {
|
||||||
}
|
id: "moderatorId",
|
||||||
} as unknown as Message;
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
||||||
|
savedAudit = audit;
|
||||||
|
});
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
// Act
|
||||||
|
const command = new Warn();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
// Assert
|
||||||
expect(logChannel.send).toBeCalledTimes(1);
|
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 user has permissions, expect embeds to be correct', async () => {
|
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
|
||||||
process.env = {
|
let sentEmbeds: EmbedBuilder[] | undefined;
|
||||||
ROLES_MODERATOR: 'Moderator',
|
let savedAudit: Audit | undefined;
|
||||||
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 = {
|
||||||
name: 'mod-logs',
|
send: jest.fn().mockImplementation((opts: any) => {
|
||||||
send: jest.fn()
|
sentEmbeds = opts.embeds;
|
||||||
|
}),
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
const interaction = {
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
reply: jest.fn(),
|
||||||
.mockReturnValue(mentionedUser);
|
options: {
|
||||||
const messageGuildMember = jest.fn()
|
get: jest.fn()
|
||||||
.mockReturnValue(mentionedMember);
|
.mockReturnValueOnce(targetUser)
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
.mockReturnValue(reason),
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
},
|
||||||
mentions: {
|
guildId: "guildId",
|
||||||
users: {
|
user: {
|
||||||
first: messageMentionsUsersFirst
|
id: "moderatorId",
|
||||||
}
|
|
||||||
},
|
},
|
||||||
guild: {
|
} as unknown as CommandInteraction;
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
||||||
|
savedAudit = audit;
|
||||||
|
});
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
// Act
|
||||||
|
const command = new Warn();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
// Assert
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
|
||||||
const logEmbed = result.embeds[0];
|
expect(Audit.prototype.Save).not.toHaveBeenCalled();
|
||||||
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 user has permission, expect logEmbed fields to be correct', async () => {
|
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
||||||
process.env = {
|
let sentEmbeds: EmbedBuilder[] | undefined;
|
||||||
ROLES_MODERATOR: 'Moderator',
|
let savedAudit: Audit | undefined;
|
||||||
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 = {
|
||||||
name: 'mod-logs',
|
send: jest.fn().mockImplementation((opts: any) => {
|
||||||
send: jest.fn()
|
sentEmbeds = opts.embeds;
|
||||||
|
}),
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
const interaction = {
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
reply: jest.fn(),
|
||||||
.mockReturnValue(mentionedUser);
|
options: {
|
||||||
const messageGuildMember = jest.fn()
|
get: jest.fn()
|
||||||
.mockReturnValue(mentionedMember);
|
.mockReturnValueOnce(targetUser)
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
.mockReturnValue(reason),
|
||||||
.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: messageGuildChannelsCacheFind
|
find: jest.fn().mockReturnValue(logChannel),
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
available: true
|
|
||||||
},
|
},
|
||||||
author: {
|
user: {
|
||||||
tag: 'AUTHORTAG'
|
id: "moderatorId",
|
||||||
}
|
},
|
||||||
} as unknown as Message;
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
||||||
|
savedAudit = audit;
|
||||||
|
});
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
// Act
|
||||||
|
const command = new Warn();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
const logEmbed = result.embeds[0];
|
// Assert
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
|
||||||
const fieldUser = logEmbed.fields[0];
|
expect(Audit.prototype.Save).not.toHaveBeenCalled();
|
||||||
const fieldModerator = logEmbed.fields[1];
|
|
||||||
const fieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(fieldUser.name).toBe("User");
|
|
||||||
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 user is not mentioned, expect error embed to be sent', async () => {
|
test("GIVEN reasonInput is null, EXPECT reason to be defaulted", async () => {
|
||||||
process.env = {
|
let sentEmbeds: EmbedBuilder[] | undefined;
|
||||||
ROLES_MODERATOR: 'Moderator',
|
let savedAudit: Audit | undefined;
|
||||||
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 mentionedMember = {
|
|
||||||
warnnable: true,
|
|
||||||
warn: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
const logChannel = {
|
||||||
name: 'mod-logs',
|
send: jest.fn().mockImplementation((opts: any) => {
|
||||||
send: jest.fn()
|
sentEmbeds = opts.embeds;
|
||||||
|
}),
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
const interaction = {
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
reply: jest.fn(),
|
||||||
.mockReturnValue(null);
|
options: {
|
||||||
const messageGuildMember = jest.fn()
|
get: jest.fn()
|
||||||
.mockReturnValue(mentionedMember);
|
.mockReturnValueOnce(targetUser)
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
.mockReturnValue(null),
|
||||||
.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: messageGuildChannelsCacheFind
|
find: jest.fn().mockReturnValue(logChannel),
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
available: true
|
|
||||||
},
|
},
|
||||||
author: {
|
guildId: "guildId",
|
||||||
tag: 'AUTHORTAG'
|
user: {
|
||||||
}
|
id: "moderatorId",
|
||||||
} as unknown as Message;
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
||||||
|
savedAudit = audit;
|
||||||
|
});
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
// Act
|
||||||
|
const command = new Warn();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
// Assert
|
||||||
expect(logChannel.send).not.toBeCalled();
|
expect(sentEmbeds).toBeDefined();
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
expect(sentEmbeds![0].data.fields).toBeDefined();
|
||||||
|
expect(sentEmbeds![0].data.fields!.length).toBe(2);
|
||||||
|
|
||||||
const embedError = result.embeds[0];
|
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 member is not in server, expect error embed to be sent', async () => {
|
test("GIVEN reasonInput.value is undefined, EXPECT reason to be defaulted", async () => {
|
||||||
process.env = {
|
let sentEmbeds: EmbedBuilder[] | undefined;
|
||||||
ROLES_MODERATOR: 'Moderator',
|
let savedAudit: Audit | undefined;
|
||||||
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 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 = {
|
||||||
name: 'mod-logs',
|
send: jest.fn().mockImplementation((opts: any) => {
|
||||||
send: jest.fn()
|
sentEmbeds = opts.embeds;
|
||||||
|
}),
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
const interaction = {
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
reply: jest.fn(),
|
||||||
.mockReturnValue(mentionedUser);
|
options: {
|
||||||
const messageGuildMember = jest.fn()
|
get: jest.fn()
|
||||||
.mockReturnValue(null);
|
.mockReturnValueOnce(targetUser)
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
.mockReturnValue(reason),
|
||||||
.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: messageGuildChannelsCacheFind
|
find: jest.fn().mockReturnValue(logChannel),
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
available: true
|
|
||||||
},
|
},
|
||||||
author: {
|
guildId: "guildId",
|
||||||
tag: 'AUTHORTAG'
|
user: {
|
||||||
}
|
id: "moderatorId",
|
||||||
} as unknown as Message;
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
SettingsHelper.GetSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
||||||
|
savedAudit = audit;
|
||||||
|
});
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
// Act
|
||||||
|
const command = new Warn();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
// Assert
|
||||||
expect(logChannel.send).not.toBeCalled();
|
expect(sentEmbeds).toBeDefined();
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
expect(sentEmbeds![0].data.fields).toBeDefined();
|
||||||
|
expect(sentEmbeds![0].data.fields!.length).toBe(2);
|
||||||
|
|
||||||
const embedError = result.embeds[0];
|
const logEmbedReasonField = sentEmbeds![0].data.fields!.find(x => x.name == "Reason");
|
||||||
|
|
||||||
expect(embedError.description).toBe('User is not in this server');
|
expect(logEmbedReasonField).toBeDefined();
|
||||||
|
expect(logEmbedReasonField!.value).toBe("*none*");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Given guild is unavailable, expect return and do nothing', async () => {
|
test("GIVEN channels.logs.mod setting is not found, EXPECT command to return", async () => {
|
||||||
process.env = {
|
let sentEmbeds: EmbedBuilder[] | undefined;
|
||||||
ROLES_MODERATOR: 'Moderator',
|
let savedAudit: Audit | undefined;
|
||||||
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 = {
|
||||||
name: 'mod-logs',
|
send: jest.fn().mockImplementation((opts: any) => {
|
||||||
send: jest.fn()
|
sentEmbeds = opts.embeds;
|
||||||
|
}),
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
const interaction = {
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
reply: jest.fn(),
|
||||||
.mockReturnValue(mentionedUser);
|
options: {
|
||||||
const messageGuildMember = jest.fn()
|
get: jest.fn()
|
||||||
.mockReturnValue(mentionedMember);
|
.mockReturnValueOnce(targetUser)
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
.mockReturnValue(reason),
|
||||||
.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: messageGuildChannelsCacheFind
|
find: jest.fn().mockReturnValue(logChannel),
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
available: false
|
|
||||||
},
|
},
|
||||||
author: {
|
guildId: "guildId",
|
||||||
tag: 'AUTHORTAG'
|
user: {
|
||||||
}
|
id: "moderatorId",
|
||||||
} as unknown as Message;
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
const context: ICommandContext = {
|
SettingsHelper.GetSetting = jest.fn().mockResolvedValue(undefined);
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
Audit.prototype.Save = jest.fn().mockImplementation((_, audit: Audit) => {
|
||||||
message: message
|
savedAudit = audit;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const command = new Warn();
|
||||||
|
await command.execute(interaction);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("Successfully warned user.");
|
||||||
|
|
||||||
|
expect(Audit.prototype.Save).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
expect(logChannel.send).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("GIVEN channel is not found, EXPECT logEmbed to not be sent", 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 warn = new Warn();
|
const reason = {
|
||||||
|
value: "Test reason",
|
||||||
|
};
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
const logChannel = {
|
||||||
|
send: jest.fn().mockImplementation((opts: any) => {
|
||||||
|
sentEmbeds = opts.embeds;
|
||||||
|
}),
|
||||||
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
expect(messageChannelSend).not.toBeCalled();
|
const interaction = {
|
||||||
expect(logChannel.send).not.toBeCalled();
|
reply: jest.fn(),
|
||||||
expect(result.embeds.length).toBe(0);
|
options: {
|
||||||
|
get: jest.fn()
|
||||||
|
.mockReturnValueOnce(targetUser)
|
||||||
|
.mockReturnValue(reason),
|
||||||
|
},
|
||||||
|
guild: {
|
||||||
|
channels: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(undefined),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
guildId: "guildId",
|
||||||
|
user: {
|
||||||
|
id: "moderatorId",
|
||||||
|
},
|
||||||
|
} 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).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();
|
||||||
});
|
});
|
||||||
});
|
});
|
53
tests/contracts/BaseEntity.test.ts
Normal file
53
tests/contracts/BaseEntity.test.ts
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
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");
|
||||||
|
});
|
13
tests/database/entities/501231711271780357/Lobby.test.ts
Normal file
13
tests/database/entities/501231711271780357/Lobby.test.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
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");
|
||||||
|
});
|
11
tests/database/entities/Audit.test.ts
Normal file
11
tests/database/entities/Audit.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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");
|
||||||
|
});
|
7
tests/database/entities/IgnoredChannel.test.ts
Normal file
7
tests/database/entities/IgnoredChannel.test.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
describe("constructor", () => {
|
||||||
|
test.todo("EXPECT properties to be defined");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("IsChannelIgnored", () => {
|
||||||
|
test.todo("EXPECT entity with channel id to be returned");
|
||||||
|
});
|
13
tests/database/entities/Role.test.ts
Normal file
13
tests/database/entities/Role.test.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
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");
|
||||||
|
});
|
11
tests/database/entities/Server.test.ts
Normal file
11
tests/database/entities/Server.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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");
|
||||||
|
});
|
13
tests/database/entities/Setting.test.ts
Normal file
13
tests/database/entities/Setting.test.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
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");
|
||||||
|
});
|
|
@ -1,164 +0,0 @@
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
13
tests/events/MemberEvents/GuildMemberAdd.test.ts
Normal file
13
tests/events/MemberEvents/GuildMemberAdd.test.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
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");
|
||||||
|
});
|
15
tests/events/MemberEvents/GuildMemberRemove.test.ts
Normal file
15
tests/events/MemberEvents/GuildMemberRemove.test.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
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,235 +1,5 @@
|
||||||
import { GuildMember, TextChannel } from "discord.js";
|
describe('GuildMemberUpdate', () => {
|
||||||
import GuildMemberUpdate from "../../../src/events/MemberEvents/GuildMemberUpdate";
|
test.todo("GIVEN user nickname has changed, EXPECT function to be executed");
|
||||||
|
|
||||||
beforeEach(() => {
|
test.todo("GIVEN nickname is the same, EXPECT nothing to happen");
|
||||||
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*');
|
|
||||||
});
|
|
||||||
});
|
});
|
|
@ -0,0 +1,15 @@
|
||||||
|
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");
|
||||||
|
});
|
|
@ -1,648 +0,0 @@
|
||||||
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*```');
|
|
||||||
});
|
|
||||||
});
|
|
11
tests/events/MessageEvents/MessageCreate.test.ts
Normal file
11
tests/events/MessageEvents/MessageCreate.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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");
|
||||||
|
});
|
|
@ -0,0 +1,23 @@
|
||||||
|
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");
|
||||||
|
});
|
21
tests/events/MessageEvents/MessageDelete.test.ts
Normal file
21
tests/events/MessageEvents/MessageDelete.test.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
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");
|
||||||
|
});
|
23
tests/events/MessageEvents/MessageUpdate.test.ts
Normal file
23
tests/events/MessageEvents/MessageUpdate.test.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
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");
|
||||||
|
});
|
31
tests/helpers/AuditTools.test.ts
Normal file
31
tests/helpers/AuditTools.test.ts
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
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");
|
||||||
|
});
|
7
tests/helpers/MigrationHelper.test.ts
Normal file
7
tests/helpers/MigrationHelper.test.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
describe('Up', () => {
|
||||||
|
test.todo("EXPECT up query to be executed");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Down", () => {
|
||||||
|
test.todo("EXPECT down query to be executed");
|
||||||
|
});
|
15
tests/helpers/SettingsHelper.test.ts
Normal file
15
tests/helpers/SettingsHelper.test.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
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,11 +1,15 @@
|
||||||
import StringTools from "../../src/helpers/StringTools";
|
|
||||||
|
|
||||||
describe('Capitalise', () => {
|
describe('Capitalise', () => {
|
||||||
test('Expect sentence to be captilised', () => {
|
test.todo('EXPECT sentence to be capitalised');
|
||||||
const inputString = 'the big brown fox jumps over the lazy dog';
|
});
|
||||||
|
|
||||||
const result = StringTools.Capitalise(inputString);
|
describe('CapitaliseArray', () => {
|
||||||
|
test.todo("EXPECT words in array to be capitalised");
|
||||||
expect(result).toBe('The Big Brown Fox Jumps Over The Lazy Dog');
|
});
|
||||||
});
|
|
||||||
|
describe("RandomString", () => {
|
||||||
|
test.todo("EXPECT random string of length to be returned");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("ReplaceAll", () => {
|
||||||
|
test.todo("EXPECT all instances of substring to be replaced");
|
||||||
});
|
});
|
37
tests/helpers/TimeLengthInput.test.ts
Normal file
37
tests/helpers/TimeLengthInput.test.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
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");
|
||||||
|
});
|
|
@ -1,57 +0,0 @@
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,292 +0,0 @@
|
||||||
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");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,407 +0,0 @@
|
||||||
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");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,67 +0,0 @@
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
11
tests/registry.test.ts
Normal file
11
tests/registry.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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