installedPluginManagementUpdate.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { act, createElement, type ReactNode } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { ZCodePluginInfo, ZCodePluginUserConfigOption } from "@zcode/shared";
  6. // 复用 taskListItemHoverActions.test.ts 的「mock Button 捕获 onClick」套路:
  7. // renderToStaticMarkup 不会派发真实点击事件,故把 Button 替换成记录 props 的桩,
  8. // 在 createRoot 渲染后直接调用捕获到的 onClick,确定性地验证更新按钮的点击回调。
  9. const { buttonProps } = vi.hoisted(() => ({
  10. buttonProps: [] as Array<Record<string, unknown>>,
  11. }));
  12. vi.mock("@/components/ui/button.js", () => ({
  13. Button: ({ children, ...props }: { children?: ReactNode }) => {
  14. buttonProps.push(props);
  15. return createElement("button", props as Record<string, unknown>, children);
  16. },
  17. }));
  18. function basePlugin(overrides: Partial<ZCodePluginInfo> = {}): ZCodePluginInfo {
  19. return {
  20. id: "skill-creator@zcode-plugins-official",
  21. name: "skill-creator",
  22. version: "1.0.0",
  23. enabled: true,
  24. source: "official",
  25. marketplace: "zcode-plugins-official",
  26. skillCount: 0,
  27. skillRootCount: 0,
  28. commandRootCount: 0,
  29. declaredMcpServerNames: [],
  30. mcpServerNames: [],
  31. hookDetails: [],
  32. rootPath: "/cache/skill-creator",
  33. components: [],
  34. ...overrides,
  35. };
  36. }
  37. const sharedPanelProps = {
  38. getPluginOptionValue: (
  39. _plugin: ZCodePluginInfo,
  40. _key: string,
  41. option: ZCodePluginUserConfigOption,
  42. ) => option.default ?? "",
  43. onSavePluginOptions: () => {},
  44. setPluginOptionDraft: () => {},
  45. renderPluginSource: (target: ZCodePluginInfo) => target.marketplace,
  46. } as const;
  47. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  48. const element = {
  49. addEventListener: () => {},
  50. appendChild: (child: { parentNode?: unknown }) => {
  51. child.parentNode = element;
  52. return child;
  53. },
  54. childNodes: [] as unknown[],
  55. getAttribute: () => null,
  56. insertBefore: (child: { parentNode?: unknown }) => {
  57. child.parentNode = element;
  58. return child;
  59. },
  60. nodeName: tagName.toUpperCase(),
  61. nodeType: 1,
  62. ownerDocument,
  63. parentNode: null as unknown,
  64. removeAttribute: () => {},
  65. removeChild: (child: { parentNode?: unknown }) => {
  66. child.parentNode = null;
  67. return child;
  68. },
  69. removeEventListener: () => {},
  70. setAttribute: () => {},
  71. style: {},
  72. tagName: tagName.toUpperCase(),
  73. };
  74. return element as unknown as Element;
  75. }
  76. function installMinimalDom() {
  77. const documentMock = {
  78. addEventListener: () => {},
  79. createElement: (tagName: string) =>
  80. createMinimalElement(documentMock as unknown as Document, tagName),
  81. createElementNS: (_namespace: string, tagName: string) =>
  82. createMinimalElement(documentMock as unknown as Document, tagName),
  83. createTextNode: (nodeValue: string) => ({
  84. nodeType: 3,
  85. nodeValue,
  86. ownerDocument: documentMock,
  87. parentNode: null,
  88. }),
  89. nodeType: 9,
  90. removeEventListener: () => {},
  91. } as unknown as Document;
  92. const windowMock = {
  93. addEventListener: () => {},
  94. document: documentMock,
  95. HTMLIFrameElement: function HTMLIFrameElement() {},
  96. HTMLElement: function HTMLElement() {},
  97. Node: function Node() {},
  98. removeEventListener: () => {},
  99. };
  100. Object.defineProperty(globalThis, "document", {
  101. configurable: true,
  102. value: documentMock,
  103. });
  104. Object.defineProperty(globalThis, "window", {
  105. configurable: true,
  106. value: windowMock,
  107. });
  108. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  109. configurable: true,
  110. value: true,
  111. });
  112. return createMinimalElement(documentMock);
  113. }
  114. describe("InstalledPluginDetailPanel update badge", () => {
  115. afterEach(() => {
  116. buttonProps.length = 0;
  117. delete (globalThis as { document?: unknown }).document;
  118. delete (globalThis as { window?: unknown }).window;
  119. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  120. });
  121. it("renders the update button, target version, and new-sessions note when update-available", async () => {
  122. const { InstalledPluginDetailPanel } =
  123. await import("../src/settings/InstalledPluginManagement.js");
  124. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  125. const html = renderToStaticMarkup(
  126. createElement(
  127. ZCodeIntlProvider,
  128. { initialLocale: "en-US" },
  129. createElement(InstalledPluginDetailPanel, {
  130. ...sharedPanelProps,
  131. activeOperations: [],
  132. plugin: basePlugin(),
  133. updateStatus: "update-available",
  134. latestVersion: "2.0.0",
  135. uninstallable: true,
  136. onRequestUninstall: () => {},
  137. onRequestUpdate: () => {},
  138. }),
  139. ),
  140. );
  141. expect(html).toContain("Update available → v2.0.0");
  142. expect(html).toContain("Takes effect in new sessions");
  143. });
  144. it("omits the update block when there is no update", async () => {
  145. const { InstalledPluginDetailPanel } =
  146. await import("../src/settings/InstalledPluginManagement.js");
  147. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  148. const html = renderToStaticMarkup(
  149. createElement(
  150. ZCodeIntlProvider,
  151. { initialLocale: "en-US" },
  152. createElement(InstalledPluginDetailPanel, {
  153. ...sharedPanelProps,
  154. activeOperations: [],
  155. plugin: basePlugin(),
  156. updateStatus: "none",
  157. uninstallable: true,
  158. onRequestUninstall: () => {},
  159. onRequestUpdate: () => {},
  160. }),
  161. ),
  162. );
  163. expect(html).not.toContain("Takes effect in new sessions");
  164. });
  165. it("invokes onRequestUpdate with the plugin id when the update button is clicked", async () => {
  166. const { InstalledPluginDetailPanel } =
  167. await import("../src/settings/InstalledPluginManagement.js");
  168. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  169. const onRequestUpdate = vi.fn();
  170. const root: Root = createRoot(installMinimalDom());
  171. act(() => {
  172. root.render(
  173. createElement(
  174. ZCodeIntlProvider,
  175. { initialLocale: "en-US" },
  176. createElement(InstalledPluginDetailPanel, {
  177. ...sharedPanelProps,
  178. activeOperations: [],
  179. plugin: basePlugin(),
  180. updateStatus: "update-available",
  181. latestVersion: "2.0.0",
  182. uninstallable: true,
  183. onRequestUninstall: () => {},
  184. onRequestUpdate,
  185. }),
  186. ),
  187. );
  188. });
  189. // 收集所有渲染到的按钮 onClick(更新按钮 + 卸载按钮),逐个触发;
  190. // 只有更新按钮的回调会带上 pluginId 调用 onRequestUpdate。
  191. const clickHandlers = buttonProps
  192. .map((props) => props.onClick)
  193. .filter((onClick): onClick is () => void => typeof onClick === "function");
  194. expect(clickHandlers.length).toBeGreaterThan(0);
  195. for (const onClick of clickHandlers) {
  196. onClick();
  197. }
  198. expect(onRequestUpdate).toHaveBeenCalledWith("skill-creator@zcode-plugins-official");
  199. expect(onRequestUpdate).toHaveBeenCalledTimes(1);
  200. act(() => {
  201. root.unmount();
  202. });
  203. });
  204. });