workspaceHelpMenuButton.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { createElement } from "react";
  2. import type { ReactNode } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { describe, expect, it, vi } from "vitest";
  5. import { DesktopCommandIds, TID_WORKSPACE_HELP_MENU_RESOURCE_MANAGER } from "@zcode/shared";
  6. vi.mock("@/i18n/IntlProvider.js", () => ({
  7. useZCodeIntl: () => ({
  8. intl: {
  9. formatMessage: ({ id }: { id: string }) => id,
  10. },
  11. locale: "zh-CN",
  12. }),
  13. }));
  14. const captured = vi.hoisted(() => ({
  15. zsrcUrl: null as string | null,
  16. loading: false,
  17. openExternal: vi.fn(),
  18. exportLogs: vi.fn(async () => ({ success: true })),
  19. toast: vi.fn(() => 7),
  20. dismissToast: vi.fn(),
  21. menuItems: [] as Array<{
  22. label: string;
  23. testId?: string;
  24. disabled?: boolean;
  25. onSelect: (() => void | Promise<void>) | undefined;
  26. }>,
  27. updateMenu: {
  28. visible: true,
  29. disabled: false,
  30. labelId: "titleBar.menu.help.checkForUpdates",
  31. labelValues: undefined,
  32. },
  33. executeDesktopCommand: vi.fn(async () => undefined),
  34. }));
  35. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  36. DropdownMenu: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  37. DropdownMenuContent: ({ children }: { children: ReactNode }) =>
  38. createElement("div", null, children),
  39. DropdownMenuItem: ({
  40. children,
  41. onSelect,
  42. disabled,
  43. "data-testid": testId,
  44. }: {
  45. children: ReactNode;
  46. onSelect?: () => void | Promise<void>;
  47. disabled?: boolean;
  48. "data-testid"?: string;
  49. }) => {
  50. const label = Array.isArray(children)
  51. ? children.filter((child) => typeof child === "string").join("")
  52. : typeof children === "string"
  53. ? children
  54. : "";
  55. captured.menuItems.push({ label, testId, onSelect, disabled });
  56. return createElement("div", { "data-testid": testId }, children);
  57. },
  58. DropdownMenuSeparator: () => createElement("hr"),
  59. DropdownMenuTrigger: ({ children }: { children: ReactNode }) =>
  60. createElement("div", null, children),
  61. }));
  62. vi.mock("@/ControlHintTooltip.js", () => ({
  63. ControlHintTooltip: ({ children }: { children: ReactNode }) =>
  64. createElement("div", null, children),
  65. }));
  66. vi.mock("@/components/ui/toast.js", () => ({
  67. toast: captured.toast,
  68. dismissToast: captured.dismissToast,
  69. }));
  70. vi.mock("@/feedback/feedbackStore.js", () => ({
  71. useFeedbackStore: (
  72. selector: (state: {
  73. openSubmit: (draft?: unknown) => void;
  74. openFeatureRequest: () => void;
  75. }) => unknown,
  76. ) => selector({ openSubmit: vi.fn(), openFeatureRequest: vi.fn() }),
  77. }));
  78. vi.mock("@/hooks/useDesktopUpdateMenu.js", () => ({
  79. useDesktopUpdateMenu: (isDesktop: boolean) => ({
  80. ...captured.updateMenu,
  81. visible: isDesktop && captured.updateMenu.visible,
  82. checkForUpdates: () => captured.executeDesktopCommand(DesktopCommandIds.CheckForUpdates),
  83. }),
  84. }));
  85. vi.mock("@/hooks/usePlatform.js", () => ({
  86. usePlatform: () => ({
  87. captureWindowScreenshot: vi.fn(async () => null),
  88. executeDesktopCommand: captured.executeDesktopCommand,
  89. exportLogs: captured.exportLogs,
  90. openCommunity: vi.fn(async () => undefined),
  91. openExternal: captured.openExternal,
  92. }),
  93. }));
  94. vi.mock("@/hooks/useVulnerabilityReportUrl.js", () => ({
  95. useVulnerabilityReportUrl: () => ({ url: captured.zsrcUrl, loading: captured.loading }),
  96. }));
  97. describe("WorkspaceHelpMenuButton", () => {
  98. async function render(isDesktop: boolean | undefined) {
  99. captured.menuItems = [];
  100. captured.executeDesktopCommand.mockClear();
  101. captured.exportLogs.mockClear();
  102. captured.toast.mockClear();
  103. captured.dismissToast.mockClear();
  104. const { WorkspaceHelpMenuButton } = await import("@/WorkspaceHelpMenuButton.js");
  105. return renderToStaticMarkup(createElement(WorkspaceHelpMenuButton, { isDesktop }));
  106. }
  107. it("桌面和 Web 的漏洞入口紧跟问题上报,打开配置地址;缺失时隐藏", async () => {
  108. for (const desktop of [true, false]) {
  109. captured.zsrcUrl = "https://security.example.test/report";
  110. captured.openExternal.mockClear();
  111. try {
  112. await render(desktop);
  113. const index = captured.menuItems.findIndex(
  114. (item) => item.label === "workspaceHeader.help.reportVulnerability",
  115. );
  116. expect(index).toBeGreaterThan(0);
  117. expect(captured.menuItems[index - 1]?.label).toBe("workspaceHeader.help.issueReport");
  118. await captured.menuItems[index]?.onSelect?.();
  119. expect(captured.openExternal).toHaveBeenCalledExactlyOnceWith(captured.zsrcUrl);
  120. } finally {
  121. captured.zsrcUrl = null;
  122. }
  123. expect(await render(desktop)).not.toContain("workspaceHeader.help.reportVulnerability");
  124. }
  125. });
  126. it("冷缓存加载项不可触发外链,有效配置到达时保留原位置与文案", async () => {
  127. captured.loading = true;
  128. captured.openExternal.mockClear();
  129. try {
  130. await render(true);
  131. const before = captured.menuItems.map((item) => item.label);
  132. const item = captured.menuItems.find(
  133. (item) => item.label === "workspaceHeader.help.reportVulnerability",
  134. );
  135. expect(item).toMatchObject({ disabled: true, onSelect: undefined });
  136. captured.loading = false;
  137. captured.zsrcUrl = "https://security.example.test/report";
  138. await render(true);
  139. expect(captured.menuItems.map((item) => item.label)).toEqual(before);
  140. expect(
  141. captured.menuItems.find((item) => item.label === "workspaceHeader.help.reportVulnerability")
  142. ?.disabled,
  143. ).toBe(false);
  144. expect(captured.openExternal).not.toHaveBeenCalled();
  145. } finally {
  146. captured.loading = false;
  147. captured.zsrcUrl = null;
  148. }
  149. });
  150. it("桌面端渲染资源管理器项,点击走 OpenResourceManager 命令", async () => {
  151. // 原因:Windows/Linux 没有原生菜单栏,自绘箭头菜单也已下线,问号菜单是资源管理器唯一入口。
  152. const html = await render(true);
  153. expect(html).toContain("titleBar.menu.help.resourceManager");
  154. const item = captured.menuItems.find(
  155. (entry) => entry.testId === TID_WORKSPACE_HELP_MENU_RESOURCE_MANAGER,
  156. );
  157. expect(item).toBeDefined();
  158. await item!.onSelect?.();
  159. expect(captured.executeDesktopCommand).toHaveBeenCalledWith(
  160. DesktopCommandIds.OpenResourceManager,
  161. );
  162. });
  163. it("Web 端(isDesktop 缺省 / false)不渲染资源管理器项", async () => {
  164. for (const isDesktop of [undefined, false]) {
  165. const html = await render(isDesktop);
  166. expect(html).not.toContain("titleBar.menu.help.resourceManager");
  167. expect(
  168. captured.menuItems.some(
  169. (entry) => entry.testId === TID_WORKSPACE_HELP_MENU_RESOURCE_MANAGER,
  170. ),
  171. ).toBe(false);
  172. }
  173. });
  174. it("桌面端关于项位于最后,点击复用 ShowAbout", async () => {
  175. await render(true);
  176. const index = captured.menuItems.findIndex(
  177. (entry) => entry.label === "titleBar.menu.help.about",
  178. );
  179. expect(index).toBeGreaterThan(0);
  180. expect(index).toBe(captured.menuItems.length - 1);
  181. await captured.menuItems[index]?.onSelect?.();
  182. expect(captured.executeDesktopCommand).toHaveBeenCalledExactlyOnceWith(
  183. DesktopCommandIds.ShowAbout,
  184. );
  185. });
  186. it("Web 和手机远控不显示桌面关于入口", async () => {
  187. for (const isDesktop of [undefined, false]) {
  188. expect(await render(isDesktop)).not.toContain("titleBar.menu.help.about");
  189. }
  190. });
  191. it("资源管理器之后显示检查更新,点击执行已有命令", async () => {
  192. await render(true);
  193. const index = captured.menuItems.findIndex(
  194. (entry) => entry.label === "titleBar.menu.help.checkForUpdates",
  195. );
  196. expect(index).toBeGreaterThan(0);
  197. expect(captured.menuItems[index - 1]?.testId).toBe(TID_WORKSPACE_HELP_MENU_RESOURCE_MANAGER);
  198. await captured.menuItems[index]?.onSelect?.();
  199. expect(captured.executeDesktopCommand).toHaveBeenCalledExactlyOnceWith(
  200. DesktopCommandIds.CheckForUpdates,
  201. );
  202. });
  203. it("Web 与 Preview 不显示检查更新", async () => {
  204. expect(await render(false)).not.toContain("titleBar.menu.help.checkForUpdates");
  205. captured.updateMenu.visible = false;
  206. try {
  207. expect(await render(true)).not.toContain("titleBar.menu.help.checkForUpdates");
  208. } finally {
  209. captured.updateMenu.visible = true;
  210. }
  211. });
  212. it("桌面导出日志复用平台导出及共享提示", async () => {
  213. await render(true);
  214. const item = captured.menuItems.find(
  215. (entry) => entry.label === "titleBar.menu.help.exportLogs",
  216. );
  217. expect(item).toBeDefined();
  218. await item!.onSelect?.();
  219. expect(captured.exportLogs).toHaveBeenCalledExactlyOnceWith();
  220. expect(captured.toast).toHaveBeenCalledWith("sidebar.exportLogs.pending", {
  221. durationMs: Number.POSITIVE_INFINITY,
  222. });
  223. expect(captured.dismissToast).toHaveBeenCalledWith(7);
  224. });
  225. it("Web 和手机远控不显示本地导出日志入口", async () => {
  226. for (const isDesktop of [undefined, false]) {
  227. expect(await render(isDesktop)).not.toContain("titleBar.menu.help.exportLogs");
  228. expect(captured.exportLogs).not.toHaveBeenCalled();
  229. }
  230. });
  231. it("桌面两组一条分隔线,导出日志位于桌面组首项,Web 仅帮助组", async () => {
  232. for (const desktop of [true, false]) {
  233. const html = await render(desktop);
  234. expect(captured.menuItems.map((item) => item.label)).toEqual([
  235. "workspaceHeader.help.docs",
  236. "workspaceHeader.help.community",
  237. "workspaceHeader.help.issueReport",
  238. "workspaceHeader.help.productRequest",
  239. ...(desktop
  240. ? [
  241. "titleBar.menu.help.exportLogs",
  242. "titleBar.menu.help.resourceManager",
  243. "titleBar.menu.help.checkForUpdates",
  244. "titleBar.menu.help.about",
  245. ]
  246. : []),
  247. ]);
  248. expect((html.match(/<hr/g) ?? []).length).toBe(desktop ? 1 : 0);
  249. }
  250. });
  251. it("桌面端与 Web 端都保留原有帮助项", async () => {
  252. for (const isDesktop of [true, false]) {
  253. const html = await render(isDesktop);
  254. for (const id of [
  255. "workspaceHeader.help.issueReport",
  256. "workspaceHeader.help.productRequest",
  257. "workspaceHeader.help.community",
  258. "workspaceHeader.help.docs",
  259. ]) {
  260. expect(html).toContain(id);
  261. }
  262. }
  263. });
  264. });