-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathgateway.test.ts
More file actions
402 lines (365 loc) · 12.3 KB
/
gateway.test.ts
File metadata and controls
402 lines (365 loc) · 12.3 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
import { beforeEach, describe, expect, it, vi } from "vitest";
import { registerGatewayTools } from "./gateway.js";
import type { ExtendedMcpServer } from "../server.js";
const {
mockGetAccessList,
mockGetDomainList,
mockCreateAccess,
mockDescribeHttpServiceRoute,
mockCreateHttpServiceRoute,
mockModifyHttpServiceRoute,
mockDeleteHttpServiceRoute,
mockBindCustomDomain,
mockDeleteCustomDomain,
mockGetCloudBaseManager,
mockLogCloudBaseResult,
} = vi.hoisted(() => ({
mockGetAccessList: vi.fn(),
mockGetDomainList: vi.fn(),
mockCreateAccess: vi.fn(),
mockDescribeHttpServiceRoute: vi.fn(),
mockCreateHttpServiceRoute: vi.fn(),
mockModifyHttpServiceRoute: vi.fn(),
mockDeleteHttpServiceRoute: vi.fn(),
mockBindCustomDomain: vi.fn(),
mockDeleteCustomDomain: vi.fn(),
mockGetCloudBaseManager: vi.fn(),
mockLogCloudBaseResult: vi.fn(),
}));
vi.mock("../cloudbase-manager.js", () => ({
getCloudBaseManager: mockGetCloudBaseManager,
logCloudBaseResult: mockLogCloudBaseResult,
}));
function createMockServer() {
const tools: Record<
string,
{
meta: any;
handler: (args: any) => Promise<any>;
}
> = {};
const server: ExtendedMcpServer = {
cloudBaseOptions: { envId: "env-test", region: "ap-guangzhou" },
logger: vi.fn(),
registerTool: vi.fn(
(name: string, meta: any, handler: (args: any) => Promise<any>) => {
tools[name] = { meta, handler };
},
),
} as unknown as ExtendedMcpServer;
registerGatewayTools(server);
return {
server,
tools,
};
}
describe("gateway tools", () => {
let tools: ReturnType<typeof createMockServer>["tools"];
beforeEach(() => {
vi.clearAllMocks();
mockGetAccessList.mockResolvedValue({
Total: 1,
EnableService: true,
APISet: [
{
Path: "api/hello",
},
],
});
mockGetDomainList.mockResolvedValue({
DefaultDomain: "env-test.tcbaccess-in.tencentcloudbase.com",
EnableService: true,
ServiceSet: [
{
Domain: "env-test.app.tcloudbase.com",
},
],
});
mockCreateAccess.mockResolvedValue({
RequestId: "req-create-access",
APIId: "api-123",
});
mockDescribeHttpServiceRoute.mockResolvedValue({
OriginDomain: "env-test.service.tcloudbase.com",
TotalCount: 1,
Domains: [
{
Domain: "env-test.service.tcloudbase.com",
Routes: [
{
RouteId: "route-1",
Path: "/api/hello",
UpstreamResourceType: "SCF",
UpstreamResourceName: "helloFn",
},
],
},
],
RequestId: "req-route-list",
});
mockCreateHttpServiceRoute.mockResolvedValue({
RouteId: "route-2",
RequestId: "req-route-create",
});
mockModifyHttpServiceRoute.mockResolvedValue({
RequestId: "req-route-update",
});
mockDeleteHttpServiceRoute.mockResolvedValue({
RequestId: "req-route-delete",
});
mockBindCustomDomain.mockResolvedValue({
RequestId: "req-domain-bind",
});
mockDeleteCustomDomain.mockResolvedValue({
RequestId: "req-domain-delete",
});
mockGetCloudBaseManager.mockResolvedValue({
access: {
getAccessList: mockGetAccessList,
getDomainList: mockGetDomainList,
createAccess: mockCreateAccess,
},
env: {
describeHttpServiceRoute: mockDescribeHttpServiceRoute,
createHttpServiceRoute: mockCreateHttpServiceRoute,
modifyHttpServiceRoute: mockModifyHttpServiceRoute,
deleteHttpServiceRoute: mockDeleteHttpServiceRoute,
bindCustomDomain: mockBindCustomDomain,
deleteCustomDomain: mockDeleteCustomDomain,
},
});
({ tools } = createMockServer());
});
it("manageGateway schema should explain how to expose an existing HTTP function", () => {
const schema = tools.manageGateway.meta.inputSchema;
expect(tools.manageGateway.meta.description).toContain("HTTP 云函数补默认域名访问");
expect(schema.action.description).toContain("createAccess");
expect(schema.action.description).toContain("默认域名访问入口");
expect(schema.targetName.description).toContain("填写函数名");
expect(schema.path.description).toContain("/api/hello");
expect(schema.type.description).toContain("已创建的 HTTP 云函数时传 HTTP");
expect(schema.auth.description).toContain("通常设为 false");
});
it("manageGateway(action=createAccess) should normalize path and return structured payload", async () => {
const result = await tools.manageGateway.handler({
action: "createAccess",
targetType: "function",
targetName: "helloFn",
path: "api/hello",
type: "HTTP",
auth: false,
});
const payload = JSON.parse(result.content[0].text);
expect(mockCreateAccess).toHaveBeenCalledWith({
name: "helloFn",
path: "/api/hello",
type: 6,
auth: false,
});
expect(payload).toMatchObject({
success: true,
message:
"已为目标 helloFn 创建网关访问路径。可访问 URL: https://env-test.app.tcloudbase.com/api/hello。注意:路由配置传播通常需要等待 30 秒到 3 分钟,请勿立即访问。该操作只创建网关入口,不会自动放开函数安全规则;若需要匿名或浏览器直接访问,请继续检查函数资源权限。",
data: {
action: "createAccess",
targetType: "function",
targetName: "helloFn",
path: "/api/hello",
urls: ["https://env-test.app.tcloudbase.com/api/hello"],
primaryUrl: "https://env-test.app.tcloudbase.com/api/hello",
domains: ["env-test.app.tcloudbase.com"],
},
nextActions: [
expect.objectContaining({
tool: "queryGateway",
action: "getAccess",
reason: "等待 30 秒到 3 分钟后再确认访问入口是否已生效",
}),
expect.objectContaining({
tool: "queryPermissions",
action: "getResourcePermission",
reason:
"确认函数安全规则是否允许预期访问方;网关 auth=false 不等于函数已允许匿名访问",
}),
expect.objectContaining({
tool: "managePermissions",
action: "updateResourcePermission",
reason: "只有在确认需要匿名或浏览器直连访问时,才按实际安全要求更新函数权限",
}),
],
});
});
it("manageGateway(action=createAccess) should default path to targetName when omitted", async () => {
const result = await tools.manageGateway.handler({
action: "createAccess",
targetType: "function",
targetName: "helloFn",
type: "Event",
});
const payload = JSON.parse(result.content[0].text);
expect(mockCreateAccess).toHaveBeenCalledWith({
name: "helloFn",
path: "/helloFn",
type: 1,
auth: undefined,
});
expect(payload).toMatchObject({
success: true,
data: {
action: "createAccess",
targetType: "function",
targetName: "helloFn",
path: "/helloFn",
urls: ["https://env-test.app.tcloudbase.com/helloFn"],
primaryUrl: "https://env-test.app.tcloudbase.com/helloFn",
},
});
});
it("manageGateway metadata should warn that createAccess requires explicit type", () => {
expect(tools.manageGateway.meta.description).toContain("createAccess");
expect(tools.manageGateway.meta.description).toContain("type");
expect(tools.manageGateway.meta.inputSchema.action.description).toContain("显式");
expect(tools.manageGateway.meta.inputSchema.type.description).toContain("createAccess");
expect(tools.manageGateway.meta.inputSchema.type.description).toContain("省略会默认按 Event 路由处理");
});
it("manageGateway(action=createAccess) should reject missing type with a clear message", async () => {
const result = await tools.manageGateway.handler({
action: "createAccess",
targetType: "function",
targetName: "helloFn",
});
const payload = JSON.parse(result.content[0].text);
expect(mockCreateAccess).not.toHaveBeenCalled();
expect(payload).toMatchObject({
success: false,
message: expect.stringContaining("必须显式提供 type"),
});
expect(payload.message).toContain("FUNCTION_PARAM_INVALID");
});
it("queryGateway(action=getAccess) should aggregate access urls from domains", async () => {
const result = await tools.queryGateway.handler({
action: "getAccess",
targetType: "function",
targetName: "helloFn",
});
const payload = JSON.parse(result.content[0].text);
expect(mockGetAccessList).toHaveBeenCalledWith({ name: "helloFn" });
expect(payload).toMatchObject({
success: true,
data: {
action: "getAccess",
targetType: "function",
targetName: "helloFn",
total: 1,
domains: ["env-test.app.tcloudbase.com"],
allDomains: ["env-test.tcbaccess-in.tencentcloudbase.com", "env-test.app.tcloudbase.com"],
urls: [
"https://env-test.app.tcloudbase.com/api/hello",
],
enableService: true,
},
nextActions: [
expect.objectContaining({
tool: "manageGateway",
action: "createAccess",
}),
],
});
});
it("queryGateway(action=getAccess) should exclude internal domains from urls", async () => {
// Verify that internal domains (tencentcloudbase.com) are filtered out of urls
// but still available in allDomains
const result = await tools.queryGateway.handler({
action: "getAccess",
targetType: "function",
targetName: "helloFn",
});
const payload = JSON.parse(result.content[0].text);
// urls should NOT contain the internal domain
expect(payload.data.urls).not.toContain(
"https://env-test.tcbaccess-in.tencentcloudbase.com/api/hello",
);
// domains should NOT contain the internal domain
expect(payload.data.domains).not.toContain(
"env-test.tcbaccess-in.tencentcloudbase.com",
);
// allDomains should still contain both
expect(payload.data.allDomains).toContain(
"env-test.tcbaccess-in.tencentcloudbase.com",
);
expect(payload.data.allDomains).toContain(
"env-test.app.tcloudbase.com",
);
});
it("queryGateway(action=listRoutes) should list gateway routes", async () => {
const result = await tools.queryGateway.handler({
action: "listRoutes",
});
const payload = JSON.parse(result.content[0].text);
expect(mockDescribeHttpServiceRoute).toHaveBeenCalledWith({
EnvId: "env-test",
});
expect(payload).toMatchObject({
success: true,
data: {
action: "listRoutes",
routes: [expect.objectContaining({ RouteId: "route-1" })],
total: 1,
},
});
});
it("manageGateway(action=createRoute) should create http route", async () => {
const result = await tools.manageGateway.handler({
action: "createRoute",
domain: "env-test.service.tcloudbase.com",
route: {
path: "/api/hello",
serviceType: "function",
serviceName: "helloFn",
},
});
const payload = JSON.parse(result.content[0].text);
expect(mockCreateHttpServiceRoute).toHaveBeenCalledWith({
EnvId: "env-test",
Domain: {
Domain: "env-test.service.tcloudbase.com",
Routes: [
{
Path: "/api/hello",
UpstreamResourceType: "SCF",
UpstreamResourceName: "helloFn",
EnableAuth: undefined,
},
],
},
});
expect(payload).toMatchObject({
success: true,
data: {
action: "createRoute",
},
});
});
it("manageGateway(action=bindCustomDomain) should bind custom domain", async () => {
const result = await tools.manageGateway.handler({
action: "bindCustomDomain",
domain: "api.example.com",
certificateId: "cert-1",
});
const payload = JSON.parse(result.content[0].text);
expect(mockBindCustomDomain).toHaveBeenCalledWith({
EnvId: "env-test",
Domain: {
Domain: "api.example.com",
CertId: "cert-1",
},
});
expect(payload).toMatchObject({
success: true,
data: {
action: "bindCustomDomain",
domain: "api.example.com",
},
});
});
});