-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbase-command.test.ts
More file actions
706 lines (561 loc) · 23.7 KB
/
base-command.test.ts
File metadata and controls
706 lines (561 loc) · 23.7 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
/*
* 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 sinon from 'sinon';
import {Config} from '@oclif/core';
import {BaseCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {globalMiddlewareRegistry} from '@salesforce/b2c-tooling-sdk/clients';
import {Telemetry} from '@salesforce/b2c-tooling-sdk/telemetry';
import {isolateConfig, restoreConfig} from '@salesforce/b2c-tooling-sdk/test-utils';
import {stubParse} from '../helpers/stub-parse.js';
// Create a concrete test command class
class TestBaseCommand extends BaseCommand<typeof TestBaseCommand> {
static id = 'test:base';
static description = 'Test base command';
async run(): Promise<void> {
// Test implementation
}
// Expose protected methods for testing
public testGetExtraParams() {
return this.getExtraParams();
}
public testCatch(err: Error & {exitCode?: number}) {
return this.catch(err);
}
public testFinally(err: Error | undefined) {
return this.finally(err);
}
public getTelemetry() {
return this.telemetry;
}
public getResolvedConfig() {
return this.resolvedConfig;
}
public setResolvedConfig(config: typeof this.resolvedConfig) {
this.resolvedConfig = config;
}
public testAddTelemetryContext() {
return this.addTelemetryContext();
}
}
describe('cli/base-command', () => {
let config: Config;
let command: TestBaseCommand;
beforeEach(async () => {
isolateConfig();
config = await Config.load();
command = new TestBaseCommand([], config);
});
afterEach(() => {
sinon.restore();
restoreConfig();
// Clean up the global middleware registry between tests
globalMiddlewareRegistry.clear();
});
describe('getExtraParams', () => {
it('returns undefined when no extra params', async () => {
stubParse(command);
await command.init();
const params = command.testGetExtraParams();
expect(params).to.be.undefined;
});
it('parses extra-query flag', async () => {
stubParse(command, {'extra-query': '{"debug":"true"}'});
await command.init();
const params = command.testGetExtraParams();
expect(params?.query).to.deep.equal({debug: 'true'});
});
it('parses extra-body flag', async () => {
stubParse(command, {'extra-body': '{"_internal":true}'});
await command.init();
const params = command.testGetExtraParams();
expect(params?.body).to.deep.equal({_internal: true});
});
it('parses both extra-query and extra-body', async () => {
stubParse(command, {
'extra-query': '{"debug":"true"}',
'extra-body': '{"_internal":true}',
});
await command.init();
const params = command.testGetExtraParams();
expect(params?.query).to.deep.equal({debug: 'true'});
expect(params?.body).to.deep.equal({_internal: true});
});
it('throws error for invalid JSON in extra-query', async () => {
stubParse(command, {'extra-query': 'invalid-json'});
// Stub error before init() because registerExtraParamsMiddleware calls getExtraParams()
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
try {
await command.init();
} catch {
// Expected
}
expect(errorStub.called).to.be.true;
});
it('throws error for invalid JSON in extra-body', async () => {
stubParse(command, {'extra-body': 'invalid-json'});
// Stub error before init() because registerExtraParamsMiddleware calls getExtraParams()
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
try {
await command.init();
} catch {
// Expected
}
expect(errorStub.called).to.be.true;
});
it('parses extra-headers flag', async () => {
stubParse(command, {'extra-headers': '{"X-Custom-Header":"value"}'});
await command.init();
const params = command.testGetExtraParams();
expect(params?.headers).to.deep.equal({'X-Custom-Header': 'value'});
});
it('parses extra-query, extra-body, and extra-headers together', async () => {
stubParse(command, {
'extra-query': '{"debug":"true"}',
'extra-body': '{"_internal":true}',
'extra-headers': '{"X-Custom":"value"}',
});
await command.init();
const params = command.testGetExtraParams();
expect(params?.query).to.deep.equal({debug: 'true'});
expect(params?.body).to.deep.equal({_internal: true});
expect(params?.headers).to.deep.equal({'X-Custom': 'value'});
});
it('throws error for invalid JSON in extra-headers', async () => {
stubParse(command, {'extra-headers': 'invalid-json'});
// Stub error before init() because registerExtraParamsMiddleware calls getExtraParams()
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
try {
await command.init();
} catch {
// Expected
}
expect(errorStub.called).to.be.true;
});
});
describe('catch', () => {
it('handles errors with exit code', async () => {
stubParse(command, {json: false});
await command.init();
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
const error = new Error('Test error') as Error & {exitCode?: number};
error.exitCode = 2;
try {
await command.testCatch(error);
} catch {
// Expected
}
expect(errorStub.called).to.be.true;
});
it('outputs JSON error in JSON mode', async () => {
stubParse(command, {json: true});
await command.init();
// Mock jsonEnabled to return true
sinon.stub(command, 'jsonEnabled').returns(true);
let writtenData = '';
sinon.stub(process.stderr, 'write').callsFake((chunk: string | Uint8Array) => {
writtenData += chunk.toString();
return true;
});
const exitStub = sinon.stub(process, 'exit').throws(new Error('Exit called'));
const error = new Error('Test error');
try {
await command.testCatch(error);
} catch {
// Expected
}
// In JSON mode, error should be written to stderr as JSON
expect(writtenData).to.include('error');
expect(writtenData).to.include('Test error');
expect(exitStub.calledWith(1)).to.be.true;
// Cleanup handled by sinon.restore() in afterEach
});
});
describe('telemetry', () => {
let telemetryStartStub: sinon.SinonStub;
let telemetryStopStub: sinon.SinonStub;
let telemetryFlushStub: sinon.SinonStub;
let telemetrySendEventStub: sinon.SinonStub;
let telemetrySendExceptionStub: sinon.SinonStub;
beforeEach(() => {
// Stub Telemetry prototype methods
telemetryStartStub = sinon.stub(Telemetry.prototype, 'start').resolves();
telemetryStopStub = sinon.stub(Telemetry.prototype, 'stop').resolves();
telemetryFlushStub = sinon.stub(Telemetry.prototype, 'flush').resolves();
telemetrySendEventStub = sinon.stub(Telemetry.prototype, 'sendEvent');
telemetrySendExceptionStub = sinon.stub(Telemetry.prototype, 'sendException');
});
describe('Telemetry.isDisabled()', () => {
let originalSfDisable: string | undefined;
let originalSfccDisable: string | undefined;
beforeEach(() => {
originalSfDisable = process.env.SF_DISABLE_TELEMETRY;
originalSfccDisable = process.env.SFCC_DISABLE_TELEMETRY;
delete process.env.SF_DISABLE_TELEMETRY;
delete process.env.SFCC_DISABLE_TELEMETRY;
});
afterEach(() => {
if (originalSfDisable !== undefined) {
process.env.SF_DISABLE_TELEMETRY = originalSfDisable;
} else {
delete process.env.SF_DISABLE_TELEMETRY;
}
if (originalSfccDisable !== undefined) {
process.env.SFCC_DISABLE_TELEMETRY = originalSfccDisable;
} else {
delete process.env.SFCC_DISABLE_TELEMETRY;
}
});
it('returns false when no disable env vars are set', () => {
expect(Telemetry.isDisabled()).to.be.false;
});
it('returns true when SF_DISABLE_TELEMETRY=true', () => {
process.env.SF_DISABLE_TELEMETRY = 'true';
expect(Telemetry.isDisabled()).to.be.true;
});
it('returns true when SFCC_DISABLE_TELEMETRY=true', () => {
process.env.SFCC_DISABLE_TELEMETRY = 'true';
expect(Telemetry.isDisabled()).to.be.true;
});
it('returns false when SF_DISABLE_TELEMETRY=false', () => {
process.env.SF_DISABLE_TELEMETRY = 'false';
expect(Telemetry.isDisabled()).to.be.false;
});
});
describe('Telemetry.getConnectionString()', () => {
let originalSfDisable: string | undefined;
let originalAppInsightsKey: string | undefined;
beforeEach(() => {
originalSfDisable = process.env.SF_DISABLE_TELEMETRY;
originalAppInsightsKey = process.env.SFCC_APP_INSIGHTS_KEY;
delete process.env.SF_DISABLE_TELEMETRY;
delete process.env.SFCC_APP_INSIGHTS_KEY;
});
afterEach(() => {
if (originalSfDisable !== undefined) {
process.env.SF_DISABLE_TELEMETRY = originalSfDisable;
} else {
delete process.env.SF_DISABLE_TELEMETRY;
}
if (originalAppInsightsKey !== undefined) {
process.env.SFCC_APP_INSIGHTS_KEY = originalAppInsightsKey;
} else {
delete process.env.SFCC_APP_INSIGHTS_KEY;
}
});
it('returns undefined when telemetry is disabled', () => {
process.env.SF_DISABLE_TELEMETRY = 'true';
expect(Telemetry.getConnectionString('default-key')).to.be.undefined;
});
it('returns project default when no env override', () => {
expect(Telemetry.getConnectionString('default-key')).to.equal('default-key');
});
it('returns env override when SFCC_APP_INSIGHTS_KEY is set', () => {
process.env.SFCC_APP_INSIGHTS_KEY = 'env-override';
expect(Telemetry.getConnectionString('default-key')).to.equal('env-override');
});
it('returns undefined when no project default and no env override', () => {
expect(Telemetry.getConnectionString()).to.be.undefined;
});
});
describe('auto-initialization from pjson', () => {
let originalSfDisable: string | undefined;
let originalSfccDisable: string | undefined;
beforeEach(() => {
originalSfDisable = process.env.SF_DISABLE_TELEMETRY;
originalSfccDisable = process.env.SFCC_DISABLE_TELEMETRY;
delete process.env.SF_DISABLE_TELEMETRY;
delete process.env.SFCC_DISABLE_TELEMETRY;
});
afterEach(() => {
if (originalSfDisable !== undefined) {
process.env.SF_DISABLE_TELEMETRY = originalSfDisable;
} else {
delete process.env.SF_DISABLE_TELEMETRY;
}
if (originalSfccDisable !== undefined) {
process.env.SFCC_DISABLE_TELEMETRY = originalSfccDisable;
} else {
delete process.env.SFCC_DISABLE_TELEMETRY;
}
});
it('initializes telemetry when pjson has connectionString', async () => {
// Mock the pjson to include telemetry config
sinon.stub(config, 'pjson').value({
...config.pjson,
oclif: {
...(config.pjson.oclif || {}),
telemetry: {connectionString: 'test-key'},
},
});
const cmd = new TestBaseCommand([], config);
stubParse(cmd);
await cmd.init();
expect(telemetryStartStub.called).to.be.true;
expect(telemetrySendEventStub.calledWith('COMMAND_START')).to.be.true;
expect(cmd.getTelemetry()).to.not.be.undefined;
});
it('does not initialize telemetry when pjson has no connectionString', async () => {
// Use default config without telemetry
stubParse(command);
await command.init();
// With no connectionString in pjson, telemetry should not be initialized
// (unless there's an env override, which we've cleared)
expect(command.getTelemetry()).to.be.undefined;
});
it('does not initialize telemetry when SF_DISABLE_TELEMETRY=true', async () => {
process.env.SF_DISABLE_TELEMETRY = 'true';
// Mock the pjson to include telemetry config
sinon.stub(config, 'pjson').value({
...config.pjson,
oclif: {
...(config.pjson.oclif || {}),
telemetry: {connectionString: 'test-key'},
},
});
const cmd = new TestBaseCommand([], config);
stubParse(cmd);
await cmd.init();
expect(cmd.getTelemetry()).to.be.undefined;
});
});
describe('catch() exception tracking', () => {
it('sends exception and COMMAND_ERROR to telemetry when telemetry is initialized', async () => {
// Create a telemetry instance and attach it to the command
const telemetry = new Telemetry({project: 'test', appInsightsKey: 'test-key'});
(command as unknown as {telemetry: Telemetry}).telemetry = telemetry;
stubParse(command, {json: false});
await command.init();
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
const error = new Error('Test error') as Error & {exitCode?: number};
error.exitCode = 2;
try {
await command.testCatch(error);
} catch {
// Expected
}
expect(telemetrySendExceptionStub.called).to.be.true;
expect(telemetrySendEventStub.calledWith('COMMAND_ERROR')).to.be.true;
expect(telemetryFlushStub.called).to.be.true;
expect(telemetryStopStub.called).to.be.true;
expect(errorStub.called).to.be.true;
});
it('includes exitCode and command in exception and COMMAND_ERROR attributes', async () => {
const telemetry = new Telemetry({project: 'test', appInsightsKey: 'test-key'});
(command as unknown as {telemetry: Telemetry}).telemetry = telemetry;
stubParse(command, {json: false});
await command.init();
sinon.stub(command, 'error').throws(new Error('Expected error'));
const error = new Error('Test error') as Error & {exitCode?: number};
error.exitCode = 42;
try {
await command.testCatch(error);
} catch {
// Expected
}
expect(telemetrySendExceptionStub.called).to.be.true;
const [sentError, attributes] = telemetrySendExceptionStub.firstCall.args;
expect(sentError).to.equal(error);
expect(attributes.exitCode).to.equal(42);
expect(attributes.command).to.equal('test:base');
const commandErrorCall = telemetrySendEventStub.getCalls().find((c) => c.args[0] === 'COMMAND_ERROR');
expect(commandErrorCall).to.not.be.undefined;
expect(commandErrorCall!.args[1]).to.include({
command: 'test:base',
exitCode: 42,
errorMessage: 'Test error',
});
});
});
describe('addTelemetryContext', () => {
let telemetryAddAttributesStub: sinon.SinonStub;
beforeEach(() => {
telemetryAddAttributesStub = sinon.stub(Telemetry.prototype, 'addAttributes');
});
function setupTelemetry(cmd: TestBaseCommand): void {
const telemetry = new Telemetry({project: 'test', appInsightsKey: 'test-key'});
(cmd as unknown as {telemetry: Telemetry}).telemetry = telemetry;
}
function setResolvedConfig(
cmd: TestBaseCommand,
values: Record<string, string | undefined>,
sources: {name: string; fields: string[]}[] = [],
): void {
cmd.setResolvedConfig({
values: values as unknown as ReturnType<TestBaseCommand['getResolvedConfig']>['values'],
sources: sources as unknown as ReturnType<TestBaseCommand['getResolvedConfig']>['sources'],
warnings: [],
hasB2CInstanceConfig: () => false,
hasMrtConfig: () => false,
hasOAuthConfig: () => false,
hasBasicAuthConfig: () => false,
createB2CInstance: () => {
throw new Error('not implemented');
},
createBasicAuth: () => {
throw new Error('not implemented');
},
createOAuth: () => {
throw new Error('not implemented');
},
createMrtAuth: () => {
throw new Error('not implemented');
},
createWebDavAuth: () => {
throw new Error('not implemented');
},
});
}
it('adds realm and tenantId from tenantId config', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {tenantId: 'zzpq_019'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.realm).to.equal('zzpq');
expect(attrs.tenantId).to.equal('zzpq_019');
});
it('extracts realm from hostname when no tenantId', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {hostname: 'zzpq-019.dx.commercecloud.salesforce.com'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.realm).to.equal('zzpq');
expect(attrs.hostname).to.equal('zzpq-019.dx.commercecloud.salesforce.com');
expect(attrs.tenantId).to.be.undefined;
});
it('adds shortCode when available', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {tenantId: 'zzpq_019', shortCode: 'kv7kzm78'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.shortCode).to.equal('kv7kzm78');
});
it('adds hostname when available', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {tenantId: 'zzpq_019', hostname: 'zzpq-019.dx.commercecloud.salesforce.com'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.hostname).to.equal('zzpq-019.dx.commercecloud.salesforce.com');
});
it('adds clientId when available', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {tenantId: 'zzpq_019', clientId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.clientId).to.equal('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
});
it('adds configSources from resolved sources', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {tenantId: 'zzpq_019'}, [
{name: 'flags', fields: ['tenantId']},
{name: 'dw.json', fields: ['hostname']},
]);
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.configSources).to.equal('flags, dw.json');
});
it('does not call addAttributes when no context available', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.called).to.be.false;
});
it('handles f_ecom_ prefixed tenantId', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {tenantId: 'f_ecom_zzpq_019'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.realm).to.equal('zzpq');
expect(attrs.tenantId).to.equal('f_ecom_zzpq_019');
});
it('handles unparseable tenantId (sets tenantId but not realm)', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
setResolvedConfig(command, {tenantId: 'some-custom-id-format'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.calledOnce).to.be.true;
const attrs = telemetryAddAttributesStub.firstCall.args[0];
expect(attrs.tenantId).to.equal('some-custom-id-format');
expect(attrs.realm).to.be.undefined;
});
it('does not throw when resolvedConfig has unexpected shape', async () => {
stubParse(command);
await command.init();
setupTelemetry(command);
// Force a broken resolvedConfig to simulate unexpected runtime state
command.setResolvedConfig(null as unknown as ReturnType<typeof command.getResolvedConfig>);
expect(() => command.testAddTelemetryContext()).to.not.throw();
});
it('does nothing when telemetry is not initialized', async () => {
stubParse(command);
await command.init();
setResolvedConfig(command, {tenantId: 'zzpq_019'});
command.testAddTelemetryContext();
expect(telemetryAddAttributesStub.called).to.be.false;
});
});
describe('finally() success tracking', () => {
it('sends COMMAND_SUCCESS when no error occurred', async () => {
const telemetry = new Telemetry({project: 'test', appInsightsKey: 'test-key'});
(command as unknown as {telemetry: Telemetry}).telemetry = telemetry;
stubParse(command);
await command.init();
await command.testFinally(undefined);
expect(telemetrySendEventStub.calledWith('COMMAND_SUCCESS')).to.be.true;
expect(telemetryStopStub.called).to.be.true;
});
it('does not send COMMAND_SUCCESS when error occurred', async () => {
const telemetry = new Telemetry({project: 'test', appInsightsKey: 'test-key'});
(command as unknown as {telemetry: Telemetry}).telemetry = telemetry;
stubParse(command);
await command.init();
// Reset the stubs to clear any calls from init
telemetrySendEventStub.resetHistory();
await command.testFinally(new Error('Some error'));
expect(telemetrySendEventStub.calledWith('COMMAND_SUCCESS')).to.be.false;
});
it('includes duration in COMMAND_SUCCESS attributes', async () => {
const telemetry = new Telemetry({project: 'test', appInsightsKey: 'test-key'});
(command as unknown as {telemetry: Telemetry}).telemetry = telemetry;
// Simulate that command started 100ms ago
(command as unknown as {commandStartTime: number}).commandStartTime = Date.now() - 100;
stubParse(command);
await command.init();
// Reset after init to check only finally's sendEvent call
telemetrySendEventStub.resetHistory();
await command.testFinally(undefined);
expect(telemetrySendEventStub.calledWith('COMMAND_SUCCESS')).to.be.true;
const [, attributes] = telemetrySendEventStub.firstCall.args;
expect(attributes.duration).to.be.a('number');
expect(attributes.duration).to.be.at.least(100);
});
});
});
});