installedPluginManagementUpdate.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 } = await import(
  123. "../src/settings/InstalledPluginManagement.js"
  124. );
  125. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  126. const html = renderToStaticMarkup(
  127. createElement(
  128. ZCodeIntlProvider,
  129. { initialLocale: "en-US" },
  130. createElement(InstalledPluginDetailPanel, {
  131. ...sharedPanelProps,
  132. operationId: null,
  133. plugin: basePlugin(),
  134. updateStatus: "update-available",
  135. latestVersion: "2.0.0",
  136. uninstallable: true,
  137. onRequestUninstall: () => {},
  138. onRequestUpdate: () => {},
  139. }),
  140. ),
  141. );
  142. expect(html).toContain("Update available → v2.0.0");
  143. expect(html).toContain("Takes effect in new sessions");
  144. });
  145. it("omits the update block when there is no update", async () => {
  146. const { InstalledPluginDetailPanel } = await import(
  147. "../src/settings/InstalledPluginManagement.js"
  148. );
  149. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  150. const html = renderToStaticMarkup(
  151. createElement(
  152. ZCodeIntlProvider,
  153. { initialLocale: "en-US" },
  154. createElement(InstalledPluginDetailPanel, {
  155. ...sharedPanelProps,
  156. operationId: null,
  157. plugin: basePlugin(),
  158. updateStatus: "none",
  159. uninstallable: true,
  160. onRequestUninstall: () => {},
  161. onRequestUpdate: () => {},
  162. }),
  163. ),
  164. );
  165. expect(html).not.toContain("Takes effect in new sessions");
  166. });
  167. it("invokes onRequestUpdate with the plugin id when the update button is clicked", async () => {
  168. const { InstalledPluginDetailPanel } = await import(
  169. "../src/settings/InstalledPluginManagement.js"
  170. );
  171. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  172. const onRequestUpdate = vi.fn();
  173. const root: Root = createRoot(installMinimalDom());
  174. act(() => {
  175. root.render(
  176. createElement(
  177. ZCodeIntlProvider,
  178. { initialLocale: "en-US" },
  179. createElement(InstalledPluginDetailPanel, {
  180. ...sharedPanelProps,
  181. operationId: null,
  182. plugin: basePlugin(),
  183. updateStatus: "update-available",
  184. latestVersion: "2.0.0",
  185. uninstallable: true,
  186. onRequestUninstall: () => {},
  187. onRequestUpdate,
  188. }),
  189. ),
  190. );
  191. });
  192. // 收集所有渲染到的按钮 onClick(更新按钮 + 卸载按钮),逐个触发;
  193. // 只有更新按钮的回调会带上 pluginId 调用 onRequestUpdate。
  194. const clickHandlers = buttonProps
  195. .map((props) => props.onClick)
  196. .filter((onClick): onClick is () => void => typeof onClick === "function");
  197. expect(clickHandlers.length).toBeGreaterThan(0);
  198. for (const onClick of clickHandlers) {
  199. onClick();
  200. }
  201. expect(onRequestUpdate).toHaveBeenCalledWith("skill-creator@zcode-plugins-official");
  202. expect(onRequestUpdate).toHaveBeenCalledTimes(1);
  203. act(() => {
  204. root.unmount();
  205. });
  206. });
  207. });