-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathregistry.test.ts
More file actions
429 lines (356 loc) · 16.7 KB
/
registry.test.ts
File metadata and controls
429 lines (356 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {expect} from 'chai';
import {createToolRegistry, registerToolsets} from '../src/registry.js';
import {Services} from '../src/services.js';
import {B2CDxMcpServer} from '../src/server.js';
import type {StartupFlags} from '../src/utils/types.js';
import {createMockResolvedConfig} from './test-helpers.js';
// Create a loadServices function for testing
function createMockLoadServicesWrapper(): () => Services {
const services = new Services({resolvedConfig: createMockResolvedConfig()});
return () => services;
}
// Create a mock server that tracks registered tools
function createMockServer(): B2CDxMcpServer & {registeredTools: string[]} {
const registeredTools: string[] = [];
const server = {
registeredTools,
addTool(name: string) {
registeredTools.push(name);
return {name, enabled: true};
},
} as unknown as B2CDxMcpServer & {registeredTools: string[]};
return server;
}
describe('registry', () => {
describe('createToolRegistry', () => {
it('should create a registry with all toolsets', () => {
const loadServices = createMockLoadServicesWrapper();
const registry = createToolRegistry(loadServices);
// Verify all expected toolsets exist
expect(registry).to.have.property('CARTRIDGES');
expect(registry).to.have.property('MRT');
expect(registry).to.have.property('PWAV3');
expect(registry).to.have.property('SCAPI');
expect(registry).to.have.property('STOREFRONTNEXT');
});
it('should create CARTRIDGES tools', () => {
const loadServices = createMockLoadServicesWrapper();
const registry = createToolRegistry(loadServices);
expect(registry.CARTRIDGES).to.be.an('array');
expect(registry.CARTRIDGES.length).to.be.greaterThan(0);
const toolNames = registry.CARTRIDGES.map((t) => t.name);
expect(toolNames).to.include('cartridge_deploy');
});
it('should create MRT tools', () => {
const loadServices = createMockLoadServicesWrapper();
const registry = createToolRegistry(loadServices);
expect(registry.MRT).to.be.an('array');
expect(registry.MRT.length).to.be.greaterThan(0);
const toolNames = registry.MRT.map((t) => t.name);
expect(toolNames).to.include('mrt_bundle_push');
});
it('should create PWAV3 tools', () => {
const loadServices = createMockLoadServicesWrapper();
const registry = createToolRegistry(loadServices);
expect(registry.PWAV3).to.be.an('array');
expect(registry.PWAV3.length).to.be.greaterThan(0);
const toolNames = registry.PWAV3.map((t) => t.name);
expect(toolNames).to.include('pwakit_create_storefront');
expect(toolNames).to.include('pwakit_create_page');
expect(toolNames).to.include('pwakit_create_component');
// mrt_bundle_push should also appear in PWAV3 (multi-toolset)
expect(toolNames).to.include('mrt_bundle_push');
});
it('should create SCAPI tools', () => {
const loadServices = createMockLoadServicesWrapper();
const registry = createToolRegistry(loadServices);
expect(registry.SCAPI).to.be.an('array');
expect(registry.SCAPI.length).to.be.greaterThan(0);
const toolNames = registry.SCAPI.map((t) => t.name);
expect(toolNames).to.include('scapi_schemas_list');
expect(toolNames).to.include('scapi_custom_apis_status');
expect(toolNames).to.include('scapi_customapi_scaffold');
});
it('should create STOREFRONTNEXT tools', () => {
const loadServices = createMockLoadServicesWrapper();
const registry = createToolRegistry(loadServices);
expect(registry.STOREFRONTNEXT).to.be.an('array');
expect(registry.STOREFRONTNEXT.length).to.be.greaterThan(0);
const toolNames = registry.STOREFRONTNEXT.map((t) => t.name);
expect(toolNames).to.include('storefront_next_development_guidelines');
expect(toolNames).to.include('storefront_next_site_theming');
expect(toolNames).to.include('storefront_next_generate_component');
// mrt_bundle_push should also appear in STOREFRONTNEXT (multi-toolset)
expect(toolNames).to.include('mrt_bundle_push');
});
it('should assign correct toolsets to each tool', () => {
const loadServices = createMockLoadServicesWrapper();
const registry = createToolRegistry(loadServices);
// Verify tools have correct toolset assignments
for (const tool of registry.CARTRIDGES) {
expect(tool.toolsets).to.include('CARTRIDGES');
}
for (const tool of registry.MRT) {
expect(tool.toolsets).to.include('MRT');
}
for (const tool of registry.PWAV3) {
expect(tool.toolsets).to.include('PWAV3');
}
for (const tool of registry.SCAPI) {
expect(tool.toolsets).to.include('SCAPI');
}
for (const tool of registry.STOREFRONTNEXT) {
expect(tool.toolsets).to.include('STOREFRONTNEXT');
}
});
});
describe('registerToolsets', () => {
it('should auto-discover and register tools when no toolsets or tools provided', async () => {
const server = createMockServer();
// Use a workspace path that won't match any patterns (should fall back to SCAPI)
const flags: StartupFlags = {
workingDirectory: '/nonexistent/path',
allowNonGaTools: true,
};
// When no flags provided, auto-discovery kicks in
// With an unknown project, it falls back to SCAPI toolset
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
expect(server.registeredTools.length).to.be.greaterThan(0);
// SCAPI tools should be registered as fallback
expect(server.registeredTools).to.include('scapi_schemas_list');
});
it('should skip auto-discovery when empty toolsets array is explicitly provided', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: [],
allowNonGaTools: true,
};
// Empty toolsets array still triggers auto-discovery (length is 0)
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Should have auto-discovered SCAPI as fallback
expect(server.registeredTools).to.include('scapi_schemas_list');
});
it('should register tools from a single toolset', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['CARTRIDGES'],
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
expect(server.registeredTools).to.include('cartridge_deploy');
// Should not include tools exclusive to other toolsets
expect(server.registeredTools).to.not.include('scapi_custom_apis_status');
});
it('should register tools from multiple toolsets', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['CARTRIDGES', 'MRT'],
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Should include CARTRIDGES tools
expect(server.registeredTools).to.include('cartridge_deploy');
// Should include MRT tools
expect(server.registeredTools).to.include('mrt_bundle_push');
// Should not include PWAV3-only tools
expect(server.registeredTools).to.not.include('pwakit_create_storefront');
});
it('should register all toolsets when ALL is specified', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['ALL'],
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Should include tools from all toolsets
expect(server.registeredTools).to.include('cartridge_deploy');
expect(server.registeredTools).to.include('mrt_bundle_push');
expect(server.registeredTools).to.include('pwakit_create_storefront');
expect(server.registeredTools).to.include('scapi_schemas_list');
expect(server.registeredTools).to.include('storefront_next_development_guidelines');
});
it('should register individual tools via --tools flag', async () => {
const server = createMockServer();
const flags: StartupFlags = {
tools: ['cartridge_deploy', 'mrt_bundle_push'],
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
expect(server.registeredTools).to.have.lengthOf(2);
expect(server.registeredTools).to.include('cartridge_deploy');
expect(server.registeredTools).to.include('mrt_bundle_push');
});
it('should combine toolsets and individual tools', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['CARTRIDGES'],
tools: ['scapi_custom_apis_status'],
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Should include all CARTRIDGES tools
expect(server.registeredTools).to.include('cartridge_deploy');
// Should also include the individual SCAPI tool
expect(server.registeredTools).to.include('scapi_custom_apis_status');
// Should not include other SCAPI tools not in CARTRIDGES
expect(server.registeredTools).to.not.include('scapi_schemas_list');
});
it('should not duplicate tools when specified in both toolset and --tools', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['CARTRIDGES'],
tools: ['cartridge_deploy'], // Already in CARTRIDGES
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Should only have one instance of cartridge_deploy
const cartridgeDeployCount = server.registeredTools.filter((t) => t === 'cartridge_deploy').length;
expect(cartridgeDeployCount).to.equal(1);
});
it('should warn and skip invalid tool names', async () => {
const server = createMockServer();
const flags: StartupFlags = {
tools: ['nonexistent_tool', 'cartridge_deploy'],
allowNonGaTools: true,
};
// Should not throw, just skip invalid tools
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Valid tool should be registered
expect(server.registeredTools).to.include('cartridge_deploy');
// Invalid tool should be skipped (not cause error)
expect(server.registeredTools).to.not.include('nonexistent_tool');
});
it('should warn and skip invalid toolset names', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['INVALID_TOOLSET', 'CARTRIDGES'],
allowNonGaTools: true,
};
// Should not throw, just skip invalid toolsets
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Valid toolset's tools should be registered
expect(server.registeredTools).to.include('cartridge_deploy');
});
it('should trigger auto-discovery when all toolsets are invalid', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['INVALID1', 'INVALID2'],
allowNonGaTools: true,
};
// Should not throw - triggers auto-discovery as fallback
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Auto-discovery always includes BASE_TOOLSET (SCAPI), even if no project type detected
expect(server.registeredTools).to.include('scapi_schemas_list');
expect(server.registeredTools).to.include('scapi_custom_apis_status');
expect(server.registeredTools).to.include('scapi_customapi_scaffold');
});
it('should trigger auto-discovery when all individual tools are invalid', async () => {
const server = createMockServer();
const flags: StartupFlags = {
tools: ['nonexistent_tool', 'another_fake_tool'],
allowNonGaTools: true,
};
// Should not throw - triggers auto-discovery as fallback
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Auto-discovery always includes BASE_TOOLSET (SCAPI), even if no project type detected
expect(server.registeredTools).to.include('scapi_schemas_list');
expect(server.registeredTools).to.include('scapi_custom_apis_status');
expect(server.registeredTools).to.include('scapi_customapi_scaffold');
});
it('should skip non-GA tools when allowNonGaTools is false', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['CARTRIDGES'],
allowNonGaTools: false,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// All current CARTRIDGES tools are non-GA (isGA: false)
// So none should be registered
expect(server.registeredTools).to.have.lengthOf(0);
});
it('should register GA tools even when allowNonGaTools is false', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['ALL'],
allowNonGaTools: false,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Currently all tools are non-GA placeholders
// This test documents expected behavior for when GA tools exist
// When GA tools are added, this test should be updated to verify they are registered
});
describe('auto-discovery', () => {
it('should use workingDirectory from flags for detection', async () => {
const server = createMockServer();
const flags: StartupFlags = {
workingDirectory: '/some/workspace',
allowNonGaTools: true,
};
// Should not throw even with non-existent path
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Falls back to SCAPI for unknown projects
expect(server.registeredTools).to.include('scapi_schemas_list');
});
it('should map detected project type to MCP toolsets', async () => {
const server = createMockServer();
// Use a path that doesn't exist - detection will return 'unknown' project type
// which maps to SCAPI toolset
const flags: StartupFlags = {
workingDirectory: '/nonexistent',
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Only SCAPI tools should be registered (the fallback for unknown projects)
expect(server.registeredTools).to.include('scapi_schemas_list');
});
it('should not auto-discover when individual tools are provided', async () => {
const server = createMockServer();
const flags: StartupFlags = {
tools: ['cartridge_deploy'],
workingDirectory: '/some/workspace',
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Should only have the explicitly requested tool
expect(server.registeredTools).to.have.lengthOf(1);
expect(server.registeredTools).to.include('cartridge_deploy');
});
it('should not auto-discover when toolsets are explicitly provided', async () => {
const server = createMockServer();
const flags: StartupFlags = {
toolsets: ['CARTRIDGES'],
workingDirectory: '/some/workspace',
allowNonGaTools: true,
};
const loadServices = createMockLoadServicesWrapper();
await registerToolsets(flags, server, loadServices);
// Should only have CARTRIDGES tools, not auto-discovered toolsets
expect(server.registeredTools).to.include('cartridge_deploy');
// Should not have tools from other toolsets unless explicitly requested
expect(server.registeredTools).to.not.include('pwakit_create_storefront');
});
});
});
});