import { describe, it, expect } from '@jest/globals';
import { EmbedBuilder } from '../../src/discord/embed-builder.js';
import { ValidationError } from '../../src/utils/errors.js';
describe('EmbedBuilder', () => {
it('should build basic embed', () => {
const embed = new EmbedBuilder()
.setTitle('Test Title')
.setDescription('Test Description')
.build();
expect(embed.title).toBe('Test Title');
expect(embed.description).toBe('Test Description');
});
it('should parse hex colors', () => {
const embed = new EmbedBuilder().setColor('#FF0000').build();
expect(embed.color).toBe(0xff0000);
});
it('should parse named colors', () => {
const embed = new EmbedBuilder().setColor('red').build();
expect(embed.color).toBe(0xff0000);
});
it('should add timestamp', () => {
const embed = new EmbedBuilder().setTimestamp(true).build();
expect(embed.timestamp).toBeDefined();
if (embed.timestamp) {
expect(new Date(embed.timestamp).getTime()).toBeGreaterThan(0);
}
});
it('should add fields', () => {
const embed = new EmbedBuilder()
.addField('Field 1', 'Value 1')
.addField('Field 2', 'Value 2', true)
.build();
expect(embed.fields).toHaveLength(2);
if (embed.fields) {
expect(embed.fields[0].name).toBe('Field 1');
expect(embed.fields[0].inline).toBe(false);
expect(embed.fields[1].inline).toBe(true);
}
});
it('should throw error for title exceeding limit', () => {
const longTitle = 'a'.repeat(257);
expect(() => new EmbedBuilder().setTitle(longTitle)).toThrow(ValidationError);
});
it('should throw error for more than 25 fields', () => {
const builder = new EmbedBuilder();
for (let i = 0; i < 25; i++) {
builder.addField(`Field ${String(i)}`, `Value ${String(i)}`);
}
expect(() => builder.addField('Field 26', 'Value 26')).toThrow(
ValidationError
);
});
it('should build from params', () => {
const embed = new EmbedBuilder()
.fromParams({
title: 'Title',
description: 'Description',
color: 'blue',
timestamp: true,
})
.build();
expect(embed.title).toBe('Title');
expect(embed.description).toBe('Description');
expect(embed.color).toBe(0x0000ff);
expect(embed.timestamp).toBeDefined();
});
});