import { describe, it, expect, vi, beforeEach } from 'vitest' import { apiClient, adminApiClient } from './client' describe('API Clients', () => { beforeEach(() => { vi.clearAllMocks() // Clear localStorage localStorage.clear() }) describe('apiClient', () => { it('should be defined and have correct base URL', () => { expect(apiClient).toBeDefined() expect(apiClient.defaults.baseURL).toBe('https://api.party-games.online') }) it('should have interceptors configured', () => { expect(apiClient.interceptors).toBeDefined() expect(apiClient.interceptors.request).toBeDefined() expect(apiClient.interceptors.response).toBeDefined() }) }) describe('adminApiClient', () => { it('should always use production API URL', () => { expect(adminApiClient).toBeDefined() expect(adminApiClient.defaults.baseURL).toBe('https://api.party-games.online') }) it('should be a separate instance from apiClient', () => { expect(adminApiClient).not.toBe(apiClient) expect(adminApiClient.defaults.baseURL).toBe(apiClient.defaults.baseURL) }) it('should have interceptors configured', () => { expect(adminApiClient.interceptors).toBeDefined() expect(adminApiClient.interceptors.request).toBeDefined() expect(adminApiClient.interceptors.response).toBeDefined() }) }) describe('Auth token handling', () => { it('should handle auth tokens correctly in request interceptor', () => { const token = 'test-admin-token' localStorage.setItem('admin_token', token) const requestConfig = { headers: {} as Record } const addAuthToken = (config: { headers: Record }) => { const storedToken = localStorage.getItem('admin_token') if (storedToken) { config.headers.Authorization = `Bearer ${storedToken}` } return config } const result = addAuthToken(requestConfig) expect(result.headers.Authorization).toBe(`Bearer ${token}`) }) it('should not add authorization header when no token exists', () => { const requestConfig = { headers: {} as Record } const addAuthToken = (config: { headers: Record }) => { const storedToken = localStorage.getItem('admin_token') if (storedToken) { config.headers.Authorization = `Bearer ${storedToken}` } return config } const result = addAuthToken(requestConfig) expect(result.headers.Authorization).toBeUndefined() }) }) describe('Error handling', () => { it('should handle 401 errors by clearing token and redirecting', async () => { // Mock window.location const mockLocation = { href: '' } Object.defineProperty(window, 'location', { value: mockLocation, writable: true, }) const handleAuthError = (error: { response?: { status?: number } }) => { if (error.response?.status === 401) { localStorage.removeItem('admin_token') window.location.href = '/login' } return Promise.reject(error) } const error = { response: { status: 401 } } // Set a token first localStorage.setItem('admin_token', 'some-token') await expect(handleAuthError(error)).rejects.toEqual(error) expect(localStorage.getItem('admin_token')).toBeNull() expect(window.location.href).toBe('/login') }) it('should not redirect for non-401 errors', async () => { // Mock window.location const mockLocation = { href: '' } Object.defineProperty(window, 'location', { value: mockLocation, writable: true, }) const handleAuthError = (error: { response?: { status?: number } }) => { if (error.response?.status === 401) { localStorage.removeItem('admin_token') window.location.href = '/login' } return Promise.reject(error) } const error = { response: { status: 500 } } await expect(handleAuthError(error)).rejects.toEqual(error) expect(localStorage.getItem('admin_token')).toBeNull() // Should be cleared from beforeEach expect(window.location.href).toBe('') }) }) })