Kord/tests/errors/BotError.test.ts

53 lines
1.7 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,
userMessage: 'Invalid input',
resolution: 'Please try again',
});
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.userMessage).toBe('Invalid input');
expect(error.resolution).toBe('Please try again');
expect(error.message).toBe('[E1001] Invalid input');
});
it('should create a BotError without optional fields', () => {
const error = new BotError({
code: 'E3999',
category: ErrorCategory.BOT_INTERNAL,
userMessage: 'Something went wrong',
});
expect(error.resolution).toBeUndefined();
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,
userMessage: 'Internal error',
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');
});
});