-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathfunctions.test.ts
More file actions
235 lines (203 loc) · 7.23 KB
/
functions.test.ts
File metadata and controls
235 lines (203 loc) · 7.23 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
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
buildFunctionOperationErrorMessage,
buildFunctionQueryErrorMessage,
DEFAULT_RUNTIME,
registerFunctionTools,
resolveEventFunctionRuntime,
shouldInstallDependencyForFunction,
} from "./functions.js";
import type { ExtendedMcpServer } from "../server.js";
const {
mockCreateFunction,
mockCreateAccess,
mockGetCloudBaseManager,
mockLogCloudBaseResult,
mockIsCloudMode,
} = vi.hoisted(() => ({
mockCreateFunction: vi.fn(),
mockCreateAccess: vi.fn(),
mockGetCloudBaseManager: vi.fn(),
mockLogCloudBaseResult: vi.fn(),
mockIsCloudMode: vi.fn(),
}));
vi.mock("../cloudbase-manager.js", () => ({
getCloudBaseManager: mockGetCloudBaseManager,
logCloudBaseResult: mockLogCloudBaseResult,
}));
vi.mock("../utils/cloud-mode.js", () => ({
isCloudMode: mockIsCloudMode,
}));
vi.mock("../utils/logger.js", () => ({
debug: vi.fn(),
}));
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;
registerFunctionTools(server);
return { tools };
}
describe("functions tool helpers", () => {
let tools: ReturnType<typeof createMockServer>["tools"];
beforeEach(() => {
vi.clearAllMocks();
mockIsCloudMode.mockReturnValue(false);
mockCreateFunction.mockResolvedValue({
RequestId: "req-create-function",
FunctionName: "httpDemo",
});
mockCreateAccess.mockResolvedValue({
RequestId: "req-create-access",
});
mockGetCloudBaseManager.mockResolvedValue({
functions: {
createFunction: mockCreateFunction,
},
access: {
createAccess: mockCreateAccess,
},
});
({ tools } = createMockServer());
});
it("keeps HTTP functions from forcing dependency install when package.json is absent", () => {
expect(shouldInstallDependencyForFunction("HTTP", false)).toBe(false);
expect(shouldInstallDependencyForFunction("HTTP", true)).toBe(true);
});
it("returns a clearer HTTP path hint for undefined paths[0] failures", () => {
const message = buildFunctionOperationErrorMessage(
"createFunction",
"httpDemo",
"/tmp/project/cloudfunctions",
new Error('[createFunction] The "paths[0]" argument must be of type string. Received undefined'),
);
expect(message).toContain("functionRootPath");
expect(message).toContain("zipFile");
});
it("adds dependency-install guidance for HTTP function failures", () => {
const message = buildFunctionOperationErrorMessage(
"createFunction",
"httpDemo",
"/tmp/project/cloudfunctions",
new Error("[httpDemo] 函数代码更新失败:云函数创建失败\n状态描述: 依赖安装失败"),
);
expect(message).toContain("原生 Node.js API");
expect(message).toContain("package.json");
});
it("warns when functionRootPath looks like project root on path-not-found errors", () => {
const message = buildFunctionOperationErrorMessage(
"createFunction",
"hello",
"/home/user/my-project",
new Error("路径不存在"),
);
expect(message).toContain("functionRootPath");
expect(message).toContain("cloudfunctions");
expect(message).toContain("functions");
expect(message).toContain("/home/user/my-project/cloudfunctions");
expect(message).toContain("/home/user/my-project/functions");
});
it("does not warn about project root when functionRootPath already ends with cloudfunctions", () => {
const message = buildFunctionOperationErrorMessage(
"createFunction",
"hello",
"/home/user/my-project/cloudfunctions",
new Error("路径不存在"),
);
expect(message).not.toContain("functionRootPath 应该是直接包含函数文件夹的目录");
});
it("normalizes supported Event runtimes with whitespace", () => {
expect(resolveEventFunctionRuntime("Python 3.9")).toBe("Python3.9");
expect(resolveEventFunctionRuntime("Php 7.4")).toBe("Php7.4");
});
it("falls back to the default runtime when Event runtime is omitted", () => {
expect(resolveEventFunctionRuntime(undefined)).toBe(DEFAULT_RUNTIME);
});
it("rejects unsupported Event runtimes with a helpful message", () => {
expect(() => resolveEventFunctionRuntime("Ruby3.2")).toThrow(/不支持的运行时环境/);
expect(() => resolveEventFunctionRuntime("Ruby3.2")).toThrow(/Python3.9/);
});
it("provides specific parameter guidance for invalid parameter errors", () => {
const message = buildFunctionQueryErrorMessage(
"getFunctionDetail",
{ action: "getFunctionDetail" },
new Error("400 invalid parameter value"),
);
expect(message).toContain("getFunctionDetail");
expect(message).toContain("functionName");
expect(message).toContain("必填");
});
it("shows current input parameters when reporting errors", () => {
const message = buildFunctionQueryErrorMessage(
"getFunctionDetail",
{ action: "getFunctionDetail", functionName: "testFunc" },
new Error("invalid parameter"),
);
expect(message).toContain("testFunc");
expect(message).toContain("当前传入的参数");
});
it("suggests listFunctions when function is not found", () => {
const message = buildFunctionQueryErrorMessage(
"getFunctionDetail",
{ action: "getFunctionDetail", functionName: "nonExistentFunc" },
new Error("Function not found"),
);
expect(message).toContain("listFunctions");
expect(message).toContain("nonExistentFunc");
});
it("guides HTTP functions through anonymous-access follow-up without auto-creating gateway access", async () => {
const result = await tools.manageFunctions.handler({
action: "createFunction",
func: {
name: "httpDemo",
type: "HTTP",
runtime: "Nodejs18.15",
},
functionRootPath: "/tmp/cloudfunctions",
});
const payload = JSON.parse(result.content[0].text);
expect(mockCreateFunction).toHaveBeenCalledWith({
func: expect.objectContaining({
name: "httpDemo",
type: "HTTP",
installDependency: false,
}),
functionRootPath: "/tmp/cloudfunctions",
force: false,
});
expect(mockCreateAccess).not.toHaveBeenCalled();
expect(payload.message).toContain("manageGateway(action=\"createAccess\")");
expect(payload.message).toContain("type=\"HTTP\"");
expect(payload.message).toContain("匿名身份访问");
expect(payload.message).toContain("EXCEED_AUTHORITY");
expect(payload.nextActions).toEqual(
expect.arrayContaining([
expect.objectContaining({
tool: "manageGateway",
action: "createAccess",
}),
expect.objectContaining({
tool: "queryPermissions",
action: "getResourcePermission",
}),
expect.objectContaining({
tool: "managePermissions",
action: "updateResourcePermission",
}),
]),
);
});
});