forked from PluralFlux/PluralFlux
refactoring commands.js and tests to not use then/catch
This commit is contained in:
@@ -30,8 +30,14 @@ cmds.memberCommand = async function(message, args) {
|
|||||||
const authorFull = `${message.author.username}#${message.author.discriminator}`
|
const authorFull = `${message.author.username}#${message.author.discriminator}`
|
||||||
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
||||||
const attachmentExpires = message.attachments.size > 0 ? message.attachments.first().expires_at : null;
|
const attachmentExpires = message.attachments.size > 0 ? message.attachments.first().expires_at : null;
|
||||||
|
let reply;
|
||||||
const reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args, attachmentUrl, attachmentExpires).catch(async (e) =>{console.error(e); await message.reply(e.message);});
|
try {
|
||||||
|
reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args, attachmentUrl, attachmentExpires)
|
||||||
|
}
|
||||||
|
catch(e) {
|
||||||
|
console.log(e);
|
||||||
|
await message.reply(e.message);
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof reply === 'string') {
|
if (typeof reply === 'string') {
|
||||||
await message.reply(reply);
|
await message.reply(reply);
|
||||||
@@ -87,24 +93,29 @@ cmds.importCommand = async function(message, args) {
|
|||||||
if ((message.content.includes('--help') || (args[0] === '' && args.length === 1)) && !attachmentUrl ) {
|
if ((message.content.includes('--help') || (args[0] === '' && args.length === 1)) && !attachmentUrl ) {
|
||||||
return await message.reply(enums.help.IMPORT);
|
return await message.reply(enums.help.IMPORT);
|
||||||
}
|
}
|
||||||
return await importHelper.pluralKitImport(message.author.id, attachmentUrl).then(async (successfullyAdded) => {
|
try {
|
||||||
|
const successfullyAdded = await importHelper.pluralKitImport(message.author.id, attachmentUrl)
|
||||||
await message.reply(successfullyAdded);
|
await message.reply(successfullyAdded);
|
||||||
}).catch(async (error) => {
|
}
|
||||||
|
catch(error) {
|
||||||
if (error instanceof AggregateError) {
|
if (error instanceof AggregateError) {
|
||||||
// errors.message can be a list of successfully added members, or say that none were successful.
|
// errors.message can be a list of successfully added members, or say that none were successful.
|
||||||
let errorsText = `${error.message}.\n\n${enums.err.ERRORS_OCCURRED}\n${error.errors.join('\n')}`;
|
let errorsText = `${error.message}.\n\n${enums.err.ERRORS_OCCURRED}\n\n${error.errors.join('\n')}`;
|
||||||
|
if (errorsText.length > 2000) {
|
||||||
await message.reply(errorsText).catch(async () => {
|
|
||||||
const returnedBuffer = messageHelper.returnBufferFromText(errorsText);
|
const returnedBuffer = messageHelper.returnBufferFromText(errorsText);
|
||||||
await message.reply({content: returnedBuffer.text, files: [{ name: 'text.txt', data: returnedBuffer.file }]
|
await message.reply({
|
||||||
|
content: returnedBuffer.text, files: [{name: 'text.txt', data: returnedBuffer.file}]
|
||||||
})
|
})
|
||||||
});
|
} else {
|
||||||
|
await message.reply(errorsText);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// If just one error was returned.
|
// If just one error was returned.
|
||||||
else {
|
else {
|
||||||
|
console.error(error);
|
||||||
return await message.reply(error.message);
|
return await message.reply(error.message);
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const commands = cmds;
|
export const commands = cmds;
|
||||||
@@ -40,7 +40,7 @@ describe('commands', () => {
|
|||||||
const authorId = '123';
|
const authorId = '123';
|
||||||
const discriminator = '123';
|
const discriminator = '123';
|
||||||
const username = 'somePerson'
|
const username = 'somePerson'
|
||||||
const attachmentUrl = 'oya.png';
|
const attachmentUrl = 'oya.json';
|
||||||
const attachmentExpiration = new Date('2026-01-01').toDateString();
|
const attachmentExpiration = new Date('2026-01-01').toDateString();
|
||||||
const message = {
|
const message = {
|
||||||
author: {
|
author: {
|
||||||
@@ -56,6 +56,7 @@ describe('commands', () => {
|
|||||||
}))
|
}))
|
||||||
},
|
},
|
||||||
reply: jest.fn().mockResolvedValue(),
|
reply: jest.fn().mockResolvedValue(),
|
||||||
|
content: 'pf;import'
|
||||||
}
|
}
|
||||||
const args = ['new']
|
const args = ['new']
|
||||||
|
|
||||||
@@ -89,16 +90,14 @@ describe('commands', () => {
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
test('if parseMemberCommand returns embed, reply with embed', () => {
|
test('if parseMemberCommand returns embed, reply with embed', async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
const embed = new EmbedBuilder();
|
const embed = new EmbedBuilder();
|
||||||
memberHelper.parseMemberCommand = jest.fn().mockResolvedValue();
|
memberHelper.parseMemberCommand = jest.fn().mockResolvedValue(embed);
|
||||||
// Act
|
// Act
|
||||||
return commands.memberCommand(message, args).catch(() => {
|
await commands.memberCommand(message, args);
|
||||||
// Assert
|
|
||||||
expect(message.reply).toHaveBeenCalledTimes(1);
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
||||||
expect(message.reply).toHaveBeenCalledWith({embeds: [embed]})
|
expect(message.reply).toHaveBeenCalledWith({embeds: [embed]})
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('if parseMemberCommand returns object, reply with embed and content', () => {
|
test('if parseMemberCommand returns object, reply with embed and content', () => {
|
||||||
@@ -141,38 +140,51 @@ describe('commands', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('if attachment URL, call pluralKitImport with correct arguments', () => {
|
test('if attachment URL, call pluralKitImport with correct arguments', async () => {
|
||||||
|
// Arrange
|
||||||
const args = [""];
|
const args = [""];
|
||||||
message.content = 'pf;import'
|
message.content = 'pf;import'
|
||||||
importHelper.pluralKitImport = jest.fn().mockResolvedValue('success');
|
importHelper.pluralKitImport = jest.fn().mockResolvedValue('success');
|
||||||
return commands.importCommand(message, args).then(() => {
|
// Act
|
||||||
|
await commands.importCommand(message, args);
|
||||||
|
// Assert
|
||||||
expect(message.reply).toHaveBeenCalledTimes(1);
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
||||||
expect(message.reply).toHaveBeenCalledWith('success');
|
expect(message.reply).toHaveBeenCalledWith('success');
|
||||||
expect(importHelper.pluralKitImport).toHaveBeenCalledTimes(1);
|
expect(importHelper.pluralKitImport).toHaveBeenCalledTimes(1);
|
||||||
expect(importHelper.pluralKitImport).toHaveBeenCalledWith(authorId, attachmentUrl);
|
expect(importHelper.pluralKitImport).toHaveBeenCalledWith(authorId, attachmentUrl);
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
test('if pluralKitImport returns aggregate errors, send errors.', () => {
|
test('if pluralKitImport returns aggregate errors with length <= 2000, send errors.', async() => {
|
||||||
const args = [""];
|
|
||||||
message.content = 'pf;import'
|
|
||||||
importHelper.pluralKitImport = jest.fn().mockImplementation(() => {throw new AggregateError(['error1', 'error2'], 'errors')});
|
|
||||||
return commands.importCommand(message, args).catch(() => {
|
|
||||||
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
||||||
expect(message.reply).toHaveBeenCalledWith(`errors. \n\n${enums.err.ERRORS_OCCURRED}\n\nerror1\nerror2`);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('if message.reply throws error, call returnBufferFromText and message.reply again.', () => {
|
|
||||||
// Arrange
|
// Arrange
|
||||||
const args = [""];
|
const args = [""];
|
||||||
message.content = 'pf;import'
|
message.content = 'pf;import'
|
||||||
message.reply = jest.fn().mockImplementationOnce(() => {throw e})
|
importHelper.pluralKitImport = jest.fn().mockImplementation(() => {
|
||||||
messageHelper.returnBufferFromText = jest.fn().mockResolvedValue({file: 'test.txt', text: 'normal content'});
|
throw new AggregateError(['error1', 'error2'], 'errors')
|
||||||
return commands.importCommand(message, args).catch(() => {
|
});
|
||||||
expect(message.reply).toHaveBeenCalledTimes(2);
|
// Act
|
||||||
expect(message.reply).toHaveBeenNthCalledWith(1, {content: 'normal content', files: [{name: 'test.txt', data: 'test.txt' }],});
|
await commands.importCommand(message, args);
|
||||||
|
// Assert
|
||||||
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(message.reply).toHaveBeenCalledWith(`errors.\n\n${enums.err.ERRORS_OCCURRED}\n\nerror1\nerror2`);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('if pluralKitImport returns aggregate errors with length > 2000, call returnBufferFromText and message.reply.', async () => {
|
||||||
|
// Arrange
|
||||||
|
const args = [""];
|
||||||
|
const text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb";
|
||||||
|
const file = Buffer.from(text, 'utf-8');
|
||||||
|
const returnedBuffer = {text: 'bbbb', file: file};
|
||||||
|
const expected = {content: returnedBuffer.text, files: [{name: 'text.txt', data: returnedBuffer.file}]};
|
||||||
|
|
||||||
|
importHelper.pluralKitImport = jest.fn().mockImplementation(() => {
|
||||||
|
throw new AggregateError([text, 'error2'], 'errors')
|
||||||
|
});
|
||||||
|
messageHelper.returnBufferFromText = jest.fn().mockReturnValue(returnedBuffer);
|
||||||
|
// Act
|
||||||
|
await commands.importCommand(message, args);
|
||||||
|
// Assert
|
||||||
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(message.reply).toHaveBeenCalledWith(expected);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user