|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { HawkUserManager } from '../../src'; |
| 3 | +import type { HawkStorage, RandomGenerator } from '../../src'; |
| 4 | + |
| 5 | +describe('HawkUserManager', () => { |
| 6 | + let storage: HawkStorage; |
| 7 | + let randomGenerator: RandomGenerator; |
| 8 | + let manager: HawkUserManager; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + storage = { |
| 12 | + getItem: vi.fn().mockReturnValue(null), |
| 13 | + setItem: vi.fn(), |
| 14 | + removeItem: vi.fn(), |
| 15 | + }; |
| 16 | + randomGenerator = { |
| 17 | + getRandomNumbers: vi.fn().mockReturnValue(new Uint8Array(40).fill(42)), |
| 18 | + }; |
| 19 | + manager = new HawkUserManager(storage, randomGenerator); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return anonymous ID when no user is set and no ID is persisted', () => { |
| 23 | + const user = manager.getUser(); |
| 24 | + |
| 25 | + expect(user.id).toBeTruthy(); |
| 26 | + expect(storage.setItem).toHaveBeenCalledWith('hawk-user-id', user.id); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return in-memory user set via setUser()', () => { |
| 30 | + const user = { id: 'user-1', name: 'Ryan Gosling', url: 'https://example.com', photo: 'https://example.com/photo.png' }; |
| 31 | + |
| 32 | + manager.setUser(user); |
| 33 | + |
| 34 | + expect(manager.getUser()).toEqual(user); |
| 35 | + expect(storage.setItem).not.toHaveBeenCalled(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not affect storage when setUser() is called', () => { |
| 39 | + manager.setUser({ id: 'user-1' }); |
| 40 | + |
| 41 | + expect(storage.setItem).not.toHaveBeenCalled(); |
| 42 | + expect(storage.removeItem).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return anonymous user from storage when no in-memory user is set', () => { |
| 46 | + vi.mocked(storage.getItem).mockReturnValue('anon-123'); |
| 47 | + |
| 48 | + expect(manager.getUser()).toEqual({ id: 'anon-123' }); |
| 49 | + expect(storage.getItem).toHaveBeenCalledWith('hawk-user-id'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should prefer in-memory user over persisted anonymous ID', () => { |
| 53 | + vi.mocked(storage.getItem).mockReturnValue('anon-123'); |
| 54 | + manager.setUser({ id: 'explicit-user' }); |
| 55 | + |
| 56 | + expect(manager.getUser()).toEqual({ id: 'explicit-user' }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should clear in-memory user and fall back to persisted anonymous ID', () => { |
| 60 | + vi.mocked(storage.getItem).mockReturnValue('anon-123'); |
| 61 | + manager.setUser({ id: 'user-1' }); |
| 62 | + manager.clear(); |
| 63 | + |
| 64 | + expect(manager.getUser()).toEqual({ id: 'anon-123' }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return new anonymous ID after clear() when no ID is persisted', () => { |
| 68 | + manager.setUser({ id: 'user-1' }); |
| 69 | + manager.clear(); |
| 70 | + |
| 71 | + const user = manager.getUser(); |
| 72 | + |
| 73 | + expect(user.id).toBeTruthy(); |
| 74 | + expect(storage.setItem).toHaveBeenCalledWith('hawk-user-id', user.id); |
| 75 | + }); |
| 76 | +}); |
0 commit comments