|
1 | 1 | import { describe, test, expect } from 'bun:test' |
2 | 2 |
|
3 | 3 | import { |
| 4 | + getApiErrorDisplayMessage, |
| 5 | + getApiErrorResponseMessage, |
4 | 6 | isOutOfCreditsError, |
5 | 7 | isFreeModeUnavailableError, |
| 8 | + isFreeModeRateLimitedError, |
| 9 | + isRateLimitError, |
6 | 10 | getCountryBlockFromFreeModeError, |
7 | 11 | OUT_OF_CREDITS_MESSAGE, |
8 | 12 | FREE_MODE_UNAVAILABLE_MESSAGE, |
| 13 | + FREEBUFF_RATE_LIMIT_MESSAGE, |
9 | 14 | createErrorMessage, |
10 | 15 | } from '../error-handling' |
11 | 16 |
|
@@ -115,6 +120,138 @@ describe('error-handling', () => { |
115 | 120 | }) |
116 | 121 | }) |
117 | 122 |
|
| 123 | + describe('isRateLimitError', () => { |
| 124 | + test('returns true for error with statusCode 429', () => { |
| 125 | + const error = { statusCode: 429, message: 'Too Many Requests' } |
| 126 | + expect(isRateLimitError(error)).toBe(true) |
| 127 | + }) |
| 128 | + |
| 129 | + test('returns true for thrown API error with status 429', () => { |
| 130 | + const error = { status: 429, message: 'Too Many Requests' } |
| 131 | + expect(isRateLimitError(error)).toBe(true) |
| 132 | + }) |
| 133 | + |
| 134 | + test('returns true for retry-wrapped API errors with nested 429 statusCode', () => { |
| 135 | + expect( |
| 136 | + isRateLimitError({ |
| 137 | + message: 'Failed after 4 attempts. Last error: Too Many Requests', |
| 138 | + lastError: { |
| 139 | + statusCode: 429, |
| 140 | + message: 'Too Many Requests', |
| 141 | + }, |
| 142 | + }), |
| 143 | + ).toBe(true) |
| 144 | + }) |
| 145 | + |
| 146 | + test('returns false for non-429 status codes', () => { |
| 147 | + expect(isRateLimitError({ statusCode: 402 })).toBe(false) |
| 148 | + expect(isRateLimitError({ statusCode: 500 })).toBe(false) |
| 149 | + }) |
| 150 | + |
| 151 | + test('returns false for string statusCode', () => { |
| 152 | + expect(isRateLimitError({ statusCode: '429' })).toBe(false) |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + describe('isFreeModeRateLimitedError', () => { |
| 157 | + test('returns true for typed free mode rate limit errors', () => { |
| 158 | + expect( |
| 159 | + isFreeModeRateLimitedError({ |
| 160 | + statusCode: 429, |
| 161 | + error: 'free_mode_rate_limited', |
| 162 | + }), |
| 163 | + ).toBe(true) |
| 164 | + }) |
| 165 | + |
| 166 | + test('returns true when AI SDK API errors keep the code in responseBody', () => { |
| 167 | + expect( |
| 168 | + isFreeModeRateLimitedError({ |
| 169 | + statusCode: 429, |
| 170 | + message: 'Too Many Requests', |
| 171 | + responseBody: JSON.stringify({ |
| 172 | + error: 'free_mode_rate_limited', |
| 173 | + message: |
| 174 | + 'Free mode rate limit exceeded (1 minute limit). Try again in 30 seconds.', |
| 175 | + }), |
| 176 | + }), |
| 177 | + ).toBe(true) |
| 178 | + }) |
| 179 | + |
| 180 | + test('returns true for retry-wrapped API errors with responseBody details', () => { |
| 181 | + expect( |
| 182 | + isFreeModeRateLimitedError({ |
| 183 | + message: 'Failed after 4 attempts. Last error: Too Many Requests', |
| 184 | + lastError: { |
| 185 | + statusCode: 429, |
| 186 | + message: 'Too Many Requests', |
| 187 | + responseBody: JSON.stringify({ |
| 188 | + error: 'free_mode_rate_limited', |
| 189 | + message: |
| 190 | + 'Free mode rate limit exceeded (1 minute limit). Try again in 30 seconds.', |
| 191 | + }), |
| 192 | + }, |
| 193 | + }), |
| 194 | + ).toBe(true) |
| 195 | + }) |
| 196 | + |
| 197 | + test('returns false for untyped provider rate limit errors', () => { |
| 198 | + expect(isFreeModeRateLimitedError({ statusCode: 429 })).toBe(false) |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + describe('getApiErrorResponseMessage', () => { |
| 203 | + test('extracts the server message from API error responseBody', () => { |
| 204 | + const message = |
| 205 | + 'Free mode rate limit exceeded (1 minute limit). Try again in 30 seconds.' |
| 206 | + |
| 207 | + expect( |
| 208 | + getApiErrorResponseMessage({ |
| 209 | + statusCode: 429, |
| 210 | + message: 'Too Many Requests', |
| 211 | + responseBody: JSON.stringify({ |
| 212 | + error: 'free_mode_rate_limited', |
| 213 | + message, |
| 214 | + }), |
| 215 | + }), |
| 216 | + ).toBe(message) |
| 217 | + }) |
| 218 | + |
| 219 | + test('returns undefined when responseBody has no parsed message', () => { |
| 220 | + expect(getApiErrorResponseMessage({ statusCode: 429 })).toBeUndefined() |
| 221 | + }) |
| 222 | + }) |
| 223 | + |
| 224 | + describe('getApiErrorDisplayMessage', () => { |
| 225 | + test('prefers the server message from API error responseBody', () => { |
| 226 | + const message = |
| 227 | + 'Free mode rate limit exceeded (1 minute limit). Try again in 30 seconds.' |
| 228 | + |
| 229 | + expect( |
| 230 | + getApiErrorDisplayMessage({ |
| 231 | + statusCode: 429, |
| 232 | + message: 'Too Many Requests', |
| 233 | + responseBody: JSON.stringify({ |
| 234 | + error: 'free_mode_rate_limited', |
| 235 | + message, |
| 236 | + }), |
| 237 | + }), |
| 238 | + ).toBe(message) |
| 239 | + }) |
| 240 | + |
| 241 | + test('falls back to top-level message for normalized API errors', () => { |
| 242 | + const message = |
| 243 | + 'Free mode rate limit exceeded (1 minute limit). Try again in 30 seconds.' |
| 244 | + |
| 245 | + expect( |
| 246 | + getApiErrorDisplayMessage({ |
| 247 | + statusCode: 429, |
| 248 | + error: 'free_mode_rate_limited', |
| 249 | + message, |
| 250 | + }), |
| 251 | + ).toBe(message) |
| 252 | + }) |
| 253 | + }) |
| 254 | + |
118 | 255 | describe('getCountryBlockFromFreeModeError', () => { |
119 | 256 | test('extracts country block details from free-mode unavailable errors', () => { |
120 | 257 | const error = { |
@@ -177,6 +314,15 @@ describe('error-handling', () => { |
177 | 314 | }) |
178 | 315 | }) |
179 | 316 |
|
| 317 | + describe('FREEBUFF_RATE_LIMIT_MESSAGE', () => { |
| 318 | + test('encourages retry without mentioning credits or payment', () => { |
| 319 | + const message = FREEBUFF_RATE_LIMIT_MESSAGE.toLowerCase() |
| 320 | + expect(message).toContain('try again') |
| 321 | + expect(message).not.toContain('credit') |
| 322 | + expect(message).not.toContain('pay') |
| 323 | + }) |
| 324 | + }) |
| 325 | + |
180 | 326 | describe('createErrorMessage', () => { |
181 | 327 | test('creates message from Error object', () => { |
182 | 328 | const error = new Error('Something went wrong') |
|
0 commit comments