@friggframework/devtools 2.0.0--canary.705.f070722.0 → 2.0.0--canary.662.6d3931c.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.
Files changed (82) hide show
  1. package/frigg-cli/README.md +31 -115
  2. package/frigg-cli/__tests__/unit/commands/build.test.js +0 -3
  3. package/frigg-cli/__tests__/unit/commands/deploy.test.js +24 -33
  4. package/frigg-cli/__tests__/unit/commands/doctor.test.js +6 -10
  5. package/frigg-cli/__tests__/unit/commands/generate-iam.test.js +0 -18
  6. package/frigg-cli/__tests__/unit/commands/install.test.js +341 -601
  7. package/frigg-cli/__tests__/unit/commands/ui.test.js +30 -31
  8. package/frigg-cli/__tests__/unit/dependencies.test.js +2 -6
  9. package/frigg-cli/build-command/index.js +0 -6
  10. package/frigg-cli/deploy-command/index.js +0 -6
  11. package/frigg-cli/generate-command/index.js +0 -1
  12. package/frigg-cli/generate-iam-command.js +0 -1
  13. package/frigg-cli/index.js +5 -22
  14. package/frigg-cli/index.test.js +158 -0
  15. package/frigg-cli/init-command/backend-first-handler.js +551 -395
  16. package/frigg-cli/init-command/index.js +35 -82
  17. package/frigg-cli/install-command/commit-changes.js +8 -40
  18. package/frigg-cli/install-command/environment-variables.js +0 -15
  19. package/frigg-cli/install-command/index.js +21 -96
  20. package/frigg-cli/install-command/install-package.js +4 -77
  21. package/frigg-cli/install-command/integration-file.js +11 -20
  22. package/frigg-cli/install-command/template.js +75 -102
  23. package/frigg-cli/install-command/validate-package.js +4 -36
  24. package/frigg-cli/package.json +1 -1
  25. package/frigg-cli/start-command/index.js +0 -4
  26. package/frigg-cli/start-command/start-command.test.js +0 -3
  27. package/frigg-cli/test/init-command.test.js +2 -9
  28. package/infrastructure/__tests__/postgres-config.test.js +835 -241
  29. package/infrastructure/__tests__/template-generation.test.js +8 -36
  30. package/infrastructure/create-frigg-infrastructure.js +1 -4
  31. package/infrastructure/docs/POSTGRES-CONFIGURATION.md +98 -80
  32. package/infrastructure/domains/admin-scripts/admin-script-builder.js +2 -6
  33. package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +0 -30
  34. package/infrastructure/domains/database/aurora-builder.js +2 -13
  35. package/infrastructure/domains/database/aurora-builder.test.js +0 -22
  36. package/infrastructure/domains/health/application/use-cases/__tests__/mismatch-analyzer-method-name.test.js +8 -12
  37. package/infrastructure/domains/health/application/use-cases/__tests__/repair-via-import-use-case.test.js +7 -19
  38. package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.test.js +8 -12
  39. package/infrastructure/domains/health/domain/services/__tests__/import-progress-monitor.test.js +18 -17
  40. package/infrastructure/domains/health/domain/services/__tests__/update-progress-monitor.test.js +2 -12
  41. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-relationship-analysis.test.js +1 -8
  42. package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.test.js +2 -4
  43. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.test.js +0 -10
  44. package/infrastructure/domains/networking/vpc-builder.js +8 -18
  45. package/infrastructure/domains/networking/vpc-builder.test.js +12 -26
  46. package/infrastructure/domains/security/iam-generator.js +5 -112
  47. package/infrastructure/domains/security/iam-generator.test.js +0 -94
  48. package/infrastructure/domains/shared/cloudformation-discovery.test.js +6 -36
  49. package/infrastructure/domains/shared/utilities/base-definition-factory.js +2 -26
  50. package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +1 -34
  51. package/infrastructure/infrastructure-composer.test.js +794 -268
  52. package/infrastructure/integration.test.js +381 -0
  53. package/infrastructure/scripts/build-prisma-layer.test.js +7 -28
  54. package/infrastructure/scripts/build-time-discovery.test.js +3 -18
  55. package/package.json +8 -10
  56. package/frigg-cli/__tests__/unit/commands/init.test.js +0 -688
  57. package/frigg-cli/__tests__/unit/commands/validate-wiring.test.js +0 -92
  58. package/frigg-cli/init-command/deployment-modes.js +0 -7
  59. package/frigg-cli/init-command/safe-path.js +0 -58
  60. package/frigg-cli/init-command/scaffold-dependencies.js +0 -202
  61. package/frigg-cli/install-command/app-definition.js +0 -174
  62. package/frigg-cli/install-command/resolve-module.js +0 -69
  63. package/frigg-cli/templates/backend/README.md +0 -267
  64. package/frigg-cli/templates/backend/env.example +0 -20
  65. package/frigg-cli/templates/backend/gitignore +0 -10
  66. package/frigg-cli/templates/backend/index.js +0 -114
  67. package/frigg-cli/templates/backend/infrastructure.js +0 -20
  68. package/frigg-cli/validate-command/__tests__/init-validation.test.js +0 -63
  69. package/frigg-cli/validate-command/__tests__/integrations-checks.test.js +0 -133
  70. package/frigg-cli/validate-command/__tests__/validate-app-definition.test.js +0 -443
  71. package/frigg-cli/validate-command/__tests__/validate-command.test.js +0 -262
  72. package/frigg-cli/validate-command/checks/config.js +0 -504
  73. package/frigg-cli/validate-command/checks/integrations.js +0 -274
  74. package/frigg-cli/validate-command/format-report.js +0 -58
  75. package/frigg-cli/validate-command/index.js +0 -107
  76. package/frigg-cli/validate-command/load-app-definition.js +0 -105
  77. package/frigg-cli/validate-command/schema-issues.js +0 -154
  78. package/frigg-cli/validate-command/validate-app-definition.js +0 -60
  79. package/infrastructure/__tests__/management-api-gateway-routes.test.js +0 -281
  80. package/infrastructure/__tests__/scaffold-default-infrastructure.test.js +0 -148
  81. package/infrastructure/app-definition-schema-contract.test.js +0 -629
  82. package/infrastructure/create-frigg-infrastructure.test.js +0 -51
