|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { retryAfterSeconds, isRateLimited, formatRetryHint } from './retryHint' |
| 3 | + |
| 4 | +describe('retryAfterSeconds', () => { |
| 5 | + it('reads a numeric retryAfter field', () => { |
| 6 | + expect(retryAfterSeconds({ retryAfter: 30 })).toBe(30) |
| 7 | + }) |
| 8 | + |
| 9 | + it('ceils a fractional retryAfter to whole seconds', () => { |
| 10 | + expect(retryAfterSeconds({ retryAfter: 12.4 })).toBe(13) |
| 11 | + }) |
| 12 | + |
| 13 | + it('accepts retryAfter of 0', () => { |
| 14 | + expect(retryAfterSeconds({ retryAfter: 0 })).toBe(0) |
| 15 | + }) |
| 16 | + |
| 17 | + it('ignores a negative retryAfter field', () => { |
| 18 | + expect(retryAfterSeconds({ retryAfter: -5 })).toBeNull() |
| 19 | + }) |
| 20 | + |
| 21 | + it('falls back to a "retry after Ns" hint in the message', () => { |
| 22 | + expect(retryAfterSeconds({ message: 'rate limited (retry after 45s)' })).toBe(45) |
| 23 | + }) |
| 24 | + |
| 25 | + it('falls back to a "retry in Ns" hint in the message', () => { |
| 26 | + expect(retryAfterSeconds({ message: 'too many requests, retry in 8 seconds' })).toBe(8) |
| 27 | + }) |
| 28 | + |
| 29 | + it('returns null when no hint is present', () => { |
| 30 | + expect(retryAfterSeconds({ message: 'something else broke' })).toBeNull() |
| 31 | + expect(retryAfterSeconds(new Error('plain error'))).toBeNull() |
| 32 | + }) |
| 33 | + |
| 34 | + it('never throws on non-object input', () => { |
| 35 | + expect(retryAfterSeconds(null)).toBeNull() |
| 36 | + expect(retryAfterSeconds(undefined)).toBeNull() |
| 37 | + expect(retryAfterSeconds('a string')).toBeNull() |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('isRateLimited', () => { |
| 42 | + it('is true for an error with status 429', () => { |
| 43 | + expect(isRateLimited({ status: 429 })).toBe(true) |
| 44 | + }) |
| 45 | + |
| 46 | + it('is false for other statuses and non-objects', () => { |
| 47 | + expect(isRateLimited({ status: 500 })).toBe(false) |
| 48 | + expect(isRateLimited({ status: 402 })).toBe(false) |
| 49 | + expect(isRateLimited(null)).toBe(false) |
| 50 | + expect(isRateLimited('429')).toBe(false) |
| 51 | + }) |
| 52 | +}) |
| 53 | + |
| 54 | +describe('formatRetryHint', () => { |
| 55 | + it('renders sub-minute delays in seconds', () => { |
| 56 | + expect(formatRetryHint(1)).toBe('Please retry in 1 second.') |
| 57 | + expect(formatRetryHint(30)).toBe('Please retry in 30 seconds.') |
| 58 | + expect(formatRetryHint(59)).toBe('Please retry in 59 seconds.') |
| 59 | + }) |
| 60 | + |
| 61 | + it('rolls up minute-plus delays to minutes', () => { |
| 62 | + expect(formatRetryHint(60)).toBe('Please retry in about 1 minute.') |
| 63 | + expect(formatRetryHint(150)).toBe('Please retry in about 3 minutes.') |
| 64 | + }) |
| 65 | + |
| 66 | + it('handles 0 and null', () => { |
| 67 | + expect(formatRetryHint(0)).toBe('You can retry now.') |
| 68 | + expect(formatRetryHint(null)).toBe('Please retry in a moment.') |
| 69 | + expect(formatRetryHint(-1)).toBe('Please retry in a moment.') |
| 70 | + }) |
| 71 | +}) |
0 commit comments