remoteConnectionDialogContentLayout.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // @vitest-environment jsdom
  2. import { createElement, useState } from "react";
  3. import { cleanup, fireEvent, render } from "@testing-library/react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import type { RemoteTarget } from "@zcode/shared";
  7. const capturedRemoteSyncDialogProps = vi.hoisted(() => [] as Array<Record<string, unknown>>);
  8. vi.mock("@/i18n/IntlProvider.js", () => ({
  9. useZCodeIntl: () => ({
  10. intl: {
  11. formatMessage: ({ id }: { id: string }) => id,
  12. },
  13. }),
  14. }));
  15. vi.mock("@/DirectoryBrowser.js", () => ({
  16. DirectoryBrowser: () => createElement("div", { "data-testid": "directory-browser" }),
  17. }));
  18. vi.mock("@/RemoteConnectionFields.js", () => ({
  19. RemoteConnectionFields: () => createElement("div", { "data-testid": "remote-connection-fields" }),
  20. }));
  21. vi.mock("@/settings/RemoteSyncActions.js", () => ({
  22. RemoteSyncDialogs: (props: Record<string, unknown>) => {
  23. capturedRemoteSyncDialogProps.push(props);
  24. return createElement("div", { "data-testid": "remote-sync-dialogs" });
  25. },
  26. RemoteSyncDropdownButton: () =>
  27. createElement("button", { type: "button" }, "settings.remoteSync.open"),
  28. shouldShowRemoteSyncActions: ({
  29. remoteSessionId,
  30. remoteTarget,
  31. }: {
  32. remoteSessionId?: string | null;
  33. remoteTarget?: { kind: string } | null;
  34. }) => Boolean(remoteSessionId?.trim() && ["ssh", "wsl"].includes(remoteTarget?.kind ?? "")),
  35. }));
  36. import {
  37. RemoteConnectionDirectoryStep,
  38. RemoteConnectionKindStep,
  39. RemoteConnectionSettingsStep,
  40. } from "@/RemoteConnectionDialogContent.js";
  41. import { buildAvailableKinds } from "@/hooks/useRemoteConnectionForm.js";
  42. afterEach(cleanup);
  43. const allKinds: RemoteTarget["kind"][] = ["ssh", "server", "wsl", "docker"];
  44. function KindStepHarness() {
  45. const [kind, setKind] = useState<RemoteTarget["kind"]>("ssh");
  46. const [settings, setSettings] = useState(false);
  47. return settings
  48. ? createElement("div", { "data-testid": "settings-kind" }, kind)
  49. : createElement(RemoteConnectionKindStep, {
  50. kind,
  51. availableKinds: allKinds,
  52. onKindChange: setKind,
  53. onNext: () => setSettings(true),
  54. onCancel: vi.fn(),
  55. });
  56. }
  57. describe("RemoteConnectionKindStep interactions", () => {
  58. it.each(allKinds)("double-click opens settings for %s", (kind) => {
  59. const view = render(createElement(KindStepHarness));
  60. const card = view.getByRole("button", {
  61. name: `remote.kind.${kind} remote.kind.${kind}.wizardDescription`,
  62. });
  63. fireEvent.click(card, { detail: 1 });
  64. expect(view.queryByTestId("settings-kind")).toBeNull();
  65. fireEvent.click(card, { detail: 2 });
  66. fireEvent.doubleClick(card, { detail: 2 });
  67. expect(view.getByTestId("settings-kind").textContent).toBe(kind);
  68. });
  69. it.each(allKinds)("single-click keeps %s selected until Next", (kind) => {
  70. const view = render(createElement(KindStepHarness));
  71. const card = view.getByRole("button", {
  72. name: `remote.kind.${kind} remote.kind.${kind}.wizardDescription`,
  73. });
  74. fireEvent.click(card);
  75. expect(view.queryByTestId("settings-kind")).toBeNull();
  76. expect(card.classList.contains("bg-selected")).toBe(true);
  77. fireEvent.click(view.getByRole("button", { name: "common.next" }));
  78. expect(view.getByTestId("settings-kind").textContent).toBe(kind);
  79. });
  80. });
  81. describe("RemoteConnectionDialogContent layout", () => {
  82. it("shows Server between SSH and Windows-only kinds in local development", () => {
  83. expect(
  84. buildAvailableKinds({
  85. isWindowsDesktop: true,
  86. isLocalDevelopmentRuntime: true,
  87. }),
  88. ).toEqual(["ssh", "server", "wsl", "docker"]);
  89. });
  90. it("hides Server from production-mode packaged runtimes", () => {
  91. expect(
  92. buildAvailableKinds({
  93. isWindowsDesktop: true,
  94. isLocalDevelopmentRuntime: false,
  95. }),
  96. ).toEqual(["ssh", "wsl", "docker"]);
  97. expect(
  98. buildAvailableKinds({
  99. isWindowsDesktop: false,
  100. isLocalDevelopmentRuntime: false,
  101. }),
  102. ).toEqual(["ssh", "docker"]);
  103. });
  104. it("renders the kind chooser as compact tiles", () => {
  105. const html = renderToStaticMarkup(
  106. createElement(RemoteConnectionKindStep, {
  107. kind: "ssh",
  108. availableKinds: ["ssh", "server", "wsl", "docker"],
  109. onKindChange: vi.fn(),
  110. onCancel: vi.fn(),
  111. onNext: vi.fn(),
  112. }),
  113. );
  114. expect(html).toContain("md:grid-cols-2");
  115. expect(html).not.toContain("lg:grid-cols-3");
  116. expect(html).toContain("min-h-32");
  117. expect(html).toContain("text-ui-lg font-medium");
  118. });
  119. it("keeps the settings fields in their own scroll area above footer actions", () => {
  120. const html = renderToStaticMarkup(
  121. createElement(RemoteConnectionSettingsStep, {
  122. kind: "ssh",
  123. host: "example.com",
  124. port: "22",
  125. username: "user",
  126. sshAuthMethod: "privateKey",
  127. assetInstallMode: "local",
  128. password: "",
  129. privateKeyPath: "/Users/test/.ssh/id_rsa",
  130. privateKeyPassphrase: "",
  131. wslDistro: "",
  132. wslDistros: [],
  133. dockerContainer: "",
  134. manualDockerContainer: "",
  135. serverUrl: "",
  136. serverName: "",
  137. serverToken: "",
  138. serverWorkspacePath: "",
  139. dockerContainers: [],
  140. dockerAvailable: null,
  141. sshConfigAliases: [],
  142. sshConfigAliasesLoading: false,
  143. sshConfigAliasesError: "",
  144. selectedSshConfigAlias: null,
  145. currentRuntimeOptionsLoading: false,
  146. currentRuntimeOptionsError: "",
  147. remoteWorkspaceSessions: [],
  148. validationMessage: "",
  149. loading: false,
  150. onBack: vi.fn(),
  151. onHostChange: vi.fn(),
  152. onPortChange: vi.fn(),
  153. onUsernameChange: vi.fn(),
  154. onSshAuthMethodChange: vi.fn(),
  155. onAssetInstallModeChange: vi.fn(),
  156. onPasswordChange: vi.fn(),
  157. onPrivateKeyPathChange: vi.fn(),
  158. onPrivateKeyPassphraseChange: vi.fn(),
  159. onWslDistroChange: vi.fn(),
  160. onDockerContainerChange: vi.fn(),
  161. onManualDockerContainerChange: vi.fn(),
  162. onServerUrlChange: vi.fn(),
  163. onServerNameChange: vi.fn(),
  164. onServerTokenChange: vi.fn(),
  165. onServerWorkspacePathChange: vi.fn(),
  166. onApplySshConfigAlias: vi.fn(),
  167. onClearSelectedSshConfigAlias: vi.fn(),
  168. onConnect: vi.fn(),
  169. }),
  170. );
  171. expect(html).toContain('data-testid="remote-connection-settings-scroll"');
  172. expect(html).toContain("overflow-y-auto");
  173. });
  174. it("shows progress feedback while finalizing a selected remote directory", () => {
  175. const html = renderToStaticMarkup(
  176. createElement(RemoteConnectionDirectoryStep, {
  177. services: {} as never,
  178. selecting: true,
  179. onSelect: vi.fn(),
  180. onBack: vi.fn(),
  181. onCancel: vi.fn(),
  182. }),
  183. );
  184. expect(html).toContain("common.loading");
  185. expect(html).toContain("animate-spin");
  186. });
  187. it("shows one sync dropdown action after connecting to an SSH host", () => {
  188. const html = renderToStaticMarkup(
  189. createElement(RemoteConnectionDirectoryStep, {
  190. services: {} as never,
  191. remoteTarget: {
  192. kind: "ssh",
  193. host: "localhost",
  194. port: 2223,
  195. username: "root",
  196. },
  197. localSkillSyncService: {} as never,
  198. remoteSkillSyncService: {} as never,
  199. localMcpSyncService: {} as never,
  200. remoteMcpSyncService: {} as never,
  201. onSelect: vi.fn(),
  202. onBack: vi.fn(),
  203. onCancel: vi.fn(),
  204. }),
  205. );
  206. expect(html).toContain("settings.remoteSync.open");
  207. expect(html).not.toContain("settings.skills.remoteSync.open");
  208. expect(html).not.toContain("settings.mcp.remoteSync.open");
  209. });
  210. it("shows one sync dropdown action after connecting to a WSL distro", () => {
  211. const html = renderToStaticMarkup(
  212. createElement(RemoteConnectionDirectoryStep, {
  213. services: {} as never,
  214. remoteTarget: {
  215. kind: "wsl",
  216. distro: "Ubuntu",
  217. user: "alice",
  218. },
  219. localSkillSyncService: {} as never,
  220. remoteSkillSyncService: {} as never,
  221. localMcpSyncService: {} as never,
  222. remoteMcpSyncService: {} as never,
  223. onSelect: vi.fn(),
  224. onBack: vi.fn(),
  225. onCancel: vi.fn(),
  226. }),
  227. );
  228. expect(html).toContain("settings.remoteSync.open");
  229. expect(html).not.toContain("settings.skills.remoteSync.open");
  230. expect(html).not.toContain("settings.mcp.remoteSync.open");
  231. });
  232. it("passes the local workspace path to MCP sync from the directory step", () => {
  233. capturedRemoteSyncDialogProps.length = 0;
  234. renderToStaticMarkup(
  235. createElement(RemoteConnectionDirectoryStep, {
  236. services: {} as never,
  237. remoteTarget: {
  238. kind: "ssh",
  239. host: "localhost",
  240. port: 2223,
  241. username: "root",
  242. },
  243. localWorkspacePath: "/Users/alice/project",
  244. localMcpSyncService: {} as never,
  245. remoteMcpSyncService: {} as never,
  246. onSelect: vi.fn(),
  247. onBack: vi.fn(),
  248. onCancel: vi.fn(),
  249. }),
  250. );
  251. expect(capturedRemoteSyncDialogProps[0]?.mcpLocalWorkspacePath).toBe("/Users/alice/project");
  252. });
  253. });