workspaceOpenInEditorTarget.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, it } from "vitest";
  2. import type { WindowTabState } from "@/store/tabStore.js";
  3. import { resolveWorkspaceOpenInEditorTarget } from "@/hooks/useWorkspaceOpenInEditorTarget.js";
  4. const tabs: WindowTabState[] = [
  5. {
  6. id: "host-a",
  7. kind: "workspace",
  8. label: "repo-a",
  9. workspacePath: "/workspace/repo",
  10. workspaceIdentity: "remote:ssh:host-a:/workspace/repo",
  11. remoteSessionId: "session-a",
  12. remoteTarget: { kind: "ssh", host: "host-a", username: "dev" },
  13. },
  14. {
  15. id: "host-b",
  16. kind: "workspace",
  17. label: "repo-b",
  18. workspacePath: "/workspace/repo",
  19. workspaceIdentity: "remote:ssh:host-b:/workspace/repo",
  20. remoteSessionId: "session-b",
  21. remoteTarget: { kind: "ssh", host: "host-b", username: "dev" },
  22. },
  23. ];
  24. describe("resolveWorkspaceOpenInEditorTarget", () => {
  25. it("同路径多远端且没有 identity 时失败关闭", () => {
  26. expect(
  27. resolveWorkspaceOpenInEditorTarget(tabs, {
  28. workspacePath: "/workspace/repo",
  29. }),
  30. ).toEqual({ isRemoteWorkspace: true, remoteTarget: undefined });
  31. });
  32. it("按 workspace identity 和 remote session 精确选择远端目标", () => {
  33. expect(
  34. resolveWorkspaceOpenInEditorTarget(tabs, {
  35. workspacePath: "/workspace/repo",
  36. workspaceIdentity: "remote:ssh:host-b:/workspace/repo",
  37. workspaceRemoteSessionId: "session-b",
  38. }),
  39. ).toEqual({
  40. isRemoteWorkspace: true,
  41. remoteTarget: { kind: "ssh", host: "host-b", username: "dev" },
  42. });
  43. });
  44. it("匹配前规范化 workspace identity 和 remote session 空白", () => {
  45. const tabsWithLegacyWhitespace: WindowTabState[] = tabs.map((tab) =>
  46. tab.id === "host-b"
  47. ? {
  48. ...tab,
  49. workspaceIdentity: ` ${tab.workspaceIdentity} `,
  50. remoteSessionId: ` ${tab.remoteSessionId} `,
  51. }
  52. : tab,
  53. );
  54. expect(
  55. resolveWorkspaceOpenInEditorTarget(tabsWithLegacyWhitespace, {
  56. workspacePath: "/workspace/repo",
  57. workspaceIdentity: " remote:ssh:host-b:/workspace/repo ",
  58. workspaceRemoteSessionId: " session-b ",
  59. }),
  60. ).toEqual({
  61. isRemoteWorkspace: true,
  62. remoteTarget: { kind: "ssh", host: "host-b", username: "dev" },
  63. });
  64. });
  65. });