@@ -8,9 +8,9 @@ import vscode from 'vscode'
88import sinon from 'sinon'
99import { join } from 'path'
1010import { getTestWorkspaceFolder } from 'aws-core-vscode/test'
11- import { CodeAnalysisScope , ZipUtil } from 'aws-core-vscode/codewhisperer'
11+ import { CodeAnalysisScope , CodeWhispererConstants , ZipUtil } from 'aws-core-vscode/codewhisperer'
1212import { codeScanTruncDirPrefix } from 'aws-core-vscode/codewhisperer'
13- import { ToolkitError } from 'aws-core-vscode/shared'
13+ import { tempDirPath , ToolkitError } from 'aws-core-vscode/shared'
1414import { LspClient } from 'aws-core-vscode/amazonq'
1515import { fs } from 'aws-core-vscode/shared'
1616import path from 'path'
@@ -142,97 +142,58 @@ describe('zipUtil', function () {
142142
143143 describe ( 'generateZipTestGen' , function ( ) {
144144 let zipUtil : ZipUtil
145- let mockFs : sinon . SinonStubbedInstance < typeof fs >
146- const projectPath = '/test/project'
147- const zipDirPath = '/test/zip'
148- const zipFilePath = '/test/zip/test.zip'
145+ let getZipDirPathStub : sinon . SinonStub
146+ let testTempDirPath : string
149147
150148 beforeEach ( function ( ) {
151149 zipUtil = new ZipUtil ( )
152- mockFs = sinon . stub ( fs )
153-
154- const mockRepoMapPath = '/path/to/repoMapData.json'
155- mockFs . exists . withArgs ( mockRepoMapPath ) . resolves ( true )
156- sinon . stub ( LspClient , 'instance' ) . get ( ( ) => ( {
157- getRepoMapJSON : sinon . stub ( ) . resolves ( mockRepoMapPath ) ,
158- } ) )
159-
160- sinon . stub ( zipUtil , 'getZipDirPath' ) . returns ( zipDirPath )
161- sinon . stub ( zipUtil as any , 'zipProject' ) . resolves ( zipFilePath )
150+ testTempDirPath = path . join ( tempDirPath , CodeWhispererConstants . TestGenerationTruncDirPrefix )
151+ getZipDirPathStub = sinon . stub ( zipUtil , 'getZipDirPath' )
152+ getZipDirPathStub . callsFake ( ( ) => testTempDirPath )
162153 } )
163154
164155 afterEach ( function ( ) {
165156 sinon . restore ( )
166157 } )
167158
168- it ( 'Should generate zip for test generation successfully' , async function ( ) {
169- mockFs . stat . resolves ( {
170- type : vscode . FileType . File ,
171- size : 1000 ,
172- ctime : Date . now ( ) ,
173- mtime : Date . now ( ) ,
174- } as vscode . FileStat )
175-
176- mockFs . readFileBytes . resolves ( Buffer . from ( 'test content' ) )
159+ it ( 'should generate zip for test generation successfully' , async function ( ) {
160+ const mkdirSpy = sinon . spy ( fs , 'mkdir' )
177161
178- // Fix: Create a Set from the array
179- zipUtil [ '_totalSize' ] = 500
180- zipUtil [ '_totalBuildSize' ] = 200
181- zipUtil [ '_totalLines' ] = 100
182- zipUtil [ '_language' ] = 'typescript'
183- zipUtil [ '_pickedSourceFiles' ] = new Set ( [ 'file1.ts' , 'file2.ts' ] )
162+ const result = await zipUtil . generateZipTestGen ( appRoot , false )
184163
185- const result = await zipUtil . generateZipTestGen ( projectPath , false )
186-
187- assert . ok ( mockFs . mkdir . calledWith ( path . join ( zipDirPath , 'utgRequiredArtifactsDir' ) ) )
164+ assert . ok ( mkdirSpy . calledWith ( path . join ( testTempDirPath , 'utgRequiredArtifactsDir' ) ) )
188165 assert . ok (
189- mockFs . mkdir . calledWith ( path . join ( zipDirPath , 'utgRequiredArtifactsDir' , 'buildAndExecuteLogDir' ) )
166+ mkdirSpy . calledWith ( path . join ( testTempDirPath , 'utgRequiredArtifactsDir' , 'buildAndExecuteLogDir' ) )
190167 )
191- assert . ok ( mockFs . mkdir . calledWith ( path . join ( zipDirPath , 'utgRequiredArtifactsDir' , 'repoMapData' ) ) )
192- assert . ok ( mockFs . mkdir . calledWith ( path . join ( zipDirPath , 'utgRequiredArtifactsDir' , 'testCoverageDir' ) ) )
193-
194- // assert.ok(
195- // mockFs.copy.calledWith(
196- // '/path/to/repoMapData.json',
197- // path.join(zipDirPath, 'utgRequiredArtifactsDir', 'repoMapData', 'repoMapData.json')
198- // )
199- // )
200-
201- assert . strictEqual ( result . rootDir , zipDirPath )
202- assert . strictEqual ( result . zipFilePath , zipFilePath )
203- assert . strictEqual ( result . srcPayloadSizeInBytes , 500 )
204- assert . strictEqual ( result . buildPayloadSizeInBytes , 200 )
205- assert . strictEqual ( result . zipFileSizeInBytes , 1000 )
206- assert . strictEqual ( result . lines , 100 )
207- assert . strictEqual ( result . language , 'typescript' )
208- assert . deepStrictEqual ( Array . from ( result . scannedFiles ) , [ 'file1.ts' , 'file2.ts' ] )
209- } )
210-
211- // it('Should handle LSP client error', async function () {
212- // // Override the default stub with one that rejects
213- // sinon.stub(LspClient, 'instance').get(() => ({
214- // getRepoMapJSON: sinon.stub().rejects(new Error('LSP error')),
215- // }))
168+ assert . ok ( mkdirSpy . calledWith ( path . join ( testTempDirPath , 'utgRequiredArtifactsDir' , 'repoMapData' ) ) )
169+ assert . ok ( mkdirSpy . calledWith ( path . join ( testTempDirPath , 'utgRequiredArtifactsDir' , 'testCoverageDir' ) ) )
216170
217- // await assert.rejects(() => zipUtil.generateZipTestGen(projectPath), /LSP error/)
218- // })
171+ assert . strictEqual ( result . rootDir , testTempDirPath )
172+ assert . strictEqual ( result . zipFilePath , testTempDirPath + CodeWhispererConstants . codeScanZipExt )
173+ assert . ok ( result . srcPayloadSizeInBytes > 0 )
174+ assert . strictEqual ( result . buildPayloadSizeInBytes , 0 )
175+ assert . ok ( result . zipFileSizeInBytes > 0 )
176+ assert . strictEqual ( result . lines , 150 )
177+ assert . strictEqual ( result . language , 'java' )
178+ assert . strictEqual ( result . scannedFiles . size , 4 )
179+ } )
219180
220181 it ( 'Should handle file system errors during directory creation' , async function ( ) {
221182 sinon . stub ( LspClient , 'instance' ) . get ( ( ) => ( {
222183 getRepoMapJSON : sinon . stub ( ) . resolves ( '{"mock": "data"}' ) ,
223184 } ) )
224- mockFs . mkdir . rejects ( new Error ( 'Directory creation failed' ) )
185+ sinon . stub ( fs , ' mkdir' ) . rejects ( new Error ( 'Directory creation failed' ) )
225186
226- await assert . rejects ( ( ) => zipUtil . generateZipTestGen ( projectPath , false ) , / D i r e c t o r y c r e a t i o n f a i l e d / )
187+ await assert . rejects ( ( ) => zipUtil . generateZipTestGen ( appRoot , false ) , / D i r e c t o r y c r e a t i o n f a i l e d / )
227188 } )
228189
229190 it ( 'Should handle zip project errors' , async function ( ) {
230191 sinon . stub ( LspClient , 'instance' ) . get ( ( ) => ( {
231192 getRepoMapJSON : sinon . stub ( ) . resolves ( '{"mock": "data"}' ) ,
232193 } ) )
233- ; ( zipUtil as any ) . zipProject . rejects ( new Error ( 'Zip failed' ) )
194+ sinon . stub ( zipUtil , 'zipProject' as keyof ZipUtil ) . rejects ( new Error ( 'Zip failed' ) )
234195
235- await assert . rejects ( ( ) => zipUtil . generateZipTestGen ( projectPath , false ) , / Z i p f a i l e d / )
196+ await assert . rejects ( ( ) => zipUtil . generateZipTestGen ( appRoot , false ) , / Z i p f a i l e d / )
236197 } )
237198
238199 it ( 'Should handle file copy to downloads folder error' , async function ( ) {
@@ -241,31 +202,15 @@ describe('zipUtil', function () {
241202 getRepoMapJSON : sinon . stub ( ) . resolves ( '{"mock": "data"}' ) ,
242203 } ) )
243204
244- // Mock file operations
245- const mockFs = {
246- mkdir : sinon . stub ( ) . resolves ( ) ,
247- copy : sinon . stub ( ) . rejects ( new Error ( 'Copy failed' ) ) ,
248- exists : sinon . stub ( ) . resolves ( true ) ,
249- stat : sinon . stub ( ) . resolves ( {
250- type : vscode . FileType . File ,
251- size : 1000 ,
252- ctime : Date . now ( ) ,
253- mtime : Date . now ( ) ,
254- } as vscode . FileStat ) ,
255- }
256-
257- // Since the function now uses Promise.all for directory creation and file operations,
258- // we need to ensure the mkdir succeeds but the copy fails
259- fs . mkdir = mockFs . mkdir
260- fs . copy = mockFs . copy
261- fs . exists = mockFs . exists
262- fs . stat = mockFs . stat
263-
264- await assert . rejects ( ( ) => zipUtil . generateZipTestGen ( projectPath , false ) , / C o p y f a i l e d / )
205+ const mkdirSpy = sinon . spy ( fs , 'mkdir' )
206+ sinon . stub ( fs , 'exists' ) . resolves ( true )
207+ sinon . stub ( fs , 'copy' ) . rejects ( new Error ( 'Copy failed' ) )
208+
209+ await assert . rejects ( ( ) => zipUtil . generateZipTestGen ( appRoot , false ) , / C o p y f a i l e d / )
265210
266211 // Verify mkdir was called for all directories
267- assert ( mockFs . mkdir . called , 'mkdir should have been called' )
268- assert . strictEqual ( mockFs . mkdir . callCount , 4 , 'mkdir should have been called 4 times' )
212+ assert ( mkdirSpy . called , 'mkdir should have been called' )
213+ assert . strictEqual ( mkdirSpy . callCount , 4 , 'mkdir should have been called 4 times' )
269214 } )
270215 } )
271216} )
0 commit comments