conversation-session-ssh-remote-host-capacity.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { parseRemoteWorkspaceIdentity, ServiceChannels } from "@zcode/shared";
  4. import type { RemoteProjectDirectory, RemoteProjectHost } from "@zcode/services";
  5. import { clearAppData } from "#e2e/helpers/desktop-app";
  6. import {
  7. callHandoffHost,
  8. installHandoffRpcProbe,
  9. restoreHandoffProbes,
  10. } from "#e2e/helpers/remote-project-handoff-probe";
  11. import {
  12. openRemoteDevicesSettings,
  13. waitForDeviceStatus,
  14. } from "#e2e/helpers/remote-project-actions";
  15. import {
  16. execInRemoteContainer,
  17. readRemoteDockerContainer,
  18. } from "#e2e/helpers/remote-docker-target";
  19. import {
  20. connectSSHWorkspace,
  21. readSSHRuntimeConfig,
  22. runSSHCommand,
  23. } from "#e2e/helpers/ssh-remote-p0";
  24. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  25. const MARKER = "E2E_SSH_HOST_CAPACITY";
  26. const artifactDir = resolve(
  27. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/ssh-host-capacity",
  28. );
  29. const readDirectory = () =>
  30. callHandoffHost<RemoteProjectDirectory>(ServiceChannels.RemoteProjects, "readDirectory");
  31. const entryOwnership = (directory: RemoteProjectDirectory) =>
  32. directory.entries.map(({ id, hostId, projectId }) => ({ id, hostId, projectId }));
  33. describe(`${MARKER}: 保存远程连接不设数量上限`, () => {
  34. const facts: Record<string, unknown> = {};
  35. after(async () => {
  36. await mkdir(artifactDir, { recursive: true });
  37. await writeFile(resolve(artifactDir, "host-capacity.json"), JSON.stringify(facts, null, 2));
  38. await restoreHandoffProbes();
  39. await browser.electron.restoreAllMocks();
  40. await clearAppData();
  41. });
  42. it("SSH-P0-CONN-10:第 21 条真实 SSH 连接成功,同设备复用与确认新身份均不受数量限制", async function () {
  43. this.timeout(15 * 60_000);
  44. const target = readSSHRuntimeConfig(MARKER);
  45. // 同批用例共用远端容器;独立目录避免后续 P0 的同名项目被自动追加名称后缀。
  46. const config = {
  47. ...target,
  48. workspacePaths: [
  49. `${target.workspacePaths[0]}/host-capacity-a`,
  50. `${target.workspacePaths[1]}/host-capacity-b`,
  51. ] as const,
  52. };
  53. await execInRemoteContainer(readRemoteDockerContainer(MARKER), [
  54. "mkdir",
  55. "-p",
  56. "--",
  57. ...config.workspacePaths,
  58. ]);
  59. await prepareV4ConversationE2E({ skipProvider: true });
  60. await installHandoffRpcProbe();
  61. expect((await readDirectory()).hosts).toHaveLength(0);
  62. // 用真实服务准备历史记录,避免直写 SQLite 绕过 Controller 的变更通知。
  63. const retained: RemoteProjectHost[] = [];
  64. for (let index = 0; index < 20; index += 1) {
  65. retained.push(
  66. await callHandoffHost<RemoteProjectHost>(ServiceChannels.RemoteProjects, "saveHost", [
  67. {
  68. id: `remote:server:e2e-retained-${index}`,
  69. target: { kind: "ssh", host: `retained-${index}.example.test`, username: "dev" },
  70. },
  71. ]),
  72. );
  73. }
  74. facts.savedBeforeConnect = (await readDirectory()).hosts.length;
  75. expect(facts.savedBeforeConnect).toBe(20);
  76. const first = await connectSSHWorkspace({
  77. caseMarker: MARKER,
  78. config,
  79. workspacePath: config.workspacePaths[0],
  80. });
  81. const sourceId = parseRemoteWorkspaceIdentity(first.workspaceIdentity!)?.sourceServerId;
  82. expect(sourceId).toBeTruthy();
  83. const connected = await readDirectory();
  84. expect(connected.hosts).toHaveLength(21);
  85. expect(connected.hosts.find((host) => host.id === `remote:server:${sourceId}`)).toMatchObject({
  86. connectionStatus: "online",
  87. });
  88. for (const host of retained) {
  89. expect(connected.hosts.find((candidate) => candidate.id === host.id)?.membershipId).toBe(
  90. host.membershipId,
  91. );
  92. }
  93. facts.savedAfterConnect = connected.hosts.length;
  94. facts.sourceId = sourceId;
  95. const second = await connectSSHWorkspace({
  96. caseMarker: MARKER,
  97. config,
  98. workspacePath: config.workspacePaths[1],
  99. });
  100. expect(parseRemoteWorkspaceIdentity(second.workspaceIdentity!)?.sourceServerId).toBe(sourceId);
  101. expect((await readDirectory()).hosts).toHaveLength(21);
  102. facts.sameSourceReused = true;
  103. const architecture = await runSSHCommand(config, "uname -m");
  104. expect(architecture.code).toBe(0);
  105. facts.containerArchitecture = architecture.stdout.trim();
  106. await openRemoteDevicesSettings();
  107. await waitForDeviceStatus(sourceId!, "online", MARKER);
  108. await browser.saveScreenshot(resolve(artifactDir, "connected-above-twenty.png"));
  109. // 第二个旧拦截点在身份确认写入;通过真实 RPC 验证,不伪装为设备重装流程。
  110. const previous = retained[0]!;
  111. const entriesBeforeConfirmation = entryOwnership(await readDirectory());
  112. const replacement = await callHandoffHost<RemoteProjectHost>(
  113. ServiceChannels.RemoteProjects,
  114. "bindHost",
  115. [
  116. {
  117. id: previous.id,
  118. membershipId: previous.membershipId,
  119. resolvedId: "remote:server:e2e-confirmed-replacement",
  120. createDistinctHost: true,
  121. },
  122. ],
  123. );
  124. expect(replacement.id).toBe("remote:server:e2e-confirmed-replacement");
  125. expect(replacement.membershipId).not.toBe(previous.membershipId);
  126. const confirmed = await readDirectory();
  127. expect(confirmed.hosts).toHaveLength(22);
  128. expect(confirmed.hosts.find((host) => host.id === previous.id)?.membershipId).toBe(
  129. previous.membershipId,
  130. );
  131. expect(entryOwnership(confirmed)).toEqual(entriesBeforeConfirmation);
  132. facts.savedAfterIdentityConfirmation = confirmed.hosts.length;
  133. });
  134. });