-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmrt-lifecycle.test.ts
More file actions
775 lines (644 loc) · 27.2 KB
/
mrt-lifecycle.test.ts
File metadata and controls
775 lines (644 loc) · 27.2 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {expect} from 'chai';
import {parseJSONOutput, runCLI, runCLIWithRetry, TIMEOUTS} from './test-utils.js';
/**
* E2E Tests for MRT (Managed Runtime) Lifecycle
*
* Tests MRT operations including:
* 1. User profile and API key management
* 2. Organization listing
* 3. Project operations (list, get)
* 4. Environment operations (list, get)
* 5. Environment variables (list, set, delete)
* 6. Bundle operations (list, history)
* 7. Project member operations (list)
*
* Note: These tests are READ-HEAVY to avoid creating/deleting real MRT resources in CI.
* Write operations (create project, deploy bundle) are tested with mock data where possible.
*/
describe('MRT Lifecycle E2E Tests', function () {
this.timeout(TIMEOUTS.DEFAULT * 10); // 5 minutes for MRT operations
this.retries(2); // Retry failed tests for network resilience
// Pass MRT cloud origin to all CLI commands
// Build env object without undefined values to satisfy Record<string, string>
const MRT_TEST_ENV: Record<string, string> = {
...(process.env.SFCC_MRT_CLOUD_ORIGIN ? {SFCC_MRT_CLOUD_ORIGIN: process.env.SFCC_MRT_CLOUD_ORIGIN} : {}),
};
let projectSlug: string;
let hasProject = false;
before(async function () {
// Check required environment variables for MRT
// Either SFCC_MRT_API_KEY as env var OR ~/.mobify file must exist
const hasMrtApiKey = Boolean(process.env.SFCC_MRT_API_KEY);
if (!hasMrtApiKey) {
// Try to check if ~/.mobify exists (CLI will auto-detect it)
try {
const testResult = await runCLI(['mrt', 'user', 'profile', '--json'], {
timeout: 10_000,
env: MRT_TEST_ENV,
});
if (testResult.exitCode !== 0) {
const errorText = String(testResult.stderr || testResult.stdout || '');
if (errorText.includes('MRT API key required') || errorText.includes('api_key')) {
console.log('⚠ SFCC_MRT_API_KEY not set and ~/.mobify not configured, skipping MRT E2E tests');
this.skip();
}
}
} catch {
console.log('⚠ MRT authentication not configured, skipping MRT E2E tests');
this.skip();
}
}
// Try to get a project from environment or discover one
if (process.env.SFCC_MRT_PROJECT) {
projectSlug = process.env.SFCC_MRT_PROJECT;
hasProject = true;
console.log(`✓ Using MRT project from env: ${projectSlug}`);
} else {
// Try to discover a project
try {
const result = await runCLI(['mrt', 'project', 'list', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
if (result.exitCode === 0) {
const response = parseJSONOutput(result);
if (response.projects && response.projects.length > 0) {
projectSlug = response.projects[0].slug;
hasProject = true;
console.log(`✓ Discovered MRT project: ${projectSlug}`);
} else {
console.log('⚠ No MRT projects found, some tests will be skipped');
}
}
} catch {
console.log('⚠ Could not discover MRT project, some tests will be skipped');
}
}
});
describe('Step 1: User Profile', () => {
it('should retrieve user profile', async function () {
const result = await runCLIWithRetry(['mrt', 'user', 'profile', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Profile command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response is a flat object, not nested under "user"
expect(response).to.have.property('uuid');
expect(response).to.have.property('email');
expect(response).to.have.property('first_name');
});
it('should retrieve email preferences', async function () {
const result = await runCLIWithRetry(['mrt', 'user', 'email-prefs', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Email prefs command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response is a flat object with preference fields
expect(response).to.have.property('node_deprecation_notifications');
expect(response).to.have.property('created_at');
expect(response).to.have.property('updated_at');
});
});
describe('Step 2: Organizations', () => {
it('should list organizations', async function () {
const result = await runCLIWithRetry(['mrt', 'org', 'list', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Org list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('organizations');
expect(response.organizations).to.be.an('array');
});
it('should get B2C connection for organization', async function () {
// First get an org slug
const listResult = await runCLI(['mrt', 'org', 'list', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(listResult.exitCode).to.equal(0);
const listResponse = parseJSONOutput(listResult);
if (!listResponse.organizations || listResponse.organizations.length === 0) {
this.skip();
}
const orgSlug = listResponse.organizations[0].slug;
const result = await runCLIWithRetry(['mrt', 'org', 'b2c', orgSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
// Skip if 404 (some orgs may not have B2C connection configured)
if (result.exitCode !== 0) {
const errorText = String(result.stderr || result.stdout || '');
if (errorText.includes('404') || errorText.includes('Not found')) {
console.log(' ⚠ B2C info not available for this org, skipping');
this.skip();
}
}
expect(result.exitCode, `Org B2C command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('is_b2c_customer');
});
});
describe('Step 3: Projects', () => {
it('should list projects', async function () {
const result = await runCLIWithRetry(['mrt', 'project', 'list', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Project list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('projects');
expect(response.projects).to.be.an('array');
});
it('should get specific project', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
// Fixed: slug is a positional argument, not a flag
const result = await runCLIWithRetry(['mrt', 'project', 'get', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Project get command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response is a flat object, not nested under "project"
expect(response).to.have.property('slug').that.equals(projectSlug);
expect(response).to.have.property('name');
});
});
describe('Step 4: Project Members', () => {
it('should list project members', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
const result = await runCLIWithRetry(['mrt', 'project', 'member', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Member list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('members');
expect(response.members).to.be.an('array');
if (response.members.length > 0) {
expect(response.members[0]).to.have.property('email');
expect(response.members[0]).to.have.property('role');
}
});
it('should get specific project member', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
// First get a member email
const listResult = await runCLI(['mrt', 'project', 'member', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(listResult.exitCode).to.equal(0);
const listResponse = parseJSONOutput(listResult);
if (!listResponse.members || listResponse.members.length === 0) {
console.log(' ⚠ No members found, skipping test');
this.skip();
}
const memberEmail = listResponse.members[0].email;
const result = await runCLIWithRetry(
['mrt', 'project', 'member', 'get', memberEmail, '--project', projectSlug, '--json'],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Member get command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response is a flat object, not nested under "member"
expect(response).to.have.property('email').that.equals(memberEmail);
expect(response).to.have.property('role');
});
});
describe('Step 5: Environments', () => {
let environmentName: string;
it('should list environments for project', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
const result = await runCLIWithRetry(['mrt', 'env', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Env list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('environments');
expect(response.environments).to.be.an('array');
if (response.environments.length > 0) {
environmentName = response.environments[0].slug; // Use slug for commands
expect(response.environments[0]).to.have.property('slug');
expect(response.environments[0]).to.have.property('name');
}
});
it('should get specific environment', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
// First ensure we have an environment
const listResult = await runCLI(['mrt', 'env', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(listResult.exitCode).to.equal(0);
const listResponse = parseJSONOutput(listResult);
if (!listResponse.environments || listResponse.environments.length === 0) {
console.log(' ⚠ No environments found, skipping test');
this.skip();
}
environmentName = listResponse.environments[0].slug; // Use slug, not name
const result = await runCLIWithRetry(
['mrt', 'env', 'get', '--project', projectSlug, '--environment', environmentName, '--json'],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Env get command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response is a flat object, not nested under "environment"
expect(response).to.have.property('slug').that.equals(environmentName);
expect(response).to.have.property('name');
});
it('should get B2C connection for environment', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
// Get first environment
const listResult = await runCLI(['mrt', 'env', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(listResult.exitCode).to.equal(0);
const listResponse = parseJSONOutput(listResult);
if (!listResponse.environments || listResponse.environments.length === 0) {
console.log(' ⚠ No environments found, skipping test');
this.skip();
}
environmentName = listResponse.environments[0].slug;
const result = await runCLIWithRetry(
['mrt', 'env', 'b2c', '--project', projectSlug, '--environment', environmentName, '--json'],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
// Skip if 404 (some environments may not have B2C connection configured)
if (result.exitCode !== 0) {
const errorText = String(result.stderr || result.stdout || '');
if (
errorText.includes('404') ||
errorText.includes('Not found') ||
errorText.includes('No B2CTargetInfo matches the given query') ||
errorText.includes('Failed to get B2C target info')
) {
console.log(' ⚠ B2C target info not available for this environment, skipping');
this.skip();
}
}
expect(result.exitCode, `Env B2C command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('b2cTargetInfo');
});
});
describe('Step 6: Environment Variables', () => {
const testVarKey = `E2E_TEST_VAR_${Date.now()}`;
const testVarValue = 'e2e-test-value';
let environmentName: string;
let varCreated = false;
before(async function () {
if (!hasProject) {
this.skip();
}
// Get first environment for testing
const listResult = await runCLI(['mrt', 'env', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
if (listResult.exitCode === 0) {
const listResponse = parseJSONOutput(listResult);
if (listResponse.environments && listResponse.environments.length > 0) {
environmentName = listResponse.environments[0].slug; // Use slug for commands
} else {
this.skip();
}
} else {
this.skip();
}
});
after(async function () {
// Cleanup: delete test variable if created
if (varCreated && environmentName) {
try {
await runCLI(
['mrt', 'env', 'var', 'delete', testVarKey, '--project', projectSlug, '--environment', environmentName],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
console.log(` 🧹 Cleaned up test environment variable: ${testVarKey}`);
} catch {
// Ignore cleanup errors
}
}
});
it('should list environment variables', async function () {
const result = await runCLIWithRetry(
['mrt', 'env', 'var', 'list', '--project', projectSlug, '--environment', environmentName, '--json'],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Env var list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('variables');
expect(response.variables).to.be.an('array');
});
it('should set environment variable', async function () {
const result = await runCLIWithRetry(
[
'mrt',
'env',
'var',
'set',
`${testVarKey}=${testVarValue}`,
'--project',
projectSlug,
'--environment',
environmentName,
'--json',
],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Env var set command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response has "variables" object (plural), not "variable"
expect(response).to.have.property('variables');
expect(response.variables).to.have.property(testVarKey).that.equals(testVarValue);
expect(response).to.have.property('project').that.equals(projectSlug);
expect(response).to.have.property('environment').that.equals(environmentName);
varCreated = true;
});
it('should delete environment variable', async function () {
if (!varCreated) {
this.skip();
}
const result = await runCLIWithRetry(
[
'mrt',
'env',
'var',
'delete',
testVarKey,
'--project',
projectSlug,
'--environment',
environmentName,
'--json',
],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
// In some environments, the delete operation can be retried by Mocha.
// If a previous attempt already deleted the variable, the backend will
// return an error indicating that the variable does not exist. Treat
// this specific case as acceptable instead of failing the test.
if (result.exitCode !== 0) {
const errorText = String(result.stderr || result.stdout || '');
if (errorText.includes('does not exist')) {
console.log(
` ⚠ Environment variable ${testVarKey} was already deleted (does not exist), treating as success`,
);
varCreated = false;
return;
}
}
expect(result.exitCode, `Env var delete command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response has {key, project, environment}, not {success: true}
expect(response).to.have.property('key').that.equals(testVarKey);
expect(response).to.have.property('project').that.equals(projectSlug);
expect(response).to.have.property('environment').that.equals(environmentName);
varCreated = false; // Mark as cleaned up
});
it('should verify environment variable was deleted', async function () {
const result = await runCLIWithRetry(
['mrt', 'env', 'var', 'list', '--project', projectSlug, '--environment', environmentName, '--json'],
{timeout: TIMEOUTS.DEFAULT, verbose: true, env: MRT_TEST_ENV},
);
expect(result.exitCode).to.equal(0);
const response = parseJSONOutput(result);
const foundVar = response.variables.find((v: {key: string}) => v.key === testVarKey);
expect(foundVar, `Test variable ${testVarKey} should be deleted`).to.not.exist;
});
});
describe('Step 7: Environment Redirects', () => {
let environmentName: string;
before(async function () {
if (!hasProject) {
this.skip();
}
// Get first environment
const listResult = await runCLI(['mrt', 'env', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
if (listResult.exitCode === 0) {
const listResponse = parseJSONOutput(listResult);
if (listResponse.environments && listResponse.environments.length > 0) {
environmentName = listResponse.environments[0].slug; // Use slug
} else {
this.skip();
}
} else {
this.skip();
}
});
it('should list environment redirects', async function () {
const result = await runCLIWithRetry(
['mrt', 'env', 'redirect', 'list', '--project', projectSlug, '--environment', environmentName, '--json'],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Redirect list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('redirects');
expect(response.redirects).to.be.an('array');
});
});
describe('Step 8: Environment Access Control', () => {
let environmentName: string;
before(async function () {
if (!hasProject) {
this.skip();
}
// Get first environment
const listResult = await runCLI(['mrt', 'env', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
if (listResult.exitCode === 0) {
const listResponse = parseJSONOutput(listResult);
if (listResponse.environments && listResponse.environments.length > 0) {
environmentName = listResponse.environments[0].slug; // Use slug
} else {
this.skip();
}
} else {
this.skip();
}
});
it('should list access control settings', async function () {
const result = await runCLIWithRetry(
['mrt', 'env', 'access-control', 'list', '--project', projectSlug, '--environment', environmentName, '--json'],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Access control list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
// Response is a paginated list with headers array
expect(response).to.have.property('headers');
expect(response.headers).to.be.an('array');
});
});
describe('Step 9: Bundles', () => {
it('should list bundles for project', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
const result = await runCLIWithRetry(['mrt', 'bundle', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, `Bundle list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('bundles');
expect(response.bundles).to.be.an('array');
if (response.bundles.length > 0) {
expect(response.bundles[0]).to.have.property('id');
expect(response.bundles[0]).to.have.property('message');
}
});
it('should view deployment history for environment', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
// Get first environment
const envListResult = await runCLI(['mrt', 'env', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(envListResult.exitCode).to.equal(0);
const envListResponse = parseJSONOutput(envListResult);
if (!envListResponse.environments || envListResponse.environments.length === 0) {
console.log(' ⚠ No environments found, skipping test');
this.skip();
}
const environmentName = envListResponse.environments[0].slug; // Use slug
const result = await runCLIWithRetry(
['mrt', 'bundle', 'history', '--project', projectSlug, '--environment', environmentName, '--json'],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Bundle history command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('deployments');
expect(response.deployments).to.be.an('array');
});
});
describe('Step 10: Project Notifications', () => {
it('should list project notifications', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
const result = await runCLIWithRetry(
['mrt', 'project', 'notification', 'list', '--project', projectSlug, '--json'],
{
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
},
);
expect(result.exitCode, `Notification list command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('notifications');
expect(response.notifications).to.be.an('array');
});
it('should get specific notification if any exist', async function () {
if (!hasProject) {
console.log(' ⚠ No project available, skipping test');
this.skip();
}
// First get list of notifications
const listResult = await runCLI(['mrt', 'project', 'notification', 'list', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(listResult.exitCode).to.equal(0);
const listResponse = parseJSONOutput(listResult);
if (!listResponse.notifications || listResponse.notifications.length === 0) {
console.log(' ⚠ No notifications found, skipping test');
this.skip();
}
const notificationId = listResponse.notifications[0].id;
const result = await runCLIWithRetry(
[
'mrt',
'project',
'notification',
'get',
'--project',
projectSlug,
'--notification-id',
notificationId,
'--json',
],
{timeout: TIMEOUTS.DEFAULT, env: MRT_TEST_ENV},
);
expect(result.exitCode, `Notification get command failed: ${result.stderr}`).to.equal(0);
const response = parseJSONOutput(result);
expect(response).to.have.property('notification');
expect(response.notification).to.have.property('id').that.equals(notificationId);
});
});
describe('Step 11: Error Handling', () => {
it('should fail gracefully with invalid project', async function () {
// Fixed: slug is a positional argument, not a flag
const result = await runCLI(['mrt', 'project', 'get', 'nonexistent-project-12345', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, 'Command should fail for invalid project').to.not.equal(0);
const errorText = result.stderr || result.stdout;
expect(errorText).to.include('error');
});
it('should fail gracefully with invalid environment', async function () {
if (!hasProject) {
this.skip();
}
const result = await runCLI(['mrt', 'env', 'get', 'nonexistent-env-12345', '--project', projectSlug, '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: MRT_TEST_ENV,
});
expect(result.exitCode, 'Command should fail for invalid environment').to.not.equal(0);
const errorText = String(result.stderr || result.stdout || '');
// We only require that some diagnostic message is shown; it may be a warning
// from oclif rather than containing the literal word "error".
expect(errorText.trim().length > 0, 'Expected diagnostic output for invalid environment').to.be.true;
});
it('should require authentication', async function () {
// Run without API key to test auth failure
const result = await runCLI(['mrt', 'project', 'list', '--json'], {
timeout: TIMEOUTS.DEFAULT,
env: {
SFCC_MRT_API_KEY: '',
...(process.env.SFCC_MRT_CLOUD_ORIGIN ? {SFCC_MRT_CLOUD_ORIGIN: process.env.SFCC_MRT_CLOUD_ORIGIN} : {}),
}, // Override API key to empty while preserving cloud origin if set
});
expect(result.exitCode, 'Command should fail without API key').to.not.equal(0);
const errorText = result.stderr || result.stdout;
expect(errorText).to.match(/api.?key|authentication|unauthorized/i);
});
});
});