-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathscapi-custom-apis-get-status.test.ts
More file actions
319 lines (270 loc) · 12 KB
/
scapi-custom-apis-get-status.test.ts
File metadata and controls
319 lines (270 loc) · 12 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
/*
* 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 {describe, it, beforeEach, afterEach} from 'mocha';
import {stub, restore, type SinonStub} from 'sinon';
import {createScapiCustomApisStatusTool} from '../../../src/tools/scapi/scapi-custom-apis-get-status.js';
import {Services} from '../../../src/services.js';
import {createMockResolvedConfig} from '../../test-helpers.js';
import type {CustomApisClient, CustomApisComponents} from '@salesforce/b2c-tooling-sdk/clients';
type CustomApiEndpoint = CustomApisComponents['schemas']['CustomApiEndpoint'];
function parseResultContent(result: {content: Array<{type: string; text?: string}>; isError?: boolean}): {
parsed: null | Record<string, unknown>;
isError: boolean;
raw?: string;
} {
const first = result.content?.[0];
const text = first && 'text' in first ? (first.text ?? '') : '';
try {
return {parsed: JSON.parse(text) as Record<string, unknown>, isError: result.isError ?? false};
} catch {
return {parsed: null, isError: result.isError ?? false, raw: text};
}
}
/**
* Creates mock endpoint data (simulating SDK response).
* Focus tests on MCP-specific logic, not SDK internals.
*/
function createMockEndpoints(overrides: Partial<CustomApiEndpoint>[] = []): CustomApiEndpoint[] {
const defaultEndpoint: CustomApiEndpoint = {
apiName: 'my-api',
apiVersion: 'v1',
cartridgeName: 'app_custom',
endpointPath: '/hello',
httpMethod: 'GET',
status: 'active',
siteId: 'RefArch',
securityScheme: 'ShopperToken',
id: 'ep-1',
};
if (overrides.length === 0) {
return [defaultEndpoint];
}
return overrides.map((override) => ({...defaultEndpoint, ...override}));
}
/**
* Creates a mock SDK client response.
*/
function createMockClientResponse(endpoints: CustomApiEndpoint[], activeCodeVersion?: string) {
return {
data: {
data: endpoints,
activeCodeVersion,
},
error: undefined,
response: {status: 200, statusText: 'OK'},
};
}
describe('tools/scapi/scapi-custom-apis-get-status', () => {
let services: Services;
let mockGet: SinonStub;
beforeEach(() => {
services = new Services({
resolvedConfig: createMockResolvedConfig({
shortCode: 'test-shortcode',
tenantId: 'test_tenant',
}),
});
// Mock SDK client - focus tests on MCP-specific logic, not SDK internals
mockGet = stub();
const mockClient = {
GET: mockGet,
} as unknown as CustomApisClient;
stub(services, 'getCustomApisClient').returns(mockClient);
stub(services, 'getOrganizationId').returns('f_ecom_test_tenant');
});
afterEach(() => {
restore();
});
describe('createScapiCustomApisStatusTool', () => {
it('should create scapi_custom_apis_get_status tool with correct metadata', () => {
const tool = createScapiCustomApisStatusTool(() => services);
expect(tool).to.exist;
expect(tool.name).to.equal('scapi_custom_apis_get_status');
expect(tool.description).to.include('Custom');
expect(tool.description).to.include('endpoint');
expect(tool.description).to.include('Custom');
expect(tool.description).to.include('b2c scapi custom status');
expect(tool.inputSchema).to.exist;
expect(tool.handler).to.be.a('function');
expect(tool.toolsets).to.deep.equal(['PWAV3', 'SCAPI', 'STOREFRONTNEXT']);
expect(tool.isGA).to.be.true;
});
it('should have optional input params: status, groupBy, columns', () => {
const tool = createScapiCustomApisStatusTool(() => services);
expect(tool.inputSchema).to.have.property('status');
expect(tool.inputSchema).to.have.property('groupBy');
expect(tool.inputSchema).to.have.property('columns');
expect(tool.inputSchema).to.not.have.property('extended');
});
});
describe('handler', () => {
it('should return endpoints with default columns and add type field', async () => {
const mockEndpoints = createMockEndpoints();
mockGet.resolves(createMockClientResponse(mockEndpoints, 'version1'));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({});
expect(result.isError).to.be.undefined;
const {parsed} = parseResultContent(result);
expect(parsed).to.not.be.null;
expect(parsed?.endpoints).to.be.an('array').with.lengthOf(1);
expect(parsed?.total).to.equal(1);
expect(parsed?.activeCodeVersion).to.equal('version1');
expect(parsed?.message).to.include('1 endpoint');
expect(parsed?.timestamp).to.match(/^\d{4}-\d{2}-\d{2}T/);
// Verify MCP-specific logic: default columns include cartridgeName and type is added
const endpoint = (parsed?.endpoints as Record<string, unknown>[])?.[0];
expect(endpoint?.type).to.equal('Shopper');
expect(endpoint?.apiName).to.equal('my-api');
expect(endpoint?.cartridgeName).to.equal('app_custom');
expect(endpoint?.endpointPath).to.equal('/hello');
expect(endpoint?.httpMethod).to.equal('GET');
expect(endpoint?.status).to.equal('active');
expect(endpoint?.siteId).to.equal('RefArch');
});
it('should pass status filter to SDK when provided', async () => {
mockGet.resolves(createMockClientResponse([]));
const tool = createScapiCustomApisStatusTool(() => services);
await tool.handler({status: 'active'});
expect(mockGet.calledOnce).to.be.true;
expect(mockGet.firstCall.args[1]?.params?.query).to.deep.equal({status: 'active'});
});
it('should add type field based on securityScheme (MCP-specific transformation)', async () => {
const mockEndpoints = createMockEndpoints([
{apiName: 'admin-api', securityScheme: 'AmOAuth2'},
{apiName: 'shopper-api', securityScheme: 'ShopperToken'},
]);
mockGet.resolves(createMockClientResponse(mockEndpoints));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({});
const {parsed} = parseResultContent(result);
const endpoints = parsed?.endpoints as Array<{type?: string; apiName?: string}>;
expect(endpoints).to.have.lengthOf(2);
const adminEp = endpoints.find((e) => e.apiName === 'admin-api');
const shopperEp = endpoints.find((e) => e.apiName === 'shopper-api');
expect(adminEp?.type).to.equal('Admin');
expect(shopperEp?.type).to.equal('Shopper');
});
it('should return empty endpoints and message when no data returned', async () => {
mockGet.resolves(createMockClientResponse([], 'v1'));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({});
const {parsed} = parseResultContent(result);
expect(parsed?.endpoints).to.be.an('array').that.is.empty;
expect(parsed?.total).to.equal(0);
expect(parsed?.message).to.include('No Custom API endpoints found');
});
it('should handle SDK errors and return remoteError (MCP error handling)', async () => {
mockGet.resolves({
data: undefined,
error: {title: 'Bad Request', detail: 'Invalid filter'},
response: {status: 400, statusText: 'Bad Request'},
});
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({});
const {parsed} = parseResultContent(result);
expect(parsed?.total).to.equal(0);
expect(parsed?.remoteError).to.exist;
expect(parsed?.message).to.include('Failed to fetch Custom API endpoints');
});
it('should handle SDK exceptions and return remoteError', async () => {
mockGet.rejects(new Error('Network error'));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({});
const {parsed} = parseResultContent(result);
expect(parsed?.total).to.equal(0);
expect(parsed?.remoteError).to.include('Network error');
});
it('should group endpoints by type (MCP-specific grouping)', async () => {
const mockEndpoints = createMockEndpoints([
{apiName: 'a', securityScheme: 'AmOAuth2'},
{apiName: 'b', securityScheme: 'ShopperToken'},
]);
mockGet.resolves(createMockClientResponse(mockEndpoints));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({groupBy: 'type'});
const {parsed} = parseResultContent(result);
expect(parsed?.groups).to.exist;
expect(parsed?.endpoints).to.be.undefined;
const groups = parsed?.groups as Record<string, unknown[]> | undefined;
expect(groups?.Admin).to.be.an('array').with.lengthOf(1);
expect(groups?.Shopper).to.be.an('array').with.lengthOf(1);
expect(parsed?.total).to.equal(2);
});
it('should group endpoints by site (MCP-specific grouping)', async () => {
const mockEndpoints = createMockEndpoints([
{apiName: 'a', siteId: 'Site1'},
{apiName: 'b', siteId: 'Site2'},
]);
mockGet.resolves(createMockClientResponse(mockEndpoints));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({groupBy: 'site'});
const {parsed} = parseResultContent(result);
expect(parsed?.groups).to.exist;
const groupsBySite = parsed?.groups as Record<string, unknown[]> | undefined;
expect(groupsBySite?.Site1).to.be.an('array').with.lengthOf(1);
expect(groupsBySite?.Site2).to.be.an('array').with.lengthOf(1);
expect(parsed?.total).to.equal(2);
});
it('should filter columns when custom columns specified (MCP column selection)', async () => {
const mockEndpoints = createMockEndpoints();
mockGet.resolves(createMockClientResponse(mockEndpoints));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({columns: 'type,apiName,status'});
const {parsed} = parseResultContent(result);
const endpoint = (parsed?.endpoints as Record<string, unknown>[])?.[0];
expect(endpoint).to.have.keys('type', 'apiName', 'status');
expect(endpoint?.apiName).to.equal('my-api');
expect(endpoint?.status).to.equal('active');
expect(endpoint).to.not.have.property('endpointPath');
expect(endpoint).to.not.have.property('cartridgeName');
});
it('should return all columns when all fields specified (MCP column selection)', async () => {
const mockEndpoints = createMockEndpoints([
{
operationId: 'getHello',
schemaFile: 'schema.yaml',
implementationScript: 'controller.js',
errorReason: undefined,
},
]);
mockGet.resolves(createMockClientResponse(mockEndpoints));
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({
columns:
'type,apiName,apiVersion,cartridgeName,endpointPath,httpMethod,status,siteId,securityScheme,operationId,schemaFile,implementationScript,errorReason,id',
});
const {parsed} = parseResultContent(result);
const endpoint = (parsed?.endpoints as Record<string, unknown>[])?.[0];
// Verify all requested columns are present
const expectedFields = [
'type',
'apiName',
'apiVersion',
'cartridgeName',
'endpointPath',
'httpMethod',
'status',
'siteId',
'securityScheme',
'operationId',
'schemaFile',
'implementationScript',
'id',
];
for (const field of expectedFields) {
expect(endpoint).to.have.property(field);
}
});
it('should return validation error for invalid status value', async () => {
const tool = createScapiCustomApisStatusTool(() => services);
const result = await tool.handler({status: 'invalid'});
expect(result.isError).to.be.true;
const first = result.content?.[0] as undefined | {text?: string};
expect(first?.text).to.include('Invalid input');
});
});
});