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();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('LoadCommand_GivenSuccessfulExection_ExpectSuccessfulResult', () => {
|
|
|
|
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);
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
const message = {
|
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
|
|
|
find: jest.fn().mockReturnValue(true),
|
|
|
|
}
|
2021-08-19 17:21:54 +01:00
|
|
|
},
|
|
|
|
},
|
2021-08-20 15:42:46 +01:00
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const util = new Util();
|
2021-08-19 17:21:54 +01:00
|
|
|
|
|
|
|
const result = util.loadCommand("name", [ "first" ], message);
|
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
2021-08-19 17:27:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('LoadCommand_GivenMemberIsNull_ExpectFailedResult', () => {
|
|
|
|
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);
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
const message = {
|
|
|
|
member: null
|
|
|
|
} as unknown as Message;
|
2021-08-19 17:27:47 +01:00
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
const util = new Util();
|
2021-08-19 17:27:47 +01:00
|
|
|
|
|
|
|
const result = util.loadCommand("name", [ "first" ], message);
|
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Member is not part of message");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('LoadCommand_GivenFolderDoesNotExist_ExpectFailedResult', () => {
|
|
|
|
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 = {
|
|
|
|
member: {
|
|
|
|
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-08-19 17:27:47 +01:00
|
|
|
|
|
|
|
const result = util.loadCommand("name", [ "first" ], message);
|
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("Command folder does not exist");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('LoadCommand_GivenFileDoesNotExist_ExpectFailedResult', () => {
|
|
|
|
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);
|
|
|
|
|
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-08-19 17:27:47 +01:00
|
|
|
|
|
|
|
const result = util.loadCommand("name", [ "first" ], message);
|
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("File does not exist");
|
2021-08-20 14:59:41 +01:00
|
|
|
});
|
|
|
|
|
2021-08-20 15:53:34 +01:00
|
|
|
test('LoadCommand_GivenUserDoesHaveRole_ExpectSuccessfulResult', () => {
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
|
|
|
const result = util.loadCommand("roles", [ "first" ], message);
|
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
test('LoadCommand_GivenUserDoesNotHaveRole_ExpectFailedResult', () => {
|
2021-08-20 14:59:41 +01:00
|
|
|
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);
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
const message = {
|
2021-08-20 14:59:41 +01:00
|
|
|
member: {
|
|
|
|
roles: {
|
|
|
|
cache: {
|
2021-08-20 15:42:46 +01:00
|
|
|
find: jest.fn().mockReturnValue(false),
|
2021-08-20 14:59:41 +01:00
|
|
|
}
|
|
|
|
},
|
2021-08-20 15:42:46 +01:00
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
2021-08-20 14:59:41 +01:00
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
|
|
|
const result = util.loadCommand("roles", [ "first" ], message);
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
expect(result.valid).toBeFalsy();
|
|
|
|
expect(result.message).toBe("You require the `Moderator` role to run this command");
|
2021-08-20 14:59:41 +01:00
|
|
|
});
|
|
|
|
|
2021-08-20 15:53:34 +01:00
|
|
|
test('LoadCommand_GivenCommandCategoryIsNull_ExpectSuccessfulResultStill', () => {
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
reply: jest.fn(),
|
|
|
|
} as unknown as Message;
|
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
|
|
|
const result = util.loadCommand("noCategory", [ "first" ], message);
|
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
test('LoadEvents_GivenEventsAreLoaded_ExpectSuccessfulResult', () => {
|
2021-08-20 14:59:41 +01:00
|
|
|
process.env = {
|
|
|
|
BOT_TOKEN: 'TOKEN',
|
|
|
|
BOT_PREFIX: '!',
|
|
|
|
FOLDERS_COMMANDS: 'commands',
|
|
|
|
FOLDERS_EVENTS: 'events',
|
|
|
|
}
|
2021-08-20 15:42:46 +01:00
|
|
|
|
2021-08-20 14:59:41 +01:00
|
|
|
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
|
|
|
|
fs.existsSync = jest.fn().mockReturnValue(true);
|
2021-08-20 15:42:46 +01:00
|
|
|
fs.readdirSync = jest.fn().mockReturnValue(["name.ts"]);
|
2021-08-20 14:59:41 +01:00
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
const client = {
|
|
|
|
on: jest.fn(),
|
|
|
|
} as unknown as Client;
|
2021-08-20 14:59:41 +01:00
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
const result = util.loadEvents(client);
|
|
|
|
|
|
|
|
const clientOn = jest.spyOn(client, 'on');
|
|
|
|
|
|
|
|
expect(result.valid).toBeTruthy();
|
|
|
|
expect(clientOn).toBeCalledTimes(13);
|
|
|
|
});
|
|
|
|
|
2021-08-20 15:53:34 +01:00
|
|
|
test('LoadEvents_GivenNoEventsFound_ExpectSuccessfulResultStill', () => {
|
|
|
|
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(["name"]);
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2021-08-20 15:42:46 +01:00
|
|
|
test('LoadEvents_GivenEventFolderDoesNotExist_FailedResult', () => {
|
|
|
|
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(["name.ts"]);
|
|
|
|
|
|
|
|
const client = {
|
|
|
|
on: jest.fn(),
|
|
|
|
} as unknown as Client;
|
|
|
|
|
|
|
|
const util = new Util();
|
|
|
|
|
|
|
|
const result = util.loadEvents(client);
|
2021-08-20 14:59:41 +01:00
|
|
|
|
|
|
|
expect(result.valid).toBeFalsy();
|
2021-08-20 15:42:46 +01:00
|
|
|
expect(result.message).toBe("Event folder does not exist");
|
2021-08-19 17:21:54 +01:00
|
|
|
});
|