botRemoteWorkspaceResolver.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. buildRemoteWorkspaceIdentity,
  4. buildServerRemoteWorkspaceIdentity,
  5. type ConnectionProfile,
  6. } from "@zcode/shared";
  7. import { resolveBotRemoteWorkspace } from "../src/host/botRemoteWorkspaceResolver.js";
  8. import type { WindowRemoteLogicalSessionSnapshot } from "../src/host/windowRemoteConnectionRegistry.js";
  9. const workspacePath = "/work/project";
  10. const target = { kind: "ssh" as const, host: "source.test", username: "dev" };
  11. const legacy = buildRemoteWorkspaceIdentity(workspacePath, target);
  12. const stable = buildServerRemoteWorkspaceIdentity({ serverId: "source-a", workspacePath });
  13. const profile: ConnectionProfile = {
  14. profileId: "profile-a",
  15. sourceServerId: "source-a",
  16. transport: "ssh",
  17. target,
  18. createdAt: 1,
  19. };
  20. function fixture() {
  21. const profiles = [profile];
  22. const servers = [
  23. { sourceServerId: "source-a", displayName: "A", preferredProfileId: profile.profileId },
  24. ];
  25. const sessions: WindowRemoteLogicalSessionSnapshot[] = [
  26. {
  27. sourceServerId: "source-a",
  28. remoteSessionId: "session-a",
  29. requestId: "request-a",
  30. target,
  31. workspacePath,
  32. workspaceIdentity: stable,
  33. generation: 1,
  34. state: "online",
  35. sourceAvailability: "online",
  36. },
  37. ];
  38. const resolveProfileTarget = vi.fn(async () => ({
  39. ...target,
  40. password: "synthetic-profile-secret",
  41. }));
  42. const directory = {
  43. snapshot: async () => ({ profiles, servers, workspaceHistory: [] }),
  44. resolveProfileTarget,
  45. };
  46. return {
  47. profiles,
  48. servers,
  49. sessions,
  50. resolveProfileTarget,
  51. resolve: (identity: string, purpose: "lookup" | "reconnect" = "lookup") =>
  52. resolveBotRemoteWorkspace({ workspacePath, workspaceIdentity: identity }, purpose, {
  53. directory,
  54. listSessions: () => sessions,
  55. }),
  56. };
  57. }
  58. describe("Bot Source workspace resolver", () => {
  59. it.each([stable, legacy])(
  60. "resolves %s to the existing stable route without reading credentials",
  61. async (identity) => {
  62. const f = fixture();
  63. expect(await f.resolve(identity)).toMatchObject({
  64. workspacePath,
  65. workspaceIdentity: stable,
  66. target,
  67. runtimeKey: JSON.stringify(["session-a", 1]),
  68. });
  69. expect(f.resolveProfileTarget).not.toHaveBeenCalled();
  70. },
  71. );
  72. it("uses the current route even when another profile is preferred", async () => {
  73. const f = fixture();
  74. f.profiles.push({
  75. ...profile,
  76. profileId: "profile-b",
  77. target: { ...target, host: "alternate.test" },
  78. });
  79. f.servers[0]!.preferredProfileId = "profile-b";
  80. expect((await f.resolve(stable))?.target).toEqual(target);
  81. });
  82. it("only restores current Profile credentials for explicit reconnect", async () => {
  83. const f = fixture();
  84. f.sessions.length = 0;
  85. expect((await f.resolve(legacy))?.runtimeKey).toBeUndefined();
  86. expect(f.resolveProfileTarget).not.toHaveBeenCalled();
  87. expect((await f.resolve(legacy, "reconnect"))?.target).toMatchObject({
  88. password: "synthetic-profile-secret",
  89. });
  90. expect(f.resolveProfileTarget).toHaveBeenCalledWith("profile-a");
  91. });
  92. it("rejects unknown stable identities even when the path matches", async () => {
  93. const f = fixture();
  94. expect(
  95. await f.resolve(
  96. buildServerRemoteWorkspaceIdentity({ serverId: "source-other", workspacePath }),
  97. ),
  98. ).toBeNull();
  99. });
  100. it("rejects an ambiguous legacy route after a retired profile was rebound", async () => {
  101. const f = fixture();
  102. f.profiles[0] = { ...profile, lifecycleState: "retired" };
  103. f.profiles.push({ ...profile, profileId: "profile-new", sourceServerId: "source-b" });
  104. expect(await f.resolve(legacy)).toBeNull();
  105. });
  106. it("reports a different runtime generation after reconnect", async () => {
  107. const f = fixture();
  108. const first = await f.resolve(legacy);
  109. f.sessions[0]!.generation += 1;
  110. expect((await f.resolve(stable))?.runtimeKey).not.toBe(first?.runtimeKey);
  111. });
  112. });
  113. describe("Bot non-SSH identities", () => {
  114. it.each([
  115. { kind: "wsl" as const, distro: "Ubuntu", user: "dev" },
  116. { kind: "docker" as const, container: "bot-e2e" },
  117. ])("resolves $kind legacy identity with the existing Profile", async (target) => {
  118. const workspaceIdentity = buildRemoteWorkspaceIdentity(workspacePath, target);
  119. const result = await resolveBotRemoteWorkspace({ workspacePath, workspaceIdentity }, "lookup", {
  120. directory: {
  121. snapshot: async () => ({
  122. profiles: [{ ...profile, transport: target.kind, target } as ConnectionProfile],
  123. servers: [{ sourceServerId: "source-a", displayName: "A" }],
  124. workspaceHistory: [],
  125. }),
  126. resolveProfileTarget: async () => target,
  127. },
  128. listSessions: () => [],
  129. });
  130. expect(result).toMatchObject({ workspaceIdentity: stable, target });
  131. });
  132. });