| 1 | import { createStore } from "@reduxjs/toolkit"; |
| 2 | import * as Sentry from "@sentry/react"; |
| 3 | import { render, screen } from "@testing-library/react"; |
| 4 | import posthog from "posthog-js"; |
| 5 | import React from "react"; |
| 6 | import { Provider } from "react-redux"; |
| 7 | import { PersistGate } from "redux-persist/integration/react"; |
| 8 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 9 | import { AuthProvider } from "../context/Auth"; |
| 10 | import TelemetryProviders from "./TelemetryProviders"; |
| 11 | |
| 12 | // Mock Sentry |
| 13 | vi.mock("@sentry/react", () => ({ |
| 14 | init: vi.fn(), |
| 15 | setUser: vi.fn(), |
| 16 | getClient: vi.fn(), |
| 17 | getCurrentScope: vi.fn(() => ({ |
| 18 | clear: vi.fn(), |
| 19 | })), |
| 20 | browserTracingIntegration: vi.fn(() => ({ name: "BrowserTracing" })), |
| 21 | replayIntegration: vi.fn(() => ({ name: "Replay" })), |
| 22 | feedbackIntegration: vi.fn(() => ({ name: "Feedback" })), |
| 23 | contextLinesIntegration: vi.fn(() => ({ name: "ContextLines" })), |
| 24 | ErrorBoundary: ({ children }: { children: React.ReactNode }) => ( |
| 25 | <div data-testid="sentry-error-boundary">{children}</div> |
| 26 | ), |
| 27 | })); |
| 28 | |
| 29 | // Mock PostHog |
| 30 | vi.mock("posthog-js", () => ({ |
| 31 | default: { |
| 32 | init: vi.fn(), |
| 33 | identify: vi.fn(), |
| 34 | opt_in_capturing: vi.fn(), |
| 35 | opt_out_capturing: vi.fn(), |
| 36 | }, |
| 37 | })); |
| 38 | |
| 39 | // Mock PostHog React provider |
| 40 | vi.mock("posthog-js/react", () => ({ |
| 41 | PostHogProvider: ({ children }: { children: React.ReactNode }) => ( |
| 42 | <div data-testid="posthog-provider">{children}</div> |
| 43 | ), |
| 44 | })); |
| 45 | |
| 46 | // Mock the Auth context |
| 47 | vi.mock("../context/Auth", () => ({ |
| 48 | AuthProvider: ({ children }: { children: React.ReactNode }) => ( |
| 49 | <div data-testid="auth-provider">{children}</div> |
| 50 | ), |
| 51 | useAuth: () => ({ |
| 52 | session: { |
| 53 | account: { |
| 54 | id: "test@continue.dev", // Mock Continue team member email |
| 55 | }, |
| 56 | }, |
| 57 | }), |
| 58 | })); |
| 59 | |
| 60 | // Mock isContinueTeamMember utility |
| 61 | vi.mock("../util/isContinueTeamMember", () => ({ |
| 62 | isContinueTeamMember: vi.fn(() => true), // Mock as Continue team member |
| 63 | })); |
| 64 | |
| 65 | // Mock window.vscMachineId |
| 66 | Object.defineProperty(window, "vscMachineId", { |
| 67 | value: "test-machine-id", |
| 68 | writable: true, |
| 69 | }); |
| 70 | |
| 71 | // Create a minimal Redux store for testing |
| 72 | const createTestStore = (allowAnonymousTelemetry: boolean) => { |
| 73 | return createStore( |
| 74 | ( |
| 75 | state = { |
| 76 | config: { |
| 77 | config: { |
| 78 | allowAnonymousTelemetry, |
| 79 | }, |
| 80 | }, |
| 81 | }, |
| 82 | ) => state, |
| 83 | ); |
| 84 | }; |
| 85 | |
| 86 | // Mock persistor |
| 87 | const mockPersistor = { |
| 88 | persist: vi.fn(), |
| 89 | purge: vi.fn(), |
| 90 | flush: vi.fn(), |
| 91 | pause: vi.fn(), |
| 92 | getState: vi.fn(() => ({ |
| 93 | bootstrapped: true, |
| 94 | registry: [], |
| 95 | })), |
| 96 | dispatch: vi.fn(), |
| 97 | subscribe: vi.fn(), |
| 98 | }; |
| 99 | |
| 100 | const TestWrapper: React.FC<{ |
| 101 | children: React.ReactNode; |
| 102 | allowAnonymousTelemetry: boolean; |
| 103 | }> = ({ children, allowAnonymousTelemetry }) => { |
| 104 | const store = createTestStore(allowAnonymousTelemetry); |
| 105 | |
| 106 | return ( |
| 107 | <Provider store={store}> |
| 108 | <PersistGate loading={null} persistor={mockPersistor}> |
| 109 | <AuthProvider>{children}</AuthProvider> |
| 110 | </PersistGate> |
| 111 | </Provider> |
| 112 | ); |
| 113 | }; |
| 114 | |
| 115 | describe("TelemetryProviders", () => { |
| 116 | const mockSentry = Sentry as any; |
| 117 | const mockPosthog = posthog as any; |
| 118 | |
| 119 | beforeEach(() => { |
| 120 | vi.clearAllMocks(); |
| 121 | // Reset NODE_ENV to test |
| 122 | process.env.NODE_ENV = "test"; |
| 123 | }); |
| 124 | |
| 125 | afterEach(() => { |
| 126 | vi.clearAllMocks(); |
| 127 | }); |
| 128 | |
| 129 | describe("when telemetry is enabled", () => { |
| 130 | it("should initialize both PostHog and Sentry", () => { |
| 131 | const originalNodeEnv = process.env.NODE_ENV; |
| 132 | process.env.NODE_ENV = "development"; |
| 133 | |
| 134 | render( |
| 135 | <TestWrapper allowAnonymousTelemetry={true}> |
| 136 | <TelemetryProviders> |
| 137 | <div data-testid="test-child">Test Child</div> |
| 138 | </TelemetryProviders> |
| 139 | </TestWrapper>, |
| 140 | ); |
| 141 | |
| 142 | // Verify PostHog initialization |
| 143 | expect(mockPosthog.init).toHaveBeenCalledWith( |
| 144 | "phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", |
| 145 | expect.objectContaining({ |
| 146 | api_host: "https://app.posthog.com", |
| 147 | disable_session_recording: true, |
| 148 | autocapture: false, |
| 149 | capture_pageleave: false, |
| 150 | capture_pageview: false, |
| 151 | }), |
| 152 | ); |
| 153 | expect(mockPosthog.identify).toHaveBeenCalledWith("test-machine-id"); |
| 154 | expect(mockPosthog.opt_in_capturing).toHaveBeenCalled(); |
| 155 | |
| 156 | // Verify Sentry initialization |
| 157 | expect(mockSentry.init).toHaveBeenCalledWith( |
| 158 | expect.objectContaining({ |
| 159 | dsn: "https://fe99934dcdc537d84209893a3f96a196@o4505462064283648.ingest.us.sentry.io/4508184596054016", |
| 160 | environment: "development", |
| 161 | tracesSampleRate: 0.25, |
| 162 | }), |
| 163 | ); |
| 164 | expect(mockSentry.setUser).toHaveBeenCalledWith({ |
| 165 | id: expect.stringMatching(/^[a-f0-9]{8}$/), // Should be 8-char hash |
| 166 | email: undefined, |
| 167 | username: undefined, |
| 168 | ip_address: undefined, |
| 169 | }); |
| 170 | |
| 171 | // Verify children are rendered |
| 172 | expect(screen.getByTestId("test-child")).toBeInTheDocument(); |
| 173 | expect(screen.getByTestId("posthog-provider")).toBeInTheDocument(); |
| 174 | |
| 175 | process.env.NODE_ENV = originalNodeEnv; |
| 176 | }); |
| 177 | }); |
| 178 | |
| 179 | describe("when telemetry is disabled", () => { |
| 180 | it("should disable PostHog and clear Sentry scope", () => { |
| 181 | const mockClear = vi.fn(); |
| 182 | const mockClose = vi.fn(); |
| 183 | const mockClient = { close: mockClose }; |
| 184 | |
| 185 | mockSentry.getCurrentScope.mockReturnValue({ clear: mockClear }); |
| 186 | mockSentry.getClient.mockReturnValue(mockClient); |
| 187 | |
| 188 | render( |
| 189 | <TestWrapper allowAnonymousTelemetry={false}> |
| 190 | <TelemetryProviders> |
| 191 | <div data-testid="test-child">Test Child</div> |
| 192 | </TelemetryProviders> |
| 193 | </TestWrapper>, |
| 194 | ); |
| 195 | |
| 196 | // Verify PostHog is disabled |
| 197 | expect(mockPosthog.opt_out_capturing).toHaveBeenCalled(); |
| 198 | expect(mockPosthog.init).not.toHaveBeenCalled(); |
| 199 | expect(mockPosthog.identify).not.toHaveBeenCalled(); |
| 200 | expect(mockPosthog.opt_in_capturing).not.toHaveBeenCalled(); |
| 201 | |
| 202 | // Verify Sentry is properly disabled |
| 203 | expect(mockSentry.getClient).toHaveBeenCalled(); |
| 204 | expect(mockClose).toHaveBeenCalled(); |
| 205 | expect(mockSentry.getCurrentScope).toHaveBeenCalled(); |
| 206 | expect(mockClear).toHaveBeenCalled(); |
| 207 | expect(mockSentry.init).not.toHaveBeenCalled(); |
| 208 | expect(mockSentry.setUser).not.toHaveBeenCalled(); |
| 209 | |
| 210 | // Verify children are still rendered |
| 211 | expect(screen.getByTestId("test-child")).toBeInTheDocument(); |
| 212 | expect(screen.getByTestId("posthog-provider")).toBeInTheDocument(); |
| 213 | }); |
| 214 | }); |
| 215 | |
| 216 | describe("provider structure", () => { |
| 217 | it("should wrap children with PostHogProvider", () => { |
| 218 | render( |
| 219 | <TestWrapper allowAnonymousTelemetry={false}> |
| 220 | <TelemetryProviders> |
| 221 | <div data-testid="nested-child"> |
| 222 | <span data-testid="deeply-nested">Deep content</span> |
| 223 | </div> |
| 224 | </TelemetryProviders> |
| 225 | </TestWrapper>, |
| 226 | ); |
| 227 | |
| 228 | // Verify the provider structure is maintained |
| 229 | expect(screen.getByTestId("posthog-provider")).toBeInTheDocument(); |
| 230 | expect(screen.getByTestId("nested-child")).toBeInTheDocument(); |
| 231 | expect(screen.getByTestId("deeply-nested")).toBeInTheDocument(); |
| 232 | }); |
| 233 | |
| 234 | it("should handle missing vscMachineId gracefully", () => { |
| 235 | // Temporarily modify vscMachineId |
| 236 | const originalMachineId = window.vscMachineId; |
| 237 | (window as any).vscMachineId = undefined; |
| 238 | |
| 239 | const originalNodeEnv = process.env.NODE_ENV; |
| 240 | process.env.NODE_ENV = "development"; |
| 241 | |
| 242 | render( |
| 243 | <TestWrapper allowAnonymousTelemetry={true}> |
| 244 | <TelemetryProviders> |
| 245 | <div data-testid="test-child">Test Child</div> |
| 246 | </TelemetryProviders> |
| 247 | </TestWrapper>, |
| 248 | ); |
| 249 | |
| 250 | // Should still initialize but with anonymized ID (hashed "anonymous") |
| 251 | expect(mockSentry.setUser).toHaveBeenCalledWith({ |
| 252 | id: expect.stringMatching(/^[a-f0-9]{8}$/), // Should be 8-char hash |
| 253 | email: undefined, |
| 254 | username: undefined, |
| 255 | ip_address: undefined, |
| 256 | }); |
| 257 | |
| 258 | // Restore |
| 259 | (window as any).vscMachineId = originalMachineId; |
| 260 | process.env.NODE_ENV = originalNodeEnv; |
| 261 | }); |
| 262 | }); |
| 263 | |
| 264 | describe("telemetry toggle behavior", () => { |
| 265 | it("should maintain proper state when telemetry setting changes", () => { |
| 266 | const originalNodeEnv = process.env.NODE_ENV; |
| 267 | process.env.NODE_ENV = "development"; |
| 268 | |
| 269 | // First render with telemetry enabled |
| 270 | const { rerender } = render( |
| 271 | <TestWrapper allowAnonymousTelemetry={true}> |
| 272 | <TelemetryProviders> |
| 273 | <div data-testid="test-child">Test Child</div> |
| 274 | </TelemetryProviders> |
| 275 | </TestWrapper>, |
| 276 | ); |
| 277 | |
| 278 | expect(mockSentry.init).toHaveBeenCalled(); |
| 279 | expect(mockPosthog.opt_in_capturing).toHaveBeenCalled(); |
| 280 | |
| 281 | // Clear mocks and rerender with telemetry disabled |
| 282 | vi.clearAllMocks(); |
| 283 | const mockClear = vi.fn(); |
| 284 | const mockClose = vi.fn(); |
| 285 | const mockClient = { close: mockClose }; |
| 286 | |
| 287 | mockSentry.getCurrentScope.mockReturnValue({ clear: mockClear }); |
| 288 | mockSentry.getClient.mockReturnValue(mockClient); |
| 289 | |
| 290 | rerender( |
| 291 | <TestWrapper allowAnonymousTelemetry={false}> |
| 292 | <TelemetryProviders> |
| 293 | <div data-testid="test-child">Test Child</div> |
| 294 | </TelemetryProviders> |
| 295 | </TestWrapper>, |
| 296 | ); |
| 297 | |
| 298 | expect(mockPosthog.opt_out_capturing).toHaveBeenCalled(); |
| 299 | expect(mockClose).toHaveBeenCalled(); |
| 300 | expect(mockClear).toHaveBeenCalled(); |
| 301 | expect(mockSentry.init).not.toHaveBeenCalled(); |
| 302 | |
| 303 | process.env.NODE_ENV = originalNodeEnv; |
| 304 | }); |
| 305 | }); |
| 306 | }); |