remoteWorkspaceProfileReconnect.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. appSettingsSchema,
  4. type RemoteWorkspaceSessionEntry,
  5. type SSHProfileTarget,
  6. } from "@zcode/shared";
  7. import { reconnectRemoteWorkspaceHistoryEntry } from "@/root/reconnectRemoteWorkspaceHistoryEntry.js";
  8. vi.mock("@/store/remotePinnedTaskStore.js", () => ({
  9. refreshRemotePinnedTasksForSession: vi.fn(async () => undefined),
  10. }));
  11. vi.mock("@/store/remoteTimelineTaskStore.js", () => ({
  12. refreshRemoteTimelineTasksForSession: vi.fn(async () => undefined),
  13. }));
  14. const profileId = "17ef20b3-622d-4a53-98c1-0cdff29f3e6d";
  15. const sourceServerId = "3cd4f9da-cd79-426c-9d70-75f36e9f6a87";
  16. const oldTarget: SSHProfileTarget = {
  17. kind: "ssh",
  18. host: "old.example.com",
  19. port: 22,
  20. username: "old-user",
  21. passwordCredentialKey: "legacy-password",
  22. };
  23. const updatedTarget: SSHProfileTarget = {
  24. kind: "ssh",
  25. host: "new.example.com",
  26. port: 2222,
  27. username: "dev",
  28. passwordCredentialKey: "profile-password",
  29. };
  30. function setup(target: SSHProfileTarget = updatedTarget) {
  31. const sessionEntry: RemoteWorkspaceSessionEntry = {
  32. kind: "remote",
  33. workspacePath: "/workspace/project",
  34. workspaceIdentity: `remote:server:${sourceServerId}:/workspace/project`,
  35. target: { ...oldTarget },
  36. lastOpenedAt: 1,
  37. };
  38. const settings = appSettingsSchema.parse({
  39. connectionProfiles: [{ profileId, sourceServerId, transport: "ssh", target, createdAt: 1 }],
  40. sourceServerWorkspaceHistory: [
  41. {
  42. profileId,
  43. sourceServerId,
  44. workspacePath: sessionEntry.workspacePath,
  45. workspaceIdentity: sessionEntry.workspaceIdentity,
  46. lastOpenedAt: 1,
  47. },
  48. ],
  49. });
  50. const params: Parameters<typeof reconnectRemoteWorkspaceHistoryEntry>[0] = {
  51. sessionEntry,
  52. activateTabByPath: vi.fn(() => false),
  53. setReconnectingRemoteWorkspaceKeys: vi.fn(),
  54. getSettings: vi.fn(async () => settings),
  55. loadCredential: vi.fn(async (key) => `${key}-value`),
  56. connectRemoteWorkspaceTarget: vi.fn(async () => "new-session"),
  57. resolveRemoteWorkspaceCanonicalPath: vi.fn(async () => sessionEntry.workspacePath),
  58. bindRemoteWorkspaceScope: vi.fn(async () => ({
  59. workspacePath: sessionEntry.workspacePath,
  60. workspaceIdentity: sessionEntry.workspaceIdentity!,
  61. })),
  62. disposeRemoteWorkspaceSession: vi.fn(async () => undefined),
  63. bindRemoteWorkspacePath: vi.fn(),
  64. bindRemoteWorkspaceIdentity: vi.fn(),
  65. upsertWorkspaceTab: vi.fn(),
  66. commitRemoteWorkspaceSessionMutation: vi.fn(async (mutation) => mutation.entry),
  67. getRemoteSessions: () => [sessionEntry],
  68. logger: { warn: vi.fn() },
  69. toast: vi.fn(),
  70. options: { requireStoredCredentials: true, throwOnFailure: true, showErrorToast: false },
  71. };
  72. return { params, settings, sessionEntry };
  73. }
  74. describe("SSH 原项目重连读取最新 Profile", () => {
  75. it.each(["manual", "automatic"] as const)(
  76. "%s 恢复使用原绑定线路的新地址和凭据,保留项目身份",
  77. async (recoveryMode) => {
  78. const { params, settings, sessionEntry } = setup();
  79. params.options = { ...params.options, recoveryMode };
  80. // 同设备另一条默认线路不能替代项目历史绑定的线路。
  81. const otherProfileId = "8a116c93-1306-48a8-b8bf-4bdc318b8684";
  82. settings.connectionProfiles!.unshift({
  83. ...settings.connectionProfiles![0]!,
  84. profileId: otherProfileId,
  85. target: { ...updatedTarget, host: "other.example.com" },
  86. });
  87. settings.sourceServers = [
  88. {
  89. sourceServerId,
  90. displayName: "Device",
  91. preferredProfileId: otherProfileId,
  92. installState: "installed",
  93. },
  94. ];
  95. await reconnectRemoteWorkspaceHistoryEntry(params);
  96. expect(params.connectRemoteWorkspaceTarget).toHaveBeenCalledWith(
  97. {
  98. kind: "ssh",
  99. host: "new.example.com",
  100. port: 2222,
  101. username: "dev",
  102. password: "profile-password-value",
  103. },
  104. undefined,
  105. {
  106. workspacePath: sessionEntry.workspacePath,
  107. workspaceIdentity: sessionEntry.workspaceIdentity,
  108. connectTrigger: "reconnect",
  109. },
  110. );
  111. expect(params.loadCredential).toHaveBeenCalledExactlyOnceWith("profile-password");
  112. expect(params.upsertWorkspaceTab).toHaveBeenCalledWith(
  113. sessionEntry.workspacePath,
  114. expect.objectContaining({
  115. workspaceIdentity: sessionEntry.workspaceIdentity,
  116. remoteTarget: { kind: "ssh", host: "new.example.com", port: 2222, username: "dev" },
  117. }),
  118. );
  119. expect(params.commitRemoteWorkspaceSessionMutation).toHaveBeenCalledWith(
  120. expect.objectContaining({
  121. entry: expect.objectContaining({
  122. workspaceIdentity: sessionEntry.workspaceIdentity,
  123. target: expect.objectContaining({ host: "new.example.com", port: 2222 }),
  124. }),
  125. }),
  126. );
  127. },
  128. );
  129. it("更换无口令私钥后不继承旧密码和旧私钥口令,也不误报凭据缺失", async () => {
  130. const { params, sessionEntry } = setup({
  131. kind: "ssh",
  132. host: "new.example.com",
  133. username: "dev",
  134. privateKeyPath: "/keys/new-key",
  135. });
  136. sessionEntry.target = {
  137. ...oldTarget,
  138. privateKeyPath: "/keys/old-key",
  139. privateKeyPassphraseCredentialKey: "legacy-passphrase",
  140. };
  141. await reconnectRemoteWorkspaceHistoryEntry(params);
  142. expect(params.loadCredential).not.toHaveBeenCalled();
  143. expect(params.connectRemoteWorkspaceTarget).toHaveBeenCalledWith(
  144. { kind: "ssh", host: "new.example.com", username: "dev", privateKeyPath: "/keys/new-key" },
  145. undefined,
  146. expect.anything(),
  147. );
  148. });
  149. it("从密码切换到有口令的私钥后使用新路径和口令", async () => {
  150. const { params } = setup({
  151. kind: "ssh",
  152. host: "new.example.com",
  153. username: "dev",
  154. privateKeyPath: "/keys/new-key",
  155. privateKeyPassphraseCredentialKey: "profile-passphrase",
  156. });
  157. await reconnectRemoteWorkspaceHistoryEntry(params);
  158. expect(params.connectRemoteWorkspaceTarget).toHaveBeenCalledWith(
  159. {
  160. kind: "ssh",
  161. host: "new.example.com",
  162. username: "dev",
  163. privateKeyPath: "/keys/new-key",
  164. privateKeyPassphrase: "profile-passphrase-value",
  165. },
  166. undefined,
  167. expect.anything(),
  168. );
  169. expect(params.loadCredential).toHaveBeenCalledExactlyOnceWith("profile-passphrase");
  170. });
  171. it("Profile 无新凭据引用时兼容相同认证方式的旧凭据,但仍使用新地址", async () => {
  172. const { params } = setup({ kind: "ssh", host: "new.example.com", username: "dev", port: 2222 });
  173. await reconnectRemoteWorkspaceHistoryEntry(params);
  174. expect(params.connectRemoteWorkspaceTarget).toHaveBeenCalledWith(
  175. {
  176. kind: "ssh",
  177. host: "new.example.com",
  178. username: "dev",
  179. port: 2222,
  180. password: "legacy-password-value",
  181. },
  182. undefined,
  183. expect.anything(),
  184. );
  185. expect(params.loadCredential).toHaveBeenCalledExactlyOnceWith("legacy-password");
  186. });
  187. it("没有绑定 Profile 的旧历史继续使用原地址和凭据", async () => {
  188. const { params, settings } = setup();
  189. settings.sourceServerWorkspaceHistory = [];
  190. await reconnectRemoteWorkspaceHistoryEntry(params);
  191. expect(params.connectRemoteWorkspaceTarget).toHaveBeenCalledWith(
  192. {
  193. kind: "ssh",
  194. host: "old.example.com",
  195. port: 22,
  196. username: "old-user",
  197. password: "legacy-password-value",
  198. },
  199. undefined,
  200. expect.anything(),
  201. );
  202. });
  203. it("新 Profile 的密码实际缺失时拒绝连接,不退回旧密码", async () => {
  204. const { params } = setup();
  205. params.loadCredential = vi.fn(async (key) => (key === "legacy-password" ? "old-secret" : null));
  206. await expect(reconnectRemoteWorkspaceHistoryEntry(params)).rejects.toMatchObject({
  207. code: "credentials-required",
  208. });
  209. expect(params.connectRemoteWorkspaceTarget).not.toHaveBeenCalled();
  210. expect(params.loadCredential).toHaveBeenCalledExactlyOnceWith("profile-password");
  211. });
  212. it("sibling 使用最新地址和发起方凭据,不重新读取本项目凭据", async () => {
  213. const { params } = setup();
  214. params.loadCredential = vi.fn(async () => {
  215. throw new Error("unexpected credential read");
  216. });
  217. params.options = {
  218. ...params.options,
  219. sshCredentialsOverride: { password: "initiator-secret", privateKeyPassphrase: null },
  220. };
  221. await reconnectRemoteWorkspaceHistoryEntry(params);
  222. expect(params.loadCredential).not.toHaveBeenCalled();
  223. expect(params.connectRemoteWorkspaceTarget).toHaveBeenCalledWith(
  224. expect.objectContaining({
  225. host: "new.example.com",
  226. port: 2222,
  227. password: "initiator-secret",
  228. }),
  229. undefined,
  230. expect.anything(),
  231. );
  232. });
  233. });