linuxDesktopTitlebarOverlay.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { readFileSync } from "node:fs";
  2. import { createElement, type ReactElement, type ReactNode } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { describe, expect, it, vi } from "vitest";
  5. import { TooltipProvider } from "@/components/ui/tooltip.js";
  6. vi.mock("radix-ui", async () => {
  7. const actual = await vi.importActual<typeof import("radix-ui")>("radix-ui");
  8. return {
  9. ...actual,
  10. Dialog: {
  11. Root: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  12. Trigger: ({ children, ...props }: { children: ReactNode }) =>
  13. createElement("button", props, children),
  14. Portal: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  15. Overlay: ({ children, ...props }: { children?: ReactNode }) =>
  16. createElement("div", props, children),
  17. Content: ({ children, ...props }: { children: ReactNode }) =>
  18. createElement("section", props, children),
  19. Title: ({ children, ...props }: { children: ReactNode }) =>
  20. createElement("h2", props, children),
  21. Description: ({ children, ...props }: { children: ReactNode }) =>
  22. createElement("p", props, children),
  23. Close: ({ children }: { children: ReactNode }) => createElement("span", null, children),
  24. },
  25. };
  26. });
  27. vi.mock("@/i18n/IntlProvider.js", () => ({
  28. useZCodeIntl: () => ({
  29. intl: {
  30. formatMessage: ({ id }: { id: string }) => id,
  31. },
  32. }),
  33. }));
  34. vi.mock("@/hooks/useGlobalTaskList.js", () => ({
  35. useGlobalTaskList: () => ({
  36. items: [],
  37. loading: false,
  38. }),
  39. }));
  40. vi.mock("@/hooks/useServices.js", () => ({
  41. useServices: () => ({
  42. fileService: {
  43. listWorkspaceFiles: vi.fn(async () => []),
  44. },
  45. }),
  46. }));
  47. function renderTaskFindDialog(options: {
  48. isLinuxDesktop?: boolean;
  49. isMacDesktop?: boolean;
  50. isWindowsDesktop?: boolean;
  51. }) {
  52. return import("@/quickpick/TaskFindDialog.js").then(({ TaskFindDialog }) =>
  53. renderWithTooltipProvider(
  54. createElement(TaskFindDialog, {
  55. open: true,
  56. focusRequestId: 0,
  57. conversationMatchCount: 0,
  58. conversationMatchIndex: -1,
  59. fileChangeMatchCount: 0,
  60. fileChangeMatchIndex: -1,
  61. onConversationFindChange: vi.fn(),
  62. onConversationFindNavigate: vi.fn(),
  63. onFileChangeFindChange: vi.fn(),
  64. onFileChangeFindNavigate: vi.fn(),
  65. onOpenChange: vi.fn(),
  66. onOpenFileChanges: vi.fn(),
  67. ...options,
  68. }),
  69. ),
  70. );
  71. }
  72. function renderCommandCenterDialog() {
  73. return import("@/command-center/CommandCenterDialog.js").then(({ CommandCenterDialog }) =>
  74. renderWithTooltipProvider(
  75. createElement(CommandCenterDialog, {
  76. open: true,
  77. commands: [],
  78. workspaceAbsPath: "/workspace",
  79. workspaceTabs: [],
  80. onOpenChange: vi.fn(),
  81. onSelectTask: vi.fn(),
  82. onOpenCodeViewer: vi.fn(),
  83. }),
  84. ),
  85. );
  86. }
  87. function renderWithTooltipProvider(element: ReactElement) {
  88. return renderToStaticMarkup(createElement(TooltipProvider, null, element));
  89. }
  90. describe("Desktop titlebar overlay guards", () => {
  91. it("registers the Linux desktop Tailwind variant used by shared overlays", () => {
  92. const styles = readFileSync("packages/ui/src/styles.css", "utf8");
  93. expect(styles).toContain("@custom-variant platform-linux-desktop");
  94. expect(styles).toContain(".platform-linux-desktop");
  95. });
  96. it("registers the Windows desktop variant and renderer root marker for caption-safe overlays", () => {
  97. const styles = readFileSync("packages/ui/src/styles.css", "utf8");
  98. const desktopRendererSource = readFileSync(
  99. "packages/desktop/src/renderer/src/main.tsx",
  100. "utf8",
  101. );
  102. expect(styles).toContain("@custom-variant platform-windows-desktop");
  103. expect(styles).toContain(".platform-windows-desktop");
  104. expect(desktopRendererSource).toContain(
  105. 'document.documentElement.classList.toggle("platform-windows-desktop", isWindowsDesktop)',
  106. );
  107. });
  108. it("registers the macOS desktop variant and renderer root marker for native-titlebar-safe overlays", () => {
  109. const styles = readFileSync("packages/ui/src/styles.css", "utf8");
  110. const desktopRendererSource = readFileSync(
  111. "packages/desktop/src/renderer/src/main.tsx",
  112. "utf8",
  113. );
  114. expect(styles).toContain("@custom-variant platform-mac-desktop");
  115. expect(styles).toContain(".platform-mac-desktop");
  116. expect(desktopRendererSource).toContain(
  117. 'document.documentElement.classList.toggle("platform-mac-desktop", isMacDesktop)',
  118. );
  119. });
  120. it("keeps Linux modal overlays full-window and vertically centered", () => {
  121. const dialogSource = readFileSync("packages/ui/src/components/ui/dialog.tsx", "utf8");
  122. const alertDialogSource = readFileSync(
  123. "packages/ui/src/components/ui/alert-dialog.tsx",
  124. "utf8",
  125. );
  126. expect(dialogSource).not.toContain("platform-linux-desktop:top-12");
  127. expect(dialogSource).not.toContain("platform-linux-desktop:top-[calc(50%+1.5rem)]");
  128. expect(alertDialogSource).not.toContain("platform-linux-desktop:top-12");
  129. expect(alertDialogSource).not.toContain("platform-linux-desktop:top-[calc(50%+1.5rem)]");
  130. });
  131. it("keeps image preview actions inset below desktop window controls", () => {
  132. const imagePreviewSource = readFileSync(
  133. "packages/ui/src/components/ai-elements/image-preview-dialog.tsx",
  134. "utf8",
  135. );
  136. // Bugfix: Windows caption controls占据右上角;Linux 预览内容虽已整体避开自绘标题栏,
  137. // action row 仍需显式保留平台 inset,避免后续全屏布局重构把按钮重新推回窗控区。
  138. expect(imagePreviewSource).toContain("platform-linux-desktop:right-6");
  139. expect(imagePreviewSource).toContain("platform-linux-desktop:top-4");
  140. expect(imagePreviewSource).toContain("platform-mac-desktop:top-12");
  141. expect(imagePreviewSource).toContain("platform-windows-desktop:right-6");
  142. expect(imagePreviewSource).toContain(
  143. "platform-windows-desktop:top-[calc(env(titlebar-area-height,48px)_+_0.5rem)]",
  144. );
  145. });
  146. it("keeps the command center top-positioned after CommandDialog class merging", async () => {
  147. const html = await renderCommandCenterDialog();
  148. // Bugfix: Command Center 不是居中弹窗,不能继承 DialogContent 的 Linux 居中补偿。
  149. expect(html).toContain("top-16");
  150. expect(html).toContain("sm:top-20");
  151. expect(html).toContain("platform-linux-desktop:top-16");
  152. expect(html).toContain("sm:platform-linux-desktop:top-20");
  153. expect(html).not.toContain("platform-linux-desktop:top-[calc(50%+1.5rem)]");
  154. });
  155. it("moves the task find popup out of the Linux titlebar and window controls", async () => {
  156. const linuxHtml = await renderTaskFindDialog({ isLinuxDesktop: true });
  157. const webHtml = await renderTaskFindDialog({});
  158. // Bugfix: Linux 的查找浮层不能贴住 top-3,否则会挡住 renderer 自绘标题栏点击区。
  159. expect(linuxHtml).toContain("top-12 right-[120px]");
  160. expect(webHtml).toContain("top-3 right-3");
  161. expect(webHtml).not.toContain("right-[120px]");
  162. });
  163. });