remoteWorkspaceIdentity.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // 远程 workspace identity 解析(与 UI buildRemoteWorkspaceIdentity 的格式契约对偶)。
  2. // 消费方:CLI v4 createSession 的 workspaceId(分屏二期远程 pane 传 identity)。
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. buildRemoteWorkspaceIdentity,
  6. isRemoteWorkspaceIdentity,
  7. parseRemoteWorkspaceIdentity,
  8. } from "../src/remote-workspace-identity.js";
  9. describe("parseRemoteWorkspaceIdentity", () => {
  10. it("统一构造 SSH/WSL/Docker/Server workspace identity", () => {
  11. expect(
  12. buildRemoteWorkspaceIdentity("/work/demo", {
  13. kind: "ssh",
  14. host: "DEV.INTERNAL",
  15. port: 22,
  16. username: "developer",
  17. }),
  18. ).toBe("remote:ssh:dev.internal:22:developer:/work/demo");
  19. expect(
  20. buildRemoteWorkspaceIdentity("/work/demo", {
  21. kind: "wsl",
  22. distro: "Ubuntu",
  23. user: "dev",
  24. }),
  25. ).toBe("remote:wsl:Ubuntu:dev:/work/demo");
  26. expect(
  27. buildRemoteWorkspaceIdentity("/work/demo", {
  28. kind: "docker",
  29. container: "demo",
  30. }),
  31. ).toBe("remote:docker:demo:/work/demo");
  32. expect(
  33. buildRemoteWorkspaceIdentity("/work/demo", {
  34. kind: "server",
  35. url: "https://remote.example.com/ws",
  36. serverId: "Server A",
  37. }),
  38. ).toBe("remote:server:server-a:/work/demo");
  39. });
  40. it("ssh:remote:ssh:<host>:<port>:<username>:<path>", () => {
  41. expect(
  42. parseRemoteWorkspaceIdentity("remote:ssh:dev.example.com:22:root:/srv/app"),
  43. ).toEqual({ kind: "ssh", workspacePath: "/srv/app" });
  44. // path 含 ":"(posix 合法)不截断
  45. expect(
  46. parseRemoteWorkspaceIdentity("remote:ssh:host:2222:dev:/data/a:b"),
  47. ).toEqual({ kind: "ssh", workspacePath: "/data/a:b" });
  48. });
  49. it("wsl / docker:单段 authority + path", () => {
  50. expect(
  51. parseRemoteWorkspaceIdentity("remote:wsl:Ubuntu-22.04:/home/dev/ws"),
  52. ).toEqual({ kind: "wsl", workspacePath: "/home/dev/ws" });
  53. expect(
  54. parseRemoteWorkspaceIdentity("remote:docker:my-container:/workspace"),
  55. ).toEqual({ kind: "docker", workspacePath: "/workspace" });
  56. });
  57. it("wsl:显式 user 属于 authority,解析后只返回真实 workspacePath", () => {
  58. expect(
  59. parseRemoteWorkspaceIdentity("remote:wsl:Ubuntu-24.04:dev:/home/dev/coding/DEMO-ERP-NEW"),
  60. ).toEqual({
  61. kind: "wsl",
  62. workspacePath: "/home/dev/coding/DEMO-ERP-NEW",
  63. });
  64. expect(
  65. parseRemoteWorkspaceIdentity("remote:wsl:Ubuntu-24.04:dev:/data/a:b"),
  66. ).toEqual({
  67. kind: "wsl",
  68. workspacePath: "/data/a:b",
  69. });
  70. });
  71. it("根路径与规范化形态(构造侧保证 path 以 / 开头)", () => {
  72. expect(parseRemoteWorkspaceIdentity("remote:wsl:default:/")).toEqual({
  73. kind: "wsl",
  74. workspacePath: "/",
  75. });
  76. });
  77. it("非远程/非法输入返回 null(调用方回落本地 workspacePath 语义)", () => {
  78. // 本地绝对路径
  79. expect(parseRemoteWorkspaceIdentity("/Users/dev/ws")).toBeNull();
  80. expect(parseRemoteWorkspaceIdentity("C:\\ws\\a")).toBeNull();
  81. // 前缀相同但 kind 未知
  82. expect(parseRemoteWorkspaceIdentity("remote:ftp:host:/x")).toBeNull();
  83. // authority 段不足
  84. expect(parseRemoteWorkspaceIdentity("remote:ssh:host:22:/x")).toBeNull();
  85. expect(parseRemoteWorkspaceIdentity("remote:wsl:/x")).toBeNull();
  86. expect(parseRemoteWorkspaceIdentity("remote:wsl:Ubuntu-24.04:user:relative/x")).toBeNull();
  87. // path 不以 / 开头(非归一形态)
  88. expect(parseRemoteWorkspaceIdentity("remote:docker:c:relative/x")).toBeNull();
  89. // 空段
  90. expect(parseRemoteWorkspaceIdentity("remote::host:22:u:/x")).toBeNull();
  91. expect(parseRemoteWorkspaceIdentity("remote:")).toBeNull();
  92. expect(parseRemoteWorkspaceIdentity("")).toBeNull();
  93. });
  94. it("isRemoteWorkspaceIdentity 与 parse 判定一致", () => {
  95. expect(
  96. isRemoteWorkspaceIdentity("remote:ssh:host:22:root:/srv/app"),
  97. ).toBe(true);
  98. expect(isRemoteWorkspaceIdentity("/Users/dev/ws")).toBe(false);
  99. });
  100. });