|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import sinon from 'sinon' |
| 7 | +import { CodeGenBase } from '../../../amazonq/session/sessionState' |
| 8 | +import { RunCommandLogFileName } from '../../../amazonq/session/sessionState' |
| 9 | +import assert from 'assert' |
| 10 | + |
| 11 | +describe('CodeGenBase generateCode log file handling', () => { |
| 12 | + class TestCodeGen extends CodeGenBase { |
| 13 | + public generatedFiles: any[] = [] |
| 14 | + constructor(config: any, tabID: string) { |
| 15 | + super(config, tabID) |
| 16 | + } |
| 17 | + protected handleProgress(_messenger: any): void { |
| 18 | + // No-op for test. |
| 19 | + } |
| 20 | + protected getScheme(): string { |
| 21 | + return 'file' |
| 22 | + } |
| 23 | + protected getTimeoutErrorCode(): string { |
| 24 | + return 'test_timeout' |
| 25 | + } |
| 26 | + protected handleGenerationComplete(_messenger: any, newFileInfo: any[]): void { |
| 27 | + this.generatedFiles = newFileInfo |
| 28 | + } |
| 29 | + protected handleError(_messenger: any, _codegenResult: any): Error { |
| 30 | + throw new Error('handleError called') |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + let fakeProxyClient: any |
| 35 | + let testConfig: any |
| 36 | + let fsMock: any |
| 37 | + let messengerMock: any |
| 38 | + let telemetryMock: any |
| 39 | + let loggerMock: any |
| 40 | + let testAction: any |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + fakeProxyClient = { |
| 44 | + getCodeGeneration: sinon.stub().resolves({ |
| 45 | + codeGenerationStatus: { status: 'Complete' }, |
| 46 | + codeGenerationRemainingIterationCount: 0, |
| 47 | + codeGenerationTotalIterationCount: 1, |
| 48 | + }), |
| 49 | + exportResultArchive: sinon.stub(), |
| 50 | + } |
| 51 | + |
| 52 | + testConfig = { |
| 53 | + conversationId: 'conv_test', |
| 54 | + uploadId: 'upload_test', |
| 55 | + workspaceRoots: ['/workspace'], |
| 56 | + proxyClient: fakeProxyClient, |
| 57 | + } |
| 58 | + |
| 59 | + fsMock = { |
| 60 | + writeFile: sinon.stub().resolves(), |
| 61 | + registerProvider: sinon.stub().resolves(), |
| 62 | + } |
| 63 | + |
| 64 | + messengerMock = { sendAnswer: sinon.spy() } |
| 65 | + |
| 66 | + telemetryMock = { |
| 67 | + setCodeGenerationResult: sinon.spy(), |
| 68 | + setNumberOfFilesGenerated: sinon.spy(), |
| 69 | + setAmazonqNumberOfReferences: sinon.spy(), |
| 70 | + setGenerateCodeIteration: sinon.spy(), |
| 71 | + setGenerateCodeLastInvocationTime: sinon.spy(), |
| 72 | + recordUserCodeGenerationTelemetry: sinon.spy(), |
| 73 | + } |
| 74 | + |
| 75 | + telemetryMock.toolkit_trackScenario = { |
| 76 | + emit: sinon.stub(), |
| 77 | + run: sinon.stub(), |
| 78 | + } |
| 79 | + |
| 80 | + loggerMock = { |
| 81 | + info: sinon.spy(), |
| 82 | + } |
| 83 | + |
| 84 | + testAction = { |
| 85 | + fs: fsMock, |
| 86 | + messenger: messengerMock, |
| 87 | + logger: loggerMock, |
| 88 | + tokenSource: { token: { isCancellationRequested: false, onCancellationRequested: () => {} } }, |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + afterEach(() => { |
| 93 | + sinon.restore() |
| 94 | + }) |
| 95 | + |
| 96 | + it('adds the log content to logger if present and excludes it from new files', async () => { |
| 97 | + const logFileInfo = { |
| 98 | + zipFilePath: RunCommandLogFileName, |
| 99 | + fileContent: 'newLog', |
| 100 | + } |
| 101 | + const otherFile = { zipFilePath: 'other.ts', fileContent: 'other content' } |
| 102 | + fakeProxyClient.exportResultArchive.resolves({ |
| 103 | + newFileContents: [logFileInfo, otherFile], |
| 104 | + deletedFiles: [], |
| 105 | + references: [], |
| 106 | + }) |
| 107 | + |
| 108 | + const testCodeGen = new TestCodeGen(testConfig, 'tab1') |
| 109 | + |
| 110 | + const result = await testCodeGen.generateCode({ |
| 111 | + messenger: messengerMock, |
| 112 | + fs: fsMock, |
| 113 | + codeGenerationId: 'codegen1', |
| 114 | + telemetry: telemetryMock, |
| 115 | + workspaceFolders: [] as any, |
| 116 | + action: testAction, |
| 117 | + }) |
| 118 | + |
| 119 | + sinon.assert.calledWith(loggerMock.info, `Run Command logs: \n ${logFileInfo.fileContent}`) |
| 120 | + |
| 121 | + sinon.assert.calledWith(fsMock, [otherFile], testConfig.uploadId, [] as any, testConfig.conversationId, 'file') |
| 122 | + |
| 123 | + assert.deepStrictEqual(result.newFiles, [otherFile]) |
| 124 | + }) |
| 125 | + |
| 126 | + it('skips log file handling if log file is not present', async () => { |
| 127 | + const file1 = { zipFilePath: 'file1.ts', fileContent: 'content1' } |
| 128 | + fakeProxyClient.exportResultArchive.resolves({ |
| 129 | + newFileContents: [file1], |
| 130 | + deletedFiles: [], |
| 131 | + references: [], |
| 132 | + }) |
| 133 | + |
| 134 | + const testCodeGen = new TestCodeGen(testConfig, 'tab1') |
| 135 | + |
| 136 | + const result = await testCodeGen.generateCode({ |
| 137 | + messenger: messengerMock, |
| 138 | + fs: fsMock, |
| 139 | + codeGenerationId: 'codegen2', |
| 140 | + telemetry: telemetryMock, |
| 141 | + workspaceFolders: [] as any, |
| 142 | + action: testAction, |
| 143 | + }) |
| 144 | + |
| 145 | + sinon.assert.notCalled(loggerMock.info) |
| 146 | + sinon.assert.calledWith(fsMock, [file1], testConfig.uploadId, [] as any, testConfig.conversationId, 'file') |
| 147 | + assert.deepStrictEqual(result.newFiles, [file1]) |
| 148 | + }) |
| 149 | +}) |
0 commit comments