Skip to content

Commit 54841b8

Browse files
committed
chore: testing
1 parent 3df1791 commit 54841b8

File tree

2 files changed

+57
-40
lines changed

2 files changed

+57
-40
lines changed

packages/core/src/test/amazonqDoc/controller.test.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ for (let i = 0; i < 1000; i++) {
3737
let getSessionStub: sinon.SinonStub
3838
let modifiedReadme: string
3939
const generatedReadme = ReadmeBuilder.createBaseReadme()
40+
let sandbox: sinon.SinonSandbox
4041

4142
const getFilePaths = (controllerSetup: ControllerSetup): NewFileInfo[] => [
4243
{
@@ -50,19 +51,19 @@ for (let i = 0; i < 1000; i++) {
5051
},
5152
]
5253

53-
async function createCodeGenState() {
54-
mockGetCodeGeneration = sinon.stub().resolves({ codeGenerationStatus: { status: 'Complete' } })
54+
async function createCodeGenState(sandbox: sinon.SinonSandbox) {
55+
mockGetCodeGeneration = sandbox.stub().resolves({ codeGenerationStatus: { status: 'Complete' } })
5556

5657
const workspaceFolders = [controllerSetup.workspaceFolder] as CurrentWsFolders
5758
const testConfig = {
5859
conversationId: conversationID,
5960
proxyClient: {
60-
createConversation: () => sinon.stub(),
61-
createUploadUrl: () => sinon.stub(),
62-
generatePlan: () => sinon.stub(),
63-
startCodeGeneration: () => sinon.stub(),
61+
createConversation: () => sandbox.stub(),
62+
createUploadUrl: () => sandbox.stub(),
63+
generatePlan: () => sandbox.stub(),
64+
startCodeGeneration: () => sandbox.stub(),
6465
getCodeGeneration: () => mockGetCodeGeneration(),
65-
exportResultArchive: () => sinon.stub(),
66+
exportResultArchive: () => sandbox.stub(),
6667
} as unknown as FeatureDevClient,
6768
workspaceRoots: [''],
6869
uploadId: uploadID,
@@ -77,6 +78,7 @@ for (let i = 0; i < 1000; i++) {
7778
tabID,
7879
uploadID,
7980
scheme: docScheme,
81+
sandbox,
8082
})
8183
}
8284
async function fireFollowUps(followUpTypes: FollowUpTypes[]) {
@@ -118,27 +120,27 @@ for (let i = 0; i < 1000; i++) {
118120
await waitForStub(getSessionStub)
119121
}
120122

121-
async function setupTest() {
122-
controllerSetup = await createController()
123-
session = await createCodeGenState()
124-
sendDocTelemetrySpy = sinon.stub(session, 'sendDocTelemetryEvent').resolves()
125-
sinon.stub(session, 'preloader').resolves()
126-
sinon.stub(session, 'send').resolves()
123+
async function setupTest(sandbox: sinon.SinonSandbox) {
124+
controllerSetup = await createController(sandbox)
125+
session = await createCodeGenState(sandbox)
126+
sendDocTelemetrySpy = sandbox.stub(session, 'sendDocTelemetryEvent').resolves()
127+
sandbox.stub(session, 'preloader').resolves()
128+
sandbox.stub(session, 'send').resolves()
127129
Object.defineProperty(session, '_conversationId', {
128130
value: conversationID,
129131
writable: true,
130132
configurable: true,
131133
})
132134

133-
sinon.stub(AuthUtil.instance, 'getChatAuthState').resolves({
135+
sandbox.stub(AuthUtil.instance, 'getChatAuthState').resolves({
134136
codewhispererCore: 'connected',
135137
codewhispererChat: 'connected',
136138
amazonQ: 'connected',
137139
})
138-
sinon.stub(FileSystem.prototype, 'exists').resolves(false)
139-
getSessionStub = sinon.stub(controllerSetup.sessionStorage, 'getSession').resolves(session)
140+
sandbox.stub(FileSystem.prototype, 'exists').resolves(false)
141+
getSessionStub = sandbox.stub(controllerSetup.sessionStorage, 'getSession').resolves(session)
140142
modifiedReadme = ReadmeBuilder.createReadmeWithRepoStructure()
141-
sinon
143+
sandbox
142144
.stub(vscode.workspace, 'openTextDocument')
143145
.callsFake(async (options?: string | vscode.Uri | { language?: string; content?: string }) => {
144146
let documentPath = ''
@@ -160,30 +162,34 @@ for (let i = 0; i < 1000; i++) {
160162
maxRetries: number = 3,
161163
delayMs: number = 1000
162164
): Promise<void> => {
165+
let lastError: Error | undefined
166+
163167
for (let attempt = 1; attempt <= maxRetries + 1; attempt++) {
168+
sandbox = sinon.createSandbox()
164169
try {
170+
await setupTest(sandbox)
165171
await testMethod()
172+
sandbox.restore()
166173
return
167174
} catch (error) {
168-
if (attempt === maxRetries) {
169-
sinon.assert.fail(`Test failed after ${maxRetries} retries`)
175+
lastError = error as Error
176+
sandbox.restore()
177+
178+
if (attempt > maxRetries) {
179+
console.error(`Test failed after ${maxRetries} retries:`, lastError)
180+
throw lastError
170181
}
182+
171183
console.log(`Test attempt ${attempt} failed, retrying...`)
172184
await new Promise((resolve) => setTimeout(resolve, delayMs))
173-
await setupTest()
174185
}
175186
}
176187
}
177188

178-
before(() => {
179-
sinon.stub(performance, 'now').returns(0)
180-
})
181-
182-
beforeEach(async () => {
183-
await setupTest()
184-
})
185-
afterEach(() => {
186-
sinon.restore()
189+
after(() => {
190+
if (sandbox) {
191+
sandbox.restore()
192+
}
187193
})
188194

189195
it('should emit generation telemetry for initial README generation', async () => {
@@ -201,6 +207,7 @@ for (let i = 0; i < 1000; i++) {
201207
spy: sendDocTelemetrySpy,
202208
expectedEvent,
203209
type: 'generation',
210+
sandbox,
204211
})
205212
})
206213
})
@@ -218,6 +225,7 @@ for (let i = 0; i < 1000; i++) {
218225
spy: sendDocTelemetrySpy,
219226
expectedEvent: firstExpectedEvent,
220227
type: 'generation',
228+
sandbox,
221229
})
222230

223231
await updateFilePaths(session, modifiedReadme, uploadID, docScheme, controllerSetup.workspaceFolder)
@@ -235,6 +243,7 @@ for (let i = 0; i < 1000; i++) {
235243
expectedEvent: secondExpectedEvent,
236244
type: 'generation',
237245
callIndex: 1,
246+
sandbox,
238247
})
239248
})
240249
})
@@ -256,6 +265,7 @@ for (let i = 0; i < 1000; i++) {
256265
expectedEvent,
257266
type: 'acceptance',
258267
callIndex: 1,
268+
sandbox,
259269
})
260270
})
261271
})
@@ -274,6 +284,7 @@ for (let i = 0; i < 1000; i++) {
274284
spy: sendDocTelemetrySpy,
275285
expectedEvent,
276286
type: 'generation',
287+
sandbox,
277288
})
278289
})
279290
})
@@ -300,6 +311,7 @@ for (let i = 0; i < 1000; i++) {
300311
expectedEvent,
301312
type: 'generation',
302313
callIndex: 1,
314+
sandbox,
303315
})
304316
})
305317
})
@@ -322,6 +334,7 @@ for (let i = 0; i < 1000; i++) {
322334
expectedEvent,
323335
type: 'acceptance',
324336
callIndex: 1,
337+
sandbox,
325338
})
326339
})
327340
})
@@ -341,6 +354,7 @@ for (let i = 0; i < 1000; i++) {
341354
spy: sendDocTelemetrySpy,
342355
expectedEvent,
343356
type: 'generation',
357+
sandbox,
344358
})
345359
})
346360
})
@@ -362,6 +376,7 @@ for (let i = 0; i < 1000; i++) {
362376
expectedEvent,
363377
type: 'acceptance',
364378
callIndex: 1,
379+
sandbox,
365380
})
366381
})
367382
})

