Kord/tests/errors/BotError.test.ts

50 lines
1.5 KiB
TypeScript

import { BotError, ErrorCategory } from '../../src/errors/BotError';
describe('BotError', () => {
it('should create a BotError with all properties', () => {
const error = new BotError({
code: 'E1001',
category: ErrorCategory.USER_INPUT,
messageKey: 'errors.E1001',
});
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(BotError);
expect(error.name).toBe('BotError');
expect(error.code).toBe('E1001');
expect(error.category).toBe(ErrorCategory.USER_INPUT);
expect(error.messageKey).toBe('errors.E1001');
expect(error.message).toBe('[E1001] errors.E1001');
});
it('should create a BotError without optional cause', () => {
const error = new BotError({
code: 'E3999',
category: ErrorCategory.BOT_INTERNAL,
messageKey: 'errors.E3999',
});
expect(error.cause).toBeUndefined();
});
it('should preserve the original cause error', () => {
const originalError = new Error('DB connection failed');
const error = new BotError({
code: 'E3001',
category: ErrorCategory.BOT_INTERNAL,
messageKey: 'errors.E3001',
cause: originalError,
});
expect(error.cause).toBe(originalError);
expect(error.cause?.message).toBe('DB connection failed');
});
it('should have correct category values', () => {
expect(ErrorCategory.USER_INPUT).toBe('USER_INPUT');
expect(ErrorCategory.PERMISSION).toBe('PERMISSION');
expect(ErrorCategory.BOT_INTERNAL).toBe('BOT_INTERNAL');
expect(ErrorCategory.DISCORD_API).toBe('DISCORD_API');
});
});