@@ -290,4 +290,113 @@ console.log('test');]]></replace></change>`
290290 expect ( mockProvider . provideInlineCompletionItems ) . not . toHaveBeenCalled ( )
291291 } )
292292 } )
293+
294+ describe ( "updateInlineCompletionProviderRegistration" , ( ) => {
295+ it ( "should register provider when enableAutoTrigger is true" , async ( ) => {
296+ const mockDisposable = { dispose : vi . fn ( ) }
297+ const mockRegister = vi . fn ( ) . mockReturnValue ( mockDisposable )
298+
299+ const mockManager = {
300+ settings : { enableAutoTrigger : true } as any ,
301+ inlineCompletionProviderDisposable : null as any ,
302+ inlineCompletionProvider : { } as any ,
303+ context : { subscriptions : [ ] as any [ ] } ,
304+ async updateInlineCompletionProviderRegistration ( ) {
305+ const shouldBeRegistered = this . settings ?. enableAutoTrigger ?? false
306+
307+ if ( shouldBeRegistered && ! this . inlineCompletionProviderDisposable ) {
308+ this . inlineCompletionProviderDisposable = mockRegister ( "*" , this . inlineCompletionProvider )
309+ this . context . subscriptions . push ( this . inlineCompletionProviderDisposable )
310+ } else if ( ! shouldBeRegistered && this . inlineCompletionProviderDisposable ) {
311+ this . inlineCompletionProviderDisposable . dispose ( )
312+ this . inlineCompletionProviderDisposable = null
313+ }
314+ } ,
315+ }
316+
317+ await mockManager . updateInlineCompletionProviderRegistration ( )
318+
319+ expect ( mockRegister ) . toHaveBeenCalledWith ( "*" , mockManager . inlineCompletionProvider )
320+ expect ( mockManager . inlineCompletionProviderDisposable ) . toBe ( mockDisposable )
321+ expect ( mockManager . context . subscriptions ) . toContain ( mockDisposable )
322+ } )
323+
324+ it ( "should deregister provider when enableAutoTrigger is false" , async ( ) => {
325+ const mockDisposable = { dispose : vi . fn ( ) }
326+
327+ const mockManager = {
328+ settings : { enableAutoTrigger : false } as any ,
329+ inlineCompletionProviderDisposable : mockDisposable as any ,
330+ inlineCompletionProvider : { } as any ,
331+ context : { subscriptions : [ mockDisposable ] as any [ ] } ,
332+ async updateInlineCompletionProviderRegistration ( ) {
333+ const shouldBeRegistered = this . settings ?. enableAutoTrigger ?? false
334+
335+ if ( shouldBeRegistered && ! this . inlineCompletionProviderDisposable ) {
336+ // Register logic (not executed in this test)
337+ } else if ( ! shouldBeRegistered && this . inlineCompletionProviderDisposable ) {
338+ this . inlineCompletionProviderDisposable . dispose ( )
339+ this . inlineCompletionProviderDisposable = null
340+ }
341+ } ,
342+ }
343+
344+ await mockManager . updateInlineCompletionProviderRegistration ( )
345+
346+ expect ( mockDisposable . dispose ) . toHaveBeenCalled ( )
347+ expect ( mockManager . inlineCompletionProviderDisposable ) . toBeNull ( )
348+ } )
349+
350+ it ( "should not register provider twice when already registered" , async ( ) => {
351+ const mockDisposable = { dispose : vi . fn ( ) }
352+ const mockRegister = vi . fn ( ) . mockReturnValue ( mockDisposable )
353+
354+ const mockManager = {
355+ settings : { enableAutoTrigger : true } as any ,
356+ inlineCompletionProviderDisposable : mockDisposable as any ,
357+ inlineCompletionProvider : { } as any ,
358+ context : { subscriptions : [ mockDisposable ] as any [ ] } ,
359+ async updateInlineCompletionProviderRegistration ( ) {
360+ const shouldBeRegistered = this . settings ?. enableAutoTrigger ?? false
361+
362+ if ( shouldBeRegistered && ! this . inlineCompletionProviderDisposable ) {
363+ this . inlineCompletionProviderDisposable = mockRegister ( "*" , this . inlineCompletionProvider )
364+ this . context . subscriptions . push ( this . inlineCompletionProviderDisposable )
365+ } else if ( ! shouldBeRegistered && this . inlineCompletionProviderDisposable ) {
366+ this . inlineCompletionProviderDisposable . dispose ( )
367+ this . inlineCompletionProviderDisposable = null
368+ }
369+ } ,
370+ }
371+
372+ await mockManager . updateInlineCompletionProviderRegistration ( )
373+
374+ expect ( mockRegister ) . not . toHaveBeenCalled ( )
375+ expect ( mockManager . inlineCompletionProviderDisposable ) . toBe ( mockDisposable )
376+ } )
377+
378+ it ( "should not deregister when already deregistered" , async ( ) => {
379+ const mockManager = {
380+ settings : { enableAutoTrigger : false } as any ,
381+ inlineCompletionProviderDisposable : null as any ,
382+ inlineCompletionProvider : { } as any ,
383+ context : { subscriptions : [ ] as any [ ] } ,
384+ async updateInlineCompletionProviderRegistration ( ) {
385+ const shouldBeRegistered = this . settings ?. enableAutoTrigger ?? false
386+
387+ if ( shouldBeRegistered && ! this . inlineCompletionProviderDisposable ) {
388+ // Register logic (not executed in this test)
389+ } else if ( ! shouldBeRegistered && this . inlineCompletionProviderDisposable ) {
390+ this . inlineCompletionProviderDisposable . dispose ( )
391+ this . inlineCompletionProviderDisposable = null
392+ }
393+ } ,
394+ }
395+
396+ // Should not throw or cause issues
397+ await mockManager . updateInlineCompletionProviderRegistration ( )
398+
399+ expect ( mockManager . inlineCompletionProviderDisposable ) . toBeNull ( )
400+ } )
401+ } )
293402} )
0 commit comments