pluginStoreCardRestorable.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { ZCodeAvailablePluginSummary, ZCodePluginInfo } from "@zcode/shared";
  5. import type { StorePluginItem } from "../src/settings/pluginStoreListing.js";
  6. import type { PluginStoreActions } from "../src/settings/PluginStoreCard.js";
  7. // 复用 installedPluginManagementUpdate.test.ts 的「mock Button 捕获 onClick」套路:
  8. // SSR 渲染时桩组件把 props 入栈,渲染后直接调用捕获到的按钮 onClick,
  9. // 确定性验证回调——无需真实 DOM/卸载/计时器。
  10. const { buttonProps } = vi.hoisted(() => ({
  11. buttonProps: [] as Array<Record<string, unknown>>,
  12. }));
  13. vi.mock("@/components/ui/button.js", () => ({
  14. Button: ({ children, ...props }: { children?: ReactNode }) => {
  15. buttonProps.push(props);
  16. return createElement("button", props as Record<string, unknown>, children);
  17. },
  18. }));
  19. function restorableItem(overrides: Partial<StorePluginItem> = {}): StorePluginItem {
  20. const summary: ZCodeAvailablePluginSummary = {
  21. id: "skill-creator@zcode-plugins-official",
  22. name: "skill-creator",
  23. marketplace: "zcode-plugins-official",
  24. version: "1.0.0",
  25. installed: false,
  26. };
  27. return {
  28. id: summary.id,
  29. name: summary.name,
  30. marketplace: summary.marketplace,
  31. installed: false,
  32. restorable: true,
  33. orphaned: false,
  34. summary,
  35. ...overrides,
  36. };
  37. }
  38. function makeActions(overrides: Partial<PluginStoreActions> = {}): PluginStoreActions {
  39. return {
  40. onOpenDetail: () => {},
  41. onInstall: () => {},
  42. onUninstall: () => {},
  43. onSetEnabled: () => {},
  44. onUpdate: () => {},
  45. activeOperations: [],
  46. togglingPluginId: null,
  47. ...overrides,
  48. };
  49. }
  50. function installedItemWithRuntimeInfo(): StorePluginItem {
  51. const item = restorableItem({ installed: true, restorable: false });
  52. const info: ZCodePluginInfo = {
  53. id: item.id,
  54. name: item.name,
  55. marketplace: item.marketplace,
  56. enabled: true,
  57. source: "official",
  58. skillCount: 1,
  59. skillRootCount: 1,
  60. commandRootCount: 0,
  61. declaredMcpServerNames: [],
  62. mcpServerNames: [],
  63. rootPath: "/cache/skill-creator",
  64. };
  65. return { ...item, info };
  66. }
  67. async function renderCard(item: StorePluginItem, actions: PluginStoreActions): Promise<string> {
  68. const { PluginStoreCard } = await import("../src/settings/PluginStoreCard.js");
  69. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  70. return renderToStaticMarkup(
  71. createElement(
  72. ZCodeIntlProvider,
  73. { initialLocale: "en-US" },
  74. createElement(PluginStoreCard, { item, actions, locale: "en-US" }),
  75. ),
  76. );
  77. }
  78. async function renderDetail(
  79. item: StorePluginItem,
  80. actions: PluginStoreActions,
  81. onUsePrompt: (item: StorePluginItem, prompt: string) => void = () => {},
  82. ): Promise<string> {
  83. const { PluginStoreDetailView } = await import("../src/settings/PluginStoreDetailView.js");
  84. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  85. return renderToStaticMarkup(
  86. createElement(
  87. ZCodeIntlProvider,
  88. { initialLocale: "en-US" },
  89. createElement(PluginStoreDetailView, {
  90. item,
  91. actions,
  92. onBack: () => {},
  93. onRetryDescribe: () => {},
  94. onUsePrompt,
  95. }),
  96. ),
  97. );
  98. }
  99. describe("PluginStoreCard restorable built-ins", () => {
  100. afterEach(() => {
  101. buttonProps.length = 0;
  102. });
  103. it("renders an install button for an uninstalled (restorable) built-in", async () => {
  104. const html = await renderCard(restorableItem(), makeActions());
  105. expect(html).toContain("skill-creator");
  106. expect(html).toContain("Install");
  107. });
  108. it.each([true, false])(
  109. "side pane market adds an open button for installed UI plugins with enabled=%s",
  110. async (enabled) => {
  111. const item = installedItemWithRuntimeInfo();
  112. item.info!.enabled = enabled;
  113. item.info!.uiSurfaces = [
  114. {
  115. pluginId: item.id,
  116. pluginName: item.name,
  117. id: "panel",
  118. title: "Panel",
  119. server: "panel",
  120. resourceUri: "ui://panel/app",
  121. availability: "session",
  122. },
  123. ];
  124. const onOpenUi = vi.fn();
  125. const html = await renderCard(item, makeActions({ onOpenUi }));
  126. expect(html).toContain(enabled ? "Open" : "Enable");
  127. // 卡片整行进入详情,不再提供独立「详情」按钮。
  128. expect(html).not.toContain('data-testid="plugin-store-ui-details"');
  129. const button = buttonProps.find((props) => props["data-testid"] === "plugin-store-open-ui");
  130. expect(button).toBeDefined();
  131. (button!.onClick as (event: { stopPropagation: () => void }) => void)({
  132. stopPropagation: () => {},
  133. });
  134. expect(onOpenUi).toHaveBeenCalledWith(item);
  135. },
  136. );
  137. it("main navigation market has no UI open or details buttons", async () => {
  138. const item = installedItemWithRuntimeInfo();
  139. item.info!.uiSurfaces = [
  140. {
  141. pluginId: item.id,
  142. pluginName: item.name,
  143. id: "panel",
  144. title: "Panel",
  145. server: "panel",
  146. resourceUri: "ui://panel/app",
  147. availability: "session",
  148. },
  149. ];
  150. const card = await renderCard(item, makeActions());
  151. expect(card).not.toContain('data-testid="plugin-store-open-ui"');
  152. expect(card).not.toContain('data-testid="plugin-store-ui-details"');
  153. // 主导航详情不打开 UI,恢复普通插件的「立即试用」。
  154. const detail = await renderDetail(item, makeActions(), vi.fn());
  155. expect(detail).not.toContain('data-testid="plugin-store-open-ui"');
  156. expect(detail).toContain('data-testid="plugin-store-try-now"');
  157. });
  158. it("shows a UI opening action and capability in details without creating a chat", async () => {
  159. const item = installedItemWithRuntimeInfo();
  160. item.info!.uiSurfaces = [
  161. {
  162. pluginId: item.id,
  163. pluginName: item.name,
  164. id: "panel",
  165. title: "Panel",
  166. server: "panel",
  167. resourceUri: "ui://panel/app",
  168. availability: "session",
  169. },
  170. ];
  171. const onOpenUi = vi.fn();
  172. const onUsePrompt = vi.fn();
  173. const html = await renderDetail(item, makeActions({ onOpenUi }), onUsePrompt);
  174. expect(html).toContain('data-testid="plugin-store-ui-capability"');
  175. expect(html).not.toContain('data-testid="plugin-store-try-now"');
  176. const button = buttonProps.find((props) => props["data-testid"] === "plugin-store-open-ui");
  177. (button?.onClick as (event: { stopPropagation: () => void }) => void)({
  178. stopPropagation: () => {},
  179. });
  180. expect(onOpenUi).toHaveBeenCalledWith(item);
  181. expect(onUsePrompt).not.toHaveBeenCalled();
  182. });
  183. it("keeps project reset accessible outside the more menu", async () => {
  184. const item = installedItemWithRuntimeInfo();
  185. const onResetConfig = vi.fn();
  186. await renderDetail(item, makeActions({ onResetConfig }));
  187. const button = buttonProps.find(
  188. (props) => props["data-testid"] === "plugin-store-reset-config",
  189. );
  190. (button?.onClick as () => void)();
  191. expect(onResetConfig).toHaveBeenCalledWith(item.id);
  192. });
  193. it("invokes onInstall with the restorable item when the install button is clicked", async () => {
  194. const onInstall = vi.fn();
  195. await renderCard(restorableItem(), makeActions({ onInstall }));
  196. const clickHandlers = buttonProps
  197. .map((props) => props.onClick)
  198. .filter((onClick): onClick is (event?: unknown) => void => typeof onClick === "function");
  199. expect(clickHandlers.length).toBeGreaterThan(0);
  200. for (const onClick of clickHandlers) {
  201. // 安装按钮的 onClick 依赖事件对象做 stopPropagation,给一个最小事件桩。
  202. onClick({ stopPropagation: () => {} });
  203. }
  204. expect(onInstall).toHaveBeenCalledTimes(1);
  205. expect(onInstall.mock.calls[0]?.[0]).toMatchObject({
  206. id: "skill-creator@zcode-plugins-official",
  207. restorable: true,
  208. });
  209. });
  210. it("keeps the management menu when an installed plugin has no runtime info", async () => {
  211. const item = restorableItem({ installed: true, restorable: false });
  212. const html = await renderCard(item, makeActions());
  213. expect(html).not.toContain("Install");
  214. expect(html).toContain('data-testid="plugin-store-item-menu"');
  215. });
  216. it("keeps the detail management menu when an installed plugin has no runtime info", async () => {
  217. const item = restorableItem({ installed: true, restorable: false });
  218. const html = await renderDetail(item, makeActions());
  219. expect(html).not.toContain("Install");
  220. expect(html).toContain('data-testid="plugin-store-item-menu"');
  221. });
  222. it("shows Try now for an installed plugin without runtime info and delegates an empty prompt", async () => {
  223. const item = restorableItem({ installed: true, restorable: false });
  224. const onUsePrompt = vi.fn();
  225. const html = await renderDetail(item, makeActions(), onUsePrompt);
  226. expect(html).toContain('data-testid="plugin-store-try-now"');
  227. expect(html).toContain("Try now");
  228. const tryNowButton = buttonProps.find(
  229. (props) => props["data-testid"] === "plugin-store-try-now",
  230. );
  231. expect(tryNowButton?.onClick).toBeTypeOf("function");
  232. (tryNowButton?.onClick as (() => void) | undefined)?.();
  233. expect(onUsePrompt).toHaveBeenCalledWith(item, "");
  234. });
  235. it("keeps Try now hidden while an uninstalled plugin shows Install", async () => {
  236. const html = await renderDetail(restorableItem(), makeActions());
  237. expect(html).not.toContain('data-testid="plugin-store-try-now"');
  238. expect(html).toContain("Install");
  239. });
  240. it("keeps Workspace reset outside the menu for a missing package", async () => {
  241. const html = await renderDetail(
  242. restorableItem({
  243. info: {
  244. id: "skill-creator@zcode-plugins-official",
  245. name: "skill-creator",
  246. marketplace: "zcode-plugins-official",
  247. enabled: true,
  248. source: "missing",
  249. packageStatus: "missing",
  250. skillRootCount: 0,
  251. commandRootCount: 0,
  252. mcpServerNames: [],
  253. rootPath: "",
  254. },
  255. }),
  256. makeActions({ onResetConfig: () => {} }),
  257. );
  258. expect(html).toContain('data-testid="plugin-store-reset-config"');
  259. expect(html).not.toContain('data-testid="plugin-store-menu-uninstall"');
  260. expect(html).toContain("Install");
  261. });
  262. it("keeps installed card and detail enablement inside the actions menu", async () => {
  263. const item = installedItemWithRuntimeInfo();
  264. const cardHtml = await renderCard(item, makeActions());
  265. const detailHtml = await renderDetail(item, makeActions());
  266. expect(cardHtml).toContain('data-testid="plugin-store-item-menu"');
  267. expect(cardHtml).not.toContain('data-testid="plugin-store-enabled-switch"');
  268. expect(detailHtml).not.toContain('data-testid="plugin-store-enabled-switch"');
  269. expect(detailHtml).toContain('data-testid="plugin-store-item-menu"');
  270. });
  271. });