appTelemetryPrivacy.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { reportAppTelemetryEvent } from "@/lib/appTelemetry.js";
  3. import { logger } from "@/logger.js";
  4. vi.mock("@/logger.js", () => ({ logger: { info: vi.fn(), debug: vi.fn() } }));
  5. beforeEach(() => vi.clearAllMocks());
  6. describe("UI telemetry privacy", () => {
  7. it.each(["agent_step", "message_completion", "automation_create_result", "future_event"])(
  8. "%s 在 IPC 和日志之前移除错误原文",
  9. async (elementName) => {
  10. const reportTelemetryEvent = vi.fn(async () => {});
  11. const payload = {
  12. elementName,
  13. eventRegion: "app",
  14. eventType: "result",
  15. eventExtraDetail: {
  16. error_msg: "Authorization: Bearer FAKE_PRIVATE_VALUE /Users/private/file",
  17. error_type: "TOOL_EXEC_ERROR",
  18. },
  19. };
  20. await reportAppTelemetryEvent({ reportTelemetryEvent }, payload, "test");
  21. expect(reportTelemetryEvent).toHaveBeenCalledWith(
  22. expect.objectContaining({
  23. eventExtraDetail: { error_msg: "[redacted]", error_type: "TOOL_EXEC_ERROR" },
  24. }),
  25. );
  26. expect(logger.info).not.toHaveBeenCalled();
  27. expect(JSON.stringify(vi.mocked(logger.debug).mock.calls)).not.toContain(
  28. "FAKE_PRIVATE_VALUE",
  29. );
  30. if (elementName === "agent_step" || elementName === "message_completion") {
  31. expect(logger.debug).toHaveBeenCalledWith(
  32. expect.any(String),
  33. expect.objectContaining({
  34. eventExtraDetail: { error_msg: "[redacted]", error_type: "TOOL_EXEC_ERROR" },
  35. }),
  36. );
  37. }
  38. expect(payload.eventExtraDetail.error_msg).toContain("FAKE_PRIVATE_VALUE");
  39. },
  40. );
  41. it("旧调用方传入完整登录 URL 也不会穿过 IPC", async () => {
  42. const reportTelemetryEvent = vi.fn(async () => {});
  43. await reportAppTelemetryEvent(
  44. { reportTelemetryEvent },
  45. {
  46. elementName: "app_login_ck",
  47. eventRegion: "app",
  48. eventType: "ck",
  49. eventExtraDetail: {
  50. login_url: "https://user:secret@login.example.com:443/oauth?state=secret#secret",
  51. },
  52. },
  53. "test",
  54. );
  55. expect(reportTelemetryEvent).toHaveBeenCalledWith(
  56. expect.objectContaining({
  57. eventExtraDetail: { login_url: "login.example.com" },
  58. }),
  59. );
  60. });
  61. });