desktopRemoteUsageArmsTelemetry.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import {
  3. buildRemoteActiveSessionCountArmsPayload,
  4. buildRemoteConnectResultArmsPayload,
  5. buildRemoteDisconnectArmsPayload,
  6. configureRemoteUsageArmsTelemetry,
  7. REMOTE_USAGE_GAUGE_INTERVAL_MS,
  8. reportRemoteConnectResultToArms,
  9. reportRemoteConnectionStateChangedToArms,
  10. reportRemoteDisconnectToArms,
  11. resetRemoteUsageArmsTelemetryForTest,
  12. stopRemoteUsageArmsPeriodicSampling,
  13. } from "../src/main/desktopRemoteUsageArmsTelemetry.js";
  14. import { createFinalArmsCustomEventE2EController } from "../src/main/desktopArmsCustomEvent.js";
  15. describe("desktop remote usage ARMS telemetry", () => {
  16. afterEach(() => {
  17. resetRemoteUsageArmsTelemetryForTest();
  18. vi.useRealTimers();
  19. });
  20. it("为连接结果、并发 gauge 和断开时长构造独立数值事件", () => {
  21. expect(
  22. buildRemoteConnectResultArmsPayload({
  23. result: "success",
  24. remoteKind: "ssh",
  25. connectTrigger: "new",
  26. }),
  27. ).toEqual({
  28. name: "remote_connect_result",
  29. group: "remote_usage",
  30. value: 1,
  31. properties: {
  32. result: "success",
  33. remote_kind: "ssh",
  34. connect_trigger: "new",
  35. error_category: "",
  36. },
  37. });
  38. expect(
  39. buildRemoteActiveSessionCountArmsPayload(
  40. {
  41. sampleReason: "state-change",
  42. transition: "connected",
  43. remoteKind: "ssh",
  44. },
  45. { activeSessionCount: 2, activeTargetCount: 1 },
  46. ),
  47. ).toEqual({
  48. name: "remote_active_session_count",
  49. group: "remote_usage",
  50. value: 2,
  51. properties: {
  52. sample_reason: "state-change",
  53. transition: "connected",
  54. remote_kind: "ssh",
  55. active_target_count: 1,
  56. },
  57. });
  58. expect(
  59. buildRemoteDisconnectArmsPayload({
  60. remoteKind: "ssh",
  61. disconnectReason: "disposed",
  62. durationMs: 12_345,
  63. }),
  64. ).toEqual({
  65. name: "remote_disconnect",
  66. group: "remote_usage",
  67. value: 12_345,
  68. properties: {
  69. remote_kind: "ssh",
  70. disconnect_reason: "disposed",
  71. duration_ms: 12_345,
  72. },
  73. });
  74. });
  75. it("经最终 ARMS bridge 补齐公共维度,并遵守 E2E suppression", () => {
  76. const controller = createFinalArmsCustomEventE2EController({
  77. now: () => 7,
  78. });
  79. const sendCustom = vi.fn();
  80. controller.configure({ suppressedEventNames: ["remote_connect_result"] });
  81. configureRemoteUsageArmsTelemetry({
  82. armsCustomContext: {
  83. deviceMid: "device-1",
  84. platform: "darwin",
  85. appVersion: "1.2.3",
  86. armsEnv: "test",
  87. },
  88. getRemoteConnectionStats: () => ({
  89. activeSessionCount: 0,
  90. activeTargetCount: 0,
  91. }),
  92. sendCustom,
  93. e2eController: controller,
  94. logger: { warn: vi.fn() },
  95. });
  96. reportRemoteConnectResultToArms({
  97. rendererId: 73,
  98. result: "failure",
  99. remoteKind: "wsl",
  100. connectTrigger: "reconnect",
  101. });
  102. expect(sendCustom).not.toHaveBeenCalled();
  103. expect(controller.read()).toEqual([
  104. {
  105. sequence: 1,
  106. recordedAt: 7,
  107. payload: {
  108. name: "remote_connect_result",
  109. type: "custom",
  110. group: "remote_usage",
  111. value: 1,
  112. properties: {
  113. event_name: "remote_connect_result",
  114. app_version: "1.2.3",
  115. arms_env: "test",
  116. device_mid: "device-1",
  117. platform: "macos",
  118. renderer_id: "73",
  119. metric_value: "1",
  120. result: "failure",
  121. remote_kind: "wsl",
  122. connect_trigger: "reconnect",
  123. error_category: "unknown",
  124. },
  125. },
  126. },
  127. ]);
  128. });
  129. it("状态变化即时上报,并在仍有连接时每五分钟补一个 gauge", async () => {
  130. vi.useFakeTimers();
  131. let stats = { activeSessionCount: 1, activeTargetCount: 1 };
  132. const sendCustom = vi.fn();
  133. configureRemoteUsageArmsTelemetry({
  134. armsCustomContext: {
  135. deviceMid: "device-1",
  136. platform: "linux",
  137. appVersion: "1.2.3",
  138. armsEnv: "test",
  139. },
  140. getRemoteConnectionStats: () => stats,
  141. sendCustom,
  142. logger: { warn: vi.fn() },
  143. });
  144. reportRemoteConnectionStateChangedToArms({
  145. rendererId: 81,
  146. transition: "connected",
  147. remoteKind: "docker",
  148. });
  149. expect(sendCustom).toHaveBeenLastCalledWith(
  150. expect.objectContaining({
  151. name: "remote_active_session_count",
  152. value: 1,
  153. properties: expect.objectContaining({
  154. renderer_id: "81",
  155. sample_reason: "state-change",
  156. transition: "connected",
  157. remote_kind: "docker",
  158. active_target_count: "1",
  159. }),
  160. }),
  161. );
  162. sendCustom.mockClear();
  163. await vi.advanceTimersByTimeAsync(REMOTE_USAGE_GAUGE_INTERVAL_MS);
  164. expect(sendCustom).toHaveBeenCalledOnce();
  165. expect(sendCustom).toHaveBeenLastCalledWith(
  166. expect.objectContaining({
  167. value: 1,
  168. properties: expect.objectContaining({
  169. renderer_id: "81",
  170. sample_reason: "periodic",
  171. transition: "none",
  172. remote_kind: "",
  173. }),
  174. }),
  175. );
  176. stats = { activeSessionCount: 0, activeTargetCount: 0 };
  177. await vi.advanceTimersByTimeAsync(REMOTE_USAGE_GAUGE_INTERVAL_MS);
  178. expect(sendCustom).toHaveBeenCalledOnce();
  179. stopRemoteUsageArmsPeriodicSampling();
  180. stats = { activeSessionCount: 1, activeTargetCount: 1 };
  181. await vi.advanceTimersByTimeAsync(REMOTE_USAGE_GAUGE_INTERVAL_MS);
  182. expect(sendCustom).toHaveBeenCalledOnce();
  183. });
  184. it("未配置或读取/发送失败时保持旁路且不抛错", () => {
  185. expect(() =>
  186. reportRemoteDisconnectToArms({
  187. rendererId: 1,
  188. remoteKind: "server",
  189. disconnectReason: "host-exit",
  190. durationMs: 500,
  191. }),
  192. ).not.toThrow();
  193. const logger = { warn: vi.fn() };
  194. const sendCustom = vi.fn(() => {
  195. throw new Error("ARMS unavailable");
  196. });
  197. configureRemoteUsageArmsTelemetry({
  198. armsCustomContext: {
  199. deviceMid: "device-1",
  200. platform: "win32",
  201. appVersion: "1.2.3",
  202. armsEnv: "test",
  203. },
  204. getRemoteConnectionStats: () => {
  205. throw new Error("stats unavailable");
  206. },
  207. sendCustom,
  208. logger,
  209. });
  210. expect(() =>
  211. reportRemoteConnectionStateChangedToArms({
  212. rendererId: 2,
  213. transition: "connected",
  214. remoteKind: "wsl",
  215. }),
  216. ).not.toThrow();
  217. expect(() =>
  218. reportRemoteDisconnectToArms({
  219. rendererId: 2,
  220. remoteKind: "wsl",
  221. disconnectReason: "app-shutdown",
  222. durationMs: 500,
  223. }),
  224. ).not.toThrow();
  225. expect(logger.warn).toHaveBeenCalledTimes(2);
  226. });
  227. });