conversation-session-ssh-remote-cold-reconnect.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { reloadElectronSessionPreservingBrowserProfile } from "#e2e/helpers/e2e-electron-reload";
  2. import {
  3. disconnectRemoteSource,
  4. requestRemoteProjectConnection,
  5. } from "#e2e/helpers/remote-project-actions";
  6. import { parseRemoteWorkspaceIdentity } from "@zcode/shared";
  7. import { clearAppData, readSettings, seedSettings } from "#e2e/helpers/desktop-app";
  8. import { selectUpstreamProviderModelById } from "#e2e/helpers/upstream-provider";
  9. import {
  10. connectSSHWorkspaceAndSendFirstTurn,
  11. prepareSSHLifecycle,
  12. readTaskRowInProject,
  13. sendSSHFollowTurn,
  14. waitForRendererAfterColdRestart,
  15. type SSHLifecycleProvider,
  16. type SSHLifecycleTask,
  17. SSH_LIFECYCLE_MODEL_ID,
  18. } from "#e2e/helpers/ssh-remote-lifecycle";
  19. import {
  20. prepareSSHConnectionPage,
  21. readHostProcesses,
  22. readRemoteTabSnapshot,
  23. waitForRemoteTab,
  24. } from "#e2e/helpers/ssh-remote-p0";
  25. import {
  26. selectTaskOrganizeMode,
  27. openWorkspaceDraftFromSidebar,
  28. } from "#e2e/helpers/workspace-agent-warmup";
  29. import {
  30. getV4PaneSnapshot,
  31. selectV4TaskById,
  32. waitForV4ConversationState,
  33. waitForV4AssistantMessageContaining,
  34. waitForV4Pane,
  35. } from "#e2e/helpers/v4-conversation";
  36. const CASE_MARKER = "E2E_SSH_P0_COLD_RECONNECT";
  37. describe(`${CASE_MARKER}: SSH cold restart and automatic reconnect`, () => {
  38. let provider: SSHLifecycleProvider | null = null;
  39. after(async () => {
  40. await provider?.tunnel.close().catch(() => undefined);
  41. provider = null;
  42. await browser.electron.restoreAllMocks();
  43. await clearAppData();
  44. });
  45. it("SSH-P0-LIFE-01 App 冷重启后连接原项目并进入草稿,手动打开原 task", async function () {
  46. this.timeout(35 * 60_000);
  47. await prepareSSHConnectionPage(CASE_MARKER);
  48. const prepared = await prepareSSHLifecycle(CASE_MARKER);
  49. provider = prepared.provider;
  50. const [workspaceA, workspaceB] = prepared.config.workspacePaths;
  51. const taskA = await connectSSHWorkspaceAndSendFirstTurn({
  52. caseMarker: CASE_MARKER,
  53. config: prepared.config,
  54. label: "A",
  55. provider,
  56. workspacePath: workspaceA,
  57. });
  58. const taskB = await connectSSHWorkspaceAndSendFirstTurn({
  59. caseMarker: CASE_MARKER,
  60. config: prepared.config,
  61. label: "B",
  62. provider,
  63. workspacePath: workspaceB,
  64. });
  65. const oldHosts = await readHostProcesses();
  66. const sourceId = parseRemoteWorkspaceIdentity(taskA.identity)?.sourceServerId;
  67. expect(sourceId).toBeTruthy();
  68. await selectTaskOrganizeMode(["项目", "Project"]);
  69. await disconnectRemoteSource(sourceId!, CASE_MARKER, {
  70. workspaceIdentities: [taskA.identity, taskB.identity],
  71. });
  72. const offlineA = $(`[data-project-workspace-identity="${taskA.identity}"]`);
  73. expect(await offlineA.isDisplayed()).toBe(true);
  74. expect(["offline", "failed"]).toContain(await offlineA.getAttribute("data-connection-state"));
  75. await requestRemoteProjectConnection(taskB.identity, CASE_MARKER);
  76. await waitForRemoteTab({
  77. caseMarker: CASE_MARKER,
  78. expectedIdentity: taskB.identity,
  79. expectedPath: workspaceB,
  80. });
  81. await waitForV4ConversationState(
  82. (snapshot) => snapshot.taskId === taskB.taskId,
  83. `${CASE_MARKER}: 原地重连没有保留原会话`,
  84. 60_000,
  85. );
  86. await waitForV4AssistantMessageContaining(taskB.firstReply);
  87. await disconnectRemoteSource(sourceId!, CASE_MARKER);
  88. await browser.waitUntil(
  89. async () => {
  90. const settings = await readSettings();
  91. return (
  92. settings.lastWorkspaceSession?.[settings.lastActiveTabIndex ?? 0]?.workspacePath ===
  93. workspaceB
  94. );
  95. },
  96. { timeout: 15_000, timeoutMsg: `${CASE_MARKER}: 原激活项目没有持久化` },
  97. );
  98. // browser.reloadSession 才会重启 Electron/Main/Host/CLI;renderer reload 不足以证明冷恢复。
  99. await reloadElectronSessionPreservingBrowserProfile(browser);
  100. await waitForRendererAfterColdRestart(CASE_MARKER);
  101. // F5:冷启动自动恢复最后活动的 B;A 保留项目入口,稍后按需打开。
  102. const reconnectedB = await waitForRemoteTab({
  103. caseMarker: CASE_MARKER,
  104. expectedIdentity: taskB.identity,
  105. expectedPath: workspaceB,
  106. requireActive: false,
  107. });
  108. expect((await readRemoteTabSnapshot(taskA.identity)).secretPersistedInTab).toBe(false);
  109. expect((await readRemoteTabSnapshot(taskB.identity)).secretPersistedInTab).toBe(false);
  110. expect(reconnectedB.remoteSessionId).not.toBe(taskB.remoteSessionId);
  111. expect(await readHostProcesses()).toHaveLength(1);
  112. // 旧用例混淆冷启动与原地重连;冷启动保留项目,但必须由用户显式选择历史任务。
  113. expect((await readRemoteTabSnapshot(taskB.identity)).activeWorkspaceIdentity).toBe(
  114. taskB.identity,
  115. );
  116. await waitForV4Pane(
  117. (snapshot) => snapshot.sessionId === "draft",
  118. `${CASE_MARKER}: 冷启动没有进入 B 项目草稿`,
  119. 60_000,
  120. );
  121. await selectV4TaskById(taskB.taskId, 60_000);
  122. await waitForV4AssistantMessageContaining(taskB.firstReply);
  123. expect((await readRemoteTabSnapshot(taskA.identity)).remoteSessionId).toBeNull();
  124. await openWorkspaceDraftFromSidebar(workspaceA);
  125. const reconnectedA = await waitForRemoteTab({
  126. caseMarker: CASE_MARKER,
  127. expectedIdentity: taskA.identity,
  128. expectedPath: workspaceA,
  129. requireActive: false,
  130. });
  131. expect(reconnectedA.remoteSessionId).not.toBe(taskA.remoteSessionId);
  132. expect(reconnectedA.remoteSessionId).not.toBe(reconnectedB.remoteSessionId);
  133. await selectUpstreamProviderModelById(SSH_LIFECYCLE_MODEL_ID, {
  134. includePlainModelFallback: false,
  135. providerId: provider.id,
  136. providerName: provider.name,
  137. });
  138. const resumedA: SSHLifecycleTask = {
  139. ...taskA,
  140. remoteSessionId: reconnectedA.remoteSessionId!,
  141. };
  142. await readTaskRowInProject(resumedA, CASE_MARKER);
  143. await sendSSHFollowTurn(resumedA, CASE_MARKER, taskB.firstPrompt);
  144. const resumedB: SSHLifecycleTask = {
  145. ...taskB,
  146. remoteSessionId: reconnectedB.remoteSessionId!,
  147. };
  148. await readTaskRowInProject(resumedB, CASE_MARKER);
  149. await sendSSHFollowTurn(resumedB, CASE_MARKER, taskA.firstPrompt);
  150. expect(await readHostProcesses()).not.toEqual(oldHosts);
  151. await seedSettings({ remoteStartupAutoReconnectEnabled: false });
  152. await disconnectRemoteSource(sourceId!, CASE_MARKER);
  153. await reloadElectronSessionPreservingBrowserProfile(browser);
  154. await waitForRendererAfterColdRestart(CASE_MARKER);
  155. const unavailable = $('[data-testid="remote-conversation-unavailable"]');
  156. await unavailable.waitForDisplayed({ timeout: 30_000 });
  157. expect((await readRemoteTabSnapshot(taskB.identity)).activeWorkspaceIdentity).toBe(
  158. taskB.identity,
  159. );
  160. expect(await getV4PaneSnapshot()).toMatchObject({ sessionId: null, rowCount: 0 });
  161. expect(await $('[data-testid="remote-conversation-banner"]').isDisplayed()).toBe(true);
  162. await browser.saveScreenshot(`${process.env.ZCODE_E2E_ARTIFACT_DIR}/rdux09-cold-offline.png`);
  163. await requestRemoteProjectConnection(taskB.identity, CASE_MARKER);
  164. await waitForRemoteTab({
  165. caseMarker: CASE_MARKER,
  166. expectedIdentity: taskB.identity,
  167. expectedPath: workspaceB,
  168. });
  169. await waitForV4Pane(
  170. (snapshot) => snapshot.sessionId === "draft",
  171. `${CASE_MARKER}: 未连接启动后挂回没有保持项目草稿`,
  172. 60_000,
  173. );
  174. await selectV4TaskById(taskB.taskId, 60_000);
  175. await waitForV4AssistantMessageContaining(taskB.firstReply);
  176. await waitForV4ConversationState(
  177. (snapshot) => snapshot.taskId === taskB.taskId,
  178. `${CASE_MARKER}: 手动打开原任务后身份不一致`,
  179. );
  180. });
  181. });