import { describe, it, expect, vi, beforeEach } from 'vitest' import type { MockedFunction } from 'vitest' import { authApi } from './auth' // Mock the adminApiClient module vi.mock('./client', () => ({ adminApiClient: { post: vi.fn(), get: vi.fn(), }, })) // Import the mocked module import { adminApiClient } from './client' import type { AxiosResponse } from 'axios' // Type the mocked functions const mockPost = adminApiClient.post as MockedFunction const mockGet = adminApiClient.get as MockedFunction describe('authApi', () => { beforeEach(() => { vi.clearAllMocks() }) describe('requestCode', () => { it('should call adminApiClient.post with correct endpoint', async () => { const mockResponse = { data: { success: true, message: 'Code sent' }, } as AxiosResponse mockPost.mockResolvedValue(mockResponse) const result = await authApi.requestCode() expect(mockPost).toHaveBeenCalledWith('/api/v2/admin/auth/request-code') expect(result).toEqual({ success: true, message: 'Code sent' }) }) it('should handle API errors', async () => { const error = new Error('Network error') mockPost.mockRejectedValue(error) await expect(authApi.requestCode()).rejects.toThrow('Network error') }) }) describe('verifyCode', () => { it('should call adminApiClient.post with code and correct endpoint', async () => { const mockResponse = { data: { success: true, token: 'jwt-token', user: { id: 1, name: 'Admin', admin: true }, }, } as AxiosResponse mockPost.mockResolvedValue(mockResponse) const result = await authApi.verifyCode('123456') expect(mockPost).toHaveBeenCalledWith('/api/v2/admin/auth/verify-code', { code: '123456', }) expect(result).toEqual(mockResponse.data) }) it('should handle invalid code response', async () => { const mockResponse = { data: { success: false, message: 'Invalid code' }, } as AxiosResponse mockPost.mockResolvedValue(mockResponse) const result = await authApi.verifyCode('invalid') expect(result).toEqual({ success: false, message: 'Invalid code' }) }) }) describe('getCurrentUser', () => { it('should call adminApiClient.get with correct endpoint', async () => { const mockResponse = { data: { success: true, user: { id: 1, name: 'Admin', admin: true }, }, } as AxiosResponse mockGet.mockResolvedValue(mockResponse) const result = await authApi.getCurrentUser() expect(mockGet).toHaveBeenCalledWith('/api/v2/admin/auth/me') expect(result).toEqual(mockResponse.data) }) it('should handle unauthorized access', async () => { const mockResponse = { data: { success: false, message: 'Not authenticated' }, } as AxiosResponse mockGet.mockResolvedValue(mockResponse) const result = await authApi.getCurrentUser() expect(result).toEqual({ success: false, message: 'Not authenticated' }) }) }) })