66
77import { expect } from 'chai' ;
88import sinon from 'sinon' ;
9- import esmock from 'esmock ' ;
9+ import { Telemetry } from '@salesforce/b2c-tooling-sdk/telemetry ' ;
1010import McpServerCommand from '../../src/commands/mcp.js' ;
11+ import { B2CDxMcpServer } from '../../src/server.js' ;
1112
1213describe ( 'McpServerCommand' , ( ) => {
1314 describe ( 'static properties' , ( ) => {
@@ -101,49 +102,38 @@ describe('McpServerCommand', () => {
101102
102103 describe ( 'telemetry initialization' , ( ) => {
103104 let sandbox : sinon . SinonSandbox ;
105+ let telemetryStartStub : sinon . SinonStub ;
106+ let serverConnectStub : sinon . SinonStub ;
104107
105108 beforeEach ( ( ) => {
106109 sandbox = sinon . createSandbox ( ) ;
110+
111+ // Stub Telemetry prototype methods - this works because createTelemetry
112+ // creates instances with `new Telemetry()`, so all instances use these stubs
113+ telemetryStartStub = sandbox . stub ( Telemetry . prototype , 'start' ) . resolves ( ) ;
114+ sandbox . stub ( Telemetry . prototype , 'stop' ) ;
115+ sandbox . stub ( Telemetry . prototype , 'sendEvent' ) ;
116+
117+ // Stub server.connect to prevent actual stdio transport
118+ serverConnectStub = sandbox . stub ( B2CDxMcpServer . prototype , 'connect' ) . resolves ( ) ;
107119 } ) ;
108120
109121 afterEach ( ( ) => {
110122 sandbox . restore ( ) ;
111123 } ) ;
112124
113- it ( 'should call createTelemetry and telemetry.start() when SFCC_TELEMETRY is not false' , async ( ) => {
114- // Create mock telemetry instance
115- const mockTelemetry = {
116- start : sandbox . stub ( ) . resolves ( ) ,
117- stop : sandbox . stub ( ) ,
118- sendEvent : sandbox . stub ( ) ,
119- addAttributes : sandbox . stub ( ) ,
120- } ;
121-
122- // Use esmock to mock the ES module imports
123- const MockedMcpServerCommand = await esmock ( '../../src/commands/mcp.js' , {
124- '@salesforce/b2c-tooling-sdk/telemetry' : {
125- createTelemetry : sandbox . stub ( ) . returns ( mockTelemetry ) ,
126- } ,
127- '../../src/server.js' : {
128- B2CDxMcpServer : class MockServer {
129- async connect ( ) {
130- /* no-op */
131- }
132- } ,
133- } ,
134- } ) ;
135-
125+ it ( 'should call telemetry.start() when SFCC_TELEMETRY is not false' , async ( ) => {
136126 // Ensure telemetry is not disabled
137127 const originalEnv = process . env . SFCC_TELEMETRY ;
138128 delete process . env . SFCC_TELEMETRY ;
139129
140130 try {
141- // Create command instance
142- const command = new MockedMcpServerCommand . default ( [ ] , {
131+ // Create command instance - cast config to avoid oclif type complexity
132+ const command = new McpServerCommand ( [ ] , {
143133 name : 'test' ,
144134 version : '1.0.0' ,
145135 root : process . cwd ( ) ,
146- } ) ;
136+ } as never ) ;
147137
148138 // Stub init to set up flags
149139 sandbox . stub ( command , 'init' ) . resolves ( ) ;
@@ -152,23 +142,25 @@ describe('McpServerCommand', () => {
152142 'log-level' : 'silent' ,
153143 } ;
154144
155- // Stub resolvedConfig with required methods
156- sandbox . stub ( command , 'resolvedConfig' ) . get ( ( ) => ( {
145+ // Stub resolvedConfig with required methods (cast to bypass protected accessor)
146+ sandbox . stub ( command as unknown as Record < string , unknown > , 'resolvedConfig' ) . get ( ( ) => ( {
157147 values : { } ,
158148 hasMrtConfig : ( ) => false ,
159149 hasB2CInstanceConfig : ( ) => false ,
160150 hasOAuth : ( ) => false ,
161151 hasBasicAuth : ( ) => false ,
162152 } ) ) ;
163153
164- // Stub logger
165- sandbox . stub ( command , 'logger' ) . get ( ( ) => ( { info : sandbox . stub ( ) } ) ) ;
154+ // Stub logger (cast to bypass protected accessor)
155+ sandbox . stub ( command as unknown as Record < string , unknown > , 'logger' ) . get ( ( ) => ( { info : sandbox . stub ( ) } ) ) ;
166156
167157 // Run the command
168158 await command . run ( ) ;
169159
170160 // Verify telemetry.start() was called
171- expect ( mockTelemetry . start . calledOnce ) . to . be . true ;
161+ expect ( telemetryStartStub . calledOnce ) . to . be . true ;
162+ // Verify server.connect was called (server started successfully)
163+ expect ( serverConnectStub . calledOnce ) . to . be . true ;
172164 } finally {
173165 // Restore env
174166 if ( originalEnv !== undefined ) {
@@ -177,42 +169,18 @@ describe('McpServerCommand', () => {
177169 }
178170 } ) ;
179171
180- it ( 'should not initialize telemetry when SFCC_TELEMETRY is false' , async ( ) => {
181- // Create mock telemetry instance
182- const mockTelemetry = {
183- start : sandbox . stub ( ) . resolves ( ) ,
184- stop : sandbox . stub ( ) ,
185- sendEvent : sandbox . stub ( ) ,
186- addAttributes : sandbox . stub ( ) ,
187- } ;
188-
189- const createTelemetryStub = sandbox . stub ( ) . returns ( mockTelemetry ) ;
190-
191- // Use esmock to mock the ES module imports
192- const MockedMcpServerCommand = await esmock ( '../../src/commands/mcp.js' , {
193- '@salesforce/b2c-tooling-sdk/telemetry' : {
194- createTelemetry : createTelemetryStub ,
195- } ,
196- '../../src/server.js' : {
197- B2CDxMcpServer : class MockServer {
198- async connect ( ) {
199- /* no-op */
200- }
201- } ,
202- } ,
203- } ) ;
204-
172+ it ( 'should not call telemetry.start() when SFCC_TELEMETRY is false' , async ( ) => {
205173 // Disable telemetry via env
206174 const originalEnv = process . env . SFCC_TELEMETRY ;
207175 process . env . SFCC_TELEMETRY = 'false' ;
208176
209177 try {
210- // Create command instance
211- const command = new MockedMcpServerCommand . default ( [ ] , {
178+ // Create command instance - cast config to avoid oclif type complexity
179+ const command = new McpServerCommand ( [ ] , {
212180 name : 'test' ,
213181 version : '1.0.0' ,
214182 root : process . cwd ( ) ,
215- } ) ;
183+ } as never ) ;
216184
217185 // Stub init to set up flags
218186 sandbox . stub ( command , 'init' ) . resolves ( ) ;
@@ -221,23 +189,25 @@ describe('McpServerCommand', () => {
221189 'log-level' : 'silent' ,
222190 } ;
223191
224- // Stub resolvedConfig with required methods
225- sandbox . stub ( command , 'resolvedConfig' ) . get ( ( ) => ( {
192+ // Stub resolvedConfig with required methods (cast to bypass protected accessor)
193+ sandbox . stub ( command as unknown as Record < string , unknown > , 'resolvedConfig' ) . get ( ( ) => ( {
226194 values : { } ,
227195 hasMrtConfig : ( ) => false ,
228196 hasB2CInstanceConfig : ( ) => false ,
229197 hasOAuth : ( ) => false ,
230198 hasBasicAuth : ( ) => false ,
231199 } ) ) ;
232200
233- // Stub logger
234- sandbox . stub ( command , 'logger' ) . get ( ( ) => ( { info : sandbox . stub ( ) } ) ) ;
201+ // Stub logger (cast to bypass protected accessor)
202+ sandbox . stub ( command as unknown as Record < string , unknown > , 'logger' ) . get ( ( ) => ( { info : sandbox . stub ( ) } ) ) ;
235203
236204 // Run the command
237205 await command . run ( ) ;
238206
239- // Verify createTelemetry was NOT called
240- expect ( createTelemetryStub . called ) . to . be . false ;
207+ // Verify telemetry.start() was NOT called (telemetry disabled)
208+ expect ( telemetryStartStub . called ) . to . be . false ;
209+ // Verify server.connect was still called (server started successfully)
210+ expect ( serverConnectStub . calledOnce ) . to . be . true ;
241211 } finally {
242212 // Restore env
243213 if ( originalEnv === undefined ) {
0 commit comments