|
| 1 | +import { store } from ':store/store.js'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { loadTelemetryScript } from './initCCA.js'; |
| 4 | + |
| 5 | +vi.mock('./telemetry-content.js', () => ({ |
| 6 | + TELEMETRY_SCRIPT_CONTENT: 'mock-telemetry-script-content', |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock(':store/store.js', () => ({ |
| 10 | + store: { |
| 11 | + config: { |
| 12 | + get: vi.fn(), |
| 13 | + set: vi.fn(), |
| 14 | + }, |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +const mockStore = store as any; |
| 19 | + |
| 20 | +describe('initCCA', () => { |
| 21 | + let mockClientAnalytics: any; |
| 22 | + let originalDocument: Document; |
| 23 | + let originalWindow: Window & typeof globalThis; |
| 24 | + let mockCrypto: any; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + mockClientAnalytics = { |
| 28 | + init: vi.fn(), |
| 29 | + identify: vi.fn(), |
| 30 | + PlatformName: { |
| 31 | + web: 'web', |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + mockCrypto = { |
| 36 | + randomUUID: vi.fn().mockReturnValue('mock-uuid-123'), |
| 37 | + }; |
| 38 | + |
| 39 | + originalDocument = global.document; |
| 40 | + originalWindow = global.window; |
| 41 | + |
| 42 | + const mockScript = { |
| 43 | + textContent: '', |
| 44 | + type: '', |
| 45 | + }; |
| 46 | + |
| 47 | + const mockHead = { |
| 48 | + appendChild: vi.fn(), |
| 49 | + removeChild: vi.fn(), |
| 50 | + }; |
| 51 | + |
| 52 | + global.document = { |
| 53 | + createElement: vi.fn().mockReturnValue(mockScript), |
| 54 | + head: mockHead, |
| 55 | + } as any; |
| 56 | + |
| 57 | + global.window = { |
| 58 | + crypto: mockCrypto, |
| 59 | + ClientAnalytics: undefined, |
| 60 | + } as any; |
| 61 | + |
| 62 | + mockStore.config.get.mockReturnValue({}); |
| 63 | + mockStore.config.set.mockImplementation(() => {}); |
| 64 | + |
| 65 | + vi.clearAllMocks(); |
| 66 | + }); |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + global.document = originalDocument; |
| 70 | + global.window = originalWindow; |
| 71 | + delete (global.window as any).ClientAnalytics; |
| 72 | + }); |
| 73 | + |
| 74 | + describe('loadTelemetryScript', () => { |
| 75 | + it('should create and execute telemetry script when ClientAnalytics does not exist', async () => { |
| 76 | + const mockScript = { |
| 77 | + textContent: '', |
| 78 | + type: '', |
| 79 | + }; |
| 80 | + |
| 81 | + (global.document.createElement as any).mockReturnValue(mockScript); |
| 82 | + |
| 83 | + const mockAppendChild = vi.fn().mockImplementation(() => { |
| 84 | + (global.window as any).ClientAnalytics = mockClientAnalytics; |
| 85 | + }); |
| 86 | + |
| 87 | + global.document.head.appendChild = mockAppendChild; |
| 88 | + |
| 89 | + const result = await loadTelemetryScript(); |
| 90 | + |
| 91 | + expect(result).toBeUndefined(); |
| 92 | + expect(global.document.createElement).toHaveBeenCalledWith('script'); |
| 93 | + expect(mockScript.textContent).toBe('mock-telemetry-script-content'); |
| 94 | + expect(mockScript.type).toBe('text/javascript'); |
| 95 | + expect(mockAppendChild).toHaveBeenCalledWith(mockScript); |
| 96 | + expect(global.document.head.removeChild).toHaveBeenCalledWith(mockScript); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should initialize ClientAnalytics with correct parameters after loading script', async () => { |
| 100 | + const mockScript = { |
| 101 | + textContent: '', |
| 102 | + type: '', |
| 103 | + }; |
| 104 | + |
| 105 | + (global.document.createElement as any).mockReturnValue(mockScript); |
| 106 | + |
| 107 | + const mockAppendChild = vi.fn().mockImplementation(() => { |
| 108 | + (global.window as any).ClientAnalytics = mockClientAnalytics; |
| 109 | + }); |
| 110 | + |
| 111 | + global.document.head.appendChild = mockAppendChild; |
| 112 | + |
| 113 | + await loadTelemetryScript(); |
| 114 | + |
| 115 | + expect(mockClientAnalytics.init).toHaveBeenCalledWith({ |
| 116 | + isProd: true, |
| 117 | + amplitudeApiKey: 'c66737ad47ec354ced777935b0af822e', |
| 118 | + platform: 'web', |
| 119 | + projectName: 'base_account_sdk', |
| 120 | + showDebugLogging: false, |
| 121 | + version: '1.0.0', |
| 122 | + apiEndpoint: 'https://cca-lite.coinbase.com', |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should use deviceId from store.config.get() when available', async () => { |
| 127 | + const mockScript = { |
| 128 | + textContent: '', |
| 129 | + type: '', |
| 130 | + }; |
| 131 | + |
| 132 | + (global.document.createElement as any).mockReturnValue(mockScript); |
| 133 | + mockStore.config.get.mockReturnValue({ deviceId: 'store-device-id-123' }); |
| 134 | + |
| 135 | + const mockAppendChild = vi.fn().mockImplementation(() => { |
| 136 | + (global.window as any).ClientAnalytics = mockClientAnalytics; |
| 137 | + }); |
| 138 | + |
| 139 | + global.document.head.appendChild = mockAppendChild; |
| 140 | + |
| 141 | + await loadTelemetryScript(); |
| 142 | + |
| 143 | + expect(mockClientAnalytics.identify).toHaveBeenCalledWith({ |
| 144 | + deviceId: 'store-device-id-123', |
| 145 | + }); |
| 146 | + expect(mockStore.config.set).toHaveBeenCalledWith({ |
| 147 | + deviceId: 'store-device-id-123', |
| 148 | + }); |
| 149 | + expect(mockCrypto.randomUUID).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should fall back to crypto.randomUUID when store deviceId is not available', async () => { |
| 153 | + const mockScript = { |
| 154 | + textContent: '', |
| 155 | + type: '', |
| 156 | + }; |
| 157 | + |
| 158 | + (global.document.createElement as any).mockReturnValue(mockScript); |
| 159 | + mockStore.config.get.mockReturnValue({}); |
| 160 | + |
| 161 | + const mockAppendChild = vi.fn().mockImplementation(() => { |
| 162 | + (global.window as any).ClientAnalytics = mockClientAnalytics; |
| 163 | + }); |
| 164 | + |
| 165 | + global.document.head.appendChild = mockAppendChild; |
| 166 | + |
| 167 | + await loadTelemetryScript(); |
| 168 | + |
| 169 | + expect(mockClientAnalytics.identify).toHaveBeenCalledWith({ |
| 170 | + deviceId: 'mock-uuid-123', |
| 171 | + }); |
| 172 | + expect(mockStore.config.set).toHaveBeenCalledWith({ |
| 173 | + deviceId: 'mock-uuid-123', |
| 174 | + }); |
| 175 | + expect(mockCrypto.randomUUID).toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should handle case when both store deviceId and crypto.randomUUID are not available', async () => { |
| 179 | + const mockScript = { |
| 180 | + textContent: '', |
| 181 | + type: '', |
| 182 | + }; |
| 183 | + |
| 184 | + (global.document.createElement as any).mockReturnValue(mockScript); |
| 185 | + mockStore.config.get.mockReturnValue({}); |
| 186 | + (global.window as any).crypto = undefined; |
| 187 | + |
| 188 | + const mockAppendChild = vi.fn().mockImplementation(() => { |
| 189 | + (global.window as any).ClientAnalytics = mockClientAnalytics; |
| 190 | + }); |
| 191 | + |
| 192 | + global.document.head.appendChild = mockAppendChild; |
| 193 | + |
| 194 | + await loadTelemetryScript(); |
| 195 | + |
| 196 | + expect(mockClientAnalytics.identify).toHaveBeenCalledWith({ |
| 197 | + deviceId: '', |
| 198 | + }); |
| 199 | + expect(mockStore.config.set).toHaveBeenCalledWith({ |
| 200 | + deviceId: '', |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should handle case when store deviceId is null', async () => { |
| 205 | + const mockScript = { |
| 206 | + textContent: '', |
| 207 | + type: '', |
| 208 | + }; |
| 209 | + |
| 210 | + (global.document.createElement as any).mockReturnValue(mockScript); |
| 211 | + mockStore.config.get.mockReturnValue({ deviceId: null }); |
| 212 | + |
| 213 | + const mockAppendChild = vi.fn().mockImplementation(() => { |
| 214 | + (global.window as any).ClientAnalytics = mockClientAnalytics; |
| 215 | + }); |
| 216 | + |
| 217 | + global.document.head.appendChild = mockAppendChild; |
| 218 | + |
| 219 | + await loadTelemetryScript(); |
| 220 | + |
| 221 | + expect(mockClientAnalytics.identify).toHaveBeenCalledWith({ |
| 222 | + deviceId: 'mock-uuid-123', |
| 223 | + }); |
| 224 | + expect(mockStore.config.set).toHaveBeenCalledWith({ |
| 225 | + deviceId: 'mock-uuid-123', |
| 226 | + }); |
| 227 | + expect(mockCrypto.randomUUID).toHaveBeenCalled(); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should reject promise when script execution fails', async () => { |
| 231 | + const mockScript = { |
| 232 | + textContent: '', |
| 233 | + type: '', |
| 234 | + }; |
| 235 | + |
| 236 | + (global.document.createElement as any).mockReturnValue(mockScript); |
| 237 | + |
| 238 | + const mockAppendChild = vi.fn().mockImplementation(() => { |
| 239 | + throw new Error('Script execution failed'); |
| 240 | + }); |
| 241 | + |
| 242 | + global.document.head.appendChild = mockAppendChild; |
| 243 | + |
| 244 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 245 | + |
| 246 | + await expect(loadTelemetryScript()).rejects.toBeUndefined(); |
| 247 | + |
| 248 | + expect(consoleSpy).toHaveBeenCalledWith('Failed to execute inlined telemetry script'); |
| 249 | + |
| 250 | + consoleSpy.mockRestore(); |
| 251 | + }); |
| 252 | + }); |
| 253 | +}); |
0 commit comments