more tests for webhookhelper

This commit is contained in:
Aster Fialla
2026-02-18 17:14:04 -05:00
parent 1bba8099e9
commit fc1c463696
2 changed files with 122 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
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";
const wh = {};
@@ -19,14 +19,14 @@ wh.sendMessageAsMember = async function(client, message) {
if (!proxyMatch || !proxyMatch.member) {
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) {
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}`)
}
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 username = member.displayname ?? member.name;
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 }]
})
console.error(e);

View File

@@ -1,15 +1,23 @@
jest.mock('../../src/helpers/messageHelper.js')
jest.mock('@fluxerjs/core', () => jest.fn());
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 {enums} = require("../../src/enums");
describe('webhookHelper', () => {
const authorId = "0001";
const authorFull = "author#0001";
const attachmentUrl = "../oya.png";
const attachmentExpiration = new Date('2026-01-01T00.00.00.0000Z')
const client = new Client();
beforeEach(() => {
jest.resetModules();
@@ -17,9 +25,112 @@ describe('webhookHelper', () => {
})
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(() => {
// restore the spy created with spyOn
jest.restoreAllMocks();