-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Track when telemetry settings change #8339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/core/webview/__tests__/telemetrySettingsTracking.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { TelemetryService } from "@roo-code/telemetry" | ||
| import { TelemetryEventName, type TelemetrySetting } from "@roo-code/types" | ||
|
|
||
| describe("Telemetry Settings Tracking", () => { | ||
| let mockTelemetryService: { | ||
| captureTelemetrySettingsChanged: ReturnType<typeof vi.fn> | ||
| updateTelemetryState: ReturnType<typeof vi.fn> | ||
| hasInstance: ReturnType<typeof vi.fn> | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| // Reset mocks | ||
| vi.clearAllMocks() | ||
|
|
||
| // Create mock service | ||
| mockTelemetryService = { | ||
| captureTelemetrySettingsChanged: vi.fn(), | ||
| updateTelemetryState: vi.fn(), | ||
| hasInstance: vi.fn().mockReturnValue(true), | ||
| } | ||
|
|
||
| // Mock the TelemetryService | ||
| vi.spyOn(TelemetryService, "hasInstance").mockReturnValue(true) | ||
| vi.spyOn(TelemetryService, "instance", "get").mockReturnValue(mockTelemetryService as any) | ||
| }) | ||
|
|
||
| describe("when telemetry is turned OFF", () => { | ||
| it("should fire event BEFORE disabling telemetry", () => { | ||
| const previousSetting = "enabled" as TelemetrySetting | ||
| const newSetting = "disabled" as TelemetrySetting | ||
|
|
||
| // Simulate the logic from webviewMessageHandler | ||
| const isOptedIn = newSetting !== "disabled" | ||
| const wasPreviouslyOptedIn = previousSetting !== "disabled" | ||
|
|
||
| // If turning telemetry OFF, fire event BEFORE disabling | ||
| if (wasPreviouslyOptedIn && !isOptedIn && TelemetryService.hasInstance()) { | ||
| TelemetryService.instance.captureTelemetrySettingsChanged(previousSetting, newSetting) | ||
| } | ||
|
|
||
| // Update the telemetry state | ||
| TelemetryService.instance.updateTelemetryState(isOptedIn) | ||
|
|
||
| // Verify the event was captured before updateTelemetryState | ||
| expect(mockTelemetryService.captureTelemetrySettingsChanged).toHaveBeenCalledWith("enabled", "disabled") | ||
| expect(mockTelemetryService.captureTelemetrySettingsChanged).toHaveBeenCalledBefore( | ||
| mockTelemetryService.updateTelemetryState as any, | ||
| ) | ||
| expect(mockTelemetryService.updateTelemetryState).toHaveBeenCalledWith(false) | ||
| }) | ||
|
|
||
| it("should fire event when going from unset to disabled", () => { | ||
| const previousSetting = "unset" as TelemetrySetting | ||
| const newSetting = "disabled" as TelemetrySetting | ||
|
|
||
| const isOptedIn = newSetting !== "disabled" | ||
| const wasPreviouslyOptedIn = previousSetting !== "disabled" | ||
|
|
||
| if (wasPreviouslyOptedIn && !isOptedIn && TelemetryService.hasInstance()) { | ||
| TelemetryService.instance.captureTelemetrySettingsChanged(previousSetting, newSetting) | ||
| } | ||
|
|
||
| TelemetryService.instance.updateTelemetryState(isOptedIn) | ||
|
|
||
| expect(mockTelemetryService.captureTelemetrySettingsChanged).toHaveBeenCalledWith("unset", "disabled") | ||
| }) | ||
| }) | ||
|
|
||
| describe("when telemetry is turned ON", () => { | ||
| it("should fire event AFTER enabling telemetry", () => { | ||
| const previousSetting = "disabled" as TelemetrySetting | ||
| const newSetting = "enabled" as TelemetrySetting | ||
|
|
||
| const isOptedIn = newSetting !== "disabled" | ||
| const wasPreviouslyOptedIn = previousSetting !== "disabled" | ||
|
|
||
| // Update the telemetry state first | ||
| TelemetryService.instance.updateTelemetryState(isOptedIn) | ||
|
|
||
| // If turning telemetry ON, fire event AFTER enabling | ||
| if (!wasPreviouslyOptedIn && isOptedIn && TelemetryService.hasInstance()) { | ||
| TelemetryService.instance.captureTelemetrySettingsChanged(previousSetting, newSetting) | ||
| } | ||
|
|
||
| // Verify the event was captured after updateTelemetryState | ||
| expect(mockTelemetryService.updateTelemetryState).toHaveBeenCalledWith(true) | ||
| expect(mockTelemetryService.captureTelemetrySettingsChanged).toHaveBeenCalledWith("disabled", "enabled") | ||
| expect(mockTelemetryService.updateTelemetryState).toHaveBeenCalledBefore( | ||
mrubens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mockTelemetryService.captureTelemetrySettingsChanged as any, | ||
| ) | ||
| }) | ||
|
|
||
| it("should not fire event when going from enabled to enabled", () => { | ||
| const previousSetting = "enabled" as TelemetrySetting | ||
| const newSetting = "enabled" as TelemetrySetting | ||
|
|
||
| const isOptedIn = newSetting !== "disabled" | ||
| const wasPreviouslyOptedIn = previousSetting !== "disabled" | ||
|
|
||
| // Neither condition should be met | ||
| if (wasPreviouslyOptedIn && !isOptedIn && TelemetryService.hasInstance()) { | ||
| TelemetryService.instance.captureTelemetrySettingsChanged(previousSetting, newSetting) | ||
| } | ||
|
|
||
| TelemetryService.instance.updateTelemetryState(isOptedIn) | ||
|
|
||
| if (!wasPreviouslyOptedIn && isOptedIn && TelemetryService.hasInstance()) { | ||
| TelemetryService.instance.captureTelemetrySettingsChanged(previousSetting, newSetting) | ||
| } | ||
|
|
||
| // Should not fire any telemetry events | ||
| expect(mockTelemetryService.captureTelemetrySettingsChanged).not.toHaveBeenCalled() | ||
| expect(mockTelemetryService.updateTelemetryState).toHaveBeenCalledWith(true) | ||
| }) | ||
|
|
||
| it("should fire event when going from unset to enabled (telemetry banner close)", () => { | ||
| const previousSetting = "unset" as TelemetrySetting | ||
| const newSetting = "enabled" as TelemetrySetting | ||
|
|
||
| const isOptedIn = newSetting !== "disabled" | ||
| const wasPreviouslyOptedIn = previousSetting !== "disabled" | ||
|
|
||
| // For unset -> enabled, both are opted in, so no event should fire | ||
| if (wasPreviouslyOptedIn && !isOptedIn && TelemetryService.hasInstance()) { | ||
| TelemetryService.instance.captureTelemetrySettingsChanged(previousSetting, newSetting) | ||
| } | ||
|
|
||
| TelemetryService.instance.updateTelemetryState(isOptedIn) | ||
|
|
||
| if (!wasPreviouslyOptedIn && isOptedIn && TelemetryService.hasInstance()) { | ||
| TelemetryService.instance.captureTelemetrySettingsChanged(previousSetting, newSetting) | ||
| } | ||
|
|
||
| // unset is treated as opted-in, so no event should fire | ||
| expect(mockTelemetryService.captureTelemetrySettingsChanged).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe("TelemetryService.captureTelemetrySettingsChanged", () => { | ||
| it("should call captureEvent with correct parameters", () => { | ||
| // Create a real instance to test the method | ||
| const mockCaptureEvent = vi.fn() | ||
| const service = new (TelemetryService as any)([]) | ||
| service.captureEvent = mockCaptureEvent | ||
|
|
||
| service.captureTelemetrySettingsChanged("enabled", "disabled") | ||
|
|
||
| expect(mockCaptureEvent).toHaveBeenCalledWith(TelemetryEventName.TELEMETRY_SETTINGS_CHANGED, { | ||
| previousSetting: "enabled", | ||
| newSetting: "disabled", | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.