pluginStoreCardRestorable.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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("keeps project reset enabled while another plugin installs and disables it for its own reset", async () => {
  194. const item = installedItemWithRuntimeInfo();
  195. const resetButton = () =>
  196. buttonProps.findLast((props) => props["data-testid"] === "plugin-store-reset-config");
  197. await renderDetail(
  198. item,
  199. makeActions({
  200. onResetConfig: () => {},
  201. activeOperations: [{ id: "plugin:install:other@personal" }],
  202. }),
  203. );
  204. expect(resetButton()?.disabled).toBe(false);
  205. await renderDetail(
  206. item,
  207. makeActions({
  208. onResetConfig: () => {},
  209. activeOperations: [{ id: `plugin:reset-config:${item.id}` }],
  210. }),
  211. );
  212. expect(resetButton()?.disabled).toBe(true);
  213. });
  214. it("disables project reset while the same plugin saves its config", async () => {
  215. const item = installedItemWithRuntimeInfo();
  216. const resetButton = () =>
  217. buttonProps.findLast((props) => props["data-testid"] === "plugin-store-reset-config");
  218. await renderDetail(
  219. item,
  220. makeActions({
  221. onResetConfig: () => {},
  222. activeOperations: [{ id: `plugin:configure:${item.id}` }],
  223. }),
  224. );
  225. expect(resetButton()?.disabled).toBe(true);
  226. await renderDetail(
  227. item,
  228. makeActions({
  229. onResetConfig: () => {},
  230. activeOperations: [{ id: "plugin:configure:other@personal" }],
  231. }),
  232. );
  233. expect(resetButton()?.disabled).toBe(false);
  234. });
  235. it("invokes onInstall with the restorable item when the install button is clicked", async () => {
  236. const onInstall = vi.fn();
  237. await renderCard(restorableItem(), makeActions({ onInstall }));
  238. const clickHandlers = buttonProps
  239. .map((props) => props.onClick)
  240. .filter((onClick): onClick is (event?: unknown) => void => typeof onClick === "function");
  241. expect(clickHandlers.length).toBeGreaterThan(0);
  242. for (const onClick of clickHandlers) {
  243. // 安装按钮的 onClick 依赖事件对象做 stopPropagation,给一个最小事件桩。
  244. onClick({ stopPropagation: () => {} });
  245. }
  246. expect(onInstall).toHaveBeenCalledTimes(1);
  247. expect(onInstall.mock.calls[0]?.[0]).toMatchObject({
  248. id: "skill-creator@zcode-plugins-official",
  249. restorable: true,
  250. });
  251. });
  252. it("keeps the management menu when an installed plugin has no runtime info", async () => {
  253. const item = restorableItem({ installed: true, restorable: false });
  254. const html = await renderCard(item, makeActions());
  255. expect(html).not.toContain("Install");
  256. expect(html).toContain('data-testid="plugin-store-item-menu"');
  257. });
  258. it("keeps the detail management menu when an installed plugin has no runtime info", async () => {
  259. const item = restorableItem({ installed: true, restorable: false });
  260. const html = await renderDetail(item, makeActions());
  261. expect(html).not.toContain("Install");
  262. expect(html).toContain('data-testid="plugin-store-item-menu"');
  263. });
  264. it("shows Try now for an installed plugin without runtime info and delegates an empty prompt", async () => {
  265. const item = restorableItem({ installed: true, restorable: false });
  266. const onUsePrompt = vi.fn();
  267. const html = await renderDetail(item, makeActions(), onUsePrompt);
  268. expect(html).toContain('data-testid="plugin-store-try-now"');
  269. expect(html).toContain("Try now");
  270. const tryNowButton = buttonProps.find(
  271. (props) => props["data-testid"] === "plugin-store-try-now",
  272. );
  273. expect(tryNowButton?.onClick).toBeTypeOf("function");
  274. (tryNowButton?.onClick as (() => void) | undefined)?.();
  275. expect(onUsePrompt).toHaveBeenCalledWith(item, "");
  276. });
  277. it("keeps Try now hidden while an uninstalled plugin shows Install", async () => {
  278. const html = await renderDetail(restorableItem(), makeActions());
  279. expect(html).not.toContain('data-testid="plugin-store-try-now"');
  280. expect(html).toContain("Install");
  281. });
  282. it("keeps Workspace reset outside the menu for a missing package", async () => {
  283. const html = await renderDetail(
  284. restorableItem({
  285. info: {
  286. id: "skill-creator@zcode-plugins-official",
  287. name: "skill-creator",
  288. marketplace: "zcode-plugins-official",
  289. enabled: true,
  290. source: "missing",
  291. packageStatus: "missing",
  292. skillRootCount: 0,
  293. commandRootCount: 0,
  294. mcpServerNames: [],
  295. rootPath: "",
  296. },
  297. }),
  298. makeActions({ onResetConfig: () => {} }),
  299. );
  300. expect(html).toContain('data-testid="plugin-store-reset-config"');
  301. expect(html).not.toContain('data-testid="plugin-store-menu-uninstall"');
  302. expect(html).toContain("Install");
  303. });
  304. it("keeps installed card and detail enablement inside the actions menu", async () => {
  305. const item = installedItemWithRuntimeInfo();
  306. const cardHtml = await renderCard(item, makeActions());
  307. const detailHtml = await renderDetail(item, makeActions());
  308. expect(cardHtml).toContain('data-testid="plugin-store-item-menu"');
  309. expect(cardHtml).not.toContain('data-testid="plugin-store-enabled-switch"');
  310. expect(detailHtml).not.toContain('data-testid="plugin-store-enabled-switch"');
  311. expect(detailHtml).toContain('data-testid="plugin-store-item-menu"');
  312. });
  313. });