conversation-session-ssh-remote-wizard-credentials.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {
  2. disconnectRemoteSource,
  3. requestRemoteProjectConnection,
  4. } from "#e2e/helpers/remote-project-actions";
  5. import { TID_CONFIRM_DIALOG_CONFIRM, TID_SSH_PASSWORD_INPUT } from "@zcode/shared/test-ids";
  6. import { reloadElectronSessionPreservingBrowserProfile } from "#e2e/helpers/e2e-electron-reload";
  7. import {
  8. clearAppData,
  9. removeSeededCredentials,
  10. readSettings,
  11. setInputValueByTestIdDom,
  12. waitForWorkspaceApp,
  13. } from "#e2e/helpers/desktop-app";
  14. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  15. import {
  16. prepareSSHConnectionPage,
  17. openSSHSourceDirectory,
  18. readSSHRuntimeConfig,
  19. clickLastEnabledSSHDialogButton,
  20. waitForSSHDirectoryStep,
  21. selectSSHRemoteDirectory,
  22. openSSHRemoteDialog,
  23. fillSSHPasswordSettings,
  24. SSH_WORKSPACE_READY_TIMEOUT_MS,
  25. waitForRemoteTab,
  26. } from "#e2e/helpers/ssh-remote-p0";
  27. const CASE_MARKER = "E2E_SSH_P0_WIZARD_CREDENTIALS";
  28. async function waitForPasswordFocus(caseMarker: string): Promise<void> {
  29. await browser.waitUntil(
  30. () =>
  31. browser.execute(
  32. (testId) => document.activeElement?.getAttribute("data-testid") === testId,
  33. TID_SSH_PASSWORD_INPUT,
  34. ),
  35. { timeout: 15_000, timeoutMsg: `${caseMarker}: 密码框未获得焦点` },
  36. );
  37. }
  38. // 用标题栏的关闭按钮(屏幕阅读器名称为“关闭”)关闭向导,与用户点击 × 的路径一致。
  39. async function closeSSHDialog(caseMarker: string): Promise<void> {
  40. const clicked = await browser.execute(() => {
  41. const button = Array.from(
  42. document.querySelectorAll<HTMLButtonElement>('[data-testid="ssh-dialog"] button'),
  43. ).find((item) => /^(关闭|Close)$/u.test(item.textContent?.trim() ?? "") && !item.disabled);
  44. button?.click();
  45. return Boolean(button);
  46. });
  47. if (!clicked) throw new Error(`${caseMarker}: 向导没有可用的关闭按钮`);
  48. const discard = $(`[data-testid="${TID_CONFIRM_DIALOG_CONFIRM}"]`);
  49. if (await discard.waitForDisplayed({ timeout: 5_000 }).catch(() => false)) {
  50. await discard.click();
  51. }
  52. await $('[data-testid="ssh-dialog"]').waitForDisplayed({ reverse: true, timeout: 30_000 });
  53. }
  54. async function readStageTitles(): Promise<string[]> {
  55. return browser.execute(() =>
  56. Array.from(
  57. document.querySelectorAll<HTMLElement>('[data-testid="remote-connection-stages"] > li'),
  58. ).map((item) => item.innerText.trim()),
  59. );
  60. }
  61. describe(`${CASE_MARKER}: 向导凭据与已在线复用`, () => {
  62. after(async () => {
  63. await browser.electron.restoreAllMocks();
  64. await clearAppData();
  65. });
  66. it("SSH-P0-CONN-06/07 缺密码可回填,手动连接在线目标明确提示复用", async function () {
  67. this.timeout(20 * 60_000);
  68. const config = readSSHRuntimeConfig(CASE_MARKER);
  69. await prepareSSHConnectionPage(CASE_MARKER);
  70. await prepareV4ConversationE2E({ skipProvider: true });
  71. await openSSHRemoteDialog(CASE_MARKER);
  72. await fillSSHPasswordSettings(config);
  73. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  74. await waitForSSHDirectoryStep(CASE_MARKER);
  75. await selectSSHRemoteDirectory(config.workspacePaths[0], CASE_MARKER);
  76. const tab = await waitForRemoteTab({
  77. caseMarker: CASE_MARKER,
  78. expectedPath: config.workspacePaths[0],
  79. });
  80. const settings = await readSettings();
  81. const profile = settings.connectionProfiles?.find(
  82. (candidate) =>
  83. candidate.target.kind === "ssh" &&
  84. candidate.target.host === config.host &&
  85. candidate.target.username === config.username,
  86. );
  87. if (!profile?.sourceServerId || !tab.workspaceIdentity)
  88. throw new Error(`${CASE_MARKER}: 首次真实连接未保存设备身份`);
  89. await disconnectRemoteSource(profile.sourceServerId, CASE_MARKER);
  90. // 旧 Host 完全退出后再删除凭据值,保留引用,避免活跃 writer 覆盖用例前置。
  91. await reloadElectronSessionPreservingBrowserProfile(browser, {
  92. afterElectronProcessExit: async () => {
  93. const latest = await readSettings();
  94. const keys = (latest.connectionProfiles ?? []).flatMap((candidate) =>
  95. candidate.profileId === profile.profileId && candidate.target.kind === "ssh"
  96. ? [
  97. candidate.target.passwordCredentialKey,
  98. candidate.target.privateKeyPassphraseCredentialKey,
  99. ]
  100. : [],
  101. );
  102. for (const entry of latest.lastWorkspaceSession ?? []) {
  103. if (
  104. entry.kind === "remote" &&
  105. entry.target.kind === "ssh" &&
  106. entry.target.host === config.host
  107. )
  108. keys.push(
  109. entry.target.passwordCredentialKey,
  110. entry.target.privateKeyPassphraseCredentialKey,
  111. );
  112. }
  113. await removeSeededCredentials(keys.filter((key): key is string => Boolean(key)));
  114. },
  115. });
  116. // R8 跟进:缺凭据的项目重连在补密码时就显示“打开项目目录”这一步;关闭后设备级补密码仍是四步,
  117. // 不沿用上一次连接的目录。
  118. await requestRemoteProjectConnection(tab.workspaceIdentity, CASE_MARKER);
  119. await waitForPasswordFocus(CASE_MARKER);
  120. const projectStages = await readStageTitles();
  121. expect(projectStages).toHaveLength(5);
  122. expect(projectStages.at(-1)).toMatch(/打开项目目录|Open project folder/u);
  123. await closeSSHDialog(CASE_MARKER);
  124. // 已保存设备的连接入口在项目表单的设备选择里:选中离线设备并浏览目录时由向导补凭据。
  125. await openSSHSourceDirectory(config, CASE_MARKER);
  126. await waitForPasswordFocus(CASE_MARKER);
  127. const deviceStages = await readStageTitles();
  128. expect(deviceStages).toHaveLength(4);
  129. expect(deviceStages.some((title) => /打开项目目录|Open project folder/u.test(title))).toBe(
  130. false,
  131. );
  132. await setInputValueByTestIdDom(TID_SSH_PASSWORD_INPUT, config.password);
  133. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  134. expect(await waitForSSHDirectoryStep(CASE_MARKER)).toBe("directory");
  135. await selectSSHRemoteDirectory(config.workspacePaths[0], CASE_MARKER);
  136. await waitForWorkspaceApp(config.workspacePaths[0], SSH_WORKSPACE_READY_TIMEOUT_MS);
  137. await openSSHRemoteDialog(CASE_MARKER);
  138. await fillSSHPasswordSettings({ ...config, password: "unused-online-reuse-password" });
  139. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  140. await browser.waitUntil(
  141. () =>
  142. browser.execute(() =>
  143. /已直接使用现有连接|existing connection is now in use/u.test(
  144. document.body.textContent ?? "",
  145. ),
  146. ),
  147. { timeout: 30_000, timeoutMsg: `${CASE_MARKER}: 复用在线连接没有提示` },
  148. );
  149. expect(await waitForSSHDirectoryStep(CASE_MARKER)).toBe("directory");
  150. expect(await $('[data-testid="ssh-error"]').isExisting()).toBe(false);
  151. });
  152. });