remoteProjectReconnectIntent.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { describe, expect, it, vi } from "vitest";
  2. import { requestRemoteProjectConnection } from "@/remote-connection-flow/contract.js";
  3. import { resolveRemoteConnectionPresentation } from "@/lib/remoteConnectionPresentation.js";
  4. const preference = { preferredDirectoryPath: "/repo", preferredProfileId: "dev" };
  5. describe("项目重连意图", () => {
  6. it("安装失败保留实际 WSL 目标,不在交接时恢复默认参数", async () => {
  7. const resolvedWslTarget = { kind: "wsl", distro: "Ubuntu", user: "dev" } as const;
  8. const profileId = "17ef20b3-622d-4a53-98c1-0cdff29f3e6d";
  9. const openWizard = vi.fn();
  10. await requestRemoteProjectConnection({
  11. preference,
  12. presentation: resolveRemoteConnectionPresentation({}),
  13. reconnect: async () => {
  14. throw Object.assign(new Error("install required"), {
  15. code: "source-server-install-required",
  16. profileId,
  17. resolvedWslTarget,
  18. });
  19. },
  20. openWizard,
  21. });
  22. expect(openWizard).toHaveBeenCalledWith({
  23. ...preference,
  24. initialFailure: {
  25. code: "source-server-install-required",
  26. message: "install required",
  27. profileId,
  28. resolvedWslTarget,
  29. },
  30. });
  31. });
  32. it("升级失败把 Host 实际 Profile 与原目录一起交给向导", async () => {
  33. const profileId = "17ef20b3-622d-4a53-98c1-0cdff29f3e6d";
  34. const openWizard = vi.fn();
  35. await requestRemoteProjectConnection({
  36. preference,
  37. presentation: resolveRemoteConnectionPresentation({}),
  38. reconnect: async () => {
  39. throw Object.assign(new Error("upgrade required"), {
  40. code: "source-server-upgrade-required",
  41. profileId,
  42. });
  43. },
  44. openWizard,
  45. });
  46. expect(openWizard).toHaveBeenCalledWith({
  47. ...preference,
  48. initialFailure: {
  49. code: "source-server-upgrade-required",
  50. message: "upgrade required",
  51. profileId,
  52. },
  53. });
  54. });
  55. it("凭据齐全复用原地路径,不打开向导、不激活新页面", async () => {
  56. const reconnect = vi.fn(async () => undefined);
  57. const openWizard = vi.fn();
  58. await requestRemoteProjectConnection({
  59. preference,
  60. presentation: resolveRemoteConnectionPresentation({}),
  61. reconnect,
  62. openWizard,
  63. });
  64. expect(reconnect).toHaveBeenCalledWith({
  65. activateWorkspaceAfterReconnect: false,
  66. showErrorToast: false,
  67. throwOnFailure: true,
  68. requireStoredCredentials: true,
  69. });
  70. expect(openWizard).not.toHaveBeenCalled();
  71. });
  72. it("普通失败留在项目行;查看已失败项目直接展示连接步骤,不再次拨号", async () => {
  73. const reconnect = vi.fn(async () => {
  74. throw Object.assign(new Error("offline"), { code: "remote-host-unreachable" });
  75. });
  76. const openWizard = vi.fn();
  77. await requestRemoteProjectConnection({
  78. preference,
  79. presentation: resolveRemoteConnectionPresentation({}),
  80. reconnect,
  81. openWizard,
  82. });
  83. expect(openWizard).not.toHaveBeenCalled();
  84. reconnect.mockClear();
  85. await requestRemoteProjectConnection({
  86. preference,
  87. presentation: resolveRemoteConnectionPresentation({
  88. state: "failed",
  89. errorCode: "remote-host-unreachable",
  90. }),
  91. failure: { message: "offline", code: "remote-host-unreachable", logs: [] },
  92. reconnect,
  93. openWizard,
  94. });
  95. expect(reconnect).not.toHaveBeenCalled();
  96. expect(openWizard).toHaveBeenCalledWith(
  97. expect.objectContaining({
  98. ...preference,
  99. initialFailure: expect.objectContaining({ code: "remote-host-unreachable" }),
  100. }),
  101. );
  102. });
  103. it.each([
  104. "credentials-required",
  105. "source-server-install-required",
  106. "source-server-upgrade-required",
  107. "source-server-identity-change-pending",
  108. ])("%s 交接原目录到向导等待点", async (code) => {
  109. const reconnect = vi.fn(async () => {
  110. throw Object.assign(new Error(code), { code });
  111. });
  112. const openWizard = vi.fn();
  113. await requestRemoteProjectConnection({
  114. preference,
  115. presentation: resolveRemoteConnectionPresentation({}),
  116. reconnect,
  117. openWizard,
  118. });
  119. expect(openWizard).toHaveBeenCalledWith(
  120. expect.objectContaining({ ...preference, initialFailure: expect.objectContaining({ code }) }),
  121. );
  122. });
  123. });
  124. it("设备在线而项目 attachment 未就绪的连接中展示,不等于已有重连请求", async () => {
  125. const reconnect = vi.fn(async () => undefined);
  126. const openWizard = vi.fn();
  127. await requestRemoteProjectConnection({
  128. preference,
  129. presentation: resolveRemoteConnectionPresentation({ state: "online", attached: false }),
  130. reconnect,
  131. openWizard,
  132. });
  133. expect(reconnect).toHaveBeenCalledOnce();
  134. expect(openWizard).not.toHaveBeenCalled();
  135. });