@@ -8,8 +8,8 @@ import assert from 'assert'
88import sinon from 'sinon'
99import { DocPrepareCodeGenState } from '../../../amazonqDoc'
1010import { createMockSessionStateAction } from '../../amazonq/utils'
11-
1211import { createTestContext , setupTestHooks } from '../../amazonq/session/testSetup'
12+ const filesModule = require ( '../../../amazonq/util/files' )
1313
1414describe ( 'sessionStateDoc' , ( ) => {
1515 const context = createTestContext ( )
@@ -26,4 +26,133 @@ describe('sessionStateDoc', () => {
2626 } )
2727 } )
2828 } )
29+
30+ describe ( 'CodeGenBase generateCode log file handling' , ( ) => {
31+ const RunCommandLogFileName = '.amazonq/dev/run_command.log'
32+ const registerNewFilesStub = sinon . stub ( )
33+ registerNewFilesStub . callsFake ( ( fs : any , newFileContents : any [ ] ) => {
34+ return newFileContents
35+ } )
36+ const getDeletedFileInfosStub = sinon . stub ( )
37+ getDeletedFileInfosStub . callsFake ( ( fs : any , deletedFiles : any [ ] ) => {
38+ return [ ]
39+ } )
40+
41+ class TestCodeGen extends ( require ( '../../../amazonq/session/sessionState' ) as any ) . CodeGenBase {
42+ public generatedFiles : any [ ] = [ ]
43+ constructor ( config : any , tabID : string ) {
44+ super ( config , tabID )
45+ }
46+ protected handleProgress ( messenger : any , action : any , detail ?: string ) : void {
47+ // no-op
48+ }
49+ protected getScheme ( ) : string {
50+ return 'file'
51+ }
52+ protected getTimeoutErrorCode ( ) : string {
53+ return 'test_timeout'
54+ }
55+ protected handleGenerationComplete ( messenger : any , newFileInfo : any [ ] , action : any ) : void {
56+ this . generatedFiles = newFileInfo
57+ }
58+ protected handleError ( messenger : any , codegenResult : any ) : Error {
59+ throw new Error ( 'handleError called' )
60+ }
61+ }
62+
63+ let testConfig : any
64+ let fakeProxyClient : any
65+ let fsMock : any
66+ let telemetryMock : any
67+ let messengerMock : any
68+ let testAction : any
69+
70+ beforeEach ( ( ) => {
71+ fakeProxyClient = {
72+ getCodeGeneration : sinon . stub ( ) . resolves ( {
73+ codeGenerationStatus : { status : 'Complete' } ,
74+ codeGenerationRemainingIterationCount : 0 ,
75+ codeGenerationTotalIterationCount : 1 ,
76+ } ) ,
77+ exportResultArchive : sinon . stub ( ) ,
78+ }
79+
80+ testConfig = {
81+ conversationId : 'conv1' ,
82+ uploadId : 'upload1' ,
83+ workspaceRoots : [ '/workspace' ] ,
84+ proxyClient : fakeProxyClient ,
85+ }
86+
87+ fsMock = {
88+ stat : sinon . stub ( ) ,
89+ readFile : sinon . stub ( ) ,
90+ writeFile : sinon . stub ( ) ,
91+ }
92+
93+ telemetryMock = {
94+ setCodeGenerationResult : sinon . spy ( ) ,
95+ setNumberOfFilesGenerated : sinon . spy ( ) ,
96+ setAmazonqNumberOfReferences : sinon . spy ( ) ,
97+ setGenerateCodeIteration : sinon . spy ( ) ,
98+ setGenerateCodeLastInvocationTime : sinon . spy ( ) ,
99+ recordUserCodeGenerationTelemetry : sinon . spy ( ) ,
100+ }
101+
102+ messengerMock = {
103+ sendAnswer : sinon . spy ( ) ,
104+ }
105+
106+ testAction = {
107+ telemetry : telemetryMock ,
108+ fs : fsMock ,
109+ messenger : messengerMock ,
110+ uploadHistory : { } ,
111+ tokenSource : { token : { isCancellationRequested : false , onCancellationRequested : ( ) => { } } } ,
112+ }
113+ } )
114+
115+ afterEach ( ( ) => {
116+ sinon . restore ( )
117+ } )
118+
119+ it ( 'writes to the log file, present or not' , async ( ) => {
120+ const logFileInfo = {
121+ zipFilePath : RunCommandLogFileName ,
122+ fileContent : 'newLog' ,
123+ }
124+ const otherFile = { zipFilePath : 'other.ts' , fileContent : 'other' }
125+
126+ fakeProxyClient . exportResultArchive . resolves ( {
127+ newFileContents : [ logFileInfo , otherFile ] ,
128+ deletedFiles : [ ] ,
129+ references : [ ] ,
130+ } )
131+
132+ fsMock . writeFile . resolves ( )
133+
134+ sinon . stub ( filesModule , 'registerNewFiles' ) . callsFake ( registerNewFilesStub )
135+ sinon . stub ( filesModule , 'getDeletedFileInfos' ) . callsFake ( getDeletedFileInfosStub )
136+
137+ const testCodeGen = new TestCodeGen ( testConfig , 'tab1' )
138+
139+ await testCodeGen . generateCode ( {
140+ messenger : messengerMock ,
141+ fs : fsMock ,
142+ codeGenerationId : 'codegen2' ,
143+ telemetry : telemetryMock ,
144+ workspaceFolders : { } ,
145+ action : testAction ,
146+ } )
147+
148+ const expectedFilePath = `${ testConfig . workspaceRoots [ 0 ] } /${ RunCommandLogFileName } `
149+ const fileUri = vscode . Uri . file ( expectedFilePath )
150+ sinon . assert . calledWith ( fsMock . writeFile , fileUri , new TextEncoder ( ) . encode ( 'newLog' ) , {
151+ create : true ,
152+ overwrite : true ,
153+ } )
154+
155+ assert . deepStrictEqual ( testCodeGen . generatedFiles , [ otherFile ] )
156+ } )
157+ } )
29158} )
0 commit comments