mcpSettings.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeMcpServer } from "@zcode/shared";
  3. import {
  4. serverToForm,
  5. formToConfig,
  6. formToJsonDraft,
  7. jsonDraftToForm,
  8. type FormState,
  9. } from "../src/settings/mcpSettingsShared.js";
  10. describe("MCP Settings Shared", () => {
  11. describe("serverToForm", () => {
  12. it("应正确转换 streamableHttp 类型", () => {
  13. const server: ZCodeMcpServer = {
  14. id: "test-id",
  15. name: "test-server",
  16. source: "zcodeagentmcp",
  17. scope: "user",
  18. enabled: true,
  19. config: {
  20. type: "streamableHttp",
  21. url: "https://example.com/mcp",
  22. },
  23. };
  24. const form = serverToForm(server);
  25. expect(form.type).toBe("streamableHttp");
  26. expect(form.url).toBe("https://example.com/mcp");
  27. expect(form.storageLevel).toBe("user");
  28. });
  29. it("应正确转换 workspace scope", () => {
  30. const server: ZCodeMcpServer = {
  31. id: "test-id",
  32. name: "test-server",
  33. source: "zcodeagentmcp",
  34. scope: "workspace",
  35. projectPath: "/workspace",
  36. enabled: true,
  37. config: {
  38. type: "http",
  39. url: "https://example.com/mcp",
  40. },
  41. };
  42. expect(serverToForm(server).storageLevel).toBe("workspace");
  43. });
  44. it("应正确转换 http 类型", () => {
  45. const server: ZCodeMcpServer = {
  46. id: "test-id",
  47. name: "test-server",
  48. source: "zcodeagentmcp",
  49. scope: "user",
  50. enabled: true,
  51. config: {
  52. type: "http",
  53. url: "https://example.com/mcp",
  54. },
  55. };
  56. const form = serverToForm(server);
  57. expect(form.type).toBe("http");
  58. expect(form.url).toBe("https://example.com/mcp");
  59. });
  60. it("应正确转换 sse 类型", () => {
  61. const server: ZCodeMcpServer = {
  62. id: "test-id",
  63. name: "test-server",
  64. source: "zcodeagentmcp",
  65. scope: "user",
  66. enabled: true,
  67. config: {
  68. type: "sse",
  69. url: "https://example.com/mcp/sse",
  70. },
  71. };
  72. const form = serverToForm(server);
  73. expect(form.type).toBe("sse");
  74. expect(form.url).toBe("https://example.com/mcp/sse");
  75. });
  76. it("应正确转换 stdio 类型", () => {
  77. const server: ZCodeMcpServer = {
  78. id: "test-id",
  79. name: "test-server",
  80. source: "zcodeagentmcp",
  81. scope: "user",
  82. enabled: true,
  83. config: {
  84. type: "stdio",
  85. command: "npx",
  86. args: ["-y", "@modelcontextprotocol/server-memory"],
  87. },
  88. };
  89. const form = serverToForm(server);
  90. expect(form.type).toBe("stdio");
  91. expect(form.command).toBe("npx");
  92. expect(form.args).toBe("-y @modelcontextprotocol/server-memory");
  93. });
  94. it("应保留 MCP timeoutMs", () => {
  95. const server: ZCodeMcpServer = {
  96. id: "test-id",
  97. name: "test-server",
  98. source: "zcodeagentmcp",
  99. scope: "user",
  100. enabled: true,
  101. config: {
  102. type: "stdio",
  103. command: "node",
  104. timeoutMs: 800,
  105. },
  106. };
  107. expect(serverToForm(server).timeoutMs).toBe("800");
  108. });
  109. });
  110. describe("formToConfig", () => {
  111. it("应正确转换 streamableHttp 表单", () => {
  112. const form: FormState = {
  113. name: "test-server",
  114. scope: "zcodeagentmcp",
  115. storageLevel: "user",
  116. type: "streamableHttp",
  117. command: "",
  118. args: "",
  119. env: "",
  120. url: "https://example.com/mcp",
  121. headers: '{"Authorization": "Bearer token"}',
  122. timeoutMs: "",
  123. };
  124. const config = formToConfig(form);
  125. expect(config.type).toBe("streamableHttp");
  126. expect(config.url).toBe("https://example.com/mcp");
  127. expect(config.headers).toEqual({ Authorization: "Bearer token" });
  128. });
  129. it("应正确转换 http 表单", () => {
  130. const form: FormState = {
  131. name: "test-server",
  132. scope: "zcodeagentmcp",
  133. storageLevel: "user",
  134. type: "http",
  135. command: "",
  136. args: "",
  137. env: "",
  138. url: "https://example.com/mcp",
  139. headers: "",
  140. timeoutMs: "",
  141. };
  142. const config = formToConfig(form);
  143. expect(config.type).toBe("http");
  144. expect(config.url).toBe("https://example.com/mcp");
  145. });
  146. it("应正确转换 sse 表单", () => {
  147. const form: FormState = {
  148. name: "test-server",
  149. scope: "zcodeagentmcp",
  150. storageLevel: "user",
  151. type: "sse",
  152. command: "",
  153. args: "",
  154. env: "",
  155. url: "https://example.com/mcp/sse",
  156. headers: "",
  157. timeoutMs: "",
  158. };
  159. const config = formToConfig(form);
  160. expect(config.type).toBe("sse");
  161. expect(config.url).toBe("https://example.com/mcp/sse");
  162. });
  163. it("应正确转换 stdio 表单", () => {
  164. const form: FormState = {
  165. name: "test-server",
  166. scope: "zcodeagentmcp",
  167. storageLevel: "user",
  168. type: "stdio",
  169. command: "npx",
  170. args: "-y @modelcontextprotocol/server-memory",
  171. env: '{"API_KEY": "test"}',
  172. url: "",
  173. headers: "",
  174. timeoutMs: "800",
  175. };
  176. const config = formToConfig(form);
  177. expect(config.type).toBe("stdio");
  178. expect(config.command).toBe("npx");
  179. expect(config.args).toEqual(["-y", "@modelcontextprotocol/server-memory"]);
  180. expect(config.env).toEqual({ API_KEY: "test" });
  181. expect(config.timeoutMs).toBe(800);
  182. });
  183. it("应忽略无效 timeoutMs", () => {
  184. const form: FormState = {
  185. name: "test-server",
  186. scope: "zcodeagentmcp",
  187. storageLevel: "user",
  188. type: "http",
  189. command: "",
  190. args: "",
  191. env: "",
  192. url: "https://example.com/mcp",
  193. headers: "",
  194. timeoutMs: "0",
  195. };
  196. expect(formToConfig(form)).not.toHaveProperty("timeoutMs");
  197. });
  198. });
  199. describe("JSON 编辑模式", () => {
  200. it("从 JSON 粘贴 MCP server 时应保留 timeoutMs", () => {
  201. const form = jsonDraftToForm(
  202. JSON.stringify({
  203. slow: {
  204. type: "stdio",
  205. command: "node",
  206. args: ["/tmp/slow-server.mjs"],
  207. timeoutMs: 800,
  208. },
  209. }),
  210. {
  211. name: "",
  212. scope: "zcodeagentmcp",
  213. storageLevel: "user",
  214. type: "stdio",
  215. command: "",
  216. args: "",
  217. env: "",
  218. url: "",
  219. headers: "",
  220. timeoutMs: "",
  221. },
  222. );
  223. expect(form.timeoutMs).toBe("800");
  224. expect(formToConfig(form)).toMatchObject({ timeoutMs: 800 });
  225. });
  226. it("从 JSON 粘贴 MCP server 时应保留 oauth", () => {
  227. const form = jsonDraftToForm(
  228. JSON.stringify({
  229. notion: {
  230. type: "http",
  231. url: "https://mcp.notion.com/mcp",
  232. oauth: {
  233. type: "authorization_code",
  234. clientName: "ZCode Notion MCP",
  235. },
  236. },
  237. }),
  238. {
  239. name: "",
  240. scope: "zcodeagentmcp",
  241. storageLevel: "user",
  242. type: "http",
  243. command: "",
  244. args: "",
  245. env: "",
  246. url: "",
  247. headers: "",
  248. timeoutMs: "",
  249. },
  250. );
  251. expect(form.oauth).toContain("authorization_code");
  252. expect(formToConfig(form)).toMatchObject({
  253. oauth: {
  254. type: "authorization_code",
  255. clientName: "ZCode Notion MCP",
  256. },
  257. });
  258. });
  259. it("全新 HTTP URL 配置不应自动写入 OAuth 草稿", () => {
  260. const draft = formToJsonDraft({
  261. name: "notion",
  262. scope: "zcodeagentmcp",
  263. storageLevel: "user",
  264. type: "http",
  265. command: "",
  266. args: "",
  267. env: "",
  268. url: "https://mcp.notion.com/mcp",
  269. headers: "",
  270. timeoutMs: "30000",
  271. });
  272. expect(JSON.parse(draft)).toMatchObject({
  273. notion: {
  274. type: "http",
  275. url: "https://mcp.notion.com/mcp",
  276. timeoutMs: 30000,
  277. },
  278. });
  279. expect(JSON.parse(draft).notion.oauth).toBeUndefined();
  280. });
  281. it("从表单生成 JSON 草稿时应写出 timeoutMs", () => {
  282. const draft = formToJsonDraft({
  283. name: "slow",
  284. scope: "zcodeagentmcp",
  285. storageLevel: "user",
  286. type: "stdio",
  287. command: "node",
  288. args: "/tmp/slow-server.mjs",
  289. env: "",
  290. url: "",
  291. headers: "",
  292. timeoutMs: "800",
  293. });
  294. expect(JSON.parse(draft)).toMatchObject({
  295. slow: {
  296. timeoutMs: 800,
  297. },
  298. });
  299. });
  300. });
  301. });