@@ -11,146 +11,89 @@ import * as vscode from 'vscode'
1111import assert from 'assert'
1212import * as sinon from 'sinon'
1313import * as path from 'path'
14+ import { MetadataManager } from '../../../../awsService/appBuilder/serverlessLand/metadataManager'
1415import * as main from '../../../../awsService/appBuilder/serverlessLand/main'
1516import {
1617 createNewServerlessLandProject ,
17- launchProjectCreationWizard ,
1818 openReadmeFile ,
1919 getProjectUri ,
2020 downloadPatternCode ,
2121} from '../../../../awsService/appBuilder/serverlessLand/main'
22- import * as workspaceUtils from '../../../../shared/utilities/workspaceUtils'
2322import { fs } from '../../../../shared/fs/fs'
2423import * as downloadPatterns from '../../../../shared/utilities/downloadPatterns'
25- import { assertTelemetryCurried } from '../../../testUtil'
26- import { CreateServerlessLandWizard } from '../../../../awsService/appBuilder/serverlessLand/wizard'
27- import { MetadataManager } from '../../../../awsService/appBuilder/serverlessLand/metadataManager'
2824import { ExtContext } from '../../../../shared/extensions'
25+ import { workspaceUtils } from '../../../../shared'
26+ import * as downloadPattern from '../../../../shared/utilities/downloadPatterns'
27+ import * as wizardModule from '../../../../awsService/appBuilder/serverlessLand/wizard'
2928
3029describe ( 'createNewServerlessLandProject' , ( ) => {
3130 let sandbox : sinon . SinonSandbox
32- const mockExtContext = {
33- awsContext : {
34- getCredentials : ( ) => Promise . resolve ( { } ) ,
35- getCredentialDefaultRegion : ( ) => 'us-west-2' ,
36- } ,
37- } as unknown as ExtContext
38- const assertTelemetry = assertTelemetryCurried ( 'serverlessland_createProject' )
39- const mockWizardResponse = {
40- name : 'test-project' ,
41- location : vscode . Uri . file ( '/test' ) ,
42- pattern : 'test-pattern' ,
43- runtime : 'nodejs' ,
44- iac : 'sam' ,
45- assetName : 'test-asset' ,
46- }
31+ let mockExtContext : ExtContext
32+ let mockMetadataManager : sinon . SinonStubbedInstance < MetadataManager >
33+ let mockWizard : { run : sinon . SinonStub }
4734
4835 beforeEach ( ( ) => {
4936 sandbox = sinon . createSandbox ( )
50- } )
51-
52- afterEach ( ( ) => {
53- sandbox . restore ( )
54- } )
55-
56- it ( 'successfully creates a new serverless land project' , async ( ) => {
57- const wizardStub = sandbox . stub ( main , 'launchProjectCreationWizard' ) . resolves ( mockWizardResponse )
58- const metadataStub = sandbox
59- . stub ( MetadataManager . prototype , 'getAssetName' )
60- . withArgs ( 'test-pattern' , 'node.js' , 'sam' )
61- . returns ( 'test-asset' )
62- const downloadStub = sandbox . stub ( main , 'downloadPatternCode' ) . resolves ( )
63- const readmeStub = sandbox . stub ( main , 'openReadmeFile' ) . resolves ( )
64- const addFolderStub = sandbox . stub ( workspaceUtils , 'addFolderToWorkspace' ) . resolves ( )
65-
66- await main . createNewServerlessLandProject ( mockExtContext )
67-
68- sandbox . assert . calledOnce ( wizardStub )
69- sandbox . assert . calledOnce ( metadataStub )
70- sandbox . assert . calledWith ( downloadStub , mockWizardResponse , 'test-asset' )
71- sandbox . assert . calledOnce ( readmeStub )
72- sandbox . assert . calledWith ( readmeStub , mockWizardResponse )
73- sandbox . assert . calledOnce ( addFolderStub )
74- assertTelemetry ( {
75- result : 'Succeeded' ,
76- } )
77- } )
37+ mockExtContext = {
38+ awsContext : {
39+ getCredentials : sandbox . stub ( ) . resolves ( { } ) ,
40+ getCredentialDefaultRegion : ( ) => 'us-west-2' ,
41+ } ,
42+ } as unknown as ExtContext
7843
79- it ( 'handles wizard cancellation' , async ( ) => {
80- sandbox . stub ( main , 'launchProjectCreationWizard' ) . resolves ( undefined )
81- await createNewServerlessLandProject ( mockExtContext )
82- assertTelemetry ( {
83- result : 'Failed' ,
84- reason : 'Error' ,
85- } )
86- } )
44+ mockMetadataManager = sandbox . createStubInstance ( MetadataManager )
45+ mockMetadataManager . getAssetName . returns ( 'test-asset-name' )
46+ sandbox . stub ( MetadataManager , 'getInstance' ) . returns ( mockMetadataManager as unknown as MetadataManager )
8747
88- it ( 'handles errors during project creation' , async ( ) => {
89- sandbox . stub ( main , 'launchProjectCreationWizard' ) . resolves ( mockWizardResponse )
90- sandbox . stub ( main , 'downloadPatternCode' ) . rejects ( new Error ( 'Download failed' ) )
91- await createNewServerlessLandProject ( mockExtContext )
92- assertTelemetry ( {
93- result : 'Failed' ,
94- reason : 'Error' ,
95- } )
96- } )
97- } )
48+ mockWizard = { run : sandbox . stub ( ) }
49+ sandbox . stub ( wizardModule , 'CreateServerlessLandWizard' ) . returns ( mockWizard )
9850
99- describe ( 'launchProjectCreationWizard' , ( ) => {
100- let sandbox : sinon . SinonSandbox
101- const mockWizardResponse = {
102- name : 'test-project' ,
103- location : 'test-location' ,
104- pattern : 'test-pattern' ,
105- runtime : 'nodejs' ,
106- iac : 'sam' ,
107- }
108- const mockExtContext = {
109- awsContext : {
110- getCredentials : ( ) => Promise . resolve ( { } ) ,
111- getCredentialDefaultRegion : ( ) => 'us-west-2' ,
112- } ,
113- } as unknown as ExtContext
51+ sandbox . stub ( vscode . Uri , 'joinPath' ) . returns ( vscode . Uri . file ( '/test' ) )
52+ sandbox . stub ( vscode . commands , 'executeCommand' ) . resolves ( )
11453
115- beforeEach ( ( ) => {
116- sandbox = sinon . createSandbox ( )
54+ sandbox . stub ( workspaceUtils , 'addFolderToWorkspace' ) . resolves ( )
55+ sandbox . stub ( downloadPattern , 'getPattern' ) . resolves ( )
11756 } )
11857 afterEach ( ( ) => {
11958 sandbox . restore ( )
12059 } )
121- it ( 'should launch wizard and return form data' , async ( ) => {
122- const wizardRunStub = sandbox . stub ( ) . resolves ( mockWizardResponse )
123- sandbox . stub ( CreateServerlessLandWizard . prototype , 'run' ) . get ( ( ) => wizardRunStub )
124-
125- const result = await launchProjectCreationWizard ( mockExtContext )
126-
127- sinon . assert . calledOnce ( wizardRunStub )
128- assert . deepStrictEqual ( result , mockWizardResponse )
60+ it ( 'should complete project creation successfully' , async ( ) => {
61+ const mockConfig = {
62+ pattern : 'testPattern' ,
63+ runtime : 'nodejs' ,
64+ iac : 'sam' ,
65+ location : vscode . Uri . file ( '/test' ) ,
66+ name : 'testProject' ,
67+ }
68+ mockWizard . run . resolves ( mockConfig )
69+ await createNewServerlessLandProject ( mockExtContext )
70+ assert . strictEqual ( mockWizard . run . calledOnce , true )
71+ assert . strictEqual ( mockMetadataManager . getAssetName . calledOnce , true )
72+ assert . strictEqual ( ( downloadPattern . getPattern as sinon . SinonStub ) . calledOnce , true )
12973 } )
130- it ( 'should return undefined when wizard is cancelled' , async ( ) => {
131- const wizardRunStub = sandbox . stub ( ) . resolves ( undefined )
132- sandbox . stub ( CreateServerlessLandWizard . prototype , 'run' ) . get ( ( ) => wizardRunStub )
133-
134- const result = await launchProjectCreationWizard ( mockExtContext )
135-
136- sinon . assert . calledOnce ( wizardRunStub )
137- assert . strictEqual ( result , undefined )
74+ it ( 'should handle wizard cancellation' , async ( ) => {
75+ mockWizard . run . resolves ( undefined )
76+ await createNewServerlessLandProject ( mockExtContext )
77+ assert . strictEqual ( mockWizard . run . calledOnce , true )
78+ assert . strictEqual ( mockMetadataManager . getAssetName . called , false )
79+ assert . strictEqual ( ( downloadPattern . getPattern as sinon . SinonStub ) . called , false )
13880 } )
13981} )
14082
14183describe ( 'downloadPatternCode' , ( ) => {
84+ let sandbox : sinon . SinonSandbox
14285 let getPatternStub : sinon . SinonStub
143- const assertTelemetry = assertTelemetryCurried ( 'serverlessland_downloadPattern' )
14486
14587 beforeEach ( function ( ) {
146- getPatternStub = sinon . stub ( downloadPatterns , 'getPattern' )
88+ sandbox = sinon . createSandbox ( )
89+ getPatternStub = sandbox . stub ( downloadPatterns , 'getPattern' )
14790 } )
14891 afterEach ( function ( ) {
149- sinon . restore ( )
92+ sandbox . restore ( )
15093 } )
15194 const mockConfig = {
15295 name : 'test-project' ,
153- location : vscode . Uri . file ( '. /test' ) ,
96+ location : vscode . Uri . file ( '/test' ) ,
15497 pattern : 'test-project-sam-python' ,
15598 runtime : 'python' ,
15699 iac : 'sam' ,
@@ -178,10 +121,6 @@ describe('downloadPatternCode', () => {
178121 await downloadPatternCode ( mockConfig , mockAssetName )
179122 assert . fail ( 'Expected an error to be thrown' )
180123 } catch ( err : any ) {
181- assertTelemetry ( {
182- result : 'Failed' ,
183- reason : 'Error' ,
184- } )
185124 assert . strictEqual ( err . message , 'Failed to download pattern: Error: Download failed' )
186125 }
187126 } )
@@ -273,7 +212,7 @@ describe('getProjectUri', () => {
273212 } as vscode . Uri )
274213
275214 const result = await getProjectUri ( mockConfig , testFile )
276- sinon . assert . calledWith ( uriFileStub , expectedPath )
215+ sandbox . assert . calledWith ( uriFileStub , expectedPath )
277216 assert . strictEqual ( result ?. fsPath , expectedPath )
278217 } )
279218 it ( 'handles missing project directory' , async ( ) => {
0 commit comments