|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { clearIdentity, initLog, log, setIdentity } from '../src/runtime/client/log' |
| 4 | + |
| 5 | +describe('client identity', () => { |
| 6 | + let fetchSpy: ReturnType<typeof vi.spyOn> |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(null, { status: 200 })) |
| 10 | + initLog({ enabled: true, transport: { enabled: true, endpoint: '/api/_evlog/ingest' } }) |
| 11 | + clearIdentity() |
| 12 | + }) |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + vi.restoreAllMocks() |
| 16 | + clearIdentity() |
| 17 | + }) |
| 18 | + |
| 19 | + function getLastSentEvent(): Record<string, unknown> { |
| 20 | + const [, options] = fetchSpy.mock.calls[fetchSpy.mock.calls.length - 1] as [string, RequestInit] |
| 21 | + return JSON.parse(options.body as string) |
| 22 | + } |
| 23 | + |
| 24 | + it('includes identity fields in emitted events', () => { |
| 25 | + setIdentity({ userId: 'usr_123' }) |
| 26 | + log.info({ action: 'click' }) |
| 27 | + |
| 28 | + const event = getLastSentEvent() |
| 29 | + expect(event.userId).toBe('usr_123') |
| 30 | + expect(event.action).toBe('click') |
| 31 | + }) |
| 32 | + |
| 33 | + it('clearIdentity removes identity fields', () => { |
| 34 | + setIdentity({ userId: 'usr_123' }) |
| 35 | + clearIdentity() |
| 36 | + log.info({ action: 'click' }) |
| 37 | + |
| 38 | + const event = getLastSentEvent() |
| 39 | + expect(event.userId).toBeUndefined() |
| 40 | + }) |
| 41 | + |
| 42 | + it('per-event fields override identity fields', () => { |
| 43 | + setIdentity({ userId: 'usr_123' }) |
| 44 | + log.info({ userId: 'usr_override' }) |
| 45 | + |
| 46 | + const event = getLastSentEvent() |
| 47 | + expect(event.userId).toBe('usr_override') |
| 48 | + }) |
| 49 | + |
| 50 | + it('identity works with tagged logs', () => { |
| 51 | + setIdentity({ userId: 'usr_123' }) |
| 52 | + log.info('auth', 'user logged in') |
| 53 | + |
| 54 | + const event = getLastSentEvent() |
| 55 | + expect(event.userId).toBe('usr_123') |
| 56 | + expect(event.tag).toBe('auth') |
| 57 | + expect(event.message).toBe('user logged in') |
| 58 | + }) |
| 59 | + |
| 60 | + it('supports multiple identity fields', () => { |
| 61 | + setIdentity({ userId: 'usr_123', orgId: 'org_456', role: 'admin' }) |
| 62 | + log.info({ action: 'click' }) |
| 63 | + |
| 64 | + const event = getLastSentEvent() |
| 65 | + expect(event.userId).toBe('usr_123') |
| 66 | + expect(event.orgId).toBe('org_456') |
| 67 | + expect(event.role).toBe('admin') |
| 68 | + }) |
| 69 | + |
| 70 | + it('setIdentity replaces previous identity', () => { |
| 71 | + setIdentity({ userId: 'usr_123', orgId: 'org_456' }) |
| 72 | + setIdentity({ userId: 'usr_789' }) |
| 73 | + log.info({ action: 'click' }) |
| 74 | + |
| 75 | + const event = getLastSentEvent() |
| 76 | + expect(event.userId).toBe('usr_789') |
| 77 | + expect(event.orgId).toBeUndefined() |
| 78 | + }) |
| 79 | +}) |
0 commit comments