| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- // @vitest-environment jsdom
- import { createElement, type ReactNode } from "react";
- import { renderToStaticMarkup } from "react-dom/server";
- import { act, renderHook } from "@testing-library/react";
- import { afterEach, describe, expect, it, vi } from "vitest";
- const { capturedSkillDialogProps, capturedMcpDialogProps, capturedPluginDialogProps } = vi.hoisted(() => ({
- capturedSkillDialogProps: [] as Array<Record<string, unknown>>,
- capturedMcpDialogProps: [] as Array<Record<string, unknown>>,
- capturedPluginDialogProps: [] as Array<Record<string, unknown>>,
- }));
- vi.mock("lucide-react", () => {
- const createIcon = (name: string) => (props: Record<string, unknown>) =>
- createElement("svg", { "data-icon": name, ...props });
- return {
- ChevronDown: createIcon("chevron-down"),
- UploadCloud: createIcon("upload-cloud"),
- };
- });
- vi.mock("@/components/ui/button.js", () => ({
- Button: ({
- children,
- ...props
- }: {
- children: ReactNode;
- }) => createElement("button", props, children),
- }));
- vi.mock("@/components/ui/dropdown-menu.js", () => ({
- DropdownMenu: ({ children }: { children: ReactNode }) =>
- createElement("div", { "data-testid": "dropdown-menu" }, children),
- DropdownMenuContent: ({
- children,
- ...props
- }: {
- children: ReactNode;
- }) => createElement("div", { "data-testid": "dropdown-menu-content", ...props }, children),
- DropdownMenuItem: ({
- children,
- disabled,
- ...props
- }: {
- children: ReactNode;
- disabled?: boolean;
- }) =>
- createElement(
- "div",
- {
- "data-disabled": disabled ? "true" : undefined,
- "data-testid": "dropdown-menu-item",
- ...props,
- },
- children,
- ),
- DropdownMenuTrigger: ({ children }: { children: ReactNode }) => children,
- }));
- vi.mock("@/i18n/IntlProvider.js", () => ({
- useZCodeIntl: () => ({
- intl: {
- formatMessage: ({ id }: { id: string }) => {
- const messages: Record<string, string> = {
- "settings.skills.remoteSync.open": "同步 Skill",
- "settings.mcp.remoteSync.open": "同步 MCP",
- "settings.plugins.remoteSync.open": "同步 Plugin",
- "settings.remoteSync.open": "同步",
- };
- return messages[id] ?? id;
- },
- },
- }),
- }));
- vi.mock("@/settings/RemoteSkillSyncDialog.js", () => ({
- RemoteSkillSyncDialog: (props: Record<string, unknown>) => {
- capturedSkillDialogProps.push(props);
- return createElement("div", { "data-testid": "remote-skill-sync-dialog" });
- },
- }));
- vi.mock("@/settings/RemoteMcpSyncDialog.js", () => ({
- RemoteMcpSyncDialog: (props: Record<string, unknown>) => {
- capturedMcpDialogProps.push(props);
- return createElement("div", { "data-testid": "remote-mcp-sync-dialog" });
- },
- }));
- vi.mock("@/settings/RemotePluginSyncDialog.js", () => ({
- RemotePluginSyncDialog: (props: Record<string, unknown>) => {
- capturedPluginDialogProps.push(props);
- return createElement("div", { "data-testid": "remote-plugin-sync-dialog" });
- },
- }));
- afterEach(() => {
- vi.useRealTimers();
- });
- describe("RemoteSyncActions", () => {
- it("drops dialog intent across disconnects and remote target changes", async () => {
- const { useRemoteSyncDialogIntent } = await import(
- "@/settings/RemoteSyncActions.js"
- );
- const hook = renderHook(
- ({ rpcReady, targetKey }) =>
- useRemoteSyncDialogIntent({ rpcReady, targetKey }),
- {
- initialProps: {
- rpcReady: true,
- targetKey: "remote-a:session-1",
- },
- },
- );
- act(() => hook.result.current.setOpen(true));
- expect(hook.result.current.open).toBe(true);
- hook.rerender({ rpcReady: false, targetKey: "remote-a:session-1" });
- expect(hook.result.current.open).toBe(false);
- hook.rerender({ rpcReady: true, targetKey: "remote-a:session-2" });
- expect(hook.result.current.open).toBe(false);
- act(() => hook.result.current.setOpen(true));
- expect(hook.result.current.open).toBe(true);
- hook.rerender({ rpcReady: true, targetKey: "remote-b:session-3" });
- expect(hook.result.current.open).toBe(false);
- });
- it("shows remote sync actions for SSH and WSL sessions only", async () => {
- const { shouldShowRemoteSyncActions } = await import("@/settings/RemoteSyncActions.js");
- expect(
- shouldShowRemoteSyncActions({
- remoteSessionId: "session-ssh",
- remoteTarget: {
- kind: "ssh",
- host: "localhost",
- port: 2223,
- username: "root",
- },
- }),
- ).toBe(true);
- expect(
- shouldShowRemoteSyncActions({
- remoteSessionId: "session-wsl",
- remoteTarget: {
- kind: "wsl",
- distro: "Ubuntu",
- user: "alice",
- },
- }),
- ).toBe(true);
- expect(
- shouldShowRemoteSyncActions({
- remoteSessionId: "session-docker",
- remoteTarget: {
- kind: "docker",
- container: "devbox",
- },
- }),
- ).toBe(false);
- expect(
- shouldShowRemoteSyncActions({
- remoteSessionId: null,
- remoteTarget: {
- kind: "wsl",
- distro: "Ubuntu",
- },
- }),
- ).toBe(false);
- });
- it("hides remote sync actions in Web remote replayable contexts", async () => {
- const { shouldShowRemoteSyncActions } = await import("@/settings/RemoteSyncActions.js");
- expect(
- shouldShowRemoteSyncActions({
- remoteSessionId: "session-wsl",
- remoteTarget: {
- kind: "wsl",
- distro: "Ubuntu",
- user: "alice",
- },
- clientMode: "web-remote-replayable",
- hasLocalSourceService: true,
- }),
- ).toBe(false);
- });
- it("hides remote sync actions when the desktop local source service is unavailable", async () => {
- const { shouldShowRemoteSyncActions } = await import("@/settings/RemoteSyncActions.js");
- expect(
- shouldShowRemoteSyncActions({
- remoteSessionId: "session-wsl",
- remoteTarget: {
- kind: "wsl",
- distro: "Ubuntu",
- },
- clientMode: "desktop-continuous",
- hasLocalSourceService: false,
- }),
- ).toBe(false);
- });
- it("allows starting a remote sync operation only when selection exists and no run is active", async () => {
- const { shouldStartRemoteSyncOperation } = await import("@/settings/RemoteSyncActions.js");
- expect(shouldStartRemoteSyncOperation({ inFlight: false, selectedCount: 1 })).toBe(true);
- expect(shouldStartRemoteSyncOperation({ inFlight: true, selectedCount: 1 })).toBe(false);
- expect(shouldStartRemoteSyncOperation({ inFlight: false, selectedCount: 0 })).toBe(false);
- });
- it("times out a remote sync preflight that never resolves", async () => {
- vi.useFakeTimers();
- const {
- isRemoteSyncPreflightTimeoutError,
- REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS,
- runRemoteSyncPreflightWithTimeout,
- } = await import("@/settings/RemoteSyncActions.js");
- const result = runRemoteSyncPreflightWithTimeout(
- () => new Promise<string>(() => undefined),
- ).catch((error: unknown) => error);
- await vi.advanceTimersByTimeAsync(REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS);
- const error = await result;
- expect(isRemoteSyncPreflightTimeoutError(error)).toBe(true);
- expect(error).toMatchObject({
- timeoutMs: REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS,
- });
- });
- it("clears the remote sync preflight timeout after a successful response", async () => {
- vi.useFakeTimers();
- const { REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS, runRemoteSyncPreflightWithTimeout } =
- await import("@/settings/RemoteSyncActions.js");
- await expect(
- runRemoteSyncPreflightWithTimeout(async () => "ok"),
- ).resolves.toBe("ok");
- await vi.advanceTimersByTimeAsync(REMOTE_SYNC_PREFLIGHT_TIMEOUT_MS);
- });
- it("renders Skill, MCP, and Plugin menu actions for SSH remote contexts", async () => {
- const { RemoteSyncMenuItems } = await import("@/settings/RemoteSyncActions.js");
- const html = renderToStaticMarkup(
- createElement(RemoteSyncMenuItems, {
- canSyncSkills: true,
- canSyncMcp: true,
- canSyncPlugins: true,
- onOpenSkillSync: vi.fn(),
- onOpenMcpSync: vi.fn(),
- onOpenPluginSync: vi.fn(),
- }),
- );
- expect(html).toContain("同步 Skill");
- expect(html).toContain("同步 MCP");
- expect(html).toContain("同步 Plugin");
- expect(html.match(/data-icon="upload-cloud"/g)?.length).toBe(3);
- });
- it("renders one sync dropdown button with Skill, MCP, and Plugin actions", async () => {
- const { RemoteSyncDropdownButton } = await import("@/settings/RemoteSyncActions.js");
- const html = renderToStaticMarkup(
- createElement(RemoteSyncDropdownButton, {
- canSyncSkills: true,
- canSyncMcp: true,
- canSyncPlugins: true,
- onOpenSkillSync: vi.fn(),
- onOpenMcpSync: vi.fn(),
- onOpenPluginSync: vi.fn(),
- }),
- );
- expect(html).toContain("同步");
- expect(html).toContain("同步 Skill");
- expect(html).toContain("同步 MCP");
- expect(html).toContain("同步 Plugin");
- expect(html.match(/data-testid="dropdown-menu-item"/g)?.length).toBe(3);
- expect(html.match(/data-icon="upload-cloud"/g)?.length).toBe(4);
- expect(html).toContain('data-icon="chevron-down"');
- });
- it("keeps the MCP dropdown action disabled until a remote workspace path exists", async () => {
- const { RemoteSyncDropdownButton } = await import("@/settings/RemoteSyncActions.js");
- const html = renderToStaticMarkup(
- createElement(RemoteSyncDropdownButton, {
- canSyncSkills: true,
- canSyncMcp: true,
- canSyncPlugins: true,
- mcpDisabled: true,
- onOpenSkillSync: vi.fn(),
- onOpenMcpSync: vi.fn(),
- onOpenPluginSync: vi.fn(),
- }),
- );
- expect(html).toContain("同步 Skill");
- expect(html).toContain("同步 MCP");
- expect(html).toContain("同步 Plugin");
- expect(html).toContain('data-disabled="true"');
- });
- it("mounts all remote sync dialogs while preserving distinct workspace paths", async () => {
- const { RemoteSyncDialogs } = await import("@/settings/RemoteSyncActions.js");
- capturedSkillDialogProps.length = 0;
- capturedMcpDialogProps.length = 0;
- capturedPluginDialogProps.length = 0;
- const localZCodeAgentService = {};
- const remoteZCodeAgentService = {};
- renderToStaticMarkup(
- createElement(RemoteSyncDialogs, {
- canSyncSkills: true,
- canSyncMcp: true,
- canSyncPlugins: true,
- skillOpen: true,
- mcpOpen: true,
- pluginOpen: true,
- onSkillOpenChange: vi.fn(),
- onMcpOpenChange: vi.fn(),
- onPluginOpenChange: vi.fn(),
- localSkillSyncService: {} as never,
- remoteSkillSyncService: {} as never,
- localMcpSyncService: {} as never,
- remoteMcpSyncService: {} as never,
- localPluginSyncService: {} as never,
- remotePluginSyncService: {} as never,
- localZCodeAgentService: localZCodeAgentService as never,
- remoteZCodeAgentService: remoteZCodeAgentService as never,
- remoteTarget: {
- kind: "ssh",
- host: "localhost",
- port: 2223,
- username: "root",
- },
- skillWorkspacePath: "",
- mcpWorkspacePath: "/root/folder1",
- pluginWorkspacePath: "/root/folder1",
- pluginLocalWorkspacePath: "/Users/me/folder1",
- mcpLocalWorkspacePath: "/Users/me/folder1",
- workspaceIdentity: "remote:ssh:localhost:2223:root:/root/folder1",
- onSkillsSynced: vi.fn(),
- onMcpSynced: vi.fn(),
- onPluginsSynced: vi.fn(),
- }),
- );
- expect(capturedSkillDialogProps).toHaveLength(1);
- expect(capturedMcpDialogProps).toHaveLength(1);
- expect(capturedPluginDialogProps).toHaveLength(1);
- expect(capturedSkillDialogProps[0]?.workspacePath).toBe("");
- expect(capturedMcpDialogProps[0]?.workspacePath).toBe("/root/folder1");
- expect(capturedPluginDialogProps[0]?.workspacePath).toBe("/root/folder1");
- expect(capturedPluginDialogProps[0]?.localWorkspacePath).toBe("/Users/me/folder1");
- expect(capturedPluginDialogProps[0]?.localZCodeAgentService).toBe(localZCodeAgentService);
- expect(capturedPluginDialogProps[0]?.remoteZCodeAgentService).toBe(remoteZCodeAgentService);
- expect(capturedMcpDialogProps[0]?.localWorkspacePath).toBe("/Users/me/folder1");
- expect(capturedSkillDialogProps[0]?.workspaceIdentity).toBe(
- "remote:ssh:localhost:2223:root:/root/folder1",
- );
- expect(capturedPluginDialogProps[0]?.workspaceIdentity).toBe(
- "remote:ssh:localhost:2223:root:/root/folder1",
- );
- });
- });
|