-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathscapi-custom-api-generate-scaffold.test.ts
More file actions
324 lines (274 loc) · 12.7 KB
/
scapi-custom-api-generate-scaffold.test.ts
File metadata and controls
324 lines (274 loc) · 12.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
/*
* 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 fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {
createScaffoldCustomApiTool,
executeScaffoldCustomApi,
} from '../../../src/tools/scapi/scapi-custom-api-generate-scaffold.js';
import {Services} from '../../../src/services.js';
import {createMockResolvedConfig} from '../../test-helpers.js';
import type {ToolResult} from '../../../src/utils/types.js';
/**
* Parse JSON from a ToolResult (success case).
*/
function getResultJson<T>(result: ToolResult): T {
const content = result.content[0];
if (content.type !== 'text') {
throw new Error(`Expected text content, got ${content.type}`);
}
return JSON.parse(content.text) as T;
}
/**
* Get raw text from a ToolResult (error case).
*/
function getResultText(result: ToolResult): string {
const content = result.content[0];
if (content.type !== 'text') {
throw new Error(`Expected text content, got ${content.type}`);
}
return content.text;
}
interface ScaffoldOutput {
scaffold: string;
outputDir: string;
dryRun: boolean;
files: Array<{path: string; action: string; skipReason?: string}>;
postInstructions?: string;
error?: string;
}
describe('tools/scapi/scapi-custom-api-generate-scaffold', () => {
let services: Services;
let tempDir: string;
let loadServices: () => Services;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'b2c-mcp-scaffold-test-'));
services = new Services({
resolvedConfig: createMockResolvedConfig({projectDirectory: tempDir}),
});
loadServices = () => services;
});
afterEach(() => {
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, {recursive: true, force: true});
}
});
describe('createScaffoldCustomApiTool', () => {
it('should create scapi_custom_api_generate_scaffold tool with correct metadata', () => {
const tool = createScaffoldCustomApiTool(loadServices);
expect(tool).to.exist;
expect(tool.name).to.equal('scapi_custom_api_generate_scaffold');
expect(tool.description).to.include('custom SCAPI');
expect(tool.description).to.include('apiName');
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 required apiName and optional cartridgeName, apiType, apiDescription, projectRoot, outputDir', () => {
const tool = createScaffoldCustomApiTool(loadServices);
expect(tool.inputSchema).to.have.property('apiName');
expect(tool.inputSchema).to.have.property('cartridgeName');
expect(tool.inputSchema).to.have.property('apiType');
expect(tool.inputSchema).to.have.property('apiDescription');
expect(tool.inputSchema).to.have.property('projectRoot');
expect(tool.inputSchema).to.have.property('outputDir');
});
});
describe('handler', () => {
it('should return error when no cartridges found in project (no .project file)', async () => {
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({apiName: 'my-api'});
expect(result.isError).to.be.true;
const text = getResultText(result);
expect(text).to.include('No cartridges found');
expect(text).to.include('.project');
});
it('should fail fast with "No cartridges found" when cartridgeName provided but project has no cartridges', async () => {
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({
apiName: 'my-api',
cartridgeName: 'app_custom',
});
expect(result.isError).to.be.true;
const text = getResultText(result);
expect(text).to.include('No cartridges found');
expect(text).not.to.include('Parameter validation failed');
});
it('should validate apiName is required', async () => {
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({});
expect(result.isError).to.be.true;
const text = getResultText(result);
expect(text).to.include('Invalid input');
});
it('should validate apiName is non-empty', async () => {
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({apiName: ''});
expect(result.isError).to.be.true;
const text = getResultText(result);
expect(text).to.include('Invalid input');
});
it('should validate apiType when provided', async () => {
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({apiName: 'my-api', apiType: 'invalid'});
expect(result.isError).to.be.true;
const text = getResultText(result);
expect(text).to.include('Invalid input');
});
it('should generate custom API files when cartridge exists (first cartridge used by default)', async () => {
const cartridgeDir = path.join(tempDir, 'app_custom');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({apiName: 'test-api'});
expect(result.isError).to.be.undefined;
const output = getResultJson<ScaffoldOutput>(result);
expect(output.scaffold).to.equal('custom-api');
expect(output.error).to.be.undefined;
expect(output.files).to.be.an('array').with.lengthOf(3);
expect(output.dryRun).to.be.false;
const paths = output.files.map((f) => f.path);
expect(paths.some((p) => p.includes('test-api') && p.endsWith('schema.yaml'))).to.be.true;
expect(paths.some((p) => p.includes('test-api') && p.endsWith('api.json'))).to.be.true;
expect(paths.some((p) => p.includes('test-api') && p.endsWith('script.js'))).to.be.true;
expect(output.postInstructions).to.include('test-api');
expect(output.postInstructions).to.include('app_custom');
const schemaPath = path.join(tempDir, 'app_custom', 'cartridge', 'rest-apis', 'test-api', 'schema.yaml');
expect(fs.existsSync(schemaPath)).to.be.true;
});
it('should use provided cartridgeName when given', async () => {
const cartridgeDir = path.join(tempDir, 'app_my_cartridge');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({
apiName: 'my-endpoints',
cartridgeName: 'app_my_cartridge',
});
expect(result.isError).to.be.undefined;
const output = getResultJson<ScaffoldOutput>(result);
expect(output.files).to.have.lengthOf(3);
expect(output.postInstructions).to.include('app_my_cartridge');
const scriptPath = path.join(tempDir, 'app_my_cartridge', 'cartridge', 'rest-apis', 'my-endpoints', 'script.js');
expect(fs.existsSync(scriptPath)).to.be.true;
});
it('should pass apiType admin and include in generated schema', async () => {
const cartridgeDir = path.join(tempDir, 'int_admin');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({
apiName: 'admin-only',
cartridgeName: 'int_admin',
apiType: 'admin',
});
expect(result.isError).to.be.undefined;
const schemaPath = path.join(tempDir, 'int_admin', 'cartridge', 'rest-apis', 'admin-only', 'schema.yaml');
expect(fs.existsSync(schemaPath)).to.be.true;
const schemaContent = fs.readFileSync(schemaPath, 'utf8');
expect(schemaContent).to.include('AmOAuth2');
});
it('should pass apiDescription and include in generated schema', async () => {
const cartridgeDir = path.join(tempDir, 'app_custom');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
const tool = createScaffoldCustomApiTool(loadServices);
await tool.handler({
apiName: 'described-api',
apiDescription: 'My custom description for the API',
});
const schemaPath = path.join(tempDir, 'app_custom', 'cartridge', 'rest-apis', 'described-api', 'schema.yaml');
const schemaContent = fs.readFileSync(schemaPath, 'utf8');
expect(schemaContent).to.include('My custom description for the API');
});
it('should use projectRoot when provided', async () => {
const otherDir = fs.mkdtempSync(path.join(os.tmpdir(), 'b2c-mcp-scaffold-other-'));
const cartridgeDir = path.join(otherDir, 'app_other');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
try {
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({
apiName: 'other-api',
projectRoot: otherDir,
});
expect(result.isError).to.be.undefined;
const output = getResultJson<ScaffoldOutput>(result);
expect(output.outputDir).to.equal(otherDir);
const schemaPath = path.join(otherDir, 'app_other', 'cartridge', 'rest-apis', 'other-api', 'schema.yaml');
expect(fs.existsSync(schemaPath)).to.be.true;
} finally {
fs.rmSync(otherDir, {recursive: true, force: true});
}
});
it('should return error when parameter validation fails (invalid cartridgeName)', async () => {
const cartridgeDir = path.join(tempDir, 'app_custom');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({
apiName: 'my-api',
cartridgeName: 'nonexistent_cartridge',
});
expect(result.isError).to.be.true;
const text = getResultText(result);
expect(text).to.include('Parameter validation failed');
});
it('should return error when generateFromScaffold throws', async () => {
const cartridgeDir = path.join(tempDir, 'app_custom');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
// Use outputDir that is a file (not a directory) so scaffold write fails
const fileAsDir = path.join(tempDir, 'blocker');
fs.writeFileSync(fileAsDir, '', 'utf8');
const tool = createScaffoldCustomApiTool(loadServices);
const result = await tool.handler({
apiName: 'my-api',
projectRoot: tempDir,
outputDir: fileAsDir,
});
expect(result.isError).to.be.true;
const text = getResultText(result);
expect(text).to.include('Scaffold generation failed');
});
it('should return error when scaffold is not found (executeScaffoldCustomApi with getScaffold override)', async () => {
const result = await executeScaffoldCustomApi({apiName: 'my-api'}, services, {getScaffold: async () => null});
expect(result.error).to.be.a('string');
expect(result.error).to.include('Scaffold not found');
expect(result.error).to.include('custom-api');
expect(result.files).to.deep.equal([]);
});
it('should return error when required parameter is missing (executeScaffoldCustomApi with resolveScaffoldParameters override)', async () => {
const cartridgeDir = path.join(tempDir, 'app_custom');
fs.mkdirSync(cartridgeDir, {recursive: true});
fs.writeFileSync(path.join(cartridgeDir, '.project'), '', 'utf8');
const result = await executeScaffoldCustomApi({apiName: 'my-api'}, services, {
getScaffold: async () =>
({
id: 'custom-api',
manifest: {},
path: '',
filesPath: '',
}) as import('@salesforce/b2c-tooling-sdk/scaffold').Scaffold,
resolveScaffoldParameters: async () => ({
variables: {},
errors: [],
missingParameters: [
{name: 'cartridgeName', required: true} as import('@salesforce/b2c-tooling-sdk/scaffold').ScaffoldParameter,
],
}),
});
expect(result.error).to.be.a('string');
expect(result.error).to.include('Missing required parameter');
expect(result.error).to.include('cartridgeName');
expect(result.files).to.deep.equal([]);
});
});
});