armsCustomEventObservability.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. recordArmsCustomEventForE2E,
  4. type ArmsCustomEventE2EEntry,
  5. } from "@/lib/armsCustomEventObservability.js";
  6. describe("recordArmsCustomEventForE2E", () => {
  7. it("非 E2E 构建不创建调试缓冲", () => {
  8. const host = {} as Window & {
  9. __zcodeArmsCustomEventsE2E?: ArmsCustomEventE2EEntry[];
  10. };
  11. recordArmsCustomEventForE2E(
  12. { name: "plan_request", group: "plan_usage" },
  13. { enabled: false, host },
  14. );
  15. expect(host.__zcodeArmsCustomEventsE2E).toBeUndefined();
  16. });
  17. it("E2E 缓冲保留最新 200 条完整 payload", () => {
  18. const host = {} as Window & {
  19. __zcodeArmsCustomEventsE2E?: ArmsCustomEventE2EEntry[];
  20. };
  21. for (let index = 0; index < 205; index += 1) {
  22. recordArmsCustomEventForE2E(
  23. {
  24. name: `event-${index}`,
  25. group: "ui_perf",
  26. value: index,
  27. properties: { talk_id: `session-${index}` },
  28. },
  29. { enabled: true, host, now: () => index },
  30. );
  31. }
  32. expect(host.__zcodeArmsCustomEventsE2E).toHaveLength(200);
  33. expect(host.__zcodeArmsCustomEventsE2E?.[0]).toEqual({
  34. name: "event-5",
  35. group: "ui_perf",
  36. value: 5,
  37. properties: { talk_id: "session-5" },
  38. recordedAt: 5,
  39. });
  40. expect(host.__zcodeArmsCustomEventsE2E?.at(-1)?.name).toBe("event-204");
  41. });
  42. });