windowsCaptionMenuButton.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. vi.mock("@/i18n/IntlProvider.js", () => ({
  6. useZCodeIntl: () => ({
  7. intl: {
  8. formatMessage: ({ id }: { id: string }) => id,
  9. },
  10. locale: "zh-CN",
  11. }),
  12. }));
  13. const capturedDropdownContentProps = vi.hoisted(() => ({
  14. calls: [] as Array<{ align?: string; alignOffset?: number }>,
  15. }));
  16. const capturedWindowsCaptionMenu = vi.hoisted(() => {
  17. const screenshot = {
  18. dataBase64: "base64-image",
  19. filename: "zcode-error-2026-06-17.png",
  20. contentType: "image/png",
  21. size: 12,
  22. };
  23. return {
  24. screenshot,
  25. menuItems: [] as Array<{
  26. label: string;
  27. onSelect: (() => void | Promise<void>) | undefined;
  28. }>,
  29. openSubmit: vi.fn(),
  30. openFeatureRequest: vi.fn(),
  31. openExternal: vi.fn(),
  32. exportLogs: vi.fn(async () => ({ success: true })),
  33. captureWindowScreenshot: vi.fn(async () => screenshot),
  34. executeDesktopCommand: vi.fn(async () => undefined),
  35. };
  36. });
  37. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  38. DropdownMenu: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  39. DropdownMenuCheckboxItem: ({ children }: { children: ReactNode }) =>
  40. createElement("div", null, children),
  41. DropdownMenuContent: ({
  42. children,
  43. align,
  44. alignOffset,
  45. }: {
  46. children: ReactNode;
  47. align?: string;
  48. alignOffset?: number;
  49. }) => {
  50. capturedDropdownContentProps.calls.push({ align, alignOffset });
  51. return createElement("div", null, children);
  52. },
  53. DropdownMenuItem: ({
  54. children,
  55. onSelect,
  56. }: {
  57. children: ReactNode;
  58. onSelect?: () => void | Promise<void>;
  59. }) => {
  60. const label = Array.isArray(children)
  61. ? children.filter((child) => typeof child === "string").join("")
  62. : typeof children === "string"
  63. ? children
  64. : "";
  65. capturedWindowsCaptionMenu.menuItems.push({ label, onSelect });
  66. return createElement("div", null, children);
  67. },
  68. DropdownMenuSeparator: () => createElement("hr"),
  69. DropdownMenuShortcut: ({ children }: { children: ReactNode }) =>
  70. createElement("span", null, children),
  71. DropdownMenuTrigger: ({ children }: { children: ReactNode }) =>
  72. createElement("div", null, children),
  73. }));
  74. vi.mock("@/components/ui/toast.js", () => ({
  75. dismissToast: vi.fn(),
  76. toast: vi.fn(() => 1),
  77. }));
  78. vi.mock("@/feedback/feedbackStore.js", () => ({
  79. useFeedbackStore: (
  80. selector: (state: {
  81. openSubmit: (draft?: unknown) => void;
  82. openFeatureRequest: () => void;
  83. }) => unknown,
  84. ) =>
  85. selector({
  86. openSubmit: capturedWindowsCaptionMenu.openSubmit,
  87. openFeatureRequest: capturedWindowsCaptionMenu.openFeatureRequest,
  88. }),
  89. }));
  90. vi.mock("@/hooks/usePlatform.js", () => ({
  91. usePlatform: () => ({
  92. canOpenCommunity: vi.fn(async () => false),
  93. captureWindowScreenshot: capturedWindowsCaptionMenu.captureWindowScreenshot,
  94. executeDesktopCommand: capturedWindowsCaptionMenu.executeDesktopCommand,
  95. exportLogs: capturedWindowsCaptionMenu.exportLogs,
  96. openExternal: capturedWindowsCaptionMenu.openExternal,
  97. openInFileManager: vi.fn(async () => ({ success: true })),
  98. }),
  99. }));
  100. describe("WindowsCaptionMenuButton", () => {
  101. async function renderButton(
  102. triggerPresentation?: "header" | "header-borderless",
  103. useWindowsCaptionSpacing = false,
  104. ) {
  105. capturedDropdownContentProps.calls = [];
  106. capturedWindowsCaptionMenu.menuItems = [];
  107. const { WindowsCaptionMenuButton } = await import("@/WindowsCaptionMenuButton.js");
  108. // 快捷键特性后组件经 useShortcutCommandLabel 读设置,裸渲染需提供最小 ServiceProvider
  109. const { ServiceProvider } = await import("@/hooks/useServices.js");
  110. const { createSettingsTestServices } = await import("./lib/settingsTestServices.js");
  111. return renderToStaticMarkup(
  112. createElement(
  113. ServiceProvider,
  114. { services: createSettingsTestServices() },
  115. createElement(WindowsCaptionMenuButton, {
  116. workspaceAbsPath: "/workspace",
  117. onCreateTask: vi.fn(),
  118. onOpenWorkspace: vi.fn(),
  119. onToggleBrowser: vi.fn(),
  120. onToggleTerminal: vi.fn(),
  121. triggerPresentation,
  122. useWindowsCaptionSpacing,
  123. }),
  124. ),
  125. );
  126. }
  127. it("keeps the normal header trigger flush with the caption edge", async () => {
  128. const html = await renderButton("header");
  129. expect(html).toContain("border-l");
  130. expect(html).not.toContain("bg-surface");
  131. });
  132. it("uses the header trigger shape without a border for Windows new task drafts", async () => {
  133. const html = await renderButton("header-borderless", true);
  134. // Bugfix: Windows 新任务草稿态要和 session task 的标题栏入口一致,但不能再带左侧分隔线。
  135. expect(html).toContain(
  136. "h-full w-[var(--windows-caption-control-width,46px)] rounded-none border-0 hover:bg-hover",
  137. );
  138. expect(html).not.toContain("border-l");
  139. expect(html).not.toContain("bg-surface");
  140. });
  141. it("keeps the legacy caption trigger width unless Windows spacing is requested", async () => {
  142. const html = await renderButton("header-borderless");
  143. expect(html).toContain("h-full w-[45px] rounded-none border-0 hover:bg-hover");
  144. expect(html).not.toContain("--windows-caption-control-width");
  145. });
  146. it("does not render the retired bulk session stress menu entry", async () => {
  147. const html = await renderButton("header");
  148. // Bugfix: Windows 标题栏使用自绘合并菜单,原生菜单删掉后这里也不能残留旧压测入口。
  149. expect(html).not.toContain("titleBar.menu.help.toggleBulkSessionStressTest");
  150. });
  151. it("Preview 隐藏更新入口,生产版继续保留", async () => {
  152. const { shouldShowDesktopUpdateEntry } = await import("@/WindowsCaptionMenuButton.js");
  153. // 更新入口跟随产品身份:生产后端的 Preview 同样隐藏。
  154. expect(shouldShowDesktopUpdateEntry("preview")).toBe(false);
  155. expect(shouldShowDesktopUpdateEntry("production")).toBe(true);
  156. const html = await renderButton("header");
  157. expect(html).not.toContain("titleBar.menu.help.checkForUpdates");
  158. });
  159. it("includes help actions migrated from the shared question-mark menu", async () => {
  160. capturedWindowsCaptionMenu.openSubmit.mockClear();
  161. capturedWindowsCaptionMenu.openFeatureRequest.mockClear();
  162. capturedWindowsCaptionMenu.openExternal.mockClear();
  163. capturedWindowsCaptionMenu.exportLogs.mockClear();
  164. capturedWindowsCaptionMenu.captureWindowScreenshot.mockClear();
  165. const html = await renderButton("header");
  166. expect(html).toContain("titleBar.menu.help.feedback");
  167. expect(html).toContain("workspaceHeader.help.productRequest");
  168. expect(html).toContain("workspaceHeader.help.docs");
  169. expect(html).toContain("titleBar.menu.help.exportLogs");
  170. await capturedWindowsCaptionMenu.menuItems
  171. .find((item) => item.label === "titleBar.menu.help.feedback")
  172. ?.onSelect?.();
  173. capturedWindowsCaptionMenu.menuItems
  174. .find((item) => item.label === "workspaceHeader.help.productRequest")
  175. ?.onSelect?.();
  176. capturedWindowsCaptionMenu.menuItems
  177. .find((item) => item.label === "workspaceHeader.help.docs")
  178. ?.onSelect?.();
  179. capturedWindowsCaptionMenu.menuItems
  180. .find((item) => item.label === "titleBar.menu.help.exportLogs")
  181. ?.onSelect?.();
  182. expect(capturedWindowsCaptionMenu.captureWindowScreenshot).not.toHaveBeenCalled();
  183. expect(capturedWindowsCaptionMenu.openSubmit).toHaveBeenCalledWith(
  184. expect.objectContaining({
  185. type: "bug",
  186. includeLogs: true,
  187. screenshots: [],
  188. }),
  189. );
  190. expect(capturedWindowsCaptionMenu.openFeatureRequest).toHaveBeenCalledTimes(1);
  191. expect(capturedWindowsCaptionMenu.openExternal).toHaveBeenCalledWith("https://zcode.z.ai/docs");
  192. expect(capturedWindowsCaptionMenu.exportLogs).toHaveBeenCalledTimes(1);
  193. });
  194. it("keeps the menu surface clear of the native Windows caption buttons", async () => {
  195. await renderButton("header");
  196. // Bugfix: Windows 原生 titleBarOverlay 宽度存在 DPI/取整差异,右对齐菜单需要轻微左移避开最小化按钮。
  197. expect(capturedDropdownContentProps.calls[0]).toMatchObject({
  198. align: "end",
  199. alignOffset: -12,
  200. });
  201. });
  202. });