also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,74 @@ | ||
| 1 | import { describe, expect, test } from 'bun:test' | |
| 2 | import { processAnalyticsDirective, generateTrackingScript } from '../src/directive' | |
| 3 | ||
| 4 | describe('processAnalyticsDirective', () => { | |
| 5 | test('replaces @analytics with a script tag', () => { | |
| 6 | const input = '<head>@analytics</head>' | |
| 7 | const result = processAnalyticsDirective(input) | |
| 8 | ||
| 9 | expect(result).toContain('<script>') | |
| 10 | expect(result).toContain('</script>') | |
| 11 | expect(result).not.toContain('@analytics') | |
| 12 | }) | |
| 13 | ||
| 14 | test('replaces multiple @analytics occurrences', () => { | |
| 15 | const input = '@analytics\n<div></div>\n@analytics' | |
| 16 | const result = processAnalyticsDirective(input) | |
| 17 | ||
| 18 | const scriptCount = (result.match(/<script>/g) || []).length | |
| 19 | expect(scriptCount).toBe(2) | |
| 20 | }) | |
| 21 | ||
| 22 | test('does not replace @analyticsExtra (word boundary)', () => { | |
| 23 | const input = '@analyticsExtra' | |
| 24 | const result = processAnalyticsDirective(input) | |
| 25 | expect(result).toBe('@analyticsExtra') | |
| 26 | }) | |
| 27 | ||
| 28 | test('leaves content without @analytics unchanged', () => { | |
| 29 | const input = '<div>Hello World</div>' | |
| 30 | const result = processAnalyticsDirective(input) | |
| 31 | expect(result).toBe(input) | |
| 32 | }) | |
| 33 | }) | |
| 34 | ||
| 35 | describe('generateTrackingScript', () => { | |
| 36 | test('returns a script tag', () => { | |
| 37 | const script = generateTrackingScript() | |
| 38 | expect(script).toStartWith('<script>') | |
| 39 | expect(script).toEndWith('</script>') | |
| 40 | }) | |
| 41 | ||
| 42 | test('uses default endpoint', () => { | |
| 43 | const script = generateTrackingScript() | |
| 44 | expect(script).toContain('/_stx/analytics/pageview') | |
| 45 | }) | |
| 46 | ||
| 47 | test('uses custom endpoint', () => { | |
| 48 | const script = generateTrackingScript({ endpoint: '/custom/track' }) | |
| 49 | expect(script).toContain('/custom/track') | |
| 50 | }) | |
| 51 | ||
| 52 | test('includes fetch call with POST method', () => { | |
| 53 | const script = generateTrackingScript() | |
| 54 | expect(script).toContain('fetch(endpoint') | |
| 55 | expect(script).toContain("method: 'POST'") | |
| 56 | }) | |
| 57 | ||
| 58 | test('sends path and referrer in body', () => { | |
| 59 | const script = generateTrackingScript() | |
| 60 | expect(script).toContain('location.pathname') | |
| 61 | expect(script).toContain('document.referrer') | |
| 62 | }) | |
| 63 | ||
| 64 | test('includes session management', () => { | |
| 65 | const script = generateTrackingScript() | |
| 66 | expect(script).toContain('sessionStorage') | |
| 67 | expect(script).toContain('_stx_sid') | |
| 68 | }) | |
| 69 | ||
| 70 | test('uses custom sessionTimeout', () => { | |
| 71 | const script = generateTrackingScript({ sessionTimeout: 60000 }) | |
| 72 | expect(script).toContain('60000') | |
| 73 | }) | |
| 74 | }) | |