|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { performanceTest } from '../shared/performance/performance' |
| 7 | +import * as sinon from 'sinon' |
| 8 | +import * as vscode from 'vscode' |
| 9 | +import assert from 'assert' |
| 10 | +import { LspClient, LspController } from '../amazonq' |
| 11 | +import { LanguageClient, ServerOptions } from 'vscode-languageclient' |
| 12 | +import { createTestWorkspace } from '../test/testUtil' |
| 13 | +import { GetUsageRequestType, IndexRequestType } from '../amazonq/lsp/types' |
| 14 | +import { getRandomString } from '../shared' |
| 15 | + |
| 16 | +interface SetupResult { |
| 17 | + clientReqStub: sinon.SinonStub |
| 18 | +} |
| 19 | + |
| 20 | +async function verifyResult(setup: SetupResult) { |
| 21 | + assert.ok(setup.clientReqStub.calledTwice) |
| 22 | + assert.ok(setup.clientReqStub.firstCall.calledWith(IndexRequestType)) |
| 23 | + assert.ok(setup.clientReqStub.secondCall.calledWith(GetUsageRequestType)) |
| 24 | +} |
| 25 | + |
| 26 | +async function setupWithWorkspace(numFiles: number, options: { fileContent: string }): Promise<SetupResult> { |
| 27 | + // Force VSCode to find my test workspace only to keep test contained and controlled. |
| 28 | + const testWorksapce = await createTestWorkspace(numFiles, options) |
| 29 | + sinon.stub(vscode.workspace, 'workspaceFolders').value([testWorksapce]) |
| 30 | + // Avoid sending real request to lsp. |
| 31 | + const clientReqStub = sinon.stub(LanguageClient.prototype, 'sendRequest').resolves(true) |
| 32 | + LspClient.instance.client = new LanguageClient('amazonq', 'test-client', {} as ServerOptions, {}) |
| 33 | + return { clientReqStub } |
| 34 | +} |
| 35 | + |
| 36 | +describe('buildIndex', function () { |
| 37 | + describe('performanceTests', function () { |
| 38 | + afterEach(function () { |
| 39 | + sinon.restore() |
| 40 | + }) |
| 41 | + performanceTest({}, 'indexing many small files', function () { |
| 42 | + return { |
| 43 | + setup: async () => setupWithWorkspace(250, { fileContent: '0123456789' }), |
| 44 | + execute: async () => { |
| 45 | + await LspController.instance.buildIndex() |
| 46 | + }, |
| 47 | + verify: verifyResult, |
| 48 | + } |
| 49 | + }) |
| 50 | + performanceTest({}, 'indexing few large files', function () { |
| 51 | + return { |
| 52 | + setup: async () => setupWithWorkspace(10, { fileContent: getRandomString(1000) }), |
| 53 | + execute: async () => { |
| 54 | + await LspController.instance.buildIndex() |
| 55 | + }, |
| 56 | + verify: verifyResult, |
| 57 | + } |
| 58 | + }) |
| 59 | + }) |
| 60 | +}) |
0 commit comments