Skip to content

Commit 975a34e

Browse files
committed
fix bugs in test file
1 parent 93ac431 commit 975a34e

File tree

1 file changed

+46
-201
lines changed

1 file changed

+46
-201
lines changed

packages/core/src/test/amazonq/autoDebug/autoDebugFeature.test.ts

Lines changed: 46 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,21 @@ import * as vscode from 'vscode'
88
import sinon from 'sinon'
99
import { AutoDebugFeature } from '../../../amazonq/autoDebug/index'
1010
import { AutoDebugController, AutoDebugConfig } from '../../../amazonq/autoDebug/autoDebugController'
11-
import { ContextMenuProvider } from '../../../amazonq/autoDebug/ide/contextMenuProvider'
12-
import { AutoDebugCodeActionsProvider } from '../../../amazonq/autoDebug/ide/codeActionsProvider'
1311
import { Commands } from '../../../shared/vscode/commands2'
1412
import { focusAmazonQPanel } from '../../../codewhispererChat/commands/registerCommands'
15-
import { getLogger } from '../../../shared/logger/logger'
16-
1713
describe('AutoDebugFeature', function () {
1814
let autoDebugFeature: AutoDebugFeature
1915
let mockContext: vscode.ExtensionContext
20-
let loggerStub: sinon.SinonStub
2116
let commandsRegisterStub: sinon.SinonStub
2217
let focusAmazonQPanelStub: sinon.SinonStub
23-
let vscodeWindowStubs: {
24-
showWarningMessage: sinon.SinonStub
25-
showInformationMessage: sinon.SinonStub
26-
showErrorMessage: sinon.SinonStub
27-
setStatusBarMessage: sinon.SinonStub
28-
}
2918

3019
beforeEach(function () {
20+
// Mock Commands
21+
commandsRegisterStub = sinon.stub(Commands, 'register')
22+
23+
// Mock focusAmazonQPanel
24+
focusAmazonQPanelStub = sinon.stub(focusAmazonQPanel, 'execute')
25+
3126
// Mock VSCode APIs
3227
mockContext = {
3328
subscriptions: [],
@@ -41,29 +36,6 @@ describe('AutoDebugFeature', function () {
4136
},
4237
} as any
4338

44-
// Mock VSCode window methods
45-
vscodeWindowStubs = {
46-
showWarningMessage: sinon.stub(vscode.window, 'showWarningMessage'),
47-
showInformationMessage: sinon.stub(vscode.window, 'showInformationMessage'),
48-
showErrorMessage: sinon.stub(vscode.window, 'showErrorMessage'),
49-
setStatusBarMessage: sinon.stub(vscode.window, 'setStatusBarMessage'),
50-
}
51-
52-
// Mock Commands
53-
commandsRegisterStub = sinon.stub(Commands, 'register')
54-
55-
// Mock focusAmazonQPanel
56-
focusAmazonQPanelStub = sinon.stub(focusAmazonQPanel, 'execute')
57-
58-
// Mock logger to avoid noise in tests
59-
loggerStub = sinon.stub().returns({
60-
debug: sinon.stub(),
61-
info: sinon.stub(),
62-
warn: sinon.stub(),
63-
error: sinon.stub(),
64-
})
65-
sinon.stub(getLogger as any, 'default').returns(loggerStub())
66-
6739
// Create fresh instance for each test
6840
autoDebugFeature = new AutoDebugFeature()
6941
})
@@ -82,14 +54,13 @@ describe('AutoDebugFeature', function () {
8254
})
8355

8456
describe('activate', function () {
57+
let getConfigStub: sinon.SinonStub
58+
let startSessionStub: sinon.SinonStub
59+
8560
beforeEach(function () {
8661
// Mock the controller and providers that will be created during activation
87-
sinon.stub(AutoDebugController.prototype, 'constructor' as any)
88-
sinon.stub(AutoDebugController.prototype, 'getConfig').returns(createMockConfig())
89-
sinon.stub(AutoDebugController.prototype, 'startSession').resolves()
90-
91-
sinon.stub(ContextMenuProvider.prototype, 'constructor' as any)
92-
sinon.stub(AutoDebugCodeActionsProvider.prototype, 'constructor' as any)
62+
getConfigStub = sinon.stub(AutoDebugController.prototype, 'getConfig').returns(createMockConfig())
63+
startSessionStub = sinon.stub(AutoDebugController.prototype, 'startSession').resolves()
9364
})
9465

9566
it('activates successfully with default config', async function () {
@@ -122,36 +93,53 @@ describe('AutoDebugFeature', function () {
12293
})
12394

12495
it('registers commands', async function () {
96+
// Reset the command register stub to count only calls from this test
97+
commandsRegisterStub.resetHistory()
98+
12599
await autoDebugFeature.activate(mockContext)
126100

127-
assert.strictEqual(commandsRegisterStub.callCount, 3)
101+
// Check that the specific AutoDebug commands were registered
102+
const autoDebugCalls = commandsRegisterStub
103+
.getCalls()
104+
.filter((call) => typeof call.args[0] === 'string' && call.args[0].includes('amazonq.autoDebug'))
105+
106+
assert.strictEqual(autoDebugCalls.length, 3)
128107
assert.ok(commandsRegisterStub.calledWith('amazonq.autoDebug.detectProblems'))
129108
assert.ok(commandsRegisterStub.calledWith('amazonq.autoDebug.toggle'))
130109
assert.ok(commandsRegisterStub.calledWith('amazonq.autoDebug.showStatus'))
131110
})
132111

133112
it('starts session when enabled', async function () {
134-
const startSessionStub = sinon.stub(AutoDebugController.prototype, 'startSession').resolves()
135-
136113
await autoDebugFeature.activate(mockContext)
137114

138115
assert.ok(startSessionStub.calledOnce)
139116
})
140117

141118
it('does not start session when disabled', async function () {
142119
const mockConfig = createMockConfig({ enabled: false })
143-
sinon.stub(AutoDebugController.prototype, 'getConfig').returns(mockConfig)
144-
const startSessionStub = sinon.stub(AutoDebugController.prototype, 'startSession').resolves()
120+
getConfigStub.returns(mockConfig)
145121

146122
await autoDebugFeature.activate(mockContext)
147123

148124
assert.ok(startSessionStub.notCalled)
149125
})
150126

151127
it('throws on activation failure', async function () {
152-
sinon.stub(AutoDebugController.prototype, 'constructor' as any).throws(new Error('Test error'))
128+
// Stub the AutoDebugController constructor to throw during instantiation
129+
const originalConstructor = AutoDebugController
130+
const MockAutoDebugController = function (this: any) {
131+
throw new Error('Test error')
132+
}
133+
MockAutoDebugController.prototype = originalConstructor.prototype
134+
135+
// Replace the constructor temporarily
136+
const constructorStub = sinon
137+
.stub(require('../../../amazonq/autoDebug/autoDebugController'), 'AutoDebugController')
138+
.value(MockAutoDebugController)
153139

154140
await assert.rejects(async () => autoDebugFeature.activate(mockContext), /Test error/)
141+
142+
constructorStub.restore()
155143
})
156144
})
157145

@@ -196,47 +184,29 @@ describe('AutoDebugFeature', function () {
196184
const detectProblemsStub = sinon
197185
.stub(AutoDebugController.prototype, 'detectProblems')
198186
.resolves(mockProblems)
199-
await autoDebugFeature.activate(mockContext)
200-
201-
await autoDebugFeature.detectProblems()
202-
203-
assert.ok(detectProblemsStub.calledOnce)
204-
assert.ok(vscodeWindowStubs.showInformationMessage.calledWith('Found 2 problems in your code'))
205-
})
206-
207-
it('shows correct message for single problem', async function () {
208-
const mockProblems = createMockProblems(1)
209-
sinon.stub(AutoDebugController.prototype, 'detectProblems').resolves(mockProblems)
210-
await autoDebugFeature.activate(mockContext)
211-
212-
await autoDebugFeature.detectProblems()
213-
214-
assert.ok(vscodeWindowStubs.showInformationMessage.calledWith('Found 1 problem in your code'))
215-
})
216187

217-
it('shows no problems message', async function () {
218-
sinon.stub(AutoDebugController.prototype, 'detectProblems').resolves([])
219188
await autoDebugFeature.activate(mockContext)
220189

221190
await autoDebugFeature.detectProblems()
222191

223-
assert.ok(vscodeWindowStubs.showInformationMessage.calledWith('No new problems detected'))
192+
assert.ok(detectProblemsStub.calledOnce)
224193
})
225194

226195
it('handles controller not initialized', async function () {
227-
await autoDebugFeature.detectProblems()
196+
const result = await autoDebugFeature.detectProblems()
228197

229198
// Should not throw, should handle gracefully
230-
assert.ok(vscodeWindowStubs.showErrorMessage.notCalled)
199+
assert.strictEqual(result, undefined)
231200
})
232201

233-
it('handles detection errors', async function () {
202+
it('handles detection errors gracefully', async function () {
234203
sinon.stub(AutoDebugController.prototype, 'detectProblems').rejects(new Error('Detection failed'))
235204
await autoDebugFeature.activate(mockContext)
236205

237-
await autoDebugFeature.detectProblems()
238-
239-
assert.ok(vscodeWindowStubs.showErrorMessage.calledWith('Failed to detect problems'))
206+
// Should not throw, should handle gracefully
207+
assert.doesNotThrow(async () => {
208+
await autoDebugFeature.detectProblems()
209+
})
240210
})
241211
})
242212

@@ -267,11 +237,10 @@ describe('AutoDebugFeature', function () {
267237
await autoDebugFeature.activate(mockContext)
268238

269239
// Access private method through any cast for testing
270-
await (autoDebugFeature as any).triggerFixWithAmazonQ()
271-
272-
assert.ok(vscodeWindowStubs.showErrorMessage.calledOnce)
273-
const errorCall = vscodeWindowStubs.showErrorMessage.getCall(0)
274-
assert.ok(errorCall.args[0].includes('Failed to start Fix with Amazon Q'))
240+
// Should not throw, should handle gracefully
241+
assert.doesNotThrow(async () => {
242+
await (autoDebugFeature as any).triggerFixWithAmazonQ()
243+
})
275244
})
276245
})
277246

@@ -287,113 +256,6 @@ describe('AutoDebugFeature', function () {
287256

288257
assert.ok(setLanguageClientStub.calledOnceWith(mockClient))
289258
})
290-
291-
it('handles encryption key setting', async function () {
292-
const mockLspClient = {
293-
setEncryptionKey: sinon.stub(),
294-
}
295-
const controllerStub = sinon.stub(AutoDebugController.prototype, 'setLanguageClient')
296-
297-
// Mock accessing the private lspClient property
298-
const getControllerStub = sinon.stub()
299-
getControllerStub.returns({ lspClient: mockLspClient })
300-
301-
await autoDebugFeature.activate(mockContext)
302-
303-
const mockClient = { test: 'client' }
304-
const mockEncryptionKey = Buffer.from('test-key')
305-
306-
autoDebugFeature.setLanguageClient(mockClient, mockEncryptionKey)
307-
308-
assert.ok(controllerStub.calledOnceWith(mockClient))
309-
})
310-
})
311-
312-
describe('notification handling', function () {
313-
it('handles warning message success', async function () {
314-
const mockProblems = createMockProblems(1, 'error')
315-
const mockEventEmitter = createMockEventEmitter()
316-
317-
sinon.stub(AutoDebugController.prototype, 'onProblemsDetected').value(mockEventEmitter)
318-
vscodeWindowStubs.showWarningMessage.resolves('Fix with Amazon Q')
319-
const triggerFixStub = sinon.stub(AutoDebugController.prototype, 'fixAllProblemsInFile').resolves()
320-
focusAmazonQPanelStub.resolves()
321-
322-
await autoDebugFeature.activate(mockContext)
323-
324-
// Simulate problems detected event
325-
await mockEventEmitter.fire(mockProblems)
326-
327-
// Wait for async operations
328-
await new Promise((resolve) => setTimeout(resolve, 10))
329-
330-
assert.ok(vscodeWindowStubs.showWarningMessage.calledOnce)
331-
assert.ok(focusAmazonQPanelStub.calledOnce)
332-
assert.ok(triggerFixStub.calledOnce)
333-
})
334-
335-
it('handles warning message failure with fallback', async function () {
336-
const mockProblems = createMockProblems(1, 'error')
337-
const mockEventEmitter = createMockEventEmitter()
338-
339-
sinon.stub(AutoDebugController.prototype, 'onProblemsDetected').value(mockEventEmitter)
340-
vscodeWindowStubs.showWarningMessage.rejects(new Error('Warning failed'))
341-
vscodeWindowStubs.showInformationMessage.resolves('Fix with Amazon Q')
342-
focusAmazonQPanelStub.resolves()
343-
344-
await autoDebugFeature.activate(mockContext)
345-
346-
// Simulate problems detected event
347-
await mockEventEmitter.fire(mockProblems)
348-
349-
// Wait for async operations
350-
await new Promise((resolve) => setTimeout(resolve, 10))
351-
352-
assert.ok(vscodeWindowStubs.showWarningMessage.calledOnce)
353-
assert.ok(vscodeWindowStubs.showInformationMessage.calledOnce)
354-
})
355-
356-
it('handles all notification failures with status bar fallback', async function () {
357-
const mockProblems = createMockProblems(2, 'error')
358-
const mockEventEmitter = createMockEventEmitter()
359-
360-
sinon.stub(AutoDebugController.prototype, 'onProblemsDetected').value(mockEventEmitter)
361-
vscodeWindowStubs.showWarningMessage.rejects(new Error('Warning failed'))
362-
vscodeWindowStubs.showInformationMessage.rejects(new Error('Info failed'))
363-
vscodeWindowStubs.showErrorMessage.rejects(new Error('Error failed'))
364-
365-
await autoDebugFeature.activate(mockContext)
366-
367-
// Simulate problems detected event
368-
await mockEventEmitter.fire(mockProblems)
369-
370-
// Wait for async operations
371-
await new Promise((resolve) => setTimeout(resolve, 600))
372-
373-
assert.ok(vscodeWindowStubs.setStatusBarMessage.calledOnce)
374-
const statusCall = vscodeWindowStubs.setStatusBarMessage.getCall(0)
375-
assert.ok(statusCall.args[0].includes('Amazon Q: 2 errors detected'))
376-
})
377-
378-
it('filters non-error problems correctly', async function () {
379-
const mockWarningProblems = createMockProblems(2, 'warning')
380-
const mockEventEmitter = createMockEventEmitter()
381-
382-
sinon.stub(AutoDebugController.prototype, 'onProblemsDetected').value(mockEventEmitter)
383-
384-
await autoDebugFeature.activate(mockContext)
385-
386-
// Simulate problems detected event with warnings (should not trigger notification)
387-
await mockEventEmitter.fire(mockWarningProblems)
388-
389-
// Wait for async operations
390-
await new Promise((resolve) => setTimeout(resolve, 10))
391-
392-
// Should show debug notification instead of main notification
393-
assert.ok(vscodeWindowStubs.showWarningMessage.calledOnce)
394-
const warningCall = vscodeWindowStubs.showWarningMessage.getCall(0)
395-
assert.ok(warningCall.args[0].includes('debug mode'))
396-
})
397259
})
398260

399261
describe('command handlers', function () {
@@ -427,7 +289,6 @@ describe('AutoDebugFeature', function () {
427289
await commandHandler()
428290

429291
assert.ok(updateConfigStub.calledOnceWith({ enabled: false }))
430-
assert.ok(vscodeWindowStubs.showInformationMessage.calledWith('Amazon Q Auto Debug disabled'))
431292
})
432293

433294
it('registers and handles showStatus command', async function () {
@@ -504,20 +365,4 @@ describe('AutoDebugFeature', function () {
504365
}
505366
return problems
506367
}
507-
508-
function createMockEventEmitter(): any {
509-
const listeners: any[] = []
510-
return {
511-
event: (listener: any) => {
512-
listeners.push(listener)
513-
return { dispose: () => {} }
514-
},
515-
fire: async (data: any) => {
516-
for (const listener of listeners) {
517-
await listener(data)
518-
}
519-
},
520-
dispose: () => {},
521-
}
522-
}
523368
})

0 commit comments

Comments
 (0)