also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,135 @@ | ||
| 1 | import { describe, expect, test, beforeEach } from 'bun:test' | |
| 2 | import { trackPageView, trackEvent, shouldTrack } from '../src/tracker' | |
| 3 | import { configureAnalytics, resetAnalytics } from '../src/analytics' | |
| 4 | import { MemoryAnalyticsStorage } from '../src/storage/memory' | |
| 5 | import type { AnalyticsConfig } from '../src/types' | |
| 6 | ||
| 7 | describe('trackPageView', () => { | |
| 8 | beforeEach(() => { | |
| 9 | resetAnalytics() | |
| 10 | configureAnalytics({ storage: new MemoryAnalyticsStorage() }) | |
| 11 | }) | |
| 12 | ||
| 13 | test('creates a PageView from plain object', async () => { | |
| 14 | const view = await trackPageView({ | |
| 15 | path: '/test', | |
| 16 | referrer: 'https://google.com', | |
| 17 | userAgent: 'TestBot/1.0', | |
| 18 | sessionId: 'sess-1', | |
| 19 | }) | |
| 20 | ||
| 21 | expect(view.id).toBeTruthy() | |
| 22 | expect(view.path).toBe('/test') | |
| 23 | expect(view.referrer).toBe('https://google.com') | |
| 24 | expect(view.userAgent).toBe('TestBot/1.0') | |
| 25 | expect(view.sessionId).toBe('sess-1') | |
| 26 | expect(view.timestamp).toBeGreaterThan(0) | |
| 27 | }) | |
| 28 | ||
| 29 | test('creates a PageView from Request', async () => { | |
| 30 | const request = new Request('https://example.com/about', { | |
| 31 | headers: { | |
| 32 | 'referer': 'https://google.com', | |
| 33 | 'user-agent': 'TestBot/2.0', | |
| 34 | }, | |
| 35 | }) | |
| 36 | ||
| 37 | const view = await trackPageView(request) | |
| 38 | expect(view.path).toBe('/about') | |
| 39 | expect(view.referrer).toBe('https://google.com') | |
| 40 | expect(view.userAgent).toBe('TestBot/2.0') | |
| 41 | }) | |
| 42 | ||
| 43 | test('creates a PageView with no referrer', async () => { | |
| 44 | const view = await trackPageView({ path: '/home' }) | |
| 45 | expect(view.path).toBe('/home') | |
| 46 | expect(view.referrer).toBeUndefined() | |
| 47 | }) | |
| 48 | ||
| 49 | test('creates a PageView with no sessionId', async () => { | |
| 50 | const view = await trackPageView({ path: '/home' }) | |
| 51 | expect(view.sessionId).toBeUndefined() | |
| 52 | }) | |
| 53 | }) | |
| 54 | ||
| 55 | describe('trackEvent', () => { | |
| 56 | beforeEach(() => { | |
| 57 | resetAnalytics() | |
| 58 | configureAnalytics({ storage: new MemoryAnalyticsStorage() }) | |
| 59 | }) | |
| 60 | ||
| 61 | test('creates an AnalyticsEvent', async () => { | |
| 62 | const event = await trackEvent('click', { button: 'signup' }) | |
| 63 | ||
| 64 | expect(event.id).toBeTruthy() | |
| 65 | expect(event.name).toBe('click') | |
| 66 | expect(event.data).toEqual({ button: 'signup' }) | |
| 67 | expect(event.timestamp).toBeGreaterThan(0) | |
| 68 | }) | |
| 69 | ||
| 70 | test('creates an event without data', async () => { | |
| 71 | const event = await trackEvent('pageload') | |
| 72 | expect(event.name).toBe('pageload') | |
| 73 | expect(event.data).toBeUndefined() | |
| 74 | }) | |
| 75 | ||
| 76 | test('extracts path from Request', async () => { | |
| 77 | const request = new Request('https://example.com/pricing') | |
| 78 | const event = await trackEvent('click', undefined, request) | |
| 79 | expect(event.path).toBe('/pricing') | |
| 80 | }) | |
| 81 | ||
| 82 | test('extracts path and sessionId from plain object', async () => { | |
| 83 | const event = await trackEvent('click', undefined, { path: '/about', sessionId: 's1' }) | |
| 84 | expect(event.path).toBe('/about') | |
| 85 | expect(event.sessionId).toBe('s1') | |
| 86 | }) | |
| 87 | }) | |
| 88 | ||
| 89 | describe('shouldTrack', () => { | |
| 90 | test('returns true when no ignorePaths', () => { | |
| 91 | const config: AnalyticsConfig = { | |
| 92 | storage: new MemoryAnalyticsStorage(), | |
| 93 | ignorePaths: [], | |
| 94 | } | |
| 95 | expect(shouldTrack('/anything', config)).toBe(true) | |
| 96 | }) | |
| 97 | ||
| 98 | test('returns true when ignorePaths is undefined', () => { | |
| 99 | const config: AnalyticsConfig = { | |
| 100 | storage: new MemoryAnalyticsStorage(), | |
| 101 | } | |
| 102 | expect(shouldTrack('/anything', config)).toBe(true) | |
| 103 | }) | |
| 104 | ||
| 105 | test('excludes exact match paths', () => { | |
| 106 | const config: AnalyticsConfig = { | |
| 107 | storage: new MemoryAnalyticsStorage(), | |
| 108 | ignorePaths: ['/health', '/api/ping'], | |
| 109 | } | |
| 110 | expect(shouldTrack('/health', config)).toBe(false) | |
| 111 | expect(shouldTrack('/api/ping', config)).toBe(false) | |
| 112 | expect(shouldTrack('/home', config)).toBe(true) | |
| 113 | }) | |
| 114 | ||
| 115 | test('excludes paths matching glob with *', () => { | |
| 116 | const config: AnalyticsConfig = { | |
| 117 | storage: new MemoryAnalyticsStorage(), | |
| 118 | ignorePaths: ['/api/*'], | |
| 119 | } | |
| 120 | expect(shouldTrack('/api/users', config)).toBe(false) | |
| 121 | expect(shouldTrack('/api/posts/1', config)).toBe(false) | |
| 122 | expect(shouldTrack('/home', config)).toBe(true) | |
| 123 | }) | |
| 124 | ||
| 125 | test('supports multiple glob patterns', () => { | |
| 126 | const config: AnalyticsConfig = { | |
| 127 | storage: new MemoryAnalyticsStorage(), | |
| 128 | ignorePaths: ['/health', '/api/*', '/_stx/*'], | |
| 129 | } | |
| 130 | expect(shouldTrack('/health', config)).toBe(false) | |
| 131 | expect(shouldTrack('/api/data', config)).toBe(false) | |
| 132 | expect(shouldTrack('/_stx/analytics/pageview', config)).toBe(false) | |
| 133 | expect(shouldTrack('/about', config)).toBe(true) | |
| 134 | }) | |
| 135 | }) | |