packages/core/src/test/amazonqDoc/utils.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import { DocGenerationTask } from '../../amazonqDoc/controllers/docGenerationTas
2222
import { DocV2GenerationEvent, DocV2AcceptanceEvent } from '../../amazonqFeatureDev/client/featuredevproxyclient'
2323
import { FollowUpTypes } from '../../amazonq/commons/types'
2424

25-
export function createMessenger(): DocMessenger {
25+
export function createMessenger(sandbox: sinon.SinonSandbox): DocMessenger {
2626
return new DocMessenger(
27-
new AppToWebViewMessageDispatcher(new MessagePublisher(sinon.createStubInstance(vscode.EventEmitter))),
27+
new AppToWebViewMessageDispatcher(new MessagePublisher(sandbox.createStubInstance(vscode.EventEmitter))),
2828
docChat
2929
)
3030
}
@@ -61,26 +61,27 @@ export async function createSession({
6161
conversationID = '0',
6262
tabID = '0',
6363
uploadID = '0',
64+
sandbox,
6465
}: {
6566
messenger: DocMessenger
6667
scheme: string
6768
sessionState?: Omit<SessionState, 'uploadId'>
6869
conversationID?: string
6970
tabID?: string
7071
uploadID?: string
72+
sandbox: sinon.SinonSandbox
7173
}) {
7274
const sessionConfig = await createSessionConfig(scheme)
7375

74-
const client = sinon.createStubInstance(FeatureDevClient)
76+
const client = sandbox.createStubInstance(FeatureDevClient)
7577
client.createConversation.resolves(conversationID)
7678
const session = new Session(sessionConfig, messenger, tabID, sessionState, client)
7779

78-
sinon.stub(session, 'conversationId').get(() => conversationID)
79-
sinon.stub(session, 'uploadId').get(() => uploadID)
80+
sandbox.stub(session, 'conversationId').get(() => conversationID)
81+
sandbox.stub(session, 'uploadId').get(() => uploadID)
8082

8183
return session
8284
}
83-
8485
export async function sessionRegisterProvider(session: Session, uri: vscode.Uri, fileContents: Uint8Array) {
8586
session.config.fs.registerProvider(uri, new VirtualMemoryFile(fileContents))
8687
}
@@ -98,12 +99,12 @@ export async function sessionWriteFile(session: Session, uri: vscode.Uri, encode
9899
})
99100
}
100101

