import { describe, it, expect, beforeEach } from '@jest/globals';
import { DiscordWebhookClient } from '../../src/discord/webhook.js';
import { sendEmbedWithFields } from '../../src/tools/send-embed-fields.js';
import { ValidationError } from '../../src/utils/errors.js';
// Mock fetch
global.fetch = ((_input: string | URL | Request, _options?: RequestInit) => {
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({
id: '123456789',
type: 0,
channel_id: '987654321',
author: {
id: '111222333',
username: 'TestBot',
avatar: null,
discriminator: '0000',
bot: true,
},
}),
} as Response);
}) as typeof fetch;
describe('sendEmbedWithFields Tool', () => {
let client: DiscordWebhookClient;
beforeEach(() => {
client = new DiscordWebhookClient(
'https://discord.com/api/webhooks/123/abc'
);
});
it('should send embed with fields', async () => {
const result = await sendEmbedWithFields(client, {
title: 'Field Test',
fields: [
{ name: 'Field 1', value: 'Value 1' },
{ name: 'Field 2', value: 'Value 2', inline: true },
],
});
expect(result).toContain('2 field(s) sent successfully');
});
it('should throw error when no fields provided', async () => {
await expect(
sendEmbedWithFields(client, { title: 'Test', fields: [] })
).rejects.toThrow(ValidationError);
});
it('should send inline and block fields', async () => {
const result = await sendEmbedWithFields(client, {
fields: [
{ name: 'Inline 1', value: 'Value 1', inline: true },
{ name: 'Inline 2', value: 'Value 2', inline: true },
{ name: 'Block', value: 'Full width value', inline: false },
],
});
expect(result).toContain('3 field(s)');
});
it('should send embed with fields and all other properties', async () => {
const result = await sendEmbedWithFields(client, {
title: 'Complete',
description: 'With fields',
color: '#FF0000',
timestamp: true,
fields: [{ name: 'Test', value: 'Value' }],
});
expect(result).toContain('successfully');
});
});