/**
 * 认证 API 测试
 */

import { describe, it, expect, beforeEach, vi } from 'vitest'
import { login, register, logout, getCurrentUser } from '../auth'
import { mockApiResponse, createMockUser } from '@/test/utils'

// Mock request module
vi.mock('../request', () => ({
  request: {
    post: vi.fn(),
    get: vi.fn()
  }
}))

import { request } from '../request'

describe('Auth API', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  describe('login', () => {
    it('应该成功登录', async () => {
      const mockUser = createMockUser()
      const mockResponse = mockApiResponse.success({
        access_token: 'mock.access.token',
        refresh_token: 'mock.refresh.token',
        user: mockUser,
        expires_in: 3600
      })

      vi.mocked(request.post).mockResolvedValue(mockResponse)

      const credentials = { username: 'testuser', password: 'password' }
      const result = await login(credentials)

      expect(request.post).toHaveBeenCalledWith('/api/v1/auth/login', credentials)
      expect(result).toEqual(mockResponse)
    })

    it('应该处理登录失败', async () => {
      const errorResponse = mockApiResponse.error('用户名或密码错误', 401)
      vi.mocked(request.post).mockRejectedValue(errorResponse)

      const credentials = { username: 'wronguser', password: 'wrongpassword' }

      await expect(login(credentials)).rejects.toEqual(errorResponse)
      expect(request.post).toHaveBeenCalledWith('/api/v1/auth/login', credentials)
    })
  })

  describe('register', () => {
    it('应该成功注册', async () => {
      const mockUser = createMockUser()
      const mockResponse = mockApiResponse.success(mockUser)

      vi.mocked(request.post).mockResolvedValue(mockResponse)

      const registerData = {
        username: 'newuser',
        email: 'newuser@example.com',
        password: 'password',
        confirm_password: 'password',
        full_name: '新用户'
      }

      const result = await register(registerData)

      expect(request.post).toHaveBeenCalledWith('/api/v1/auth/register', registerData)
      expect(result).toEqual(mockResponse)
    })

    it('应该处理注册失败', async () => {
      const errorResponse = mockApiResponse.error('用户名已存在', 400)
      vi.mocked(request.post).mockRejectedValue(errorResponse)

      const registerData = {
        username: 'existinguser',
        email: 'existing@example.com',
        password: 'password',
        confirm_password: 'password',
        full_name: '已存在用户'
      }

      await expect(register(registerData)).rejects.toEqual(errorResponse)
    })
  })

  describe('logout', () => {
    it('应该成功登出', async () => {
      const mockResponse = mockApiResponse.success(null)
      vi.mocked(request.post).mockResolvedValue(mockResponse)

      const result = await logout()

      expect(request.post).toHaveBeenCalledWith('/api/v1/auth/logout')
      expect(result).toEqual(mockResponse)
    })
  })

  describe('getCurrentUser', () => {
    it('应该成功获取当前用户信息', async () => {
      const mockUser = createMockUser()
      const mockResponse = mockApiResponse.success(mockUser)

      vi.mocked(request.get).mockResolvedValue(mockResponse)

      const result = await getCurrentUser()

      expect(request.get).toHaveBeenCalledWith('/api/v1/auth/me')
      expect(result).toEqual(mockResponse)
    })

    it('应该处理未认证错误', async () => {
      const errorResponse = mockApiResponse.error('未登录', 401)
      vi.mocked(request.get).mockRejectedValue(errorResponse)

      await expect(getCurrentUser()).rejects.toEqual(errorResponse)
    })
  })
})
