11import { afterEach , describe , expect , it , vi } from "vitest" ;
2+ import { withFetchPreconnect } from "../../test/helpers/extensions/fetch-mock.js" ;
23import { refreshQwenPortalCredentials } from "./refresh.js" ;
34
45function expiredCredentials ( ) {
@@ -22,18 +23,20 @@ describe("refreshQwenPortalCredentials", () => {
2223 const runRefresh = async ( ) => await refreshQwenPortalCredentials ( expiredCredentials ( ) ) ;
2324
2425 it ( "refreshes oauth credentials and preserves existing refresh token when absent" , async ( ) => {
25- globalThis . fetch = vi . fn ( async ( ) => {
26- return new Response (
27- JSON . stringify ( {
28- access_token : "new-access" ,
29- expires_in : 3600 ,
30- } ) ,
31- {
32- status : 200 ,
33- headers : { "Content-Type" : "application/json" } ,
34- } ,
35- ) ;
36- } ) as typeof fetch ;
26+ globalThis . fetch = withFetchPreconnect (
27+ vi . fn ( async ( ) => {
28+ return new Response (
29+ JSON . stringify ( {
30+ access_token : "new-access" ,
31+ expires_in : 3600 ,
32+ } ) ,
33+ {
34+ status : 200 ,
35+ headers : { "Content-Type" : "application/json" } ,
36+ } ,
37+ ) ;
38+ } ) ,
39+ ) ;
3740
3841 const result = await runRefresh ( ) ;
3942
@@ -51,48 +54,52 @@ describe("refreshQwenPortalCredentials", () => {
5154 } ) ;
5255
5356 it ( "replaces the refresh token when the server rotates it" , async ( ) => {
54- globalThis . fetch = vi . fn ( async ( ) => {
55- return new Response (
56- JSON . stringify ( {
57- access_token : "new-access" ,
58- refresh_token : "rotated-refresh" ,
59- expires_in : 1200 ,
60- } ) ,
61- {
62- status : 200 ,
63- headers : { "Content-Type" : "application/json" } ,
64- } ,
65- ) ;
66- } ) as typeof fetch ;
57+ globalThis . fetch = withFetchPreconnect (
58+ vi . fn ( async ( ) => {
59+ return new Response (
60+ JSON . stringify ( {
61+ access_token : "new-access" ,
62+ refresh_token : "rotated-refresh" ,
63+ expires_in : 1200 ,
64+ } ) ,
65+ {
66+ status : 200 ,
67+ headers : { "Content-Type" : "application/json" } ,
68+ } ,
69+ ) ;
70+ } ) ,
71+ ) ;
6772
6873 const result = await runRefresh ( ) ;
6974
7075 expect ( result . refresh ) . toBe ( "rotated-refresh" ) ;
7176 } ) ;
7277
7378 it ( "rejects invalid expires_in payloads" , async ( ) => {
74- globalThis . fetch = vi . fn ( async ( ) => {
75- return new Response (
76- JSON . stringify ( {
77- access_token : "new-access" ,
78- expires_in : 0 ,
79- } ) ,
80- {
81- status : 200 ,
82- headers : { "Content-Type" : "application/json" } ,
83- } ,
84- ) ;
85- } ) as typeof fetch ;
79+ globalThis . fetch = withFetchPreconnect (
80+ vi . fn ( async ( ) => {
81+ return new Response (
82+ JSON . stringify ( {
83+ access_token : "new-access" ,
84+ expires_in : 0 ,
85+ } ) ,
86+ {
87+ status : 200 ,
88+ headers : { "Content-Type" : "application/json" } ,
89+ } ,
90+ ) ;
91+ } ) ,
92+ ) ;
8693
8794 await expect ( runRefresh ( ) ) . rejects . toThrow (
8895 "Qwen OAuth refresh response missing or invalid expires_in" ,
8996 ) ;
9097 } ) ;
9198
9299 it ( "turns 400 responses into a re-authenticate hint" , async ( ) => {
93- globalThis . fetch = vi . fn (
94- async ( ) => new Response ( "bad refresh" , { status : 400 } ) ,
95- ) as typeof fetch ;
100+ globalThis . fetch = withFetchPreconnect (
101+ vi . fn ( async ( ) => new Response ( "bad refresh" , { status : 400 } ) ) ,
102+ ) ;
96103
97104 await expect ( runRefresh ( ) ) . rejects . toThrow ( "Qwen OAuth refresh token expired or invalid" ) ;
98105 } ) ;
@@ -110,25 +117,27 @@ describe("refreshQwenPortalCredentials", () => {
110117 } ) ;
111118
112119 it ( "rejects missing access tokens" , async ( ) => {
113- globalThis . fetch = vi . fn ( async ( ) => {
114- return new Response (
115- JSON . stringify ( {
116- expires_in : 3600 ,
117- } ) ,
118- {
119- status : 200 ,
120- headers : { "Content-Type" : "application/json" } ,
121- } ,
122- ) ;
123- } ) as typeof fetch ;
120+ globalThis . fetch = withFetchPreconnect (
121+ vi . fn ( async ( ) => {
122+ return new Response (
123+ JSON . stringify ( {
124+ expires_in : 3600 ,
125+ } ) ,
126+ {
127+ status : 200 ,
128+ headers : { "Content-Type" : "application/json" } ,
129+ } ,
130+ ) ;
131+ } ) ,
132+ ) ;
124133
125134 await expect ( runRefresh ( ) ) . rejects . toThrow ( "Qwen OAuth refresh response missing access token" ) ;
126135 } ) ;
127136
128137 it ( "surfaces non-400 refresh failures" , async ( ) => {
129- globalThis . fetch = vi . fn (
130- async ( ) => new Response ( "gateway down" , { status : 502 } ) ,
131- ) as typeof fetch ;
138+ globalThis . fetch = withFetchPreconnect (
139+ vi . fn ( async ( ) => new Response ( "gateway down" , { status : 502 } ) ) ,
140+ ) ;
132141
133142 await expect ( runRefresh ( ) ) . rejects . toThrow ( "Qwen OAuth refresh failed: gateway down" ) ;
134143 } ) ;
0 commit comments