conversation-session-ssh-disconnect-experience.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { mkdir } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { parseRemoteWorkspaceIdentity } from "@zcode/shared";
  4. import { TID_SETTINGS_BACK_BUTTON, TID_SETTINGS_PAGE } from "@zcode/shared/test-ids";
  5. import { clearAppData, clickTestIdByDom, readSettings } from "#e2e/helpers/desktop-app";
  6. import {
  7. disconnectRemoteSource,
  8. connectRemoteSourceFromSettings,
  9. requestRemoteProjectConnection,
  10. } from "#e2e/helpers/remote-project-actions";
  11. import {
  12. prepareV4ConversationE2E,
  13. setV4ComposerText,
  14. waitForV4ComposerText,
  15. getV4QueueItems,
  16. getV4Messages,
  17. } from "#e2e/helpers/v4-conversation";
  18. import {
  19. prepareSSHConnectionPage,
  20. readSSHRuntimeConfig,
  21. openSSHRemoteDialog,
  22. fillSSHPasswordSettings,
  23. clickLastEnabledSSHDialogButton,
  24. waitForSSHDirectoryStep,
  25. selectSSHRemoteDirectory,
  26. waitForRemoteTab,
  27. } from "#e2e/helpers/ssh-remote-p0";
  28. const CASE_MARKER = "E2E_RDUX_08_DISCONNECT";
  29. const DRAFT = `${CASE_MARKER}_UNSENT_DRAFT`;
  30. describe(`${CASE_MARKER}: 断线会话与原地重连`, () => {
  31. after(async () => {
  32. await browser.electron.restoreAllMocks();
  33. await clearAppData();
  34. });
  35. it("RDUX-08-01/02/03 设置页主动断开后不空白、草稿不提交,项目原地重连与设置页重连", async function () {
  36. this.timeout(20 * 60_000);
  37. const config = readSSHRuntimeConfig(CASE_MARKER);
  38. await prepareSSHConnectionPage(CASE_MARKER);
  39. await prepareV4ConversationE2E({ skipProvider: true });
  40. await openSSHRemoteDialog(CASE_MARKER);
  41. await fillSSHPasswordSettings(config);
  42. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  43. await waitForSSHDirectoryStep(CASE_MARKER);
  44. await selectSSHRemoteDirectory(config.workspacePaths[0], CASE_MARKER);
  45. const tab = await waitForRemoteTab({
  46. caseMarker: CASE_MARKER,
  47. expectedPath: config.workspacePaths[0],
  48. });
  49. if (!tab.workspaceIdentity) throw new Error(`${CASE_MARKER}: 缺少 workspace identity`);
  50. const sourceId = parseRemoteWorkspaceIdentity(tab.workspaceIdentity)?.sourceServerId;
  51. if (!sourceId) throw new Error(`${CASE_MARKER}: 缺少 Source identity`);
  52. await setV4ComposerText(DRAFT);
  53. // 走原空白页路径:会话 -> 设置设备卡断开 -> 返回原会话。
  54. await disconnectRemoteSource(sourceId, CASE_MARKER);
  55. const banner = $('[data-testid="remote-conversation-banner"]');
  56. await banner.waitForDisplayed({ timeout: 30_000 });
  57. expect(await banner.getAttribute("data-connection-state")).toBe("offline");
  58. expect(
  59. await browser.execute(
  60. () =>
  61. document.body.textContent?.includes("设备未连接,连接后继续此会话") ||
  62. document.body.textContent?.includes("Reconnect to continue this conversation."),
  63. ),
  64. ).toBe(true);
  65. expect(
  66. await browser.execute(
  67. (draft) =>
  68. Array.from(document.querySelectorAll<HTMLElement>("[contenteditable], textarea")).some(
  69. (input) =>
  70. (input instanceof HTMLTextAreaElement ? input.value : input.textContent)?.includes(
  71. draft,
  72. ),
  73. ),
  74. DRAFT,
  75. ),
  76. ).toBe(true);
  77. const artifactDir = resolve(process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/rdux08");
  78. await mkdir(artifactDir, { recursive: true });
  79. const originalSize = await browser.electron.execute((electron) =>
  80. electron.BrowserWindow.getAllWindows()
  81. .find((window) => !window.isDestroyed())
  82. ?.getSize(),
  83. );
  84. for (const theme of ["light", "dark"]) {
  85. await browser.execute((value) => {
  86. const actions = (window as Window & { __testActions?: { setTheme(value: string): void } })
  87. .__testActions;
  88. if (!actions) throw new Error("Missing theme test actions");
  89. actions.setTheme(value);
  90. }, theme);
  91. for (const width of [1400, 480]) {
  92. await browser.electron.execute((electron, nextWidth) => {
  93. electron.BrowserWindow.getAllWindows()
  94. .find((window) => !window.isDestroyed())
  95. ?.setSize(nextWidth, 900);
  96. }, width);
  97. await browser.executeAsync((done) =>
  98. requestAnimationFrame(() => requestAnimationFrame(() => done())),
  99. );
  100. // 窄窗口会自动收起侧栏;连续采样确认位置稳定,避免把动画帧当作视觉证据。
  101. let previousLayout = "";
  102. let stableSamples = 0;
  103. await browser.waitUntil(
  104. async () => {
  105. const layout = await browser.execute(() => {
  106. const banner = document
  107. .querySelector('[data-testid="remote-conversation-banner"]')
  108. ?.getBoundingClientRect();
  109. return banner
  110. ? {
  111. left: Math.round(banner.left * 100) / 100,
  112. right: Math.round(banner.right * 100) / 100,
  113. width: banner.width,
  114. viewport: window.innerWidth,
  115. }
  116. : null;
  117. });
  118. const serialized = JSON.stringify(layout);
  119. stableSamples = serialized === previousLayout ? stableSamples + 1 : 0;
  120. previousLayout = serialized;
  121. return Boolean(
  122. layout &&
  123. layout.width > 0 &&
  124. layout.left >= -1 &&
  125. layout.right <= layout.viewport + 1 &&
  126. stableSamples >= 2,
  127. );
  128. },
  129. {
  130. timeout: 5000,
  131. interval: 100,
  132. timeoutMsg: `${CASE_MARKER}: ${theme}/${width} 横幅布局未稳定或超出窗口`,
  133. },
  134. );
  135. const noControlOverlap = await browser.execute(() => {
  136. const reason = document.querySelector('[data-testid="remote-send-unavailable"]');
  137. const leading = document.querySelector("[data-composer-leading-content]");
  138. return (
  139. !reason ||
  140. !leading ||
  141. reason.getBoundingClientRect().left >= leading.getBoundingClientRect().right - 1
  142. );
  143. });
  144. await browser.saveScreenshot(
  145. resolve(artifactDir, `disconnected-conversation-${theme}-${width}.png`),
  146. );
  147. expect(noControlOverlap).toBe(true);
  148. }
  149. }
  150. if (originalSize)
  151. await browser.electron.execute((electron, size) => {
  152. electron.BrowserWindow.getAllWindows()
  153. .find((window) => !window.isDestroyed())
  154. ?.setSize(size[0]!, size[1]!);
  155. }, originalSize);
  156. await browser.executeAsync((done) =>
  157. requestAnimationFrame(() => requestAnimationFrame(() => done())),
  158. );
  159. const projectOpen = $(
  160. `[data-project-workspace-identity="${tab.workspaceIdentity}"] [data-testid="virtual-project-toggle"] .truncate.text-ui-base`,
  161. );
  162. if (!(await projectOpen.isDisplayed())) {
  163. for (const toggle of await $$('button[aria-label="切换侧边栏"]')) {
  164. if (await toggle.isDisplayed()) {
  165. await toggle.click();
  166. break;
  167. }
  168. }
  169. await projectOpen.waitForDisplayed();
  170. }
  171. // 观察整个连接期间,不能只在成功之后断言向导已关闭。
  172. await browser.execute(() => {
  173. const observer = new MutationObserver(() => {
  174. if (document.querySelector('[data-testid="ssh-dialog"]'))
  175. document.body.dataset.rduxUnexpectedWizard = "true";
  176. });
  177. observer.observe(document.body, { subtree: true, childList: true });
  178. (window as unknown as { __rduxObserver: MutationObserver }).__rduxObserver = observer;
  179. });
  180. // 新标题只折叠历史;原地恢复应使用项目菜单的连接动作。
  181. await requestRemoteProjectConnection(tab.workspaceIdentity, CASE_MARKER);
  182. await waitForRemoteTab({
  183. caseMarker: CASE_MARKER,
  184. expectedIdentity: tab.workspaceIdentity,
  185. expectedPath: config.workspacePaths[0],
  186. requireActive: false,
  187. });
  188. expect(
  189. await browser.execute(() => {
  190. (window as unknown as { __rduxObserver: MutationObserver }).__rduxObserver.disconnect();
  191. return document.body.dataset.rduxUnexpectedWizard ?? "false";
  192. }),
  193. ).toBe("false");
  194. await banner.waitForDisplayed({ reverse: true, timeout: 30_000 });
  195. await waitForV4ComposerText(DRAFT, `${CASE_MARKER}: 重连丢失草稿`);
  196. expect(await getV4QueueItems()).toHaveLength(0);
  197. expect((await getV4Messages("user")).some((message) => message.text.includes(DRAFT))).toBe(
  198. false,
  199. );
  200. await disconnectRemoteSource(sourceId, CASE_MARKER);
  201. const countBefore = (await readSettings()).lastWorkspaceSession?.length;
  202. await connectRemoteSourceFromSettings(sourceId, CASE_MARKER);
  203. // 设置入口连接完成后留在设备管理;旧用例继续点击已移除的目录步骤导致假失败。
  204. expect(await waitForSSHDirectoryStep(CASE_MARKER)).toBe("closed");
  205. await $(`[data-testid="${TID_SETTINGS_PAGE}"]`).waitForDisplayed();
  206. await $(
  207. `[data-source-server-card="${sourceId}"] [data-remote-connection-status="online"]`,
  208. ).waitForDisplayed();
  209. expect(await $('[data-testid="virtual-project-name"]').isExisting()).toBe(false);
  210. expect((await readSettings()).lastWorkspaceSession?.length).toBe(countBefore);
  211. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON);
  212. await waitForV4ComposerText(DRAFT, `${CASE_MARKER}: 设置重连丢失草稿`);
  213. });
  214. });