Files
git-agent-sync/tests/unit/errors.test.ts
7000pctAUTO 2e118be81b
Some checks failed
/ test (push) Failing after 6s
chore: Push unit test files
2026-02-03 08:43:04 +00:00

72 lines
2.3 KiB
TypeScript

import {
GitAgentSyncError,
WorkspaceExistsError,
WorkspaceNotFoundError,
InvalidAgentNameError,
MergeConflictError,
handleError,
formatError
} from '../../src/utils/errors';
describe('Error classes', () => {
it('should create GitAgentSyncError with code', () => {
const error = new GitAgentSyncError('Test error', 'TEST_CODE');
expect(error.message).toBe('Test error');
expect(error.code).toBe('TEST_CODE');
expect(error.name).toBe('GitAgentSyncError');
});
it('should create WorkspaceExistsError with details', () => {
const error = new WorkspaceExistsError('agent-1', '/path/to/workspace');
expect(error.code).toBe('WORKSPACE_EXISTS');
expect(error.details?.agentName).toBe('agent-1');
});
it('should create WorkspaceNotFoundError', () => {
const error = new WorkspaceNotFoundError('agent-1');
expect(error.code).toBe('WORKSPACE_NOT_FOUND');
expect(error.details?.agentName).toBe('agent-1');
});
it('should create InvalidAgentNameError', () => {
const error = new InvalidAgentNameError('invalid name');
expect(error.code).toBe('INVALID_AGENT_NAME');
});
it('should create MergeConflictError with file list', () => {
const error = new MergeConflictError(['file1.ts', 'file2.ts']);
expect(error.code).toBe('MERGE_CONFLICT');
expect(error.details?.conflicts).toEqual(['file1.ts', 'file2.ts']);
});
});
describe('handleError', () => {
it('should pass through GitAgentSyncError', () => {
const error = new GitAgentSyncError('test');
const result = handleError(error);
expect(result).toBe(error);
});
it('should wrap unknown errors', () => {
const result = handleError(new Error('unknown'));
expect(result).toBeInstanceOf(GitAgentSyncError);
expect(result.message).toBe('unknown');
});
it('should handle non-Error objects', () => {
const result = handleError('string error');
expect(result).toBeInstanceOf(GitAgentSyncError);
expect(result.message).toBe('string error');
});
});
describe('formatError', () => {
it('should format error with color and details', () => {
const error = new GitAgentSyncError('test error', 'TEST', { key: 'value' });
const result = formatError(error);
expect(result).toContain('test error');
expect(result).toContain('TEST');
expect(result).toContain('key');
});
});