@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.
- package/frigg-cli/README.md +38 -1
- package/frigg-cli/__tests__/unit/commands/build.test.js +3 -0
- package/frigg-cli/__tests__/unit/commands/deploy.test.js +3 -0
- package/frigg-cli/__tests__/unit/commands/validate-wiring.test.js +92 -0
- package/frigg-cli/build-command/index.js +6 -0
- package/frigg-cli/deploy-command/index.js +6 -0
- package/frigg-cli/index.js +13 -1
- package/frigg-cli/init-command/backend-first-handler.js +48 -47
- package/frigg-cli/start-command/index.js +4 -0
- package/frigg-cli/start-command/start-command.test.js +3 -0
- package/frigg-cli/validate-command/__tests__/init-validation.test.js +63 -0
- package/frigg-cli/validate-command/__tests__/integrations-checks.test.js +133 -0
- package/frigg-cli/validate-command/__tests__/validate-app-definition.test.js +443 -0
- package/frigg-cli/validate-command/__tests__/validate-command.test.js +262 -0
- package/frigg-cli/validate-command/checks/config.js +504 -0
- package/frigg-cli/validate-command/checks/integrations.js +274 -0
- package/frigg-cli/validate-command/format-report.js +58 -0
- package/frigg-cli/validate-command/index.js +107 -0
- package/frigg-cli/validate-command/load-app-definition.js +105 -0
- package/frigg-cli/validate-command/schema-issues.js +154 -0
- package/frigg-cli/validate-command/validate-app-definition.js +60 -0
- package/infrastructure/app-definition-schema-contract.test.js +629 -0
- package/infrastructure/create-frigg-infrastructure.js +4 -1
- package/infrastructure/create-frigg-infrastructure.test.js +51 -0
- package/infrastructure/docs/POSTGRES-CONFIGURATION.md +60 -98
- package/package.json +7 -7
package/frigg-cli/README.md
CHANGED
|
@@ -157,6 +157,41 @@ Found 5 modules matching "crm"
|
|
|
157
157
|
|
|
158
158
|
---
|
|
159
159
|
|
|
160
|
+
#### `frigg validate [options]`
|
|
161
|
+
|
|
162
|
+
Validate the app definition (`index.js`) before you build or deploy it (ADR-051).
|
|
163
|
+
|
|
164
|
+
**Usage:**
|
|
165
|
+
```bash
|
|
166
|
+
frigg validate # schema + runtime checks
|
|
167
|
+
frigg validate --stage prod # also the stage checks (encryption, deploy-time environment)
|
|
168
|
+
frigg validate --json # machine-readable report for CI
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**What it does:**
|
|
172
|
+
1. Loads `index.js` the way `frigg build` does (current directory, or the nearest `backend/`)
|
|
173
|
+
2. Validates it against the app-definition schema: unknown keys (with "did you mean"), types, and the values the infrastructure builders accept
|
|
174
|
+
3. Checks what the schema cannot: integration classes (static `Definition` with a valid, unique `name`, `version`, `modules`, and API module definitions that pass core's checks), an enabled database, management settings that conflict or lack ids, encryption (`aes` keys in `environment`, KMS key source, stages that never encrypt), a user config with which login works, `'ssm'` entries without `ssm.enable`, and keys that have no effect
|
|
175
|
+
4. Prints each problem with its JSON pointer and a fix; exits `1` when there are errors
|
|
176
|
+
|
|
177
|
+
**Example output:**
|
|
178
|
+
```
|
|
179
|
+
Validating /path/to/backend/index.js (stage: prod)
|
|
180
|
+
error /encrpytion Unknown key "encrpytion": nothing in Frigg reads it.
|
|
181
|
+
fix: Did you mean "encryption"?
|
|
182
|
+
error /database/postgres/management /database/postgres/management is "create-new", which is not an allowed value.
|
|
183
|
+
fix: Use one of: "discover", "managed", "use-existing".
|
|
184
|
+
warning /custom custom is not read by Frigg: it has no effect.
|
|
185
|
+
fix: Remove it.
|
|
186
|
+
✗ 2 error(s), 1 warning(s)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`--json` prints `{ valid, source, stage, errors, warnings }`; each issue is `{ severity, code, pointer, message, hint }`.
|
|
190
|
+
|
|
191
|
+
`frigg build` and `frigg deploy` run the same validation first and stop on errors (`--skip-validate` bypasses it, with a loud warning). `frigg start` runs it and only warns. `frigg init` validates the generated app the same way.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
160
195
|
#### `frigg start [options]`
|
|
161
196
|
|
|
162
197
|
Start local development server with hot reload.
|
|
@@ -170,7 +205,7 @@ frigg start --frontend
|
|
|
170
205
|
```
|
|
171
206
|
|
|
172
207
|
**What it does:**
|
|
173
|
-
1. Validates DATABASE_URL environment variable
|
|
208
|
+
1. Validates DATABASE_URL environment variable, and the app definition (`frigg validate`; problems are printed as warnings, `--skip-validate` skips it)
|
|
174
209
|
2. Detects database type (MongoDB or PostgreSQL)
|
|
175
210
|
3. Checks Prisma client generation status
|
|
176
211
|
4. Tests database connection
|
|
@@ -229,10 +264,12 @@ frigg deploy --region us-west-2
|
|
|
229
264
|
frigg deploy --force
|
|
230
265
|
frigg deploy --skip-env-validation
|
|
231
266
|
frigg deploy --skip-doctor # Skip health check (not recommended)
|
|
267
|
+
frigg deploy --skip-validate # Skip app definition validation (not recommended)
|
|
232
268
|
frigg deploy --no-interactive # CI/CD mode (no prompts, auto-repair safe issues)
|
|
233
269
|
```
|
|
234
270
|
|
|
235
271
|
**What it does:**
|
|
272
|
+
0. **Validates the app definition** (`frigg validate`) and stops on errors, unless `--skip-validate`
|
|
236
273
|
1. **Runs health check** (`frigg doctor`) to detect infrastructure issues
|
|
237
274
|
2. **Auto-repairs safe issues** (or prompts for confirmation in interactive mode)
|
|
238
275
|
3. **Fails if critical issues found** (unless `--skip-doctor` flag used)
|
|
@@ -14,6 +14,9 @@ jest.mock('child_process', () => ({
|
|
|
14
14
|
spawnSync: jest.fn()
|
|
15
15
|
}));
|
|
16
16
|
|
|
17
|
+
// The app definition validation step is tested in validate-command/.
|
|
18
|
+
jest.mock('../../../validate-command', () => ({ preflightValidation: jest.fn(() => true) }));
|
|
19
|
+
|
|
17
20
|
// Require after mocks
|
|
18
21
|
const { spawnSync } = require('child_process');
|
|
19
22
|
const { buildCommand } = require('../../../build-command');
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
// Mock dependencies BEFORE requiring modules
|
|
13
|
+
// The app definition validation step is tested in validate-command/.
|
|
14
|
+
jest.mock('../../../validate-command', () => ({ preflightValidation: jest.fn(() => true) }));
|
|
15
|
+
|
|
13
16
|
jest.mock('child_process', () => ({
|
|
14
17
|
spawn: jest.fn()
|
|
15
18
|
}));
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* build and deploy validate the app definition first and stop on errors;
|
|
3
|
+
* start validates and only warns (ADR-051).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// child_process and node:child_process are one module to jest: one mock.
|
|
7
|
+
const mockChildProcess = {
|
|
8
|
+
spawnSync: jest.fn(() => ({ status: 0 })),
|
|
9
|
+
spawn: jest.fn(),
|
|
10
|
+
};
|
|
11
|
+
jest.mock('child_process', () => mockChildProcess);
|
|
12
|
+
jest.mock('node:child_process', () => mockChildProcess);
|
|
13
|
+
jest.mock('../../../validate-command', () => ({
|
|
14
|
+
preflightValidation: jest.fn(),
|
|
15
|
+
}));
|
|
16
|
+
jest.mock('../../../doctor-command', () => ({ doctorCommand: jest.fn() }));
|
|
17
|
+
jest.mock('../../../utils/database-validator', () => ({
|
|
18
|
+
validateDatabaseUrl: jest.fn(() => ({ valid: true })),
|
|
19
|
+
getDatabaseType: jest.fn(() => ({ dbType: 'postgresql' })),
|
|
20
|
+
checkPrismaClientGenerated: jest.fn(() => ({ generated: true })),
|
|
21
|
+
}));
|
|
22
|
+
jest.mock('dotenv', () => ({ config: jest.fn() }));
|
|
23
|
+
|
|
24
|
+
const childProcess = mockChildProcess;
|
|
25
|
+
const nodeChildProcess = mockChildProcess;
|
|
26
|
+
const { preflightValidation } = require('../../../validate-command');
|
|
27
|
+
const { buildCommand } = require('../../../build-command');
|
|
28
|
+
const { deployCommand } = require('../../../deploy-command');
|
|
29
|
+
const { startCommand } = require('../../../start-command');
|
|
30
|
+
|
|
31
|
+
describe('validation before build, deploy and start', () => {
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
jest.clearAllMocks();
|
|
34
|
+
jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
35
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
36
|
+
jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
37
|
+
jest.spyOn(process, 'exit').mockImplementation(() => {});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => jest.restoreAllMocks());
|
|
41
|
+
|
|
42
|
+
it('build validates first and fails on errors', async () => {
|
|
43
|
+
preflightValidation.mockReturnValue(false);
|
|
44
|
+
|
|
45
|
+
await buildCommand({ stage: 'prod' });
|
|
46
|
+
|
|
47
|
+
expect(preflightValidation).toHaveBeenCalledWith({
|
|
48
|
+
command: 'build',
|
|
49
|
+
options: { stage: 'prod' },
|
|
50
|
+
failOnErrors: true,
|
|
51
|
+
});
|
|
52
|
+
expect(childProcess.spawnSync).not.toHaveBeenCalled();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('build packages after a passing validation', async () => {
|
|
56
|
+
preflightValidation.mockReturnValue(true);
|
|
57
|
+
|
|
58
|
+
await buildCommand({ stage: 'prod' });
|
|
59
|
+
|
|
60
|
+
expect(childProcess.spawnSync).toHaveBeenCalled();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('deploy validates first and fails on errors', async () => {
|
|
64
|
+
preflightValidation.mockReturnValue(false);
|
|
65
|
+
|
|
66
|
+
await deployCommand({ stage: 'prod', skipValidate: false });
|
|
67
|
+
|
|
68
|
+
expect(preflightValidation).toHaveBeenCalledWith({
|
|
69
|
+
command: 'deploy',
|
|
70
|
+
options: { stage: 'prod', skipValidate: false },
|
|
71
|
+
failOnErrors: true,
|
|
72
|
+
});
|
|
73
|
+
expect(childProcess.spawn).not.toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('start validates and only warns', async () => {
|
|
77
|
+
preflightValidation.mockReturnValue(true);
|
|
78
|
+
nodeChildProcess.spawn.mockReturnValue({ on: jest.fn() });
|
|
79
|
+
const previous = process.env.DATABASE_URL;
|
|
80
|
+
process.env.DATABASE_URL = 'postgresql://localhost/frigg';
|
|
81
|
+
|
|
82
|
+
await startCommand({ stage: 'dev' });
|
|
83
|
+
|
|
84
|
+
process.env.DATABASE_URL = previous;
|
|
85
|
+
expect(preflightValidation).toHaveBeenCalledWith({
|
|
86
|
+
command: 'start',
|
|
87
|
+
options: { stage: 'dev' },
|
|
88
|
+
failOnErrors: false,
|
|
89
|
+
});
|
|
90
|
+
expect(nodeChildProcess.spawn).toHaveBeenCalled();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
const { spawnSync } = require('child_process');
|
|
2
2
|
const path = require('path');
|
|
3
|
+
const { preflightValidation } = require('../validate-command');
|
|
3
4
|
|
|
4
5
|
async function buildCommand(options) {
|
|
5
6
|
console.log('Building the serverless application...');
|
|
6
7
|
|
|
8
|
+
// Validate the app definition first (ADR-051); errors stop the build.
|
|
9
|
+
if (!preflightValidation({ command: 'build', options, failOnErrors: true })) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
// Suppress AWS SDK warning message about maintenance mode
|
|
8
14
|
process.env.AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE = '1';
|
|
9
15
|
|
|
@@ -4,6 +4,7 @@ const fs = require('fs');
|
|
|
4
4
|
|
|
5
5
|
// Import doctor command for post-deployment health check
|
|
6
6
|
const { doctorCommand } = require('../doctor-command');
|
|
7
|
+
const { preflightValidation } = require('../validate-command');
|
|
7
8
|
|
|
8
9
|
// Configuration constants
|
|
9
10
|
const PATHS = {
|
|
@@ -312,6 +313,11 @@ async function pushOffloadedParametersOrAbort(appDefinition, options) {
|
|
|
312
313
|
async function deployCommand(options) {
|
|
313
314
|
console.log('Deploying the serverless application...');
|
|
314
315
|
|
|
316
|
+
// Validate the app definition first (ADR-051); errors stop the deploy.
|
|
317
|
+
if (!preflightValidation({ command: 'deploy', options, failOnErrors: true })) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
315
321
|
const appDefinition = loadAppDefinition();
|
|
316
322
|
const environment = validateAndBuildEnvironment(appDefinition, options);
|
|
317
323
|
|
package/frigg-cli/index.js
CHANGED
|
@@ -87,6 +87,7 @@ const { doctorCommand } = require('./doctor-command');
|
|
|
87
87
|
const { repairCommand } = require('./repair-command');
|
|
88
88
|
const { authCommand } = require('./auth-command');
|
|
89
89
|
const { ssmPushCommand } = require('./ssm-command');
|
|
90
|
+
const { validateCommand } = require('./validate-command');
|
|
90
91
|
|
|
91
92
|
const program = new Command();
|
|
92
93
|
|
|
@@ -113,14 +114,24 @@ program
|
|
|
113
114
|
.description('Run the backend and optional frontend')
|
|
114
115
|
.option('-s, --stage <stage>', 'deployment stage', 'dev')
|
|
115
116
|
.option('-v, --verbose', 'enable verbose output')
|
|
117
|
+
.option('--skip-validate', 'skip app definition validation')
|
|
116
118
|
.action(startCommand);
|
|
117
119
|
|
|
120
|
+
program
|
|
121
|
+
.command('validate')
|
|
122
|
+
.description('Validate the app definition (index.js) against the schema and the runtime rules')
|
|
123
|
+
.option('-s, --stage <stage>', 'also run the checks for this stage (encryption, deploy-time environment)')
|
|
124
|
+
.option('--json', 'print the report as JSON')
|
|
125
|
+
.option('-v, --verbose', 'enable verbose output')
|
|
126
|
+
.action(validateCommand);
|
|
127
|
+
|
|
118
128
|
program
|
|
119
129
|
.command('build')
|
|
120
130
|
.description('Build the serverless application')
|
|
121
131
|
.option('-s, --stage <stage>', 'deployment stage', 'dev')
|
|
122
132
|
.option('-v, --verbose', 'enable verbose output')
|
|
123
133
|
.option('-p, --production', 'build for production (enables AWS discovery)')
|
|
134
|
+
.option('--skip-validate', 'skip app definition validation (not recommended)')
|
|
124
135
|
.action(buildCommand);
|
|
125
136
|
|
|
126
137
|
program
|
|
@@ -130,6 +141,7 @@ program
|
|
|
130
141
|
.option('-v, --verbose', 'enable verbose output')
|
|
131
142
|
.option('-f, --force', 'force deployment (bypasses caching for layers and functions)')
|
|
132
143
|
.option('--skip-doctor', 'skip post-deployment health check')
|
|
144
|
+
.option('--skip-validate', 'skip app definition validation (not recommended)')
|
|
133
145
|
.action(deployCommand);
|
|
134
146
|
|
|
135
147
|
program
|
|
@@ -225,4 +237,4 @@ authProgram
|
|
|
225
237
|
|
|
226
238
|
program.parse(process.argv);
|
|
227
239
|
|
|
228
|
-
module.exports = { initCommand, installCommand, startCommand, buildCommand, deployCommand, generateIamCommand, uiCommand, dbSetupCommand, doctorCommand, repairCommand, authCommand, ssmPushCommand };
|
|
240
|
+
module.exports = { initCommand, installCommand, startCommand, buildCommand, deployCommand, validateCommand, generateIamCommand, uiCommand, dbSetupCommand, doctorCommand, repairCommand, authCommand, ssmPushCommand };
|
|
@@ -5,10 +5,8 @@ const { spawnSync } = require('child_process');
|
|
|
5
5
|
const spawn = require('cross-spawn');
|
|
6
6
|
const { resolveInside } = require('./safe-path');
|
|
7
7
|
const npmRegistry = require('../utils/npm-registry');
|
|
8
|
-
const {
|
|
9
|
-
|
|
10
|
-
formatErrors,
|
|
11
|
-
} = require('@friggframework/schemas');
|
|
8
|
+
const { runValidation } = require('../validate-command');
|
|
9
|
+
const { formatReport } = require('../validate-command/format-report');
|
|
12
10
|
const {
|
|
13
11
|
getScaffoldDependencies,
|
|
14
12
|
getScaffoldScripts,
|
|
@@ -468,8 +466,10 @@ class BackendFirstHandler {
|
|
|
468
466
|
}
|
|
469
467
|
|
|
470
468
|
/**
|
|
471
|
-
* Validate the generated app definition
|
|
472
|
-
*
|
|
469
|
+
* Validate the generated app definition with the same code path as
|
|
470
|
+
* `frigg validate`, `frigg build` and `frigg deploy` (ADR-051): load the
|
|
471
|
+
* `Definition` the generated index.js exports, check it against the
|
|
472
|
+
* app-definition schema and run the semantic checks.
|
|
473
473
|
*
|
|
474
474
|
* Validation problems are reported as warnings; they never abort init.
|
|
475
475
|
*
|
|
@@ -477,52 +477,53 @@ class BackendFirstHandler {
|
|
|
477
477
|
*/
|
|
478
478
|
async validateGeneratedAppDefinition(appDefPath) {
|
|
479
479
|
if (this.options.verbose) {
|
|
480
|
-
console.log(
|
|
481
|
-
chalk.gray('🔍 Validating app definition against schema...')
|
|
482
|
-
);
|
|
480
|
+
console.log(chalk.gray('🔍 Validating app definition...'));
|
|
483
481
|
}
|
|
484
|
-
|
|
485
|
-
let definition;
|
|
486
482
|
try {
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
if (!result.valid) {
|
|
513
|
-
console.log(
|
|
514
|
-
chalk.yellow('⚠️ App definition has validation warnings:')
|
|
515
|
-
);
|
|
516
|
-
console.log(chalk.gray(formatErrors(result.errors)));
|
|
517
|
-
return false;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
if (this.options.verbose) {
|
|
483
|
+
const report = runValidation({ cwd: path.dirname(appDefPath) });
|
|
484
|
+
const issues = [...report.errors, ...report.warnings];
|
|
485
|
+
if (issues.length === 0) {
|
|
486
|
+
if (this.options.verbose) {
|
|
487
|
+
console.log(
|
|
488
|
+
chalk.green('✅ App definition passes validation')
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
if (
|
|
494
|
+
report.errors.length === 1 &&
|
|
495
|
+
report.errors[0].code === 'load-failed'
|
|
496
|
+
) {
|
|
497
|
+
// Before `npm install`, index.js cannot load its dependencies yet.
|
|
498
|
+
if (this.options.verbose) {
|
|
499
|
+
console.log(
|
|
500
|
+
chalk.gray(
|
|
501
|
+
'App definition not validated yet: run `frigg validate` after `npm install`.'
|
|
502
|
+
)
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
console.log(chalk.yellow('⚠️ App definition validation:'));
|
|
521
508
|
console.log(
|
|
522
|
-
|
|
509
|
+
formatReport({
|
|
510
|
+
...report,
|
|
511
|
+
errors: [],
|
|
512
|
+
warnings: issues.map((issue) => ({
|
|
513
|
+
...issue,
|
|
514
|
+
severity: 'warning',
|
|
515
|
+
})),
|
|
516
|
+
})
|
|
523
517
|
);
|
|
518
|
+
return report.errors.length === 0;
|
|
519
|
+
} catch (error) {
|
|
520
|
+
if (this.options.verbose) {
|
|
521
|
+
console.log(
|
|
522
|
+
chalk.yellow(`⚠️ Validation skipped: ${error.message}`)
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
return true; // Don't fail the process for validation issues
|
|
524
526
|
}
|
|
525
|
-
return true;
|
|
526
527
|
}
|
|
527
528
|
|
|
528
529
|
/**
|
|
@@ -12,6 +12,7 @@ const {
|
|
|
12
12
|
getDatabaseTypeNotConfiguredError,
|
|
13
13
|
getPrismaClientNotGeneratedError
|
|
14
14
|
} = require('../utils/error-messages');
|
|
15
|
+
const { preflightValidation } = require('../validate-command');
|
|
15
16
|
|
|
16
17
|
async function startCommand(options) {
|
|
17
18
|
if (options.verbose) {
|
|
@@ -25,6 +26,9 @@ async function startCommand(options) {
|
|
|
25
26
|
const envPath = path.join(process.cwd(), '.env');
|
|
26
27
|
dotenv.config({ path: envPath });
|
|
27
28
|
|
|
29
|
+
// Validate the app definition (ADR-051); local development only warns.
|
|
30
|
+
preflightValidation({ command: 'start', options, failOnErrors: false });
|
|
31
|
+
|
|
28
32
|
// Pre-flight database checks
|
|
29
33
|
try {
|
|
30
34
|
await performDatabaseChecks(options.verbose);
|
|
@@ -23,6 +23,9 @@ jest.mock('node:child_process', () => ({
|
|
|
23
23
|
}));
|
|
24
24
|
|
|
25
25
|
jest.mock('../utils/database-validator', () => mockValidator);
|
|
26
|
+
// The app definition validation step is tested in validate-command/.
|
|
27
|
+
jest.mock('../validate-command', () => ({ preflightValidation: jest.fn(() => true) }));
|
|
28
|
+
|
|
26
29
|
jest.mock('dotenv');
|
|
27
30
|
|
|
28
31
|
const { spawn } = require('node:child_process');
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `frigg init` validates the generated app with the same code path as
|
|
3
|
+
* `frigg validate`, and never fails because of it (ADR-051).
|
|
4
|
+
*/
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
jest.mock('@inquirer/prompts', () => ({}));
|
|
10
|
+
|
|
11
|
+
const BackendFirstHandler = require('../../init-command/backend-first-handler');
|
|
12
|
+
|
|
13
|
+
function writeApp(source) {
|
|
14
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-init-'));
|
|
15
|
+
fs.writeFileSync(path.join(dir, 'package.json'), '{"name":"backend"}');
|
|
16
|
+
fs.writeFileSync(path.join(dir, 'index.js'), source);
|
|
17
|
+
return dir;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('frigg init validation', () => {
|
|
21
|
+
const dirs = [];
|
|
22
|
+
let log;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
log = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
jest.restoreAllMocks();
|
|
30
|
+
for (const dir of dirs.splice(0))
|
|
31
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('validates the real generated definition, not a stand-in', async () => {
|
|
35
|
+
const dir = writeApp(
|
|
36
|
+
"module.exports = { Definition: { integrations: [], database: { postgres: { enable: true, management: 'create-new' } } } };"
|
|
37
|
+
);
|
|
38
|
+
dirs.push(dir);
|
|
39
|
+
const handler = new BackendFirstHandler(dir, {});
|
|
40
|
+
|
|
41
|
+
const ok = await handler.validateGeneratedAppDefinition(
|
|
42
|
+
path.join(dir, 'index.js')
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
expect(ok).toBe(false);
|
|
46
|
+
expect(log.mock.calls.flat().join('\n')).toContain(
|
|
47
|
+
'/database/postgres/management'
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('stays quiet before npm install, when index.js cannot load its dependencies', async () => {
|
|
52
|
+
const dir = writeApp(
|
|
53
|
+
"require('@friggframework/api-module-does-not-exist');"
|
|
54
|
+
);
|
|
55
|
+
dirs.push(dir);
|
|
56
|
+
const handler = new BackendFirstHandler(dir, {});
|
|
57
|
+
|
|
58
|
+
await expect(
|
|
59
|
+
handler.validateGeneratedAppDefinition(path.join(dir, 'index.js'))
|
|
60
|
+
).resolves.toBe(true);
|
|
61
|
+
expect(log).not.toHaveBeenCalled();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
const { checkIntegrations } = require('../checks/integrations');
|
|
2
|
+
|
|
3
|
+
class FakeApi {
|
|
4
|
+
static requesterType = 'oauth2';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const moduleDefinition = () => ({
|
|
8
|
+
moduleName: 'acme',
|
|
9
|
+
API: FakeApi,
|
|
10
|
+
requiredAuthMethods: {
|
|
11
|
+
getToken: async () => {},
|
|
12
|
+
getEntityDetails: async () => {},
|
|
13
|
+
getCredentialDetails: async () => {},
|
|
14
|
+
testAuthRequest: async () => {},
|
|
15
|
+
apiPropertiesToPersist: { credential: [], entity: [] },
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
function integrationClass(definition) {
|
|
20
|
+
return class Integration {
|
|
21
|
+
static Definition = definition;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const goodIntegration = () =>
|
|
26
|
+
integrationClass({
|
|
27
|
+
name: 'acme',
|
|
28
|
+
version: '1.0.0',
|
|
29
|
+
modules: { acme: { definition: moduleDefinition() } },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const codes = (issues) =>
|
|
33
|
+
issues.map((i) => `${i.severity}:${i.code}:${i.pointer}`);
|
|
34
|
+
|
|
35
|
+
describe('checkIntegrations', () => {
|
|
36
|
+
it('accepts a well-formed integration class', () => {
|
|
37
|
+
expect(
|
|
38
|
+
checkIntegrations({ integrations: [goodIntegration()] })
|
|
39
|
+
).toEqual([]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('warns when the app lists no integrations', () => {
|
|
43
|
+
expect(codes(checkIntegrations({ integrations: [] }))).toEqual([
|
|
44
|
+
'warning:no-integrations:/integrations',
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('rejects an entry that is not a class', () => {
|
|
49
|
+
expect(
|
|
50
|
+
codes(
|
|
51
|
+
checkIntegrations({
|
|
52
|
+
integrations: [{ Definition: { name: 'x' } }],
|
|
53
|
+
})
|
|
54
|
+
)
|
|
55
|
+
).toEqual(['error:integration-not-a-class:/integrations/0']);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('rejects a class without a static Definition', () => {
|
|
59
|
+
class Bare {}
|
|
60
|
+
expect(codes(checkIntegrations({ integrations: [Bare] }))).toEqual([
|
|
61
|
+
'error:integration-definition-missing:/integrations/0/Definition',
|
|
62
|
+
]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('requires a valid name and modules, and warns on a missing version', () => {
|
|
66
|
+
const Cls = integrationClass({ name: '1 bad name' });
|
|
67
|
+
expect(codes(checkIntegrations({ integrations: [Cls] }))).toEqual([
|
|
68
|
+
'error:integration-name-invalid:/integrations/0/Definition/name',
|
|
69
|
+
'warning:integration-version-missing:/integrations/0/Definition/version',
|
|
70
|
+
'error:integration-modules-missing:/integrations/0/Definition/modules',
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('reports duplicate integration names', () => {
|
|
75
|
+
expect(
|
|
76
|
+
codes(
|
|
77
|
+
checkIntegrations({
|
|
78
|
+
integrations: [goodIntegration(), goodIntegration()],
|
|
79
|
+
})
|
|
80
|
+
)
|
|
81
|
+
).toEqual([
|
|
82
|
+
'error:integration-name-duplicate:/integrations/1/Definition/name',
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('checks module definitions the way Module.validateDefinition does', () => {
|
|
87
|
+
const definition = moduleDefinition();
|
|
88
|
+
delete definition.requiredAuthMethods.getToken;
|
|
89
|
+
delete definition.moduleName;
|
|
90
|
+
const Cls = integrationClass({
|
|
91
|
+
name: 'acme',
|
|
92
|
+
version: '1',
|
|
93
|
+
modules: { acme: { definition }, other: {} },
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(codes(checkIntegrations({ integrations: [Cls] }))).toEqual([
|
|
97
|
+
'error:module-name-missing:/integrations/0/Definition/modules/acme/definition/moduleName',
|
|
98
|
+
'error:module-auth-method-missing:/integrations/0/Definition/modules/acme/definition/requiredAuthMethods/getToken',
|
|
99
|
+
'error:module-definition-missing:/integrations/0/Definition/modules/other/definition',
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('does not require getToken for non-OAuth2 modules', () => {
|
|
104
|
+
const definition = moduleDefinition();
|
|
105
|
+
definition.API = class {
|
|
106
|
+
static requesterType = 'apiKey';
|
|
107
|
+
};
|
|
108
|
+
delete definition.requiredAuthMethods.getToken;
|
|
109
|
+
const Cls = integrationClass({
|
|
110
|
+
name: 'acme',
|
|
111
|
+
version: '1',
|
|
112
|
+
modules: { acme: { definition } },
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(checkIntegrations({ integrations: [Cls] })).toEqual([]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('warns that a module rateLimit is not applied', () => {
|
|
119
|
+
const definition = {
|
|
120
|
+
...moduleDefinition(),
|
|
121
|
+
rateLimit: { requests: 10, period: 'second' },
|
|
122
|
+
};
|
|
123
|
+
const Cls = integrationClass({
|
|
124
|
+
name: 'acme',
|
|
125
|
+
version: '1',
|
|
126
|
+
modules: { acme: { definition } },
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(codes(checkIntegrations({ integrations: [Cls] }))).toEqual([
|
|
130
|
+
'warning:module-rate-limit-ignored:/integrations/0/Definition/modules/acme/definition/rateLimit',
|
|
131
|
+
]);
|
|
132
|
+
});
|
|
133
|
+
});
|