sshConnectionCompletion.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. | "unexpected-confirm";
  23. function installBrowserFixture(initialStep: Step) {
  24. let step = initialStep;
  25. const install = {
  26. textContent: "Install and continue",
  27. disabled: false,
  28. getClientRects: () => [1],
  29. click: vi.fn(() => {
  30. step = "closed";
  31. }),
  32. };
  33. const dialog = { querySelectorAll: () => (step === "install" ? [install] : []) };
  34. const confirmation = {
  35. disabled: false,
  36. getClientRects: () => [1],
  37. closest: () => ({ dataset: { confirmIntent: "source-server-force-update" } }),
  38. click: vi.fn(),
  39. };
  40. const document = {
  41. querySelectorAll: () =>
  42. ["project-form", "closing-form"].includes(step)
  43. ? [
  44. {
  45. closest: () => ({
  46. getAttribute: () => (step === "project-form" ? "open" : "closed"),
  47. }),
  48. },
  49. ]
  50. : [],
  51. querySelector: (selector: string) => {
  52. if (selector === '[data-testid="ssh-dialog"]')
  53. return [
  54. "closed",
  55. "project-picker",
  56. "project-picker-loading",
  57. "project-picker-error",
  58. ].includes(step)
  59. ? null
  60. : dialog;
  61. if (selector === '[data-testid="remote-project-new"]' && step.startsWith("project-picker"))
  62. return {
  63. disabled: step !== "project-picker",
  64. closest: () => ({
  65. querySelector: () =>
  66. step === "project-picker-error"
  67. ? {
  68. getClientRects: () => [1],
  69. innerText: "SourceServerWorkspaceIdentityConflictError: /work",
  70. }
  71. : null,
  72. }),
  73. };
  74. if (selector === '[data-testid="ssh-success"]' && step === "directory") {
  75. return { getClientRects: () => [1] };
  76. }
  77. if (selector === '[data-testid="ssh-error"]' && step === "error") {
  78. return { getClientRects: () => [1], innerText: "Authentication failed" };
  79. }
  80. if (selector === '[data-testid="confirm-dialog-confirm"]' && step === "unexpected-confirm") {
  81. return confirmation;
  82. }
  83. return null;
  84. },
  85. };
  86. const waitUntil = vi.fn(
  87. async (predicate: () => Promise<boolean>, options: { timeoutMsg: string }) => {
  88. // 虚拟轮询只控制浏览器边界;独立 VM 执行实际 helper 回调,避免复制被测状态机。
  89. for (let attempt = 0; attempt < 3; attempt += 1) {
  90. if (await predicate()) return;
  91. }
  92. throw new Error(options.timeoutMsg);
  93. },
  94. );
  95. vi.stubGlobal("browser", {
  96. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  97. runInNewContext(`(${fn.toString()})(...args)`, { document, args }),
  98. waitUntil,
  99. });
  100. return { install, confirmation };
  101. }
  102. afterEach(() => vi.unstubAllGlobals());
  103. describe("SSH 连接完成等待", () => {
  104. it("向导实际关闭且没有项目选择页时返回 closed", async () => {
  105. installBrowserFixture("closed");
  106. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).resolves.toBe("closed");
  107. });
  108. it("首装确认后向导关闭也能完成", async () => {
  109. const { install } = installBrowserFixture("install");
  110. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).resolves.toBe("closed");
  111. expect(install.click).toHaveBeenCalledOnce();
  112. });
  113. it("目录页仍返回独立的 directory 完成态", async () => {
  114. installBrowserFixture("directory");
  115. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).resolves.toBe("directory");
  116. });
  117. it("项目选择页替换连接向导后继续当前流程,不再打开被遮挡的侧栏", async () => {
  118. installBrowserFixture("project-picker");
  119. await expect(
  120. ensureSSHProjectDirectoryStep(
  121. { host: "127.0.0.1", port: 43210, username: "dev", password: "fixture" },
  122. "E2E_SSH_COMPLETION",
  123. ),
  124. ).resolves.toBeUndefined();
  125. });
  126. it("项目列表尚在加载时不把连接向导消失当作完成", async () => {
  127. installBrowserFixture("project-picker-loading");
  128. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  129. "SSH 部署/连接没有完成",
  130. );
  131. });
  132. it("连接中不提前宣告成功", async () => {
  133. installBrowserFixture("connecting");
  134. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  135. "SSH 部署/连接没有完成",
  136. );
  137. });
  138. it("项目列表错误立即带出原始原因,不等待被错误禁用的新项目按钮", async () => {
  139. installBrowserFixture("project-picker-error");
  140. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  141. "SourceServerWorkspaceIdentityConflictError: /work",
  142. );
  143. });
  144. it("连接错误直接携带原始原因失败", async () => {
  145. installBrowserFixture("error");
  146. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  147. "SSH 连接失败: Authentication failed",
  148. );
  149. });
  150. it("不确认安装和边界提示以外的动作", async () => {
  151. const { confirmation } = installBrowserFixture("unexpected-confirm");
  152. await expect(waitForSSHDirectoryStep("E2E_SSH_COMPLETION")).rejects.toThrow(
  153. "unexpected confirmation intent: source-server-force-update",
  154. );
  155. expect(confirmation.click).not.toHaveBeenCalled();
  156. });
  157. });
  158. it("已有设备连接完成回到打开的项目表单,目录意图可以继续", async () => {
  159. installBrowserFixture("project-form");
  160. await expect(waitForSSHDirectoryStep("E2E_PROJECT_FORM")).resolves.toBe("directory");
  161. });
  162. it("原表单正在收起时不把未完成连接误判为完成", async () => {
  163. installBrowserFixture("closing-form");
  164. await expect(waitForSSHDirectoryStep("E2E_PROJECT_FORM")).rejects.toThrow(
  165. "SSH 部署/连接没有完成",
  166. );
  167. });