@@ -8,8 +8,9 @@ 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+ import * as filesModule from '../../../amazonq/util/files'
13+ import { CodeGenBase as CodeGenBaseClass } from '../../../amazonq/session/sessionState'
1314
1415describe ( 'sessionStateDoc' , ( ) => {
1516 const context = createTestContext ( )
@@ -26,4 +27,111 @@ describe('sessionStateDoc', () => {
2627 } )
2728 } )
2829 } )
30+
31+ describe ( 'CodeGenBase generateCode log file handling' , ( ) => {
32+ const RunCommandLogFileName = '.amazonq/dev/run_command.log'
33+
34+ class TestCodeGen extends CodeGenBaseClass {
35+ public generatedFiles : any [ ] = [ ]
36+ constructor ( config : any , tabID : string ) {
37+ super ( config , tabID )
38+ }
39+ protected handleProgress ( _messenger : any ) : void { }
40+ protected getScheme ( ) : string {
41+ return 'file'
42+ }
43+ protected getTimeoutErrorCode ( ) : string {
44+ return 'test_timeout'
45+ }
46+ protected handleGenerationComplete ( _messenger : any , newFileInfo : any [ ] ) : void {
47+ this . generatedFiles = newFileInfo
48+ }
49+ protected handleError ( _messenger : any , _codegenResult : any ) : Error {
50+ throw new Error ( 'handleError called' )
51+ }
52+ }
53+
54+ let testConfig : any , fakeProxyClient : any , fsMock : any , messengerMock : any , testAction : any , telemetryMock : any
55+
56+ beforeEach ( ( ) => {
57+ fakeProxyClient = {
58+ getCodeGeneration : sinon . stub ( ) . resolves ( {
59+ codeGenerationStatus : { status : 'Complete' } ,
60+ codeGenerationRemainingIterationCount : 0 ,
61+ codeGenerationTotalIterationCount : 1 ,
62+ } ) ,
63+ exportResultArchive : sinon . stub ( ) ,
64+ }
65+
66+ testConfig = {
67+ conversationId : 'conv1' ,
68+ uploadId : 'upload1' ,
69+ workspaceRoots : [ '/workspace' ] ,
70+ proxyClient : fakeProxyClient ,
71+ }
72+
73+ fsMock = {
74+ writeFile : sinon . stub ( ) . resolves ( ) ,
75+ }
76+
77+ messengerMock = { sendAnswer : sinon . spy ( ) }
78+
79+ telemetryMock = {
80+ setCodeGenerationResult : sinon . spy ( ) ,
81+ setNumberOfFilesGenerated : sinon . spy ( ) ,
82+ setAmazonqNumberOfReferences : sinon . spy ( ) ,
83+ setGenerateCodeIteration : sinon . spy ( ) ,
84+ setGenerateCodeLastInvocationTime : sinon . spy ( ) ,
85+ recordUserCodeGenerationTelemetry : sinon . spy ( ) ,
86+ }
87+
88+ testAction = {
89+ fs : fsMock ,
90+ messenger : messengerMock ,
91+ tokenSource : { token : { isCancellationRequested : false , onCancellationRequested : ( ) => { } } } ,
92+ }
93+ } )
94+
95+ afterEach ( ( ) => {
96+ sinon . restore ( )
97+ } )
98+
99+ it ( 'writes to the log file, present or not' , async ( ) => {
100+ const logFileInfo = {
101+ zipFilePath : RunCommandLogFileName ,
102+ fileContent : 'newLog' ,
103+ }
104+ const otherFile = { zipFilePath : 'other.ts' , fileContent : 'other' }
105+
106+ fakeProxyClient . exportResultArchive . resolves ( {
107+ newFileContents : [ logFileInfo , otherFile ] ,
108+ deletedFiles : [ ] ,
109+ references : [ ] ,
110+ } )
111+
112+ // Minimal stubs to simulate file processing behavior.
113+ sinon . stub ( filesModule , 'registerNewFiles' ) . callsFake ( ( _ , newFileContents : any [ ] ) => newFileContents )
114+ sinon . stub ( filesModule , 'getDeletedFileInfos' ) . callsFake ( ( ) => [ ] )
115+
116+ const testCodeGen = new TestCodeGen ( testConfig , 'tab1' )
117+
118+ await testCodeGen . generateCode ( {
119+ messenger : messengerMock ,
120+ fs : fsMock ,
121+ codeGenerationId : 'codegen2' ,
122+ telemetry : telemetryMock ,
123+ workspaceFolders : { } as any ,
124+ action : testAction ,
125+ } )
126+
127+ const expectedFilePath = `${ testConfig . workspaceRoots [ 0 ] } /${ RunCommandLogFileName } `
128+ const fileUri = vscode . Uri . file ( expectedFilePath )
129+ sinon . assert . calledWith ( fsMock . writeFile , fileUri , new TextEncoder ( ) . encode ( 'newLog' ) , {
130+ create : true ,
131+ overwrite : true ,
132+ } )
133+
134+ assert . deepStrictEqual ( testCodeGen . generatedFiles , [ otherFile ] )
135+ } )
136+ } )
29137} )
0 commit comments