101-
export async function createController(): Promise<ControllerSetup> {
102-
const messenger = createMessenger()
102+
export async function createController(sandbox: sinon.SinonSandbox): Promise<ControllerSetup> {
103+
const messenger = createMessenger(sandbox)
103104

104105
// Create a new workspace root
105106
const testWorkspaceFolder = await createTestWorkspaceFolder()
106-
sinon.stub(vscode.workspace, 'workspaceFolders').value([testWorkspaceFolder])
107+
sandbox.stub(vscode.workspace, 'workspaceFolders').value([testWorkspaceFolder])
107108

108109
const sessionStorage = new DocChatSessionStorage(messenger)
109110

@@ -113,7 +114,7 @@ export async function createController(): Promise<ControllerSetup> {
113114
mockChatControllerEventEmitters,
114115
messenger,
115116
sessionStorage,
116-
sinon.createStubInstance(vscode.EventEmitter).event
117+
sandbox.createStubInstance(vscode.EventEmitter).event
117118
)
118119

119120
new DocGenerationTask()
@@ -199,10 +200,11 @@ export async function assertTelemetry(params: {
199200
expectedEvent: DocV2GenerationEvent | DocV2AcceptanceEvent
200201
type: 'generation' | 'acceptance'
201202
callIndex?: number
203+
sandbox: sinon.SinonSandbox
202204
}) {
203205
await new Promise((resolve) => setTimeout(resolve, 100))
204206
const spyCall = params.callIndex !== undefined ? params.spy.getCall(params.callIndex) : params.spy
205-
sinon.assert.calledWith(spyCall, sinon.match(params.expectedEvent), params.type)
207+
params.sandbox.assert.calledWith(spyCall, params.sandbox.match(params.expectedEvent), params.type)
206208
}
207209

208210
export async function updateFilePaths(

0 commit comments

Comments
 (0)