@@ -3,6 +3,10 @@ import { getUserInfoFromApiKey } from '../impl/database'
33import type { Logger } from '@codebuff/common/types/contracts/logger'
44import { isAuthenticationError , isNetworkError } from '../errors'
55
6+ const setMockFetch = ( impl : ( ) => Promise < any > ) => {
7+ globalThis . fetch = mock ( impl ) as unknown as typeof fetch
8+ }
9+
610describe ( 'getUserInfoFromApiKey error handling' , ( ) => {
711 const originalFetch = globalThis . fetch
812 let mockLogger : Logger
@@ -22,7 +26,7 @@ describe('getUserInfoFromApiKey error handling', () => {
2226
2327 describe ( 'authentication errors' , ( ) => {
2428 test ( 'throws AUTH_FAILED error for 401 status' , async ( ) => {
25- globalThis . fetch = mock ( ( ) =>
29+ setMockFetch ( ( ) =>
2630 Promise . resolve ( {
2731 ok : false ,
2832 status : 401 ,
@@ -47,7 +51,7 @@ describe('getUserInfoFromApiKey error handling', () => {
4751 } )
4852
4953 test ( 'throws AUTH_FAILED error for 403 status' , async ( ) => {
50- globalThis . fetch = mock ( ( ) =>
54+ setMockFetch ( ( ) =>
5155 Promise . resolve ( {
5256 ok : false ,
5357 status : 403 ,
@@ -72,7 +76,7 @@ describe('getUserInfoFromApiKey error handling', () => {
7276 } )
7377
7478 test ( 'throws NETWORK_ERROR for 500 server errors' , async ( ) => {
75- globalThis . fetch = mock ( ( ) =>
79+ setMockFetch ( ( ) =>
7680 Promise . resolve ( {
7781 ok : false ,
7882 status : 500 ,
@@ -100,9 +104,7 @@ describe('getUserInfoFromApiKey error handling', () => {
100104
101105 describe ( 'network errors' , ( ) => {
102106 test ( 'throws NETWORK_ERROR for fetch failures' , async ( ) => {
103- globalThis . fetch = mock ( ( ) =>
104- Promise . reject ( new Error ( 'Network request failed' ) ) ,
105- )
107+ setMockFetch ( ( ) => Promise . reject ( new Error ( 'Network request failed' ) ) )
106108
107109 try {
108110 await getUserInfoFromApiKey ( {
@@ -119,9 +121,7 @@ describe('getUserInfoFromApiKey error handling', () => {
119121 } )
120122
121123 test ( 'throws NETWORK_ERROR for connection timeout' , async ( ) => {
122- globalThis . fetch = mock ( ( ) =>
123- Promise . reject ( new Error ( 'Connection timeout' ) ) ,
124- )
124+ setMockFetch ( ( ) => Promise . reject ( new Error ( 'Connection timeout' ) ) )
125125
126126 try {
127127 await getUserInfoFromApiKey ( {
@@ -137,9 +137,7 @@ describe('getUserInfoFromApiKey error handling', () => {
137137 } )
138138
139139 test ( 'logs network errors with masked API key' , async ( ) => {
140- globalThis . fetch = mock ( ( ) =>
141- Promise . reject ( new Error ( 'Network failure' ) ) ,
142- )
140+ setMockFetch ( ( ) => Promise . reject ( new Error ( 'Network failure' ) ) )
143141
144142 try {
145143 await getUserInfoFromApiKey ( {
@@ -160,7 +158,7 @@ describe('getUserInfoFromApiKey error handling', () => {
160158
161159 describe ( 'successful requests' , ( ) => {
162160 test ( 'returns user info for valid API key' , async ( ) => {
163- globalThis . fetch = mock ( ( ) =>
161+ setMockFetch ( ( ) =>
164162 Promise . resolve ( {
165163 ok : true ,
166164 json : ( ) =>
@@ -195,7 +193,7 @@ describe('getUserInfoFromApiKey error handling', () => {
195193 } ) ,
196194 } as Response ) ,
197195 )
198- globalThis . fetch = mockFetch as any
196+ globalThis . fetch = mockFetch as unknown as typeof fetch
199197
200198 const apiKey = 'cache-test-key'
201199
@@ -220,7 +218,7 @@ describe('getUserInfoFromApiKey error handling', () => {
220218
221219 describe ( 'error propagation' , ( ) => {
222220 test ( 're-throws AUTH_FAILED errors without wrapping' , async ( ) => {
223- globalThis . fetch = mock ( ( ) =>
221+ setMockFetch ( ( ) =>
224222 Promise . resolve ( {
225223 ok : false ,
226224 status : 401 ,
@@ -241,9 +239,7 @@ describe('getUserInfoFromApiKey error handling', () => {
241239 } )
242240
243241 test ( 'wraps non-auth errors in NETWORK_ERROR' , async ( ) => {
244- globalThis . fetch = mock ( ( ) =>
245- Promise . reject ( new Error ( 'Random error' ) ) ,
246- )
242+ setMockFetch ( ( ) => Promise . reject ( new Error ( 'Random error' ) ) )
247243
248244 try {
249245 await getUserInfoFromApiKey ( {
0 commit comments