admin/lib/trafficAttribution.test.ts
alisaza e1eaf5eff5 feat: initial ghabilee-admin backoffice app
Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js
app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
2026-09-05 13:12:59 +03:30

45 lines
1.3 KiB
TypeScript

import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
import { getEventAttribution, getOrCreateVisitorId, parseTrafficSource, rememberEventAttribution } from '@/lib/trafficAttribution'
describe('trafficAttribution', () => {
const store = new Map<string, string>()
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)
})
})