@friggframework/core 2.0.0--canary.463.62579dd.0 → 2.0.0--canary.461.84ff4f5.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.
@@ -0,0 +1,486 @@
1
+ // Mock dependencies BEFORE requiring modules
2
+ jest.mock('child_process', () => ({
3
+ execSync: jest.fn(),
4
+ spawn: jest.fn()
5
+ }));
6
+ jest.mock('fs', () => ({
7
+ existsSync: jest.fn(),
8
+ readFileSync: jest.fn(),
9
+ writeFileSync: jest.fn()
10
+ }));
11
+
12
+ const { execSync, spawn } = require('child_process');
13
+ const fs = require('fs');
14
+ const {
15
+ getPrismaSchemaPath,
16
+ runPrismaGenerate,
17
+ checkDatabaseState,
18
+ runPrismaMigrate,
19
+ runPrismaDbPush,
20
+ getMigrationCommand
21
+ } = require('./prisma-runner');
22
+
23
+ describe('Prisma Runner Utility', () => {
24
+ beforeEach(() => {
25
+ jest.clearAllMocks();
26
+ delete process.env.STAGE;
27
+ delete process.env.PRISMA_HIDE_UPDATE_MESSAGE;
28
+ });
29
+
30
+ afterEach(() => {
31
+ delete process.env.STAGE;
32
+ delete process.env.PRISMA_HIDE_UPDATE_MESSAGE;
33
+ });
34
+
35
+ describe('getPrismaSchemaPath()', () => {
36
+ it('should return correct path for MongoDB', () => {
37
+ fs.existsSync.mockReturnValue(true);
38
+
39
+ const path = getPrismaSchemaPath('mongodb');
40
+
41
+ expect(path).toContain('prisma-mongodb');
42
+ expect(path).toContain('schema.prisma');
43
+ expect(path).toContain('@friggframework/core');
44
+ });
45
+
46
+ it('should return correct path for PostgreSQL', () => {
47
+ fs.existsSync.mockReturnValue(true);
48
+
49
+ const path = getPrismaSchemaPath('postgresql');
50
+
51
+ expect(path).toContain('prisma-postgresql');
52
+ expect(path).toContain('schema.prisma');
53
+ expect(path).toContain('@friggframework/core');
54
+ });
55
+
56
+ it('should throw error when schema file does not exist', () => {
57
+ fs.existsSync.mockReturnValue(false);
58
+
59
+ expect(() => getPrismaSchemaPath('mongodb')).toThrow('Prisma schema not found');
60
+ });
61
+
62
+ it('should include helpful error message when schema missing', () => {
63
+ fs.existsSync.mockReturnValue(false);
64
+
65
+ expect(() => getPrismaSchemaPath('mongodb')).toThrow('@friggframework/core');
66
+ });
67
+
68
+ it('should use process.cwd() for base path', () => {
69
+ fs.existsSync.mockReturnValue(true);
70
+ const originalCwd = process.cwd();
71
+
72
+ const path = getPrismaSchemaPath('mongodb');
73
+
74
+ expect(path).toContain(originalCwd);
75
+ });
76
+
77
+ it('should accept custom project root', () => {
78
+ fs.existsSync.mockReturnValue(true);
79
+ const customRoot = '/custom/project';
80
+
81
+ const path = getPrismaSchemaPath('mongodb', customRoot);
82
+
83
+ expect(path).toContain(customRoot);
84
+ });
85
+ });
86
+
87
+ describe('runPrismaGenerate()', () => {
88
+ beforeEach(() => {
89
+ fs.existsSync.mockReturnValue(true);
90
+ });
91
+
92
+ it('should execute prisma generate successfully', async () => {
93
+ execSync.mockReturnValue('Generated successfully');
94
+
95
+ const result = await runPrismaGenerate('mongodb');
96
+
97
+ expect(result.success).toBe(true);
98
+ expect(execSync).toHaveBeenCalled();
99
+ });
100
+
101
+ it('should use correct schema path for MongoDB', async () => {
102
+ execSync.mockReturnValue('');
103
+
104
+ await runPrismaGenerate('mongodb');
105
+
106
+ const call = execSync.mock.calls[0][0];
107
+ expect(call).toContain('prisma generate');
108
+ expect(call).toContain('--schema');
109
+ expect(call).toContain('prisma-mongodb');
110
+ });
111
+
112
+ it('should use correct schema path for PostgreSQL', async () => {
113
+ execSync.mockReturnValue('');
114
+
115
+ await runPrismaGenerate('postgresql');
116
+
117
+ const call = execSync.mock.calls[0][0];
118
+ expect(call).toContain('prisma-postgresql');
119
+ });
120
+
121
+ it('should suppress telemetry when verbose false', async () => {
122
+ execSync.mockReturnValue('');
123
+
124
+ await runPrismaGenerate('mongodb', false);
125
+
126
+ const options = execSync.mock.calls[0][1];
127
+ expect(options.env.PRISMA_HIDE_UPDATE_MESSAGE).toBe('1');
128
+ });
129
+
130
+ it('should show output when verbose true', async () => {
131
+ execSync.mockReturnValue('Generated successfully');
132
+
133
+ await runPrismaGenerate('mongodb', true);
134
+
135
+ const options = execSync.mock.calls[0][1];
136
+ expect(options.stdio).toBe('inherit');
137
+ });
138
+
139
+ it('should hide output when verbose false', async () => {
140
+ execSync.mockReturnValue('');
141
+
142
+ await runPrismaGenerate('mongodb', false);
143
+
144
+ const options = execSync.mock.calls[0][1];
145
+ expect(options.stdio).toBe('pipe');
146
+ });
147
+
148
+ it('should handle generation failures with error details', async () => {
149
+ const error = new Error('Generation failed');
150
+ error.stdout = 'Schema validation error';
151
+ execSync.mockImplementation(() => {
152
+ throw error;
153
+ });
154
+
155
+ const result = await runPrismaGenerate('mongodb');
156
+
157
+ expect(result.success).toBe(false);
158
+ expect(result.error).toContain('Generation failed');
159
+ });
160
+
161
+ it('should include stdout in error output', async () => {
162
+ const error = new Error('Failed');
163
+ error.stdout = Buffer.from('Detailed error info');
164
+ execSync.mockImplementation(() => {
165
+ throw error;
166
+ });
167
+
168
+ const result = await runPrismaGenerate('mongodb');
169
+
170
+ expect(result.output).toContain('Detailed error info');
171
+ });
172
+
173
+ it('should handle schema syntax errors', async () => {
174
+ execSync.mockImplementation(() => {
175
+ throw new Error('Schema parsing failed');
176
+ });
177
+
178
+ const result = await runPrismaGenerate('mongodb');
179
+
180
+ expect(result.success).toBe(false);
181
+ expect(result.error).toBeDefined();
182
+ });
183
+ });
184
+
185
+ describe('checkDatabaseState()', () => {
186
+ beforeEach(() => {
187
+ fs.existsSync.mockReturnValue(true);
188
+ });
189
+
190
+ it('should return upToDate: true when migrations current (PostgreSQL)', async () => {
191
+ execSync.mockReturnValue('Database schema is up to date');
192
+
193
+ const result = await checkDatabaseState('postgresql');
194
+
195
+ expect(result.upToDate).toBe(true);
196
+ });
197
+
198
+ it('should return upToDate: true for MongoDB (N/A)', async () => {
199
+ const result = await checkDatabaseState('mongodb');
200
+
201
+ expect(result.upToDate).toBe(true);
202
+ });
203
+
204
+ it('should return pendingMigrations count when migrations pending', async () => {
205
+ execSync.mockReturnValue('3 migrations have not been applied');
206
+
207
+ const result = await checkDatabaseState('postgresql');
208
+
209
+ expect(result.upToDate).toBe(false);
210
+ expect(result.pendingMigrations).toBe(3);
211
+ });
212
+
213
+ it('should handle uninitialized database', async () => {
214
+ execSync.mockImplementation(() => {
215
+ throw new Error('No migrations found');
216
+ });
217
+
218
+ const result = await checkDatabaseState('postgresql');
219
+
220
+ expect(result.upToDate).toBe(false);
221
+ expect(result.error).toBeDefined();
222
+ });
223
+
224
+ it('should handle migrate status command errors', async () => {
225
+ execSync.mockImplementation(() => {
226
+ throw new Error('Migration status failed');
227
+ });
228
+
229
+ const result = await checkDatabaseState('postgresql');
230
+
231
+ expect(result.upToDate).toBe(false);
232
+ expect(result.error).toContain('Migration status failed');
233
+ });
234
+
235
+ it('should not run migrate status for MongoDB', async () => {
236
+ await checkDatabaseState('mongodb');
237
+
238
+ expect(execSync).not.toHaveBeenCalled();
239
+ });
240
+ });
241
+
242
+ describe('runPrismaMigrate()', () => {
243
+ let mockChildProcess;
244
+
245
+ beforeEach(() => {
246
+ fs.existsSync.mockReturnValue(true);
247
+ mockChildProcess = {
248
+ on: jest.fn((event, callback) => {
249
+ if (event === 'close') {
250
+ callback(0);
251
+ }
252
+ }),
253
+ stdout: { on: jest.fn() },
254
+ stderr: { on: jest.fn() }
255
+ };
256
+ spawn.mockReturnValue(mockChildProcess);
257
+ });
258
+
259
+ it('should run migrate dev successfully', async () => {
260
+ const result = await runPrismaMigrate('dev');
261
+
262
+ expect(result.success).toBe(true);
263
+ expect(spawn).toHaveBeenCalled();
264
+ });
265
+
266
+ it('should run migrate deploy successfully', async () => {
267
+ const result = await runPrismaMigrate('deploy');
268
+
269
+ expect(result.success).toBe(true);
270
+ expect(spawn).toHaveBeenCalled();
271
+ });
272
+
273
+ it('should use correct command for dev mode', async () => {
274
+ await runPrismaMigrate('dev');
275
+
276
+ const args = spawn.mock.calls[0][1];
277
+ expect(args).toContain('migrate');
278
+ expect(args).toContain('dev');
279
+ });
280
+
281
+ it('should use correct command for deploy mode', async () => {
282
+ await runPrismaMigrate('deploy');
283
+
284
+ const args = spawn.mock.calls[0][1];
285
+ expect(args).toContain('migrate');
286
+ expect(args).toContain('deploy');
287
+ });
288
+
289
+ it('should handle migration failures with error', async () => {
290
+ mockChildProcess.on.mockImplementation((event, callback) => {
291
+ if (event === 'close') {
292
+ callback(1); // Exit code 1
293
+ }
294
+ });
295
+
296
+ const result = await runPrismaMigrate('dev');
297
+
298
+ expect(result.success).toBe(false);
299
+ expect(result.error).toContain('exited with code 1');
300
+ });
301
+
302
+ it('should respect verbose flag', async () => {
303
+ await runPrismaMigrate('dev', true);
304
+
305
+ const options = spawn.mock.calls[0][2];
306
+ expect(options.stdio).toBe('inherit');
307
+ });
308
+
309
+ it('should handle process spawn errors', async () => {
310
+ mockChildProcess.on.mockImplementation((event, callback) => {
311
+ if (event === 'error') {
312
+ callback(new Error('Spawn failed'));
313
+ }
314
+ });
315
+
316
+ const result = await runPrismaMigrate('dev');
317
+
318
+ expect(result.success).toBe(false);
319
+ });
320
+
321
+ it('should hide telemetry messages', async () => {
322
+ await runPrismaMigrate('dev');
323
+
324
+ const options = spawn.mock.calls[0][2];
325
+ expect(options.env.PRISMA_HIDE_UPDATE_MESSAGE).toBe('1');
326
+ });
327
+ });
328
+
329
+ describe('runPrismaDbPush()', () => {
330
+ let mockChildProcess;
331
+
332
+ beforeEach(() => {
333
+ fs.existsSync.mockReturnValue(true);
334
+ mockChildProcess = {
335
+ on: jest.fn((event, callback) => {
336
+ if (event === 'close') {
337
+ callback(0);
338
+ }
339
+ }),
340
+ stdout: { on: jest.fn() },
341
+ stderr: { on: jest.fn() }
342
+ };
343
+ spawn.mockReturnValue(mockChildProcess);
344
+ });
345
+
346
+ it('should push schema successfully for MongoDB', async () => {
347
+ const result = await runPrismaDbPush();
348
+
349
+ expect(result.success).toBe(true);
350
+ expect(spawn).toHaveBeenCalled();
351
+ });
352
+
353
+ it('should use --skip-generate flag', async () => {
354
+ await runPrismaDbPush();
355
+
356
+ const args = spawn.mock.calls[0][1];
357
+ expect(args).toContain('--skip-generate');
358
+ });
359
+
360
+ it('should use db push command', async () => {
361
+ await runPrismaDbPush();
362
+
363
+ const args = spawn.mock.calls[0][1];
364
+ expect(args).toContain('db');
365
+ expect(args).toContain('push');
366
+ });
367
+
368
+ it('should handle push failures with error', async () => {
369
+ mockChildProcess.on.mockImplementation((event, callback) => {
370
+ if (event === 'close') {
371
+ callback(1);
372
+ }
373
+ });
374
+
375
+ const result = await runPrismaDbPush();
376
+
377
+ expect(result.success).toBe(false);
378
+ expect(result.error).toContain('exited with code 1');
379
+ });
380
+
381
+ it('should respect verbose flag', async () => {
382
+ await runPrismaDbPush(true);
383
+
384
+ const options = spawn.mock.calls[0][2];
385
+ expect(options.stdio).toBe('inherit');
386
+ });
387
+
388
+ it('should use interactive mode (stdio: inherit)', async () => {
389
+ await runPrismaDbPush();
390
+
391
+ const options = spawn.mock.calls[0][2];
392
+ expect(options.stdio).toBe('inherit');
393
+ });
394
+
395
+ it('should handle schema validation errors', async () => {
396
+ mockChildProcess.on.mockImplementation((event, callback) => {
397
+ if (event === 'close') {
398
+ callback(1);
399
+ }
400
+ });
401
+
402
+ const result = await runPrismaDbPush();
403
+
404
+ expect(result.success).toBe(false);
405
+ });
406
+ });
407
+
408
+ describe('getMigrationCommand()', () => {
409
+ it('should return dev for development stage', () => {
410
+ const command = getMigrationCommand('development');
411
+
412
+ expect(command).toBe('dev');
413
+ });
414
+
415
+ it('should return dev for dev stage', () => {
416
+ const command = getMigrationCommand('dev');
417
+
418
+ expect(command).toBe('dev');
419
+ });
420
+
421
+ it('should return dev for local stage', () => {
422
+ const command = getMigrationCommand('local');
423
+
424
+ expect(command).toBe('dev');
425
+ });
426
+
427
+ it('should return dev for test stage', () => {
428
+ const command = getMigrationCommand('test');
429
+
430
+ expect(command).toBe('dev');
431
+ });
432
+
433
+ it('should return deploy for production stage', () => {
434
+ const command = getMigrationCommand('production');
435
+
436
+ expect(command).toBe('deploy');
437
+ });
438
+
439
+ it('should return deploy for prod stage', () => {
440
+ const command = getMigrationCommand('prod');
441
+
442
+ expect(command).toBe('deploy');
443
+ });
444
+
445
+ it('should return deploy for staging stage', () => {
446
+ const command = getMigrationCommand('staging');
447
+
448
+ expect(command).toBe('deploy');
449
+ });
450
+
451
+ it('should default to dev when stage undefined', () => {
452
+ const command = getMigrationCommand();
453
+
454
+ expect(command).toBe('dev');
455
+ });
456
+
457
+ it('should read from STAGE environment variable when no argument', () => {
458
+ process.env.STAGE = 'production';
459
+
460
+ const command = getMigrationCommand();
461
+
462
+ expect(command).toBe('deploy');
463
+ });
464
+
465
+ it('should prioritize argument over STAGE env var', () => {
466
+ process.env.STAGE = 'production';
467
+
468
+ const command = getMigrationCommand('development');
469
+
470
+ expect(command).toBe('dev');
471
+ });
472
+
473
+ it('should handle case-insensitive stage names', () => {
474
+ expect(getMigrationCommand('DEVELOPMENT')).toBe('dev');
475
+ expect(getMigrationCommand('PRODUCTION')).toBe('deploy');
476
+ expect(getMigrationCommand('Dev')).toBe('dev');
477
+ expect(getMigrationCommand('Prod')).toBe('deploy');
478
+ });
479
+
480
+ it('should return deploy for unknown stages', () => {
481
+ const command = getMigrationCommand('unknown-stage');
482
+
483
+ expect(command).toBe('deploy');
484
+ });
485
+ });
486
+ });
@@ -19,7 +19,6 @@ const {
19
19
  const {
20
20
  getModulesDefinitionFromIntegrationClasses,
21
21
  } = require('../integrations/utils/map-integration-dto');
22
- const { loadAppDefinition } = require('./app-definition-loader');
23
22
 
24
23
  const loadRouterFromObject = (IntegrationClass, routerObject) => {
25
24
  const router = Router();
@@ -51,29 +50,37 @@ const loadRouterFromObject = (IntegrationClass, routerObject) => {
51
50
  };
52
51
 
53
52
  const initializeRepositories = () => {
54
- return {
55
- processRepository: createProcessRepository(),
56
- integrationRepository: createIntegrationRepository(),
57
- moduleRepository: createModuleRepository(),
58
- };
53
+ const processRepository = createProcessRepository();
54
+ const integrationRepository = createIntegrationRepository();
55
+ const moduleRepository = createModuleRepository();
56
+
57
+ return { processRepository, integrationRepository, moduleRepository };
58
+ };
59
+
60
+ const createModuleFactoryWithDefinitions = (
61
+ moduleRepository,
62
+ integrationClasses
63
+ ) => {
64
+ const moduleDefinitions =
65
+ getModulesDefinitionFromIntegrationClasses(integrationClasses);
66
+
67
+ return new ModuleFactory({
68
+ moduleRepository,
69
+ moduleDefinitions,
70
+ });
59
71
  };
60
72
 
61
- /**
62
- * Load hydrated integration instance for webhook events WITH integrationId.
63
- * Must load app definition to find all integration classes, then match
64
- * against the integration record's type.
65
- */
66
73
  const loadIntegrationForWebhook = async (integrationId) => {
74
+ const { loadAppDefinition } = require('./app-definition-loader');
67
75
  const { integrations: integrationClasses } = loadAppDefinition();
68
- const { integrationRepository, moduleRepository } = initializeRepositories();
69
76
 
70
- const moduleDefinitions = getModulesDefinitionFromIntegrationClasses(
77
+ const { integrationRepository, moduleRepository } =
78
+ initializeRepositories();
79
+
80
+ const moduleFactory = createModuleFactoryWithDefinitions(
81
+ moduleRepository,
71
82
  integrationClasses
72
83
  );
73
- const moduleFactory = new ModuleFactory({
74
- moduleRepository,
75
- moduleDefinitions,
76
- });
77
84
 
78
85
  const getIntegrationInstance = new GetIntegrationInstance({
79
86
  integrationRepository,
@@ -85,42 +92,19 @@ const loadIntegrationForWebhook = async (integrationId) => {
85
92
  integrationId
86
93
  );
87
94
 
88
- if (!integrationRecord) {
89
- throw new Error(
90
- `No integration found by the ID of ${integrationId}`
91
- );
92
- }
93
-
94
95
  return await getIntegrationInstance.execute(
95
96
  integrationId,
96
97
  integrationRecord.userId
97
98
  );
98
99
  };
99
100
 
100
- /**
101
- * Load hydrated integration instance for process-based events.
102
- * Integration class is already known from queue worker context,
103
- * so we receive it as a parameter rather than loading all classes.
104
- */
105
101
  const loadIntegrationForProcess = async (processId, integrationClass) => {
106
- if (!integrationClass) {
107
- throw new Error('integrationClass parameter is required');
108
- }
109
-
110
- if (!processId) {
111
- throw new Error('processId is required in queue message data');
112
- }
113
-
114
102
  const { processRepository, integrationRepository, moduleRepository } =
115
103
  initializeRepositories();
116
104
 
117
- const moduleDefinitions = getModulesDefinitionFromIntegrationClasses([
105
+ const moduleFactory = createModuleFactoryWithDefinitions(moduleRepository, [
118
106
  integrationClass,
119
107
  ]);
120
- const moduleFactory = new ModuleFactory({
121
- moduleRepository,
122
- moduleDefinitions,
123
- });
124
108
 
125
109
  const getIntegrationInstance = new GetIntegrationInstance({
126
110
  integrationRepository,
@@ -128,6 +112,10 @@ const loadIntegrationForProcess = async (processId, integrationClass) => {
128
112
  moduleFactory,
129
113
  });
130
114
 
115
+ if (!processId) {
116
+ throw new Error('processId is required in queue message data');
117
+ }
118
+
131
119
  const process = await processRepository.findById(processId);
132
120
 
133
121
  if (!process) {