Files
PluralFlux-infra/tests/helpers/messageHelper.test.js
pieartsy 7fead5e3d7 feat: Add unit tests (#21)
Tests are mostly complete (though some are failing -- to be fixed in the
async/await refactor.

Also refactored, fixed bugs found while testing, and added ability to
type `pf;m` instead of `pf;member`.

---------

Co-authored-by: Aster Fialla <asterfialla@gmail.com>
2026-02-24 12:42:23 -05:00

120 lines
6.6 KiB
JavaScript

const env = require('dotenv');
env.config();
jest.mock('../../src/helpers/memberHelper.js', () => {
return {memberHelper: {
getMembersByAuthor: jest.fn()
}}
})
const {memberHelper} = require("../../src/helpers/memberHelper.js");
const {messageHelper} = require("../../src/helpers/messageHelper.js");
describe('messageHelper', () => {
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
})
describe('parseCommandArgs', () => {
test.each([
['pk;member', ['']],
['pk;member add somePerson "Some Person"', ['add', 'somePerson', 'Some Person']],
['pk;member add \"Some Person\"', ['add', 'Some Person']],
['pk;member add somePerson \'Some Person\'', ['add', 'somePerson', 'Some Person']],
['pk;member add somePerson \"\'Some\' Person\"', ['add', 'somePerson', 'Some Person']],
])('%s returns correct arguments', (content, expected) => {
// Arrange
const command = "member";
const result = messageHelper.parseCommandArgs(content, command);
expect(result).toEqual(expected);
})
})
describe(`parseProxyTags`, () => {
const membersFor1 = [
{name: "somePerson", proxy: "--text"},
{name: "someSecondPerson", proxy: undefined},
{name: "someOtherPerson", proxy: "?text}"},
{name: "someLastPerson", proxy: "{text}"},
{name: "someEmojiPerson", proxy: "⭐text"},
{name: "someSpacePerson", proxy: "! text"},
]
const membersFor2 = []
const membersFor3 = [
{name: "someOtherThirdPerson", proxy: undefined}
]
const attachmentUrl = "../oya.png"
beforeEach(() => {
memberHelper.getMembersByAuthor = jest.fn().mockImplementation((specificAuthorId) => {
if (specificAuthorId === "1") return membersFor1;
if (specificAuthorId === "2") return membersFor2;
if (specificAuthorId === "3") return membersFor3;
})
});
test.each([
['1', 'hello', null, {}],
['1', '--hello', null, {member: membersFor1[0], message: 'hello', hasAttachment: false}],
['1', 'hello', attachmentUrl, {}],
['1', '--hello', attachmentUrl, {member: membersFor1[0], message: 'hello', hasAttachment: true}],
['1', '--', attachmentUrl, {member: membersFor1[0], message: '', hasAttachment: true}],
['1', '?hello}', null, {member: membersFor1[2], message: 'hello', hasAttachment: false}],
['1', '{hello}', null, {member: membersFor1[3], message: 'hello', hasAttachment: false}],
['1', '⭐hello', null, {member: membersFor1[4], message: 'hello', hasAttachment: false}],
['1', '! hello', null, {member: membersFor1[5], message: 'hello', hasAttachment: false}],
['2', 'hello', null, undefined],
['2', '--hello', null, undefined],
['2', 'hello', attachmentUrl, undefined],
['2', '--hello', attachmentUrl,undefined],
['3', 'hello', null, {}],
['3', '--hello', null, {}],
['3', 'hello', attachmentUrl, {}],
['3', '--hello', attachmentUrl,{}],
])('ID %s with string %s returns correct proxy', async(specificAuthorId, content, attachmentUrl, expected) => {
// Act
return messageHelper.parseProxyTags(specificAuthorId, content, attachmentUrl).then((res) => {
// Assert
expect(res).toEqual(expected);
})
});
})
describe('returnBufferFromText', () => {
const charas2000 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
test('returns truncated text and buffer file when text is more than 2000 characters', () => {
// Arrange
const charasOver2000 = "bbbbb"
const expectedBuffer = Buffer.from(charasOver2000, 'utf-8');
const expected = {text: charas2000, file: expectedBuffer};
// Act
const result = messageHelper.returnBufferFromText(`${charas2000}${charasOver2000}`);
// Assert
expect(result).toEqual(expected);
})
test('returns text when text is 2000 characters or less', () => {
// Arrange
const expected = {text: charas2000, file: undefined};
// Act
const result = messageHelper.returnBufferFromText(`${charas2000}`);
// Assert
expect(result).toEqual(expected);
})
})
afterEach(() => {
// restore the spy created with spyOn
jest.restoreAllMocks();
});
})