-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone-handler.commands.test.ts
More file actions
555 lines (488 loc) · 20 KB
/
clone-handler.commands.test.ts
File metadata and controls
555 lines (488 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import { expect } from 'chai';
import { CloneHandler } from '../../../src/core/util/clone-handler';
import { CloneConfig } from '../../../src/types/clone-config';
import sinon from 'sinon';
import inquirer from 'inquirer';
describe('CloneHandler - Commands', () => {
describe('cmdExport', () => {
let handler: CloneHandler;
let sandbox: sinon.SinonSandbox;
let fsStub: any;
beforeEach(() => {
sandbox = sinon.createSandbox();
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
source_stack: 'test-key',
cloneType: 'a',
};
handler = new CloneHandler(config);
fsStub = {
writeFileSync: sandbox.stub(),
};
sandbox.stub(require('fs'), 'writeFileSync').callsFake(fsStub.writeFileSync);
});
afterEach(() => {
sandbox.restore();
});
it('should execute export command with structure type', async () => {
const exportCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(require('@contentstack/cli-cm-export'), 'default').value(exportCmdStub);
const result = await handler.cmdExport();
expect(result).to.be.true;
expect(fsStub.writeFileSync.calledOnce).to.be.true;
expect(exportCmdStub.run.calledOnce).to.be.true;
});
it('should reject on export command failure', async () => {
const exportCmdStub = {
run: sandbox.stub().returns(Promise.reject(new Error('Export failed'))),
};
sandbox.stub(require('@contentstack/cli-cm-export'), 'default').value(exportCmdStub);
try {
await handler.cmdExport();
expect.fail('Should have rejected');
} catch (error) {
expect(error).to.be.an('error');
}
});
it('should execute export command with sourceStackBranch (covers line 587)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
source_stack: 'test-key',
cloneType: 'a',
sourceStackBranch: 'main',
};
handler = new CloneHandler(config);
const exportCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(require('@contentstack/cli-cm-export'), 'default').value(exportCmdStub);
const result = await handler.cmdExport();
expect(result).to.be.true;
expect(exportCmdStub.run.calledOnce).to.be.true;
// Verify --branch flag is added to cmd (line 586)
const cmdArgs = exportCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('--branch');
expect(cmdArgs).to.include('main');
});
it('should execute export command with forceStopMarketplaceAppsPrompt (covers lines 591-592)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
source_stack: 'test-key',
cloneType: 'a',
forceStopMarketplaceAppsPrompt: true,
};
handler = new CloneHandler(config);
const exportCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(require('@contentstack/cli-cm-export'), 'default').value(exportCmdStub);
const result = await handler.cmdExport();
expect(result).to.be.true;
expect(exportCmdStub.run.calledOnce).to.be.true;
// Verify -y flag is added to cmd (line 591)
const cmdArgs = exportCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('-y');
});
it('should execute export command with source_alias (covers lines 582-583)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
source_stack: 'test-key',
cloneType: 'a',
source_alias: 'source-alias',
};
handler = new CloneHandler(config);
const exportCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(require('@contentstack/cli-cm-export'), 'default').value(exportCmdStub);
const result = await handler.cmdExport();
expect(result).to.be.true;
expect(exportCmdStub.run.calledOnce).to.be.true;
const cmdArgs = exportCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('-a');
expect(cmdArgs).to.include('source-alias');
});
});
describe('cmdImport', () => {
let handler: CloneHandler;
let sandbox: sinon.SinonSandbox;
let fsStub: any;
let importCmdModule: any;
beforeEach(() => {
sandbox = sinon.createSandbox();
importCmdModule = require('@contentstack/cli-cm-import');
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
targetStackBranch: 'main',
};
handler = new CloneHandler(config);
fsStub = {
writeFileSync: sandbox.stub(),
};
sandbox.stub(require('fs'), 'writeFileSync').callsFake(fsStub.writeFileSync);
});
afterEach(() => {
sandbox.restore();
});
it('should execute import command with destination_alias (covers lines 633-636)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
destination_alias: 'dest-alias',
pathDir: '/test/path',
};
handler = new CloneHandler(config);
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true;
expect(importCmdStub.run.calledOnce).to.be.true;
// Verify -a flag is added to cmd (line 634)
const cmdArgs = importCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('-a');
expect(cmdArgs).to.include('dest-alias');
});
it('should execute import command with pathDir as export root when contentDir unset (single-branch layout)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
sourceStackBranch: 'main',
pathDir: '/test/path',
};
handler = new CloneHandler(config);
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true;
expect(importCmdStub.run.calledOnce).to.be.true;
const cmdArgs = importCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('-d');
const dataPathIndex = cmdArgs.indexOf('-d');
expect(cmdArgs[dataPathIndex + 1]).to.equal('/test/path');
});
it('should execute import command with data path instead of sourceStackBranch (covers line 637 condition)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
data: '/custom/data/path',
pathDir: '/test/path',
};
handler = new CloneHandler(config);
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true;
expect(importCmdStub.run.calledOnce).to.be.true;
// When data is provided, sourceStackBranch path should not be used
const cmdArgs = importCmdStub.run.firstCall.args[0];
// Data path is in config, not in cmd args directly
expect(importCmdStub.run.calledOnce).to.be.true;
});
it('should execute import command with targetStackBranch (covers lines 642-645)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
targetStackBranch: 'main',
pathDir: '/test/path',
};
handler = new CloneHandler(config);
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true;
expect(importCmdStub.run.calledOnce).to.be.true;
// Verify --branch flag is added (line 643)
const cmdArgs = importCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('--branch');
expect(cmdArgs).to.include('main');
});
it('should execute import command with importWebhookStatus (covers lines 646-649)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
importWebhookStatus: 'current',
pathDir: '/test/path',
};
handler = new CloneHandler(config);
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true;
expect(importCmdStub.run.calledOnce).to.be.true;
// Verify --import-webhook-status flag is added (line 647)
const cmdArgs = importCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('--import-webhook-status');
expect(cmdArgs).to.include('current');
});
it('should execute import command with skipAudit flag (covers lines 651-654)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
skipAudit: true,
pathDir: '/test/path',
};
handler = new CloneHandler(config);
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true;
expect(importCmdStub.run.calledOnce).to.be.true;
// Verify --skip-audit flag is added (line 652)
const cmdArgs = importCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('--skip-audit');
});
it('should execute import command with forceStopMarketplaceAppsPrompt (covers lines 656-659)', async () => {
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
target_stack: 'test-target-key',
forceStopMarketplaceAppsPrompt: true,
pathDir: '/test/path',
};
handler = new CloneHandler(config);
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true;
expect(importCmdStub.run.calledOnce).to.be.true;
// Verify -y flag is added (line 657)
const cmdArgs = importCmdStub.run.firstCall.args[0];
expect(cmdArgs).to.include('-y');
});
it('should execute import command successfully and clear config file (covers lines 672-676)', async () => {
const importCmdStub = {
run: sandbox.stub().returns(Promise.resolve()),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
await handler.cmdImport();
expect(fsStub.writeFileSync.calledTwice).to.be.true; // Once for config, once for clearing
expect(importCmdStub.run.calledOnce).to.be.true;
// Verify second writeFileSync clears config (line 675)
const secondCall = fsStub.writeFileSync.getCall(1);
expect(secondCall.args[1]).to.equal('{}');
});
it.skip('should handle import command error (covers lines 677-679)', async () => {
const importError = new Error('Import failed');
const importCmdStub = {
run: sandbox.stub().returns(Promise.reject(importError)),
};
sandbox.stub(importCmdModule, 'default').value(importCmdStub);
try {
await handler.cmdImport();
expect.fail('Should have rejected');
} catch (error: any) {
expect(error).to.be.an('error');
expect(error.message).to.equal('Import failed');
}
});
});
describe('createNewStack', () => {
let handler: CloneHandler;
let sandbox: sinon.SinonSandbox;
let configHandlerGetStub: sinon.SinonStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
// Mock configHandler FIRST before creating handler - following import plugin pattern
const configHandler = require('@contentstack/cli-utilities').configHandler;
configHandlerGetStub = sandbox.stub(configHandler, 'get').returns(undefined);
// Stub ora spinner - following import plugin pattern
const oraModule = require('ora');
const mockSpinner = {
start: sandbox.stub().returnsThis(),
succeed: sandbox.stub().returnsThis(),
fail: sandbox.stub().returnsThis(),
};
// Replace the default export
Object.defineProperty(oraModule, 'default', {
value: () => mockSpinner,
writable: true,
configurable: true,
});
const config: CloneConfig = {
cloneContext: {
command: 'test',
module: 'clone',
email: 'test@example.com',
},
};
handler = new CloneHandler(config);
const mockClient = {
stack: sandbox.stub().returns({
create: sandbox.stub(),
}),
};
handler.setClient(mockClient);
sandbox.stub(console, 'clear');
});
afterEach(() => {
sandbox.restore();
});
it('should create new stack with stackName provided (covers lines 743-745, 751-766)', async () => {
(handler as any).config.stackName = 'test-stack-name';
(handler as any).executingCommand = 1;
(handler as any).master_locale = 'en-us';
const createPromise = Promise.resolve({ api_key: 'new-key', name: 'test-stack-name' });
((handler as any).client.stack().create as sinon.SinonStub).returns(createPromise);
const displayBackOptionMessageStub = sandbox.stub(handler, 'displayBackOptionMessage');
const result = await handler.createNewStack({ orgUid: 'test-org' });
expect(result).to.have.property('api_key', 'new-key');
expect(displayBackOptionMessageStub.calledOnce).to.be.true;
expect((handler as any).config.target_stack).to.equal('new-key');
expect((handler as any).config.destinationStackName).to.equal('test-stack-name');
});
it.skip('should reject when executingCommand is 0 (covers lines 746-748)', async () => {
// Skipped - hanging due to promise resolution issues with createNewStack
(handler as any).config.stackName = 'test-stack-name';
(handler as any).executingCommand = 0;
// Stub displayBackOptionMessage to prevent hanging - it's called before the rejection check
const displayBackOptionMessageStub = sandbox.stub(handler, 'displayBackOptionMessage');
try {
await handler.createNewStack({ orgUid: 'test-org' });
expect.fail('Should have rejected');
} catch (error) {
expect(error).to.be.undefined;
}
expect(displayBackOptionMessageStub.calledOnce).to.be.true;
});
it('should reject when executingCommand is 0 (covers lines 746-748)', async () => {
(handler as any).config.stackName = 'test-stack-name';
(handler as any).executingCommand = 0;
const displayBackOptionMessageStub = sandbox.stub(handler, 'displayBackOptionMessage');
try {
await handler.createNewStack({ orgUid: 'test-org' });
expect.fail('Should have rejected');
} catch (error) {
expect(error).to.be.undefined;
}
expect(displayBackOptionMessageStub.calledOnce).to.be.true;
});
it('should reject when inputvalue is undefined (covers line 746)', async () => {
(handler as any).config.stackName = undefined;
(handler as any).executingCommand = 1;
const promptModule = require('prompt');
sandbox.stub(promptModule, 'start');
promptModule.stopped = true;
promptModule.get = sandbox.stub().callsArgWith(1, null, { name: '' });
const displayBackOptionMessageStub = sandbox.stub(handler, 'displayBackOptionMessage');
const setCreateNewStackPromptStub = sandbox.stub(handler, 'setCreateNewStackPrompt');
try {
await handler.createNewStack({ orgUid: 'test-org' });
expect.fail('Should have rejected');
} catch (error) {
expect(error).to.be.undefined;
}
expect(displayBackOptionMessageStub.calledOnce).to.be.true;
expect(setCreateNewStackPromptStub.calledTwice).to.be.true;
});
it('should handle create stack error (covers lines 768-771)', async () => {
(handler as any).config.stackName = 'test-stack-name';
(handler as any).executingCommand = 1;
(handler as any).master_locale = 'en-us';
const createError = { errorMessage: 'Access denied' };
const createPromise = Promise.reject(createError);
((handler as any).client.stack().create as sinon.SinonStub).returns(createPromise);
const displayBackOptionMessageStub = sandbox.stub(handler, 'displayBackOptionMessage');
try {
await handler.createNewStack({ orgUid: 'test-org' });
expect.fail('Should have rejected');
} catch (error: any) {
expect(error).to.equal('Access denied Contact the Organization owner for Stack Creation access.');
}
expect(displayBackOptionMessageStub.calledOnce).to.be.true;
});
it('should handle error in createNewStack catch block (covers line 773)', async () => {
(handler as any).config.stackName = 'test-stack-name';
(handler as any).executingCommand = 1;
const testError = new Error('Test error');
const displayBackOptionMessageStub = sandbox.stub(handler, 'displayBackOptionMessage').throws(testError);
try {
await handler.createNewStack({ orgUid: 'test-org' });
expect.fail('Should have rejected');
} catch (error) {
expect(error).to.equal(testError);
}
});
it('should prompt for stack name when not provided (covers lines 736-742, 708-727)', async () => {
(handler as any).config.stackName = undefined;
(handler as any).executingCommand = 1;
(handler as any).master_locale = 'en-us';
(handler as any).stackNamePrompt = { message: 'Enter stack name:', default: 'DefaultStack' };
const promptModule = require('prompt');
promptModule.stopped = false;
promptModule.get = sandbox.stub().callsArgWith(1, null, { name: 'prompted-stack-name' });
sandbox.stub(promptModule, 'start');
const setCreateNewStackPromptStub = sandbox.stub(handler, 'setCreateNewStackPrompt');
const displayBackOptionMessageStub = sandbox.stub(handler, 'displayBackOptionMessage');
const createPromise = Promise.resolve({ api_key: 'new-key', name: 'prompted-stack-name' });
((handler as any).client.stack().create as sinon.SinonStub).returns(createPromise);
const result = await handler.createNewStack({ orgUid: 'test-org' });
expect(result).to.have.property('api_key', 'new-key');
expect(setCreateNewStackPromptStub.calledTwice).to.be.true;
expect(displayBackOptionMessageStub.calledOnce).to.be.true;
});
});
});