@@ -192,3 +192,110 @@ test('copyAuthKeyValue falls back to document.execCommand when clipboard API is
192192 assert . equal ( module . authKeyCopied , true ) ;
193193 assert . equal ( module . authKeyCopyError , false ) ;
194194} ) ;
195+
196+ test ( 'fetchAuthKeys preserves existing rows and surfaces non-auth HTTP errors' , async ( ) => {
197+ const module = createAuthKeysModule ( {
198+ fetch : async ( ) => ( {
199+ status : 500 ,
200+ ok : false ,
201+ statusText : 'Internal Server Error' ,
202+ async json ( ) {
203+ return {
204+ error : {
205+ message : 'storage unavailable'
206+ }
207+ } ;
208+ }
209+ } ) ,
210+ console : {
211+ error ( ) { }
212+ }
213+ } ) ;
214+
215+ module . authKeys = [ { id : 'existing-key' } ] ;
216+ module . headers = ( ) => ( { } ) ;
217+ module . handleFetchResponse = ( ) => false ;
218+
219+ await module . fetchAuthKeys ( ) ;
220+
221+ assert . deepEqual ( module . authKeys , [ { id : 'existing-key' } ] ) ;
222+ assert . equal ( module . authKeyError , 'storage unavailable' ) ;
223+ } ) ;
224+
225+ test ( 'fetchAuthKeys preserves existing rows on authentication failures handled by handleFetchResponse' , async ( ) => {
226+ const module = createAuthKeysModule ( {
227+ fetch : async ( ) => ( {
228+ status : 401 ,
229+ ok : false ,
230+ statusText : 'Unauthorized'
231+ } )
232+ } ) ;
233+
234+ module . authKeys = [ { id : 'existing-key' } ] ;
235+ module . headers = ( ) => ( { } ) ;
236+ module . handleFetchResponse = ( res ) => {
237+ if ( res . status === 401 ) {
238+ module . authError = true ;
239+ module . needsAuth = true ;
240+ }
241+ return false ;
242+ } ;
243+
244+ await module . fetchAuthKeys ( ) ;
245+
246+ assert . deepEqual ( module . authKeys , [ { id : 'existing-key' } ] ) ;
247+ assert . equal ( module . authKeyError , '' ) ;
248+ assert . equal ( module . authError , true ) ;
249+ assert . equal ( module . needsAuth , true ) ;
250+ } ) ;
251+
252+ test ( 'openAuthKeyForm and closeAuthKeyForm preserve an issued key instead of clearing it' , ( ) => {
253+ const module = createAuthKeysModule ( ) ;
254+ module . authKeyIssuedValue = 'sk_gom_once' ;
255+
256+ module . openAuthKeyForm ( ) ;
257+ assert . equal ( module . authKeyFormOpen , true ) ;
258+ assert . equal ( module . authKeyIssuedValue , 'sk_gom_once' ) ;
259+
260+ module . closeAuthKeyForm ( ) ;
261+ assert . equal ( module . authKeyFormOpen , false ) ;
262+ assert . equal ( module . authKeyIssuedValue , 'sk_gom_once' ) ;
263+ } ) ;
264+
265+ test ( 'submitAuthKeyForm reopens the editor if issuance finishes after a manual close' , async ( ) => {
266+ let resolveResponse ;
267+ const responsePromise = new Promise ( ( resolve ) => {
268+ resolveResponse = resolve ;
269+ } ) ;
270+ const module = createAuthKeysModule ( {
271+ fetch : async ( ) => responsePromise
272+ } ) ;
273+
274+ module . headers = ( ) => ( { 'Content-Type' : 'application/json' } ) ;
275+ module . fetchAuthKeys = async ( ) => { } ;
276+ module . authKeyFormOpen = true ;
277+ module . authKeyForm = {
278+ name : 'ci-deploy' ,
279+ description : '' ,
280+ expires_at : ''
281+ } ;
282+
283+ const submitPromise = module . submitAuthKeyForm ( ) ;
284+ module . closeAuthKeyForm ( ) ;
285+
286+ assert . equal ( module . authKeyFormOpen , false ) ;
287+ assert . equal ( module . authKeyFormSubmitting , true ) ;
288+
289+ resolveResponse ( {
290+ status : 201 ,
291+ async json ( ) {
292+ return { value : 'sk_gom_async' } ;
293+ }
294+ } ) ;
295+
296+ await submitPromise ;
297+
298+ assert . equal ( module . authKeyFormOpen , true ) ;
299+ assert . equal ( module . authKeyIssuedValue , 'sk_gom_async' ) ;
300+ assert . equal ( module . authKeyFormSubmitting , false ) ;
301+ } ) ;
0 commit comments