@friggframework/devtools 2.0.0-next.123 → 2.0.0-next.125

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 (26) hide show
  1. package/frigg-cli/README.md +38 -1
  2. package/frigg-cli/__tests__/unit/commands/build.test.js +3 -0
  3. package/frigg-cli/__tests__/unit/commands/deploy.test.js +3 -0
  4. package/frigg-cli/__tests__/unit/commands/validate-wiring.test.js +92 -0
  5. package/frigg-cli/build-command/index.js +6 -0
  6. package/frigg-cli/deploy-command/index.js +6 -0
  7. package/frigg-cli/index.js +13 -1
  8. package/frigg-cli/init-command/backend-first-handler.js +48 -47
  9. package/frigg-cli/start-command/index.js +4 -0
  10. package/frigg-cli/start-command/start-command.test.js +3 -0
  11. package/frigg-cli/validate-command/__tests__/init-validation.test.js +63 -0
  12. package/frigg-cli/validate-command/__tests__/integrations-checks.test.js +133 -0
  13. package/frigg-cli/validate-command/__tests__/validate-app-definition.test.js +443 -0
  14. package/frigg-cli/validate-command/__tests__/validate-command.test.js +262 -0
  15. package/frigg-cli/validate-command/checks/config.js +504 -0
  16. package/frigg-cli/validate-command/checks/integrations.js +274 -0
  17. package/frigg-cli/validate-command/format-report.js +58 -0
  18. package/frigg-cli/validate-command/index.js +107 -0
  19. package/frigg-cli/validate-command/load-app-definition.js +105 -0
  20. package/frigg-cli/validate-command/schema-issues.js +154 -0
  21. package/frigg-cli/validate-command/validate-app-definition.js +60 -0
  22. package/infrastructure/app-definition-schema-contract.test.js +629 -0
  23. package/infrastructure/create-frigg-infrastructure.js +4 -1
  24. package/infrastructure/create-frigg-infrastructure.test.js +51 -0
  25. package/infrastructure/docs/POSTGRES-CONFIGURATION.md +60 -98
  26. package/package.json +7 -7
