File size: 15,447 Bytes
3201ca6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | import { renderHook, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as telemetry from "#/services/telemetry";
const useSettingsMock = vi.fn();
vi.mock("#/hooks/query/use-settings", () => ({
useSettings: () => useSettingsMock(),
}));
const compatibilityMocks = vi.hoisted(() => ({
getCachedAgentServerVersion: vi.fn(),
}));
vi.mock("#/api/agent-server-compatibility", () => ({
getCachedAgentServerVersion: compatibilityMocks.getCachedAgentServerVersion,
}));
const automationSdkVersionHookMock = vi.hoisted(() => ({
useAutomationSdkVersion: vi.fn(),
}));
vi.mock("#/hooks/query/use-automation-sdk-version", () => ({
useAutomationSdkVersion: automationSdkVersionHookMock.useAutomationSdkVersion,
}));
import { useTracking } from "#/hooks/use-tracking";
// Resolved at test-run time so it matches whatever URL jsdom is configured
// with in the current environment (varies between local and CI).
let COMMON: Record<string, unknown>;
describe("useTracking", () => {
let captureMock: ReturnType<typeof vi.spyOn>;
let setBackendContextMock: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
captureMock = vi
.spyOn(telemetry, "trackEvent")
.mockResolvedValue(undefined);
setBackendContextMock = vi.spyOn(telemetry, "setTelemetryBackendContext");
useSettingsMock.mockReset();
automationSdkVersionHookMock.useAutomationSdkVersion.mockReset();
automationSdkVersionHookMock.useAutomationSdkVersion.mockReturnValue(null);
compatibilityMocks.getCachedAgentServerVersion.mockReset();
compatibilityMocks.getCachedAgentServerVersion.mockReturnValue(null);
useSettingsMock.mockReturnValue({
data: { email: "user@example.com", user_consents_to_analytics: true },
});
COMMON = {
current_url: window.location.href,
};
});
afterEach(() => {
captureMock.mockRestore();
setBackendContextMock.mockRestore();
});
const getTracking = () => renderHook(() => useTracking()).result.current;
describe("trackLoginButtonClick", () => {
it("captures login_button_clicked with provider and commonProperties", () => {
getTracking().trackLoginButtonClick({ provider: "github" });
expect(captureMock).toHaveBeenCalledWith("login_button_clicked", {
provider: "github",
...COMMON,
});
});
});
describe("trackConversationCreated", () => {
it("captures conversation_start_requested with full metadata for a repository, task-start conversation", () => {
getTracking().trackConversationCreated({
conversationId: "task-abc123",
taskId: "abc123",
hasRepository: true,
gitProvider: "github",
hasWorkspace: false,
hasInitialQuery: true,
hasParentConversation: false,
entryPoint: "sidebar_cloud_menu",
});
expect(captureMock).toHaveBeenCalledWith("conversation_start_requested", {
conversation_id: "task-abc123",
task_id: "abc123",
// task-prefixed conversation_id => cloud sandbox still provisioning
is_start_task: true,
has_repository: true,
git_provider: "github",
has_workspace: false,
workspace_mode: undefined,
has_initial_query: true,
agent_type: undefined,
has_parent_conversation: false,
entry_point: "sidebar_cloud_menu",
...COMMON,
});
});
it("captures conversation_start_requested for a local workspace start with no repository", () => {
getTracking().trackConversationCreated({
conversationId: "conv-1",
taskId: "conv-1",
hasRepository: false,
hasWorkspace: true,
workspaceMode: "local_repo",
hasInitialQuery: false,
hasParentConversation: false,
entryPoint: "sidebar_local_menu",
});
expect(captureMock).toHaveBeenCalledWith("conversation_start_requested", {
conversation_id: "conv-1",
task_id: "conv-1",
// real conversation_id => ready immediately (local)
is_start_task: false,
has_repository: false,
git_provider: undefined,
has_workspace: true,
workspace_mode: "local_repo",
has_initial_query: false,
agent_type: undefined,
has_parent_conversation: false,
entry_point: "sidebar_local_menu",
...COMMON,
});
});
it("captures agent_type and has_parent_conversation for a plan sub-conversation", () => {
getTracking().trackConversationCreated({
conversationId: "task-plan-1",
taskId: "plan-1",
hasRepository: false,
hasWorkspace: false,
hasInitialQuery: false,
agentType: "plan",
hasParentConversation: true,
entryPoint: "plan_sub_conversation",
});
expect(captureMock).toHaveBeenCalledWith(
"conversation_start_requested",
expect.objectContaining({
agent_type: "plan",
has_parent_conversation: true,
}),
);
});
it("declares backend versions to the telemetry service when known", async () => {
automationSdkVersionHookMock.useAutomationSdkVersion.mockReturnValue(
"1.36.3",
);
compatibilityMocks.getCachedAgentServerVersion.mockReturnValue("1.36.2");
const { result } = renderHook(() => useTracking());
await waitFor(() =>
expect(setBackendContextMock).toHaveBeenCalledWith(
expect.objectContaining({ automationSdkVersion: "1.36.3" }),
),
);
setBackendContextMock.mockClear();
result.current.trackConversationCreated({
conversationId: "conv-versioned",
taskId: "conv-versioned",
hasRepository: false,
hasWorkspace: false,
hasInitialQuery: true,
hasParentConversation: false,
entryPoint: "home",
});
expect(setBackendContextMock).toHaveBeenCalledWith(
expect.objectContaining({
backendKind: "local",
agentServerVersion: "1.36.2",
automationSdkVersion: "1.36.3",
}),
);
expect(captureMock).toHaveBeenCalledWith(
"conversation_start_requested",
expect.not.objectContaining({
agent_server_version: "1.36.2",
automation_sdk_version: "1.36.3",
backend_version: "1.36.2",
}),
);
expect(
compatibilityMocks.getCachedAgentServerVersion,
).toHaveBeenCalledWith(expect.any(String));
});
it("uses the latest cached agent-server version when an event fires", () => {
compatibilityMocks.getCachedAgentServerVersion.mockReturnValue(null);
const tracking = getTracking();
setBackendContextMock.mockClear();
compatibilityMocks.getCachedAgentServerVersion.mockReturnValue("1.36.1");
tracking.trackConversationCreated({
conversationId: "conv-cache-late",
taskId: "conv-cache-late",
hasRepository: false,
hasWorkspace: false,
hasInitialQuery: true,
hasParentConversation: false,
entryPoint: "home",
});
expect(setBackendContextMock).toHaveBeenCalledWith(
expect.objectContaining({
backendKind: "local",
agentServerVersion: "1.36.1",
}),
);
});
});
describe("trackPushButtonClick", () => {
it("captures push_button_clicked with commonProperties", () => {
getTracking().trackPushButtonClick();
expect(captureMock).toHaveBeenCalledWith(
"push_button_clicked",
expect.objectContaining(COMMON),
);
});
});
describe("trackPullButtonClick", () => {
it("captures pull_button_clicked with commonProperties", () => {
getTracking().trackPullButtonClick();
expect(captureMock).toHaveBeenCalledWith(
"pull_button_clicked",
expect.objectContaining(COMMON),
);
});
});
describe("trackCreatePrButtonClick", () => {
it("captures create_pr_button_clicked with commonProperties", () => {
getTracking().trackCreatePrButtonClick();
expect(captureMock).toHaveBeenCalledWith(
"create_pr_button_clicked",
expect.objectContaining(COMMON),
);
});
});
describe("trackUserSignupCompleted", () => {
it("captures user_signup_completed with signup_timestamp and commonProperties", () => {
getTracking().trackUserSignupCompleted();
expect(captureMock).toHaveBeenCalledWith(
"user_signup_completed",
expect.objectContaining({
signup_timestamp: expect.any(String),
...COMMON,
}),
);
});
});
describe("trackPrebuiltAutomationEnabled", () => {
it("captures prebuilt_automation_enabled with all fields", () => {
getTracking().trackPrebuiltAutomationEnabled({
automationId: "auto-1",
automationName: "My Automation",
automationCategory: "CI",
});
expect(captureMock).toHaveBeenCalledWith("prebuilt_automation_enabled", {
automation_id: "auto-1",
automation_name: "My Automation",
automation_category: "CI",
...COMMON,
});
});
it("sends undefined fields when optional args are omitted", () => {
getTracking().trackPrebuiltAutomationEnabled({
automationName: "Minimal",
});
expect(captureMock).toHaveBeenCalledWith(
"prebuilt_automation_enabled",
expect.objectContaining({
automation_name: "Minimal",
automation_id: undefined,
automation_category: undefined,
}),
);
});
});
describe("trackInitialQuerySubmitted", () => {
it("captures initial_query_submitted with all properties", () => {
getTracking().trackInitialQuerySubmitted({
entryPoint: "github",
queryCharacterLength: 42,
replayJsonSize: 100,
});
expect(captureMock).toHaveBeenCalledWith("initial_query_submitted", {
entry_point: "github",
query_character_length: 42,
replay_json_size: 100,
...COMMON,
});
});
it("sends undefined replayJsonSize when omitted", () => {
getTracking().trackInitialQuerySubmitted({
entryPoint: "direct",
queryCharacterLength: 10,
});
expect(captureMock).toHaveBeenCalledWith(
"initial_query_submitted",
expect.objectContaining({ replay_json_size: undefined }),
);
});
});
describe("trackUserMessageSent", () => {
it("captures user_message_sent with message counts", () => {
getTracking().trackUserMessageSent({
sessionMessageCount: 5,
currentMessageLength: 120,
});
expect(captureMock).toHaveBeenCalledWith("user_message_sent", {
session_message_count: 5,
current_message_length: 120,
...COMMON,
});
});
});
describe("trackDownloadVsCodeButtonClicked", () => {
it("captures download_via_vscode_button_clicked with commonProperties", () => {
getTracking().trackDownloadVsCodeButtonClicked();
expect(captureMock).toHaveBeenCalledWith(
"download_via_vscode_button_clicked",
expect.objectContaining(COMMON),
);
});
});
describe("trackSettingsSaved", () => {
it("captures settings_saved with all properties using SCREAMING_SNAKE_CASE keys", () => {
getTracking().trackSettingsSaved({
llmModel: "gpt-4",
llmApiKeySet: "SET",
searchApiKeySet: "UNSET",
remoteRuntimeResourceFactor: 2,
});
expect(captureMock).toHaveBeenCalledWith("settings_saved", {
LLM_MODEL: "gpt-4",
LLM_API_KEY_SET: "SET",
SEARCH_API_KEY_SET: "UNSET",
REMOTE_RUNTIME_RESOURCE_FACTOR: 2,
...COMMON,
});
});
});
describe("trackMcpConfigUpdated", () => {
it("captures mcp_config_updated with server counts and has_mcp_config: true", () => {
getTracking().trackMcpConfigUpdated({
sseServersCount: 2,
stdioServersCount: 1,
});
expect(captureMock).toHaveBeenCalledWith("mcp_config_updated", {
has_mcp_config: true,
sse_servers_count: 2,
stdio_servers_count: 1,
...COMMON,
});
});
});
describe("trackDownloadTrajectoryButtonClicked", () => {
it("captures download_trajectory_button_clicked with commonProperties", () => {
getTracking().trackDownloadTrajectoryButtonClicked();
expect(captureMock).toHaveBeenCalledWith(
"download_trajectory_button_clicked",
expect.objectContaining(COMMON),
);
});
});
describe("trackBackendAdded", () => {
it("captures backend_added with coarse, non-sensitive backend metadata", () => {
getTracking().trackBackendAdded({
backendKind: "cloud",
connectionMethod: "cloud_login",
hasApiKey: true,
source: "add_backend_modal",
agentServerVersion: "1.36.1",
});
expect(captureMock).toHaveBeenCalledWith("backend_added", {
...COMMON,
backend_kind: "cloud",
connection_method: "cloud_login",
has_api_key: true,
source: "add_backend_modal",
agent_server_version: "1.36.1",
automation_sdk_version: "unknown",
backend_version: "1.36.1",
});
});
});
describe("trackOnboardingLinkClicked", () => {
it("captures onboarding_link_clicked with the typed link contract and commonProperties", () => {
getTracking().trackOnboardingLinkClicked({
linkId: "join_slack",
destinationType: "community",
surface: "landing_checklist",
checklistItem: "join_slack",
isExternal: true,
});
expect(captureMock).toHaveBeenCalledWith("onboarding_link_clicked", {
link_id: "join_slack",
destination_type: "community",
surface: "landing_checklist",
checklist_item: "join_slack",
step_id: undefined,
is_external: true,
...COMMON,
});
});
});
describe("shared telemetry boundary", () => {
it("delegates consent enforcement when backend settings report false", () => {
useSettingsMock.mockReturnValue({
data: { email: "user@example.com", user_consents_to_analytics: false },
});
getTracking().trackPushButtonClick();
expect(captureMock).toHaveBeenCalledWith(
"push_button_clicked",
expect.objectContaining(COMMON),
);
});
it("delegates consent enforcement when backend settings report null", () => {
useSettingsMock.mockReturnValue({
data: { email: "user@example.com", user_consents_to_analytics: null },
});
getTracking().trackPushButtonClick();
expect(captureMock).toHaveBeenCalledWith(
"push_button_clicked",
expect.objectContaining(COMMON),
);
});
it("does not drop backend milestones while settings are loading", () => {
useSettingsMock.mockReturnValue({ data: undefined });
getTracking().trackBackendAdded({
backendKind: "cloud",
connectionMethod: "cloud_login",
hasApiKey: true,
source: "onboarding",
});
expect(captureMock).toHaveBeenCalledWith(
"backend_added",
expect.objectContaining({
backend_kind: "cloud",
connection_method: "cloud_login",
source: "onboarding",
}),
);
});
});
});
|