remoteWorkspaceIdentity.test.ts 3.7 KB

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