remoteProjectNavigation.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { expect, it, vi } from "vitest";
  2. import { attachRemoteProjectWorkspace } from "@/project-sidebar/remote-navigation.js";
  3. function fixture() {
  4. const sessions = new Map<string, { sessionId: string }>();
  5. const identity = "remote:server:s:/same";
  6. const input = {
  7. hostId: "remote:server:s",
  8. workspacePath: "/same",
  9. workspaceIdentity: identity,
  10. remoteService: {
  11. connectHost: vi.fn(async () => ({
  12. id: "remote:server:s",
  13. target: { kind: "docker", container: "s" },
  14. })),
  15. } as never,
  16. findSession: (key: string) => sessions.get(key),
  17. connect: vi.fn(async () => "new-session"),
  18. bind: vi.fn(async (sessionId: string) => {
  19. sessions.set(identity, { sessionId });
  20. }),
  21. cancel: vi.fn(async () => {}),
  22. };
  23. return { sessions, identity, input };
  24. }
  25. it("uses the exact identity and existing physical connection, not another Source's same cwd", async () => {
  26. const { sessions, input } = fixture();
  27. sessions.set("remote:server:other:/same", { sessionId: "other" });
  28. expect(await attachRemoteProjectWorkspace(input)).toBe("new-session");
  29. expect(input.connect).toHaveBeenCalledOnce();
  30. expect(input.bind).toHaveBeenCalledWith("new-session", "/same");
  31. expect(input.cancel).not.toHaveBeenCalled();
  32. });
  33. it("reuses the attached workspace and does not cancel it on failure", async () => {
  34. const { sessions, identity, input } = fixture();
  35. sessions.set(identity, { sessionId: "existing" });
  36. input.bind.mockRejectedValueOnce(new Error("bind failed"));
  37. await expect(attachRemoteProjectWorkspace(input)).rejects.toThrow("bind failed");
  38. expect(input.connect).not.toHaveBeenCalled();
  39. expect(input.cancel).not.toHaveBeenCalled();
  40. });
  41. it("releases only the new logical attachment when binding fails", async () => {
  42. const { input } = fixture();
  43. input.bind.mockRejectedValueOnce(new Error("bind failed"));
  44. await expect(attachRemoteProjectWorkspace(input)).rejects.toThrow("bind failed");
  45. expect(input.cancel).toHaveBeenCalledWith("new-session");
  46. });
  47. it("does not bind a late connection after navigation is superseded and releases its attachment", async () => {
  48. const { input } = fixture();
  49. let current = true;
  50. input.connect.mockImplementation(async () => {
  51. current = false;
  52. return "late";
  53. });
  54. await expect(
  55. attachRemoteProjectWorkspace({ ...input, isCurrent: () => current }),
  56. ).rejects.toThrow("project.navigation-cancelled");
  57. expect(input.bind).not.toHaveBeenCalled();
  58. expect(input.cancel).toHaveBeenCalledWith("late");
  59. });
  60. it("does not create an attachment when the Host response belongs to an old navigation", async () => {
  61. const { input } = fixture();
  62. await expect(attachRemoteProjectWorkspace({ ...input, isCurrent: () => false })).rejects.toThrow(
  63. "project.navigation-cancelled",
  64. );
  65. expect(input.connect).not.toHaveBeenCalled();
  66. });
  67. it("does not bind a replaced Host membership after attachment preparation", async () => {
  68. const { input } = fixture();
  69. const host = {
  70. id: input.hostId,
  71. membershipId: "original",
  72. target: { kind: "docker" as const, container: "s" },
  73. };
  74. const remoteService = {
  75. connectHost: vi.fn(async () => host),
  76. readDirectory: vi.fn(async () => ({
  77. hosts: [{ ...host, membershipId: "replacement" }],
  78. entries: [],
  79. })),
  80. };
  81. await expect(
  82. attachRemoteProjectWorkspace({
  83. ...input,
  84. remoteService: remoteService as never,
  85. expectedMembershipId: "original",
  86. }),
  87. ).rejects.toThrow("remote-project.host-removed");
  88. expect(input.bind).not.toHaveBeenCalled();
  89. expect(input.cancel).toHaveBeenCalledWith("new-session");
  90. });
  91. it("rejects a removed project at the navigation barrier and preserves a shared attachment", async () => {
  92. const { input, sessions, identity } = fixture();
  93. sessions.set(identity, { sessionId: "shared" });
  94. const projectService = { readProjects: vi.fn(async () => []) };
  95. await expect(
  96. attachRemoteProjectWorkspace({ ...input, projectId: "removed", projectService }),
  97. ).rejects.toThrow("project.not-found");
  98. expect(input.bind).not.toHaveBeenCalled();
  99. expect(input.cancel).not.toHaveBeenCalled();
  100. });