@friggframework/devtools 2.0.0--canary.643.45b1fbc.0 → 2.0.0--canary.636.7dc8dca.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.
@@ -1,356 +0,0 @@
1
- /**
2
- * ADR-033: Aurora Serverless v2 scale-to-zero + NAT-free public connectivity
3
- *
4
- * These tests assert on the GENERATED CloudFormation template (via the full
5
- * composer) plus the AuroraBuilder validator. They are deterministic and require
6
- * neither a live AWS account nor Prisma client generation:
7
- * - Validator cases call AuroraBuilder.validate() directly (pure, no I/O).
8
- * - Template cases use vpc.management='create-new' + database.postgres
9
- * management='managed', which resolve to STACK ownership without depending on
10
- * any discovered AWS resource, so cloud discovery returning empty is fine.
11
- *
12
- * Both new capabilities are opt-in and default-off; the final describe block
13
- * guards the no-regression promise: a default (vpc, no minCapacity) definition
14
- * composes the same template as before.
15
- */
16
-
17
- const { composeServerlessDefinition } = require('../infrastructure-composer');
18
- const { AuroraBuilder } = require('../domains/database/aurora-builder');
19
-
20
- // Shared: an app definition that creates a fresh VPC + a Frigg-owned Aurora
21
- // cluster in-stack, with overridable postgres config.
22
- function makeApp(postgresOverrides = {}) {
23
- return {
24
- name: 'adr033-app',
25
- provider: 'aws',
26
- region: 'us-east-1',
27
- integrations: [],
28
- vpc: { enable: true, management: 'create-new' },
29
- database: {
30
- postgres: {
31
- enable: true,
32
- management: 'managed',
33
- ...postgresOverrides,
34
- },
35
- },
36
- };
37
- }
38
-
39
- function findResources(template, predicate) {
40
- return Object.entries(template.resources.Resources).filter(([, r]) => predicate(r));
41
- }
42
-
43
- describe('ADR-033: Aurora scale-to-zero + connectivity', () => {
44
- beforeAll(() => {
45
- process.env.AWS_REGION = 'us-east-1';
46
- // Intentionally NOT setting FRIGG_SKIP_AWS_DISCOVERY — the builders must
47
- // execute. create-new/managed resolve to STACK without needing discovery.
48
- });
49
-
50
- afterAll(() => {
51
- delete process.env.AWS_REGION;
52
- });
53
-
54
- // ---------------------------------------------------------------------
55
- // Validator
56
- // ---------------------------------------------------------------------
57
- describe('validator (AuroraBuilder.validate)', () => {
58
- const build = new AuroraBuilder();
59
- // ValidationResult exposes hasErrors(); "valid" means no errors.
60
- // vpc.enable defaults to true so connectivity:'public' cases don't trip the
61
- // "public requires vpc.enable" rule; pass appOverrides to change it.
62
- const validateResult = (postgres, appOverrides = {}) =>
63
- build.validate({
64
- vpc: { enable: true },
65
- database: { postgres: { enable: true, ...postgres } },
66
- ...appOverrides,
67
- });
68
- const isValid = (postgres, appOverrides) => !validateResult(postgres, appOverrides).hasErrors();
69
-
70
- test('accepts minCapacity: 0 (scale-to-zero)', () => {
71
- expect(isValid({ minCapacity: 0 })).toBe(true);
72
- });
73
-
74
- test('rejects minCapacity: 0.3 (inside the forbidden (0, 0.5) band)', () => {
75
- const r = validateResult({ minCapacity: 0.3 });
76
- expect(r.hasErrors()).toBe(true);
77
- expect(r.errors.join(' ')).toMatch(/minCapacity must be 0 \(scale-to-zero\) or between 0\.5 and 128/);
78
- });
79
-
80
- test('accepts minCapacity: 0.5 and minCapacity: 64', () => {
81
- expect(isValid({ minCapacity: 0.5 })).toBe(true);
82
- expect(isValid({ minCapacity: 64 })).toBe(true);
83
- });
84
-
85
- test('rejects secondsUntilAutoPause: 100 (below 300)', () => {
86
- const r = validateResult({ minCapacity: 0, secondsUntilAutoPause: 100 });
87
- expect(r.hasErrors()).toBe(true);
88
- expect(r.errors.join(' ')).toMatch(/secondsUntilAutoPause must be an integer between 300 and 86400/);
89
- });
90
-
91
- test('accepts secondsUntilAutoPause: 3600', () => {
92
- expect(isValid({ minCapacity: 0, secondsUntilAutoPause: 3600 })).toBe(true);
93
- });
94
-
95
- test("accepts connectivity: 'public'", () => {
96
- expect(isValid({ connectivity: 'public' })).toBe(true);
97
- });
98
-
99
- test("rejects connectivity: 'nope'", () => {
100
- const r = validateResult({ connectivity: 'nope' });
101
- expect(r.hasErrors()).toBe(true);
102
- expect(r.errors.join(' ')).toMatch(/Invalid database\.postgres\.connectivity/);
103
- });
104
-
105
- test('rejects non-array / non-CIDR allowedCidrs, accepts valid CIDRs', () => {
106
- expect(isValid({ allowedCidrs: 'nope' })).toBe(false);
107
- expect(isValid({ allowedCidrs: ['not-a-cidr'] })).toBe(false);
108
- expect(isValid({ allowedCidrs: ['10.0.0.0/8', '203.0.113.5/32'] })).toBe(true);
109
- });
110
-
111
- test('rejects out-of-range CIDR octets/prefix (numeric, not just shape)', () => {
112
- expect(isValid({ allowedCidrs: ['999.999.999.999/99'] })).toBe(false);
113
- expect(isValid({ allowedCidrs: ['10.0.0.0/33'] })).toBe(false);
114
- expect(isValid({ allowedCidrs: ['256.1.1.1/24'] })).toBe(false);
115
- expect(isValid({ allowedCidrs: ['0.0.0.0/0'] })).toBe(true);
116
- });
117
-
118
- test('rejects empty allowedCidrs in public mode (no silent full-internet fallback)', () => {
119
- const r = validateResult({ connectivity: 'public', allowedCidrs: [] });
120
- expect(r.hasErrors()).toBe(true);
121
- expect(r.errors.join(' ')).toMatch(/allowedCidrs is empty with connectivity="public"/);
122
- });
123
-
124
- test('rejects minCapacity > maxCapacity', () => {
125
- const r = validateResult({ minCapacity: 8, maxCapacity: 4 });
126
- expect(r.hasErrors()).toBe(true);
127
- expect(r.errors.join(' ')).toMatch(/minCapacity \(8\) must be <= maxCapacity \(4\)/);
128
- // equal is fine
129
- expect(isValid({ minCapacity: 4, maxCapacity: 4 })).toBe(true);
130
- });
131
-
132
- test("connectivity:'public' requires vpc.enable=true", () => {
133
- const r = validateResult({ connectivity: 'public' }, { vpc: { enable: false } });
134
- expect(r.hasErrors()).toBe(true);
135
- expect(r.errors.join(' ')).toMatch(/connectivity="public" requires vpc\.enable=true/);
136
- // With vpc.enable true (default helper), it's valid
137
- expect(isValid({ connectivity: 'public' })).toBe(true);
138
- });
139
-
140
- test('warns when secondsUntilAutoPause set with minCapacity !== 0', () => {
141
- const r = validateResult({ minCapacity: 0.5, secondsUntilAutoPause: 3600 });
142
- expect(r.hasErrors()).toBe(false);
143
- expect(r.warnings.join(' ')).toMatch(/secondsUntilAutoPause is ignored unless minCapacity is 0/);
144
- // No such warning when minCapacity is 0
145
- const r0 = validateResult({ minCapacity: 0, secondsUntilAutoPause: 3600 });
146
- expect(r0.warnings.join(' ')).not.toMatch(/ignored unless minCapacity is 0/);
147
- });
148
-
149
- test("warns (not errors) for connectivity:'public' with discover/use-existing management", () => {
150
- const rDiscover = validateResult({ connectivity: 'public', management: 'discover' });
151
- expect(rDiscover.hasErrors()).toBe(false);
152
- expect(rDiscover.warnings.join(' ')).toMatch(/assumes the EXISTING Aurora cluster is already publicly accessible/);
153
-
154
- const rUseExisting = validateResult({
155
- connectivity: 'public',
156
- management: 'use-existing',
157
- endpoint: 'db.example.com',
158
- });
159
- expect(rUseExisting.warnings.join(' ')).toMatch(/only affects TLS/);
160
- });
161
-
162
- test('warns (does not fail) when minCapacity:0 with an older pinned engine version', () => {
163
- const r = validateResult({ minCapacity: 0, engineVersion: '15.4' });
164
- expect(r.hasErrors()).toBe(false); // warning, not error
165
- expect(r.warnings.join(' ')).toMatch(/may not support .*scale-to-zero/);
166
- });
167
-
168
- test('does not warn about engine when minCapacity:0 on a capable version', () => {
169
- const r = validateResult({ minCapacity: 0, engineVersion: '15.13' });
170
- expect(r.warnings.join(' ')).not.toMatch(/may not support .*scale-to-zero/);
171
- });
172
- });
173
-
174
- // ---------------------------------------------------------------------
175
- // Scale-to-zero (template shape)
176
- // ---------------------------------------------------------------------
177
- describe('scale-to-zero (minCapacity: 0)', () => {
178
- test('MinCapacity is exactly 0 and SecondsUntilAutoPause defaults to 300', async () => {
179
- const t = await composeServerlessDefinition(makeApp({ minCapacity: 0 }));
180
- const scaling = t.resources.Resources.FriggAuroraCluster.Properties.ServerlessV2ScalingConfiguration;
181
-
182
- // Mutation guard: with the old `|| 0.5` bug this would be 0.5, not 0.
183
- expect(scaling.MinCapacity).toBe(0);
184
- expect(scaling.MinCapacity).not.toBe(0.5);
185
- expect(scaling.SecondsUntilAutoPause).toBe(300);
186
- });
187
-
188
- test('SecondsUntilAutoPause honors a custom value', async () => {
189
- const t = await composeServerlessDefinition(makeApp({ minCapacity: 0, secondsUntilAutoPause: 1800 }));
190
- const scaling = t.resources.Resources.FriggAuroraCluster.Properties.ServerlessV2ScalingConfiguration;
191
- expect(scaling.MinCapacity).toBe(0);
192
- expect(scaling.SecondsUntilAutoPause).toBe(1800);
193
- });
194
-
195
- test('MaxCapacity defaults to 4 and is preserved with scale-to-zero', async () => {
196
- const t = await composeServerlessDefinition(makeApp({ minCapacity: 0 }));
197
- const scaling = t.resources.Resources.FriggAuroraCluster.Properties.ServerlessV2ScalingConfiguration;
198
- expect(scaling.MaxCapacity).toBe(4);
199
- });
200
- });
201
-
202
- // ---------------------------------------------------------------------
203
- // Public connectivity (template shape)
204
- // ---------------------------------------------------------------------
205
- describe("connectivity: 'public'", () => {
206
- test('Aurora ingress uses CidrIp — one rule per allowedCidr — not SourceSecurityGroupId', async () => {
207
- const t = await composeServerlessDefinition(
208
- makeApp({ connectivity: 'public', allowedCidrs: ['10.1.0.0/16', '203.0.113.7/32'] })
209
- );
210
- const ingress = findResources(
211
- t,
212
- (r) => r.Type === 'AWS::EC2::SecurityGroupIngress' && r.Properties.FromPort === 5432
213
- );
214
- expect(ingress).toHaveLength(2);
215
- const cidrs = ingress.map(([, r]) => r.Properties.CidrIp).sort();
216
- expect(cidrs).toEqual(['10.1.0.0/16', '203.0.113.7/32']);
217
- ingress.forEach(([, r]) => {
218
- expect(r.Properties.CidrIp).toBeDefined();
219
- expect(r.Properties.SourceSecurityGroupId).toBeUndefined();
220
- });
221
- });
222
-
223
- test('allowedCidrs defaults to 0.0.0.0/0 when omitted', async () => {
224
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public' }));
225
- const ingress = findResources(
226
- t,
227
- (r) => r.Type === 'AWS::EC2::SecurityGroupIngress' && r.Properties.FromPort === 5432
228
- );
229
- expect(ingress).toHaveLength(1);
230
- expect(ingress[0][1].Properties.CidrIp).toBe('0.0.0.0/0');
231
- });
232
-
233
- test('Aurora instance is PubliclyAccessible and cluster sits in public subnets', async () => {
234
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public' }));
235
- expect(t.resources.Resources.FriggAuroraInstance.Properties.PubliclyAccessible).toBe(true);
236
- expect(t.resources.Resources.FriggDBSubnetGroup.Properties.SubnetIds).toEqual([
237
- { Ref: 'FriggPublicSubnet' },
238
- { Ref: 'FriggPublicSubnet2' },
239
- ]);
240
- });
241
-
242
- test('NO NAT Gateway resource is emitted', async () => {
243
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public' }));
244
- const nats = findResources(t, (r) => r.Type === 'AWS::EC2::NatGateway');
245
- expect(nats).toHaveLength(0);
246
- });
247
-
248
- test('public subnets get an IGW default route + route table + both associations (create-new VPC)', async () => {
249
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public' }));
250
- const R = t.resources.Resources;
251
-
252
- // Public route table
253
- expect(R.FriggPublicRouteTable).toBeDefined();
254
- expect(R.FriggPublicRouteTable.Type).toBe('AWS::EC2::RouteTable');
255
-
256
- // 0.0.0.0/0 -> Internet Gateway default route
257
- const igwRoutes = findResources(
258
- t,
259
- (r) =>
260
- r.Type === 'AWS::EC2::Route' &&
261
- r.Properties.DestinationCidrBlock === '0.0.0.0/0' &&
262
- r.Properties.GatewayId &&
263
- r.Properties.GatewayId.Ref === 'FriggInternetGateway'
264
- );
265
- expect(igwRoutes).toHaveLength(1);
266
-
267
- // Both public subnet associations
268
- expect(R.FriggPublicSubnet1RouteTableAssociation).toBeDefined();
269
- expect(R.FriggPublicSubnet2RouteTableAssociation).toBeDefined();
270
- expect(R.FriggPublicSubnet1RouteTableAssociation.Properties.RouteTableId).toEqual({
271
- Ref: 'FriggPublicRouteTable',
272
- });
273
-
274
- // And there must be NO NAT route (that would imply a NAT default route)
275
- const natRoutes = findResources(
276
- t,
277
- (r) => r.Type === 'AWS::EC2::Route' && r.Properties.NatGatewayId
278
- );
279
- expect(natRoutes).toHaveLength(0);
280
- });
281
-
282
- test('VPC_ENABLED is false in public mode (Lambda not VPC-attached)', async () => {
283
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public' }));
284
- expect(t.provider.environment.VPC_ENABLED).toBe('false');
285
- });
286
-
287
- test('Lambda is NOT attached to the VPC (provider.vpc unset)', async () => {
288
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public' }));
289
- expect(t.provider.vpc).toBeUndefined();
290
- });
291
-
292
- test('DATABASE_URL enforces TLS (sslmode=require)', async () => {
293
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public' }));
294
- const url = t.provider.environment.DATABASE_URL;
295
- expect(url['Fn::Sub'][0]).toContain('sslmode=require');
296
- });
297
-
298
- test('combines with scale-to-zero: $0-idle NAT-free Aurora', async () => {
299
- const t = await composeServerlessDefinition(makeApp({ connectivity: 'public', minCapacity: 0 }));
300
- const scaling = t.resources.Resources.FriggAuroraCluster.Properties.ServerlessV2ScalingConfiguration;
301
- expect(scaling.MinCapacity).toBe(0);
302
- expect(scaling.SecondsUntilAutoPause).toBe(300);
303
- expect(findResources(t, (r) => r.Type === 'AWS::EC2::NatGateway')).toHaveLength(0);
304
- expect(t.provider.vpc).toBeUndefined();
305
- });
306
- });
307
-
308
- // ---------------------------------------------------------------------
309
- // No-regression: default (vpc) connectivity, no new fields
310
- // ---------------------------------------------------------------------
311
- describe("default connectivity: 'vpc' (no regression)", () => {
312
- test('MinCapacity defaults to 0.5 with no SecondsUntilAutoPause', async () => {
313
- const t = await composeServerlessDefinition(makeApp());
314
- const scaling = t.resources.Resources.FriggAuroraCluster.Properties.ServerlessV2ScalingConfiguration;
315
- expect(scaling.MinCapacity).toBe(0.5);
316
- expect(scaling.MaxCapacity).toBe(4);
317
- expect(scaling.SecondsUntilAutoPause).toBeUndefined();
318
- });
319
-
320
- test('Aurora ingress uses SourceSecurityGroupId (Lambda SG), not CidrIp', async () => {
321
- const t = await composeServerlessDefinition(makeApp());
322
- const ingress = findResources(
323
- t,
324
- (r) => r.Type === 'AWS::EC2::SecurityGroupIngress' && r.Properties.FromPort === 5432
325
- );
326
- expect(ingress).toHaveLength(1);
327
- expect(ingress[0][1].Properties.SourceSecurityGroupId).toEqual({ Ref: 'FriggLambdaSecurityGroup' });
328
- expect(ingress[0][1].Properties.CidrIp).toBeUndefined();
329
- // Logical ID unchanged for the vpc path
330
- expect(ingress[0][0]).toBe('FriggAuroraIngressRule');
331
- });
332
-
333
- test('Aurora instance is not publicly accessible by default', async () => {
334
- const t = await composeServerlessDefinition(makeApp());
335
- expect(t.resources.Resources.FriggAuroraInstance.Properties.PubliclyAccessible).toBe(false);
336
- });
337
-
338
- test('Lambda IS attached to the VPC (provider.vpc set)', async () => {
339
- const t = await composeServerlessDefinition(makeApp());
340
- expect(t.provider.vpc).toBeDefined();
341
- expect(t.provider.vpc.subnetIds).toBeDefined();
342
- expect(t.provider.vpc.securityGroupIds).toBeDefined();
343
- });
344
-
345
- test('DATABASE_URL does not add sslmode in vpc mode', async () => {
346
- const t = await composeServerlessDefinition(makeApp());
347
- const url = t.provider.environment.DATABASE_URL;
348
- expect(url['Fn::Sub'][0]).not.toContain('sslmode=require');
349
- });
350
-
351
- test('VPC_ENABLED stays true in vpc mode', async () => {
352
- const t = await composeServerlessDefinition(makeApp());
353
- expect(t.provider.environment.VPC_ENABLED).toBe('true');
354
- });
355
- });
356
- });