This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
vylbot-core/tests/client/util.test.ts

422 lines
11 KiB
TypeScript
Raw Normal View History

2021-08-19 17:21:54 +01:00
import { Util } from "../../src/client/util";
2021-08-20 14:59:41 +01:00
import { Client, Guild, Message, Role, SnowflakeUtil, TextChannel, User } from "discord.js";
2021-08-19 17:21:54 +01:00
import fs from "fs";
jest.mock("fs");
beforeEach(() => {
fs.existsSync = jest.fn();
});
describe('LoadCommand', () => {
2021-09-29 17:52:21 +01:00
test('Given Successful Exection, Expect Successful Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
2021-08-19 17:27:47 +01:00
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeTruthy();
});
2021-09-29 17:52:21 +01:00
test('Given Member Is Null, Expect Failed Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: null
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Member is not part of message");
});
2021-09-29 17:52:21 +01:00
test('Given Folder Does Not Exist, Expect Failed Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(false);
2021-08-20 15:42:46 +01:00
const message = {
2021-08-19 17:27:47 +01:00
member: {
2021-08-20 15:42:46 +01:00
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
2021-08-19 17:27:47 +01:00
},
2021-08-20 15:42:46 +01:00
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command folder does not exist");
});
2021-09-29 17:52:21 +01:00
test('Given File Does Not Exist, Expect Failed Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValueOnce(true)
.mockReturnValue(false);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("File does not exist");
});
2021-09-29 17:52:21 +01:00
test('Given User Does Have Role, Expect Successful Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
2021-08-20 15:53:34 +01:00
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("roles", [ "first" ], message);
expect(result.valid).toBeTruthy();
});
2021-09-29 17:52:21 +01:00
test('Given User Does Not Have Role, Expect Failed Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(false),
}
},
2021-08-20 14:59:41 +01:00
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("roles", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("You require the `Moderator` role to run this command");
});
2021-09-29 17:52:21 +01:00
test('Given Command Category Is Null, Expect Successful Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
2021-08-20 15:53:34 +01:00
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("noCategory", [ "first" ], message);
expect(result.valid).toBeTruthy();
});
2021-09-22 19:59:40 +01:00
2021-09-29 17:52:21 +01:00
test('Given command is set to disabled, Expect command to not fire', () => {
2021-09-22 19:59:40 +01:00
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'normal',
2021-09-24 18:52:12 +01:00
COMMANDS_DISABLED_MESSAGE: 'disabled',
2021-09-22 19:59:40 +01:00
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
2021-09-24 18:52:12 +01:00
const messageReply = jest.spyOn(message, 'reply');
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
2021-09-24 18:52:12 +01:00
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
expect(messageReply).toBeCalledWith("disabled");
});
2021-09-29 17:52:21 +01:00
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', () => {
2021-09-24 18:52:12 +01:00
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'normal',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const messageReply = jest.spyOn(message, 'reply');
2021-09-22 19:59:40 +01:00
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
2021-09-22 19:59:40 +01:00
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
2021-09-24 18:52:12 +01:00
expect(messageReply).toBeCalledWith("This command is disabled.");
2021-09-22 19:59:40 +01:00
});
2021-09-29 17:52:21 +01:00
test('Given a different command is disabled, Expect command to still fire', () => {
2021-09-22 19:59:40 +01:00
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'anything',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
2021-09-22 19:59:40 +01:00
expect(result.valid).toBeTruthy();
});
2021-09-29 17:52:21 +01:00
test('Given a different command is disabled with this one, Expect command to not fire', () => {
2021-09-22 19:59:40 +01:00
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'normal,anything,',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
2021-09-29 17:52:21 +01:00
const result = util.loadCommand("normal", [ "first" ], message);
2021-09-22 19:59:40 +01:00
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
});
2021-08-20 15:53:34 +01:00
});
describe('LoadEvents', () => {
test('Given Events Are Loaded, Expect Successful Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
fs.readdirSync = jest.fn().mockReturnValue(["normal.ts"]);
const client = {
on: jest.fn(),
} as unknown as Client;
const util = new Util();
const result = util.loadEvents(client);
const clientOn = jest.spyOn(client, 'on');
expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(13);
});
test('Given No Events Found, Expect Successful Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
fs.readdirSync = jest.fn().mockReturnValue(["normal"]);
const client = {
on: jest.fn(),
} as unknown as Client;
const util = new Util();
const result = util.loadEvents(client);
const clientOn = jest.spyOn(client, 'on');
expect(result.valid).toBeTruthy();
expect(clientOn).toBeCalledTimes(0);
});
test('Given Event Folder Does Not Exist, Expect Failed Result', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(false);
fs.readdirSync = jest.fn().mockReturnValue(["normal.ts"]);
const client = {
on: jest.fn(),
} as unknown as Client;
const util = new Util();
const result = util.loadEvents(client);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Event folder does not exist");
});
2021-09-26 14:32:56 +01:00
});