@@ -196,7 +196,44 @@ describe('Replicate client', () => {
196
196
expect ( ( error as ApiError ) . message ) . toContain ( "Invalid input" )
197
197
}
198
198
} )
199
- // Add more tests for error handling, edge cases, etc.
199
+
200
+ test ( 'Automatically retries on 429' , async ( ) => {
201
+ nock ( BASE_URL )
202
+ . post ( '/predictions' )
203
+ . reply ( 429 , {
204
+ detail : "Too many requests" ,
205
+ } , { "Content-Type" : "application/json" , "Retry-After" : "1" } )
206
+ . post ( '/predictions' )
207
+ . reply ( 201 , {
208
+ id : 'ufawqhfynnddngldkgtslldrkq' ,
209
+ } ) ;
210
+ const prediction = await client . predictions . create ( {
211
+ version :
212
+ '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa' ,
213
+ input : {
214
+ text : 'Alice' ,
215
+ } ,
216
+ } ) ;
217
+ expect ( prediction . id ) . toBe ( 'ufawqhfynnddngldkgtslldrkq' ) ;
218
+ } ) ;
219
+
220
+ test ( 'Does not automatically retry on 500' , async ( ) => {
221
+ nock ( BASE_URL )
222
+ . post ( '/predictions' )
223
+ . reply ( 500 , {
224
+ detail : "Internal server error" ,
225
+ } , { "Content-Type" : "application/json" } ) ;
226
+
227
+ await expect (
228
+ client . predictions . create ( {
229
+ version :
230
+ '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa' ,
231
+ input : {
232
+ text : 'Alice' ,
233
+ } ,
234
+ } )
235
+ ) . rejects . toThrow ( `Request to https://api.replicate.com/v1/predictions failed with status 500 Internal Server Error: {"detail":"Internal server error"}.` )
236
+ } ) ;
200
237
} ) ;
201
238
202
239
describe ( 'predictions.get' , ( ) => {
@@ -234,7 +271,40 @@ describe('Replicate client', () => {
234
271
) ;
235
272
expect ( prediction . id ) . toBe ( 'rrr4z55ocneqzikepnug6xezpe' ) ;
236
273
} ) ;
237
- // Add more tests for error handling, edge cases, etc.
274
+
275
+ test ( 'Automatically retries on 429' , async ( ) => {
276
+ nock ( BASE_URL )
277
+ . get ( '/predictions/rrr4z55ocneqzikepnug6xezpe' )
278
+ . reply ( 429 , {
279
+ detail : "Too many requests" ,
280
+ } , { "Content-Type" : "application/json" , "Retry-After" : "1" } )
281
+ . get ( '/predictions/rrr4z55ocneqzikepnug6xezpe' )
282
+ . reply ( 200 , {
283
+ id : 'rrr4z55ocneqzikepnug6xezpe' ,
284
+ } ) ;
285
+
286
+ const prediction = await client . predictions . get (
287
+ 'rrr4z55ocneqzikepnug6xezpe'
288
+ ) ;
289
+ expect ( prediction . id ) . toBe ( 'rrr4z55ocneqzikepnug6xezpe' ) ;
290
+ } ) ;
291
+
292
+ test ( 'Automatically retries on 500' , async ( ) => {
293
+ nock ( BASE_URL )
294
+ . get ( '/predictions/rrr4z55ocneqzikepnug6xezpe' )
295
+ . reply ( 500 , {
296
+ detail : "Internal server error" ,
297
+ } , { "Content-Type" : "application/json" } )
298
+ . get ( '/predictions/rrr4z55ocneqzikepnug6xezpe' )
299
+ . reply ( 200 , {
300
+ id : 'rrr4z55ocneqzikepnug6xezpe' ,
301
+ } ) ;
302
+
303
+ const prediction = await client . predictions . get (
304
+ 'rrr4z55ocneqzikepnug6xezpe'
305
+ ) ;
306
+ expect ( prediction . id ) . toBe ( 'rrr4z55ocneqzikepnug6xezpe' ) ;
307
+ } ) ;
238
308
} ) ;
239
309
240
310
describe ( 'predictions.cancel' , ( ) => {
0 commit comments