remoteWorkspaceIdentityRoundtrip.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // 构造(UI buildRemoteWorkspaceIdentity)↔ 解析(shared parseRemoteWorkspaceIdentity)
  2. // 的对偶契约测试:格式一旦漂移(任一侧改动),此处红——防止两侧手写规则分叉
  3. // (Workspace Identity 约束:构造与解析必须复用统一工具)。
  4. import { describe, expect, it } from "vitest";
  5. import { parseRemoteWorkspaceIdentity } from "@zcode/shared";
  6. import { buildRemoteWorkspaceIdentity } from "@/lib/remoteWorkspaceHistory.js";
  7. describe("远程 workspace identity 构造↔解析对偶", () => {
  8. it("ssh:build → parse 还原 workspacePath", () => {
  9. const identity = buildRemoteWorkspaceIdentity("/srv/app/", {
  10. kind: "ssh",
  11. host: "Dev.Example.com",
  12. port: 2222,
  13. username: "root",
  14. });
  15. expect(parseRemoteWorkspaceIdentity(identity)).toEqual({
  16. kind: "ssh",
  17. // 构造侧归一:收尾斜杠清理
  18. workspacePath: "/srv/app",
  19. });
  20. });
  21. it("ssh 缺省端口(22)与 wsl/docker:build → parse 还原", () => {
  22. const sshDefault = buildRemoteWorkspaceIdentity("/home/dev", {
  23. kind: "ssh",
  24. host: "h",
  25. username: "u",
  26. });
  27. expect(parseRemoteWorkspaceIdentity(sshDefault)?.workspacePath).toBe(
  28. "/home/dev",
  29. );
  30. const wsl = buildRemoteWorkspaceIdentity("/mnt/c/ws", {
  31. kind: "wsl",
  32. distro: "Ubuntu-22.04",
  33. });
  34. expect(parseRemoteWorkspaceIdentity(wsl)).toEqual({
  35. kind: "wsl",
  36. workspacePath: "/mnt/c/ws",
  37. });
  38. const docker = buildRemoteWorkspaceIdentity("/workspace", {
  39. kind: "docker",
  40. container: "my-container",
  41. });
  42. expect(parseRemoteWorkspaceIdentity(docker)).toEqual({
  43. kind: "docker",
  44. workspacePath: "/workspace",
  45. });
  46. });
  47. it("wsl 显式 user:build → parse 不把 user 混入 workspacePath", () => {
  48. const identity = buildRemoteWorkspaceIdentity("/home/dev/ws", {
  49. kind: "wsl",
  50. distro: "Ubuntu-24.04",
  51. user: "dev",
  52. });
  53. expect(identity).toBe("remote:wsl:Ubuntu-24.04:dev:/home/dev/ws");
  54. expect(parseRemoteWorkspaceIdentity(identity)).toEqual({
  55. kind: "wsl",
  56. workspacePath: "/home/dev/ws",
  57. });
  58. });
  59. it("反斜杠路径构造侧归一为 posix,解析可还原", () => {
  60. const identity = buildRemoteWorkspaceIdentity("\\srv\\app", {
  61. kind: "docker",
  62. container: "c1",
  63. });
  64. expect(parseRemoteWorkspaceIdentity(identity)?.workspacePath).toBe(
  65. "/srv/app",
  66. );
  67. });
  68. });