conversation-session-ssh-remote-account-isolation.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { parseRemoteWorkspaceIdentity } from "@zcode/shared";
  4. import { clearAppData, readSettings } from "#e2e/helpers/desktop-app";
  5. import {
  6. disconnectRemoteSource,
  7. requestRemoteProjectConnection,
  8. } from "#e2e/helpers/remote-project-actions";
  9. import {
  10. connectSSHWorkspaceAndSendFirstTurn,
  11. createSSHLifecycleProvider,
  12. prepareSSHLifecycle,
  13. sendSSHFollowTurn,
  14. type SSHLifecycleProvider,
  15. } from "#e2e/helpers/ssh-remote-lifecycle";
  16. import { runSSHCommand, waitForRemoteTab } from "#e2e/helpers/ssh-remote-p0";
  17. const MARKER = "E2E_SSH_ACCOUNT_ISOLATION";
  18. const artifactDir = resolve(
  19. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/ssh-account-isolation",
  20. );
  21. describe(`${MARKER}: 同地址不同登录账号`, () => {
  22. const providers: SSHLifecycleProvider[] = [];
  23. const facts: Record<string, unknown> = {};
  24. after(async () => {
  25. await mkdir(artifactDir, { recursive: true });
  26. await writeFile(resolve(artifactDir, "account-isolation.json"), JSON.stringify(facts, null, 2));
  27. await Promise.all(providers.map((provider) => provider.tunnel.close()));
  28. await browser.electron.restoreAllMocks();
  29. await clearAppData();
  30. });
  31. it("SSH-GUIDE-ACCOUNT 两个账号独立设备,同路径首发与重连续聊不串历史,不误报重装", async function () {
  32. this.timeout(15 * 60_000);
  33. const prepared = await prepareSSHLifecycle(MARKER, { tunnelUsername: "tunnel" });
  34. providers.push(prepared.provider);
  35. // 两账号同路径场景要求双方能遍历目录;镜像 home 的默认权限会先触发另一个权限场景。
  36. expect(
  37. (
  38. await runSSHCommand(
  39. prepared.config,
  40. `chmod 755 '/home/${prepared.config.username}' '${prepared.config.workspacePaths[0]}'`,
  41. )
  42. ).code,
  43. ).toBe(0);
  44. const first = await connectSSHWorkspaceAndSendFirstTurn({
  45. caseMarker: MARKER,
  46. config: prepared.config,
  47. provider: prepared.provider,
  48. label: "A",
  49. workspacePath: prepared.config.workspacePaths[0],
  50. });
  51. // 通用目标中的 tunnel 是独立真实 OS 用户,与 dev 使用本轮随机凭据;不修改共享服务器账号。
  52. const secondConfig = { ...prepared.config, username: "tunnel" };
  53. const secondProvider = await createSSHLifecycleProvider(secondConfig, MARKER, {
  54. tunnelUsername: "tunnel",
  55. });
  56. providers.push(secondProvider);
  57. const second = await connectSSHWorkspaceAndSendFirstTurn({
  58. caseMarker: MARKER,
  59. config: secondConfig,
  60. provider: secondProvider,
  61. label: "B",
  62. workspacePath: prepared.config.workspacePaths[0],
  63. });
  64. const firstSource = parseRemoteWorkspaceIdentity(first.identity)?.sourceServerId;
  65. const secondSource = parseRemoteWorkspaceIdentity(second.identity)?.sourceServerId;
  66. expect(firstSource).toBeTruthy();
  67. expect(secondSource).toBeTruthy();
  68. expect(firstSource).not.toBe(secondSource);
  69. expect(first.identity).not.toBe(second.identity);
  70. expect(first.taskId).not.toBe(second.taskId);
  71. expect(await $('[data-confirm-intent="source-server-identity"]').isExisting()).toBe(false);
  72. expect((await readSettings()).sourceServers).toHaveLength(2);
  73. facts.first = { identity: first.identity, taskId: first.taskId, sourceId: firstSource };
  74. facts.second = { identity: second.identity, taskId: second.taskId, sourceId: secondSource };
  75. await disconnectRemoteSource(firstSource!, MARKER);
  76. const secondStillOnline = await waitForRemoteTab({
  77. caseMarker: MARKER,
  78. expectedIdentity: second.identity,
  79. expectedPath: second.workspacePath,
  80. requireActive: false,
  81. });
  82. expect(secondStillOnline.remoteSessionId).toBe(second.remoteSessionId);
  83. await sendSSHFollowTurn(second, MARKER, first.firstPrompt);
  84. // 重连设备应保留另一台设备上当前查看的页签,续聊时再由真实任务选择切回。
  85. await requestRemoteProjectConnection(first.identity, MARKER);
  86. const restoredFirst = await waitForRemoteTab({
  87. caseMarker: MARKER,
  88. expectedIdentity: first.identity,
  89. expectedPath: first.workspacePath,
  90. requireActive: false,
  91. });
  92. first.remoteSessionId = restoredFirst.remoteSessionId!;
  93. await sendSSHFollowTurn(first, MARKER, second.firstPrompt);
  94. facts.independentReconnect = true;
  95. await browser.saveScreenshot(resolve(artifactDir, "two-accounts-same-path.png"));
  96. });
  97. });