app-telemetry-runtime.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { describe, expect, it, vi } from "vitest";
  2. describe("app telemetry runtime", () => {
  3. it("按 renderer id 返回对应的埋点上下文", async () => {
  4. const { createAppTelemetryRuntime } = await import("../src/main/appTelemetryRuntime.js");
  5. const runtime = createAppTelemetryRuntime({
  6. telemetryCore: {
  7. reportAppLaunch: vi.fn(async () => {}),
  8. reportAppDailyActive: vi.fn(async () => {}),
  9. },
  10. appLaunchCoordinator: {
  11. onRendererReady: () => false,
  12. onOAuthCallbackHandled: () => false,
  13. },
  14. });
  15. const firstContext = {
  16. clientTimezone: "Asia/Shanghai",
  17. clientLanguage: "zh-CN",
  18. screenResolution: "3024x1964",
  19. };
  20. const secondContext = {
  21. clientTimezone: "Europe/Berlin",
  22. clientLanguage: "de-DE",
  23. screenResolution: "1920x1080",
  24. };
  25. runtime.syncRendererContext({ rendererId: 1, context: firstContext });
  26. runtime.syncRendererContext({ rendererId: 2, context: secondContext });
  27. expect(runtime.getRendererContext(1)).toEqual(firstContext);
  28. expect(runtime.getRendererContext(2)).toEqual(secondContext);
  29. expect(runtime.getRendererContext(3)).toBeNull();
  30. });
  31. it("waits for renderer context before reporting startup telemetry", async () => {
  32. const { createAppTelemetryRuntime } = await import("../src/main/appTelemetryRuntime.js");
  33. const reportAppLaunch = vi.fn(async () => {});
  34. const reportAppDailyActive = vi.fn(async () => {});
  35. const runtime = createAppTelemetryRuntime({
  36. telemetryCore: {
  37. reportAppLaunch,
  38. reportAppDailyActive,
  39. },
  40. appLaunchCoordinator: {
  41. onRendererReady: () => true,
  42. onOAuthCallbackHandled: () => false,
  43. },
  44. });
  45. runtime.onRendererReady({
  46. hasPendingOAuthCallback: false,
  47. rendererId: 1,
  48. });
  49. expect(reportAppLaunch).not.toHaveBeenCalled();
  50. expect(reportAppDailyActive).not.toHaveBeenCalled();
  51. runtime.syncRendererContext({
  52. rendererId: 1,
  53. context: {
  54. clientTimezone: "Asia/Shanghai",
  55. clientLanguage: "zh-CN",
  56. screenResolution: "3024x1964",
  57. },
  58. });
  59. await Promise.resolve();
  60. expect(reportAppLaunch).toHaveBeenCalledWith({
  61. clientTimezone: "Asia/Shanghai",
  62. clientLanguage: "zh-CN",
  63. screenResolution: "3024x1964",
  64. });
  65. expect(reportAppDailyActive).toHaveBeenCalledWith({
  66. clientTimezone: "Asia/Shanghai",
  67. clientLanguage: "zh-CN",
  68. screenResolution: "3024x1964",
  69. });
  70. });
  71. it("waits for OAuth callback handling before reporting startup telemetry", async () => {
  72. const { createAppTelemetryRuntime } = await import("../src/main/appTelemetryRuntime.js");
  73. const reportAppLaunch = vi.fn(async () => {});
  74. const reportAppDailyActive = vi.fn(async () => {});
  75. const runtime = createAppTelemetryRuntime({
  76. telemetryCore: {
  77. reportAppLaunch,
  78. reportAppDailyActive,
  79. },
  80. appLaunchCoordinator: {
  81. onRendererReady: () => false,
  82. onOAuthCallbackHandled: () => true,
  83. },
  84. });
  85. runtime.syncRendererContext({
  86. rendererId: 1,
  87. context: {
  88. clientTimezone: "Asia/Shanghai",
  89. clientLanguage: "zh-CN",
  90. screenResolution: "3024x1964",
  91. },
  92. });
  93. runtime.onRendererReady({
  94. hasPendingOAuthCallback: true,
  95. rendererId: 1,
  96. });
  97. expect(reportAppLaunch).not.toHaveBeenCalled();
  98. expect(reportAppDailyActive).not.toHaveBeenCalled();
  99. runtime.onOAuthCallbackHandled({ rendererId: 1 });
  100. await Promise.resolve();
  101. expect(reportAppLaunch).toHaveBeenCalledTimes(1);
  102. expect(reportAppDailyActive).toHaveBeenCalledTimes(1);
  103. });
  104. it("binds startup telemetry context to the renderer that completes OAuth", async () => {
  105. const { createAppTelemetryRuntime } = await import("../src/main/appTelemetryRuntime.js");
  106. const reportAppLaunch = vi.fn(async () => {});
  107. const reportAppDailyActive = vi.fn(async () => {});
  108. const runtime = createAppTelemetryRuntime({
  109. telemetryCore: {
  110. reportAppLaunch,
  111. reportAppDailyActive,
  112. },
  113. appLaunchCoordinator: {
  114. onRendererReady: () => false,
  115. onOAuthCallbackHandled: () => true,
  116. },
  117. });
  118. runtime.onRendererReady({
  119. hasPendingOAuthCallback: true,
  120. rendererId: 1,
  121. });
  122. runtime.syncRendererContext({
  123. rendererId: 2,
  124. context: {
  125. clientTimezone: "Europe/Berlin",
  126. clientLanguage: "de-DE",
  127. screenResolution: "1920x1080",
  128. },
  129. });
  130. runtime.onOAuthCallbackHandled({ rendererId: 1 });
  131. expect(reportAppLaunch).not.toHaveBeenCalled();
  132. expect(reportAppDailyActive).not.toHaveBeenCalled();
  133. runtime.syncRendererContext({
  134. rendererId: 1,
  135. context: {
  136. clientTimezone: "Asia/Shanghai",
  137. clientLanguage: "zh-CN",
  138. screenResolution: "3024x1964",
  139. },
  140. });
  141. await Promise.resolve();
  142. expect(reportAppLaunch).toHaveBeenCalledWith({
  143. clientTimezone: "Asia/Shanghai",
  144. clientLanguage: "zh-CN",
  145. screenResolution: "3024x1964",
  146. });
  147. expect(reportAppDailyActive).toHaveBeenCalledWith({
  148. clientTimezone: "Asia/Shanghai",
  149. clientLanguage: "zh-CN",
  150. screenResolution: "3024x1964",
  151. });
  152. });
  153. it("reports daily active when the app becomes interactive again", async () => {
  154. const { createAppTelemetryRuntime } = await import("../src/main/appTelemetryRuntime.js");
  155. const reportAppLaunch = vi.fn(async () => {});
  156. const reportAppDailyActive = vi.fn(async () => {});
  157. const runtime = createAppTelemetryRuntime({
  158. telemetryCore: {
  159. reportAppLaunch,
  160. reportAppDailyActive,
  161. },
  162. appLaunchCoordinator: {
  163. onRendererReady: () => false,
  164. onOAuthCallbackHandled: () => false,
  165. },
  166. });
  167. runtime.syncRendererContext({
  168. rendererId: 1,
  169. context: {
  170. clientTimezone: "Asia/Shanghai",
  171. clientLanguage: "zh-CN",
  172. screenResolution: "3024x1964",
  173. },
  174. });
  175. runtime.setInteractive(false);
  176. runtime.setInteractive(true);
  177. runtime.setInteractive(true);
  178. await Promise.resolve();
  179. expect(reportAppLaunch).not.toHaveBeenCalled();
  180. expect(reportAppDailyActive).toHaveBeenCalledTimes(1);
  181. expect(reportAppDailyActive).toHaveBeenCalledWith({
  182. clientTimezone: "Asia/Shanghai",
  183. clientLanguage: "zh-CN",
  184. screenResolution: "3024x1964",
  185. });
  186. });
  187. it("uses the daily active heartbeat only while interactive", async () => {
  188. const { createAppTelemetryRuntime } = await import("../src/main/appTelemetryRuntime.js");
  189. const reportAppLaunch = vi.fn(async () => {});
  190. const reportAppDailyActive = vi.fn(async () => {});
  191. let intervalHandler: (() => void) | null = null;
  192. const runtime = createAppTelemetryRuntime({
  193. telemetryCore: {
  194. reportAppLaunch,
  195. reportAppDailyActive,
  196. },
  197. appLaunchCoordinator: {
  198. onRendererReady: () => false,
  199. onOAuthCallbackHandled: () => false,
  200. },
  201. setInterval: (handler) => {
  202. intervalHandler = handler;
  203. return 1;
  204. },
  205. clearInterval: vi.fn(),
  206. });
  207. runtime.syncRendererContext({
  208. rendererId: 1,
  209. context: {
  210. clientTimezone: "Asia/Shanghai",
  211. clientLanguage: "zh-CN",
  212. screenResolution: "3024x1964",
  213. },
  214. });
  215. intervalHandler?.();
  216. runtime.setInteractive(true);
  217. intervalHandler?.();
  218. runtime.setInteractive(false);
  219. intervalHandler?.();
  220. await Promise.resolve();
  221. expect(reportAppLaunch).not.toHaveBeenCalled();
  222. expect(reportAppDailyActive).toHaveBeenCalledTimes(2);
  223. });
  224. });