telemetryRegistration.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { resolve } from "node:path";
  2. import { beforeAll, describe, expect, it, vi } from "vitest";
  3. describe("telemetry service registration", () => {
  4. // Bugfix: 在 pre-push 全量并发执行时,被测模块初始化触发的 console 输出会通过 RPC 上报到主进程,
  5. // 偶发出现 "EnvironmentTeardownError: Closing rpc while onUserConsoleLog was pending",
  6. // 把单测假红挡住 git push。这里在整份测试文件生命周期内静默 console,避免 afterEach 恢复后还有后台日志。
  7. beforeAll(() => {
  8. vi.spyOn(console, "log").mockImplementation(() => undefined);
  9. vi.spyOn(console, "info").mockImplementation(() => undefined);
  10. vi.spyOn(console, "warn").mockImplementation(() => undefined);
  11. vi.spyOn(console, "error").mockImplementation(() => undefined);
  12. vi.spyOn(console, "debug").mockImplementation(() => undefined);
  13. const originalEmitWarning = process.emitWarning;
  14. vi.spyOn(process, "emitWarning").mockImplementation(((...args: unknown[]) => {
  15. const [warning, second] = args;
  16. const message = warning instanceof Error ? warning.message : String(warning);
  17. const warningType =
  18. typeof second === "string"
  19. ? second
  20. : typeof second === "object" && second !== null && "type" in second
  21. ? String((second as { type?: unknown }).type)
  22. : undefined;
  23. // Bugfix: Windows/Node 24 动态加载 node:sqlite 时会异步抛 ExperimentalWarning。
  24. // 这条 warning 会绕过 console spy,并在 Vitest 环境关闭时触发 onUserConsoleLog pending。
  25. if (warningType === "ExperimentalWarning" && message.includes("SQLite")) {
  26. return;
  27. }
  28. return (originalEmitWarning as (...warningArgs: unknown[]) => void).apply(process, args);
  29. }) as typeof process.emitWarning);
  30. });
  31. it("does not register app-level telemetry in local services", async () => {
  32. // Bugfix: 这条用例依赖动态 import Node 侧服务装配模块。
  33. // 在 pre-push 全量并发跑测试时,模块初始化偶发超过默认 5 秒,
  34. // 之前会被误判成失败并直接挡住 git push。这里放宽单测超时,
  35. // 保持断言语义不变,只消除环境抖动带来的假红。
  36. const [{ createLocalServices }, { createServiceDescriptor }] = await Promise.all([
  37. import("../src/node.js"),
  38. import("../src/descriptors.js"),
  39. ]);
  40. const services = createLocalServices({
  41. zcodeBuiltinProviderConfigFilePath: resolve(
  42. import.meta.dirname,
  43. "../../../config/provider/zcode-builtin.json",
  44. ),
  45. });
  46. const telemetryDescriptor = createServiceDescriptor<{
  47. reportAppLaunch: unknown;
  48. reportAppDailyActive: unknown;
  49. }>("telemetry");
  50. expect(() => services.get(telemetryDescriptor)).toThrow("Service not registered: telemetry");
  51. }, 15_000);
  52. });