|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { TelemetryEvents } from '../../types' |
| 4 | + |
| 5 | +const hoisted = vi.hoisted(() => { |
| 6 | + const mockCapture = vi.fn() |
| 7 | + const mockInit = vi.fn() |
| 8 | + const mockIdentify = vi.fn() |
| 9 | + const mockPeopleSet = vi.fn() |
| 10 | + const mockOnUserResolved = vi.fn() |
| 11 | + |
| 12 | + return { |
| 13 | + mockCapture, |
| 14 | + mockInit, |
| 15 | + mockIdentify, |
| 16 | + mockPeopleSet, |
| 17 | + mockOnUserResolved, |
| 18 | + mockPosthog: { |
| 19 | + default: { |
| 20 | + init: mockInit, |
| 21 | + capture: mockCapture, |
| 22 | + identify: mockIdentify, |
| 23 | + people: { set: mockPeopleSet } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +vi.mock('vue', async () => { |
| 30 | + const actual = await vi.importActual('vue') |
| 31 | + return { |
| 32 | + ...actual, |
| 33 | + watch: vi.fn() |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +vi.mock('@/composables/auth/useCurrentUser', () => ({ |
| 38 | + useCurrentUser: () => ({ |
| 39 | + onUserResolved: hoisted.mockOnUserResolved |
| 40 | + }) |
| 41 | +})) |
| 42 | + |
| 43 | +vi.mock('@/platform/remoteConfig/remoteConfig', () => ({ |
| 44 | + remoteConfig: { value: null } |
| 45 | +})) |
| 46 | + |
| 47 | +vi.mock('posthog-js', () => hoisted.mockPosthog) |
| 48 | + |
| 49 | +import { PostHogTelemetryProvider } from './PostHogTelemetryProvider' |
| 50 | + |
| 51 | +function createProvider( |
| 52 | + config: Partial<typeof window.__CONFIG__> = {} |
| 53 | +): PostHogTelemetryProvider { |
| 54 | + const original = window.__CONFIG__ |
| 55 | + window.__CONFIG__ = { ...original, ...config } |
| 56 | + const provider = new PostHogTelemetryProvider() |
| 57 | + window.__CONFIG__ = original |
| 58 | + return provider |
| 59 | +} |
| 60 | + |
| 61 | +describe('PostHogTelemetryProvider', () => { |
| 62 | + beforeEach(() => { |
| 63 | + vi.clearAllMocks() |
| 64 | + window.__CONFIG__ = { |
| 65 | + posthog_project_token: 'phc_test_token' |
| 66 | + } as typeof window.__CONFIG__ |
| 67 | + }) |
| 68 | + |
| 69 | + describe('initialization', () => { |
| 70 | + it('disables itself when posthog_project_token is not provided', async () => { |
| 71 | + const provider = createProvider({ posthog_project_token: undefined }) |
| 72 | + await vi.dynamicImportSettled() |
| 73 | + |
| 74 | + provider.trackSignupOpened() |
| 75 | + |
| 76 | + expect(hoisted.mockCapture).not.toHaveBeenCalled() |
| 77 | + }) |
| 78 | + |
| 79 | + it('calls posthog.init with the token and default api_host', async () => { |
| 80 | + createProvider() |
| 81 | + await vi.dynamicImportSettled() |
| 82 | + |
| 83 | + expect(hoisted.mockInit).toHaveBeenCalledWith('phc_test_token', { |
| 84 | + api_host: 'https://ph.comfy.org', |
| 85 | + autocapture: false, |
| 86 | + capture_pageview: false, |
| 87 | + capture_pageleave: false, |
| 88 | + persistence: 'localStorage+cookie' |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + it('uses custom api_host from config when provided', async () => { |
| 93 | + window.__CONFIG__ = { |
| 94 | + posthog_project_token: 'phc_test_token', |
| 95 | + posthog_api_host: 'https://custom.host.com' |
| 96 | + } as typeof window.__CONFIG__ |
| 97 | + new PostHogTelemetryProvider() |
| 98 | + await vi.dynamicImportSettled() |
| 99 | + |
| 100 | + expect(hoisted.mockInit).toHaveBeenCalledWith( |
| 101 | + 'phc_test_token', |
| 102 | + expect.objectContaining({ api_host: 'https://custom.host.com' }) |
| 103 | + ) |
| 104 | + }) |
| 105 | + |
| 106 | + it('registers onUserResolved callback after init', async () => { |
| 107 | + createProvider() |
| 108 | + await vi.dynamicImportSettled() |
| 109 | + |
| 110 | + expect(hoisted.mockOnUserResolved).toHaveBeenCalledOnce() |
| 111 | + }) |
| 112 | + |
| 113 | + it('identifies user when onUserResolved fires', async () => { |
| 114 | + createProvider() |
| 115 | + await vi.dynamicImportSettled() |
| 116 | + |
| 117 | + const callback = hoisted.mockOnUserResolved.mock.calls[0][0] |
| 118 | + callback({ id: 'user-123' }) |
| 119 | + |
| 120 | + expect(hoisted.mockIdentify).toHaveBeenCalledWith('user-123') |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + describe('event tracking', () => { |
| 125 | + it('captures events after initialization', async () => { |
| 126 | + const provider = createProvider() |
| 127 | + await vi.dynamicImportSettled() |
| 128 | + |
| 129 | + provider.trackSignupOpened() |
| 130 | + |
| 131 | + expect(hoisted.mockCapture).toHaveBeenCalledWith( |
| 132 | + TelemetryEvents.USER_SIGN_UP_OPENED, |
| 133 | + {} |
| 134 | + ) |
| 135 | + }) |
| 136 | + |
| 137 | + it('captures events with metadata', async () => { |
| 138 | + const provider = createProvider() |
| 139 | + await vi.dynamicImportSettled() |
| 140 | + |
| 141 | + provider.trackAuth({ method: 'google' }) |
| 142 | + |
| 143 | + expect(hoisted.mockCapture).toHaveBeenCalledWith( |
| 144 | + TelemetryEvents.USER_AUTH_COMPLETED, |
| 145 | + { method: 'google' } |
| 146 | + ) |
| 147 | + }) |
| 148 | + |
| 149 | + it('queues events before initialization and flushes after', async () => { |
| 150 | + const provider = createProvider() |
| 151 | + |
| 152 | + provider.trackUserLoggedIn() |
| 153 | + expect(hoisted.mockCapture).not.toHaveBeenCalled() |
| 154 | + |
| 155 | + await vi.dynamicImportSettled() |
| 156 | + |
| 157 | + expect(hoisted.mockCapture).toHaveBeenCalledWith( |
| 158 | + TelemetryEvents.USER_LOGGED_IN, |
| 159 | + {} |
| 160 | + ) |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + describe('disabled events', () => { |
| 165 | + it('does not capture default disabled events', async () => { |
| 166 | + const provider = createProvider() |
| 167 | + await vi.dynamicImportSettled() |
| 168 | + |
| 169 | + provider.trackWorkflowOpened({ |
| 170 | + missing_node_count: 0, |
| 171 | + missing_node_types: [] |
| 172 | + }) |
| 173 | + |
| 174 | + expect(hoisted.mockCapture).not.toHaveBeenCalled() |
| 175 | + }) |
| 176 | + |
| 177 | + it('captures events not in the disabled list', async () => { |
| 178 | + const provider = createProvider() |
| 179 | + await vi.dynamicImportSettled() |
| 180 | + |
| 181 | + provider.trackMonthlySubscriptionSucceeded() |
| 182 | + |
| 183 | + expect(hoisted.mockCapture).toHaveBeenCalledWith( |
| 184 | + TelemetryEvents.MONTHLY_SUBSCRIPTION_SUCCEEDED, |
| 185 | + {} |
| 186 | + ) |
| 187 | + }) |
| 188 | + }) |
| 189 | + |
| 190 | + describe('survey tracking', () => { |
| 191 | + it('sets user properties on survey submission', async () => { |
| 192 | + const provider = createProvider() |
| 193 | + await vi.dynamicImportSettled() |
| 194 | + |
| 195 | + const responses = { familiarity: 'beginner', industry: 'tech' } |
| 196 | + provider.trackSurvey('submitted', responses) |
| 197 | + |
| 198 | + expect(hoisted.mockCapture).toHaveBeenCalledWith( |
| 199 | + TelemetryEvents.USER_SURVEY_SUBMITTED, |
| 200 | + expect.objectContaining({ familiarity: 'beginner' }) |
| 201 | + ) |
| 202 | + expect(hoisted.mockPeopleSet).toHaveBeenCalled() |
| 203 | + }) |
| 204 | + |
| 205 | + it('does not set user properties on survey opened', async () => { |
| 206 | + const provider = createProvider() |
| 207 | + await vi.dynamicImportSettled() |
| 208 | + |
| 209 | + provider.trackSurvey('opened') |
| 210 | + |
| 211 | + expect(hoisted.mockCapture).toHaveBeenCalledWith( |
| 212 | + TelemetryEvents.USER_SURVEY_OPENED, |
| 213 | + {} |
| 214 | + ) |
| 215 | + expect(hoisted.mockPeopleSet).not.toHaveBeenCalled() |
| 216 | + }) |
| 217 | + }) |
| 218 | + |
| 219 | + describe('page view', () => { |
| 220 | + it('captures page view with page_name property', async () => { |
| 221 | + const provider = createProvider() |
| 222 | + await vi.dynamicImportSettled() |
| 223 | + |
| 224 | + provider.trackPageView('workflow_editor') |
| 225 | + |
| 226 | + expect(hoisted.mockCapture).toHaveBeenCalledWith( |
| 227 | + TelemetryEvents.PAGE_VIEW, |
| 228 | + { page_name: 'workflow_editor' } |
| 229 | + ) |
| 230 | + }) |
| 231 | + }) |
| 232 | +}) |
0 commit comments