@friggframework/devtools 2.0.0-next.62 → 2.0.0-next.63

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.
Files changed (165) hide show
  1. package/infrastructure/ARCHITECTURE.md +487 -0
  2. package/infrastructure/CLAUDE.md +481 -0
  3. package/infrastructure/HEALTH.md +468 -0
  4. package/infrastructure/README.md +522 -0
  5. package/infrastructure/__tests__/fixtures/mock-aws-resources.js +391 -0
  6. package/infrastructure/__tests__/helpers/test-utils.js +277 -0
  7. package/infrastructure/__tests__/postgres-config.test.js +914 -0
  8. package/infrastructure/__tests__/template-generation.test.js +687 -0
  9. package/infrastructure/create-frigg-infrastructure.js +147 -0
  10. package/infrastructure/docs/POSTGRES-CONFIGURATION.md +630 -0
  11. package/infrastructure/docs/PRE-DEPLOYMENT-HEALTH-CHECK-SPEC.md +1317 -0
  12. package/infrastructure/docs/WEBSOCKET-CONFIGURATION.md +105 -0
  13. package/infrastructure/docs/deployment-instructions.md +268 -0
  14. package/infrastructure/docs/generate-iam-command.md +278 -0
  15. package/infrastructure/docs/iam-policy-templates.md +193 -0
  16. package/infrastructure/domains/database/aurora-builder.js +809 -0
  17. package/infrastructure/domains/database/aurora-builder.test.js +950 -0
  18. package/infrastructure/domains/database/aurora-discovery.js +87 -0
  19. package/infrastructure/domains/database/aurora-discovery.test.js +188 -0
  20. package/infrastructure/domains/database/aurora-resolver.js +210 -0
  21. package/infrastructure/domains/database/aurora-resolver.test.js +347 -0
  22. package/infrastructure/domains/database/migration-builder.js +701 -0
  23. package/infrastructure/domains/database/migration-builder.test.js +321 -0
  24. package/infrastructure/domains/database/migration-resolver.js +163 -0
  25. package/infrastructure/domains/database/migration-resolver.test.js +337 -0
  26. package/infrastructure/domains/health/application/ports/IPropertyReconciler.js +164 -0
  27. package/infrastructure/domains/health/application/ports/IResourceDetector.js +129 -0
  28. package/infrastructure/domains/health/application/ports/IResourceImporter.js +142 -0
  29. package/infrastructure/domains/health/application/ports/IStackRepository.js +131 -0
  30. package/infrastructure/domains/health/application/ports/index.js +26 -0
  31. package/infrastructure/domains/health/application/use-cases/__tests__/execute-resource-import-use-case.test.js +679 -0
  32. package/infrastructure/domains/health/application/use-cases/__tests__/mismatch-analyzer-method-name.test.js +167 -0
  33. package/infrastructure/domains/health/application/use-cases/__tests__/repair-via-import-use-case.test.js +1130 -0
  34. package/infrastructure/domains/health/application/use-cases/execute-resource-import-use-case.js +221 -0
  35. package/infrastructure/domains/health/application/use-cases/reconcile-properties-use-case.js +152 -0
  36. package/infrastructure/domains/health/application/use-cases/reconcile-properties-use-case.test.js +343 -0
  37. package/infrastructure/domains/health/application/use-cases/repair-via-import-use-case.js +535 -0
  38. package/infrastructure/domains/health/application/use-cases/repair-via-import-use-case.test.js +376 -0
  39. package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.js +213 -0
  40. package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.test.js +441 -0
  41. package/infrastructure/domains/health/docs/ACME-DEV-DRIFT-ANALYSIS.md +267 -0
  42. package/infrastructure/domains/health/docs/BUILD-VS-DEPLOYED-TEMPLATE-ANALYSIS.md +324 -0
  43. package/infrastructure/domains/health/docs/ORPHAN-DETECTION-ANALYSIS.md +386 -0
  44. package/infrastructure/domains/health/docs/SPEC-CLEANUP-COMMAND.md +1419 -0
  45. package/infrastructure/domains/health/docs/TDD-IMPLEMENTATION-SUMMARY.md +391 -0
  46. package/infrastructure/domains/health/docs/TEMPLATE-COMPARISON-IMPLEMENTATION.md +551 -0
  47. package/infrastructure/domains/health/domain/entities/issue.js +299 -0
  48. package/infrastructure/domains/health/domain/entities/issue.test.js +528 -0
  49. package/infrastructure/domains/health/domain/entities/property-mismatch.js +108 -0
  50. package/infrastructure/domains/health/domain/entities/property-mismatch.test.js +275 -0
  51. package/infrastructure/domains/health/domain/entities/resource.js +159 -0
  52. package/infrastructure/domains/health/domain/entities/resource.test.js +432 -0
  53. package/infrastructure/domains/health/domain/entities/stack-health-report.js +306 -0
  54. package/infrastructure/domains/health/domain/entities/stack-health-report.test.js +601 -0
  55. package/infrastructure/domains/health/domain/services/__tests__/health-score-percentage-based.test.js +380 -0
  56. package/infrastructure/domains/health/domain/services/__tests__/import-progress-monitor.test.js +971 -0
  57. package/infrastructure/domains/health/domain/services/__tests__/import-template-generator.test.js +1150 -0
  58. package/infrastructure/domains/health/domain/services/__tests__/logical-id-mapper.test.js +672 -0
  59. package/infrastructure/domains/health/domain/services/__tests__/template-parser.test.js +496 -0
  60. package/infrastructure/domains/health/domain/services/__tests__/update-progress-monitor.test.js +419 -0
  61. package/infrastructure/domains/health/domain/services/health-score-calculator.js +248 -0
  62. package/infrastructure/domains/health/domain/services/health-score-calculator.test.js +504 -0
  63. package/infrastructure/domains/health/domain/services/import-progress-monitor.js +195 -0
  64. package/infrastructure/domains/health/domain/services/import-template-generator.js +435 -0
  65. package/infrastructure/domains/health/domain/services/logical-id-mapper.js +345 -0
  66. package/infrastructure/domains/health/domain/services/mismatch-analyzer.js +234 -0
  67. package/infrastructure/domains/health/domain/services/mismatch-analyzer.test.js +431 -0
  68. package/infrastructure/domains/health/domain/services/property-mutability-config.js +382 -0
  69. package/infrastructure/domains/health/domain/services/template-parser.js +245 -0
  70. package/infrastructure/domains/health/domain/services/update-progress-monitor.js +192 -0
  71. package/infrastructure/domains/health/domain/value-objects/health-score.js +138 -0
  72. package/infrastructure/domains/health/domain/value-objects/health-score.test.js +267 -0
  73. package/infrastructure/domains/health/domain/value-objects/property-mutability.js +161 -0
  74. package/infrastructure/domains/health/domain/value-objects/property-mutability.test.js +198 -0
  75. package/infrastructure/domains/health/domain/value-objects/resource-state.js +167 -0
  76. package/infrastructure/domains/health/domain/value-objects/resource-state.test.js +196 -0
  77. package/infrastructure/domains/health/domain/value-objects/stack-identifier.js +192 -0
  78. package/infrastructure/domains/health/domain/value-objects/stack-identifier.test.js +262 -0
  79. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-cfn-tagged.test.js +312 -0
  80. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-multi-stack.test.js +367 -0
  81. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-relationship-analysis.test.js +432 -0
  82. package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.js +784 -0
  83. package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.test.js +1133 -0
  84. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.js +565 -0
  85. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.test.js +554 -0
  86. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-importer.js +318 -0
  87. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-importer.test.js +398 -0
  88. package/infrastructure/domains/health/infrastructure/adapters/aws-stack-repository.js +777 -0
  89. package/infrastructure/domains/health/infrastructure/adapters/aws-stack-repository.test.js +580 -0
  90. package/infrastructure/domains/integration/integration-builder.js +404 -0
  91. package/infrastructure/domains/integration/integration-builder.test.js +690 -0
  92. package/infrastructure/domains/integration/integration-resolver.js +170 -0
  93. package/infrastructure/domains/integration/integration-resolver.test.js +369 -0
  94. package/infrastructure/domains/integration/websocket-builder.js +69 -0
  95. package/infrastructure/domains/integration/websocket-builder.test.js +195 -0
  96. package/infrastructure/domains/networking/vpc-builder.js +2051 -0
  97. package/infrastructure/domains/networking/vpc-builder.test.js +1960 -0
  98. package/infrastructure/domains/networking/vpc-discovery.js +177 -0
  99. package/infrastructure/domains/networking/vpc-discovery.test.js +350 -0
  100. package/infrastructure/domains/networking/vpc-resolver.js +505 -0
  101. package/infrastructure/domains/networking/vpc-resolver.test.js +801 -0
  102. package/infrastructure/domains/parameters/ssm-builder.js +79 -0
  103. package/infrastructure/domains/parameters/ssm-builder.test.js +189 -0
  104. package/infrastructure/domains/parameters/ssm-discovery.js +84 -0
  105. package/infrastructure/domains/parameters/ssm-discovery.test.js +210 -0
  106. package/infrastructure/domains/security/iam-generator.js +816 -0
  107. package/infrastructure/domains/security/iam-generator.test.js +204 -0
  108. package/infrastructure/domains/security/kms-builder.js +415 -0
  109. package/infrastructure/domains/security/kms-builder.test.js +392 -0
  110. package/infrastructure/domains/security/kms-discovery.js +80 -0
  111. package/infrastructure/domains/security/kms-discovery.test.js +177 -0
  112. package/infrastructure/domains/security/kms-resolver.js +96 -0
  113. package/infrastructure/domains/security/kms-resolver.test.js +216 -0
  114. package/infrastructure/domains/security/templates/frigg-deployment-iam-stack.yaml +401 -0
  115. package/infrastructure/domains/security/templates/iam-policy-basic.json +218 -0
  116. package/infrastructure/domains/security/templates/iam-policy-full.json +288 -0
  117. package/infrastructure/domains/shared/base-builder.js +112 -0
  118. package/infrastructure/domains/shared/base-resolver.js +186 -0
  119. package/infrastructure/domains/shared/base-resolver.test.js +305 -0
  120. package/infrastructure/domains/shared/builder-orchestrator.js +212 -0
  121. package/infrastructure/domains/shared/builder-orchestrator.test.js +213 -0
  122. package/infrastructure/domains/shared/cloudformation-discovery-v2.js +334 -0
  123. package/infrastructure/domains/shared/cloudformation-discovery.js +672 -0
  124. package/infrastructure/domains/shared/cloudformation-discovery.test.js +985 -0
  125. package/infrastructure/domains/shared/environment-builder.js +119 -0
  126. package/infrastructure/domains/shared/environment-builder.test.js +247 -0
  127. package/infrastructure/domains/shared/providers/aws-provider-adapter.js +579 -0
  128. package/infrastructure/domains/shared/providers/aws-provider-adapter.test.js +416 -0
  129. package/infrastructure/domains/shared/providers/azure-provider-adapter.stub.js +93 -0
  130. package/infrastructure/domains/shared/providers/cloud-provider-adapter.js +136 -0
  131. package/infrastructure/domains/shared/providers/gcp-provider-adapter.stub.js +82 -0
  132. package/infrastructure/domains/shared/providers/provider-factory.js +108 -0
  133. package/infrastructure/domains/shared/providers/provider-factory.test.js +170 -0
  134. package/infrastructure/domains/shared/resource-discovery.enhanced.test.js +306 -0
  135. package/infrastructure/domains/shared/resource-discovery.js +233 -0
  136. package/infrastructure/domains/shared/resource-discovery.test.js +588 -0
  137. package/infrastructure/domains/shared/types/app-definition.js +205 -0
  138. package/infrastructure/domains/shared/types/discovery-result.js +106 -0
  139. package/infrastructure/domains/shared/types/discovery-result.test.js +258 -0
  140. package/infrastructure/domains/shared/types/index.js +46 -0
  141. package/infrastructure/domains/shared/types/resource-ownership.js +108 -0
  142. package/infrastructure/domains/shared/types/resource-ownership.test.js +101 -0
  143. package/infrastructure/domains/shared/utilities/base-definition-factory.js +408 -0
  144. package/infrastructure/domains/shared/utilities/base-definition-factory.js.bak +338 -0
  145. package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +291 -0
  146. package/infrastructure/domains/shared/utilities/handler-path-resolver.js +134 -0
  147. package/infrastructure/domains/shared/utilities/handler-path-resolver.test.js +268 -0
  148. package/infrastructure/domains/shared/utilities/prisma-layer-manager.js +159 -0
  149. package/infrastructure/domains/shared/utilities/prisma-layer-manager.test.js +444 -0
  150. package/infrastructure/domains/shared/validation/env-validator.js +78 -0
  151. package/infrastructure/domains/shared/validation/env-validator.test.js +173 -0
  152. package/infrastructure/domains/shared/validation/plugin-validator.js +187 -0
  153. package/infrastructure/domains/shared/validation/plugin-validator.test.js +323 -0
  154. package/infrastructure/esbuild.config.js +53 -0
  155. package/infrastructure/index.js +4 -0
  156. package/infrastructure/infrastructure-composer.js +117 -0
  157. package/infrastructure/infrastructure-composer.test.js +1895 -0
  158. package/infrastructure/integration.test.js +383 -0
  159. package/infrastructure/scripts/build-prisma-layer.js +701 -0
  160. package/infrastructure/scripts/build-prisma-layer.test.js +170 -0
  161. package/infrastructure/scripts/build-time-discovery.js +238 -0
  162. package/infrastructure/scripts/build-time-discovery.test.js +379 -0
  163. package/infrastructure/scripts/run-discovery.js +110 -0
  164. package/infrastructure/scripts/verify-prisma-layer.js +72 -0
  165. package/package.json +8 -7
