conversation-session-preinstalled-tools-tracer.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import {
  2. executePreinstalledEntry,
  3. PREINSTALLED_FIXTURE_PROGRAM,
  4. preinstalledFixtureRecordPath,
  5. preinstalledFixtureTarget,
  6. preinstalledEntryPath,
  7. } from "#e2e/helpers/preinstalled-tools-fixture";
  8. import { TID_VIRTUAL_PROJECT_ADD } from "@zcode/shared/test-ids";
  9. import {
  10. startDraftFromProject,
  11. selectNativeProjectDirectories,
  12. } from "#e2e/helpers/project-sidebar";
  13. import { getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  14. import { readFile, readdir, rm, stat, readlink, mkdir } from "node:fs/promises";
  15. import { join } from "node:path";
  16. import { resolveE2EStorageRoot } from "#e2e/helpers/e2e-runtime-paths";
  17. import { reloadElectronSessionPreservingBrowserProfile } from "#e2e/helpers/e2e-electron-reload";
  18. import {
  19. prepareV4ConversationE2E,
  20. sendV4Prompt,
  21. waitForV4AssistantMessageContaining,
  22. } from "#e2e/helpers/v4-conversation";
  23. const fixtureUrl = () => process.env.ZCODE_E2E_PREINSTALL_FIXTURE_URL!;
  24. const status = async () =>
  25. (await fetch(`${fixtureUrl()}/status`)).json() as Promise<{
  26. requests: number;
  27. pending: number;
  28. runtimeChecks: number;
  29. }>;
  30. async function openDialog() {
  31. await $('[data-testid="workspace-help-menu-trigger"]').click();
  32. // 修复原因:合并 staging 后,Windows 上重启 Electron 之后用 $$().map() 并发读取十余个菜单项属性,
  33. // 部分新连接被 chromedriver 拒绝(ECONNREFUSED,2/2 复现);chromedriver 为何短暂拒绝新连接尚未查明。
  34. // 改为一次 execute 在页面内读出全部菜单项,检查内容不变,也不再依赖并发连接。
  35. const menuIds = await browser.execute(() =>
  36. Array.from(document.querySelectorAll('[role="menuitem"]'), (item) =>
  37. item.getAttribute("data-testid"),
  38. ),
  39. );
  40. expect(menuIds.indexOf("workspace-help-menu-preinstalled-tools")).toBe(
  41. menuIds.indexOf("workspace-help-menu-resource-manager") + 1,
  42. );
  43. await $('[data-testid="workspace-help-menu-preinstalled-tools"]').click();
  44. await $('[data-testid="preinstalled-tools-dialog"]').waitForDisplayed();
  45. }
  46. async function waitStatus(value: string, toolId = "qpdf") {
  47. await browser.waitUntil(
  48. async () =>
  49. (await $(`[data-tool-id="${toolId}"] [data-testid="preinstalled-tools-status"]`).getAttribute(
  50. "data-status",
  51. )) === value,
  52. { timeout: 30000, timeoutMsg: `预装状态未达到 ${value}` },
  53. );
  54. }
  55. async function preinstallPid() {
  56. return browser.electron.execute(
  57. (electron) =>
  58. electron.app.getAppMetrics().find((metric) => metric.name === "zcode-preinstalled-tools")
  59. ?.pid ?? null,
  60. );
  61. }
  62. describe("预装工具后台准备(待审核)", () => {
  63. it("LOP-01/02/14/31: 静默准备、重启复用与子进程恢复", async function () {
  64. this.timeout(240000);
  65. await prepareV4ConversationE2E();
  66. await openDialog();
  67. await waitStatus("preparing");
  68. expect(await status()).toMatchObject({ requests: 1, pending: 1 });
  69. expect(
  70. await $(
  71. '[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-status"]',
  72. ).getAttribute("data-phase"),
  73. ).toBe("queued");
  74. await $('[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-cancel"]').click();
  75. await waitStatus("not-installed", "libreoffice");
  76. await $('[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-download"]').click();
  77. await browser.waitUntil(
  78. async () =>
  79. (await $(
  80. '[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-status"]',
  81. ).getAttribute("data-phase")) === "queued",
  82. );
  83. expect(await status()).toMatchObject({ requests: 1, pending: 1 });
  84. expect(await $$("[data-sonner-toast]").length).toBe(0);
  85. await browser.saveScreenshot(`${process.env.ZCODE_E2E_ARTIFACT_DIR}/lop-tracer-preparing.png`);
  86. await browser.keys("Escape");
  87. await sendV4Prompt("E2E_LOP_TRACER_CHAT: Reply with E2E_LOP_TRACER_CHAT_DONE.");
  88. await waitForV4AssistantMessageContaining("E2E_LOP_TRACER_CHAT_DONE", 90000);
  89. await openDialog();
  90. await waitStatus("preparing");
  91. await fetch(`${fixtureUrl()}/qpdf/release`);
  92. await browser.waitUntil(
  93. async () =>
  94. (await $('[data-tool-id="qpdf"] [data-testid="preinstalled-tools-status"]').getAttribute(
  95. "data-phase",
  96. )) === "checking",
  97. { timeout: 30000 },
  98. );
  99. expect(
  100. await $('[data-tool-id="qpdf"] [role="progressbar"]').getAttribute("aria-valuenow"),
  101. ).toBeNull();
  102. await browser.saveScreenshot(`${process.env.ZCODE_E2E_ARTIFACT_DIR}/lop-tracer-checking.png`);
  103. await browser.waitUntil(async () => (await status()).runtimeChecks === 1, {
  104. timeout: 10000,
  105. timeoutMsg: "运行检查请求未到达受控服务",
  106. });
  107. await fetch(`${fixtureUrl()}/qpdf/finish-check`);
  108. await waitStatus("ready");
  109. await browser.waitUntil(async () => (await status()).requests === 2);
  110. expect(
  111. await $(
  112. '[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-status"]',
  113. ).getAttribute("data-phase"),
  114. ).toBe("downloading");
  115. await fetch(`${fixtureUrl()}/libreoffice/release`);
  116. await fetch(`${fixtureUrl()}/libreoffice/finish-check`);
  117. await waitStatus("ready", "libreoffice");
  118. const roots = join(resolveE2EStorageRoot(), "preinstalled-tools");
  119. const release = (await readdir(roots))[0]!;
  120. const root = join(roots, release);
  121. const recordPath = await preinstalledFixtureRecordPath(root, "qpdf");
  122. const record = JSON.parse(await readFile(recordPath, "utf8"));
  123. const program = join(record.installationDir, PREINSTALLED_FIXTURE_PROGRAM);
  124. if (process.platform === "win32") {
  125. // Windows 夹具不含符号链接,也没有 POSIX 可执行位;只核对入口目标与程序都已解压。
  126. expect(
  127. (await stat(join(record.installationDir, preinstalledFixtureTarget("qpdf")))).isFile(),
  128. ).toBe(true);
  129. expect((await stat(program)).isFile()).toBe(true);
  130. } else {
  131. expect(await readlink(join(record.installationDir, "bin/qpdf"))).toBe("office");
  132. expect((await stat(program)).mode & 0o111).not.toBe(0);
  133. }
  134. await browser.saveScreenshot(`${process.env.ZCODE_E2E_ARTIFACT_DIR}/lop-tracer-ready.png`);
  135. await browser.keys("Escape");
  136. const workspaceB = join(getE2EAppDataPaths().homeDir, "preinstalled-tools-workspace-b");
  137. await mkdir(workspaceB, { recursive: true });
  138. await $('[data-testid="project-section"]').moveTo();
  139. await $(`[data-testid="${TID_VIRTUAL_PROJECT_ADD}"]`).click();
  140. await $('[data-testid="virtual-project-name"]').setValue("preinstalled-tools-workspace-b");
  141. await selectNativeProjectDirectories([workspaceB]);
  142. await $('[data-testid="virtual-project-save"]').click();
  143. await $('[data-testid="project-folder-trust-confirm"]').waitForDisplayed();
  144. await $('[data-testid="project-folder-trust-confirm"]').click();
  145. await $('[data-testid="virtual-project-save"]').waitForExist({ reverse: true });
  146. await startDraftFromProject(workspaceB);
  147. await openDialog();
  148. await waitStatus("ready");
  149. expect(await status()).toMatchObject({ requests: 2, runtimeChecks: 3 });
  150. const firstPid = await preinstallPid();
  151. expect(firstPid).not.toBeNull();
  152. await reloadElectronSessionPreservingBrowserProfile(browser);
  153. await prepareV4ConversationE2E();
  154. await openDialog();
  155. await waitStatus("ready");
  156. expect(await status()).toMatchObject({ requests: 2, runtimeChecks: 3 });
  157. expect(await preinstallPid()).not.toBe(firstPid);
  158. await $('[data-tool-id="qpdf"] [data-testid="preinstalled-tools-delete"]').click();
  159. await $('[data-testid="preinstalled-tools-delete-confirm-accept"]').click();
  160. await waitStatus("not-installed");
  161. await waitStatus("ready", "libreoffice");
  162. await expect(
  163. stat(await preinstalledFixtureRecordPath(root, "libreoffice")),
  164. ).resolves.toBeDefined();
  165. await expect(
  166. stat(preinstalledEntryPath(join(root, "bin"), "zcode-soffice")),
  167. ).resolves.toBeDefined();
  168. await fetch(`${fixtureUrl()}/qpdf/hold`);
  169. await reloadElectronSessionPreservingBrowserProfile(browser, {
  170. afterElectronProcessExit: async () => {
  171. await rm(recordPath, { force: true });
  172. await rm(record.installationDir, { recursive: true, force: true });
  173. },
  174. });
  175. await prepareV4ConversationE2E();
  176. await openDialog();
  177. await waitStatus("preparing");
  178. const killed = await preinstallPid();
  179. expect(killed).not.toBeNull();
  180. process.kill(killed!, "SIGKILL");
  181. await browser.waitUntil(
  182. async () => {
  183. const pid = await preinstallPid();
  184. return pid !== null && pid !== killed;
  185. },
  186. { timeout: 30000 },
  187. );
  188. await waitStatus("not-installed");
  189. expect((await status()).requests).toBe(3);
  190. await expect(stat(recordPath)).rejects.toHaveProperty("code", "ENOENT");
  191. expect(await readdir(join(root, "tools", "qpdf", "staging"))).toEqual([]);
  192. await browser.saveScreenshot(`${process.env.ZCODE_E2E_ARTIFACT_DIR}/lop-tracer-recovered.png`);
  193. // 活动项取消后队列必须继续;关闭自动预装不能取消已经排队的另一项。
  194. await $('[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-delete"]').click();
  195. await $('[data-testid="preinstalled-tools-delete-confirm-accept"]').click();
  196. await waitStatus("not-installed", "libreoffice");
  197. await fetch(`${fixtureUrl()}/libreoffice/hold`);
  198. await fetch(`${fixtureUrl()}/libreoffice/hold-check`);
  199. const beforeCancel = (await status()).requests;
  200. await $('[data-tool-id="qpdf"] [data-testid="preinstalled-tools-download"]').click();
  201. await browser.waitUntil(async () => (await status()).requests === beforeCancel + 1);
  202. await $('[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-download"]').click();
  203. await browser.waitUntil(
  204. async () =>
  205. (await $(
  206. '[data-tool-id="libreoffice"] [data-testid="preinstalled-tools-status"]',
  207. ).getAttribute("data-phase")) === "queued",
  208. );
  209. await $('[data-tool-id="qpdf"] [data-testid="preinstalled-tools-cancel"]').click();
  210. await waitStatus("not-installed");
  211. await browser.waitUntil(async () => (await status()).requests === beforeCancel + 2);
  212. await $('[data-tool-id="qpdf"] [data-testid="preinstalled-tools-download"]').click();
  213. await browser.waitUntil(
  214. async () =>
  215. (await $('[data-tool-id="qpdf"] [data-testid="preinstalled-tools-status"]').getAttribute(
  216. "data-phase",
  217. )) === "queued",
  218. );
  219. await $('[data-testid="preinstalled-tools-auto-install"]').click();
  220. await browser.waitUntil(
  221. async () =>
  222. (await $('[data-testid="preinstalled-tools-auto-install"]').getAttribute(
  223. "aria-checked",
  224. )) === "false",
  225. );
  226. await fetch(`${fixtureUrl()}/libreoffice/release`);
  227. await fetch(`${fixtureUrl()}/libreoffice/finish-check`);
  228. await waitStatus("ready", "libreoffice");
  229. await browser.waitUntil(async () => (await status()).requests === beforeCancel + 3);
  230. await fetch(`${fixtureUrl()}/qpdf/release`);
  231. await waitStatus("ready");
  232. const invocation = await executePreinstalledEntry(join(root, "bin"), "qpdf", ["--version"]);
  233. expect(invocation.stderr).toContain("qpdf fixture");
  234. await browser.saveScreenshot(
  235. `${process.env.ZCODE_E2E_ARTIFACT_DIR}/pt-active-cancel-queue-continued.png`,
  236. );
  237. });
  238. });