File size: 7,321 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { describe, it, expect } from 'vitest';
import {
  isAbortError,
  isAuthenticationError,
  isCancellationError,
  classifyError,
  getUserFriendlyErrorMessage,
  type ErrorType,
} from '@automaker/utils';

describe('error-handler.ts', () => {
  describe('isAbortError', () => {
    it('should detect AbortError by error name', () => {
      const error = new Error('Operation cancelled');
      error.name = 'AbortError';
      expect(isAbortError(error)).toBe(true);
    });

    it('should detect abort error by message content', () => {
      const error = new Error('Request was aborted');
      expect(isAbortError(error)).toBe(true);
    });

    it('should return false for non-abort errors', () => {
      const error = new Error('Something else went wrong');
      expect(isAbortError(error)).toBe(false);
    });

    it('should return false for non-Error objects', () => {
      expect(isAbortError('not an error')).toBe(false);
      expect(isAbortError(null)).toBe(false);
      expect(isAbortError(undefined)).toBe(false);
    });
  });

  describe('isCancellationError', () => {
    it("should detect 'cancelled' message", () => {
      expect(isCancellationError('Operation was cancelled')).toBe(true);
    });

    it("should detect 'canceled' message", () => {
      expect(isCancellationError('Request was canceled')).toBe(true);
    });

    it("should detect 'stopped' message", () => {
      expect(isCancellationError('Process was stopped')).toBe(true);
    });

    it("should detect 'aborted' message", () => {
      expect(isCancellationError('Task was aborted')).toBe(true);
    });

    it('should be case insensitive', () => {
      expect(isCancellationError('CANCELLED')).toBe(true);
      expect(isCancellationError('Canceled')).toBe(true);
    });

    it('should return false for non-cancellation errors', () => {
      expect(isCancellationError('File not found')).toBe(false);
      expect(isCancellationError('Network error')).toBe(false);
    });
  });

  describe('isAuthenticationError', () => {
    it("should detect 'Authentication failed' message", () => {
      expect(isAuthenticationError('Authentication failed')).toBe(true);
    });

    it("should detect 'Invalid API key' message", () => {
      expect(isAuthenticationError('Invalid API key provided')).toBe(true);
    });

    it("should detect 'authentication_failed' message", () => {
      expect(isAuthenticationError('authentication_failed')).toBe(true);
    });

    it("should detect 'Fix external API key' message", () => {
      expect(isAuthenticationError('Fix external API key configuration')).toBe(true);
    });

    it('should return false for non-authentication errors', () => {
      expect(isAuthenticationError('Network connection error')).toBe(false);
      expect(isAuthenticationError('File not found')).toBe(false);
    });

    it('should be case sensitive', () => {
      expect(isAuthenticationError('authentication Failed')).toBe(false);
    });
  });

  describe('classifyError', () => {
    it('should classify authentication errors', () => {
      const error = new Error('Authentication failed');
      const result = classifyError(error);

      expect(result.type).toBe('authentication');
      expect(result.isAuth).toBe(true);
      expect(result.isAbort).toBe(false);
      expect(result.message).toBe('Authentication failed');
      expect(result.originalError).toBe(error);
    });

    it('should classify abort errors', () => {
      const error = new Error('Operation aborted');
      error.name = 'AbortError';
      const result = classifyError(error);

      expect(result.type).toBe('abort');
      expect(result.isAbort).toBe(true);
      expect(result.isAuth).toBe(false);
      expect(result.message).toBe('Operation aborted');
    });

    it('should prioritize auth over abort if both match', () => {
      const error = new Error('Authentication failed and aborted');
      const result = classifyError(error);

      expect(result.type).toBe('authentication');
      expect(result.isAuth).toBe(true);
      expect(result.isAbort).toBe(true); // Still detected as abort too
    });

    it('should classify cancellation errors', () => {
      const error = new Error('Operation was cancelled');
      const result = classifyError(error);

      expect(result.type).toBe('cancellation');
      expect(result.isCancellation).toBe(true);
      expect(result.isAbort).toBe(false);
      expect(result.isAuth).toBe(false);
    });

    it('should prioritize abort over cancellation if both match', () => {
      const error = new Error('Operation aborted');
      error.name = 'AbortError';
      const result = classifyError(error);

      expect(result.type).toBe('abort');
      expect(result.isAbort).toBe(true);
      expect(result.isCancellation).toBe(true); // Still detected as cancellation too
    });

    it("should classify cancellation errors with 'canceled' spelling", () => {
      const error = new Error('Request was canceled');
      const result = classifyError(error);

      expect(result.type).toBe('cancellation');
      expect(result.isCancellation).toBe(true);
    });

    it("should classify cancellation errors with 'stopped' message", () => {
      const error = new Error('Process was stopped');
      const result = classifyError(error);

      expect(result.type).toBe('cancellation');
      expect(result.isCancellation).toBe(true);
    });

    it('should classify generic Error as execution error', () => {
      const error = new Error('Something went wrong');
      const result = classifyError(error);

      expect(result.type).toBe('execution');
      expect(result.isAuth).toBe(false);
      expect(result.isAbort).toBe(false);
    });

    it('should classify non-Error objects as unknown', () => {
      const error = 'string error';
      const result = classifyError(error);

      expect(result.type).toBe('unknown');
      expect(result.message).toBe('string error');
    });

    it('should handle null and undefined', () => {
      const nullResult = classifyError(null);
      expect(nullResult.type).toBe('unknown');
      expect(nullResult.message).toBe('Unknown error');

      const undefinedResult = classifyError(undefined);
      expect(undefinedResult.type).toBe('unknown');
      expect(undefinedResult.message).toBe('Unknown error');
    });
  });

  describe('getUserFriendlyErrorMessage', () => {
    it('should return friendly message for abort errors', () => {
      const error = new Error('abort');
      const result = getUserFriendlyErrorMessage(error);
      expect(result).toBe('Operation was cancelled');
    });

    it('should return friendly message for authentication errors', () => {
      const error = new Error('Authentication failed');
      const result = getUserFriendlyErrorMessage(error);
      expect(result).toBe('Authentication failed. Please check your API key.');
    });

    it('should return original message for other errors', () => {
      const error = new Error('File not found');
      const result = getUserFriendlyErrorMessage(error);
      expect(result).toBe('File not found');
    });

    it('should handle non-Error objects', () => {
      const result = getUserFriendlyErrorMessage('Custom error');
      expect(result).toBe('Custom error');
    });
  });
});