@@ -33,17 +33,6 @@ const {
33
33
  } = require('../../../utils/repo-detection');
34
34
  const { uiCommand } = require('../../../ui-command');
35
35
 
36
- // uiCommand waits 2 s for the dev servers and opens the browser 1 s later.
37
- // Drive those timers with fake time instead of sleeping for real (the real
38
- // waits made this suite take ~40 s and tripped the 5 s default timeout).
39
- const STARTUP_AND_BROWSER_DELAY_MS = 3100;
40
-
41
- async function runUi(options) {
42
- const done = uiCommand(options);
43
- await jest.advanceTimersByTimeAsync(STARTUP_AND_BROWSER_DELAY_MS);
44
- return done;
45
- }
46
-
47
36
  describe('CLI Command: ui', () => {
48
37
  let consoleLogSpy;
49
38
  let consoleErrorSpy;
@@ -54,7 +43,6 @@ describe('CLI Command: ui', () => {
54
43
 
55
44
  beforeEach(() => {
56
45
  jest.clearAllMocks();
57
- jest.useFakeTimers();
58
46
 
59
47
  // Mock console methods
60
48
  consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
@@ -87,7 +75,6 @@ describe('CLI Command: ui', () => {
87
75
  });
88
76
 
89
77
  afterEach(() => {
90
- jest.useRealTimers();
91
78
  consoleLogSpy.mockRestore();
92
79
  consoleErrorSpy.mockRestore();
93
80
  processExitSpy.mockRestore();
@@ -97,7 +84,7 @@ describe('CLI Command: ui', () => {
97
84
 
98
85
  describe('Repository Detection', () => {
99
86
  it('should use specified repo path when provided', async () => {
100
- await runUi({ repo: '/custom/repo', port: 3001, open: false });
87
+ await uiCommand({ repo: '/custom/repo', port: 3001, open: false });
101
88
 
102
89
  expect(consoleLogSpy).toHaveBeenCalledWith(
103
90
  expect.stringContaining('Using specified repository')
@@ -105,7 +92,7 @@ describe('CLI Command: ui', () => {
105
92
  });
106
93
 
107
94
  it('should detect current Frigg repository', async () => {
108
- await runUi({ port: 3001, open: false });
95
+ await uiCommand({ port: 3001, open: false });
109
96
 
110
97
  expect(getCurrentRepositoryInfo).toHaveBeenCalled();
111
98
  expect(consoleLogSpy).toHaveBeenCalledWith(
@@ -120,7 +107,7 @@ describe('CLI Command: ui', () => {
120
107
  currentSubPath: 'packages/backend'
121
108
  });
122
109
 
123
- await runUi({ port: 3001, open: false });
110
+ await uiCommand({ port: 3001, open: false });
124
111
 
125
112
  expect(consoleLogSpy).toHaveBeenCalledWith(
126
113
  expect.stringContaining('Currently in subdirectory: packages/backend')
@@ -134,7 +121,7 @@ describe('CLI Command: ui', () => {
134
121
  { path: '/repos/frigg-2', name: 'frigg-2' }
135
122
  ]);
136
123
 
137
- await runUi({ port: 3001, open: false });
124
+ await uiCommand({ port: 3001, open: false });
138
125
 
139
126
  expect(discoverFriggRepositories).toHaveBeenCalled();
140
127
  expect(consoleLogSpy).toHaveBeenCalledWith(
@@ -146,7 +133,7 @@ describe('CLI Command: ui', () => {
146
133
  getCurrentRepositoryInfo.mockResolvedValue(null);
147
134
  discoverFriggRepositories.mockResolvedValue([]);
148
135
 
149
- await runUi({ port: 3001, open: false });
136
+ await uiCommand({ port: 3001, open: false });
150
137
 
151
138
  expect(consoleLogSpy).toHaveBeenCalledWith(
152
139
  expect.stringContaining('No Frigg repositories found')
@@ -157,7 +144,7 @@ describe('CLI Command: ui', () => {
157
144
 
158
145
  describe('Development Mode', () => {
159
146
  it('should spawn backend and frontend in dev mode', async () => {
160
- await runUi({ port: 3001, open: false, dev: true });
147
+ await uiCommand({ port: 3001, open: false, dev: true });
161
148
 
162
149
  expect(mockProcessManager.spawnProcess).toHaveBeenCalledTimes(2);
163
150
  expect(mockProcessManager.spawnProcess).toHaveBeenCalledWith(
@@ -181,7 +168,7 @@ describe('CLI Command: ui', () => {
181
168
  });
182
169
 
183
170
  it('should use default port 3001', async () => {
184
- await runUi({ open: false, dev: true });
171
+ await uiCommand({ open: false, dev: true });
185
172
 
186
173
  expect(mockProcessManager.spawnProcess).toHaveBeenCalledWith(
187
174
  expect.any(String),
@@ -194,7 +181,7 @@ describe('CLI Command: ui', () => {
194
181
  });
195
182
 
196
183
  it('should use custom port when specified', async () => {
197
- await runUi({ port: 4000, open: false, dev: true });
184
+ await uiCommand({ port: 4000, open: false, dev: true });
198
185
 
199
186
  expect(mockProcessManager.spawnProcess).toHaveBeenCalledWith(
200
187
  expect.any(String),
@@ -210,7 +197,10 @@ describe('CLI Command: ui', () => {
210
197
  });
211
198
 
212
199
  it('should print status with correct URLs', async () => {
213
- await runUi({ port: 3001, open: false, dev: true });
200
+ await uiCommand({ port: 3001, open: false, dev: true });
201
+
202
+ // Wait for startup delay (testing async behavior, not mocks)
203
+ await new Promise(resolve => setTimeout(resolve, 2100));
214
204
 
215
205
  // Verify printStatus called with exact URLs
216
206
  expect(mockProcessManager.printStatus).toHaveBeenCalledWith(
@@ -221,14 +211,20 @@ describe('CLI Command: ui', () => {
221
211
  });
222
212
 
223
213
  it('should open browser when open=true', async () => {
224
- await runUi({ port: 3001, open: true, dev: true });
214
+ await uiCommand({ port: 3001, open: true, dev: true });
215
+
216
+ // Wait for startup + browser delay (testing async behavior, not mocks)
217
+ await new Promise(resolve => setTimeout(resolve, 3100));
225
218
 
226
219
  // Verify browser opened with correct URL
227
220
  expect(open).toHaveBeenCalledWith('http://localhost:5173');
228
221
  });
229
222
 
230
223
  it('should NOT open browser when open=false', async () => {
231
- await runUi({ port: 3001, open: false, dev: true });
224
+ await uiCommand({ port: 3001, open: false, dev: true });
225
+
226
+ // Wait for startup delay (testing async behavior, not mocks)
227
+ await new Promise(resolve => setTimeout(resolve, 3100));
232
228
 
233
229
  // Verify browser was not opened
234
230
  expect(open).not.toHaveBeenCalled();
@@ -241,7 +237,7 @@ describe('CLI Command: ui', () => {
241
237
  currentSubPath: null
242
238
  });
243
239
 
244
- await runUi({ port: 3001, open: false, dev: true });
240
+ await uiCommand({ port: 3001, open: false, dev: true });
245
241
 
246
242
  expect(mockProcessManager.spawnProcess).toHaveBeenCalledWith(
247
243
  expect.any(String),
@@ -256,7 +252,7 @@ describe('CLI Command: ui', () => {
256
252
  });
257
253
 
258
254
  it('should set REPOSITORY_INFO as JSON string with correct structure', async () => {
259
- await runUi({ port: 3001, open: false, dev: true });
255
+ await uiCommand({ port: 3001, open: false, dev: true });
260
256
 
261
257
  const call = mockProcessManager.spawnProcess.mock.calls[0];
262
258
  const env = call[3].env;
@@ -279,7 +275,7 @@ describe('CLI Command: ui', () => {
279
275
  { path: '/repos/frigg-1', name: 'frigg-1' }
280
276
  ]);
281
277
 
282
- await runUi({ port: 3001, open: false, dev: true });
278
+ await uiCommand({ port: 3001, open: false, dev: true });
283
279
 
284
280
  const call = mockProcessManager.spawnProcess.mock.calls[0];
285
281
  const env = call[3].env;
@@ -303,7 +299,7 @@ describe('CLI Command: ui', () => {
303
299
  throw error;
304
300
  });
305
301
 
306
- await runUi({ port: 3001, open: false, dev: true });
302
+ await uiCommand({ port: 3001, open: false, dev: true });
307
303
 
308
304
  expect(consoleErrorSpy).toHaveBeenCalledWith(
309
305
  expect.stringContaining('Failed to start Management UI'),
@@ -320,7 +316,7 @@ describe('CLI Command: ui', () => {
320
316
  throw new Error('Generic startup error');
321
317
  });
322
318
 
323
- await runUi({ port: 3001, open: false, dev: true });
319
+ await uiCommand({ port: 3001, open: false, dev: true });
324
320
 
325
321
  expect(consoleErrorSpy).toHaveBeenCalledWith(
326
322
  expect.stringContaining('Failed to start Management UI'),
@@ -332,13 +328,16 @@ describe('CLI Command: ui', () => {
332
328
 
333
329
  describe('Process Management', () => {
334
330
  it('should create ProcessManager instance', async () => {
335
- await runUi({ port: 3001, open: false, dev: true });
331
+ await uiCommand({ port: 3001, open: false, dev: true });
336
332
 
337
333
  expect(ProcessManager).toHaveBeenCalled();
338
334
  });
339
335
 
340
336
  it('should keep process running with stdin.resume', async () => {
341
- await runUi({ port: 3001, open: false, dev: true });
337
+ await uiCommand({ port: 3001, open: false, dev: true });
338
+
339
+ // Wait for startup delay (testing async behavior, not mocks)
340
+ await new Promise(resolve => setTimeout(resolve, 2100));
342
341
 
343
342
  // Verify stdin.resume called to keep process alive
344
343
  expect(stdinResumeSpy).toHaveBeenCalled();
@@ -51,12 +51,8 @@ describe('frigg-cli dependencies', () => {
51
51
  expect(packageJson.dependencies.commander).toBeDefined();
52
52
  });
53
53
 
54
- // frigg-cli ships inside @friggframework/devtools (14236100), so
55
- // devtools is a peer, not a runtime dependency — a hard dependency
56
- // would make the package depend on itself.
57
- it('should declare @friggframework/devtools as a peer dependency for infrastructure', () => {
58
- expect(packageJson.peerDependencies['@friggframework/devtools']).toBeDefined();
59
- expect(packageJson.dependencies['@friggframework/devtools']).toBeUndefined();
54
+ it('should have @friggframework/devtools for infrastructure', () => {
55
+ expect(packageJson.dependencies['@friggframework/devtools']).toBeDefined();
60
56
  });
61
57
  });
62
58
 
@@ -1,15 +1,9 @@
1
1
  const { spawnSync } = require('child_process');
2
2
  const path = require('path');
3
- const { preflightValidation } = require('../validate-command');
4
3
 
5
4
  async function buildCommand(options) {
6
5
  console.log('Building the serverless application...');
7
6
 
8
- // Validate the app definition first (ADR-051); errors stop the build.
9
- if (!preflightValidation({ command: 'build', options, failOnErrors: true })) {
10
- return;
11
- }
12
-
13
7
  // Suppress AWS SDK warning message about maintenance mode
14
8
  process.env.AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE = '1';
15
9
 
@@ -4,7 +4,6 @@ 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');
8
7
 
9
8
  // Configuration constants
10
9
  const PATHS = {
@@ -313,11 +312,6 @@ async function pushOffloadedParametersOrAbort(appDefinition, options) {
313
312
  async function deployCommand(options) {
314
313
  console.log('Deploying the serverless application...');
315
314
 
316
- // Validate the app definition first (ADR-051); errors stop the deploy.
317
- if (!preflightValidation({ command: 'deploy', options, failOnErrors: true })) {
318
- return;
319
- }
320
-
321
315
  const appDefinition = loadAppDefinition();
322
316
  const environment = validateAndBuildEnvironment(appDefinition, options);
323
317
 
@@ -118,7 +118,6 @@ async function generateCommand(options = {}) {
118
118
  if (options.format === 'cloudformation') {
119
119
  template = await generateCloudFormationTemplate({
120
120
  appName,
121
- serviceName: appDefinition.name || 'create-frigg-app',
122
121
  features,
123
122
  userPrefix: options.user || 'frigg-deployment-user',
124
123
  stackName: options.stackName || 'frigg-deployment-iam',
@@ -62,7 +62,6 @@ async function generateIamCommand(options = {}) {
62
62
  // Use the summary already extracted above (line 44)
63
63
  const cloudFormationYaml = generateIAMCloudFormation({
64
64
  appName: summary.appName,
65
- serviceName: summary.serviceName,
66
65
  features: summary.features,
67
66
  userPrefix: deploymentUserName,
68
67
  stackName,
@@ -87,21 +87,15 @@ 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');
91
90
 
92
91
  const program = new Command();
93
92
 
94
93
  program
95
- .command('init [projectName]')
94
+ .command('init [templateName]')
96
95
  .description('Initialize a new Frigg application')
97
- .option('-n, --name <name>', 'project name (alternative to the positional argument)')
98
- .option('-m, --mode <mode>', 'deployment mode: standalone or embedded')
99
- .option('-y, --yes', 'accept the default for every prompt (implied when stdin is not a TTY)')
100
- .option('--no-install', 'skip installing dependencies')
101
- .option('--no-git', 'skip initializing a git repository')
102
- .option('-f, --force', 'scaffold into a non-empty directory and overwrite README.md')
103
- .option('-v, --verbose', 'enable verbose output')
104
- .option('-t, --template <template>', 'legacy template (no longer supported)')
96
+ .option('-t, --template <template>', 'template to use', 'backend-only')
97
+ .option('-n, --name <name>', 'project name')
98
+ .option('-d, --directory <directory>', 'target directory')
105
99
  .action(initCommand);
106
100
 
107
101
  program
@@ -114,24 +108,14 @@ program
114
108
  .description('Run the backend and optional frontend')
115
109
  .option('-s, --stage <stage>', 'deployment stage', 'dev')
116
110
  .option('-v, --verbose', 'enable verbose output')
117
- .option('--skip-validate', 'skip app definition validation')
118
111
  .action(startCommand);
119
112
 
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
-
128
113
  program
129
114
  .command('build')
130
115
  .description('Build the serverless application')
131
116
  .option('-s, --stage <stage>', 'deployment stage', 'dev')
132
117
  .option('-v, --verbose', 'enable verbose output')
133
118
  .option('-p, --production', 'build for production (enables AWS discovery)')
134
- .option('--skip-validate', 'skip app definition validation (not recommended)')
135
119
  .action(buildCommand);
136
120
 
137
121
  program
@@ -141,7 +125,6 @@ program
141
125
  .option('-v, --verbose', 'enable verbose output')
142
126
  .option('-f, --force', 'force deployment (bypasses caching for layers and functions)')
143
127
  .option('--skip-doctor', 'skip post-deployment health check')
144
- .option('--skip-validate', 'skip app definition validation (not recommended)')
145
128
  .action(deployCommand);
146
129
 
147
130
  program
@@ -237,4 +220,4 @@ authProgram
237
220
 
238
221
  program.parse(process.argv);
239
222
 
240
- module.exports = { initCommand, installCommand, startCommand, buildCommand, deployCommand, validateCommand, generateIamCommand, uiCommand, dbSetupCommand, doctorCommand, repairCommand, authCommand, ssmPushCommand };
223
+ module.exports = { initCommand, installCommand, startCommand, buildCommand, deployCommand, generateIamCommand, uiCommand, dbSetupCommand, doctorCommand, repairCommand, authCommand, ssmPushCommand };
@@ -0,0 +1,158 @@
1
+ const { Command } = require('commander');
2
+ const { installCommand } = require('./index');
3
+ const { validatePackageExists } = require('./install-command/validate-package');
4
+ const { findNearestBackendPackageJson, validateBackendPath } = require('@friggframework/core');
5
+ const { installPackage } = require('./install-command/install-package');
6
+ const { createIntegrationFile } = require('./install-command/integration-file');
7
+ const { updateBackendJsFile } = require('./install-command/backend-js');
8
+ const { commitChanges } = require('./install-command/commit-changes');
9
+ const { logInfo, logError } = require('./install-command/logger');
10
+
11
+ describe('CLI Command Tests', () => {
12
+ it('should successfully install an API module when all steps complete without errors', async () => {
13
+ const mockApiModuleName = 'testModule';
14
+ const mockPackageName = `@friggframework/api-module-${mockApiModuleName}`;
15
+ const mockBackendPath = '/mock/backend/path';
16
+
17
+ jest.mock('./install-command/validate-package', () => ({
18
+ validatePackageExists: jest.fn().mockResolvedValue(true),
19
+ }));
20
+ jest.mock('./utils/backend-path', () => ({
21
+ findNearestBackendPackageJson: jest
22
+ .fn()
23
+ .mockReturnValue(mockBackendPath),
24
+ validateBackendPath: jest.fn().mockReturnValue(true),
25
+ }));
26
+ jest.mock('./install-command/install-package', () => ({
27
+ installPackage: jest.fn().mockReturnValue(true),
28
+ }));
29
+ jest.mock('./install-command/integration-file', () => ({
30
+ createIntegrationFile: jest.fn().mockReturnValue(true),
31
+ }));
32
+ jest.mock('./install-command/backend-js', () => ({
33
+ updateBackendJsFile: jest.fn().mockReturnValue(true),
34
+ }));
35
+ jest.mock('./install-command/commit-changes', () => ({
36
+ commitChanges: jest.fn().mockReturnValue(true),
37
+ }));
38
+ jest.mock('./install-command/logger', () => ({
39
+ logInfo: jest.fn(),
40
+ logError: jest.fn(),
41
+ }));
42
+
43
+ const program = new Command();
44
+ program
45
+ .command('install <apiModuleName>')
46
+ .description('Install an API module')
47
+ .action(installCommand);
48
+
49
+ await program.parseAsync(['node', 'install', mockApiModuleName]);
50
+
51
+ expect(validatePackageExists).toHaveBeenCalledWith(mockPackageName);
52
+ expect(findNearestBackendPackageJson).toHaveBeenCalled();
53
+ expect(validateBackendPath).toHaveBeenCalledWith(mockBackendPath);
54
+ expect(installPackage).toHaveBeenCalledWith(
55
+ mockBackendPath,
56
+ mockPackageName
57
+ );
58
+ expect(createIntegrationFile).toHaveBeenCalledWith(
59
+ mockBackendPath,
60
+ mockApiModuleName
61
+ );
62
+ expect(updateBackendJsFile).toHaveBeenCalledWith(
63
+ mockBackendPath,
64
+ mockApiModuleName
65
+ );
66
+ expect(commitChanges).toHaveBeenCalledWith(
67
+ mockBackendPath,
68
+ mockApiModuleName
69
+ );
70
+ expect(logInfo).toHaveBeenCalledWith(
71
+ `Successfully installed ${mockPackageName} and updated the project.`
72
+ );
73
+ });
74
+
75
+ it('should log an error and exit with code 1 if the package does not exist', async () => {
76
+ const mockExit = jest
77
+ .spyOn(process, 'exit')
78
+ .mockImplementation(() => {});
79
+ const mockLogError = jest
80
+ .spyOn(require('./install-command/logger'), 'logError')
81
+ .mockImplementation(() => {});
82
+ const mockValidatePackageExists = jest
83
+ .spyOn(
84
+ require('./install-command/validate-package'),
85
+ 'validatePackageExists'
86
+ )
87
+ .mockImplementation(() => {
88
+ throw new Error('Package not found');
89
+ });
90
+
91
+ const program = new Command();
92
+ program
93
+ .command('install <apiModuleName>')
94
+ .description('Install an API module')
95
+ .action(installCommand);
96
+
97
+ await program.parseAsync(['node', 'install', 'nonexistent-package']);
98
+
99
+ expect(mockValidatePackageExists).toHaveBeenCalledWith(
100
+ '@friggframework/api-module-nonexistent-package'
101
+ );
102
+ expect(mockLogError).toHaveBeenCalledWith(
103
+ 'An error occurred:',
104
+ expect.any(Error)
105
+ );
106
+ expect(mockExit).toHaveBeenCalledWith(1);
107
+
108
+ mockExit.mockRestore();
109
+ mockLogError.mockRestore();
110
+ mockValidatePackageExists.mockRestore();
111
+ });
112
+
113
+ it('should log an error and exit with code 1 if the backend path is invalid', async () => {
114
+ const mockLogError = jest
115
+ .spyOn(require('./install-command/logger'), 'logError')
116
+ .mockImplementation(() => {});
117
+ const mockProcessExit = jest
118
+ .spyOn(process, 'exit')
119
+ .mockImplementation(() => {});
120
+ const mockValidatePackageExists = jest
121
+ .spyOn(
122
+ require('./install-command/validate-package'),
123
+ 'validatePackageExists'
124
+ )
125
+ .mockResolvedValue(true);
126
+ const mockFindNearestBackendPackageJson = jest
127
+ .spyOn(
128
+ require('./utils/backend-path'),
129
+ 'findNearestBackendPackageJson'
130
+ )
131
+ .mockReturnValue('/invalid/path');
132
+ const mockValidateBackendPath = jest
133
+ .spyOn(require('./utils/backend-path'), 'validateBackendPath')
134
+ .mockImplementation(() => {
135
+ throw new Error('Invalid backend path');
136
+ });
137
+
138
+ const program = new Command();
139
+ program
140
+ .command('install <apiModuleName>')
141
+ .description('Install an API module')
142
+ .action(installCommand);
143
+
144
+ await program.parseAsync(['node', 'install', 'test-module']);
145
+
146
+ expect(mockLogError).toHaveBeenCalledWith(
147
+ 'An error occurred:',
148
+ expect.any(Error)
149
+ );
150
+ expect(mockProcessExit).toHaveBeenCalledWith(1);
151
+
152
+ mockLogError.mockRestore();
153
+ mockProcessExit.mockRestore();
154
+ mockValidatePackageExists.mockRestore();
155
+ mockFindNearestBackendPackageJson.mockRestore();
156
+ mockValidateBackendPath.mockRestore();
157
+ });
158
+ });