@@ -0,0 +1,443 @@
1
+ const { validateDefinition } = require('../validate-app-definition');
2
+
3
+ class Api {
4
+ static requesterType = 'oauth2';
5
+ }
6
+ const auth = {
7
+ getToken: async () => {},
8
+ getEntityDetails: async () => {},
9
+ getCredentialDetails: async () => {},
10
+ testAuthRequest: async () => {},
11
+ apiPropertiesToPersist: { credential: [], entity: [] },
12
+ };
13
+ class AcmeIntegration {
14
+ static Definition = {
15
+ name: 'acme',
16
+ version: '1.0.0',
17
+ modules: {
18
+ acme: {
19
+ definition: {
20
+ moduleName: 'acme',
21
+ API: Api,
22
+ requiredAuthMethods: auth,
23
+ },
24
+ },
25
+ },
26
+ };
27
+ }
28
+
29
+ /** The app `frigg init` scaffolds, with one integration installed. */
30
+ const scaffold = (overrides = {}) => ({
31
+ name: 'frigg-app',
32
+ integrations: [AcmeIntegration],
33
+ user: {
34
+ usePassword: true,
35
+ primary: 'individual',
36
+ individualUserRequired: true,
37
+ organizationUserRequired: false,
38
+ },
39
+ encryption: { fieldLevelEncryptionMethod: 'kms' },
40
+ managementMode: 'managed',
41
+ vpcIsolation: 'isolated',
42
+ vpc: { enable: false },
43
+ database: {
44
+ postgres: {
45
+ enable: true,
46
+ management: 'external',
47
+ },
48
+ },
49
+ environment: { DATABASE_URL: true },
50
+ logging: { retentionInDays: 14 },
51
+ ssm: { enable: false },
52
+ ...overrides,
53
+ });
54
+
55
+ const summary = ({ errors, warnings }) => ({
56
+ errors: errors.map((i) => `${i.code} ${i.pointer}`),
57
+ warnings: warnings.map((i) => `${i.code} ${i.pointer}`),
58
+ });
59
+
60
+ describe('validateDefinition', () => {
61
+ it('passes a scaffolded app with an integration class', () => {
62
+ expect(
63
+ summary(
64
+ validateDefinition({
65
+ definition: scaffold(),
66
+ stage: 'prod',
67
+ env: {},
68
+ })
69
+ )
70
+ ).toEqual({ errors: [], warnings: [] });
71
+ });
72
+
73
+ it('passes the definition `frigg init` generates, on a deployed stage', () => {
74
+ const { Definition } = require('../../templates/backend/index.js');
75
+
76
+ expect(
77
+ summary(
78
+ validateDefinition({
79
+ definition: Definition,
80
+ stage: 'prod',
81
+ env: {},
82
+ })
83
+ )
84
+ ).toEqual({ errors: [], warnings: ['no-integrations /integrations'] });
85
+ });
86
+
87
+ it("accepts database.postgres.management 'external' and fieldLevelEncryptionMethod 'none'", () => {
88
+ expect(
89
+ summary(
90
+ validateDefinition({
91
+ definition: scaffold({
92
+ encryption: { fieldLevelEncryptionMethod: 'none' },
93
+ }),
94
+ stage: 'prod',
95
+ env: {},
96
+ })
97
+ )
98
+ ).toEqual({ errors: [], warnings: [] });
99
+ });
100
+
101
+ it('reports schema errors with JSON pointers and fix hints', () => {
102
+ const result = validateDefinition({
103
+ definition: scaffold({
104
+ vpc: { enable: false, mangement: 'discover' },
105
+ logging: { level: 'loud' },
106
+ }),
107
+ env: {},
108
+ });
109
+
110
+ expect(result.errors).toEqual([
111
+ expect.objectContaining({
112
+ code: 'unknown-key',
113
+ pointer: '/vpc/mangement',
114
+ hint: 'Did you mean "management"?',
115
+ }),
116
+ expect.objectContaining({
117
+ code: 'invalid-value',
118
+ pointer: '/logging/level',
119
+ hint: expect.stringContaining('"debug"'),
120
+ }),
121
+ ]);
122
+ });
123
+
124
+ it("rejects database.postgres.management 'existing', which the Aurora builder rejects", () => {
125
+ const definition = scaffold({
126
+ managementMode: undefined,
127
+ database: { postgres: { enable: true, management: 'existing' } },
128
+ vpc: { enable: true },
129
+ });
130
+
131
+ expect(
132
+ summary(validateDefinition({ definition, env: {} })).errors
133
+ ).toEqual(['invalid-value /database/postgres/management']);
134
+ });
135
+
136
+ it('reports a broken integration class', () => {
137
+ class Broken {
138
+ static Definition = { name: 'broken' };
139
+ }
140
+ const result = validateDefinition({
141
+ definition: scaffold({ integrations: [Broken] }),
142
+ env: {},
143
+ });
144
+
145
+ expect(summary(result).errors).toEqual([
146
+ 'integration-modules-missing /integrations/0/Definition/modules',
147
+ ]);
148
+ });
149
+
150
+ it('reports an integration that is not a class once', () => {
151
+ const result = validateDefinition({
152
+ definition: scaffold({ integrations: ['acme'] }),
153
+ env: {},
154
+ });
155
+
156
+ expect(summary(result).errors).toEqual([
157
+ 'integration-not-a-class /integrations/0',
158
+ ]);
159
+ });
160
+
161
+ it('reports a missing database', () => {
162
+ const result = validateDefinition({
163
+ definition: scaffold({ database: undefined }),
164
+ env: {},
165
+ });
166
+
167
+ expect(summary(result).errors).toEqual(['no-database /database']);
168
+ });
169
+
170
+ it('a non-object definition is a schema error, not a crash', () => {
171
+ expect(
172
+ summary(validateDefinition({ definition: 'nope', env: {} })).errors
173
+ ).toEqual(['invalid-type ']);
174
+ });
175
+ });
176
+
177
+ describe('semantic checks', () => {
178
+ const run = (overrides, options = {}) =>
179
+ summary(
180
+ validateDefinition({
181
+ definition: scaffold(overrides),
182
+ env: {},
183
+ ...options,
184
+ })
185
+ );
186
+
187
+ describe('encryption', () => {
188
+ it("requires AES_KEY_ID and AES_KEY in environment for 'aes'", () => {
189
+ expect(
190
+ run({ encryption: { fieldLevelEncryptionMethod: 'aes' } })
191
+ .errors
192
+ ).toEqual([
193
+ 'missing-encryption-env /environment/AES_KEY_ID',
194
+ 'missing-encryption-env /environment/AES_KEY',
195
+ ]);
196
+ });
197
+
198
+ it('warns when the AES values are missing from the deploying shell', () => {
199
+ const result = run(
200
+ {
201
+ encryption: { fieldLevelEncryptionMethod: 'aes' },
202
+ environment: {
203
+ DATABASE_URL: true,
204
+ AES_KEY_ID: true,
205
+ AES_KEY: true,
206
+ },
207
+ },
208
+ { stage: 'prod', env: { AES_KEY_ID: 'k1' } }
209
+ );
210
+ expect(result.errors).toEqual([]);
211
+ expect(result.warnings).toContain(
212
+ 'missing-env-value /environment/AES_KEY'
213
+ );
214
+ });
215
+
216
+ it('warns that a deployed stage without a method stores plaintext', () => {
217
+ expect(
218
+ run({ encryption: undefined }, { stage: 'prod' }).warnings
219
+ ).toContain(
220
+ 'no-field-encryption /encryption/fieldLevelEncryptionMethod'
221
+ );
222
+ });
223
+
224
+ it("does not warn about plaintext when 'none' opts out explicitly", () => {
225
+ const result = run(
226
+ { encryption: { fieldLevelEncryptionMethod: 'none' } },
227
+ { stage: 'prod' }
228
+ );
229
+ expect(result.errors).toEqual([]);
230
+ expect(result.warnings).not.toContain(
231
+ 'no-field-encryption /encryption/fieldLevelEncryptionMethod'
232
+ );
233
+ expect(
234
+ run(
235
+ { encryption: { fieldLevelEncryptionMethod: 'none' } },
236
+ { stage: 'dev' }
237
+ ).warnings
238
+ ).not.toContain(
239
+ 'encryption-bypassed /encryption/fieldLevelEncryptionMethod'
240
+ );
241
+ });
242
+
243
+ it('warns that stage dev never encrypts', () => {
244
+ expect(run({}, { stage: 'dev' }).warnings).toContain(
245
+ 'encryption-bypassed /encryption/fieldLevelEncryptionMethod'
246
+ );
247
+ });
248
+
249
+ it('warns about the KMS key fallback outside managed mode', () => {
250
+ expect(
251
+ run({ managementMode: undefined, vpcIsolation: undefined })
252
+ .warnings
253
+ ).toContain('kms-key-source /encryption/createResourceIfNoneFound');
254
+ });
255
+ });
256
+
257
+ describe('user', () => {
258
+ it('rejects a user config with which login always fails', () => {
259
+ expect(
260
+ run({ user: { individualUserRequired: false } }).errors
261
+ ).toEqual(['no-login /user']);
262
+ });
263
+
264
+ it('warns an organization-only app that individualUserRequired now defaults to true', () => {
265
+ expect(
266
+ run({ user: { organizationUserRequired: true } }).warnings
267
+ ).toContain('default-changed /user/individualUserRequired');
268
+ });
269
+
270
+ it('rejects adopterJwt without a secret and an app with every auth mode off', () => {
271
+ expect(
272
+ run({
273
+ user: {
274
+ authModes: {
275
+ friggToken: false,
276
+ sharedSecret: false,
277
+ adopterJwt: false,
278
+ },
279
+ },
280
+ }).errors
281
+ ).toEqual(['no-auth-mode /user/authModes']);
282
+ expect(
283
+ run({ user: { authModes: { adopterJwt: true } } }).errors
284
+ ).toEqual(['missing-key /user/jwtConfig/secret']);
285
+ expect(
286
+ run({
287
+ user: {
288
+ authModes: { adopterJwt: true },
289
+ jwtConfig: { userIdClaim: 'sub' },
290
+ },
291
+ }).errors
292
+ ).toEqual(['missing-key /user/jwtConfig/secret']);
293
+ });
294
+
295
+ it('only warns when jwtConfig.secret comes from an env var unset in this shell', () => {
296
+ // `secret: process.env.JWT_SECRET` with JWT_SECRET unset: build and
297
+ // deploy validate before .env is loaded, and the Lambda may still
298
+ // receive it at runtime.
299
+ const result = run({
300
+ user: {
301
+ authModes: { adopterJwt: true },
302
+ jwtConfig: { secret: undefined },
303
+ },
304
+ });
305
+ expect(result.errors).toEqual([]);
306
+ expect(result.warnings).toContain(
307
+ 'missing-env-value /user/jwtConfig/secret'
308
+ );
309
+ });
310
+
311
+ it('accepts adopterJwt with a secret', () => {
312
+ const result = run({
313
+ user: {
314
+ authModes: { adopterJwt: true },
315
+ jwtConfig: { secret: 's3cret' },
316
+ },
317
+ });
318
+ expect(result.errors).toEqual([]);
319
+ expect(result.warnings).not.toContain(
320
+ 'missing-env-value /user/jwtConfig/secret'
321
+ );
322
+ });
323
+
324
+ it('warns that user.password is the old spelling of usePassword', () => {
325
+ expect(run({ user: { password: true } }).warnings).toContain(
326
+ 'deprecated-alias /user/password'
327
+ );
328
+ });
329
+ });
330
+
331
+ describe('management and VPC', () => {
332
+ it("warns that 'managed' does not stop the VPC builder applying vpc.management", () => {
333
+ expect(
334
+ run({ vpc: { enable: true, management: 'discover' } }).warnings
335
+ ).toContain('conflicting-settings /vpc/management');
336
+ });
337
+
338
+ it("warns when 'managed' is used without vpcIsolation", () => {
339
+ expect(run({ vpcIsolation: undefined }).warnings).toContain(
340
+ 'ambiguous-default /vpcIsolation'
341
+ );
342
+ });
343
+
344
+ it('warns about subnet and NAT values the VPC builder ignores', () => {
345
+ const result = run({
346
+ managementMode: undefined,
347
+ vpc: {
348
+ enable: true,
349
+ subnets: { management: 'create-new' },
350
+ natGateway: { management: 'discover' },
351
+ },
352
+ });
353
+ expect(result.warnings).toEqual(
354
+ expect.arrayContaining([
355
+ 'no-effect /vpc/subnets/management',
356
+ 'no-effect /vpc/natGateway/management',
357
+ ])
358
+ );
359
+ });
360
+
361
+ it('requires the ids use-existing needs', () => {
362
+ const result = run({
363
+ managementMode: undefined,
364
+ vpc: {
365
+ enable: true,
366
+ management: 'use-existing',
367
+ subnets: { management: 'use-existing' },
368
+ },
369
+ database: {
370
+ postgres: { enable: true, management: 'use-existing' },
371
+ },
372
+ });
373
+ expect(result.errors).toEqual([
374
+ 'missing-key /vpc/vpcId',
375
+ 'missing-key /vpc/subnets/ids',
376
+ 'missing-key /database/postgres/endpoint',
377
+ ]);
378
+ });
379
+
380
+ it('warns that a private Aurora cluster is unreachable from Lambdas outside a VPC', () => {
381
+ expect(
382
+ run({
383
+ managementMode: undefined,
384
+ database: {
385
+ postgres: { enable: true, management: 'managed' },
386
+ },
387
+ }).warnings
388
+ ).toContain('unreachable-database /vpc/enable');
389
+ });
390
+
391
+ it.each(['managed', 'existing'])(
392
+ "warns about the unreachable cluster under managementMode '%s', which ignores 'use-existing'",
393
+ (managementMode) => {
394
+ expect(
395
+ run({
396
+ managementMode,
397
+ database: {
398
+ postgres: {
399
+ enable: true,
400
+ management: 'use-existing',
401
+ endpoint: 'db.example.com',
402
+ },
403
+ },
404
+ }).warnings
405
+ ).toContain('unreachable-database /vpc/enable');
406
+ }
407
+ );
408
+
409
+ it("does not warn when 'use-existing' is honoured, or for 'external'", () => {
410
+ expect(
411
+ run({
412
+ managementMode: undefined,
413
+ vpcIsolation: undefined,
414
+ database: {
415
+ postgres: {
416
+ enable: true,
417
+ management: 'use-existing',
418
+ endpoint: 'db.example.com',
419
+ },
420
+ },
421
+ }).warnings
422
+ ).not.toContain('unreachable-database /vpc/enable');
423
+ expect(run({}).warnings).not.toContain(
424
+ 'unreachable-database /vpc/enable'
425
+ );
426
+ });
427
+ });
428
+
429
+ it('warns about keys nothing reads', () => {
430
+ expect(
431
+ run({ custom: { appName: 'x' }, security: { cors: {} } }).warnings
432
+ ).toEqual(
433
+ expect.arrayContaining(['no-effect /security', 'no-effect /custom'])
434
+ );
435
+ });
436
+
437
+ it("warns that 'ssm' environment entries need ssm.enable", () => {
438
+ expect(
439
+ run({ environment: { DATABASE_URL: true, API_KEY: 'ssm' } })
440
+ .warnings
441
+ ).toContain('ssm-inactive /environment/API_KEY');
442
+ });
443
+ });
@@ -0,0 +1,262 @@
1
+ const fs = require('fs');
2
+ const os = require('os');
3
+ const path = require('path');
4
+
5
+ const { validateCommand, runValidation, preflightValidation } = require('..');
6
+
7
+ const GOOD_INDEX = `
8
+ class Api { static requesterType = 'apiKey'; }
9
+ const definition = {
10
+ moduleName: 'acme',
11
+ API: Api,
12
+ requiredAuthMethods: {
13
+ getEntityDetails: async () => {},
14
+ getCredentialDetails: async () => {},
15
+ testAuthRequest: async () => {},
16
+ apiPropertiesToPersist: { credential: ['api_key'], entity: [] },
17
+ },
18
+ };
19
+ class AcmeIntegration {
20
+ static Definition = { name: 'acme', version: '1.0.0', modules: { acme: { definition } } };
21
+ }
22
+ module.exports = {
23
+ Definition: {
24
+ name: 'frigg-app',
25
+ integrations: [AcmeIntegration],
26
+ user: { usePassword: true },
27
+ encryption: { fieldLevelEncryptionMethod: 'kms', createResourceIfNoneFound: true },
28
+ database: { postgres: { enable: true, management: 'use-existing', endpoint: 'db.example.com' } },
29
+ environment: { DATABASE_URL: true },
30
+ },
31
+ };
32
+ `;
33
+
34
+ const BAD_INDEX = `
35
+ module.exports = {
36
+ Definition: {
37
+ integrations: [{ name: 'not-a-class' }],
38
+ database: { postgres: { enable: true, management: 'create-new' } },
39
+ encrpytion: { fieldLevelEncryptionMethod: 'kms' },
40
+ },
41
+ };
42
+ `;
43
+
44
+ function backend(indexSource) {
45
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-validate-'));
46
+ fs.writeFileSync(path.join(dir, 'package.json'), '{"name":"backend"}');
47
+ fs.writeFileSync(path.join(dir, 'index.js'), indexSource);
48
+ return dir;
49
+ }
50
+
51
+ describe('frigg validate', () => {
52
+ const dirs = [];
53
+ let log;
54
+ let warn;
55
+ let error;
56
+ let exit;
57
+
58
+ beforeEach(() => {
59
+ log = jest.spyOn(console, 'log').mockImplementation(() => {});
60
+ warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
61
+ error = jest.spyOn(console, 'error').mockImplementation(() => {});
62
+ exit = jest.spyOn(process, 'exit').mockImplementation(() => {});
63
+ process.exitCode = undefined;
64
+ });
65
+
66
+ afterEach(() => {
67
+ jest.restoreAllMocks();
68
+ process.exitCode = undefined;
69
+ for (const dir of dirs.splice(0))
70
+ fs.rmSync(dir, { recursive: true, force: true });
71
+ });
72
+
73
+ const inBackend = (source) => {
74
+ const dir = backend(source);
75
+ dirs.push(dir);
76
+ jest.spyOn(process, 'cwd').mockReturnValue(dir);
77
+ return dir;
78
+ };
79
+
80
+ it('passes an app with integration classes and exits 0', async () => {
81
+ inBackend(GOOD_INDEX);
82
+
83
+ const report = await validateCommand({ stage: 'prod' });
84
+
85
+ expect(report.errors).toEqual([]);
86
+ expect(process.exitCode).toBeUndefined();
87
+ expect(log.mock.calls.flat().join('\n')).toContain(
88
+ 'The app definition is valid'
89
+ );
90
+ });
91
+
92
+ it('prints errors with pointers and hints and exits 1', async () => {
93
+ inBackend(BAD_INDEX);
94
+
95
+ const report = await validateCommand({});
96
+ const output = log.mock.calls.flat().join('\n');
97
+
98
+ expect(process.exitCode).toBe(1);
99
+ expect(report.errors.map((e) => e.pointer)).toEqual(
100
+ expect.arrayContaining([
101
+ '/encrpytion',
102
+ '/database/postgres/management',
103
+ '/integrations/0',
104
+ ])
105
+ );
106
+ expect(output).toContain('/encrpytion');
107
+ expect(output).toContain('Did you mean "encryption"?');
108
+ expect(output).toContain(
109
+ 'Use one of: "discover", "managed", "use-existing"'
110
+ );
111
+ });
112
+
113
+ it('--json prints a machine-readable report', async () => {
114
+ inBackend(BAD_INDEX);
115
+
116
+ await validateCommand({ json: true, stage: 'prod' });
117
+ const json = JSON.parse(log.mock.calls[0][0]);
118
+
119
+ expect(json).toEqual(
120
+ expect.objectContaining({
121
+ valid: false,
122
+ stage: 'prod',
123
+ errors: expect.arrayContaining([
124
+ expect.objectContaining({
125
+ pointer: '/encrpytion',
126
+ code: 'unknown-key',
127
+ severity: 'error',
128
+ }),
129
+ ]),
130
+ warnings: expect.any(Array),
131
+ })
132
+ );
133
+ });
134
+
135
+ it('reports a backend that fails to load', () => {
136
+ const dir = backend("require('./missing-file');");
137
+ dirs.push(dir);
138
+
139
+ const report = runValidation({ cwd: dir });
140
+
141
+ expect(report.errors).toEqual([
142
+ expect.objectContaining({
143
+ code: 'load-failed',
144
+ message: expect.stringContaining('missing-file'),
145
+ }),
146
+ ]);
147
+ });
148
+
149
+ it('reports a directory without a backend', () => {
150
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-empty-'));
151
+ dirs.push(dir);
152
+
153
+ expect(runValidation({ cwd: dir }).errors).toEqual([
154
+ expect.objectContaining({ code: 'backend-not-found' }),
155
+ ]);
156
+ });
157
+
158
+ it('finds the backend/ directory from the project root', () => {
159
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-root-'));
160
+ dirs.push(root);
161
+ fs.mkdirSync(path.join(root, 'backend'));
162
+ fs.writeFileSync(path.join(root, 'backend', 'package.json'), '{}');
163
+ fs.writeFileSync(path.join(root, 'backend', 'index.js'), GOOD_INDEX);
164
+
165
+ expect(runValidation({ cwd: root }).source).toBe(
166
+ path.join(root, 'backend', 'index.js')
167
+ );
168
+ });
169
+
170
+ it('stops at the nearest backend/ like core does, even without index.js', () => {
171
+ // Core's findNearestBackendPackageJson stops at the nearest
172
+ // backend/package.json, so build would fail there; validation must
173
+ // not pass by loading a farther app instead.
174
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-root-'));
175
+ dirs.push(root);
176
+ fs.mkdirSync(path.join(root, 'backend'));
177
+ fs.writeFileSync(path.join(root, 'backend', 'package.json'), '{}');
178
+ fs.writeFileSync(path.join(root, 'backend', 'index.js'), GOOD_INDEX);
179
+ const nested = path.join(root, 'apps', 'child');
180
+ fs.mkdirSync(path.join(nested, 'backend'), { recursive: true });
181
+ fs.writeFileSync(path.join(nested, 'backend', 'package.json'), '{}');
182
+
183
+ expect(runValidation({ cwd: nested }).errors).toEqual([
184
+ expect.objectContaining({
185
+ code: 'index-not-found',
186
+ message: expect.stringContaining(path.join(nested, 'backend')),
187
+ }),
188
+ ]);
189
+ });
190
+
191
+ it('reports an index.js without a Definition', () => {
192
+ const dir = backend('module.exports = {};');
193
+ dirs.push(dir);
194
+
195
+ expect(runValidation({ cwd: dir }).errors).toEqual([
196
+ expect.objectContaining({ code: 'definition-not-exported' }),
197
+ ]);
198
+ });
199
+
200
+ describe('preflightValidation', () => {
201
+ it('stops build/deploy on errors', () => {
202
+ inBackend(BAD_INDEX);
203
+
204
+ const proceed = preflightValidation({
205
+ command: 'deploy',
206
+ options: { stage: 'prod' },
207
+ failOnErrors: true,
208
+ });
209
+
210
+ expect(proceed).toBe(false);
211
+ expect(exit).toHaveBeenCalledWith(1);
212
+ expect(error.mock.calls.flat().join('\n')).toContain(
213
+ '--skip-validate'
214
+ );
215
+ });
216
+
217
+ it('lets build/deploy continue on a valid definition', () => {
218
+ inBackend(GOOD_INDEX);
219
+
220
+ expect(
221
+ preflightValidation({
222
+ command: 'build',
223
+ options: { stage: 'prod' },
224
+ failOnErrors: true,
225
+ })
226
+ ).toBe(true);
227
+ expect(exit).not.toHaveBeenCalled();
228
+ });
229
+
230
+ it('only warns for start', () => {
231
+ inBackend(BAD_INDEX);
232
+
233
+ expect(
234
+ preflightValidation({
235
+ command: 'start',
236
+ options: {},
237
+ failOnErrors: false,
238
+ })
239
+ ).toBe(true);
240
+ expect(exit).not.toHaveBeenCalled();
241
+ const output = log.mock.calls.flat().join('\n');
242
+ expect(output).toContain('warning');
243
+ expect(output).not.toMatch(/^\s+error /m);
244
+ });
245
+
246
+ it('--skip-validate skips validation, loudly', () => {
247
+ inBackend(BAD_INDEX);
248
+
249
+ expect(
250
+ preflightValidation({
251
+ command: 'deploy',
252
+ options: { skipValidate: true },
253
+ failOnErrors: true,
254
+ })
255
+ ).toBe(true);
256
+ expect(exit).not.toHaveBeenCalled();
257
+ expect(warn.mock.calls.flat().join('\n')).toContain(
258
+ 'VALIDATION SKIPPED'
259
+ );
260
+ });
261
+ });
262
+ });