mcpSettings.test.ts 9.5 KB

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