previewCardActions.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  4. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  5. import { OpenSplitButton, type OpenSplitButtonTarget } from "@/OpenSplitButton.js";
  6. import { toast } from "@/components/ui/toast.js";
  7. import type { OpenInEditorRemoteTarget } from "@zcode/shared";
  8. const state = vi.hoisted(() => ({
  9. native: true,
  10. remote: false,
  11. remoteTarget: undefined as OpenInEditorRemoteTarget | undefined,
  12. openInEditor: vi.fn(),
  13. openExternalFile: vi.fn(),
  14. openInFileManager: vi.fn(),
  15. getInstalledEditors: vi.fn(),
  16. openExternal: vi.fn(),
  17. writeText: vi.fn(),
  18. }));
  19. vi.mock("@/hooks/usePlatform.js", () => ({
  20. usePlatform: () => ({
  21. canOpenLocalFiles: state.native,
  22. openExternalFile: state.openExternalFile,
  23. openInFileManager: state.openInFileManager,
  24. getInstalledEditors: state.getInstalledEditors,
  25. openExternal: state.openExternal,
  26. openInEditor: state.openInEditor,
  27. }),
  28. }));
  29. vi.mock("@/hooks/useWorkspaceOpenInEditorTarget.js", () => ({
  30. useWorkspaceOpenInEditorTarget: () => ({
  31. isRemoteWorkspace: state.remote,
  32. remoteTarget: state.remoteTarget,
  33. }),
  34. }));
  35. vi.mock("@/i18n/IntlProvider.js", () => ({
  36. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  37. }));
  38. vi.mock("@/components/ui/toast.js", () => ({ toast: vi.fn() }));
  39. const file: OpenSplitButtonTarget = {
  40. type: "file",
  41. path: "/workspace/space name/report.pptx",
  42. title: "report.pptx",
  43. label: "report.pptx",
  44. previewSource: {
  45. type: "file",
  46. path: "/workspace/space name/report.pptx",
  47. title: "report.pptx",
  48. workspacePath: "/workspace",
  49. },
  50. };
  51. function mount(target: OpenSplitButtonTarget = file) {
  52. const preview = vi.fn();
  53. render(
  54. createElement(OpenSplitButton, {
  55. target,
  56. onOpenCodeViewer: preview,
  57. onOpenBrowserUrl: preview,
  58. renderCard: (button) =>
  59. createElement("div", { "data-testid": "card" }, "report.pptx", button),
  60. }),
  61. );
  62. return preview;
  63. }
  64. function openContext() {
  65. fireEvent.contextMenu(screen.getByTestId("card"), { button: 2, clientX: 30, clientY: 30 });
  66. }
  67. afterEach(cleanup);
  68. beforeEach(() => {
  69. vi.clearAllMocks();
  70. state.native = true;
  71. state.remote = false;
  72. state.remoteTarget = undefined;
  73. state.openInEditor.mockResolvedValue({ success: true });
  74. state.openExternalFile.mockResolvedValue({ success: true });
  75. state.openInFileManager.mockResolvedValue({ success: true });
  76. state.getInstalledEditors.mockResolvedValue([]);
  77. Object.defineProperty(navigator, "clipboard", {
  78. configurable: true,
  79. value: { writeText: state.writeText },
  80. });
  81. state.writeText.mockResolvedValue(undefined);
  82. });
  83. describe("preview card actions", () => {
  84. it("context menu previews the same scoped source as the primary button", () => {
  85. const preview = mount();
  86. openContext();
  87. fireEvent.click(screen.getByText("chat.previewCards.previewInSidePane"));
  88. expect(preview).toHaveBeenCalledWith(file.previewSource);
  89. });
  90. it("keeps the original Open button and flat dropdown while retaining context actions", async () => {
  91. mount();
  92. expect(screen.getByRole("button", { name: "common.open" })).toBeTruthy();
  93. openContext();
  94. await waitFor(() => expect(state.getInstalledEditors).toHaveBeenCalledWith(file.path));
  95. expect(screen.getByText("chat.previewCards.previewInSidePane")).toBeTruthy();
  96. fireEvent.keyDown(screen.getByRole("menu"), { key: "Escape" });
  97. fireEvent.pointerDown(screen.getByLabelText("appHeader.selectOpenApp"), {
  98. button: 0,
  99. ctrlKey: false,
  100. });
  101. await screen.findByText("chat.previewCards.noOpenApps");
  102. expect(screen.getAllByRole("menuitem").map((el) => el.textContent)).toEqual([
  103. "chat.previewCards.noOpenApps",
  104. "fileActions.copyAbsolutePath",
  105. "fileActions.copyRelativePath",
  106. ]);
  107. expect(screen.queryByText("chat.previewCards.previewInSidePane")).toBeNull();
  108. expect(screen.queryByText("chat.previewCards.openDefaultApp")).toBeNull();
  109. expect(screen.queryByText("chat.previewCards.openContainingDirectory")).toBeNull();
  110. });
  111. it("uses native file opening and omits the containing directory", async () => {
  112. mount();
  113. openContext();
  114. fireEvent.click(screen.getByText("chat.previewCards.openDefaultApp"));
  115. await waitFor(() => expect(state.openExternalFile).toHaveBeenCalledWith(file.path));
  116. openContext();
  117. expect(screen.queryByText("chat.previewCards.openContainingDirectory")).toBeNull();
  118. expect(screen.getAllByRole("menuitem").map((el) => el.textContent)).toEqual([
  119. "chat.previewCards.previewInSidePane",
  120. "chat.previewCards.openDefaultApp",
  121. "chat.previewCards.openWith",
  122. "fileActions.copyRelativePath",
  123. "fileActions.copyAbsolutePath",
  124. ]);
  125. expect(state.openInFileManager).not.toHaveBeenCalled();
  126. });
  127. it.each(["web", "remote"])("omits unsafe native actions on %s", (mode) => {
  128. state.native = mode !== "web";
  129. state.remote = mode === "remote";
  130. mount();
  131. openContext();
  132. expect(screen.queryByText("chat.previewCards.openDefaultApp")).toBeNull();
  133. expect(screen.queryByText("chat.previewCards.openContainingDirectory")).toBeNull();
  134. expect(screen.queryByText("chat.previewCards.openWith")).toBeNull();
  135. expect(screen.getByText("fileActions.copyAbsolutePath")).toBeTruthy();
  136. });
  137. it("mobile retains its existing Open button preview behavior", () => {
  138. state.native = false;
  139. const preview = mount();
  140. fireEvent.click(screen.getByRole("button", { name: "common.open" }));
  141. expect(preview).toHaveBeenCalledWith(file.previewSource);
  142. expect(state.getInstalledEditors).not.toHaveBeenCalled();
  143. });
  144. it("URL-only website opens the original browser URL", () => {
  145. const url = "http://localhost:5173/page?x=1";
  146. mount({ type: "website", url });
  147. openContext();
  148. fireEvent.click(screen.getByRole("menuitem", { name: "common.open" }));
  149. expect(state.openExternal).toHaveBeenCalledWith(url);
  150. expect(state.openExternalFile).not.toHaveBeenCalled();
  151. });
  152. it("file-backed HTML uses the local file browser route", () => {
  153. mount({
  154. type: "website",
  155. url: "file:///workspace/index.html",
  156. localPath: "/workspace/index.html",
  157. });
  158. openContext();
  159. fireEvent.click(screen.getByRole("menuitem", { name: "common.open" }));
  160. expect(state.openExternalFile).toHaveBeenCalledWith("/workspace/index.html");
  161. });
  162. it("copies website URLs and omits file actions for URL-only cards", async () => {
  163. mount({ type: "website", url: "http://localhost:5173/page?x=1" });
  164. openContext();
  165. fireEvent.click(screen.getByText("chat.previewCards.copyLink"));
  166. await waitFor(() =>
  167. expect(state.writeText).toHaveBeenCalledWith("http://localhost:5173/page?x=1"),
  168. );
  169. openContext();
  170. expect(screen.queryByText("fileActions.copyRelativePath")).toBeNull();
  171. });
  172. it("file-backed HTML omits containing-directory and retains path actions", async () => {
  173. mount({
  174. type: "website",
  175. url: "file:///workspace/index.html",
  176. localPath: "/workspace/index.html",
  177. workspacePath: "/workspace",
  178. });
  179. openContext();
  180. expect(screen.queryByText("chat.previewCards.openContainingDirectory")).toBeNull();
  181. fireEvent.click(screen.getByText("fileActions.copyRelativePath"));
  182. await waitFor(() => expect(state.writeText).toHaveBeenCalledWith("index.html"));
  183. });
  184. });
  185. describe("preview editor failures", () => {
  186. it.each(["result", "rejection"])(
  187. "shows a toast for %s failures after filtering SSH apps",
  188. async (failure) => {
  189. state.remote = true;
  190. state.remoteTarget = {
  191. kind: "ssh",
  192. host: "example.com",
  193. username: "dev",
  194. port: 22,
  195. } as OpenInEditorRemoteTarget;
  196. state.getInstalledEditors.mockResolvedValue([
  197. { id: "vscode", name: "VS Code", iconDataUrl: "" },
  198. { id: "word", name: "Word", iconDataUrl: "" },
  199. ]);
  200. if (failure === "result")
  201. state.openInEditor.mockResolvedValue({ success: false, error: "failed" });
  202. else state.openInEditor.mockRejectedValue(new Error("failed"));
  203. mount();
  204. fireEvent.pointerDown(screen.getByLabelText("appHeader.selectOpenApp"), {
  205. button: 0,
  206. ctrlKey: false,
  207. });
  208. await screen.findByRole("menuitem", { name: /VS Code/ });
  209. expect(screen.queryByRole("menuitem", { name: /Word/ })).toBeNull();
  210. fireEvent.click(screen.getByRole("menuitem", { name: /VS Code/ }));
  211. await waitFor(() => expect(toast).toHaveBeenCalledWith("chat.previewCards.openFileFailed"));
  212. expect(state.openInEditor).toHaveBeenCalledWith(
  213. "vscode",
  214. file.path,
  215. expect.objectContaining({ remoteTarget: state.remoteTarget }),
  216. );
  217. },
  218. );
  219. });