|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import {expect} from 'chai'; |
| 8 | +import sinon from 'sinon'; |
| 9 | +import esmock from 'esmock'; |
8 | 10 | import McpServerCommand from '../../src/commands/mcp.js'; |
9 | 11 |
|
10 | 12 | describe('McpServerCommand', () => { |
@@ -96,4 +98,154 @@ describe('McpServerCommand', () => { |
96 | 98 | } |
97 | 99 | }); |
98 | 100 | }); |
| 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 | + }); |
99 | 251 | }); |
0 commit comments