| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { PlatformErrorCategory } from '../platforms/platforms.exception' |
| 3 | import { |
| 4 | categoryFromGoogleApiError, |
| 5 | googleApiErrorReason, |
| 6 | googleApiPlatformCode, |
| 7 | googleApiPlatformMessage, |
| 8 | isGoogleApiErrorRetryable, |
| 9 | } from './google-api-error.util' |
| 10 | |
| 11 | describe('google api error classifier', () => { |
| 12 | it('classifies Google error reasons before falling back to HTTP status', () => { |
| 13 | const body = { |
| 14 | error: { |
| 15 | code: 403, |
| 16 | message: 'Quota exceeded.', |
| 17 | status: 'RESOURCE_EXHAUSTED', |
| 18 | errors: [ |
| 19 | { |
| 20 | reason: 'quotaExceeded', |
| 21 | message: 'Daily Limit Exceeded', |
| 22 | }, |
| 23 | ], |
| 24 | }, |
| 25 | } |
| 26 | |
| 27 | expect(categoryFromGoogleApiError(403, body)).toBe(PlatformErrorCategory.Quota) |
| 28 | expect(isGoogleApiErrorRetryable(403, body)).toBe(false) |
| 29 | expect(googleApiPlatformCode(body)).toBe('RESOURCE_EXHAUSTED') |
| 30 | expect(googleApiPlatformMessage(body)).toBe('Daily Limit Exceeded') |
| 31 | }) |
| 32 | |
| 33 | it('classifies rate limit reasons as retryable', () => { |
| 34 | const body = { |
| 35 | error: { |
| 36 | code: 403, |
| 37 | message: 'Rate Limit Exceeded', |
| 38 | errors: [ |
| 39 | { |
| 40 | reason: 'userRateLimitExceeded', |
| 41 | }, |
| 42 | ], |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | expect(categoryFromGoogleApiError(403, body)).toBe(PlatformErrorCategory.RateLimit) |
| 47 | expect(isGoogleApiErrorRetryable(403, body)).toBe(true) |
| 48 | }) |
| 49 | |
| 50 | it('classifies OAuth token errors as auth failures', () => { |
| 51 | const body = { |
| 52 | error: 'invalid_grant', |
| 53 | error_description: 'Bad Request', |
| 54 | } |
| 55 | |
| 56 | expect(categoryFromGoogleApiError(400, body)).toBe(PlatformErrorCategory.Auth) |
| 57 | expect(googleApiPlatformCode(body)).toBe('invalid_grant') |
| 58 | expect(googleApiPlatformMessage(body)).toBe('Bad Request') |
| 59 | }) |
| 60 | |
| 61 | it('reads structured Google API error reasons', () => { |
| 62 | const body = { |
| 63 | error: { |
| 64 | code: 400, |
| 65 | message: 'The <code>snippet.categoryId</code> property specifies an invalid category ID.', |
| 66 | errors: [ |
| 67 | { |
| 68 | reason: 'invalidCategoryId', |
| 69 | location: 'body.snippet.categoryId', |
| 70 | locationType: 'other', |
| 71 | }, |
| 72 | ], |
| 73 | }, |
| 74 | } |
| 75 | |
| 76 | expect(googleApiErrorReason(body)).toBe('invalidCategoryId') |
| 77 | }) |
| 78 | }) |
| 79 |