1- import { assert , beforeAll , describe , expect , it } from "vitest" ;
1+ import { assert , beforeAll , describe , expect , it , vi } from "vitest" ;
22import { getSuperuserToken } from "../../src/utils/get-superuser-token" ;
33import { checkE2eConnection } from "../_mocks/check-e2e-connection" ;
44import { createLoaderContext } from "../_mocks/create-loader-context" ;
@@ -16,6 +16,7 @@ describe("getSuperuserToken", () => {
1616 email : "invalid" ,
1717 password : "invalid"
1818 } ) ;
19+
1920 expect ( result ) . toBeUndefined ( ) ;
2021 } ) ;
2122
@@ -42,6 +43,37 @@ describe("getSuperuserToken", () => {
4243 options . url ,
4344 options . superuserCredentials
4445 ) ;
46+
4547 expect ( result ) . toBeDefined ( ) ;
4648 } ) ;
49+
50+ it ( "should retry on rate limit error" , async ( ) => {
51+ assert ( options . superuserCredentials , "Superuser credentials are not set." ) ;
52+ assert (
53+ ! ( "impersonateToken" in options . superuserCredentials ) ,
54+ "Impersonate token should not be used in tests."
55+ ) ;
56+
57+ vi . useFakeTimers ( {
58+ toFake : [ "setTimeout" ]
59+ } ) ;
60+ vi . spyOn ( global , "fetch" ) . mockResolvedValueOnce (
61+ new Response ( undefined , { status : 429 } )
62+ ) ;
63+
64+ const promise = getSuperuserToken (
65+ options . url ,
66+ options . superuserCredentials
67+ ) ;
68+
69+ // Fast-forward time to speed up retries
70+ await vi . runAllTimersAsync ( ) ;
71+
72+ const result = await promise ;
73+
74+ expect ( result ) . toBeDefined ( ) ;
75+ expect ( global . fetch ) . toHaveBeenCalledTimes ( 2 ) ;
76+
77+ vi . useRealTimers ( ) ;
78+ } ) ;
4779} ) ;
0 commit comments