@friggframework/devtools 2.0.0--canary.474.082077e.0 → 2.0.0--canary.474.884529c.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,461 @@
1
+ /**
2
+ * Tests for AWSPropertyReconciler Adapter
3
+ *
4
+ * Tests property drift reconciliation operations
5
+ */
6
+
7
+ const AWSPropertyReconciler = require('./aws-property-reconciler');
8
+ const StackIdentifier = require('../../domain/value-objects/stack-identifier');
9
+ const PropertyMismatch = require('../../domain/entities/property-mismatch');
10
+ const PropertyMutability = require('../../domain/value-objects/property-mutability');
11
+
12
+ // Mock AWS SDK
13
+ jest.mock('@aws-sdk/client-cloudformation', () => ({
14
+ CloudFormationClient: jest.fn(),
15
+ UpdateStackCommand: jest.fn(),
16
+ GetTemplateCommand: jest.fn(),
17
+ }));
18
+
19
+ jest.mock('@aws-sdk/client-ec2', () => ({
20
+ EC2Client: jest.fn(),
21
+ ModifyVpcAttributeCommand: jest.fn(),
22
+ }));
23
+
24
+ describe('AWSPropertyReconciler', () => {
25
+ let reconciler;
26
+ let mockCFSend;
27
+ let mockEC2Send;
28
+
29
+ beforeEach(() => {
30
+ jest.clearAllMocks();
31
+
32
+ // Mock CloudFormation client
33
+ mockCFSend = jest.fn();
34
+ const { CloudFormationClient } = require('@aws-sdk/client-cloudformation');
35
+ CloudFormationClient.mockImplementation(() => ({ send: mockCFSend }));
36
+
37
+ // Mock EC2 client
38
+ mockEC2Send = jest.fn();
39
+ const { EC2Client } = require('@aws-sdk/client-ec2');
40
+ EC2Client.mockImplementation(() => ({ send: mockEC2Send }));
41
+
42
+ reconciler = new AWSPropertyReconciler({ region: 'us-east-1' });
43
+ });
44
+
45
+ describe('canReconcile', () => {
46
+ it('should return true for mutable property mismatch', async () => {
47
+ const mismatch = new PropertyMismatch({
48
+ propertyPath: 'Properties.Tags',
49
+ expectedValue: { Environment: 'production' },
50
+ actualValue: { Environment: 'staging' },
51
+ mutability: PropertyMutability.MUTABLE,
52
+ });
53
+
54
+ const canReconcile = await reconciler.canReconcile(mismatch);
55
+ expect(canReconcile).toBe(true);
56
+ });
57
+
58
+ it('should return false for immutable property mismatch', async () => {
59
+ const mismatch = new PropertyMismatch({
60
+ propertyPath: 'Properties.CidrBlock',
61
+ expectedValue: '10.0.0.0/16',
62
+ actualValue: '10.1.0.0/16',
63
+ mutability: PropertyMutability.IMMUTABLE,
64
+ });
65
+
66
+ const canReconcile = await reconciler.canReconcile(mismatch);
67
+ expect(canReconcile).toBe(false);
68
+ });
69
+
70
+ it('should return true for conditional property mismatch', async () => {
71
+ const mismatch = new PropertyMismatch({
72
+ propertyPath: 'Properties.EngineVersion',
73
+ expectedValue: '13.7',
74
+ actualValue: '13.8',
75
+ mutability: PropertyMutability.CONDITIONAL,
76
+ });
77
+
78
+ const canReconcile = await reconciler.canReconcile(mismatch);
79
+ expect(canReconcile).toBe(true);
80
+ });
81
+ });
82
+
83
+ describe('reconcileProperty - template mode', () => {
84
+ it('should reconcile property by updating template', async () => {
85
+ const stackIdentifier = new StackIdentifier({
86
+ stackName: 'my-app-prod',
87
+ region: 'us-east-1',
88
+ });
89
+
90
+ const mismatch = new PropertyMismatch({
91
+ propertyPath: 'Properties.EnableDnsSupport',
92
+ expectedValue: true,
93
+ actualValue: false,
94
+ mutability: PropertyMutability.MUTABLE,
95
+ });
96
+
97
+ // Mock GetTemplate
98
+ mockCFSend.mockResolvedValueOnce({
99
+ TemplateBody: JSON.stringify({
100
+ Resources: {
101
+ MyVPC: {
102
+ Type: 'AWS::EC2::VPC',
103
+ Properties: {
104
+ CidrBlock: '10.0.0.0/16',
105
+ EnableDnsSupport: true,
106
+ },
107
+ },
108
+ },
109
+ }),
110
+ });
111
+
112
+ // Mock UpdateStack
113
+ mockCFSend.mockResolvedValueOnce({
114
+ StackId: 'arn:aws:cloudformation:us-east-1:123456789012:stack/my-app-prod/guid',
115
+ });
116
+
117
+ const result = await reconciler.reconcileProperty({
118
+ stackIdentifier,
119
+ logicalId: 'MyVPC',
120
+ mismatch,
121
+ mode: 'template',
122
+ });
123
+
124
+ expect(result).toEqual({
125
+ success: true,
126
+ mode: 'template',
127
+ propertyPath: 'Properties.EnableDnsSupport',
128
+ oldValue: true,
129
+ newValue: false,
130
+ message: 'Template updated to match actual resource state',
131
+ });
132
+
133
+ expect(mockCFSend).toHaveBeenCalledTimes(2);
134
+ });
135
+ });
136
+
137
+ describe('reconcileProperty - resource mode', () => {
138
+ it('should reconcile property by updating resource', async () => {
139
+ const stackIdentifier = new StackIdentifier({
140
+ stackName: 'my-app-prod',
141
+ region: 'us-east-1',
142
+ });
143
+
144
+ const mismatch = new PropertyMismatch({
145
+ propertyPath: 'Properties.EnableDnsSupport',
146
+ expectedValue: true,
147
+ actualValue: false,
148
+ mutability: PropertyMutability.MUTABLE,
149
+ });
150
+
151
+ // Mock EC2 ModifyVpcAttribute
152
+ mockEC2Send.mockResolvedValue({});
153
+
154
+ const result = await reconciler.reconcileProperty({
155
+ stackIdentifier,
156
+ logicalId: 'MyVPC',
157
+ mismatch,
158
+ mode: 'resource',
159
+ });
160
+
161
+ expect(result.success).toBe(true);
162
+ expect(result.mode).toBe('resource');
163
+ expect(result.propertyPath).toBe('Properties.EnableDnsSupport');
164
+ });
165
+
166
+ it('should throw error for unsupported resource update', async () => {
167
+ const stackIdentifier = new StackIdentifier({
168
+ stackName: 'my-app-prod',
169
+ region: 'us-east-1',
170
+ });
171
+
172
+ const mismatch = new PropertyMismatch({
173
+ propertyPath: 'Properties.SomeUnsupportedProperty',
174
+ expectedValue: 'value1',
175
+ actualValue: 'value2',
176
+ mutability: PropertyMutability.MUTABLE,
177
+ });
178
+
179
+ await expect(
180
+ reconciler.reconcileProperty({
181
+ stackIdentifier,
182
+ logicalId: 'MyVPC',
183
+ mismatch,
184
+ mode: 'resource',
185
+ })
186
+ ).rejects.toThrow('Resource property update not supported');
187
+ });
188
+ });
189
+
190
+ describe('reconcileMultipleProperties', () => {
191
+ it('should reconcile multiple properties in template mode', async () => {
192
+ const stackIdentifier = new StackIdentifier({
193
+ stackName: 'my-app-prod',
194
+ region: 'us-east-1',
195
+ });
196
+
197
+ const mismatches = [
198
+ new PropertyMismatch({
199
+ propertyPath: 'Properties.EnableDnsSupport',
200
+ expectedValue: true,
201
+ actualValue: false,
202
+ mutability: PropertyMutability.MUTABLE,
203
+ }),
204
+ new PropertyMismatch({
205
+ propertyPath: 'Properties.EnableDnsHostnames',
206
+ expectedValue: true,
207
+ actualValue: false,
208
+ mutability: PropertyMutability.MUTABLE,
209
+ }),
210
+ ];
211
+
212
+ // Mock GetTemplate for first property
213
+ mockCFSend.mockResolvedValueOnce({
214
+ TemplateBody: JSON.stringify({
215
+ Resources: {
216
+ MyVPC: {
217
+ Type: 'AWS::EC2::VPC',
218
+ Properties: {
219
+ CidrBlock: '10.0.0.0/16',
220
+ EnableDnsSupport: true,
221
+ EnableDnsHostnames: true,
222
+ },
223
+ },
224
+ },
225
+ }),
226
+ });
227
+
228
+ // Mock UpdateStack for first property
229
+ mockCFSend.mockResolvedValueOnce({
230
+ StackId: 'arn:aws:cloudformation:us-east-1:123456789012:stack/my-app-prod/guid',
231
+ });
232
+
233
+ // Mock GetTemplate for second property
234
+ mockCFSend.mockResolvedValueOnce({
235
+ TemplateBody: JSON.stringify({
236
+ Resources: {
237
+ MyVPC: {
238
+ Type: 'AWS::EC2::VPC',
239
+ Properties: {
240
+ CidrBlock: '10.0.0.0/16',
241
+ EnableDnsSupport: false,
242
+ EnableDnsHostnames: true,
243
+ },
244
+ },
245
+ },
246
+ }),
247
+ });
248
+
249
+ // Mock UpdateStack for second property
250
+ mockCFSend.mockResolvedValueOnce({
251
+ StackId: 'arn:aws:cloudformation:us-east-1:123456789012:stack/my-app-prod/guid',
252
+ });
253
+
254
+ const result = await reconciler.reconcileMultipleProperties({
255
+ stackIdentifier,
256
+ logicalId: 'MyVPC',
257
+ mismatches,
258
+ mode: 'template',
259
+ });
260
+
261
+ expect(result.reconciledCount).toBe(2);
262
+ expect(result.failedCount).toBe(0);
263
+ expect(result.results).toHaveLength(2);
264
+ });
265
+ });
266
+
267
+ describe('previewReconciliation', () => {
268
+ it('should preview template reconciliation', async () => {
269
+ const stackIdentifier = new StackIdentifier({
270
+ stackName: 'my-app-prod',
271
+ region: 'us-east-1',
272
+ });
273
+
274
+ const mismatch = new PropertyMismatch({
275
+ propertyPath: 'Properties.EnableDnsSupport',
276
+ expectedValue: true,
277
+ actualValue: false,
278
+ mutability: PropertyMutability.MUTABLE,
279
+ });
280
+
281
+ const preview = await reconciler.previewReconciliation({
282
+ stackIdentifier,
283
+ logicalId: 'MyVPC',
284
+ mismatch,
285
+ mode: 'template',
286
+ });
287
+
288
+ expect(preview).toEqual({
289
+ canReconcile: true,
290
+ mode: 'template',
291
+ propertyPath: 'Properties.EnableDnsSupport',
292
+ currentValue: true,
293
+ proposedValue: false,
294
+ impact: 'Will update CloudFormation template to match actual resource state',
295
+ warnings: [],
296
+ });
297
+ });
298
+
299
+ it('should preview resource reconciliation', async () => {
300
+ const stackIdentifier = new StackIdentifier({
301
+ stackName: 'my-app-prod',
302
+ region: 'us-east-1',
303
+ });
304
+
305
+ const mismatch = new PropertyMismatch({
306
+ propertyPath: 'Properties.EnableDnsSupport',
307
+ expectedValue: true,
308
+ actualValue: false,
309
+ mutability: PropertyMutability.MUTABLE,
310
+ });
311
+
312
+ const preview = await reconciler.previewReconciliation({
313
+ stackIdentifier,
314
+ logicalId: 'MyVPC',
315
+ mismatch,
316
+ mode: 'resource',
317
+ });
318
+
319
+ expect(preview.canReconcile).toBe(true);
320
+ expect(preview.mode).toBe('resource');
321
+ expect(preview.impact).toContain('Will update cloud resource');
322
+ });
323
+
324
+ it('should warn for immutable property', async () => {
325
+ const stackIdentifier = new StackIdentifier({
326
+ stackName: 'my-app-prod',
327
+ region: 'us-east-1',
328
+ });
329
+
330
+ const mismatch = new PropertyMismatch({
331
+ propertyPath: 'Properties.CidrBlock',
332
+ expectedValue: '10.0.0.0/16',
333
+ actualValue: '10.1.0.0/16',
334
+ mutability: PropertyMutability.IMMUTABLE,
335
+ });
336
+
337
+ const preview = await reconciler.previewReconciliation({
338
+ stackIdentifier,
339
+ logicalId: 'MyVPC',
340
+ mismatch,
341
+ mode: 'template',
342
+ });
343
+
344
+ expect(preview.canReconcile).toBe(false);
345
+ expect(preview.warnings).toContain(
346
+ 'Property is immutable - requires resource replacement'
347
+ );
348
+ });
349
+ });
350
+
351
+ describe('updateTemplateProperty', () => {
352
+ it('should update CloudFormation template property', async () => {
353
+ const stackIdentifier = new StackIdentifier({
354
+ stackName: 'my-app-prod',
355
+ region: 'us-east-1',
356
+ });
357
+
358
+ // Mock GetTemplate
359
+ mockCFSend.mockResolvedValueOnce({
360
+ TemplateBody: JSON.stringify({
361
+ Resources: {
362
+ MyVPC: {
363
+ Type: 'AWS::EC2::VPC',
364
+ Properties: {
365
+ CidrBlock: '10.0.0.0/16',
366
+ EnableDnsSupport: true,
367
+ },
368
+ },
369
+ },
370
+ }),
371
+ });
372
+
373
+ // Mock UpdateStack
374
+ mockCFSend.mockResolvedValueOnce({
375
+ StackId: 'arn:aws:cloudformation:us-east-1:123456789012:stack/my-app-prod/guid',
376
+ });
377
+
378
+ const result = await reconciler.updateTemplateProperty({
379
+ stackIdentifier,
380
+ logicalId: 'MyVPC',
381
+ propertyPath: 'Properties.EnableDnsSupport',
382
+ newValue: false,
383
+ });
384
+
385
+ expect(result.success).toBe(true);
386
+ expect(result.changeSetId).toBeDefined();
387
+ });
388
+ });
389
+
390
+ describe('updateResourceProperty', () => {
391
+ it('should update VPC DNS support', async () => {
392
+ mockEC2Send.mockResolvedValue({});
393
+
394
+ const result = await reconciler.updateResourceProperty({
395
+ resourceType: 'AWS::EC2::VPC',
396
+ physicalId: 'vpc-123',
397
+ region: 'us-east-1',
398
+ propertyPath: 'Properties.EnableDnsSupport',
399
+ newValue: false,
400
+ });
401
+
402
+ expect(result.success).toBe(true);
403
+ expect(result.updatedAt).toBeInstanceOf(Date);
404
+ });
405
+
406
+ it('should throw error for unsupported resource type', async () => {
407
+ await expect(
408
+ reconciler.updateResourceProperty({
409
+ resourceType: 'AWS::Lambda::Function',
410
+ physicalId: 'my-function',
411
+ region: 'us-east-1',
412
+ propertyPath: 'Properties.MemorySize',
413
+ newValue: 512,
414
+ })
415
+ ).rejects.toThrow('Resource type AWS::Lambda::Function updates not supported');
416
+ });
417
+ });
418
+
419
+ describe('getReconciliationStrategy', () => {
420
+ it('should get strategy for VPC', async () => {
421
+ const strategy = await reconciler.getReconciliationStrategy('AWS::EC2::VPC');
422
+
423
+ expect(strategy).toEqual({
424
+ supportsTemplateUpdate: true,
425
+ supportsResourceUpdate: true,
426
+ recommendedMode: 'template',
427
+ limitations: [
428
+ 'Some VPC properties require resource replacement (e.g., CidrBlock)',
429
+ ],
430
+ });
431
+ });
432
+
433
+ it('should get strategy for RDS DBCluster', async () => {
434
+ const strategy = await reconciler.getReconciliationStrategy(
435
+ 'AWS::RDS::DBCluster'
436
+ );
437
+
438
+ expect(strategy.supportsTemplateUpdate).toBe(true);
439
+ expect(strategy.supportsResourceUpdate).toBe(false);
440
+ expect(strategy.recommendedMode).toBe('template');
441
+ });
442
+
443
+ it('should throw error for unsupported type', async () => {
444
+ await expect(
445
+ reconciler.getReconciliationStrategy('AWS::Lambda::Function')
446
+ ).rejects.toThrow('Resource type AWS::Lambda::Function not supported');
447
+ });
448
+ });
449
+
450
+ describe('constructor', () => {
451
+ it('should create instance with default region', () => {
452
+ const rec = new AWSPropertyReconciler();
453
+ expect(rec).toBeInstanceOf(AWSPropertyReconciler);
454
+ });
455
+
456
+ it('should create instance with custom region', () => {
457
+ const rec = new AWSPropertyReconciler({ region: 'eu-west-1' });
458
+ expect(rec).toBeInstanceOf(AWSPropertyReconciler);
459
+ });
460
+ });
461
+ });