remoteSyncActions.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // @vitest-environment jsdom
  2. import { createElement, type ReactNode } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { act, renderHook } from "@testing-library/react";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. const { capturedSkillDialogProps, capturedMcpDialogProps, capturedPluginDialogProps } = vi.hoisted(() => ({
  7. capturedSkillDialogProps: [] as Array<Record<string, unknown>>,
  8. capturedMcpDialogProps: [] as Array<Record<string, unknown>>,
  9. capturedPluginDialogProps: [] as Array<Record<string, unknown>>,
  10. }));
  11. vi.mock("lucide-react", () => {
  12. const createIcon = (name: string) => (props: Record<string, unknown>) =>
  13. createElement("svg", { "data-icon": name, ...props });
  14. return {
  15. ChevronDown: createIcon("chevron-down"),
  16. UploadCloud: createIcon("upload-cloud"),
  17. };
  18. });
  19. vi.mock("@/components/ui/button.js", () => ({
  20. Button: ({
  21. children,
  22. ...props
  23. }: {
  24. children: ReactNode;
  25. }) => createElement("button", props, children),
  26. }));
  27. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  28. DropdownMenu: ({ children }: { children: ReactNode }) =>
  29. createElement("div", { "data-testid": "dropdown-menu" }, children),
  30. DropdownMenuContent: ({
  31. children,
  32. ...props
  33. }: {
  34. children: ReactNode;
  35. }) => createElement("div", { "data-testid": "dropdown-menu-content", ...props }, children),
  36. DropdownMenuItem: ({
  37. children,
  38. disabled,
  39. ...props
  40. }: {
  41. children: ReactNode;
  42. disabled?: boolean;
  43. }) =>
  44. createElement(
  45. "div",
  46. {
  47. "data-disabled": disabled ? "true" : undefined,
  48. "data-testid": "dropdown-menu-item",
  49. ...props,
  50. },
  51. children,
  52. ),
  53. DropdownMenuTrigger: ({ children }: { children: ReactNode }) => children,
  54. }));
  55. vi.mock("@/i18n/IntlProvider.js", () => ({
  56. useZCodeIntl: () => ({
  57. intl: {
  58. formatMessage: ({ id }: { id: string }) => {
  59. const messages: Record<string, string> = {
  60. "settings.skills.remoteSync.open": "同步 Skill",
  61. "settings.mcp.remoteSync.open": "同步 MCP",
  62. "settings.plugins.remoteSync.open": "同步 Plugin",
  63. "settings.remoteSync.open": "同步",
  64. };
  65. return messages[id] ?? id;
  66. },
  67. },
  68. }),
  69. }));
  70. vi.mock("@/settings/RemoteSkillSyncDialog.js", () => ({
  71. RemoteSkillSyncDialog: (props: Record<string, unknown>) => {
  72. capturedSkillDialogProps.push(props);
  73. return createElement("div", { "data-testid": "remote-skill-sync-dialog" });
  74. },
  75. }));
  76. vi.mock("@/settings/RemoteMcpSyncDialog.js", () => ({
  77. RemoteMcpSyncDialog: (props: Record<string, unknown>) => {
  78. capturedMcpDialogProps.push(props);
  79. return createElement("div", { "data-testid": "remote-mcp-sync-dialog" });
  80. },
  81. }));
  82. vi.mock("@/settings/RemotePluginSyncDialog.js", () => ({
  83. RemotePluginSyncDialog: (props: Record<string, unknown>) => {
  84. capturedPluginDialogProps.push(props);
  85. return createElement("div", { "data-testid": "remote-plugin-sync-dialog" });
  86. },
  87. }));
  88. afterEach(() => {
  89. vi.useRealTimers();
  90. });
  91. describe("RemoteSyncActions", () => {
  92. it("drops dialog intent across disconnects and remote target changes", async () => {
  93. const { useRemoteSyncDialogIntent } = await import(
  94. "@/settings/RemoteSyncActions.js"
  95. );
  96. const hook = renderHook(
  97. ({ rpcReady, targetKey }) =>
  98. useRemoteSyncDialogIntent({ rpcReady, targetKey }),
  99. {
  100. initialProps: {
  101. rpcReady: true,
  102. targetKey: "remote-a:session-1",
  103. },
  104. },
  105. );
  106. act(() => hook.result.current.setOpen(true));
  107. expect(hook.result.current.open).toBe(true);
  108. hook.rerender({ rpcReady: false, targetKey: "remote-a:session-1" });
  109. expect(hook.result.current.open).toBe(false);
  110. hook.rerender({ rpcReady: true, targetKey: "remote-a:session-2" });
  111. expect(hook.result.current.open).toBe(false);
  112. act(() => hook.result.current.setOpen(true));
  113. expect(hook.result.current.open).toBe(true);
  114. hook.rerender({ rpcReady: true, targetKey: "remote-b:session-3" });
  115. expect(hook.result.current.open).toBe(false);
  116. });
  117. it("shows remote sync actions for SSH and WSL sessions only", async () => {
  118. const { shouldShowRemoteSyncActions } = await import("@/settings/RemoteSyncActions.js");
  119. expect(
  120. shouldShowRemoteSyncActions({
  121. remoteSessionId: "session-ssh",
  122. remoteTarget: {
  123. kind: "ssh",
  124. host: "localhost",
  125. port: 2223,
  126. username: "root",
  127. },
  128. }),
  129. ).toBe(true);
  130. expect(
  131. shouldShowRemoteSyncActions({
  132. remoteSessionId: "session-wsl",
  133. remoteTarget: {
  134. kind: "wsl",
  135. distro: "Ubuntu",
  136. user: "alice",
  137. },
  138. }),
  139. ).toBe(true);
  140. expect(
  141. shouldShowRemoteSyncActions({
  142. remoteSessionId: "session-docker",
  143. remoteTarget: {
  144. kind: "docker",
  145. container: "devbox",
  146. },
  147. }),
  148. ).toBe(false);
  149. expect(
  150. shouldShowRemoteSyncActions({
  151. remoteSessionId: null,
  152. remoteTarget: {
  153. kind: "wsl",
  154. distro: "Ubuntu",
  155. },
  156. }),
  157. ).toBe(false);
  158. });
  159. it("hides remote sync actions in Web remote replayable contexts", async () => {
  160. const { shouldShowRemoteSyncActions } = await import("@/settings/RemoteSyncActions.js");
  161. expect(
  162. shouldShowRemoteSyncActions({
  163. remoteSessionId: "session-wsl",
  164. remoteTarget: {
  165. kind: "wsl",
  166. distro: "Ubuntu",
  167. user: "alice",
  168. },
  169. clientMode: "web-remote-replayable",
  170. hasLocalSourceService: true,
  171. }),
  172. ).toBe(false);
  173. });
  174. it("hides remote sync actions when the desktop local source service is unavailable", async () => {
  175. const { shouldShowRemoteSyncActions } = await import("@/settings/RemoteSyncActions.js");
  176. expect(
  177. shouldShowRemoteSyncActions({
  178. remoteSessionId: "session-wsl",
  179. remoteTarget: {
  180. kind: "wsl",
  181. distro: "Ubuntu",
  182. },
  183. clientMode: "desktop-continuous",
  184. hasLocalSourceService: false,
  185. }),
  186. ).toBe(false);
  187. });
  188. it("allows starting a remote sync operation only when selection exists and no run is active", async () => {
  189. const { shouldStartRemoteSyncOperation } = await import("@/settings/RemoteSyncActions.js");
  190. expect(shouldStartRemoteSyncOperation({ inFlight: false, selectedCount: 1 })).toBe(true);
  191. expect(shouldStartRemoteSyncOperation({ inFlight: true, selectedCount: 1 })).toBe(false);
  192. expect(shouldStartRemoteSyncOperation({ inFlight: false, selectedCount: 0 })).toBe(false);
  193. });
  194. it("times out a remote sync preflight that never resolves", async () => {
  195. vi.useFakeTimers();
  196. const {
  197. isRemoteSyncPreflightTimeoutError,
  198. REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS,
  199. runRemoteSyncPreflightWithTimeout,
  200. } = await import("@/settings/RemoteSyncActions.js");
  201. const result = runRemoteSyncPreflightWithTimeout(
  202. () => new Promise<string>(() => undefined),
  203. ).catch((error: unknown) => error);
  204. await vi.advanceTimersByTimeAsync(REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS);
  205. const error = await result;
  206. expect(isRemoteSyncPreflightTimeoutError(error)).toBe(true);
  207. expect(error).toMatchObject({
  208. timeoutMs: REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS,
  209. });
  210. });
  211. it("clears the remote sync preflight timeout after a successful response", async () => {
  212. vi.useFakeTimers();
  213. const { REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS, runRemoteSyncPreflightWithTimeout } =
  214. await import("@/settings/RemoteSyncActions.js");
  215. await expect(
  216. runRemoteSyncPreflightWithTimeout(async () => "ok"),
  217. ).resolves.toBe("ok");
  218. await vi.advanceTimersByTimeAsync(REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS);
  219. });
  220. it("renders Skill, MCP, and Plugin menu actions for SSH remote contexts", async () => {
  221. const { RemoteSyncMenuItems } = await import("@/settings/RemoteSyncActions.js");
  222. const html = renderToStaticMarkup(
  223. createElement(RemoteSyncMenuItems, {
  224. canSyncSkills: true,
  225. canSyncMcp: true,
  226. canSyncPlugins: true,
  227. onOpenSkillSync: vi.fn(),
  228. onOpenMcpSync: vi.fn(),
  229. onOpenPluginSync: vi.fn(),
  230. }),
  231. );
  232. expect(html).toContain("同步 Skill");
  233. expect(html).toContain("同步 MCP");
  234. expect(html).toContain("同步 Plugin");
  235. expect(html.match(/data-icon="upload-cloud"/g)?.length).toBe(3);
  236. });
  237. it("renders one sync dropdown button with Skill, MCP, and Plugin actions", async () => {
  238. const { RemoteSyncDropdownButton } = await import("@/settings/RemoteSyncActions.js");
  239. const html = renderToStaticMarkup(
  240. createElement(RemoteSyncDropdownButton, {
  241. canSyncSkills: true,
  242. canSyncMcp: true,
  243. canSyncPlugins: true,
  244. onOpenSkillSync: vi.fn(),
  245. onOpenMcpSync: vi.fn(),
  246. onOpenPluginSync: vi.fn(),
  247. }),
  248. );
  249. expect(html).toContain("同步");
  250. expect(html).toContain("同步 Skill");
  251. expect(html).toContain("同步 MCP");
  252. expect(html).toContain("同步 Plugin");
  253. expect(html.match(/data-testid="dropdown-menu-item"/g)?.length).toBe(3);
  254. expect(html.match(/data-icon="upload-cloud"/g)?.length).toBe(4);
  255. expect(html).toContain('data-icon="chevron-down"');
  256. });
  257. it("keeps the MCP dropdown action disabled until a remote workspace path exists", async () => {
  258. const { RemoteSyncDropdownButton } = await import("@/settings/RemoteSyncActions.js");
  259. const html = renderToStaticMarkup(
  260. createElement(RemoteSyncDropdownButton, {
  261. canSyncSkills: true,
  262. canSyncMcp: true,
  263. canSyncPlugins: true,
  264. mcpDisabled: true,
  265. onOpenSkillSync: vi.fn(),
  266. onOpenMcpSync: vi.fn(),
  267. onOpenPluginSync: vi.fn(),
  268. }),
  269. );
  270. expect(html).toContain("同步 Skill");
  271. expect(html).toContain("同步 MCP");
  272. expect(html).toContain("同步 Plugin");
  273. expect(html).toContain('data-disabled="true"');
  274. });
  275. it("mounts all remote sync dialogs while preserving distinct workspace paths", async () => {
  276. const { RemoteSyncDialogs } = await import("@/settings/RemoteSyncActions.js");
  277. capturedSkillDialogProps.length = 0;
  278. capturedMcpDialogProps.length = 0;
  279. capturedPluginDialogProps.length = 0;
  280. const localZCodeAgentService = {};
  281. const remoteZCodeAgentService = {};
  282. renderToStaticMarkup(
  283. createElement(RemoteSyncDialogs, {
  284. canSyncSkills: true,
  285. canSyncMcp: true,
  286. canSyncPlugins: true,
  287. skillOpen: true,
  288. mcpOpen: true,
  289. pluginOpen: true,
  290. onSkillOpenChange: vi.fn(),
  291. onMcpOpenChange: vi.fn(),
  292. onPluginOpenChange: vi.fn(),
  293. localSkillSyncService: {} as never,
  294. remoteSkillSyncService: {} as never,
  295. localMcpSyncService: {} as never,
  296. remoteMcpSyncService: {} as never,
  297. localPluginSyncService: {} as never,
  298. remotePluginSyncService: {} as never,
  299. localZCodeAgentService: localZCodeAgentService as never,
  300. remoteZCodeAgentService: remoteZCodeAgentService as never,
  301. remoteTarget: {
  302. kind: "ssh",
  303. host: "localhost",
  304. port: 2223,
  305. username: "root",
  306. },
  307. skillWorkspacePath: "",
  308. mcpWorkspacePath: "/root/folder1",
  309. pluginWorkspacePath: "/root/folder1",
  310. pluginLocalWorkspacePath: "/Users/me/folder1",
  311. mcpLocalWorkspacePath: "/Users/me/folder1",
  312. workspaceIdentity: "remote:ssh:localhost:2223:root:/root/folder1",
  313. onSkillsSynced: vi.fn(),
  314. onMcpSynced: vi.fn(),
  315. onPluginsSynced: vi.fn(),
  316. }),
  317. );
  318. expect(capturedSkillDialogProps).toHaveLength(1);
  319. expect(capturedMcpDialogProps).toHaveLength(1);
  320. expect(capturedPluginDialogProps).toHaveLength(1);
  321. expect(capturedSkillDialogProps[0]?.workspacePath).toBe("");
  322. expect(capturedMcpDialogProps[0]?.workspacePath).toBe("/root/folder1");
  323. expect(capturedPluginDialogProps[0]?.workspacePath).toBe("/root/folder1");
  324. expect(capturedPluginDialogProps[0]?.localWorkspacePath).toBe("/Users/me/folder1");
  325. expect(capturedPluginDialogProps[0]?.localZCodeAgentService).toBe(localZCodeAgentService);
  326. expect(capturedPluginDialogProps[0]?.remoteZCodeAgentService).toBe(remoteZCodeAgentService);
  327. expect(capturedMcpDialogProps[0]?.localWorkspacePath).toBe("/Users/me/folder1");
  328. expect(capturedSkillDialogProps[0]?.workspaceIdentity).toBe(
  329. "remote:ssh:localhost:2223:root:/root/folder1",
  330. );
  331. expect(capturedPluginDialogProps[0]?.workspaceIdentity).toBe(
  332. "remote:ssh:localhost:2223:root:/root/folder1",
  333. );
  334. });
  335. });