conversation-session-ssh-remote-connection-p0.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { TID_SSH_PASSWORD_INPUT } from "@zcode/shared/test-ids";
  2. import {
  3. clearAppData,
  4. waitForWorkspaceApp,
  5. setInputValueByTestIdDom,
  6. } from "#e2e/helpers/desktop-app";
  7. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  8. import {
  9. clickLastEnabledSSHDialogButton,
  10. fillSSHPasswordSettings,
  11. openSSHRemoteDialog,
  12. openSSHSourceDirectory,
  13. readHostProcesses,
  14. readRemoteTabSnapshot,
  15. readSSHRuntimeConfig,
  16. prepareSSHConnectionPage,
  17. selectSSHRemoteDirectory,
  18. waitForRemoteTab,
  19. waitForSSHDirectoryStep,
  20. waitForSSHError,
  21. SSH_WORKSPACE_READY_TIMEOUT_MS,
  22. } from "#e2e/helpers/ssh-remote-p0";
  23. const CASE_MARKER = "SSH_P0_CONNECTION";
  24. async function readRemoteWorkspaceTabCount(): Promise<number> {
  25. return browser.execute(() => {
  26. const store = (
  27. window as Window & {
  28. __zcodeTabStoreE2E?: {
  29. getState: () => {
  30. tabs: Array<{
  31. remoteTarget?: unknown;
  32. remoteSessionId?: string;
  33. workspaceIdentity?: string;
  34. }>;
  35. };
  36. };
  37. }
  38. ).__zcodeTabStoreE2E;
  39. if (!store) throw new Error("SSH P0 缺少真实 tab store 取证入口");
  40. return store
  41. .getState()
  42. .tabs.filter(
  43. (tab) =>
  44. tab.remoteTarget || tab.remoteSessionId || tab.workspaceIdentity?.startsWith("remote:"),
  45. ).length;
  46. });
  47. }
  48. describe(`${CASE_MARKER}: SSH password 连接 P0`, () => {
  49. after(async () => {
  50. await browser.electron.restoreAllMocks();
  51. await clearAppData();
  52. });
  53. it("SSH-P0-CONN-02/01 错误密码失败后,以正确密码重试并选择远端目录", async function () {
  54. this.timeout(20 * 60_000);
  55. const config = readSSHRuntimeConfig(CASE_MARKER);
  56. const workspacePath = config.workspacePaths[0];
  57. await prepareSSHConnectionPage(CASE_MARKER);
  58. await prepareV4ConversationE2E({ skipProvider: true });
  59. await browser.execute(() => {
  60. const state = window as Window & {
  61. __connectionLogs?: Array<{ message: string; level: string }>;
  62. zcode?: {
  63. onRemoteConnectionLog: (
  64. callback: (entry: { message: string; level: string }) => void,
  65. ) => () => void;
  66. };
  67. };
  68. state.__connectionLogs = [];
  69. if (!state.zcode) throw new Error("Missing desktop platform");
  70. state.zcode.onRemoteConnectionLog((entry) =>
  71. state.__connectionLogs!.push({ message: entry.message, level: entry.level }),
  72. );
  73. });
  74. const initialHosts = await readHostProcesses();
  75. expect(initialHosts).toHaveLength(1);
  76. expect(await readRemoteWorkspaceTabCount()).toBe(0);
  77. await openSSHRemoteDialog(CASE_MARKER);
  78. const invalidPassword = `${config.password}-e2e-invalid`;
  79. await fillSSHPasswordSettings({
  80. ...config,
  81. password: invalidPassword,
  82. });
  83. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  84. const errorText = await waitForSSHError(CASE_MARKER);
  85. expect(errorText).not.toContain(config.password);
  86. expect(errorText).not.toContain(invalidPassword);
  87. expect((await readRemoteTabSnapshot(undefined, workspacePath)).remoteSessionId).toBeNull();
  88. expect(await readRemoteWorkspaceTabCount()).toBe(0);
  89. expect(await readHostProcesses()).toEqual(initialHosts);
  90. const authenticationState = await browser.execute(() => ({
  91. inlineCredentials: Boolean(
  92. document.querySelector('[data-testid="ssh-error"] input[type="password"]')?.closest("li"),
  93. ),
  94. failureText: document.querySelector('[data-testid="ssh-error"]')?.textContent,
  95. visibleLog: document.querySelector('[data-testid="remote-connection-log"]')?.textContent,
  96. }));
  97. expect(authenticationState).toMatchObject({ inlineCredentials: true });
  98. expect(await $('[data-testid="remote-connection-settings-scroll"]').isExisting()).toBe(false);
  99. expect(await $('[data-testid="remote-connection-log"]').isExisting()).toBe(true);
  100. await setInputValueByTestIdDom(TID_SSH_PASSWORD_INPUT, config.password);
  101. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  102. const retryFlow = await waitForSSHDirectoryStep(CASE_MARKER);
  103. if (retryFlow === "closed") {
  104. // 已保存线路可能直接恢复原项目;从该 Source 的目录入口选择本次目标。
  105. await openSSHSourceDirectory(config, CASE_MARKER);
  106. }
  107. await selectSSHRemoteDirectory(workspacePath, CASE_MARKER);
  108. await waitForWorkspaceApp(workspacePath, SSH_WORKSPACE_READY_TIMEOUT_MS);
  109. const tab = await waitForRemoteTab({
  110. caseMarker: CASE_MARKER,
  111. expectedPath: workspacePath,
  112. });
  113. expect(tab.remoteKind).toBe("ssh");
  114. expect(tab.remoteHost).toBe(config.host);
  115. expect(tab.secretPersistedInTab).toBe(false);
  116. const logs = await browser.execute(
  117. () =>
  118. (window as Window & { __connectionLogs?: Array<{ message: string; level: string }> })
  119. .__connectionLogs ?? [],
  120. );
  121. expect(logs.length).toBeGreaterThan(0);
  122. const text = logs.map((entry) => entry.message).join("\n");
  123. for (const secret of [
  124. config.password,
  125. invalidPassword,
  126. "setting.json",
  127. "workspacePath",
  128. "credentialRef",
  129. "profileId",
  130. "sourceServerId",
  131. ]) {
  132. // 测试密码可能是用户名等短词;按词边界判定,避免把 device 里的 dev 当成凭据泄漏。
  133. if (secret === config.password && secret.length < 8) {
  134. const escaped = secret.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
  135. expect(text).not.toMatch(
  136. new RegExp(`(?<![\\p{L}\\p{N}_])${escaped}(?![\\p{L}\\p{N}_])`, "u"),
  137. );
  138. } else expect(text).not.toContain(secret);
  139. }
  140. expect(text).not.toMatch(
  141. /\/Users\/|[A-Za-z]:\\Users\\|[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/iu,
  142. );
  143. const confirmation = logs.filter((entry) =>
  144. entry.message.includes("requires explicit user confirmation"),
  145. );
  146. for (const entry of confirmation) expect(entry.level).toBe("info");
  147. expect(await readHostProcesses()).toEqual(initialHosts);
  148. });
  149. });