From 223292c2d39b27108d1552882223ce4b6e981baa Mon Sep 17 00:00:00 2001 From: Aster Fialla Date: Wed, 18 Feb 2026 10:16:51 -0500 Subject: [PATCH] added tests for parseProxyTags and updated logic --- src/helpers/messageHelper.js | 4 +- tests/helpers/messageHelper.test.js | 66 ++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/helpers/messageHelper.js b/src/helpers/messageHelper.js index 66296f5..037040f 100644 --- a/src/helpers/messageHelper.js +++ b/src/helpers/messageHelper.js @@ -57,14 +57,14 @@ msgh.parseProxyTags = async function (authorId, content, attachmentUrl = null){ const splitProxy = member.proxy.split("text"); if(content.startsWith(splitProxy[0]) && content.endsWith(splitProxy[1])) { proxyMessage.member = member; - if (attachmentUrl) return proxyMessage.message = enums.misc.ATTACHMENT_SENT_BY; + if (attachmentUrl) proxyMessage.hasAttachment = true; let escapedPrefix = splitProxy[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); let escapedSuffix = splitProxy[1].replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); escapedPrefix = new RegExp("^" + escapedPrefix); escapedSuffix = new RegExp(escapedSuffix + "$") proxyMessage.message = content.replace(escapedPrefix, "").replace(escapedSuffix, ""); - if (proxyMessage.message.length === 0) throw new Error(enums.err.NO_MESSAGE_SENT_WITH_PROXY); + if (proxyMessage.message.length === 0 && !attachmentUrl) throw new Error(enums.err.NO_MESSAGE_SENT_WITH_PROXY); } } }) diff --git a/tests/helpers/messageHelper.test.js b/tests/helpers/messageHelper.test.js index 64b8917..aa32234 100644 --- a/tests/helpers/messageHelper.test.js +++ b/tests/helpers/messageHelper.test.js @@ -1,17 +1,26 @@ -jest.mock('../../src/helpers/memberHelper.js') -jest.mock('tmp'); -jest.mock('fs'); -jest.mock('@fluxerjs/core', () => jest.fn()); +const env = require('dotenv'); +env.config(); const {memberHelper} = require("../../src/helpers/memberHelper.js"); const {Message} = require("@fluxerjs/core"); const {fs} = require('fs'); const {enums} = require('../../src/enums'); const {tmp, setGracefulCleanup} = require('tmp'); -const {messageHelper} = require("../../src/helpers/memberHelper.js"); -const {describe} = require("pm2"); + +jest.mock('../../src/helpers/memberHelper.js', () => { + return {memberHelper: { + getMembersByAuthor: jest.fn() + }} +}) + +jest.mock('tmp'); +jest.mock('fs'); +jest.mock('@fluxerjs/core'); + +const {messageHelper} = require("../../src/helpers/messageHelper.js"); describe('messageHelper', () => { + // let memberHelper = {} const authorId = "0001"; const authorFull = "author#0001"; const attachmentUrl = "../oya.png"; @@ -23,6 +32,51 @@ describe('messageHelper', () => { }) describe(`parseProxyTags`, () => { + const membersFor1 = [ + {name: "somePerson", proxy: "--text"}, + {name: "someSecondPerson", proxy: undefined} + ] + + 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'}], + ['1', 'hello', attachmentUrl, {}], + ['1', '--hello', attachmentUrl, {member: membersFor1[0], message: 'hello', hasAttachment: true}], + ['1', '--', attachmentUrl, {member: membersFor1[0], message: '', hasAttachment: true}], + ['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,{}], + ])('Member %s returns correct proxy', (specificAuthorId, content, attachmentUrl, expected) => { + messageHelper.parseProxyTags(specificAuthorId, content, attachmentUrl).then((res) => { + expect(res).toEqual(expected); + }) + }); + + + }) + + describe('parseCommandArgs', () => { })