@friggframework/devtools 2.0.0-next.41 → 2.0.0-next.43
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/__tests__/unit/commands/build.test.js +173 -405
- package/frigg-cli/__tests__/unit/commands/db-setup.test.js +548 -0
- package/frigg-cli/__tests__/unit/commands/install.test.js +359 -377
- package/frigg-cli/__tests__/unit/commands/ui.test.js +266 -512
- package/frigg-cli/__tests__/unit/utils/database-validator.test.js +366 -0
- package/frigg-cli/__tests__/unit/utils/error-messages.test.js +304 -0
- package/frigg-cli/__tests__/unit/utils/prisma-runner.test.js +486 -0
- package/frigg-cli/__tests__/utils/prisma-mock.js +194 -0
- package/frigg-cli/__tests__/utils/test-setup.js +22 -21
- package/frigg-cli/db-setup-command/index.js +186 -0
- package/frigg-cli/generate-command/__tests__/generate-command.test.js +151 -162
- package/frigg-cli/generate-iam-command.js +7 -4
- package/frigg-cli/index.js +9 -1
- package/frigg-cli/install-command/index.js +1 -1
- package/frigg-cli/jest.config.js +124 -0
- package/frigg-cli/package.json +4 -1
- package/frigg-cli/start-command/index.js +95 -2
- package/frigg-cli/start-command/start-command.test.js +161 -19
- package/frigg-cli/utils/database-validator.js +158 -0
- package/frigg-cli/utils/error-messages.js +257 -0
- package/frigg-cli/utils/prisma-runner.js +280 -0
- package/infrastructure/CLAUDE.md +481 -0
- package/infrastructure/IAM-POLICY-TEMPLATES.md +30 -12
- package/infrastructure/create-frigg-infrastructure.js +0 -2
- package/infrastructure/iam-generator.js +18 -38
- package/infrastructure/iam-generator.test.js +40 -8
- package/management-ui/src/App.jsx +1 -85
- package/management-ui/src/hooks/useFrigg.jsx +1 -215
- package/package.json +6 -6
- package/test/index.js +2 -4
- package/test/mock-integration.js +4 -14
- package/frigg-cli/__tests__/jest.config.js +0 -102
- package/frigg-cli/__tests__/utils/command-tester.js +0 -170
- package/test/auther-definition-tester.js +0 -125
|
@@ -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('../../../utils/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
|
+
});
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prisma Mock Utilities
|
|
3
|
+
* Standardized mocking for Prisma Client in CLI tests
|
|
4
|
+
* Following official Prisma testing patterns with jest-mock-extended
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a mock Prisma client with common operations
|
|
9
|
+
* @returns {object} Mock Prisma client
|
|
10
|
+
*/
|
|
11
|
+
function createMockPrismaClient() {
|
|
12
|
+
return {
|
|
13
|
+
$connect: jest.fn().mockResolvedValue(undefined),
|
|
14
|
+
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
15
|
+
$queryRaw: jest.fn().mockResolvedValue([{ result: 1 }]),
|
|
16
|
+
$executeRaw: jest.fn().mockResolvedValue(1),
|
|
17
|
+
$runCommandRaw: jest.fn().mockResolvedValue({ ok: 1 }), // MongoDB command support
|
|
18
|
+
$transaction: jest.fn().mockImplementation(async (fn) => fn(createMockPrismaClient())),
|
|
19
|
+
// Common model operations
|
|
20
|
+
user: {
|
|
21
|
+
findMany: jest.fn().mockResolvedValue([]),
|
|
22
|
+
findUnique: jest.fn().mockResolvedValue(null),
|
|
23
|
+
create: jest.fn().mockResolvedValue({}),
|
|
24
|
+
update: jest.fn().mockResolvedValue({}),
|
|
25
|
+
delete: jest.fn().mockResolvedValue({}),
|
|
26
|
+
},
|
|
27
|
+
credential: {
|
|
28
|
+
findMany: jest.fn().mockResolvedValue([]),
|
|
29
|
+
findUnique: jest.fn().mockResolvedValue(null),
|
|
30
|
+
create: jest.fn().mockResolvedValue({}),
|
|
31
|
+
update: jest.fn().mockResolvedValue({}),
|
|
32
|
+
delete: jest.fn().mockResolvedValue({}),
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates a mock for successful Prisma connection
|
|
39
|
+
* @param {object} mockClient - Optional pre-configured mock client
|
|
40
|
+
* @returns {jest.Mock} Mock connectPrisma function
|
|
41
|
+
*/
|
|
42
|
+
function createMockConnectPrisma(mockClient = null) {
|
|
43
|
+
const client = mockClient || createMockPrismaClient();
|
|
44
|
+
return jest.fn().mockResolvedValue(client);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Creates a mock for successful Prisma disconnection
|
|
49
|
+
* @returns {jest.Mock} Mock disconnectPrisma function
|
|
50
|
+
*/
|
|
51
|
+
function createMockDisconnectPrisma() {
|
|
52
|
+
return jest.fn().mockResolvedValue(undefined);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Creates a mock for Prisma connection that fails
|
|
57
|
+
* @param {string} errorMessage - Error message to throw
|
|
58
|
+
* @returns {jest.Mock} Mock connectPrisma that throws error
|
|
59
|
+
*/
|
|
60
|
+
function createMockConnectPrismaWithError(errorMessage = 'Connection failed') {
|
|
61
|
+
return jest.fn().mockRejectedValue(new Error(errorMessage));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates a mock for Prisma query that times out
|
|
66
|
+
* @param {number} timeout - Timeout in milliseconds
|
|
67
|
+
* @returns {jest.Mock} Mock that times out
|
|
68
|
+
*/
|
|
69
|
+
function createMockPrismaTimeout(timeout = 5000) {
|
|
70
|
+
return jest.fn().mockImplementation(() =>
|
|
71
|
+
new Promise((_, reject) =>
|
|
72
|
+
setTimeout(() => reject(new Error('Connection timeout')), timeout)
|
|
73
|
+
)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Common Prisma error types for testing
|
|
79
|
+
*/
|
|
80
|
+
const PrismaErrors = {
|
|
81
|
+
/**
|
|
82
|
+
* Connection error (P1001)
|
|
83
|
+
*/
|
|
84
|
+
CONNECTION_ERROR: {
|
|
85
|
+
code: 'P1001',
|
|
86
|
+
message: 'Can\'t reach database server',
|
|
87
|
+
clientVersion: '5.0.0',
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Authentication error (P1002)
|
|
92
|
+
*/
|
|
93
|
+
AUTH_ERROR: {
|
|
94
|
+
code: 'P1002',
|
|
95
|
+
message: 'The database server was reached but timed out',
|
|
96
|
+
clientVersion: '5.0.0',
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Database not found error (P1003)
|
|
101
|
+
*/
|
|
102
|
+
DATABASE_NOT_FOUND: {
|
|
103
|
+
code: 'P1003',
|
|
104
|
+
message: 'Database does not exist',
|
|
105
|
+
clientVersion: '5.0.0',
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Connection timeout (P1008)
|
|
110
|
+
*/
|
|
111
|
+
TIMEOUT_ERROR: {
|
|
112
|
+
code: 'P1008',
|
|
113
|
+
message: 'Operations timed out',
|
|
114
|
+
clientVersion: '5.0.0',
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Invalid credentials
|
|
119
|
+
*/
|
|
120
|
+
INVALID_CREDENTIALS: {
|
|
121
|
+
code: 'P1000',
|
|
122
|
+
message: 'Authentication failed against database server',
|
|
123
|
+
clientVersion: '5.0.0',
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Creates a Prisma error object
|
|
129
|
+
* @param {string} errorType - Error type from PrismaErrors
|
|
130
|
+
* @param {object} customFields - Additional custom fields
|
|
131
|
+
* @returns {Error} Prisma-style error object
|
|
132
|
+
*/
|
|
133
|
+
function createPrismaError(errorType = 'CONNECTION_ERROR', customFields = {}) {
|
|
134
|
+
const baseError = PrismaErrors[errorType] || PrismaErrors.CONNECTION_ERROR;
|
|
135
|
+
const error = new Error(baseError.message);
|
|
136
|
+
error.code = baseError.code;
|
|
137
|
+
error.clientVersion = baseError.clientVersion;
|
|
138
|
+
Object.assign(error, customFields);
|
|
139
|
+
return error;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Creates a mock for database validator functions
|
|
144
|
+
* @param {object} overrides - Override specific mock behaviors
|
|
145
|
+
* @returns {object} Mock database validator
|
|
146
|
+
*/
|
|
147
|
+
function createMockDatabaseValidator(overrides = {}) {
|
|
148
|
+
return {
|
|
149
|
+
validateDatabaseUrl: jest.fn().mockReturnValue({
|
|
150
|
+
valid: true,
|
|
151
|
+
url: 'mongodb://localhost:27017/test'
|
|
152
|
+
}),
|
|
153
|
+
getDatabaseType: jest.fn().mockReturnValue({
|
|
154
|
+
dbType: 'mongodb'
|
|
155
|
+
}),
|
|
156
|
+
testDatabaseConnection: jest.fn().mockResolvedValue({
|
|
157
|
+
connected: true
|
|
158
|
+
}),
|
|
159
|
+
checkPrismaClientGenerated: jest.fn().mockReturnValue({
|
|
160
|
+
generated: true,
|
|
161
|
+
path: '/mock/path'
|
|
162
|
+
}),
|
|
163
|
+
...overrides,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Creates a mock for Prisma runner functions
|
|
169
|
+
* @param {object} overrides - Override specific mock behaviors
|
|
170
|
+
* @returns {object} Mock Prisma runner
|
|
171
|
+
*/
|
|
172
|
+
function createMockPrismaRunner(overrides = {}) {
|
|
173
|
+
return {
|
|
174
|
+
getPrismaSchemaPath: jest.fn().mockReturnValue('/mock/schema.prisma'),
|
|
175
|
+
runPrismaGenerate: jest.fn().mockResolvedValue({ success: true }),
|
|
176
|
+
checkDatabaseState: jest.fn().mockResolvedValue({ upToDate: true }),
|
|
177
|
+
runPrismaMigrate: jest.fn().mockResolvedValue({ success: true }),
|
|
178
|
+
runPrismaDbPush: jest.fn().mockResolvedValue({ success: true }),
|
|
179
|
+
getMigrationCommand: jest.fn().mockReturnValue('dev'),
|
|
180
|
+
...overrides,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
module.exports = {
|
|
185
|
+
createMockPrismaClient,
|
|
186
|
+
createMockConnectPrisma,
|
|
187
|
+
createMockDisconnectPrisma,
|
|
188
|
+
createMockConnectPrismaWithError,
|
|
189
|
+
createMockPrismaTimeout,
|
|
190
|
+
createPrismaError,
|
|
191
|
+
PrismaErrors,
|
|
192
|
+
createMockDatabaseValidator,
|
|
193
|
+
createMockPrismaRunner,
|
|
194
|
+
};
|