windows-agent-metric-probe-absence.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { readdir, readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_LOGIN_API_KEY_SKIP_BUTTON,
  5. TID_LOGIN_USE_API_KEY_BUTTON,
  6. TID_TASK_ITEM,
  7. testId,
  8. } from "@zcode/shared/test-ids";
  9. import {
  10. clearAppData,
  11. clickTestIdByDom,
  12. waitForDefaultWorkspaceReady,
  13. waitForTestIdByDom,
  14. } from "#e2e/helpers/desktop-app";
  15. import {
  16. PROVIDER_READINESS_HISTORY_SESSION_ID,
  17. PROVIDER_READINESS_HISTORY_TITLE,
  18. } from "#e2e/helpers/provider-readiness-history-fixture";
  19. const AGENT_METRIC_PROBE_SKIPPED_MARKER =
  20. "agent_metric_probe action=skipped reason=main_process_external_probe_disabled";
  21. const AGENT_METRIC_PROBE_SPAWN_MARKER = "agent_metric_probe action=spawn";
  22. /**
  23. * 性能红线(docs/monitoring/process-resource-telemetry.md):全链路禁止 PowerShell / WMI / CIM。
  24. * PRT-024 在 PERF-RES-01 原断言之上再加一条运行时证据——worker 专属 runtime log 里
  25. * 不能出现 powershell,任何进程真的起过 PowerShell 都会在日志里留痕。
  26. */
  27. const POWERSHELL_MARKER = "powershell";
  28. // 只有 Windows 才有 PowerShell 这条回归路径,macOS 与 Linux 明确 skip。
  29. const windowsDescribe = process.platform === "win32" ? describe : describe.skip;
  30. windowsDescribe("Windows Agent 指标采集 main process 门禁", () => {
  31. after(async () => {
  32. await browser.electron.restoreAllMocks();
  33. await clearAppData();
  34. });
  35. it("PERF-RES-01: 真实 Agent 注册后的资源采样日志只记录 skipped,不启动外部 probe", async function () {
  36. this.timeout(90000);
  37. await waitForTestIdByDom(TID_LOGIN_USE_API_KEY_BUTTON, {
  38. timeout: 30000,
  39. timeoutMsg: "空 auth/provider 冷启动后没有展示 API Key 配置入口",
  40. });
  41. await clickTestIdByDom(TID_LOGIN_USE_API_KEY_BUTTON);
  42. await clickTestIdByDom(TID_LOGIN_API_KEY_SKIP_BUTTON, {
  43. timeout: 15000,
  44. timeoutMsg: "API Key 登录页没有可用的暂时跳过入口",
  45. });
  46. await waitForDefaultWorkspaceReady(30000);
  47. await waitForSeededHistory();
  48. const runtimeLogDir = process.env.ZCODE_E2E_RUNTIME_LOG_DIR?.trim();
  49. if (!runtimeLogDir) {
  50. throw new Error("当前 E2E worker 没有配置 desktop runtime log 目录");
  51. }
  52. let desktopLogs = "";
  53. await browser.waitUntil(
  54. async () => {
  55. desktopLogs = await readDesktopRuntimeLogs(runtimeLogDir);
  56. return desktopLogs.includes(AGENT_METRIC_PROBE_SKIPPED_MARKER);
  57. },
  58. {
  59. timeout: 45000,
  60. interval: 250,
  61. timeoutMsg:
  62. "真实 Agent 注册后跨过资源采样周期,main 日志仍未记录禁用外部 probe 的 skipped 审计",
  63. },
  64. );
  65. // Bug 根因:Agent PID 接入资源采样后,旧实现会在 Electron main process
  66. // 每 10 秒同步启动 PowerShell。E2E 只接受显式 skipped,任何 spawn 审计都失败。
  67. await browser.pause(500);
  68. desktopLogs = await readDesktopRuntimeLogs(runtimeLogDir);
  69. expect(desktopLogs).toContain(AGENT_METRIC_PROBE_SKIPPED_MARKER);
  70. expect(desktopLogs).not.toContain(AGENT_METRIC_PROBE_SPAWN_MARKER);
  71. // PRT-024:跨过采样周期后的 runtime log 不含 powershell。断言点选在测试体内、
  72. // App 关闭之前——退出时的进程树回收走 Host 的 windowsProcessListAsync(按需 CIM,
  73. // 已在红线豁免清单内),不属于遥测链路,不能把它的收尾日志算进来。
  74. expect(desktopLogs.toLowerCase()).not.toContain(POWERSHELL_MARKER);
  75. });
  76. });
  77. async function waitForSeededHistory(): Promise<void> {
  78. const itemTestId = testId(TID_TASK_ITEM, PROVIDER_READINESS_HISTORY_SESSION_ID);
  79. await waitForTestIdByDom(itemTestId, {
  80. timeout: 30000,
  81. timeoutMsg: "provider 未就绪时预置历史行没有出现在侧栏",
  82. });
  83. const title = await browser.execute((targetTestId) => {
  84. return document
  85. .querySelector<HTMLElement>(`[data-testid="${targetTestId}"]`)
  86. ?.innerText.replace(/\s+/g, " ")
  87. .trim();
  88. }, itemTestId);
  89. expect(title).toContain(PROVIDER_READINESS_HISTORY_TITLE);
  90. }
  91. async function readDesktopRuntimeLogs(runtimeLogDir: string): Promise<string> {
  92. try {
  93. const entries = await readdir(runtimeLogDir, { withFileTypes: true });
  94. const logFiles = entries
  95. .filter((entry) => entry.isFile() && entry.name.endsWith(".log"))
  96. .map((entry) => entry.name)
  97. .sort();
  98. const contents = await Promise.all(
  99. logFiles.map((fileName) => readFile(join(runtimeLogDir, fileName), "utf8")),
  100. );
  101. return contents.join("\n");
  102. } catch (error) {
  103. if ((error as NodeJS.ErrnoException).code === "ENOENT") {
  104. return "";
  105. }
  106. throw error;
  107. }
  108. }