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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import { describe, it, expect } from 'vitest';
import {
  isAbortError,
  isCancellationError,
  isAuthenticationError,
  isRateLimitError,
  isQuotaExhaustedError,
  isModelNotFoundError,
  isStreamDisconnectedError,
  extractRetryAfter,
  classifyError,
  getUserFriendlyErrorMessage,
} from '../src/error-handler';

describe('error-handler.ts', () => {
  describe('isAbortError', () => {
    it("should return true for Error with name 'AbortError'", () => {
      const error = new Error('Operation aborted');
      error.name = 'AbortError';
      expect(isAbortError(error)).toBe(true);
    });

    it("should return true for Error with message containing 'abort'", () => {
      const error = new Error('Request was aborted');
      expect(isAbortError(error)).toBe(true);
    });

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

    it('should return false for non-Error values', () => {
      expect(isAbortError('abort')).toBe(false);
      expect(isAbortError(null)).toBe(false);
      expect(isAbortError(undefined)).toBe(false);
      expect(isAbortError({})).toBe(false);
    });

    it('should handle Error with both AbortError name and abort message', () => {
      const error = new Error('abort');
      error.name = 'AbortError';
      expect(isAbortError(error)).toBe(true);
    });
  });

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

    it("should return true for 'canceled' message (US spelling)", () => {
      expect(isCancellationError('Operation canceled')).toBe(true);
      expect(isCancellationError('CANCELED')).toBe(true);
    });

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

    it("should return true for 'aborted' message", () => {
      expect(isCancellationError('Request aborted')).toBe(true);
      expect(isCancellationError('ABORTED')).toBe(true);
    });

    it('should return false for non-cancellation messages', () => {
      expect(isCancellationError('Something went wrong')).toBe(false);
      expect(isCancellationError('Error occurred')).toBe(false);
      expect(isCancellationError('')).toBe(false);
    });

    it('should be case-insensitive', () => {
      expect(isCancellationError('CaNcElLeD')).toBe(true);
      expect(isCancellationError('StOpPeD')).toBe(true);
    });
  });

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

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

    it("should return true for 'authentication_failed' message", () => {
      expect(isAuthenticationError('Error: authentication_failed')).toBe(true);
    });

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

    it('should return false for non-authentication errors', () => {
      expect(isAuthenticationError('Something went wrong')).toBe(false);
      expect(isAuthenticationError('Network error')).toBe(false);
      expect(isAuthenticationError('')).toBe(false);
    });

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

  describe('isRateLimitError', () => {
    it('should return true for errors with 429 status code', () => {
      const error = new Error('Error: 429 Too Many Requests');
      expect(isRateLimitError(error)).toBe(true);
    });

    it('should return true for errors with rate_limit in message', () => {
      const error = new Error('rate_limit_error: Too many requests');
      expect(isRateLimitError(error)).toBe(true);
    });

    it('should return true for string errors with 429', () => {
      expect(isRateLimitError('429 - rate limit exceeded')).toBe(true);
    });

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

    it('should return false for null/undefined', () => {
      expect(isRateLimitError(null)).toBe(false);
      expect(isRateLimitError(undefined)).toBe(false);
    });
  });

  describe('isQuotaExhaustedError', () => {
    it('should return true for overloaded errors', () => {
      expect(isQuotaExhaustedError(new Error('overloaded_error: service is busy'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('Server is overloaded'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('At capacity'))).toBe(true);
    });

    it('should return true for usage limit errors', () => {
      expect(isQuotaExhaustedError(new Error('limit reached'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('Usage limit exceeded'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('quota exceeded'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('quota_exceeded'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('session limit reached'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('weekly limit hit'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('monthly limit reached'))).toBe(true);
    });

    it('should return true for billing/credit errors', () => {
      expect(isQuotaExhaustedError(new Error('credit balance is too low'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('insufficient credits'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('insufficient balance'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('no credits remaining'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('out of credits'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('billing issue detected'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('payment required'))).toBe(true);
    });

    it('should return true for upgrade prompts', () => {
      expect(isQuotaExhaustedError(new Error('Please /upgrade your plan'))).toBe(true);
      expect(isQuotaExhaustedError(new Error('extra-usage not enabled'))).toBe(true);
    });

    it('should return false for regular errors', () => {
      expect(isQuotaExhaustedError(new Error('Something went wrong'))).toBe(false);
      expect(isQuotaExhaustedError(new Error('Network error'))).toBe(false);
      expect(isQuotaExhaustedError(new Error(''))).toBe(false);
    });

    it('should return false for null/undefined', () => {
      expect(isQuotaExhaustedError(null)).toBe(false);
      expect(isQuotaExhaustedError(undefined)).toBe(false);
    });

    it('should handle string errors', () => {
      expect(isQuotaExhaustedError('overloaded')).toBe(true);
      expect(isQuotaExhaustedError('regular error')).toBe(false);
    });
  });

  describe('isModelNotFoundError', () => {
    it('should return true for "does not exist or you do not have access" errors', () => {
      expect(
        isModelNotFoundError(
          new Error('The model `gpt-5.3-codex` does not exist or you do not have access to it.')
        )
      ).toBe(true);
    });

    it('should return true for model_not_found errors', () => {
      expect(isModelNotFoundError(new Error('model_not_found: gpt-5.3-codex'))).toBe(true);
    });

    it('should return true for invalid_model errors', () => {
      expect(isModelNotFoundError(new Error('invalid_model: unknown model'))).toBe(true);
    });

    it('should return true for "model does not exist" errors', () => {
      expect(isModelNotFoundError(new Error('The model does not exist'))).toBe(true);
    });

    it('should return true for "model not found" errors', () => {
      expect(isModelNotFoundError(new Error('model not found'))).toBe(true);
    });

    it('should return false for regular errors', () => {
      expect(isModelNotFoundError(new Error('Something went wrong'))).toBe(false);
      expect(isModelNotFoundError(new Error('Network error'))).toBe(false);
    });

    it('should return false for null/undefined', () => {
      expect(isModelNotFoundError(null)).toBe(false);
      expect(isModelNotFoundError(undefined)).toBe(false);
    });
  });

  describe('isStreamDisconnectedError', () => {
    it('should return true for "stream disconnected" errors', () => {
      expect(isStreamDisconnectedError(new Error('stream disconnected before completion'))).toBe(
        true
      );
    });

    it('should return true for "stream ended" errors', () => {
      expect(isStreamDisconnectedError(new Error('stream ended unexpectedly'))).toBe(true);
    });

    it('should return true for "connection reset" errors', () => {
      expect(isStreamDisconnectedError(new Error('connection reset by peer'))).toBe(true);
    });

    it('should return true for "socket hang up" errors', () => {
      expect(isStreamDisconnectedError(new Error('socket hang up'))).toBe(true);
    });

    it('should return true for ECONNRESET errors', () => {
      expect(isStreamDisconnectedError(new Error('ECONNRESET'))).toBe(true);
    });

    it('should return false for regular errors', () => {
      expect(isStreamDisconnectedError(new Error('Something went wrong'))).toBe(false);
      expect(isStreamDisconnectedError(new Error('Network error'))).toBe(false);
    });

    it('should return false for null/undefined', () => {
      expect(isStreamDisconnectedError(null)).toBe(false);
      expect(isStreamDisconnectedError(undefined)).toBe(false);
    });
  });

  describe('extractRetryAfter', () => {
    it('should extract retry-after from error message', () => {
      const error = new Error('Rate limit exceeded. retry-after: 60');
      expect(extractRetryAfter(error)).toBe(60);
    });

    it('should extract from retry_after format', () => {
      const error = new Error('retry_after: 120 seconds');
      expect(extractRetryAfter(error)).toBe(120);
    });

    it('should extract from wait format', () => {
      const error = new Error('Please wait: 30 seconds before retrying');
      expect(extractRetryAfter(error)).toBe(30);
    });

    it('should return undefined for rate limit errors without explicit retry-after', () => {
      const error = new Error('429 rate_limit_error');
      expect(extractRetryAfter(error)).toBeUndefined();
    });

    it('should return undefined for non-rate-limit errors', () => {
      const error = new Error('Something went wrong');
      expect(extractRetryAfter(error)).toBeUndefined();
    });

    it('should handle string errors', () => {
      expect(extractRetryAfter('retry-after: 45')).toBe(45);
    });
  });

  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.isCancellation).toBe(false);
      expect(result.isRateLimit).toBe(false);
      expect(result.isQuotaExhausted).toBe(false);
      expect(result.message).toBe('Authentication failed');
      expect(result.originalError).toBe(error);
    });

    it('should classify quota exhausted errors', () => {
      const error = new Error('overloaded_error: service is busy');
      const result = classifyError(error);

      expect(result.type).toBe('quota_exhausted');
      expect(result.isQuotaExhausted).toBe(true);
      expect(result.isRateLimit).toBe(false);
      expect(result.isAuth).toBe(false);
    });

    it('should classify credit balance errors as quota exhausted', () => {
      const error = new Error('credit balance is too low');
      const result = classifyError(error);

      expect(result.type).toBe('quota_exhausted');
      expect(result.isQuotaExhausted).toBe(true);
    });

    it('should classify usage limit errors as quota exhausted', () => {
      const error = new Error('usage limit reached');
      const result = classifyError(error);

      expect(result.type).toBe('quota_exhausted');
      expect(result.isQuotaExhausted).toBe(true);
    });

    it('should classify rate limit errors', () => {
      const error = new Error('Error: 429 rate_limit_error');
      const result = classifyError(error);

      expect(result.type).toBe('rate_limit');
      expect(result.isRateLimit).toBe(true);
      expect(result.isAuth).toBe(false);
      expect(result.retryAfter).toBe(60); // Default
    });

    it('should extract retryAfter from rate limit errors', () => {
      const error = new Error('429 - retry-after: 120');
      const result = classifyError(error);

      expect(result.type).toBe('rate_limit');
      expect(result.isRateLimit).toBe(true);
      expect(result.retryAfter).toBe(120);
    });

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

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

    it('should classify AbortError by name', () => {
      const error = new Error('Request cancelled');
      error.name = 'AbortError';
      const result = classifyError(error);

      expect(result.type).toBe('abort');
      expect(result.isAbort).toBe(true);
    });

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

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

    it('should classify model not found errors', () => {
      const error = new Error(
        'The model `gpt-5.3-codex` does not exist or you do not have access to it.'
      );
      const result = classifyError(error);

      expect(result.type).toBe('model_not_found');
      expect(result.isModelNotFound).toBe(true);
      expect(result.isStreamDisconnected).toBe(false);
      expect(result.isAuth).toBe(false);
    });

    it('should classify stream disconnected errors', () => {
      const error = new Error('stream disconnected before completion');
      const result = classifyError(error);

      expect(result.type).toBe('stream_disconnected');
      expect(result.isStreamDisconnected).toBe(true);
      expect(result.isModelNotFound).toBe(false);
      expect(result.isAuth).toBe(false);
    });

    it('should classify execution errors (regular 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);
      expect(result.isCancellation).toBe(false);
    });

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

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

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

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

    it('should prioritize authentication over rate limit', () => {
      const error = new Error('Authentication failed - 429');
      const result = classifyError(error);

      expect(result.type).toBe('authentication');
      expect(result.isAuth).toBe(true);
      expect(result.isRateLimit).toBe(true); // Both flags can be true
    });

    it('should prioritize rate limit over abort', () => {
      const error = new Error('429 rate_limit - aborted');
      const result = classifyError(error);

      expect(result.type).toBe('rate_limit');
      expect(result.isRateLimit).toBe(true);
      expect(result.isAbort).toBe(true);
    });

    it('should prioritize authentication over abort', () => {
      const error = new Error('Authentication failed - aborted');
      const result = classifyError(error);

      expect(result.type).toBe('authentication');
      expect(result.isAuth).toBe(true);
      expect(result.isAbort).toBe(true); // Both flags can be true
    });

    it('should prioritize abort over cancellation', () => {
      const error = new Error('Request cancelled');
      error.name = 'AbortError';
      const result = classifyError(error);

      expect(result.type).toBe('abort');
      expect(result.isAbort).toBe(true);
      expect(result.isCancellation).toBe(true); // Both flags can be true
    });

    it('should convert object errors to string', () => {
      const result = classifyError({ code: 500, message: 'Server error' });
      expect(result.message).toContain('Object');
    });

    it('should convert number errors to string', () => {
      const result = classifyError(404);
      expect(result.message).toBe('404');
      expect(result.type).toBe('unknown');
    });
  });

  describe('getUserFriendlyErrorMessage', () => {
    it('should return friendly message for abort errors', () => {
      const error = new Error('abort');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toBe('Operation was cancelled');
    });

    it('should return friendly message for AbortError by name', () => {
      const error = new Error('Something');
      error.name = 'AbortError';
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toBe('Operation was cancelled');
    });

    it('should return friendly message for authentication errors', () => {
      const error = new Error('Authentication failed');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toBe('Authentication failed. Please check your API key.');
    });

    it('should return friendly message for model not found errors', () => {
      const error = new Error(
        'The model `gpt-5.3-codex` does not exist or you do not have access to it.'
      );
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toContain('Model not available');
      expect(message).toContain('codex login');
    });

    it('should return friendly message for stream disconnected errors', () => {
      const error = new Error('stream disconnected before completion');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toContain('Connection interrupted');
      expect(message).toContain('stream was disconnected');
    });

    it('should return friendly message for quota exhausted errors', () => {
      const error = new Error('overloaded_error');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toContain('Usage limit reached');
      expect(message).toContain('Auto Mode has been paused');
    });

    it('should return friendly message for rate limit errors', () => {
      const error = new Error('429 rate_limit_error');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toContain('Rate limit exceeded');
      expect(message).toContain('60 seconds');
    });

    it('should include custom retry-after in rate limit message', () => {
      const error = new Error('429 - retry-after: 120');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toContain('Rate limit exceeded');
      expect(message).toContain('120 seconds');
    });

    it('should prioritize abort message over auth', () => {
      const error = new Error('Authentication failed - abort');
      const message = getUserFriendlyErrorMessage(error);

      // Auth is checked first in classifyError, but abort check happens before auth in getUserFriendlyErrorMessage
      expect(message).toBe('Operation was cancelled');
    });

    it('should return original message for other errors', () => {
      const error = new Error('Network timeout');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toBe('Network timeout');
    });

    it('should handle non-Error values', () => {
      expect(getUserFriendlyErrorMessage('string error')).toBe('string error');
      expect(getUserFriendlyErrorMessage(null)).toBe('Unknown error');
      expect(getUserFriendlyErrorMessage(undefined)).toBe('Unknown error');
    });

    it('should return original message for cancellation errors', () => {
      const error = new Error('Operation cancelled by user');
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toBe('Operation cancelled by user');
    });

    it('should handle Error without message', () => {
      const error = new Error();
      const message = getUserFriendlyErrorMessage(error);

      expect(message).toBe('');
    });
  });
});