remoteSyncActions.test.ts 12 KB

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