installedPluginManagement.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 authoritative skill component names for both enabled and disabled plugins", () => {
  13. // 复现反馈中的 superpowers(14 个技能)。components 由 CLI 权威枚举随 list 下发,
  14. // 与启用态无关:启用/停用都应展示具体技能名,而不只是数量、也不应整组消失。
  15. const skillItems = Array.from({ length: 14 }, (_, index) => ({
  16. name: `superpowers-skill-${index + 1}`,
  17. ...(index === 0 ? { description: "Brainstorming ideas into designs" } : {}),
  18. }));
  19. for (const enabled of [true, false]) {
  20. const plugin: ZCodePluginInfo = {
  21. id: "superpowers@zcode-plugins-official",
  22. name: "superpowers",
  23. version: "5.1.0",
  24. enabled,
  25. source: "official",
  26. marketplace: "zcode-plugins-official",
  27. // 停用时 CLI 的 skillCount 为 0,但 components 仍完整。
  28. skillCount: enabled ? 14 : 0,
  29. skillRootCount: enabled ? 1 : 0,
  30. commandRootCount: 0,
  31. declaredMcpServerNames: [],
  32. mcpServerNames: [],
  33. hookDetails: [],
  34. rootPath: "/cache/superpowers",
  35. components: [{ kind: "skill", items: skillItems }],
  36. };
  37. const html = renderToStaticMarkup(
  38. createElement(
  39. ZCodeIntlProvider,
  40. { initialLocale: "zh-CN" },
  41. createElement(InstalledPluginDetailPanel, {
  42. getPluginOptionValue: (
  43. _plugin: ZCodePluginInfo,
  44. _key: string,
  45. option: ZCodePluginUserConfigOption,
  46. ) => option.default ?? "",
  47. onSavePluginOptions: () => {},
  48. activeOperations: [],
  49. plugin,
  50. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  51. setPluginOptionDraft: () => {},
  52. }),
  53. ),
  54. );
  55. // 数量徽标 + 首尾技能名都应可见。
  56. expect(html).toContain("14");
  57. expect(html).toContain("superpowers-skill-1");
  58. expect(html).toContain("superpowers-skill-14");
  59. expect(html).toContain("Brainstorming ideas into designs");
  60. // 不应再落到「启用后查看组件」的空态。
  61. expect(html).not.toContain("启用插件后查看其组件。");
  62. }
  63. });
  64. it("renders warning diagnostics scoped to the plugin and hides them when none match", () => {
  65. // Bugfix(静默失败)回归:声明的技能路径扫描为空等 warning 此前在 UI 无渲染位置。
  66. const plugin: ZCodePluginInfo = {
  67. id: "mattpocock-skills@claude-plugins-official",
  68. name: "mattpocock-skills",
  69. enabled: true,
  70. source: "cache",
  71. marketplace: "claude-plugins-official",
  72. skillCount: 0,
  73. skillRootCount: 25,
  74. commandRootCount: 0,
  75. declaredMcpServerNames: [],
  76. mcpServerNames: [],
  77. hookDetails: [],
  78. rootPath: "/cache/mattpocock-skills",
  79. components: [],
  80. };
  81. const renderPanel = (diagnostics: ZCodePluginDiagnostic[]) =>
  82. renderToStaticMarkup(
  83. createElement(
  84. ZCodeIntlProvider,
  85. { initialLocale: "zh-CN" },
  86. createElement(InstalledPluginDetailPanel, {
  87. diagnostics,
  88. getPluginOptionValue: (
  89. _plugin: ZCodePluginInfo,
  90. _key: string,
  91. option: ZCodePluginUserConfigOption,
  92. ) => option.default ?? "",
  93. onSavePluginOptions: () => {},
  94. activeOperations: [],
  95. plugin,
  96. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  97. setPluginOptionDraft: () => {},
  98. }),
  99. ),
  100. );
  101. // 只有当前插件自身的 warning 才展示;其他插件 / error 级别不进入列表。
  102. const withWarnings = renderPanel([
  103. {
  104. code: "plugin_skill_root_empty",
  105. message: "Plugin skills path does not contain any skills: ./skills/engineering/tdd",
  106. pluginId: "mattpocock-skills@claude-plugins-official",
  107. severity: "warning",
  108. },
  109. {
  110. code: "plugin_skill_root_empty",
  111. message: "Plugin skills path does not contain any skills: ./other",
  112. pluginId: "other-plugin@claude-plugins-official",
  113. severity: "warning",
  114. },
  115. {
  116. code: "plugin_manifest_invalid",
  117. message: "boom",
  118. pluginId: "mattpocock-skills@claude-plugins-official",
  119. severity: "error",
  120. },
  121. ]);
  122. expect(withWarnings).toContain("警告");
  123. expect(withWarnings).toContain("./skills/engineering/tdd");
  124. expect(withWarnings).not.toContain("./other");
  125. expect(withWarnings).not.toContain("boom");
  126. // 无匹配 warning 时不渲染空区块。
  127. expect(renderPanel([])).not.toContain("警告");
  128. });
  129. it("renders a details empty state for a plugin that declares no components", () => {
  130. // 真正无组件的插件给出诚实空态,不留白。
  131. const plugin: ZCodePluginInfo = {
  132. id: "empty-plugin@zcode-plugins-official",
  133. name: "empty-plugin",
  134. version: "0.1.0",
  135. enabled: false,
  136. source: "cache",
  137. marketplace: "zcode-plugins-official",
  138. skillCount: 0,
  139. skillRootCount: 0,
  140. commandRootCount: 0,
  141. declaredMcpServerNames: [],
  142. mcpServerNames: [],
  143. hookDetails: [],
  144. rootPath: "/cache/empty-plugin",
  145. components: [],
  146. };
  147. const html = renderToStaticMarkup(
  148. createElement(
  149. ZCodeIntlProvider,
  150. { initialLocale: "zh-CN" },
  151. createElement(InstalledPluginDetailPanel, {
  152. getPluginOptionValue: (
  153. _plugin: ZCodePluginInfo,
  154. _key: string,
  155. option: ZCodePluginUserConfigOption,
  156. ) => option.default ?? "",
  157. onSavePluginOptions: () => {},
  158. activeOperations: [],
  159. plugin,
  160. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  161. setPluginOptionDraft: () => {},
  162. }),
  163. ),
  164. );
  165. expect(html).toContain("详情");
  166. expect(html).toContain("无组件");
  167. });
  168. });