@@ -22,11 +22,21 @@ import {
2222 expect ,
2323 vi
2424} from "vitest"
25- import { createApp } from "../../src/apps/createApp"
25+ import { createApp , type CreateAppParams } from "../../src/apps/createApp"
2626import { AwsSolutionsChecks } from "cdk-nag"
2727
2828describe ( "createApp" , ( ) => {
2929 const originalEnv = process . env
30+ const defaultParams : CreateAppParams = {
31+ productName : "testProduct" ,
32+ appName : "testApp" ,
33+ repoName : "testRepo" ,
34+ driftDetectionGroup : "test-drift-group"
35+ }
36+ const buildParams = ( overrides : Partial < CreateAppParams > = { } ) : CreateAppParams => ( {
37+ ...defaultParams ,
38+ ...overrides
39+ } )
3040
3141 beforeEach ( ( ) => {
3242 // Reset environment before each test
@@ -45,10 +55,11 @@ describe("createApp", () => {
4555 process . env . CDK_CONFIG_versionNumber = "1.2.3"
4656 process . env . CDK_CONFIG_commitId = "abc123def456"
4757 process . env . CDK_CONFIG_isPullRequest = "false"
58+ process . env . CDK_CONFIG_environment = "test-environment"
4859 } )
4960
5061 test ( "creates an App with correct configuration" , ( ) => {
51- const { app, props} = createApp ( "testApp" , "testRepo" , "test-drift-group" )
62+ const { app, props} = createApp ( buildParams ( ) )
5263
5364 expect ( app ) . toBeInstanceOf ( App )
5465 expect ( props . stackName ) . toBe ( "test-stack-1-2-3" )
@@ -59,13 +70,13 @@ describe("createApp", () => {
5970 } )
6071
6172 test ( "creates stateful App with correct stackName" , ( ) => {
62- const { props} = createApp ( "testApp" , "testRepo" , "test-drift-group" , false )
73+ const { props} = createApp ( buildParams ( { isStateless : false } ) )
6374
6475 expect ( props . stackName ) . toBe ( "test-stack" )
6576 } )
6677
6778 test ( "uses custom region when provided" , ( ) => {
68- const { props} = createApp ( "testApp" , "testRepo" , "test-drift-group" , true , " us-east-1")
79+ const { props} = createApp ( buildParams ( { region : " us-east-1"} ) )
6980
7081 expect ( props . env ?. region ) . toBe ( "us-east-1" )
7182 } )
@@ -77,12 +88,27 @@ describe("createApp", () => {
7788 add : addTagSpy
7889 } as unknown as Tags )
7990
80- const { app} = createApp ( "testApp" , "testRepo" , "test-drift-group" )
91+ const { app} = createApp ( buildParams ( ) )
8192
8293 // Verify Tags.of was called with the app
8394 expect ( tagsOfSpy ) . toHaveBeenCalledWith ( app )
8495
8596 // Verify all expected tags were added with correct values
97+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "TagVersion" , "1" )
98+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "Programme" , "EPS" )
99+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "Product" , "testProduct" )
100+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "Owner" , "england.epssupport@nhs.net" )
101+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "Product" , "testProduct" )
102+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "CostCentre" , "128997" )
103+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "Customer" , "NHS England" )
104+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "data_classification" , "5" )
105+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "DataType" , "PII" )
106+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "Environment" , "test-environment" )
107+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "ProjectType" , "Production" )
108+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "PublicFacing" , "Y" )
109+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "ServiceCategory" , "Platinum" )
110+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "OnOffPattern" , "AlwaysOn" )
111+ expect ( addTagSpy ) . toHaveBeenCalledWith ( "DeploymentTool" , "CDK" )
86112 expect ( addTagSpy ) . toHaveBeenCalledWith ( "version" , "1.2.3" )
87113 expect ( addTagSpy ) . toHaveBeenCalledWith ( "commit" , "abc123def456" )
88114 expect ( addTagSpy ) . toHaveBeenCalledWith ( "stackName" , "test-stack" )
@@ -91,14 +117,14 @@ describe("createApp", () => {
91117 expect ( addTagSpy ) . toHaveBeenCalledWith ( "cfnDriftDetectionGroup" , "test-drift-group" )
92118
93119 // Verify exactly 6 tags were added
94- expect ( addTagSpy ) . toHaveBeenCalledTimes ( 6 )
120+ expect ( addTagSpy ) . toHaveBeenCalledTimes ( 20 )
95121
96122 // Restore the spy
97123 tagsOfSpy . mockRestore ( )
98124 } )
99125
100126 test ( "adds AwsSolutionsChecks aspect" , ( ) => {
101- const { app} = createApp ( "testApp" , "testRepo" , "test-drift-group" )
127+ const { app} = createApp ( buildParams ( ) )
102128
103129 const aspects = Aspects . of ( app ) . all
104130 expect ( aspects ) . toContainEqual ( new AwsSolutionsChecks ( { verbose : true } ) )
@@ -111,19 +137,21 @@ describe("createApp", () => {
111137 process . env . CDK_CONFIG_versionNumber = "0.0.1-pr"
112138 process . env . CDK_CONFIG_commitId = "pr123"
113139 process . env . CDK_CONFIG_isPullRequest = "true"
140+ process . env . CDK_CONFIG_environment = "test-environment"
114141 } )
115142
116143 test ( "correctly modifies props" , ( ) => {
117- const { props} = createApp ( "testApp" , "testRepo" , "test-drift-group" )
144+ const { props} = createApp ( buildParams ( ) )
118145
119146 expect ( props . stackName ) . toBe ( "pr-stack" )
120147 expect ( props . version ) . toBe ( "0.0.1-pr" )
121148 expect ( props . commitId ) . toBe ( "pr123" )
122149 expect ( props . isPullRequest ) . toBe ( true )
150+ expect ( props . environment ) . toBe ( "test-environment" )
123151 } )
124152
125153 test ( "creates stateful App with unmodified stackName" , ( ) => {
126- const { props} = createApp ( "testApp" , "testRepo" , "test-drift-group" , false )
154+ const { props} = createApp ( buildParams ( { isStateless : false } ) )
127155
128156 expect ( props . stackName ) . toBe ( "pr-stack" )
129157 } )
@@ -135,7 +163,7 @@ describe("createApp", () => {
135163 add : addTagSpy
136164 } as unknown as Tags )
137165
138- const { app} = createApp ( "testApp" , "testRepo" , "test-drift-group" )
166+ const { app} = createApp ( buildParams ( ) )
139167
140168 // Verify Tags.of was called with the app
141169 expect ( tagsOfSpy ) . toHaveBeenCalledWith ( app )
@@ -150,40 +178,54 @@ describe("createApp", () => {
150178 process . env . CDK_CONFIG_versionNumber = "1.0.0"
151179 process . env . CDK_CONFIG_commitId = "abc123"
152180 process . env . CDK_CONFIG_isPullRequest = "false"
181+ process . env . CDK_CONFIG_environment = "test-environment"
153182
154183 expect ( ( ) => {
155- createApp ( "testApp" , "testRepo" , "test-drift-group" )
184+ createApp ( buildParams ( ) )
156185 } ) . toThrow ( "Environment variable CDK_CONFIG_stackName is not set" )
157186 } )
158187
159188 test ( "throws error when versionNumber is not set" , ( ) => {
160189 process . env . CDK_CONFIG_stackName = "test-stack"
161190 process . env . CDK_CONFIG_commitId = "abc123"
162191 process . env . CDK_CONFIG_isPullRequest = "false"
192+ process . env . CDK_CONFIG_environment = "test-environment"
163193
164194 expect ( ( ) => {
165- createApp ( "testApp" , "testRepo" , "test-drift-group" )
195+ createApp ( buildParams ( ) )
166196 } ) . toThrow ( "Environment variable CDK_CONFIG_versionNumber is not set" )
167197 } )
168198
169199 test ( "throws error when commitId is not set" , ( ) => {
170200 process . env . CDK_CONFIG_stackName = "test-stack"
171201 process . env . CDK_CONFIG_versionNumber = "1.0.0"
172202 process . env . CDK_CONFIG_isPullRequest = "false"
203+ process . env . CDK_CONFIG_environment = "test-environment"
173204
174205 expect ( ( ) => {
175- createApp ( "testApp" , "testRepo" , "test-drift-group" )
206+ createApp ( buildParams ( ) )
176207 } ) . toThrow ( "Environment variable CDK_CONFIG_commitId is not set" )
177208 } )
178209
179210 test ( "throws error when isPullRequest is not set" , ( ) => {
180211 process . env . CDK_CONFIG_stackName = "test-stack"
181212 process . env . CDK_CONFIG_versionNumber = "1.0.0"
182213 process . env . CDK_CONFIG_commitId = "abc123"
183-
214+ process . env . CDK_CONFIG_environment = "test-environment"
184215 expect ( ( ) => {
185- createApp ( "testApp" , "testRepo" , "test-drift-group" )
216+ createApp ( buildParams ( ) )
186217 } ) . toThrow ( "Environment variable CDK_CONFIG_isPullRequest is not set" )
187218 } )
219+
220+ test ( "throws error when environment is not set" , ( ) => {
221+ process . env . CDK_CONFIG_stackName = "test-stack"
222+ process . env . CDK_CONFIG_versionNumber = "1.0.0"
223+ process . env . CDK_CONFIG_commitId = "abc123"
224+ process . env . CDK_CONFIG_isPullRequest = "false"
225+ expect ( ( ) => {
226+ createApp ( buildParams ( ) )
227+ } ) . toThrow ( "Environment variable CDK_CONFIG_environment is not set" )
228+ } )
229+
188230 } )
189231} )
0 commit comments