@@ -10,6 +10,7 @@ import { CursorUpdateManager } from '../../../../src/app/inline/cursorUpdateMana
10
10
import { globals } from 'aws-core-vscode/shared'
11
11
import assert from 'assert'
12
12
import { AmazonQInlineCompletionItemProvider } from '../../../../src/app/inline/completion'
13
+ import { CodeSuggestionsState } from 'aws-core-vscode/codewhisperer'
13
14
14
15
describe ( 'CursorUpdateManager' , ( ) => {
15
16
let cursorUpdateManager : CursorUpdateManager
@@ -236,4 +237,93 @@ describe('CursorUpdateManager', () => {
236
237
// Verify the provider was called again
237
238
assert . strictEqual ( provideStub . callCount , 1 , 'Update should be sent when position has changed' )
238
239
} )
240
+
241
+ describe ( 'autotrigger state handling' , ( ) => {
242
+ class TestCodeSuggestionsState extends CodeSuggestionsState {
243
+ private _isEnabled : boolean
244
+ private _onDidChangeStateEmitter = new vscode . EventEmitter < boolean > ( )
245
+
246
+ public constructor ( initialState : boolean = true ) {
247
+ super ( initialState )
248
+ this . _isEnabled = initialState
249
+ }
250
+
251
+ public override onDidChangeState = this . _onDidChangeStateEmitter . event
252
+
253
+ public override isSuggestionsEnabled ( ) : boolean {
254
+ return this . _isEnabled
255
+ }
256
+
257
+ public override async setSuggestionsEnabled ( enabled : boolean ) : Promise < void > {
258
+ if ( this . _isEnabled !== enabled ) {
259
+ this . _isEnabled = enabled
260
+ this . _onDidChangeStateEmitter . fire ( enabled )
261
+ }
262
+ }
263
+ }
264
+
265
+ let testCodeSuggestionsState : TestCodeSuggestionsState
266
+ let instanceStub : sinon . SinonStub
267
+ let testCursorUpdateManager : CursorUpdateManager
268
+
269
+ beforeEach ( ( ) => {
270
+ // Create test instance
271
+ testCodeSuggestionsState = new TestCodeSuggestionsState ( true )
272
+
273
+ // Stub the static getter to return our test instance
274
+ instanceStub = sinon . stub ( CodeSuggestionsState , 'instance' ) . get ( ( ) => testCodeSuggestionsState )
275
+
276
+ // Create the manager AFTER the stub is in place so it uses the test instance
277
+ const mockInlineCompletionProvider = {
278
+ provideInlineCompletionItems : sinon . stub ( ) . resolves ( [ ] ) ,
279
+ } as unknown as AmazonQInlineCompletionItemProvider
280
+ testCursorUpdateManager = new CursorUpdateManager ( languageClient , mockInlineCompletionProvider )
281
+ } )
282
+
283
+ afterEach ( ( ) => {
284
+ // Dispose the test manager
285
+ testCursorUpdateManager . dispose ( )
286
+ // Restore the original getter
287
+ instanceStub . restore ( )
288
+ } )
289
+
290
+ it ( 'should not start timer when autotrigger is disabled' , async ( ) => {
291
+ // Test the new behavior: timer doesn't start when autotrigger is disabled
292
+ await testCodeSuggestionsState . setSuggestionsEnabled ( false )
293
+ sendRequestStub . resolves ( { } )
294
+
295
+ await testCursorUpdateManager . start ( )
296
+
297
+ // Manager should be active but timer should not be started
298
+ assert . strictEqual ( ( testCursorUpdateManager as any ) . isActive , true )
299
+ assert . ok ( ! setIntervalStub . called , 'Timer should NOT be started when autotrigger is disabled' )
300
+ } )
301
+
302
+ it ( 'should start/stop timer when autotrigger state changes' , async ( ) => {
303
+ // Start with autotrigger enabled
304
+ await testCodeSuggestionsState . setSuggestionsEnabled ( true )
305
+ sendRequestStub . resolves ( { } )
306
+ await testCursorUpdateManager . start ( )
307
+
308
+ // Reset stubs to test state changes
309
+ setIntervalStub . resetHistory ( )
310
+ clearIntervalStub . resetHistory ( )
311
+
312
+ // Simulate autotrigger being disabled
313
+ await testCodeSuggestionsState . setSuggestionsEnabled ( false )
314
+ assert . ok ( clearIntervalStub . called , 'Timer should be stopped when autotrigger is disabled' )
315
+
316
+ // Simulate autotrigger being enabled again
317
+ await testCodeSuggestionsState . setSuggestionsEnabled ( true )
318
+ assert . ok ( setIntervalStub . called , 'Timer should be started when autotrigger is re-enabled' )
319
+ } )
320
+
321
+ it ( 'should dispose autotrigger state listener on dispose' , ( ) => {
322
+ testCursorUpdateManager . dispose ( )
323
+ // The dispose method should clean up the state listener
324
+ // We can't easily test the disposal without more complex mocking,
325
+ // but we can at least verify dispose doesn't throw
326
+ assert . ok ( true , 'Dispose should complete without errors' )
327
+ } )
328
+ } )
239
329
} )
0 commit comments