databaseStartupTelemetry.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { expect, it, vi } from "vitest";
  2. import type { DatabaseStartupState } from "@zcode/shared";
  3. const sdk = vi.hoisted(() => ({ sendCustom: vi.fn(), getConfig: () => ({ env: "prod" }) }));
  4. vi.mock("@arms/rum-electron", () => ({ default: sdk }));
  5. vi.mock("../src/main/desktopDeviceMid.js", () => ({
  6. ensureDesktopDeviceMidSync: () => "test-device",
  7. }));
  8. vi.mock("../src/main/logger.js", () => ({ logger: { info: vi.fn() } }));
  9. import { reportDatabaseStartupState } from "../src/main/databaseStartupTelemetry.js";
  10. it("reports bounded terminal summaries, deduplicates terminal events, and keeps unknown out of the peak distribution", () => {
  11. const state: DatabaseStartupState = {
  12. schemaVersion: 1,
  13. startupId: "telemetry-startup",
  14. attemptId: "telemetry-attempt",
  15. sequence: 1,
  16. phase: "preparing_host_storage",
  17. startedAt: 100,
  18. updatedAt: 200,
  19. disk: [],
  20. };
  21. reportDatabaseStartupState(state);
  22. const count = sdk.sendCustom.mock.calls.length;
  23. reportDatabaseStartupState({ ...state, sequence: 2, updatedAt: 2200 });
  24. expect(sdk.sendCustom).toHaveBeenCalledTimes(count);
  25. const failed: DatabaseStartupState = {
  26. ...state,
  27. sequence: 3,
  28. phase: "failed",
  29. failedPhase: "preparing_host_storage",
  30. errorCode: "storage_full",
  31. sqliteCode: 13,
  32. updatedAt: 3000,
  33. disk: [
  34. {
  35. scopeId: "known",
  36. observedAvailableDropPeakBytes: 800,
  37. minAvailableBytes: 200,
  38. quality: "complete",
  39. sampledAt: 2000,
  40. },
  41. {
  42. scopeId: "unknown",
  43. observedAvailableDropPeakBytes: null,
  44. minAvailableBytes: null,
  45. quality: "unknown",
  46. sampledAt: null,
  47. },
  48. ],
  49. };
  50. reportDatabaseStartupState(failed);
  51. reportDatabaseStartupState(failed);
  52. const events = sdk.sendCustom.mock.calls.map(([event]) => event);
  53. expect(events.filter((event) => event.name === "database_startup_result")).toHaveLength(1);
  54. expect(
  55. events.filter((event) => event.name === "database_startup_disk").map((event) => event.value),
  56. ).toEqual([800]);
  57. expect(events.filter((event) => event.name === "database_startup_disk_unavailable")).toHaveLength(
  58. 1,
  59. );
  60. expect(
  61. events.find((event) => event.name === "database_startup_result").properties.sqlite_code,
  62. ).toBe("13");
  63. for (const event of events) expect(Object.keys(event.properties).length).toBeLessThanOrEqual(20);
  64. });
  65. it("SILENTDB-07: includes observed migration kind and execution/commit facts in the database result", () => {
  66. for (const kind of ["none", "upgrade"] as const) {
  67. reportDatabaseStartupState({
  68. schemaVersion: 1,
  69. startupId: kind,
  70. attemptId: "silent-telemetry-" + kind,
  71. sequence: 1,
  72. startedAt: 1,
  73. updatedAt: 2,
  74. phase: "ready",
  75. disk: [],
  76. migration: {
  77. kind,
  78. executedCount: kind === "none" ? 0 : 2,
  79. committedCount: kind === "none" ? 0 : 2,
  80. },
  81. });
  82. const event = sdk.sendCustom.mock.calls
  83. .map(([event]) => event)
  84. .find(
  85. (e) =>
  86. e.name === "database_startup_result" &&
  87. e.properties.attempt_id === "silent-telemetry-" + kind,
  88. );
  89. expect(event.properties).toMatchObject({
  90. migration_kind: kind,
  91. migration_executed_count: kind === "none" ? "0" : "2",
  92. migration_committed_count: kind === "none" ? "0" : "2",
  93. });
  94. }
  95. });
  96. it.each(["failed", "starting_services"] as const)(
  97. "reports baseline events once at %s, including none and unknown",
  98. (resultPhase) => {
  99. const state: DatabaseStartupState = {
  100. schemaVersion: 1,
  101. startupId: "baseline",
  102. attemptId: "baseline-attempt-" + resultPhase,
  103. sequence: 1,
  104. phase: "preparing_host_storage",
  105. startedAt: 100,
  106. updatedAt: 200,
  107. disk: [],
  108. migrationBaselines: [
  109. {
  110. databaseId: "tasks-index",
  111. databaseKind: "tasks-index",
  112. lastAppliedMigrationId: "0002_provider_selection",
  113. },
  114. { databaseId: "session-a", databaseKind: "session", lastAppliedMigrationId: null },
  115. { databaseId: "session-b", databaseKind: "session" },
  116. ],
  117. };
  118. const events = () =>
  119. sdk.sendCustom.mock.calls
  120. .map(([e]) => e)
  121. .filter(
  122. (e) =>
  123. e.name === "database_startup_baseline" && e.properties.attempt_id === state.attemptId,
  124. );
  125. reportDatabaseStartupState(state);
  126. expect(events()).toHaveLength(0);
  127. const failed = {
  128. ...state,
  129. phase: resultPhase,
  130. failedPhase: "preparing_session_storage" as const,
  131. errorCode: "storage_full" as const,
  132. sequence: 2,
  133. };
  134. reportDatabaseStartupState(failed);
  135. reportDatabaseStartupState(failed);
  136. reportDatabaseStartupState({ ...failed, phase: "ready", sequence: 3 });
  137. expect(events().map((e) => e.properties.last_applied_migration_id)).toEqual([
  138. "0002_provider_selection",
  139. "none",
  140. "unknown",
  141. ]);
  142. expect(events().map((e) => e.properties.database_kind)).toEqual([
  143. "tasks-index",
  144. "session",
  145. "session",
  146. ]);
  147. expect(new Set(events().map((e) => e.properties.startup_event_id)).size).toBe(3);
  148. for (const event of events())
  149. expect(Object.keys(event.properties).length).toBeLessThanOrEqual(20);
  150. },
  151. );