Skip to content

Commit 895791b

Browse files
committed
Add extended mcp telemetry.start() tests
1 parent d6f09da commit 895791b

3 files changed

Lines changed: 172 additions & 1 deletion

File tree

packages/b2c-dx-mcp/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,19 @@
106106
"@types/chai": "^4",
107107
"@types/mocha": "^10",
108108
"@types/node": "^22.16.5",
109+
"@types/sinon": "^21.0.0",
109110
"chai": "^4",
110111
"eslint": "^9",
111112
"eslint-config-oclif": "^6",
112113
"eslint-config-prettier": "^10",
113114
"eslint-plugin-header": "^3.1.1",
114115
"eslint-plugin-prettier": "^5.5.4",
116+
"esmock": "^2.7.3",
115117
"mocha": "^10",
116118
"oclif": "^4",
117119
"prettier": "^3.6.2",
118120
"shx": "^0.3.3",
121+
"sinon": "^21.0.1",
119122
"tsx": "^4",
120123
"typescript": "^5",
121124
"typescript-eslint": "^8"

packages/b2c-dx-mcp/test/commands/mcp.test.ts

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
import {expect} from 'chai';
8+
import sinon from 'sinon';
9+
import esmock from 'esmock';
810
import McpServerCommand from '../../src/commands/mcp.js';
911

1012
describe('McpServerCommand', () => {
@@ -96,4 +98,154 @@ describe('McpServerCommand', () => {
9698
}
9799
});
98100
});
101+
102+
describe('telemetry initialization', () => {
103+
let sandbox: sinon.SinonSandbox;
104+
105+
beforeEach(() => {
106+
sandbox = sinon.createSandbox();
107+
});
108+
109+
afterEach(() => {
110+
sandbox.restore();
111+
});
112+
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+
136+
// Ensure telemetry is not disabled
137+
const originalEnv = process.env.SFCC_TELEMETRY;
138+
delete process.env.SFCC_TELEMETRY;
139+
140+
try {
141+
// Create command instance
142+
const command = new MockedMcpServerCommand.default([], {
143+
name: 'test',
144+
version: '1.0.0',
145+
root: process.cwd(),
146+
});
147+
148+
// Stub init to set up flags
149+
sandbox.stub(command, 'init').resolves();
150+
(command as unknown as {flags: Record<string, unknown>}).flags = {
151+
'allow-non-ga-tools': false,
152+
'log-level': 'silent',
153+
};
154+
155+
// Stub resolvedConfig with required methods
156+
sandbox.stub(command, 'resolvedConfig').get(() => ({
157+
values: {},
158+
hasMrtConfig: () => false,
159+
hasB2CInstanceConfig: () => false,
160+
hasOAuth: () => false,
161+
hasBasicAuth: () => false,
162+
}));
163+
164+
// Stub logger
165+
sandbox.stub(command, 'logger').get(() => ({info: sandbox.stub()}));
166+
167+
// Run the command
168+
await command.run();
169+
170+
// Verify telemetry.start() was called
171+
expect(mockTelemetry.start.calledOnce).to.be.true;
172+
} finally {
173+
// Restore env
174+
if (originalEnv !== undefined) {
175+
process.env.SFCC_TELEMETRY = originalEnv;
176+
}
177+
}
178+
});
179+
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+
205+
// Disable telemetry via env
206+
const originalEnv = process.env.SFCC_TELEMETRY;
207+
process.env.SFCC_TELEMETRY = 'false';
208+
209+
try {
210+
// Create command instance
211+
const command = new MockedMcpServerCommand.default([], {
212+
name: 'test',
213+
version: '1.0.0',
214+
root: process.cwd(),
215+
});
216+
217+
// Stub init to set up flags
218+
sandbox.stub(command, 'init').resolves();
219+
(command as unknown as {flags: Record<string, unknown>}).flags = {
220+
'allow-non-ga-tools': false,
221+
'log-level': 'silent',
222+
};
223+
224+
// Stub resolvedConfig with required methods
225+
sandbox.stub(command, 'resolvedConfig').get(() => ({
226+
values: {},
227+
hasMrtConfig: () => false,
228+
hasB2CInstanceConfig: () => false,
229+
hasOAuth: () => false,
230+
hasBasicAuth: () => false,
231+
}));
232+
233+
// Stub logger
234+
sandbox.stub(command, 'logger').get(() => ({info: sandbox.stub()}));
235+
236+
// Run the command
237+
await command.run();
238+
239+
// Verify createTelemetry was NOT called
240+
expect(createTelemetryStub.called).to.be.false;
241+
} finally {
242+
// Restore env
243+
if (originalEnv === undefined) {
244+
delete process.env.SFCC_TELEMETRY;
245+
} else {
246+
process.env.SFCC_TELEMETRY = originalEnv;
247+
}
248+
}
249+
});
250+
});
99251
});

pnpm-lock.yaml

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)