import { describe, it, expect, beforeEach } from '@jest/globals';
import { DiscordWebhookClient } from '../../src/discord/webhook.js';
import { sendMessage } from '../../src/tools/send-message.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('sendMessage Tool', () => {
let client: DiscordWebhookClient;
beforeEach(() => {
client = new DiscordWebhookClient(
'https://discord.com/api/webhooks/123/abc'
);
});
it('should send simple message', async () => {
const result = await sendMessage(client, { content: 'Hello World' });
expect(result).toContain('Message sent successfully');
expect(result).toContain('ID:');
});
it('should throw error for empty content', async () => {
await expect(sendMessage(client, { content: '' })).rejects.toThrow(
ValidationError
);
await expect(sendMessage(client, { content: ' ' })).rejects.toThrow(
ValidationError
);
});
it('should throw error for content exceeding limit', async () => {
const longContent = 'a'.repeat(2001);
await expect(sendMessage(client, { content: longContent })).rejects.toThrow(
ValidationError
);
});
it('should send message with custom username', async () => {
const result = await sendMessage(client, {
content: 'Test',
username: 'CustomBot',
});
expect(result).toContain('successfully');
});
});