@@ -0,0 +1,96 @@
1
+ /**
2
+ * KMS (Key Management Service) Resource Resolver
3
+ *
4
+ * Resolves KMS key ownership based on user intent and discovered resources.
5
+ *
6
+ * Ownership Resolution Logic:
7
+ * - User sets 'stack' → Create KMS key in CloudFormation stack
8
+ * - User sets 'external' → Use existing KMS key (discovered or env var)
9
+ * - User sets 'auto' (or unspecified):
10
+ * - If KMS key found in stack → Use stack resource (STACK)
11
+ * - If KMS key found externally → Use external resource (EXTERNAL)
12
+ * - If nothing found → Create in stack (STACK)
13
+ */
14
+
15
+ const BaseResourceResolver = require('../shared/base-resolver');
16
+ const { ResourceOwnership } = require('../shared/types');
17
+
18
+ class KmsResourceResolver extends BaseResourceResolver {
19
+ constructor() {
20
+ super();
21
+ }
22
+
23
+ /**
24
+ * Resolve KMS key ownership
25
+ * @param {Object} appDefinition - Application definition
26
+ * @param {Object} discovery - Structured discovery result
27
+ * @returns {Object} Ownership decision with metadata
28
+ */
29
+ resolveKey(appDefinition, discovery) {
30
+ // Get user intent from app definition
31
+ const userIntent = appDefinition.encryption?.ownership?.key || ResourceOwnership.AUTO;
32
+
33
+ // Check if KMS key exists in CloudFormation stack
34
+ const inStack = this.isInStack('FriggKMSKey', discovery);
35
+
36
+ if (userIntent === ResourceOwnership.STACK) {
37
+ // Explicit: Create/manage in stack
38
+ const stackResource = inStack ? this.findInStack('FriggKMSKey', discovery) : null;
39
+ return this.createStackDecision(
40
+ stackResource?.physicalId || null,
41
+ inStack ? 'Found FriggKMSKey in CloudFormation stack' : 'Will create FriggKMSKey in stack'
42
+ );
43
+ }
44
+
45
+ if (userIntent === ResourceOwnership.EXTERNAL) {
46
+ // Explicit: Use external key
47
+ const external = this.findExternal('AWS::KMS::Key', discovery);
48
+ if (!external) {
49
+ throw new Error(
50
+ 'ownership.key=external but no KMS key discovered. ' +
51
+ 'Provide defaultKmsKeyId in discoveredResources or set ownership.key=stack'
52
+ );
53
+ }
54
+ return this.createExternalDecision(
55
+ external.physicalId,
56
+ 'Using external KMS key per ownership.key=external'
57
+ );
58
+ }
59
+
60
+ // AUTO resolution
61
+ if (inStack) {
62
+ const stackResource = this.findInStack('FriggKMSKey', discovery);
63
+ return this.createStackDecision(
64
+ stackResource.physicalId,
65
+ 'Found FriggKMSKey in CloudFormation stack'
66
+ );
67
+ }
68
+
69
+ // Check for external KMS key
70
+ const external = this.findExternal('AWS::KMS::Key', discovery);
71
+ if (external) {
72
+ return this.createExternalDecision(
73
+ external.physicalId,
74
+ 'Found external KMS key via discovery'
75
+ );
76
+ }
77
+
78
+ // No KMS key found - create in stack
79
+ return this.createStackDecision(
80
+ null,
81
+ 'No existing KMS key - will create in stack'
82
+ );
83
+ }
84
+
85
+ /**
86
+ * Resolve all KMS resources
87
+ * Convenience method for resolving all KMS resource ownership
88
+ */
89
+ resolveAll(appDefinition, discovery) {
90
+ return {
91
+ key: this.resolveKey(appDefinition, discovery),
92
+ };
93
+ }
94
+ }
95
+
96
+ module.exports = { KmsResourceResolver };
@@ -0,0 +1,216 @@
1
+ const { KmsResourceResolver } = require('./kms-resolver');
2
+ const { ResourceOwnership, createEmptyDiscoveryResult } = require('../shared/types');
3
+
4
+ describe('KmsResourceResolver', () => {
5
+ let resolver;
6
+
7
+ beforeEach(() => {
8
+ resolver = new KmsResourceResolver();
9
+ });
10
+
11
+ describe('resolveKey', () => {
12
+ describe('Explicit ownership intent', () => {
13
+ it('should respect ownership.key=stack when specified', () => {
14
+ const appDefinition = {
15
+ encryption: {
16
+ ownership: { key: 'stack' }
17
+ }
18
+ };
19
+ const discovery = createEmptyDiscoveryResult();
20
+
21
+ const decision = resolver.resolveKey(appDefinition, discovery);
22
+
23
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
24
+ expect(decision.physicalId).toBeNull();
25
+ expect(decision.reason).toContain('Will create FriggKMSKey in stack');
26
+ });
27
+
28
+ it('should respect ownership.key=external when KMS key discovered', () => {
29
+ const appDefinition = {
30
+ encryption: {
31
+ ownership: { key: 'external' }
32
+ }
33
+ };
34
+ const discovery = createEmptyDiscoveryResult();
35
+ discovery.external.push({
36
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/abcd-1234',
37
+ resourceType: 'AWS::KMS::Key',
38
+ source: 'aws-discovery'
39
+ });
40
+
41
+ const decision = resolver.resolveKey(appDefinition, discovery);
42
+
43
+ expect(decision.ownership).toBe(ResourceOwnership.EXTERNAL);
44
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/abcd-1234');
45
+ expect(decision.reason).toContain('external');
46
+ });
47
+
48
+ it('should error when ownership.key=external but no KMS key discovered', () => {
49
+ const appDefinition = {
50
+ encryption: {
51
+ ownership: { key: 'external' }
52
+ }
53
+ };
54
+ const discovery = createEmptyDiscoveryResult();
55
+
56
+ expect(() => resolver.resolveKey(appDefinition, discovery))
57
+ .toThrow('ownership.key=external but no KMS key discovered');
58
+ });
59
+ });
60
+
61
+ describe('Auto resolution (ownership.key=auto)', () => {
62
+ it('should use stack KMS key when found in CloudFormation', () => {
63
+ const appDefinition = {
64
+ encryption: {
65
+ ownership: { key: 'auto' }
66
+ }
67
+ };
68
+ const discovery = createEmptyDiscoveryResult();
69
+ discovery.fromCloudFormation = true;
70
+ discovery.stackManaged.push({
71
+ logicalId: 'FriggKMSKey',
72
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/stack-key',
73
+ resourceType: 'AWS::KMS::Key'
74
+ });
75
+
76
+ const decision = resolver.resolveKey(appDefinition, discovery);
77
+
78
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
79
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/stack-key');
80
+ expect(decision.reason).toContain('Found FriggKMSKey in CloudFormation stack');
81
+ });
82
+
83
+ it('should use external KMS key when found via discovery', () => {
84
+ const appDefinition = {
85
+ encryption: {
86
+ ownership: { key: 'auto' }
87
+ }
88
+ };
89
+ const discovery = createEmptyDiscoveryResult();
90
+ discovery.external.push({
91
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/external-key',
92
+ resourceType: 'AWS::KMS::Key',
93
+ source: 'aws-discovery'
94
+ });
95
+
96
+ const decision = resolver.resolveKey(appDefinition, discovery);
97
+
98
+ expect(decision.ownership).toBe(ResourceOwnership.EXTERNAL);
99
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/external-key');
100
+ expect(decision.reason).toContain('Found external KMS key via discovery');
101
+ });
102
+
103
+ it('should create new KMS key when none found', () => {
104
+ const appDefinition = {
105
+ encryption: {
106
+ ownership: { key: 'auto' }
107
+ }
108
+ };
109
+ const discovery = createEmptyDiscoveryResult();
110
+
111
+ const decision = resolver.resolveKey(appDefinition, discovery);
112
+
113
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
114
+ expect(decision.physicalId).toBeNull();
115
+ expect(decision.reason).toContain('No existing KMS key - will create in stack');
116
+ });
117
+ });
118
+
119
+ describe('Default behavior (no ownership specified)', () => {
120
+ it('should default to auto resolution', () => {
121
+ const appDefinition = {
122
+ encryption: {
123
+ fieldLevelEncryptionMethod: 'kms'
124
+ // No ownership specified
125
+ }
126
+ };
127
+ const discovery = createEmptyDiscoveryResult();
128
+
129
+ const decision = resolver.resolveKey(appDefinition, discovery);
130
+
131
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
132
+ expect(decision.reason).toContain('No existing KMS key - will create in stack');
133
+ });
134
+ });
135
+ });
136
+
137
+ describe('resolveAll', () => {
138
+ it('should return decisions for all KMS resources', () => {
139
+ const appDefinition = {
140
+ encryption: {
141
+ fieldLevelEncryptionMethod: 'kms'
142
+ }
143
+ };
144
+ const discovery = createEmptyDiscoveryResult();
145
+
146
+ const decisions = resolver.resolveAll(appDefinition, discovery);
147
+
148
+ expect(decisions).toHaveProperty('key');
149
+ expect(decisions.key.ownership).toBe(ResourceOwnership.STACK);
150
+ });
151
+ });
152
+
153
+ describe('Real-world scenarios', () => {
154
+ it('should handle managementMode=managed scenario (create KMS)', () => {
155
+ // In managed mode, we want to create KMS in stack
156
+ const appDefinition = {
157
+ managementMode: 'managed',
158
+ encryption: {
159
+ fieldLevelEncryptionMethod: 'kms',
160
+ ownership: { key: 'stack' }
161
+ }
162
+ };
163
+ const discovery = createEmptyDiscoveryResult();
164
+
165
+ const decision = resolver.resolveKey(appDefinition, discovery);
166
+
167
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
168
+ expect(decision.physicalId).toBeNull();
169
+ });
170
+
171
+ it('should handle existing stack KMS key (reuse)', () => {
172
+ // Stack already has KMS from previous deployment
173
+ const appDefinition = {
174
+ encryption: {
175
+ fieldLevelEncryptionMethod: 'kms',
176
+ ownership: { key: 'auto' }
177
+ }
178
+ };
179
+ const discovery = createEmptyDiscoveryResult();
180
+ discovery.fromCloudFormation = true;
181
+ discovery.stackManaged.push({
182
+ logicalId: 'FriggKMSKey',
183
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/existing-stack-key',
184
+ resourceType: 'AWS::KMS::Key'
185
+ });
186
+
187
+ const decision = resolver.resolveKey(appDefinition, discovery);
188
+
189
+ expect(decision.ownership).toBe(ResourceOwnership.STACK);
190
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/existing-stack-key');
191
+ });
192
+
193
+ it('should handle shared KMS key scenario (vpcIsolation=shared)', () => {
194
+ // Using shared infrastructure KMS key
195
+ const appDefinition = {
196
+ managementMode: 'managed',
197
+ vpcIsolation: 'shared',
198
+ encryption: {
199
+ fieldLevelEncryptionMethod: 'kms',
200
+ ownership: { key: 'auto' }
201
+ }
202
+ };
203
+ const discovery = createEmptyDiscoveryResult();
204
+ discovery.external.push({
205
+ physicalId: 'arn:aws:kms:us-east-1:123456789012:key/shared-key',
206
+ resourceType: 'AWS::KMS::Key',
207
+ source: 'aws-discovery'
208
+ });
209
+
210
+ const decision = resolver.resolveKey(appDefinition, discovery);
211
+
212
+ expect(decision.ownership).toBe(ResourceOwnership.EXTERNAL);
213
+ expect(decision.physicalId).toBe('arn:aws:kms:us-east-1:123456789012:key/shared-key');
214
+ });
215
+ });
216
+ });