import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest' import { getEventAttribution, getOrCreateVisitorId, parseTrafficSource, rememberEventAttribution } from '@/lib/trafficAttribution' describe('trafficAttribution', () => { const store = new Map() beforeEach(() => { store.clear() vi.stubGlobal('localStorage', { getItem: (key: string) => store.get(key) ?? null, setItem: (key: string, value: string) => { store.set(key, value) }, removeItem: (key: string) => { store.delete(key) }, }) }) afterEach(() => { vi.unstubAllGlobals() }) it('parses only instagram and telegram', () => { expect(parseTrafficSource('instagram')).toBe('instagram') expect(parseTrafficSource('TELEGRAM')).toBe('telegram') expect(parseTrafficSource('whatsapp')).toBeNull() }) it('keeps first-touch attribution for an event', () => { rememberEventAttribution('event-1', 'instagram') rememberEventAttribution('event-1', 'telegram') expect(getEventAttribution('event-1')).toBe('instagram') }) it('creates a durable visitor id', () => { const first = getOrCreateVisitorId() const second = getOrCreateVisitorId() expect(first).toMatch(/^[0-9a-f-]{36}$/i) expect(second).toBe(first) }) })