installedPluginManagement.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import type {
  5. ZCodePluginDiagnostic,
  6. ZCodePluginInfo,
  7. ZCodePluginUserConfigOption,
  8. } from "@zcode/shared";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. import { InstalledPluginDetailPanel } from "../src/settings/InstalledPluginManagement.js";
  11. describe("InstalledPluginDetailPanel", () => {
  12. it("renders plugin hook event, matcher, and command details", () => {
  13. const plugin: ZCodePluginInfo = {
  14. id: "ralph-loop@claude-plugins-official",
  15. name: "ralph-loop",
  16. enabled: true,
  17. source: "cache",
  18. marketplace: "claude-plugins-official",
  19. skillCount: 0,
  20. skillRootCount: 0,
  21. commandRootCount: 1,
  22. declaredMcpServerNames: [],
  23. mcpServerNames: [],
  24. hookDetails: [
  25. {
  26. event: "Stop",
  27. type: "command",
  28. command: 'bash "${CLAUDE_PLUGIN_ROOT}/hooks/stop-hook.sh"',
  29. sourcePath: "/cache/ralph-loop/hooks/hooks.json",
  30. runnable: true,
  31. },
  32. ],
  33. rootPath: "/cache/ralph-loop",
  34. };
  35. const html = renderToStaticMarkup(
  36. createElement(
  37. ZCodeIntlProvider,
  38. { initialLocale: "zh-CN" },
  39. createElement(InstalledPluginDetailPanel, {
  40. getPluginOptionValue: (
  41. _plugin: ZCodePluginInfo,
  42. _key: string,
  43. option: ZCodePluginUserConfigOption,
  44. ) => option.default ?? "",
  45. onSavePluginOptions: () => {},
  46. operationId: null,
  47. plugin,
  48. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  49. setPluginOptionDraft: () => {},
  50. }),
  51. ),
  52. );
  53. expect(html).toContain("Stop");
  54. expect(html).toContain("stop-hook.sh");
  55. expect(html).toContain("可运行");
  56. expect(html).not.toContain("暂未暴露 Hook 明细");
  57. });
  58. it("renders authoritative skill component names for both enabled and disabled plugins", () => {
  59. // 复现反馈中的 superpowers(14 个技能)。components 由 CLI 权威枚举随 list 下发,
  60. // 与启用态无关:启用/停用都应展示具体技能名,而不只是数量、也不应整组消失。
  61. const skillItems = Array.from({ length: 14 }, (_, index) => ({
  62. name: `superpowers-skill-${index + 1}`,
  63. ...(index === 0 ? { description: "Brainstorming ideas into designs" } : {}),
  64. }));
  65. for (const enabled of [true, false]) {
  66. const plugin: ZCodePluginInfo = {
  67. id: "superpowers@zcode-plugins-official",
  68. name: "superpowers",
  69. version: "5.1.0",
  70. enabled,
  71. source: "official",
  72. marketplace: "zcode-plugins-official",
  73. // 停用时 CLI 的 skillCount 为 0,但 components 仍完整。
  74. skillCount: enabled ? 14 : 0,
  75. skillRootCount: enabled ? 1 : 0,
  76. commandRootCount: 0,
  77. declaredMcpServerNames: [],
  78. mcpServerNames: [],
  79. hookDetails: [],
  80. rootPath: "/cache/superpowers",
  81. components: [{ kind: "skill", items: skillItems }],
  82. };
  83. const html = renderToStaticMarkup(
  84. createElement(
  85. ZCodeIntlProvider,
  86. { initialLocale: "zh-CN" },
  87. createElement(InstalledPluginDetailPanel, {
  88. getPluginOptionValue: (
  89. _plugin: ZCodePluginInfo,
  90. _key: string,
  91. option: ZCodePluginUserConfigOption,
  92. ) => option.default ?? "",
  93. onSavePluginOptions: () => {},
  94. operationId: null,
  95. plugin,
  96. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  97. setPluginOptionDraft: () => {},
  98. }),
  99. ),
  100. );
  101. // 数量徽标 + 首尾技能名都应可见。
  102. expect(html).toContain("14");
  103. expect(html).toContain("superpowers-skill-1");
  104. expect(html).toContain("superpowers-skill-14");
  105. expect(html).toContain("Brainstorming ideas into designs");
  106. // 不应再落到「启用后查看组件」的空态。
  107. expect(html).not.toContain("启用插件后查看其组件。");
  108. }
  109. });
  110. it("renders warning diagnostics scoped to the plugin and hides them when none match", () => {
  111. // Bugfix(静默失败)回归:声明的技能路径扫描为空等 warning 此前在 UI 无渲染位置。
  112. const plugin: ZCodePluginInfo = {
  113. id: "mattpocock-skills@claude-plugins-official",
  114. name: "mattpocock-skills",
  115. enabled: true,
  116. source: "cache",
  117. marketplace: "claude-plugins-official",
  118. skillCount: 0,
  119. skillRootCount: 25,
  120. commandRootCount: 0,
  121. declaredMcpServerNames: [],
  122. mcpServerNames: [],
  123. hookDetails: [],
  124. rootPath: "/cache/mattpocock-skills",
  125. components: [],
  126. };
  127. const renderPanel = (diagnostics: ZCodePluginDiagnostic[]) =>
  128. renderToStaticMarkup(
  129. createElement(
  130. ZCodeIntlProvider,
  131. { initialLocale: "zh-CN" },
  132. createElement(InstalledPluginDetailPanel, {
  133. diagnostics,
  134. getPluginOptionValue: (
  135. _plugin: ZCodePluginInfo,
  136. _key: string,
  137. option: ZCodePluginUserConfigOption,
  138. ) => option.default ?? "",
  139. onSavePluginOptions: () => {},
  140. operationId: null,
  141. plugin,
  142. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  143. setPluginOptionDraft: () => {},
  144. }),
  145. ),
  146. );
  147. // 只有当前插件自身的 warning 才展示;其他插件 / error 级别不进入列表。
  148. const withWarnings = renderPanel([
  149. {
  150. code: "plugin_skill_root_empty",
  151. message: "Plugin skills path does not contain any skills: ./skills/engineering/tdd",
  152. pluginId: "mattpocock-skills@claude-plugins-official",
  153. severity: "warning",
  154. },
  155. {
  156. code: "plugin_skill_root_empty",
  157. message: "Plugin skills path does not contain any skills: ./other",
  158. pluginId: "other-plugin@claude-plugins-official",
  159. severity: "warning",
  160. },
  161. {
  162. code: "plugin_manifest_invalid",
  163. message: "boom",
  164. pluginId: "mattpocock-skills@claude-plugins-official",
  165. severity: "error",
  166. },
  167. ]);
  168. expect(withWarnings).toContain("警告");
  169. expect(withWarnings).toContain("./skills/engineering/tdd");
  170. expect(withWarnings).not.toContain("./other");
  171. expect(withWarnings).not.toContain("boom");
  172. // 无匹配 warning 时不渲染空区块。
  173. expect(renderPanel([])).not.toContain("警告");
  174. });
  175. it("renders a details empty state for a plugin that declares no components", () => {
  176. // 真正无组件的插件给出诚实空态,不留白。
  177. const plugin: ZCodePluginInfo = {
  178. id: "empty-plugin@zcode-plugins-official",
  179. name: "empty-plugin",
  180. version: "0.1.0",
  181. enabled: false,
  182. source: "cache",
  183. marketplace: "zcode-plugins-official",
  184. skillCount: 0,
  185. skillRootCount: 0,
  186. commandRootCount: 0,
  187. declaredMcpServerNames: [],
  188. mcpServerNames: [],
  189. hookDetails: [],
  190. rootPath: "/cache/empty-plugin",
  191. components: [],
  192. };
  193. const html = renderToStaticMarkup(
  194. createElement(
  195. ZCodeIntlProvider,
  196. { initialLocale: "zh-CN" },
  197. createElement(InstalledPluginDetailPanel, {
  198. getPluginOptionValue: (
  199. _plugin: ZCodePluginInfo,
  200. _key: string,
  201. option: ZCodePluginUserConfigOption,
  202. ) => option.default ?? "",
  203. onSavePluginOptions: () => {},
  204. operationId: null,
  205. plugin,
  206. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  207. setPluginOptionDraft: () => {},
  208. }),
  209. ),
  210. );
  211. expect(html).toContain("详情");
  212. expect(html).toContain("无组件");
  213. });
  214. });