sshConnectionCompletion.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { runInNewContext } from "node:vm";
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { ensureSSHProjectDirectoryStep, waitForSSHDirectoryStep } from "#e2e/helpers/ssh-remote-p0";
  4. vi.mock("#e2e/helpers/desktop-app", () => ({}));
  5. vi.mock("#e2e/helpers/custom-openai-provider-store", () => ({}));
  6. vi.mock("#e2e/helpers/v4-conversation", () => ({
  7. startNewV4Draft: vi.fn(() => {
  8. throw new Error("当前项目选择流程仍在进行,不应新建草稿并重开向导");
  9. }),
  10. }));
  11. type Step =
  12. | "install"
  13. | "connecting"
  14. | "directory"
  15. | "project-form"
  16. | "closing-form"
  17. | "project-picker"
  18. | "project-picker-loading"
  19. | "project-picker-error"
  20. | "closed"
  21. | "error"
  22. | "updating"
  23. | "unexpected-confirm";
  24. function installBrowserFixture(initialStep: Step, followingSteps: Step[] = []) {
  25. let step = initialStep;
  26. const install = {
  27. textContent: "Install and continue",
  28. disabled: false,
  29. getClientRects: () => [1],
  30. click: vi.fn(() => {
  31. step = "closed";
  32. }),
  33. };
  34. const dialog = { querySelectorAll: () => (step === "install" ? [install] : []) };
  35. const confirmation = {
  36. disabled: false,
  37. getClientRects: () => [1],
  38. closest: () => ({ dataset: { confirmIntent: "source-server-force-update" } }),
  39. click: vi.fn(),
  40. };
  41. const document = {
  42. querySelectorAll: () =>
  43. ["project-form", "closing-form"].includes(step)
  44. ? [
  45. {
  46. closest: () => ({
  47. getAttribute: () => (step === "project-form" ? "open" : "closed"),
  48. }),
  49. },
  50. ]
  51. : [],
  52. querySelector: (selector: string) => {
  53. if (selector === '[data-testid="ssh-dialog"]')
  54. return [
  55. "closed",
  56. "project-picker",
  57. "project-picker-loading",
  58. "project-picker-error",
  59. ].includes(step)
  60. ? null
  61. : dialog;
  62. if (selector === '[data-testid="remote-project-new"]' && step.startsWith("project-picker"))
  63. return {
  64. disabled: step !== "project-picker",
  65. closest: () => ({
  66. querySelector: () =>
  67. step === "project-picker-error"
  68. ? {
  69. getClientRects: () => [1],
  70. innerText: "SourceServerWorkspaceIdentityConflictError: /work",
  71. }
  72. : null,
  73. }),
  74. };
  75. if (selector === '[data-testid="ssh-success"]' && step === "directory") {
  76. return { getClientRects: () => [1] };
  77. }
  78. if (selector === '[data-testid="ssh-error"]' && ["error", "updating"].includes(step)) {
  79. return {
  80. getClientRects: () => [1],
  81. innerText: step === "updating" ? "Updating\nCancel connection" : "Authentication failed",
  82. };
  83. }
  84. if (
  85. selector === '[data-testid="ssh-dialog"] [data-remote-connection-status="connecting"]' &&
  86. step === "updating"
  87. ) {
  88. return { getClientRects: () => [1] };
  89. }
  90. if (selector === '[data-testid="confirm-dialog-confirm"]' && step === "unexpected-confirm") {
  91. return confirmation;
  92. }
  93. return null;
  94. },
  95. };
  96. const waitUntil = vi.fn(
  97. async (predicate: () => Promise<boolean>, options: { timeoutMsg: string }) => {
  98. // 虚拟轮询只控制浏览器边界;独立 VM 执行实际 helper 回调,避免复制被测状态机。
  99. for (let attempt = 0; attempt < 3; attempt += 1) {
  100. if (await predicate()) return;
  101. step = followingSteps.shift() ?? step;
  102. }
  103. throw new Error(options.timeoutMsg);
  104. },
  105. );
  106. vi.stubGlobal("browser", {
  107. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  108. runInNewContext(`(${fn.toString()})(...args)`, { document, args }),
  109. waitUntil,
  110. });
  111. return { install, confirmation };
  112. }
  113. afterEach(() => vi.unstubAllGlobals());
  114. describe("SSH 连接完成等待", () => {
  115. it("向导实际关闭且没有项目选择页时返回 closed", async () => {
  116. installBrowserFixture("closed");
  117. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).resolves.toBe("closed");
  118. });
  119. it("首装确认后向导关闭也能完成", async () => {
  120. const { install } = installBrowserFixture("install");
  121. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).resolves.toBe("closed");
  122. expect(install.click).toHaveBeenCalledOnce();
  123. });
  124. it("目录页仍返回独立的 directory 完成态", async () => {
  125. installBrowserFixture("directory");
  126. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).resolves.toBe("directory");
  127. });
  128. it("项目选择页替换连接向导后继续当前流程,不再打开被遮挡的侧栏", async () => {
  129. installBrowserFixture("project-picker");
  130. await expect(
  131. ensureSSHProjectDirectoryStep(
  132. { host: "127.0.0.1", port: 43210, username: "dev", password: "fixture" },
  133. "E2E_SSH_COMPLETION",
  134. ),
  135. ).resolves.toBeUndefined();
  136. });
  137. it("项目列表尚在加载时不把连接向导消失当作完成", async () => {
  138. installBrowserFixture("project-picker-loading");
  139. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  140. "SSH 部署/连接没有完成",
  141. );
  142. });
  143. it("连接中不提前宣告成功", async () => {
  144. installBrowserFixture("connecting");
  145. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  146. "SSH 部署/连接没有完成",
  147. );
  148. });
  149. it("项目列表错误立即带出原始原因,不等待被错误禁用的新项目按钮", async () => {
  150. installBrowserFixture("project-picker-error");
  151. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  152. "SourceServerWorkspaceIdentityConflictError: /work",
  153. );
  154. });
  155. it("连接错误直接携带原始原因失败", async () => {
  156. installBrowserFixture("error");
  157. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  158. "SSH 连接失败: Authentication failed",
  159. );
  160. });
  161. it("升级期间的错误面板继续等待,目录入口就绪后才完成", async () => {
  162. installBrowserFixture("updating", ["updating", "directory"]);
  163. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).resolves.toBe("directory");
  164. });
  165. it("升级失败后仍立即报告错误", async () => {
  166. installBrowserFixture("updating", ["error"]);
  167. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  168. "SSH 连接失败: Authentication failed",
  169. );
  170. });
  171. it("升级一直未结束时保留原有部署超时边界", async () => {
  172. installBrowserFixture("updating");
  173. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  174. "SSH 部署/连接没有完成",
  175. );
  176. });
  177. it("不确认安装和边界提示以外的动作", async () => {
  178. const { confirmation } = installBrowserFixture("unexpected-confirm");
  179. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  180. "unexpected confirmation intent: source-server-force-update",
  181. );
  182. expect(confirmation.click).not.toHaveBeenCalled();
  183. });
  184. });
  185. it("已有设备连接完成回到打开的项目表单,目录意图可以继续", async () => {
  186. installBrowserFixture("project-form");
  187. await expect(waitForSSHDirectoryStep("E2E_PROJECT_FORM")).resolves.toBe("directory");
  188. });
  189. it("原表单正在收起时不把未完成连接误判为完成", async () => {
  190. installBrowserFixture("closing-form");
  191. await expect(waitForSSHDirectoryStep("E2E_PROJECT_FORM")).rejects.toThrow(
  192. "SSH 部署/连接没有完成",
  193. );
  194. });