conversation-session-preinstalled-tools-proxy.test.ts 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { preinstalledFixtureRecordPath } from "#e2e/helpers/preinstalled-tools-fixture";
  2. import { readdir, stat } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { resolveE2EStorageRoot } from "#e2e/helpers/e2e-runtime-paths";
  5. import { readSettings } from "#e2e/helpers/desktop-app";
  6. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  7. interface FixtureStatus {
  8. requests: Array<{ id: number; resource: string; mode: string; range: string | null }>;
  9. proxyUrl: string;
  10. caCertPath: string;
  11. }
  12. const fixtureUrl = () => process.env.ZCODE_E2E_PREINSTALL_FIXTURE_URL!;
  13. const status = async () => (await (await fetch(`${fixtureUrl()}/status`)).json()) as FixtureStatus;
  14. const statusSelector = '[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-status"]';
  15. async function openDialog() {
  16. await $('[data-testid="workspace-help-menu-trigger"]').click();
  17. await $('[data-testid="workspace-help-menu-preinstalled-tools"]').click();
  18. await $('[data-testid="preinstalled-tools-dialog"]').waitForDisplayed();
  19. }
  20. async function terminalStatus() {
  21. await browser.waitUntil(
  22. async () =>
  23. ["ready", "failed"].includes((await $(statusSelector).getAttribute("data-status")) ?? ""),
  24. { timeout: 30000 },
  25. );
  26. return $(statusSelector).getAttribute("data-status");
  27. }
  28. async function screenshot(name: string) {
  29. await browser.saveScreenshot(`${process.env.ZCODE_E2E_ARTIFACT_DIR}/lop-proxy-${name}.png`);
  30. }
  31. async function preinstallPid() {
  32. return browser.electron.execute(
  33. (electron) =>
  34. electron.app.getAppMetrics().find((metric) => metric.name === "zcode-preinstalled-tools")
  35. ?.pid ?? null,
  36. );
  37. }
  38. async function saveNetworkSettings(values: { proxy?: string; noProxy?: string; ca?: string }) {
  39. await browser.keys("Escape");
  40. await $('[data-testid="task-settings-button"]').click();
  41. await $('[data-testid="settings-page"]').waitForDisplayed();
  42. await $('[data-testid="settings-section-nav-general"]').click();
  43. for (const [key, value, selector] of [
  44. ["httpProxy", values.proxy, 'input[placeholder*="7890"]'],
  45. ["httpProxyNoProxy", values.noProxy, 'input[placeholder*="localhost,127"]'],
  46. ["httpProxyCaCertPath", values.ca, 'input[placeholder*="root-ca.pem"]'],
  47. ] as const) {
  48. if (value === undefined) continue;
  49. const input = $(selector);
  50. await input.scrollIntoView();
  51. await input.setValue(value);
  52. await browser.keys("Enter");
  53. await browser.waitUntil(async () => (await readSettings())[key] === value, { timeout: 10000 });
  54. }
  55. await $('[data-testid="settings-back-button"]').click();
  56. await openDialog();
  57. }
  58. // 两个下载基址都在只能经代理访问的域名下;与 faults 分开启动,因为基址在 App 启动时固定。
  59. describe("预装工具代理与私有 CA(待审核)", () => {
  60. it("LOP-13: 下载读取当前代理设置,私有 CA 配置前失败、配置后同一进程重试成功", async function () {
  61. this.timeout(300000);
  62. await prepareV4ConversationE2E();
  63. await openDialog();
  64. // 未配置代理时域名不可达,自动准备失败,且没有请求绕过代理到达本地服务。
  65. expect(await terminalStatus()).toBe("failed");
  66. expect(await $('[data-testid="preinstalled-tools-dialog"]').getText()).toContain(
  67. "无法连接下载服务器",
  68. );
  69. expect((await status()).requests).toEqual([]);
  70. await screenshot("without-proxy");
  71. const proxy = await status();
  72. const pid = await preinstallPid();
  73. expect(pid).not.toBeNull();
  74. await saveNetworkSettings({ proxy: proxy.proxyUrl, noProxy: "localhost,127.0.0.1,::1" });
  75. await $('[data-testid="preinstalled-tools-retry"]').click();
  76. expect(await terminalStatus()).toBe("failed");
  77. expect(await $('[data-testid="preinstalled-tools-dialog"]').getText()).toContain(
  78. "无法连接下载服务器",
  79. );
  80. expect((await status()).requests).toEqual([]);
  81. await screenshot("without-ca");
  82. await saveNetworkSettings({ ca: proxy.caCertPath });
  83. await $('[data-testid="preinstalled-tools-retry"]').click();
  84. expect(await terminalStatus()).toBe("ready");
  85. expect(await preinstallPid()).toBe(pid);
  86. expect((await status()).requests.map((item) => item.mode)).toEqual(["http-error", "complete"]);
  87. const roots = join(resolveE2EStorageRoot(), "preinstalled-tools");
  88. const root = join(roots, (await readdir(roots))[0]!);
  89. expect((await stat(await preinstalledFixtureRecordPath(root))).isFile()).toBe(true);
  90. await screenshot("with-ca-ready");
  91. });
  92. });