also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,165 @@ | ||
| 1 | import { describe, expect, test, beforeEach } from 'bun:test' | |
| 2 | import { | |
| 3 | AnalyticsEngine, | |
| 4 | configureAnalytics, | |
| 5 | getAnalytics, | |
| 6 | getAnalyticsEngine, | |
| 7 | resetAnalytics, | |
| 8 | } from '../src/analytics' | |
| 9 | import { MemoryAnalyticsStorage } from '../src/storage/memory' | |
| 10 | ||
| 11 | describe('configureAnalytics and resetAnalytics', () => { | |
| 12 | beforeEach(() => { | |
| 13 | resetAnalytics() | |
| 14 | }) | |
| 15 | ||
| 16 | test('configureAnalytics sets up the engine', () => { | |
| 17 | const storage = new MemoryAnalyticsStorage() | |
| 18 | configureAnalytics({ storage }) | |
| 19 | const engine = getAnalyticsEngine() | |
| 20 | expect(engine).toBeInstanceOf(AnalyticsEngine) | |
| 21 | expect(engine.storage).toBe(storage) | |
| 22 | }) | |
| 23 | ||
| 24 | test('configureAnalytics uses defaults', () => { | |
| 25 | configureAnalytics({}) | |
| 26 | const engine = getAnalyticsEngine() | |
| 27 | expect(engine.config.anonymize).toBe(true) | |
| 28 | expect(engine.config.sessionTimeout).toBe(1800000) | |
| 29 | expect(engine.storage).toBeInstanceOf(MemoryAnalyticsStorage) | |
| 30 | }) | |
| 31 | ||
| 32 | test('resetAnalytics clears the engine', () => { | |
| 33 | const storage = new MemoryAnalyticsStorage() | |
| 34 | configureAnalytics({ storage }) | |
| 35 | resetAnalytics() | |
| 36 | ||
| 37 | // Getting engine after reset creates a new default one | |
| 38 | const engine = getAnalyticsEngine() | |
| 39 | expect(engine.storage).not.toBe(storage) | |
| 40 | }) | |
| 41 | ||
| 42 | test('getAnalyticsEngine creates default engine if not configured', () => { | |
| 43 | const engine = getAnalyticsEngine() | |
| 44 | expect(engine).toBeInstanceOf(AnalyticsEngine) | |
| 45 | expect(engine.config.anonymize).toBe(true) | |
| 46 | }) | |
| 47 | }) | |
| 48 | ||
| 49 | describe('getAnalytics', () => { | |
| 50 | beforeEach(() => { | |
| 51 | resetAnalytics() | |
| 52 | }) | |
| 53 | ||
| 54 | test('returns correct stats after tracking', async () => { | |
| 55 | const storage = new MemoryAnalyticsStorage() | |
| 56 | configureAnalytics({ storage }) | |
| 57 | const engine = getAnalyticsEngine() | |
| 58 | ||
| 59 | await engine.trackPageView({ path: '/home', sessionId: 'a' }) | |
| 60 | await engine.trackPageView({ path: '/about', sessionId: 'a' }) | |
| 61 | await engine.trackPageView({ path: '/home', sessionId: 'b' }) | |
| 62 | ||
| 63 | const stats = await getAnalytics() | |
| 64 | expect(stats.totalPageViews).toBe(3) | |
| 65 | expect(stats.uniqueVisitors).toBe(2) | |
| 66 | expect(stats.topPages[0]).toEqual({ path: '/home', views: 2 }) | |
| 67 | expect(stats.topPages[1]).toEqual({ path: '/about', views: 1 }) | |
| 68 | }) | |
| 69 | ||
| 70 | test('returns empty stats when no data', async () => { | |
| 71 | configureAnalytics({}) | |
| 72 | const stats = await getAnalytics() | |
| 73 | expect(stats.totalPageViews).toBe(0) | |
| 74 | expect(stats.uniqueVisitors).toBe(0) | |
| 75 | }) | |
| 76 | ||
| 77 | test('filters by date range', async () => { | |
| 78 | const storage = new MemoryAnalyticsStorage() | |
| 79 | configureAnalytics({ storage }) | |
| 80 | ||
| 81 | const now = Date.now() | |
| 82 | await storage.savePageView({ | |
| 83 | id: '1', | |
| 84 | path: '/old', | |
| 85 | timestamp: now - 86400000 * 10, | |
| 86 | }) | |
| 87 | await storage.savePageView({ | |
| 88 | id: '2', | |
| 89 | path: '/new', | |
| 90 | timestamp: now, | |
| 91 | sessionId: 's1', | |
| 92 | }) | |
| 93 | ||
| 94 | const stats = await getAnalytics({ | |
| 95 | from: new Date(now - 86400000), | |
| 96 | }) | |
| 97 | expect(stats.totalPageViews).toBe(1) | |
| 98 | expect(stats.topPages[0].path).toBe('/new') | |
| 99 | }) | |
| 100 | }) | |
| 101 | ||
| 102 | describe('AnalyticsEngine', () => { | |
| 103 | test('trackPageView stores and returns view', async () => { | |
| 104 | const storage = new MemoryAnalyticsStorage() | |
| 105 | const engine = new AnalyticsEngine({ storage }) | |
| 106 | ||
| 107 | const view = await engine.trackPageView({ path: '/test' }) | |
| 108 | expect(view.path).toBe('/test') | |
| 109 | expect(view.id).toBeTruthy() | |
| 110 | ||
| 111 | const views = await engine.getPageViews() | |
| 112 | expect(views).toHaveLength(1) | |
| 113 | }) | |
| 114 | ||
| 115 | test('trackPageView from Request', async () => { | |
| 116 | const storage = new MemoryAnalyticsStorage() | |
| 117 | const engine = new AnalyticsEngine({ storage }) | |
| 118 | ||
| 119 | const request = new Request('https://example.com/dashboard', { | |
| 120 | headers: { | |
| 121 | 'referer': 'https://google.com', | |
| 122 | 'user-agent': 'Mozilla/5.0', | |
| 123 | }, | |
| 124 | }) | |
| 125 | ||
| 126 | const view = await engine.trackPageView(request) | |
| 127 | expect(view.path).toBe('/dashboard') | |
| 128 | expect(view.referrer).toBe('https://google.com') | |
| 129 | expect(view.userAgent).toBe('Mozilla/5.0') | |
| 130 | }) | |
| 131 | ||
| 132 | test('trackPageView rejects ignored paths', async () => { | |
| 133 | const storage = new MemoryAnalyticsStorage() | |
| 134 | const engine = new AnalyticsEngine({ | |
| 135 | storage, | |
| 136 | ignorePaths: ['/health'], | |
| 137 | }) | |
| 138 | ||
| 139 | expect(engine.trackPageView({ path: '/health' })).rejects.toThrow('excluded from tracking') | |
| 140 | }) | |
| 141 | ||
| 142 | test('trackEvent stores and returns event', async () => { | |
| 143 | const storage = new MemoryAnalyticsStorage() | |
| 144 | const engine = new AnalyticsEngine({ storage }) | |
| 145 | ||
| 146 | const event = await engine.trackEvent('click', { button: 'cta' }) | |
| 147 | expect(event.name).toBe('click') | |
| 148 | expect(event.data).toEqual({ button: 'cta' }) | |
| 149 | ||
| 150 | const events = await engine.getEvents() | |
| 151 | expect(events).toHaveLength(1) | |
| 152 | }) | |
| 153 | ||
| 154 | test('getStats delegates to storage', async () => { | |
| 155 | const storage = new MemoryAnalyticsStorage() | |
| 156 | const engine = new AnalyticsEngine({ storage }) | |
| 157 | ||
| 158 | await engine.trackPageView({ path: '/a', sessionId: 'x' }) | |
| 159 | await engine.trackPageView({ path: '/b', sessionId: 'y' }) | |
| 160 | ||
| 161 | const stats = await engine.getStats() | |
| 162 | expect(stats.totalPageViews).toBe(2) | |
| 163 | expect(stats.uniqueVisitors).toBe(2) | |
| 164 | }) | |
| 165 | }) | |