mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-16 17:45:28 +10:00
more tests for webhookhelper
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import {messageHelper} from "./messageHelper.js";
|
import {messageHelper} from "./messageHelper.js";
|
||||||
import {Webhook, Channel, Message} from '@fluxerjs/core';
|
import {Webhook, Channel, Message, Client} from '@fluxerjs/core';
|
||||||
import {enums} from "../enums.js";
|
import {enums} from "../enums.js";
|
||||||
|
|
||||||
const wh = {};
|
const wh = {};
|
||||||
@@ -19,11 +19,11 @@ wh.sendMessageAsMember = async function(client, message) {
|
|||||||
if (!proxyMatch || !proxyMatch.member) {
|
if (!proxyMatch || !proxyMatch.member) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs
|
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs)
|
||||||
if (!message.guildId) {
|
if (!message.guildId) {
|
||||||
throw new Error(enums.err.NOT_IN_SERVER);
|
throw new Error(enums.err.NOT_IN_SERVER);
|
||||||
}
|
}
|
||||||
if (proxyMatch.message === enums.misc.ATTACHMENT_SENT_BY) {
|
if (proxyMatch.hasAttachment) {
|
||||||
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname ?? proxyMatch.member.name}`)
|
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname ?? proxyMatch.member.name}`)
|
||||||
}
|
}
|
||||||
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
|
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
|
||||||
@@ -43,7 +43,7 @@ wh.replaceMessage = async function(client, message, text, member) {
|
|||||||
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) =>{throw e});
|
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) =>{throw e});
|
||||||
const username = member.displayname ?? member.name;
|
const username = member.displayname ?? member.name;
|
||||||
await webhook.send({content: text, username: username, avatar_url: member.propic}).catch(async(e) => {
|
await webhook.send({content: text, username: username, avatar_url: member.propic}).catch(async(e) => {
|
||||||
const returnedBuffer = await messageHelper.returnBufferFromText(text);
|
const returnedBuffer = messageHelper.returnBufferFromText(text);
|
||||||
await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.pdf', data: returnedBuffer.file }]
|
await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.pdf', data: returnedBuffer.file }]
|
||||||
})
|
})
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
jest.mock('../../src/helpers/messageHelper.js')
|
jest.mock('../../src/helpers/messageHelper.js')
|
||||||
jest.mock('@fluxerjs/core', () => jest.fn());
|
|
||||||
|
|
||||||
const {messageHelper} = require("../../src/helpers/messageHelper.js");
|
const {messageHelper} = require("../../src/helpers/messageHelper.js");
|
||||||
const {Message, Webhook, Channel} = require("@fluxerjs/core");
|
const {Message, Webhook, Channel, Client} = require("@fluxerjs/core");
|
||||||
|
|
||||||
|
jest.mock('../../src/helpers/messageHelper.js', () => {
|
||||||
|
return {messageHelper: {
|
||||||
|
parseProxyTags: jest.fn(),
|
||||||
|
returnBuffer: jest.fn()
|
||||||
|
}}
|
||||||
|
})
|
||||||
|
|
||||||
|
// jest.mock("@fluxerjs/core");
|
||||||
|
|
||||||
const {webhookHelper} = require("../../src/helpers/webhookHelper.js");
|
const {webhookHelper} = require("../../src/helpers/webhookHelper.js");
|
||||||
|
const {enums} = require("../../src/enums");
|
||||||
|
|
||||||
describe('webhookHelper', () => {
|
describe('webhookHelper', () => {
|
||||||
const authorId = "0001";
|
|
||||||
const authorFull = "author#0001";
|
const client = new Client();
|
||||||
const attachmentUrl = "../oya.png";
|
|
||||||
const attachmentExpiration = new Date('2026-01-01T00.00.00.0000Z')
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.resetModules();
|
jest.resetModules();
|
||||||
@@ -17,9 +25,112 @@ describe('webhookHelper', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe(`sendMessageAsMember`, () => {
|
describe(`sendMessageAsMember`, () => {
|
||||||
|
const content = "hi"
|
||||||
|
const attachments = new Map();
|
||||||
|
const message = {
|
||||||
|
client,
|
||||||
|
content: content,
|
||||||
|
attachments: attachments,
|
||||||
|
author: {
|
||||||
|
id: '123'
|
||||||
|
},
|
||||||
|
guild: {
|
||||||
|
guildId: '123'
|
||||||
|
},
|
||||||
|
reply: jest.fn()
|
||||||
|
}
|
||||||
|
const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person"};
|
||||||
|
const proxyMessage = {message: content, member: member}
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.spyOn(webhookHelper, 'replaceMessage');
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('calls parseProxyTags and returns if proxyMatch is empty object', async() => {
|
||||||
|
// Arrange
|
||||||
|
messageHelper.parseProxyTags.mockResolvedValue({});
|
||||||
|
// Act
|
||||||
|
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||||
|
expect(res).toBeUndefined();
|
||||||
|
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||||
|
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
|
||||||
|
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('calls parseProxyTags and returns if proxyMatch is undefined', async() => {
|
||||||
|
// Arrange
|
||||||
|
messageHelper.parseProxyTags.mockResolvedValue(undefined);
|
||||||
|
// Act
|
||||||
|
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||||
|
// Assert
|
||||||
|
expect(res).toBeUndefined();
|
||||||
|
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||||
|
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('calls parseProxyTags with attachmentUrl', async() => {
|
||||||
|
// Arrange
|
||||||
|
message.attachments = {
|
||||||
|
size: 1,
|
||||||
|
first: () => {
|
||||||
|
return {url: 'oya.png'}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// message.attachments.set('attachment', {url: 'oya.png'})
|
||||||
|
// message.attachments.set('first', () => {return {url: 'oya.png'}})
|
||||||
|
messageHelper.parseProxyTags.mockResolvedValue(undefined);
|
||||||
|
// Act
|
||||||
|
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||||
|
// Assert
|
||||||
|
expect(res).toBeUndefined();
|
||||||
|
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
|
||||||
|
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, 'oya.png');
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('if message matches member proxy but is not sent from a guild, throw an error', async() => {
|
||||||
|
// Arrange
|
||||||
|
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
|
||||||
|
// Act
|
||||||
|
return webhookHelper.sendMessageAsMember(client, message).catch((res) => {
|
||||||
|
// Assert
|
||||||
|
expect(res).toEqual(new Error(enums.err.NOT_IN_SERVER));
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('if message matches member proxy and sent in a guild and has an attachment, reply to message with ping', async() => {
|
||||||
|
// Arrange
|
||||||
|
message.guildId = '123'
|
||||||
|
proxyMessage.hasAttachment = true;
|
||||||
|
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
|
||||||
|
const expected = `${enums.misc.ATTACHMENT_SENT_BY} ${proxyMessage.member.displayname}`
|
||||||
|
// Act
|
||||||
|
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
|
||||||
|
// Assert
|
||||||
|
expect(message.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(message.reply).toHaveBeenCalledWith(expected);
|
||||||
|
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
describe(`replaceMessage`, () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
describe(`getOrCreateWebhook`, () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
describe(`getWebhook`, () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
// restore the spy created with spyOn
|
// restore the spy created with spyOn
|
||||||
jest.restoreAllMocks();
|
jest.restoreAllMocks();
|
||||||
|
|||||||
Reference in New Issue
Block a user