@@ -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,193 @@ 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 . hostname ) . to . equal ( 'zzpq-019.dx.commercecloud.salesforce.com' ) ;
538+ expect ( attrs . tenantId ) . to . be . undefined ;
539+ } ) ;
540+
541+ it ( 'adds shortCode when available' , async ( ) => {
542+ stubParse ( command ) ;
543+ await command . init ( ) ;
544+ setupTelemetry ( command ) ;
545+ setResolvedConfig ( command , { tenantId : 'zzpq_019' , shortCode : 'kv7kzm78' } ) ;
546+
547+ command . testAddTelemetryContext ( ) ;
548+
549+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
550+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
551+ expect ( attrs . shortCode ) . to . equal ( 'kv7kzm78' ) ;
552+ } ) ;
553+
554+ it ( 'adds hostname when available' , async ( ) => {
555+ stubParse ( command ) ;
556+ await command . init ( ) ;
557+ setupTelemetry ( command ) ;
558+ setResolvedConfig ( command , { tenantId : 'zzpq_019' , hostname : 'zzpq-019.dx.commercecloud.salesforce.com' } ) ;
559+
560+ command . testAddTelemetryContext ( ) ;
561+
562+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
563+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
564+ expect ( attrs . hostname ) . to . equal ( 'zzpq-019.dx.commercecloud.salesforce.com' ) ;
565+ } ) ;
566+
567+ it ( 'adds clientId when available' , async ( ) => {
568+ stubParse ( command ) ;
569+ await command . init ( ) ;
570+ setupTelemetry ( command ) ;
571+ setResolvedConfig ( command , { tenantId : 'zzpq_019' , clientId : 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' } ) ;
572+
573+ command . testAddTelemetryContext ( ) ;
574+
575+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
576+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
577+ expect ( attrs . clientId ) . to . equal ( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) ;
578+ } ) ;
579+
580+ it ( 'adds configSources from resolved sources' , async ( ) => {
581+ stubParse ( command ) ;
582+ await command . init ( ) ;
583+ setupTelemetry ( command ) ;
584+ setResolvedConfig ( command , { tenantId : 'zzpq_019' } , [
585+ { name : 'flags' , fields : [ 'tenantId' ] } ,
586+ { name : 'dw.json' , fields : [ 'hostname' ] } ,
587+ ] ) ;
588+
589+ command . testAddTelemetryContext ( ) ;
590+
591+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
592+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
593+ expect ( attrs . configSources ) . to . equal ( 'flags, dw.json' ) ;
594+ } ) ;
595+
596+ it ( 'does not call addAttributes when no context available' , async ( ) => {
597+ stubParse ( command ) ;
598+ await command . init ( ) ;
599+ setupTelemetry ( command ) ;
600+ setResolvedConfig ( command , { } ) ;
601+
602+ command . testAddTelemetryContext ( ) ;
603+
604+ expect ( telemetryAddAttributesStub . called ) . to . be . false ;
605+ } ) ;
606+
607+ it ( 'handles f_ecom_ prefixed tenantId' , async ( ) => {
608+ stubParse ( command ) ;
609+ await command . init ( ) ;
610+ setupTelemetry ( command ) ;
611+ setResolvedConfig ( command , { tenantId : 'f_ecom_zzpq_019' } ) ;
612+
613+ command . testAddTelemetryContext ( ) ;
614+
615+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
616+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
617+ expect ( attrs . realm ) . to . equal ( 'zzpq' ) ;
618+ expect ( attrs . tenantId ) . to . equal ( 'f_ecom_zzpq_019' ) ;
619+ } ) ;
620+
621+ it ( 'handles unparseable tenantId (sets tenantId but not realm)' , async ( ) => {
622+ stubParse ( command ) ;
623+ await command . init ( ) ;
624+ setupTelemetry ( command ) ;
625+ setResolvedConfig ( command , { tenantId : 'some-custom-id-format' } ) ;
626+
627+ command . testAddTelemetryContext ( ) ;
628+
629+ expect ( telemetryAddAttributesStub . calledOnce ) . to . be . true ;
630+ const attrs = telemetryAddAttributesStub . firstCall . args [ 0 ] ;
631+ expect ( attrs . tenantId ) . to . equal ( 'some-custom-id-format' ) ;
632+ expect ( attrs . realm ) . to . be . undefined ;
633+ } ) ;
634+
635+ it ( 'does not throw when resolvedConfig has unexpected shape' , async ( ) => {
636+ stubParse ( command ) ;
637+ await command . init ( ) ;
638+ setupTelemetry ( command ) ;
639+ // Force a broken resolvedConfig to simulate unexpected runtime state
640+ command . setResolvedConfig ( null as unknown as ReturnType < typeof command . getResolvedConfig > ) ;
641+
642+ expect ( ( ) => command . testAddTelemetryContext ( ) ) . to . not . throw ( ) ;
643+ } ) ;
644+
645+ it ( 'does nothing when telemetry is not initialized' , async ( ) => {
646+ stubParse ( command ) ;
647+ await command . init ( ) ;
648+ setResolvedConfig ( command , { tenantId : 'zzpq_019' } ) ;
649+
650+ command . testAddTelemetryContext ( ) ;
651+
652+ expect ( telemetryAddAttributesStub . called ) . to . be . false ;
653+ } ) ;
654+ } ) ;
655+
457656 describe ( 'finally() success tracking' , ( ) => {
458657 it ( 'sends COMMAND_SUCCESS when no error occurred' , async ( ) => {
459658 const telemetry = new Telemetry ( { project : 'test' , appInsightsKey : 'test-key' } ) ;
0 commit comments