desktopArmsCustomEventE2E.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { describe, expect, it, vi } from "vitest";
  2. import { shouldEnableE2ETestBridge } from "@zcode/shared";
  3. import {
  4. createFinalArmsCustomEventE2EController,
  5. dispatchFinalArmsCustomEvent,
  6. } from "../src/main/desktopArmsCustomEvent.js";
  7. const context = {
  8. deviceMid: "device-1",
  9. platform: "darwin" as const,
  10. appVersion: "9.8.7",
  11. armsEnv: "local" as const,
  12. rendererId: 42,
  13. };
  14. describe("desktop final ARMS custom E2E bridge", () => {
  15. it("只有 build flag 与真实 E2E runner 同时存在才开启", () => {
  16. expect(shouldEnableE2ETestBridge({})).toBe(false);
  17. expect(
  18. shouldEnableE2ETestBridge({
  19. VITE_ZCODE_E2E_STORE_BRIDGE: "1",
  20. }),
  21. ).toBe(false);
  22. expect(
  23. shouldEnableE2ETestBridge({
  24. ZCODE_E2E_RUN_ID: "run-1",
  25. }),
  26. ).toBe(false);
  27. expect(
  28. shouldEnableE2ETestBridge({
  29. VITE_ZCODE_E2E_STORE_BRIDGE: "1",
  30. ZCODE_E2E_RUN_ID: "run-1",
  31. }),
  32. ).toBe(true);
  33. });
  34. it("捕获 main 补齐后的最终参数,并只抑制命中的目标事件", () => {
  35. let now = 1_000;
  36. const controller = createFinalArmsCustomEventE2EController({
  37. now: () => now,
  38. });
  39. const sendCustom = vi.fn();
  40. controller.configure({ suppressedEventNames: ["plan_ttft"] });
  41. const dispatched = dispatchFinalArmsCustomEvent({
  42. payload: {
  43. name: "plan_ttft",
  44. group: "plan_usage",
  45. value: 123,
  46. properties: {
  47. talk_id: "session-1",
  48. waiting_tool: false,
  49. },
  50. },
  51. context,
  52. e2eController: controller,
  53. sendCustom,
  54. });
  55. expect(dispatched.suppressed).toBe(true);
  56. expect(sendCustom).not.toHaveBeenCalled();
  57. expect(controller.read()).toEqual([
  58. {
  59. sequence: 1,
  60. recordedAt: 1_000,
  61. payload: {
  62. name: "plan_ttft",
  63. type: "custom",
  64. group: "plan_usage",
  65. value: 123,
  66. properties: {
  67. event_name: "plan_ttft",
  68. app_version: "9.8.7",
  69. arms_env: "local",
  70. device_mid: "device-1",
  71. platform: "macos",
  72. renderer_id: "42",
  73. metric_value: "123",
  74. talk_id: "session-1",
  75. waiting_tool: "false",
  76. },
  77. },
  78. },
  79. ]);
  80. now = 1_001;
  81. dispatchFinalArmsCustomEvent({
  82. payload: { name: "perf_ui_message_complete", group: "ui_perf" },
  83. context,
  84. e2eController: controller,
  85. sendCustom,
  86. });
  87. expect(sendCustom).toHaveBeenCalledOnce();
  88. expect(sendCustom.mock.calls[0]?.[0]).toMatchObject({
  89. name: "perf_ui_message_complete",
  90. value: 1,
  91. properties: { metric_value: "1" },
  92. });
  93. expect(controller.read()[1]?.payload).toEqual(sendCustom.mock.calls[0]?.[0]);
  94. });
  95. it("ring 有界、读取返回副本,clear 不持久化历史", () => {
  96. const controller = createFinalArmsCustomEventE2EController({
  97. capacity: 2,
  98. now: () => 7,
  99. });
  100. const sendCustom = vi.fn();
  101. for (const name of ["event-1", "event-2", "event-3"]) {
  102. dispatchFinalArmsCustomEvent({
  103. payload: { name, group: "test" },
  104. context,
  105. e2eController: controller,
  106. sendCustom,
  107. });
  108. }
  109. const firstRead = controller.read();
  110. expect(firstRead.map((entry) => entry.payload.name)).toEqual(["event-2", "event-3"]);
  111. expect(firstRead.map((entry) => entry.sequence)).toEqual([2, 3]);
  112. firstRead[0]!.payload.properties.app_version = "mutated";
  113. expect(controller.read()[0]?.payload.properties.app_version).toBe("9.8.7");
  114. controller.clear();
  115. expect(controller.read()).toEqual([]);
  116. });
  117. it("拒绝无界或非法 suppression 配置", () => {
  118. const controller = createFinalArmsCustomEventE2EController();
  119. expect(() =>
  120. controller.configure({
  121. suppressedEventNames: Array.from({ length: 101 }, (_, index) => `event-${index}`),
  122. }),
  123. ).toThrow("最多 100 项");
  124. expect(() => controller.configure({ suppressedEventNames: [""] })).toThrow("空值");
  125. });
  126. });