|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { SDK } from '@lytics/sdk-kit'; |
| 3 | +import { debugPlugin } from './debug'; |
| 4 | + |
| 5 | +describe('Debug Plugin', () => { |
| 6 | + let sdk: SDK; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + sdk = new SDK({ debug: { enabled: true, console: true, window: true } }); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('Plugin Registration', () => { |
| 13 | + it('should register without errors', () => { |
| 14 | + expect(() => sdk.use(debugPlugin)).not.toThrow(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should expose debug API', () => { |
| 18 | + sdk.use(debugPlugin); |
| 19 | + |
| 20 | + expect(sdk.debug).toBeDefined(); |
| 21 | + expect(sdk.debug.log).toBeTypeOf('function'); |
| 22 | + expect(sdk.debug.isEnabled).toBeTypeOf('function'); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('Configuration', () => { |
| 27 | + it('should respect debug.enabled config', () => { |
| 28 | + const disabledSdk = new SDK({ debug: { enabled: false } }); |
| 29 | + disabledSdk.use(debugPlugin); |
| 30 | + |
| 31 | + expect(disabledSdk.debug.isEnabled()).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should default to disabled', () => { |
| 35 | + const defaultSdk = new SDK(); |
| 36 | + defaultSdk.use(debugPlugin); |
| 37 | + |
| 38 | + expect(defaultSdk.debug.isEnabled()).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should respect debug.console config', () => { |
| 42 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 43 | + |
| 44 | + const consoleEnabledSdk = new SDK({ debug: { enabled: true, console: true } }); |
| 45 | + consoleEnabledSdk.use(debugPlugin); |
| 46 | + consoleEnabledSdk.debug.log('test message'); |
| 47 | + |
| 48 | + expect(consoleSpy).toHaveBeenCalledWith('[experiences] test message', ''); |
| 49 | + consoleSpy.mockRestore(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should not log to console when disabled', () => { |
| 53 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 54 | + |
| 55 | + const consoleDisabledSdk = new SDK({ debug: { enabled: true, console: false } }); |
| 56 | + consoleDisabledSdk.use(debugPlugin); |
| 57 | + consoleDisabledSdk.debug.log('test message'); |
| 58 | + |
| 59 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 60 | + consoleSpy.mockRestore(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('Window Events', () => { |
| 65 | + it('should emit window events when enabled', () => { |
| 66 | + if (typeof window === 'undefined') { |
| 67 | + // Skip in non-browser environment |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const eventHandler = vi.fn(); |
| 72 | + window.addEventListener('experience-sdk:debug', eventHandler); |
| 73 | + |
| 74 | + sdk.use(debugPlugin); |
| 75 | + sdk.debug.log('test message', { foo: 'bar' }); |
| 76 | + |
| 77 | + expect(eventHandler).toHaveBeenCalled(); |
| 78 | + const event = eventHandler.mock.calls[0][0] as CustomEvent; |
| 79 | + expect(event.detail).toMatchObject({ |
| 80 | + message: 'test message', |
| 81 | + data: { foo: 'bar' }, |
| 82 | + }); |
| 83 | + expect(event.detail.timestamp).toBeDefined(); |
| 84 | + |
| 85 | + window.removeEventListener('experience-sdk:debug', eventHandler); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not emit window events when debug is disabled', () => { |
| 89 | + if (typeof window === 'undefined') { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const eventHandler = vi.fn(); |
| 94 | + window.addEventListener('experience-sdk:debug', eventHandler); |
| 95 | + |
| 96 | + const disabledSdk = new SDK({ debug: { enabled: false } }); |
| 97 | + disabledSdk.use(debugPlugin); |
| 98 | + disabledSdk.debug.log('test message'); |
| 99 | + |
| 100 | + expect(eventHandler).not.toHaveBeenCalled(); |
| 101 | + |
| 102 | + window.removeEventListener('experience-sdk:debug', eventHandler); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not emit window events when window is disabled', () => { |
| 106 | + if (typeof window === 'undefined') { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const eventHandler = vi.fn(); |
| 111 | + window.addEventListener('experience-sdk:debug', eventHandler); |
| 112 | + |
| 113 | + const windowDisabledSdk = new SDK({ debug: { enabled: true, window: false } }); |
| 114 | + windowDisabledSdk.use(debugPlugin); |
| 115 | + windowDisabledSdk.debug.log('test message'); |
| 116 | + |
| 117 | + expect(eventHandler).not.toHaveBeenCalled(); |
| 118 | + |
| 119 | + window.removeEventListener('experience-sdk:debug', eventHandler); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('Event Listening', () => { |
| 124 | + it('should listen to experiences:ready event', () => { |
| 125 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 126 | + |
| 127 | + sdk.use(debugPlugin); |
| 128 | + sdk.emit('experiences:ready'); |
| 129 | + |
| 130 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 131 | + '[experiences] SDK initialized and ready', |
| 132 | + '' |
| 133 | + ); |
| 134 | + |
| 135 | + consoleSpy.mockRestore(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should listen to experiences:registered event', () => { |
| 139 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 140 | + |
| 141 | + sdk.use(debugPlugin); |
| 142 | + const payload = { id: 'test', experience: { type: 'banner' } }; |
| 143 | + sdk.emit('experiences:registered', payload); |
| 144 | + |
| 145 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 146 | + '[experiences] Experience registered', |
| 147 | + payload |
| 148 | + ); |
| 149 | + |
| 150 | + consoleSpy.mockRestore(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should listen to experiences:evaluated event', () => { |
| 154 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 155 | + |
| 156 | + sdk.use(debugPlugin); |
| 157 | + const decision = { show: true, experienceId: 'test' }; |
| 158 | + sdk.emit('experiences:evaluated', decision); |
| 159 | + |
| 160 | + expect(consoleSpy).toHaveBeenCalledWith('[experiences] Experience evaluated', decision); |
| 161 | + |
| 162 | + consoleSpy.mockRestore(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should not log when debug is disabled', () => { |
| 166 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 167 | + |
| 168 | + const disabledSdk = new SDK({ debug: { enabled: false } }); |
| 169 | + disabledSdk.use(debugPlugin); |
| 170 | + disabledSdk.emit('experiences:ready'); |
| 171 | + |
| 172 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 173 | + |
| 174 | + consoleSpy.mockRestore(); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('debug.log() method', () => { |
| 179 | + it('should log message without data', () => { |
| 180 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 181 | + |
| 182 | + sdk.use(debugPlugin); |
| 183 | + sdk.debug.log('test message'); |
| 184 | + |
| 185 | + expect(consoleSpy).toHaveBeenCalledWith('[experiences] test message', ''); |
| 186 | + |
| 187 | + consoleSpy.mockRestore(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should log message with data', () => { |
| 191 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 192 | + |
| 193 | + sdk.use(debugPlugin); |
| 194 | + const data = { foo: 'bar', count: 42 }; |
| 195 | + sdk.debug.log('test message', data); |
| 196 | + |
| 197 | + expect(consoleSpy).toHaveBeenCalledWith('[experiences] test message', data); |
| 198 | + |
| 199 | + consoleSpy.mockRestore(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should include timestamp in window event', () => { |
| 203 | + if (typeof window === 'undefined') { |
| 204 | + return; |
| 205 | + } |
| 206 | + |
| 207 | + const eventHandler = vi.fn(); |
| 208 | + window.addEventListener('experience-sdk:debug', eventHandler); |
| 209 | + |
| 210 | + sdk.use(debugPlugin); |
| 211 | + sdk.debug.log('test message'); |
| 212 | + |
| 213 | + const event = eventHandler.mock.calls[0][0] as CustomEvent; |
| 214 | + expect(event.detail.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/); |
| 215 | + |
| 216 | + window.removeEventListener('experience-sdk:debug', eventHandler); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + describe('debug.isEnabled() method', () => { |
| 221 | + it('should return true when enabled', () => { |
| 222 | + sdk.use(debugPlugin); |
| 223 | + |
| 224 | + expect(sdk.debug.isEnabled()).toBe(true); |
| 225 | + }); |
| 226 | + |
| 227 | + it('should return false when disabled', () => { |
| 228 | + const disabledSdk = new SDK({ debug: { enabled: false } }); |
| 229 | + disabledSdk.use(debugPlugin); |
| 230 | + |
| 231 | + expect(disabledSdk.debug.isEnabled()).toBe(false); |
| 232 | + }); |
| 233 | + }); |
| 234 | +}); |
| 235 | + |
0 commit comments