import { describe, it, expect, beforeEach } from '@jest/globals';
import { DiscordWebhookClient } from '../../src/discord/webhook.js';
import { WebhookError } from '../../src/utils/errors.js';
// Mock fetch
global.fetch = ((_input: string | URL | Request, options?: RequestInit) => {
const body = JSON.parse(options?.body as string) as { content?: string };
if (body.content === 'trigger_error') {
return Promise.resolve({
ok: false,
status: 400,
statusText: 'Bad Request',
text: () => Promise.resolve(JSON.stringify({ error: 'Invalid content' })),
} as Response);
}
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('DiscordWebhookClient', () => {
let client: DiscordWebhookClient;
beforeEach(() => {
client = new DiscordWebhookClient(
'https://discord.com/api/webhooks/123/abc'
);
});
it('should send message successfully', async () => {
const response = await client.send({ content: 'Test message' });
expect(response.id).toBe('123456789');
expect(response.author.username).toBe('TestBot');
});
it('should throw WebhookError on API error', async () => {
await expect(client.send({ content: 'trigger_error' })).rejects.toThrow(
WebhookError
);
});
it('should send embed successfully', async () => {
const response = await client.send({
embeds: [{ title: 'Test Embed', description: 'Test Description' }],
});
expect(response.id).toBeDefined();
});
});