remoteConnectionReuse.test.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // @vitest-environment jsdom
  2. import { act, renderHook, screen, cleanup } from "@testing-library/react";
  3. import { afterEach, expect, it, vi } from "vitest";
  4. import type { IServiceAccessor } from "@zcode/services";
  5. import type { IPlatformService, RemoteTarget } from "@zcode/shared";
  6. import { useRemoteWorkspaceHistory } from "@/root/useRemoteWorkspaceHistory.js";
  7. import { useRemoteWorkspaceSessionStore } from "@/store/remoteWorkspaceSessionStore.js";
  8. import { createTabStore } from "@/store/tabStore.js";
  9. import zhCN from "@/i18n/locales/zh-CN.js";
  10. afterEach(() => {
  11. cleanup();
  12. useRemoteWorkspaceSessionStore.setState({ sessionsById: {} });
  13. });
  14. it.each([false, true])(
  15. "Host 声明复用:只为新建意图提示实际连接且不覆盖凭据(%s)",
  16. async (announceReusedConnection) => {
  17. const reused = true;
  18. const target: RemoteTarget = { kind: "ssh", host: "actual.example.com", username: "dev" };
  19. const save = vi.fn(async () => undefined);
  20. const services = {
  21. settingService: { get: async () => ({}), update: vi.fn(async () => undefined) },
  22. credentialService: { load: async () => null, save, delete: vi.fn() },
  23. fileService: { resolvePath: async ({ path }: { path: string }) => path },
  24. zcodeTaskService: {
  25. listPinnedTasks: async () => [],
  26. listTasks: async () => ({ items: [], total: 0, hasMore: false }),
  27. },
  28. } as unknown as IServiceAccessor;
  29. const sessionId = `session-${reused}`;
  30. const platform = {
  31. connectRemote: vi.fn(async () => {
  32. useRemoteWorkspaceSessionStore.getState().registerSession({ sessionId, target, services });
  33. return { success: true, sessionId, ...(reused ? { reusedConnectionTarget: target } : {}) };
  34. }),
  35. bindRemoteWorkspaceSessionContext: vi.fn(
  36. async ({ workspacePath }: { workspacePath: string }) => ({
  37. workspacePath,
  38. workspaceIdentity: `remote:server:device:${workspacePath}`,
  39. }),
  40. ),
  41. onRemoteConnectionLog: () => () => undefined,
  42. onRemoteSessionClosed: () => () => undefined,
  43. onRemoteSessionReconnected: () => () => undefined,
  44. onBotRemoteWorkspaceReconnected: () => () => undefined,
  45. } as unknown as IPlatformService;
  46. const tabStoreApi = createTabStore();
  47. const { result } = renderHook(() =>
  48. useRemoteWorkspaceHistory({
  49. intl: {
  50. formatMessage: ({ id }, values = {}) =>
  51. (zhCN[id as keyof typeof zhCN] ?? id).replace(/\{(\w+)\}/gu, (_, key) =>
  52. String(values[key] ?? `{${key}}`),
  53. ),
  54. },
  55. services,
  56. platform,
  57. supportsSettings: false,
  58. isDesktop: true,
  59. tabStoreApi,
  60. activateTabByPath: tabStoreApi.getState().activateTabByPath,
  61. addTab: tabStoreApi.getState().addTab,
  62. }),
  63. );
  64. await act(async () => {
  65. await result.current.handleConnectRemote(
  66. { ...target, password: "newly-entered-password" },
  67. undefined,
  68. undefined,
  69. { announceReusedConnection },
  70. );
  71. });
  72. if (announceReusedConnection)
  73. expect(
  74. await screen.findByText(
  75. "这台设备已通过 SSH · dev@actual.example.com:22 连接,已直接使用现有连接",
  76. ),
  77. ).toBeTruthy();
  78. else expect(screen.queryByText(/已直接使用现有连接/u)).toBeNull();
  79. await act(async () => {
  80. await result.current.handleSelectRemoteProject(sessionId, "/srv/project");
  81. });
  82. if (reused) expect(save).not.toHaveBeenCalled();
  83. },
  84. );