pluginManagedResourceGroups.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import { describe, expect, it } from "vitest";
  2. import type {
  3. SkillSummary,
  4. ZCodeCommand,
  5. ZCodeMcpServer,
  6. ZCodePluginComponentGroup,
  7. ZCodePluginInfo,
  8. } from "@zcode/shared";
  9. import {
  10. buildInstalledPluginDisplayGroups,
  11. buildPluginMcpServerItems,
  12. filterLocalMcpServers,
  13. groupCommandsByPlugin,
  14. groupPluginMcpServersByPlugin,
  15. groupSkillsByPlugin,
  16. } from "@/settings/pluginManagedResourceGroups.js";
  17. describe("pluginManagedResourceGroups", () => {
  18. it("groups MCP servers by stable plugin id and keeps attention rows first", () => {
  19. const groups = groupPluginMcpServersByPlugin([
  20. {
  21. active: true,
  22. id: "plugin-a:healthy",
  23. name: "healthy",
  24. pluginEnabled: true,
  25. pluginId: "plugin-a@market-a",
  26. pluginMarketplace: "market-a",
  27. pluginName: "same-name",
  28. runtimeServerName: "plugin:same-name:healthy",
  29. status: "connected",
  30. },
  31. {
  32. active: true,
  33. error: "failed",
  34. id: "plugin-a:failed",
  35. name: "failed",
  36. pluginEnabled: true,
  37. pluginId: "plugin-a@market-a",
  38. pluginMarketplace: "market-a",
  39. pluginName: "same-name",
  40. runtimeServerName: "plugin:same-name:failed",
  41. status: "error",
  42. },
  43. {
  44. active: true,
  45. id: "plugin-b:other",
  46. name: "other",
  47. pluginEnabled: true,
  48. pluginId: "plugin-b@market-b",
  49. pluginMarketplace: "market-b",
  50. pluginName: "same-name",
  51. runtimeServerName: "plugin:same-name:other",
  52. },
  53. ]);
  54. expect(groups).toHaveLength(2);
  55. expect(groups.map((group) => group.pluginId)).toEqual([
  56. "plugin-a@market-a",
  57. "plugin-b@market-b",
  58. ]);
  59. expect(groups[0]?.items.map((item) => item.name)).toEqual(["failed", "healthy"]);
  60. });
  61. it("groups skills by plugin scope", () => {
  62. const skills: SkillSummary[] = [
  63. {
  64. body: "",
  65. description: "local",
  66. enabled: true,
  67. id: "local",
  68. name: "local-review",
  69. path: "/workspace/.zcode/skills/local-review/SKILL.md",
  70. scope: "workspace",
  71. },
  72. {
  73. body: "",
  74. description: "plugin",
  75. enabled: true,
  76. id: "plugin",
  77. name: "ios-dev",
  78. path: "/home/.zcode/cli/plugins/cache/zcode-plugins-official/ios/0.1.0/skills/ios-dev/SKILL.md",
  79. pluginName: "ios-simulator",
  80. scope: "plugin",
  81. },
  82. ];
  83. const grouped = groupSkillsByPlugin(skills, "IOS");
  84. expect(grouped.local).toEqual([]);
  85. expect(grouped.plugin.map((skill) => skill.name)).toEqual(["ios-dev"]);
  86. });
  87. it("groups user and plugin commands separately", () => {
  88. const commands: ZCodeCommand[] = [
  89. {
  90. agentSource: "zcodeAgent",
  91. content: "local",
  92. description: "local command",
  93. enabled: true,
  94. filePath: "/home/.zcode/commands/review.md",
  95. id: "zcodeAgent:zcode:global:/review",
  96. location: {
  97. directoryPath: "/home/.zcode/commands",
  98. scope: "user",
  99. source: "zcode",
  100. },
  101. name: "/review",
  102. prompt: "local",
  103. scope: "global",
  104. source: "user",
  105. },
  106. {
  107. content: "plugin",
  108. description: "plugin command",
  109. enabled: true,
  110. filePath: "/home/.zcode/cli/plugins/cache/plugin/commands/ios-dev.md",
  111. id: "plugin:zcode-plugins-official:ios-simulator:/ios-dev",
  112. name: "/ios-dev",
  113. pluginEnabled: true,
  114. pluginMarketplace: "zcode-plugins-official",
  115. pluginName: "ios-simulator",
  116. prompt: "plugin",
  117. scope: "global",
  118. source: "plugin",
  119. },
  120. ];
  121. const grouped = groupCommandsByPlugin(commands, "");
  122. expect(grouped.local.map((command) => command.name)).toEqual(["/review"]);
  123. expect(grouped.plugin.map((command) => command.pluginName)).toEqual(["ios-simulator"]);
  124. });
  125. it("builds plugin MCP items from enabled plugin metadata", () => {
  126. const servers: ZCodeMcpServer[] = [
  127. {
  128. config: { command: "npx", type: "stdio" },
  129. enabled: true,
  130. id: "zcodeagentmcp-global-memory",
  131. name: "memory",
  132. scope: "user",
  133. source: "zcodeagentmcp",
  134. },
  135. {
  136. config: { command: "npx", type: "stdio" },
  137. enabled: true,
  138. id: "codex-global-other",
  139. name: "other",
  140. scope: "user",
  141. source: "codexclimcp",
  142. },
  143. ];
  144. const plugins: ZCodePluginInfo[] = [
  145. {
  146. commandRootCount: 0,
  147. declaredMcpServerNames: ["ios-simulator", "ios-helper"],
  148. enabled: false,
  149. id: "ios-simulator@zcode-plugins-official",
  150. marketplace: "zcode-plugins-official",
  151. mcpServerNames: [],
  152. name: "ios-simulator",
  153. rootPath: "/plugins/ios-simulator",
  154. skillRootCount: 0,
  155. source: "official",
  156. },
  157. ];
  158. expect(filterLocalMcpServers(servers, "MEMORY").map((server) => server.name)).toEqual([
  159. "memory",
  160. ]);
  161. expect(buildPluginMcpServerItems(plugins, "IoS")).toEqual([
  162. {
  163. active: false,
  164. id: "ios-simulator@zcode-plugins-official:ios-simulator",
  165. name: "ios-simulator",
  166. pluginEnabled: false,
  167. pluginId: "ios-simulator@zcode-plugins-official",
  168. pluginMarketplace: "zcode-plugins-official",
  169. pluginName: "ios-simulator",
  170. runtimeServerName: "plugin:ios-simulator:ios-simulator",
  171. },
  172. {
  173. active: false,
  174. id: "ios-simulator@zcode-plugins-official:ios-helper",
  175. name: "ios-helper",
  176. pluginEnabled: false,
  177. pluginId: "ios-simulator@zcode-plugins-official",
  178. pluginMarketplace: "zcode-plugins-official",
  179. pluginName: "ios-simulator",
  180. runtimeServerName: "plugin:ios-simulator:ios-helper",
  181. },
  182. ]);
  183. });
  184. it("marks plugin MCP items active when runtime server names are namespaced", () => {
  185. const plugins: ZCodePluginInfo[] = [
  186. {
  187. commandRootCount: 0,
  188. declaredMcpServerNames: ["chrome-devtools"],
  189. enabled: true,
  190. id: "chrome-devtools-mcp@claude-plugins-official",
  191. marketplace: "claude-plugins-official",
  192. mcpServerNames: ["plugin:chrome-devtools-mcp:chrome-devtools"],
  193. name: "chrome-devtools-mcp",
  194. rootPath: "/plugins/chrome-devtools-mcp",
  195. skillRootCount: 0,
  196. source: "cache",
  197. },
  198. ];
  199. expect(buildPluginMcpServerItems(plugins, "chrome")).toEqual([
  200. {
  201. active: true,
  202. id: "chrome-devtools-mcp@claude-plugins-official:chrome-devtools",
  203. name: "chrome-devtools",
  204. pluginEnabled: true,
  205. pluginId: "chrome-devtools-mcp@claude-plugins-official",
  206. pluginMarketplace: "claude-plugins-official",
  207. pluginName: "chrome-devtools-mcp",
  208. runtimeServerName: "plugin:chrome-devtools-mcp:chrome-devtools",
  209. },
  210. ]);
  211. });
  212. it("maps Browser host MCP metadata to the unprefixed node_repl runtime", () => {
  213. const plugins: ZCodePluginInfo[] = [
  214. {
  215. commandRootCount: 0,
  216. declaredMcpServerNames: [],
  217. enabled: false,
  218. hostMcpServerNames: ["node_repl"],
  219. id: "browser-use@zcode-plugins-official",
  220. marketplace: "zcode-plugins-official",
  221. mcpServerNames: [],
  222. name: "browser-use",
  223. rootPath: "/plugins/browser-use",
  224. skillRootCount: 1,
  225. source: "official",
  226. },
  227. ];
  228. expect(
  229. buildPluginMcpServerItems(plugins, "node", {
  230. node_repl: {
  231. status: "connected",
  232. toolCount: 3,
  233. transport: "stdio",
  234. updatedAt: "2026-07-13T00:00:00.000Z",
  235. },
  236. }),
  237. ).toEqual([
  238. {
  239. active: true,
  240. authorization: undefined,
  241. error: undefined,
  242. hostProvided: true,
  243. id: "browser-use@zcode-plugins-official:node_repl",
  244. name: "node_repl",
  245. pluginEnabled: false,
  246. pluginId: "browser-use@zcode-plugins-official",
  247. pluginMarketplace: "zcode-plugins-official",
  248. pluginName: "browser-use",
  249. runtimeServerName: "node_repl",
  250. status: "connected",
  251. toolCount: 3,
  252. },
  253. ]);
  254. });
  255. it("attaches plugin MCP OAuth authorization from runtime status snapshots", () => {
  256. const plugins: ZCodePluginInfo[] = [
  257. {
  258. commandRootCount: 0,
  259. declaredMcpServerNames: ["canva"],
  260. enabled: true,
  261. id: "canva@zcode-plugins-official",
  262. marketplace: "zcode-plugins-official",
  263. mcpServerNames: ["plugin:canva:canva"],
  264. name: "canva",
  265. rootPath: "/plugins/canva",
  266. skillRootCount: 0,
  267. source: "official",
  268. },
  269. ];
  270. expect(
  271. buildPluginMcpServerItems(plugins, "canva", {
  272. "plugin:canva:canva": {
  273. authorization: {
  274. authorizationUrl: "https://auth.example.test/authorize?state=plugin",
  275. startedAt: "2026-07-09T00:00:00.000Z",
  276. type: "oauth_authorization_code",
  277. },
  278. status: "connecting",
  279. toolCount: 0,
  280. transport: "http",
  281. updatedAt: "2026-07-09T00:00:00.000Z",
  282. },
  283. }),
  284. ).toEqual([
  285. {
  286. active: true,
  287. authorization: {
  288. authorizationUrl: "https://auth.example.test/authorize?state=plugin",
  289. startedAt: "2026-07-09T00:00:00.000Z",
  290. type: "oauth_authorization_code",
  291. },
  292. error: undefined,
  293. id: "canva@zcode-plugins-official:canva",
  294. name: "canva",
  295. pluginEnabled: true,
  296. pluginId: "canva@zcode-plugins-official",
  297. pluginMarketplace: "zcode-plugins-official",
  298. pluginName: "canva",
  299. runtimeServerName: "plugin:canva:canva",
  300. status: "connecting",
  301. toolCount: 0,
  302. },
  303. ]);
  304. });
  305. it("ignores stale plugin MCP runtime authorization when plugin is disabled", () => {
  306. const plugins: ZCodePluginInfo[] = [
  307. {
  308. commandRootCount: 0,
  309. declaredMcpServerNames: ["canva"],
  310. enabled: false,
  311. id: "canva@zcode-plugins-official",
  312. marketplace: "zcode-plugins-official",
  313. mcpServerNames: ["plugin:canva:canva"],
  314. name: "canva",
  315. rootPath: "/plugins/canva",
  316. skillRootCount: 0,
  317. source: "official",
  318. },
  319. ];
  320. expect(
  321. buildPluginMcpServerItems(plugins, "canva", {
  322. "plugin:canva:canva": {
  323. authorization: {
  324. authorizationUrl: "https://auth.example.test/authorize?state=stale",
  325. startedAt: "2026-07-09T00:00:00.000Z",
  326. type: "oauth_authorization_code",
  327. },
  328. status: "connecting",
  329. toolCount: 0,
  330. transport: "http",
  331. updatedAt: "2026-07-09T00:00:00.000Z",
  332. },
  333. }),
  334. ).toEqual([
  335. {
  336. active: true,
  337. id: "canva@zcode-plugins-official:canva",
  338. name: "canva",
  339. pluginEnabled: false,
  340. pluginId: "canva@zcode-plugins-official",
  341. pluginMarketplace: "zcode-plugins-official",
  342. pluginName: "canva",
  343. runtimeServerName: "plugin:canva:canva",
  344. },
  345. ]);
  346. });
  347. describe("buildInstalledPluginDisplayGroups", () => {
  348. function makeComponents(): ZCodePluginComponentGroup[] {
  349. return [
  350. {
  351. kind: "skill",
  352. items: [{ name: "brainstorming", description: "Plan first" }, { name: "writing-plans" }],
  353. },
  354. { kind: "hook", items: [{ name: "SessionStart" }] },
  355. ];
  356. }
  357. function makePlugin(overrides: Partial<ZCodePluginInfo> = {}): ZCodePluginInfo {
  358. return {
  359. commandRootCount: 0,
  360. enabled: true,
  361. id: "superpowers@zcode-plugins-official",
  362. marketplace: "zcode-plugins-official",
  363. mcpServerNames: [],
  364. name: "superpowers",
  365. rootPath: "/cache/superpowers",
  366. skillCount: 2,
  367. skillRootCount: 1,
  368. source: "official",
  369. ...overrides,
  370. };
  371. }
  372. it("maps authoritative components to display groups with names and descriptions", () => {
  373. const groups = buildInstalledPluginDisplayGroups(
  374. makePlugin({ components: makeComponents() }),
  375. );
  376. // 固定顺序:skill 在 hook 之前(COMPONENT_KIND_ORDER)。
  377. expect(groups.map((group) => group.kind)).toEqual(["skill", "hook"]);
  378. const skillGroup = groups.find((group) => group.kind === "skill");
  379. // 数量取 items.length,与名称一致(修复「有数量没有名称」)。
  380. expect(skillGroup?.count).toBe(2);
  381. expect(skillGroup?.items.map((item) => item.name)).toEqual([
  382. "brainstorming",
  383. "writing-plans",
  384. ]);
  385. // 描述随名称一并展示(修复旧实现 names.map 丢描述)。
  386. expect(skillGroup?.items[0]?.description).toBe("Plan first");
  387. expect(skillGroup?.items[1]?.description).toBeUndefined();
  388. });
  389. it("renders components even when the plugin is disabled", () => {
  390. // 停用插件的 components 依然由 CLI 权威枚举下发,详情弹窗不再整组消失。
  391. const groups = buildInstalledPluginDisplayGroups(
  392. makePlugin({ enabled: false, skillCount: 0, components: makeComponents() }),
  393. );
  394. const skillGroup = groups.find((group) => group.kind === "skill");
  395. expect(skillGroup?.items.map((item) => item.name)).toEqual([
  396. "brainstorming",
  397. "writing-plans",
  398. ]);
  399. });
  400. it("omits empty groups and returns nothing when components are absent", () => {
  401. expect(buildInstalledPluginDisplayGroups(makePlugin({ components: [] }))).toEqual([]);
  402. // 旧 payload 没有 components 字段时安全降级为空。
  403. expect(buildInstalledPluginDisplayGroups(makePlugin())).toEqual([]);
  404. expect(
  405. buildInstalledPluginDisplayGroups(
  406. makePlugin({ components: [{ kind: "skill", items: [] }] }),
  407. ),
  408. ).toEqual([]);
  409. });
  410. });
  411. });