@@ -37,6 +37,18 @@ class TestBaseCommand extends BaseCommand<typeof TestBaseCommand> {
3737 public getTelemetry ( ) {
3838 return this . telemetry ;
3939 }
40+
41+ public getResolvedConfig ( ) {
42+ return this . resolvedConfig ;
43+ }
44+
45+ public setResolvedConfig ( config : typeof this . resolvedConfig ) {
46+ this . resolvedConfig = config ;
47+ }
48+
49+ public testAddTelemetryContext ( ) {
50+ return this . addTelemetryContext ( ) ;
51+ }
4052}
4153
4254describe ( 'cli/base-command' , ( ) => {
@@ -454,6 +466,156 @@ describe('cli/base-command', () => {
454466 } ) ;
455467 } ) ;
456468
469+ describe ( 'addTelemetryContext' , ( ) => {
470+ let telemetryAddAttributesStub : sinon . SinonStub ;
471+
472+ beforeEach ( ( ) => {
473+ telemetryAddAttributesStub = sinon . stub ( Telemetry . prototype , 'addAttributes' ) ;
474+ } ) ;
475+
476+ function setupTelemetry ( cmd : TestBaseCommand ) : void {
477+ const telemetry = new Telemetry ( { project : 'test' , appInsightsKey : 'test-key' } ) ;
478+ ( cmd as unknown as { telemetry : Telemetry } ) . telemetry = telemetry ;
479+ }
480+
481+ function setResolvedConfig (
482+ cmd : TestBaseCommand ,
483+ values : Record < string , string | undefined > ,
484+ sources : { name : string ; fields : string [ ] } [ ] = [ ] ,
485+ ) : void {
486+ cmd . setResolvedConfig ( {
487+ values : values as unknown as ReturnType < TestBaseCommand [ 'getResolvedConfig' ] > [ 'values' ] ,
488+ sources : sources as unknown as ReturnType < TestBaseCommand [ 'getResolvedConfig' ] > [ 'sources' ] ,
489+ warnings : [ ] ,
490+ hasB2CInstanceConfig : ( ) => false ,
491+ hasMrtConfig : ( ) => false ,
492+ hasOAuthConfig : ( ) => false ,
493+ hasBasicAuthConfig : ( ) => false ,
494+ createB2CInstance : ( ) => {
495+ throw new Error ( 'not implemented' ) ;
496+ } ,
497+ createBasicAuth : ( ) => {
498+ throw new Error ( 'not implemented' ) ;
499+ } ,
500+ createOAuth : ( ) => {
501+ throw new Error ( 'not implemented' ) ;
502+ } ,
503+ createMrtAuth : ( ) => {
504+ throw new Error ( 'not implemented' ) ;
505+ } ,
506+ createWebDavAuth : ( ) => {
507+ throw new Error ( 'not implemented' ) ;
508+ } ,
509+ } ) ;
510+ }
511+
512+ it ( 'adds realm and tenantId from tenantId config' , async ( ) => {
513+ stubParse ( command ) ;
514+ await command . init ( ) ;
515+ setupTelemetry ( command ) ;
516+ setResolvedConfig ( command , { tenantId : 'zzpq_019' } ) ;
517+
518+ command . testAddTelemetryContext ( ) ;
519+
520+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
521+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
522+ expect ( attrs . realm ) . to . equal ( 'zzpq' ) ;
523+ expect ( attrs . tenantId ) . to . equal ( 'zzpq_019' ) ;
524+ } ) ;
525+
526+ it ( 'extracts realm from hostname when no tenantId' , async ( ) => {
527+ stubParse ( command ) ;
528+ await command . init ( ) ;
529+ setupTelemetry ( command ) ;
530+ setResolvedConfig ( command , { hostname : 'zzpq-019.dx.commercecloud.salesforce.com' } ) ;
531+
532+ command . testAddTelemetryContext ( ) ;
533+
534+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
535+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
536+ expect ( attrs . realm ) . to . equal ( 'zzpq' ) ;
537+ expect ( attrs . tenantId ) . to . be . undefined ;
538+ } ) ;
539+
540+ it ( 'adds shortCode when available' , async ( ) => {
541+ stubParse ( command ) ;
542+ await command . init ( ) ;
543+ setupTelemetry ( command ) ;
544+ setResolvedConfig ( command , { tenantId : 'zzpq_019' , shortCode : 'kv7kzm78' } ) ;
545+
546+ command . testAddTelemetryContext ( ) ;
547+
548+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
549+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
550+ expect ( attrs . shortCode ) . to . equal ( 'kv7kzm78' ) ;
551+ } ) ;
552+
553+ it ( 'adds configSources from resolved sources' , async ( ) => {
554+ stubParse ( command ) ;
555+ await command . init ( ) ;
556+ setupTelemetry ( command ) ;
557+ setResolvedConfig ( command , { tenantId : 'zzpq_019' } , [
558+ { name : 'flags' , fields : [ 'tenantId' ] } ,
559+ { name : 'dw.json' , fields : [ 'hostname' ] } ,
560+ ] ) ;
561+
562+ command . testAddTelemetryContext ( ) ;
563+
564+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
565+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
566+ expect ( attrs . configSources ) . to . equal ( 'flags, dw.json' ) ;
567+ } ) ;
568+
569+ it ( 'does not call addAttributes when no context available' , async ( ) => {
570+ stubParse ( command ) ;
571+ await command . init ( ) ;
572+ setupTelemetry ( command ) ;
573+ setResolvedConfig ( command , { } ) ;
574+
575+ command . testAddTelemetryContext ( ) ;
576+
577+ expect ( telemetryAddAttributesStub . called ) . to . be . false ;
578+ } ) ;
579+
580+ it ( 'handles f_ecom_ prefixed tenantId' , async ( ) => {
581+ stubParse ( command ) ;
582+ await command . init ( ) ;
583+ setupTelemetry ( command ) ;
584+ setResolvedConfig ( command , { tenantId : 'f_ecom_zzpq_019' } ) ;
585+
586+ command . testAddTelemetryContext ( ) ;
587+
588+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
589+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
590+ expect ( attrs . realm ) . to . equal ( 'zzpq' ) ;
591+ expect ( attrs . tenantId ) . to . equal ( 'f_ecom_zzpq_019' ) ;
592+ } ) ;
593+
594+ it ( 'handles unparseable tenantId (sets tenantId but not realm)' , async ( ) => {
595+ stubParse ( command ) ;
596+ await command . init ( ) ;
597+ setupTelemetry ( command ) ;
598+ setResolvedConfig ( command , { tenantId : 'some-custom-id-format' } ) ;
599+
600+ command . testAddTelemetryContext ( ) ;
601+
602+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
603+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
604+ expect ( attrs . tenantId ) . to . equal ( 'some-custom-id-format' ) ;
605+ expect ( attrs . realm ) . to . be . undefined ;
606+ } ) ;
607+
608+ it ( 'does nothing when telemetry is not initialized' , async ( ) => {
609+ stubParse ( command ) ;
610+ await command . init ( ) ;
611+ setResolvedConfig ( command , { tenantId : 'zzpq_019' } ) ;
612+
613+ command . testAddTelemetryContext ( ) ;
614+
615+ expect ( telemetryAddAttributesStub . called ) . to . be . false ;
616+ } ) ;
617+ } ) ;
618+
457619 describe ( 'finally() success tracking' , ( ) => {
458620 it ( 'sends COMMAND_SUCCESS when no error occurred' , async ( ) => {
459621 const telemetry = new Telemetry ( { project : 'test' , appInsightsKey : 'test-key' } ) ;
0 commit comments