@@ -9,6 +9,8 @@ import {createSandbox, type SinonStub, type SinonSandbox} from 'sinon';
99import { Telemetry } from '@salesforce/b2c-tooling-sdk/telemetry' ;
1010import McpServerCommand from '../../src/commands/mcp.js' ;
1111import { B2CDxMcpServer } from '../../src/server.js' ;
12+ import { Services } from '../../src/services.js' ;
13+ import { createMockResolvedConfig } from '../test-helpers.js' ;
1214
1315describe ( 'McpServerCommand' , ( ) => {
1416 describe ( 'static properties' , ( ) => {
@@ -505,6 +507,118 @@ describe('McpServerCommand', () => {
505507 } ) ;
506508 } ) ;
507509
510+ describe ( 'loadServices' , ( ) => {
511+ let sandbox : SinonSandbox ;
512+ let command : McpServerCommand ;
513+ let loadConfigurationStub : SinonStub ;
514+ let fromResolvedConfigStub : SinonStub ;
515+
516+ beforeEach ( ( ) => {
517+ sandbox = createSandbox ( ) ;
518+ command = new McpServerCommand ( [ ] , {
519+ name : 'test' ,
520+ version : '1.0.0' ,
521+ root : process . cwd ( ) ,
522+ dataDir : '/tmp/test-data' ,
523+ } as never ) ;
524+ } ) ;
525+
526+ afterEach ( ( ) => {
527+ sandbox . restore ( ) ;
528+ } ) ;
529+
530+ it ( 'should call loadConfiguration and Services.fromResolvedConfig' , ( ) => {
531+ const mockConfig = createMockResolvedConfig ( ) ;
532+ const mockServices = new Services ( {
533+ resolvedConfig : mockConfig ,
534+ } ) ;
535+
536+ // Stub loadConfiguration to return mock config
537+ loadConfigurationStub = sandbox
538+ . stub ( command as unknown as Record < string , unknown > , 'loadConfiguration' )
539+ . returns ( mockConfig ) ;
540+
541+ // Stub Services.fromResolvedConfig to return mock services
542+ fromResolvedConfigStub = sandbox . stub ( Services , 'fromResolvedConfig' ) . returns ( mockServices ) ;
543+
544+ // Call loadServices via protected access
545+ const services = ( command as unknown as { loadServices ( ) : Services } ) . loadServices ( ) ;
546+
547+ // Verify loadConfiguration was called
548+ expect ( loadConfigurationStub . calledOnce ) . to . be . true ;
549+
550+ // Verify Services.fromResolvedConfig was called with the config from loadConfiguration
551+ expect ( fromResolvedConfigStub . calledOnce ) . to . be . true ;
552+ expect ( fromResolvedConfigStub . firstCall . args [ 0 ] ) . to . equal ( mockConfig ) ;
553+
554+ // Verify the returned services instance
555+ expect ( services ) . to . equal ( mockServices ) ;
556+ } ) ;
557+
558+ it ( 'should return Services instance created from resolved config' , ( ) => {
559+ const mockConfig = createMockResolvedConfig ( {
560+ hostname : 'test-server' ,
561+ mrtProject : 'test-project' ,
562+ } ) ;
563+ const mockServices = new Services ( {
564+ resolvedConfig : mockConfig ,
565+ } ) ;
566+
567+ // Stub loadConfiguration
568+ sandbox . stub ( command as unknown as Record < string , unknown > , 'loadConfiguration' ) . returns ( mockConfig ) ;
569+
570+ // Stub Services.fromResolvedConfig to return mock services
571+ sandbox . stub ( Services , 'fromResolvedConfig' ) . returns ( mockServices ) ;
572+
573+ // Call loadServices
574+ const services = ( command as unknown as { loadServices ( ) : Services } ) . loadServices ( ) ;
575+
576+ // Verify the returned services instance
577+ expect ( services ) . to . equal ( mockServices ) ;
578+ expect ( services ) . to . be . instanceOf ( Services ) ;
579+ } ) ;
580+
581+ it ( 'should reload configuration on each call' , ( ) => {
582+ const mockConfig1 = createMockResolvedConfig ( { hostname : 'server1' } ) ;
583+ const mockConfig2 = createMockResolvedConfig ( { hostname : 'server2' } ) ;
584+ const mockServices1 = new Services ( { resolvedConfig : mockConfig1 } ) ;
585+ const mockServices2 = new Services ( { resolvedConfig : mockConfig2 } ) ;
586+
587+ // Stub loadConfiguration to return different configs on each call
588+ const loadConfigurationStub = sandbox
589+ . stub ( command as unknown as Record < string , unknown > , 'loadConfiguration' )
590+ . onFirstCall ( )
591+ . returns ( mockConfig1 )
592+ . onSecondCall ( )
593+ . returns ( mockConfig2 ) ;
594+
595+ // Stub Services.fromResolvedConfig to return different services
596+ const fromResolvedConfigStub = sandbox
597+ . stub ( Services , 'fromResolvedConfig' )
598+ . onFirstCall ( )
599+ . returns ( mockServices1 )
600+ . onSecondCall ( )
601+ . returns ( mockServices2 ) ;
602+
603+ // Call loadServices twice
604+ const services1 = ( command as unknown as { loadServices ( ) : Services } ) . loadServices ( ) ;
605+ const services2 = ( command as unknown as { loadServices ( ) : Services } ) . loadServices ( ) ;
606+
607+ // Verify loadConfiguration was called twice
608+ expect ( loadConfigurationStub . calledTwice ) . to . be . true ;
609+
610+ // Verify Services.fromResolvedConfig was called with correct configs
611+ expect ( fromResolvedConfigStub . calledTwice ) . to . be . true ;
612+ expect ( fromResolvedConfigStub . firstCall . args [ 0 ] ) . to . equal ( mockConfig1 ) ;
613+ expect ( fromResolvedConfigStub . secondCall . args [ 0 ] ) . to . equal ( mockConfig2 ) ;
614+
615+ // Verify different services instances were returned
616+ expect ( services1 ) . to . equal ( mockServices1 ) ;
617+ expect ( services2 ) . to . equal ( mockServices2 ) ;
618+ expect ( services1 ) . to . not . equal ( services2 ) ;
619+ } ) ;
620+ } ) ;
621+
508622 describe ( 'finally' , ( ) => {
509623 let sandbox : SinonSandbox ;
510624 let command : McpServerCommand ;
0 commit comments