2026-02-24 12:42:23 -05:00
|
|
|
import {enums} from "../src/enums.js";
|
|
|
|
|
|
|
|
|
|
jest.mock("../src/helpers/messageHelper.js", () => {
|
|
|
|
|
return {
|
|
|
|
|
messageHelper: {
|
|
|
|
|
returnBufferFromText: jest.fn(),
|
|
|
|
|
prefix: 'pf;'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
jest.mock('../src/helpers/memberHelper.js', () => {
|
|
|
|
|
return {
|
|
|
|
|
memberHelper: {
|
|
|
|
|
parseMemberCommand: jest.fn()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
jest.mock('../src/helpers/importHelper.js', () => {
|
|
|
|
|
return {
|
|
|
|
|
importHelper: {
|
|
|
|
|
pluralKitImport: jest.fn()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
import {messageHelper} from "../src/helpers/messageHelper.js";
|
2026-02-24 12:42:23 -05:00
|
|
|
|
|
|
|
|
import {memberHelper} from "../src/helpers/memberHelper.js";
|
|
|
|
|
import {EmbedBuilder} from "@fluxerjs/core";
|
|
|
|
|
import {importHelper} from "../src/helpers/importHelper.js";
|
|
|
|
|
import {commands} from "../src/commands.js";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('commands', () => {
|
|
|
|
|
const authorId = '123';
|
|
|
|
|
const discriminator = '123';
|
|
|
|
|
const username = 'somePerson'
|
2026-02-25 19:30:39 -05:00
|
|
|
const attachmentUrl = 'oya.json';
|
2026-02-24 12:42:23 -05:00
|
|
|
const attachmentExpiration = new Date('2026-01-01').toDateString();
|
2026-02-25 19:30:39 -05:00
|
|
|
let message;
|
2026-02-24 12:42:23 -05:00
|
|
|
const args = ['new']
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
|
|
|
|
jest.resetModules();
|
|
|
|
|
jest.clearAllMocks();
|
2026-02-25 19:30:39 -05:00
|
|
|
message = {
|
|
|
|
|
author: {
|
|
|
|
|
username: username,
|
|
|
|
|
id: authorId,
|
|
|
|
|
discriminator: discriminator,
|
|
|
|
|
},
|
|
|
|
|
attachments: {
|
|
|
|
|
size: 1,
|
|
|
|
|
first: jest.fn().mockImplementation(() => {
|
|
|
|
|
return {
|
|
|
|
|
url: attachmentUrl,
|
|
|
|
|
expires_at: attachmentExpiration
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
reply: jest.fn().mockResolvedValue(),
|
|
|
|
|
content: 'pf;import'
|
|
|
|
|
}
|
2026-02-24 12:42:23 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('memberCommand', () => {
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('calls parseMemberCommand with the correct arguments', async () => {
|
2026-02-24 12:42:23 -05:00
|
|
|
// Arrange
|
|
|
|
|
memberHelper.parseMemberCommand = jest.fn().mockResolvedValue("parsed command");
|
|
|
|
|
// Act
|
2026-02-25 19:30:39 -05:00
|
|
|
await commands.memberCommand(message, args)
|
|
|
|
|
// Assert
|
|
|
|
|
expect(memberHelper.parseMemberCommand).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(memberHelper.parseMemberCommand).toHaveBeenCalledWith(authorId, `${username}#${discriminator}`, args, attachmentUrl, attachmentExpiration);
|
|
|
|
|
});
|
|
|
|
|
})
|
2026-02-24 12:42:23 -05:00
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if parseMemberCommand returns error, log error and reply with error', async () => {
|
|
|
|
|
// Arrange
|
|
|
|
|
memberHelper.parseMemberCommand = jest.fn().mockRejectedValue(new Error('error'));
|
|
|
|
|
// Act
|
|
|
|
|
await commands.memberCommand(message, args)
|
|
|
|
|
// Assert
|
|
|
|
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledWith('error');
|
|
|
|
|
});
|
2026-02-24 12:42:23 -05:00
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if parseMemberCommand returns embed, reply with embed', async () => {
|
|
|
|
|
// Arrange
|
|
|
|
|
const embed = new EmbedBuilder();
|
|
|
|
|
memberHelper.parseMemberCommand = jest.fn().mockResolvedValue(embed);
|
|
|
|
|
// Act
|
|
|
|
|
await commands.memberCommand(message, args);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledWith({embeds: [embed]})
|
|
|
|
|
})
|
2026-02-24 12:42:23 -05:00
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if parseMemberCommand returns object, reply with embed and content', async () => {
|
|
|
|
|
// Arrange
|
|
|
|
|
const reply = {
|
|
|
|
|
errors: ['error', 'error2'],
|
|
|
|
|
success: 'success',
|
|
|
|
|
embed: {title: 'hi'}
|
|
|
|
|
}
|
|
|
|
|
const expected = {
|
|
|
|
|
content: `success \n\n${enums.err.ERRORS_OCCURRED}\n- error\n- error2`,
|
|
|
|
|
embeds: [reply.embed]
|
|
|
|
|
}
|
|
|
|
|
console.log(expected)
|
|
|
|
|
memberHelper.parseMemberCommand = jest.fn().mockResolvedValue(reply);
|
|
|
|
|
// Act
|
|
|
|
|
await commands.memberCommand(message, args);
|
|
|
|
|
// Assert
|
|
|
|
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledWith(expected)
|
2026-02-24 12:42:23 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('importCommand', () => {
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if message includes --help and no attachmentURL, return help message', async () => {
|
|
|
|
|
// Arrange
|
2026-02-24 12:42:23 -05:00
|
|
|
const args = ["--help"];
|
|
|
|
|
message.content = "pf;import --help";
|
|
|
|
|
message.attachments.size = 0;
|
2026-02-25 19:30:39 -05:00
|
|
|
// Act
|
|
|
|
|
await commands.importCommand(message, args)
|
|
|
|
|
// Assert
|
|
|
|
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledWith(enums.help.IMPORT);
|
|
|
|
|
expect(importHelper.pluralKitImport).not.toHaveBeenCalled();
|
2026-02-24 12:42:23 -05:00
|
|
|
})
|
|
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if no args and no attachmentURL, return help message', async () => {
|
|
|
|
|
// Arrange
|
2026-02-24 12:42:23 -05:00
|
|
|
const args = [""];
|
|
|
|
|
message.content = 'pf;import'
|
|
|
|
|
message.attachments.size = 0;
|
2026-02-25 19:30:39 -05:00
|
|
|
// Act
|
|
|
|
|
await commands.importCommand(message, args)
|
|
|
|
|
// Assert
|
|
|
|
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledWith(enums.help.IMPORT);
|
|
|
|
|
expect(importHelper.pluralKitImport).not.toHaveBeenCalled();
|
2026-02-24 12:42:23 -05:00
|
|
|
})
|
|
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if attachment URL, call pluralKitImport with correct arguments', async () => {
|
|
|
|
|
// Arrange
|
2026-02-24 12:42:23 -05:00
|
|
|
const args = [""];
|
2026-02-25 19:30:39 -05:00
|
|
|
message.content = 'pf;import';
|
2026-02-24 12:42:23 -05:00
|
|
|
importHelper.pluralKitImport = jest.fn().mockResolvedValue('success');
|
2026-02-25 19:30:39 -05:00
|
|
|
// Act
|
|
|
|
|
await commands.importCommand(message, args);
|
|
|
|
|
// Assert
|
|
|
|
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledWith('success');
|
|
|
|
|
expect(importHelper.pluralKitImport).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(importHelper.pluralKitImport).toHaveBeenCalledWith(authorId, attachmentUrl);
|
2026-02-24 12:42:23 -05:00
|
|
|
})
|
|
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if pluralKitImport returns aggregate errors with length <= 2000, send errors.', async () => {
|
|
|
|
|
// Arrange
|
2026-02-24 12:42:23 -05:00
|
|
|
const args = [""];
|
|
|
|
|
message.content = 'pf;import'
|
2026-02-25 19:30:39 -05:00
|
|
|
importHelper.pluralKitImport = jest.fn().mockRejectedValue(new AggregateError(['error1', 'error2'], 'errors'));
|
|
|
|
|
// Act
|
|
|
|
|
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`);
|
2026-02-24 12:42:23 -05:00
|
|
|
})
|
|
|
|
|
|
2026-02-25 19:30:39 -05:00
|
|
|
test('if pluralKitImport returns aggregate errors with length > 2000, call returnBufferFromText and message.reply.', async () => {
|
2026-02-24 12:42:23 -05:00
|
|
|
// Arrange
|
|
|
|
|
const args = [""];
|
2026-02-25 19:30:39 -05:00
|
|
|
const text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb";
|
|
|
|
|
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().mockRejectedValue(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);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('if pluralKitImport returns one error, reply with error and log it', async () => {
|
|
|
|
|
// Arrange
|
|
|
|
|
importHelper.pluralKitImport = jest.fn().mockRejectedValue(new Error('error'));
|
|
|
|
|
jest.spyOn(global.console, 'error').mockImplementation(() => {})
|
|
|
|
|
// Act
|
|
|
|
|
await commands.importCommand(message, args);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(message.reply).toHaveBeenCalledWith('error');
|
|
|
|
|
expect(console.error).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(console.error).toHaveBeenCalledWith(new Error('error'));
|
2026-02-24 12:42:23 -05:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
// restore the spy created with spyOn
|
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
|
});
|
|
|
|
|
})
|