forked from PluralFlux/PluralFlux
added tests for parseProxyTags and updated logic
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user