pluginStoreCardRestorable.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. operationId: null,
  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("invokes onInstall with the restorable item when the install button is clicked", async () => {
  109. const onInstall = vi.fn();
  110. await renderCard(restorableItem(), makeActions({ onInstall }));
  111. const clickHandlers = buttonProps
  112. .map((props) => props.onClick)
  113. .filter((onClick): onClick is (event?: unknown) => void => typeof onClick === "function");
  114. expect(clickHandlers.length).toBeGreaterThan(0);
  115. for (const onClick of clickHandlers) {
  116. // 安装按钮的 onClick 依赖事件对象做 stopPropagation,给一个最小事件桩。
  117. onClick({ stopPropagation: () => {} });
  118. }
  119. expect(onInstall).toHaveBeenCalledTimes(1);
  120. expect(onInstall.mock.calls[0]?.[0]).toMatchObject({
  121. id: "skill-creator@zcode-plugins-official",
  122. restorable: true,
  123. });
  124. });
  125. it("keeps the management menu when an installed plugin has no runtime info", async () => {
  126. const item = restorableItem({ installed: true, restorable: false });
  127. const html = await renderCard(item, makeActions());
  128. expect(html).not.toContain("Install");
  129. expect(html).toContain('data-testid="plugin-store-item-menu"');
  130. });
  131. it("keeps the detail management menu when an installed plugin has no runtime info", async () => {
  132. const item = restorableItem({ installed: true, restorable: false });
  133. const html = await renderDetail(item, makeActions());
  134. expect(html).not.toContain("Install");
  135. expect(html).toContain('data-testid="plugin-store-item-menu"');
  136. });
  137. it("shows Try now for an installed plugin without runtime info and delegates an empty prompt", async () => {
  138. const item = restorableItem({ installed: true, restorable: false });
  139. const onUsePrompt = vi.fn();
  140. const html = await renderDetail(item, makeActions(), onUsePrompt);
  141. expect(html).toContain('data-testid="plugin-store-try-now"');
  142. expect(html).toContain("Try now");
  143. const tryNowButton = buttonProps.find(
  144. (props) => props["data-testid"] === "plugin-store-try-now",
  145. );
  146. expect(tryNowButton?.onClick).toBeTypeOf("function");
  147. (tryNowButton?.onClick as (() => void) | undefined)?.();
  148. expect(onUsePrompt).toHaveBeenCalledWith(item, "");
  149. });
  150. it("keeps Try now hidden while an uninstalled plugin shows Install", async () => {
  151. const html = await renderDetail(restorableItem(), makeActions());
  152. expect(html).not.toContain('data-testid="plugin-store-try-now"');
  153. expect(html).toContain("Install");
  154. });
  155. it("keeps the Workspace config menu for a missing package without offering uninstall", async () => {
  156. const html = await renderDetail(
  157. restorableItem({
  158. info: {
  159. id: "skill-creator@zcode-plugins-official",
  160. name: "skill-creator",
  161. marketplace: "zcode-plugins-official",
  162. enabled: true,
  163. source: "missing",
  164. packageStatus: "missing",
  165. skillRootCount: 0,
  166. commandRootCount: 0,
  167. mcpServerNames: [],
  168. rootPath: "",
  169. },
  170. }),
  171. makeActions({ onResetConfig: () => {} }),
  172. );
  173. expect(html).toContain('data-testid="plugin-store-item-menu"');
  174. expect(html).not.toContain('data-testid="plugin-store-menu-uninstall"');
  175. expect(html).toContain("Install");
  176. });
  177. it("keeps installed card and detail enablement inside the actions menu", async () => {
  178. const item = installedItemWithRuntimeInfo();
  179. const cardHtml = await renderCard(item, makeActions());
  180. const detailHtml = await renderDetail(item, makeActions());
  181. expect(cardHtml).toContain('data-testid="plugin-store-item-menu"');
  182. expect(cardHtml).not.toContain('data-testid="plugin-store-enabled-switch"');
  183. expect(detailHtml).not.toContain('data-testid="plugin-store-enabled-switch"');
  184. expect(detailHtml).toContain('data-testid="plugin-store-item-menu"');
  185. });
  186. });