conversation-session-preinstalled-tools-remote-context.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { preinstalledFixtureRecordPath } from "#e2e/helpers/preinstalled-tools-fixture";
  2. import { writeFile, readdir, stat } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  5. import { prepareSSHConnectionPage, readSSHRuntimeConfig } from "#e2e/helpers/ssh-remote-p0";
  6. import {
  7. execInRemoteContainer,
  8. readRemoteDockerContainer,
  9. } from "#e2e/helpers/remote-docker-target";
  10. import { resolveE2EStorageRoot } from "#e2e/helpers/e2e-runtime-paths";
  11. import {
  12. createSSHLifecycleProvider,
  13. connectSSHWorkspaceAndSendFirstTurn,
  14. type SSHLifecycleProvider,
  15. } from "#e2e/helpers/ssh-remote-lifecycle";
  16. const MARKER = "E2E_LOP_REMOTE_CONTEXT";
  17. describe("远端工作区的本机预装工具(待审核)", () => {
  18. let provider: SSHLifecycleProvider | undefined;
  19. after(async () => {
  20. await provider?.tunnel.close();
  21. });
  22. it("LOP-24: 远端提示明确,取消只作用于本机准备", async function () {
  23. this.timeout(8 * 60_000);
  24. const config = readSSHRuntimeConfig(MARKER);
  25. const container = readRemoteDockerContainer(MARKER);
  26. await prepareSSHConnectionPage(MARKER);
  27. await prepareV4ConversationE2E({ skipProvider: true });
  28. // 仅连接项目可能没有 Agent,空进程集合曾让环境隔离断言无条件通过。
  29. provider = await createSSHLifecycleProvider(config, MARKER, { apiType: "anthropic-messages" });
  30. const remote = await connectSSHWorkspaceAndSendFirstTurn({
  31. caseMarker: MARKER,
  32. config,
  33. provider,
  34. label: "A",
  35. workspacePath: config.workspacePaths[0],
  36. });
  37. expect(remote.remoteSessionId).toBeTruthy();
  38. const before = await execInRemoteContainer(container, [
  39. "sh",
  40. "-c",
  41. 'test ! -e "$HOME/.zcode/preinstalled-tools" && printf "E2E_LOP_REMOTE_ABSENT\\n"',
  42. ]);
  43. expect(before.stdout.trim()).toBe("E2E_LOP_REMOTE_ABSENT");
  44. await $('[data-testid="workspace-help-menu-trigger"]').click();
  45. await $('[data-testid="workspace-help-menu-preinstalled-tools"]').click();
  46. await $('[data-testid="preinstalled-tools-remote-notice"]').waitForDisplayed();
  47. expect(await $('[data-testid="preinstalled-tools-remote-notice"]').getText()).toBe(
  48. "当前打开的是远端工作区。这里管理的是本机,不会安装到远端。",
  49. );
  50. await browser.waitUntil(
  51. async () =>
  52. (await $(
  53. '[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-status"]',
  54. ).getAttribute("data-status")) === "preparing",
  55. { timeout: 30000 },
  56. );
  57. const fixtureUrl = process.env.ZCODE_E2E_PREINSTALL_FIXTURE_URL!;
  58. const initial = (await (await fetch(`${fixtureUrl}/status`)).json()) as {
  59. requests: number;
  60. pending: number;
  61. };
  62. expect(initial).toMatchObject({ requests: 1, pending: 1 });
  63. await browser.saveScreenshot(
  64. `${process.env.ZCODE_E2E_ARTIFACT_DIR}/lop-remote-context-preparing.png`,
  65. );
  66. await $('[data-testid="preinstalled-tools-auto-install"]').click();
  67. await browser.waitUntil(
  68. async () =>
  69. (await $('[data-testid="preinstalled-tools-auto-install"]').getAttribute(
  70. "aria-checked",
  71. )) === "false" && (await $('[data-testid="preinstalled-tools-auto-install"]').isEnabled()),
  72. { timeout: 10000 },
  73. );
  74. await $('[data-testid="preinstalled-tools-cancel"]').click();
  75. await browser.waitUntil(
  76. async () =>
  77. (await $(
  78. '[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-status"]',
  79. ).getAttribute("data-status")) === "not-installed",
  80. { timeout: 30000 },
  81. );
  82. const roots = join(resolveE2EStorageRoot(), "preinstalled-tools");
  83. const root = join(roots, (await readdir(roots))[0]!);
  84. await expect(stat(await preinstalledFixtureRecordPath(root))).rejects.toHaveProperty(
  85. "code",
  86. "ENOENT",
  87. );
  88. expect(await readdir(join(root, "tools", "libreoffice", "staging"))).toEqual([]);
  89. const after = await execInRemoteContainer(container, [
  90. "sh",
  91. "-c",
  92. 'test ! -e "$HOME/.zcode/preinstalled-tools" && printf "E2E_LOP_REMOTE_ABSENT\\n"',
  93. ]);
  94. expect(after.stdout.trim()).toBe("E2E_LOP_REMOTE_ABSENT");
  95. // CLI 会设置 process.title,Linux 的 cmdline 因而不再保留 app-server 参数。
  96. // 按实际进程名识别自然启动的 CLI,只读目标变量是否存在,不输出完整环境。
  97. const environment = await execInRemoteContainer(container, [
  98. "sh",
  99. "-c",
  100. `
  101. for file in /proc/[0-9]*/cmdline; do
  102. pid="\${file%/cmdline}"; pid="\${pid##*/}"
  103. [ "$pid" = "$$" ] && continue
  104. [ -r "$file" ] || continue
  105. command=$(tr '\\000' ' ' < "$file" 2>/dev/null) || continue
  106. case "$command" in
  107. "zcode-cli "*|*" app-server --stdio"*)
  108. if grep -zq '^ZCODE_PREINSTALLED_TOOLS_AVAILABLE=' "/proc/$pid/environ"; then
  109. available=present
  110. else
  111. result=$?
  112. [ "$result" = 1 ] || exit "$result"
  113. available=absent
  114. fi
  115. if grep -zq '^PATH=.*preinstalled-tools' "/proc/$pid/environ"; then
  116. bin=present
  117. else
  118. result=$?
  119. [ "$result" = 1 ] || exit "$result"
  120. bin=absent
  121. fi
  122. printf '%s %s %s\\n' "$pid" "$available" "$bin"
  123. ;;
  124. esac
  125. done
  126. `,
  127. ]);
  128. const agents = environment.stdout
  129. .trim()
  130. .split("\n")
  131. .filter(Boolean)
  132. .map((line) => {
  133. const [pid, value, bin] = line.split(" ");
  134. expect(["present", "absent"]).toContain(bin);
  135. expect(["present", "absent"]).toContain(value);
  136. expect(Number.isSafeInteger(Number(pid)) && Number(pid) > 0).toBe(true);
  137. return {
  138. pid: Number(pid),
  139. availabilityPresent: value === "present",
  140. binPresent: bin === "present",
  141. };
  142. });
  143. expect(agents.length).toBeGreaterThan(0);
  144. for (const agent of agents) {
  145. expect(agent.availabilityPresent).toBe(false);
  146. expect(agent.binPresent).toBe(false);
  147. }
  148. await writeFile(
  149. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "lop-remote-context.json"),
  150. JSON.stringify(
  151. {
  152. localPlatform: process.platform,
  153. localArch: process.arch,
  154. remoteWorkspaceIdentity: remote.identity,
  155. initialLocalDownload: initial,
  156. localRecordAbsent: true,
  157. remoteResourcesAbsent: true,
  158. observedRemoteAgents: agents,
  159. },
  160. null,
  161. 2,
  162. ),
  163. );
  164. await browser.saveScreenshot(
  165. `${process.env.ZCODE_E2E_ARTIFACT_DIR}/lop-remote-context-cancelled.png`,
  166. );
  167. });
  168. });