workspaceHelpMenuButton.test.ts 10 KB

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