refactor webhookHelper and tests to not use then/catch

This commit is contained in:
Aster Fialla
2026-02-24 13:56:37 -05:00
parent a0f62763af
commit fc58c47b03
2 changed files with 85 additions and 100 deletions

View File

@@ -15,7 +15,7 @@ const name = 'PluralFlux Proxy Webhook';
*/ */
wh.sendMessageAsMember = async function(client, message) { wh.sendMessageAsMember = async function(client, message) {
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null; const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
const proxyMatch = await messageHelper.parseProxyTags(message.author.id, message.content, attachmentUrl).catch(e =>{throw e}); const proxyMatch = await messageHelper.parseProxyTags(message.author.id, message.content, attachmentUrl);
// If the message doesn't match a proxy, just return. // If the message doesn't match a proxy, just return.
if (!proxyMatch || !proxyMatch.member || (proxyMatch.message.length === 0 && !proxyMatch.hasAttachment) ) { if (!proxyMatch || !proxyMatch.member || (proxyMatch.message.length === 0 && !proxyMatch.hasAttachment) ) {
return; return;
@@ -27,7 +27,7 @@ wh.sendMessageAsMember = async function(client, message) {
if (proxyMatch.hasAttachment) { 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);
} }
/** /**
@@ -43,15 +43,15 @@ wh.replaceMessage = async function(client, message, text, member) {
// attachment logic is not relevant yet, text length will always be over 0 right now // attachment logic is not relevant yet, text length will always be over 0 right now
if (text.length > 0 || message.attachments.size > 0) { if (text.length > 0 || message.attachments.size > 0) {
const channel = client.channels.get(message.channelId); const channel = client.channels.get(message.channelId);
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) =>{throw e}); const webhook = await wh.getOrCreateWebhook(client, channel);
const username = member.displayname ?? member.name; const username = member.displayname ?? member.name;
if (text.length > 0) { if (text.length > 0 && text <= 2000) {
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})
}
else if (text.length > 2000) {
const returnedBuffer = messageHelper.returnBufferFromText(text); const returnedBuffer = messageHelper.returnBufferFromText(text);
await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.txt', data: returnedBuffer.file }] await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.txt', data: returnedBuffer.file }]
}) })
console.error(e);
});
} }
if (message.attachments.size > 0) { if (message.attachments.size > 0) {
// Not implemented yet // Not implemented yet
@@ -72,7 +72,7 @@ wh.replaceMessage = async function(client, message, text, member) {
wh.getOrCreateWebhook = async function(client, channel) { wh.getOrCreateWebhook = async function(client, channel) {
// If channel doesn't allow webhooks // If channel doesn't allow webhooks
if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED); if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
let webhook = await wh.getWebhook(client, channel).catch((e) =>{throw e}); let webhook = await wh.getWebhook(client, channel)
if (!webhook) { if (!webhook) {
webhook = await channel.createWebhook({name: name}); webhook = await channel.createWebhook({name: name});
} }

View File

@@ -34,9 +34,7 @@ describe('webhookHelper', () => {
author: { author: {
id: '123' id: '123'
}, },
guild: { guildId: '123',
guildId: '123'
},
reply: jest.fn() reply: jest.fn()
} }
const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person"}; const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person"};
@@ -50,25 +48,24 @@ describe('webhookHelper', () => {
// Arrange // Arrange
messageHelper.parseProxyTags.mockResolvedValue({}); messageHelper.parseProxyTags.mockResolvedValue({});
// Act // Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => { const res = await webhookHelper.sendMessageAsMember(client, message)
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 // Assert
expect(res).toBeUndefined(); expect(res).toBeUndefined();
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1); expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null); expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled(); expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
}) })
test('calls parseProxyTags and returns if proxyMatch is undefined', async() => {
// Arrange
messageHelper.parseProxyTags.mockResolvedValue(undefined);
// Act
const res = await webhookHelper.sendMessageAsMember(client, message)
// Assert
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 with attachmentUrl', async() => { test('calls parseProxyTags with attachmentUrl', async() => {
@@ -79,28 +76,23 @@ describe('webhookHelper', () => {
return {url: 'oya.png'} return {url: 'oya.png'}
} }
} }
// message.attachments.set('attachment', {url: 'oya.png'})
// message.attachments.set('first', () => {return {url: 'oya.png'}})
messageHelper.parseProxyTags.mockResolvedValue(undefined); messageHelper.parseProxyTags.mockResolvedValue(undefined);
// Act // Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => { const res = await webhookHelper.sendMessageAsMember(client, message)
// Assert // Assert
expect(res).toBeUndefined(); expect(res).toBeUndefined();
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1); expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, 'oya.png'); 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() => { test('if message matches member proxy but is not sent from a guild, throw an error', async() => {
// Arrange // Arrange
message.guildId = null;
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage); messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
// Act // Act and Assert
return webhookHelper.sendMessageAsMember(client, message).catch((res) => { await expect(webhookHelper.sendMessageAsMember(client, message)).rejects.toThrow(enums.err.NOT_IN_SERVER);
// Assert
expect(res).toEqual(new Error(enums.err.NOT_IN_SERVER));
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled(); expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
}) })
})
test('if message matches member proxy and sent in a guild and has an attachment, reply to message with ping', async() => { test('if message matches member proxy and sent in a guild and has an attachment, reply to message with ping', async() => {
// Arrange // Arrange
@@ -109,13 +101,12 @@ describe('webhookHelper', () => {
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage); messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
const expected = `${enums.misc.ATTACHMENT_SENT_BY} ${proxyMessage.member.displayname}` const expected = `${enums.misc.ATTACHMENT_SENT_BY} ${proxyMessage.member.displayname}`
// Act // Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => { await webhookHelper.sendMessageAsMember(client, message)
// Assert // Assert
expect(message.reply).toHaveBeenCalledTimes(1); expect(message.reply).toHaveBeenCalledTimes(1);
expect(message.reply).toHaveBeenCalledWith(expected); expect(message.reply).toHaveBeenCalledWith(expected);
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled(); expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
}) })
})
test('if message matches member proxy and sent in a guild channel and no attachment, calls replace message', async() => { test('if message matches member proxy and sent in a guild channel and no attachment, calls replace message', async() => {
// Arrange // Arrange
@@ -124,27 +115,24 @@ describe('webhookHelper', () => {
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage); messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
jest.spyOn(webhookHelper, 'replaceMessage').mockResolvedValue(); jest.spyOn(webhookHelper, 'replaceMessage').mockResolvedValue();
// Act // Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => { await webhookHelper.sendMessageAsMember(client, message);
// Assert // Assert
expect(message.reply).not.toHaveBeenCalled(); expect(message.reply).not.toHaveBeenCalled();
expect(webhookHelper.replaceMessage).toHaveBeenCalledTimes(1); expect(webhookHelper.replaceMessage).toHaveBeenCalledTimes(1);
expect(webhookHelper.replaceMessage).toHaveBeenCalledWith(client, message, proxyMessage.message, proxyMessage.member); expect(webhookHelper.replaceMessage).toHaveBeenCalledWith(client, message, proxyMessage.message, proxyMessage.member);
}) })
})
test('if replace message throws error, throw same error', async() => { test('if replace message throws error, throw same error and does not call message.reply', async () => {
// Arrange // Arrange
message.guildId = '123'; message.guildId = '123';
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage); messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
jest.spyOn(webhookHelper, 'replaceMessage').mockImplementation(() => {throw new Error("error")}); jest.spyOn(webhookHelper, 'replaceMessage').mockImplementation(() => {
throw new Error("error")
});
// Act // Act
return webhookHelper.sendMessageAsMember(client, message).catch((res) => { await expect(webhookHelper.sendMessageAsMember(client, message)).rejects.toThrow("error");
// Assert // Assert
expect(message.reply).not.toHaveBeenCalled(); expect(message.reply).not.toHaveBeenCalled();
expect(webhookHelper.replaceMessage).toHaveBeenCalledTimes(1);
expect(webhookHelper.replaceMessage).toHaveBeenCalledWith(client, message, proxyMessage.message, proxyMessage.member);
expect(res).toEqual(new Error('error'));
})
}) })
}) })
@@ -174,8 +162,8 @@ describe('webhookHelper', () => {
guild: { guild: {
guildId: guildId guildId: guildId
}, },
reply: jest.fn(), reply: jest.fn().mockResolvedValue(),
delete: jest.fn() delete: jest.fn().mockResolvedValue()
} }
const webhook = { const webhook = {
@@ -192,13 +180,12 @@ describe('webhookHelper', () => {
message.attachments = noAttachments; message.attachments = noAttachments;
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook); jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
// Act // Act
return webhookHelper.replaceMessage(client, message, emptyText, member).then(() => { await webhookHelper.replaceMessage(client, message, emptyText, member)
expect(webhookHelper.getOrCreateWebhook).not.toHaveBeenCalled(); expect(webhookHelper.getOrCreateWebhook).not.toHaveBeenCalled();
expect(message.delete).not.toHaveBeenCalled(); expect(message.delete).not.toHaveBeenCalled();
}) })
})
test('calls getOrCreateWebhook and message.delete with correct arguments if text >= 0', async() => { test('calls getOrCreateWebhook and message.delete with correct arguments if text > 0 & < 2000', async() => {
// Arrange // Arrange
message.attachments = { message.attachments = {
size: 0, size: 0,
@@ -207,49 +194,47 @@ describe('webhookHelper', () => {
}; };
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook); jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
// Act // Act
return webhookHelper.replaceMessage(client, message, text, member).then((res) => { await webhookHelper.replaceMessage(client, message, text, member);
// Assert // Assert
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1); expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1);
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId); expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId);
expect(message.delete).toHaveBeenCalledTimes(1); expect(message.delete).toHaveBeenCalledTimes(1);
expect(message.delete).toHaveBeenCalledWith(); expect(message.delete).toHaveBeenCalledWith();
}) })
})
// TODO: flaky for some reason // TODO: Flaky for some reason. Skipping until attachments are implemented
test('calls getOrCreateWebhook and message.delete with correct arguments if attachments exist', async() => { test.skip('calls getOrCreateWebhook and message.delete with correct arguments if attachments exist', async() => {
// Arrange // Arrange
const emptyText = '' const emptyText = ''
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook); jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
// Act // Act
return webhookHelper.replaceMessage(client, message, emptyText, member).then((res) => { await webhookHelper.replaceMessage(client, message, emptyText, member);
// Assert // Assert
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1); // expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1);
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId); // expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId);
expect(message.delete).toHaveBeenCalledTimes(1); expect(message.delete).toHaveBeenCalledTimes(1);
expect(message.delete).toHaveBeenCalledWith(); expect(message.delete).toHaveBeenCalledWith();
}) })
})
test('calls returnBufferFromText and console error if webhook.send returns error', async() => { test('calls returnBufferFromText if text is more than 2000 characters', async() => {
// Arrange // Arrange
const text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbb";
message.content = text;
const file = Buffer.from(text, 'utf-8'); const file = Buffer.from(text, 'utf-8');
const returnedBuffer = {text: text, file: file}; const returnedBuffer = {text: 'bbbb', file: file};
const expected2ndSend = {content: returnedBuffer.text, username: member.displayname, avatar_url: member.propic, files: [{name: 'text.txt', data: returnedBuffer.file}]}; const expected = {content: returnedBuffer.text, username: member.displayname, avatar_url: member.propic, files: [{name: 'text.txt', data: returnedBuffer.file}]};
jest.mock('console', () => ({error: jest.fn()}));
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook); jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
webhook.send = jest.fn().mockImplementationOnce(async() => {throw new Error('error')}); webhook.send = jest.fn();
messageHelper.returnBufferFromText = jest.fn().mockResolvedValue(returnedBuffer); messageHelper.returnBufferFromText = jest.fn().mockReturnValue(returnedBuffer);
// Act // Act
return webhookHelper.replaceMessage(client, message, text, member).catch((res) => { await webhookHelper.replaceMessage(client, message, text, member);
// Assert // Assert
expect(messageHelper.returnBufferFromText).toHaveBeenCalledTimes(1); expect(messageHelper.returnBufferFromText).toHaveBeenCalledTimes(1);
expect(messageHelper.returnBufferFromText).toHaveBeenCalledWith(text); expect(messageHelper.returnBufferFromText).toHaveBeenCalledWith(text);
expect(webhook.send).toHaveBeenCalledTimes(2); expect(webhook.send).toHaveBeenCalledTimes(1);
expect(webhook.send).toHaveBeenNthCalledWith(2, expected2ndSend); expect(webhook.send).toHaveBeenCalledWith(expected);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(new Error('error'));
})
}) })
}) })