import { describe, it, expect, vi, beforeEach } from 'vitest'
import { getDashboardStats } from '@/api/stats'
import * as request from '@/utils/request'

vi.mock('@/utils/request', () => ({
  get: vi.fn()
}))

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

  describe('getDashboardStats', () => {
    it('should call GET /admin/stats/dashboard', async () => {
      const mockResponse = {
        code: 200,
        message: 'success',
        data: {
          machineTotal: 5,
          machineEnabled: 4,
          cameraTotal: 20,
          cameraOnline: 18,
          cameraOffline: 2,
          channelTotal: 40
        }
      }
      vi.mocked(request.get).mockResolvedValue(mockResponse)

      const result = await getDashboardStats()

      expect(request.get).toHaveBeenCalledWith('/admin/stats/dashboard')
      expect(result.code).toBe(200)
      expect(result.data.machineTotal).toBe(5)
      expect(result.data.cameraTotal).toBe(20)
      expect(result.data.cameraOnline).toBe(18)
    })

    it('should handle empty stats', async () => {
      const mockResponse = {
        code: 200,
        message: 'success',
        data: {
          machineTotal: 0,
          machineEnabled: 0,
          cameraTotal: 0,
          cameraOnline: 0,
          cameraOffline: 0,
          channelTotal: 0
        }
      }
      vi.mocked(request.get).mockResolvedValue(mockResponse)

      const result = await getDashboardStats()

      expect(result.data.machineTotal).toBe(0)
      expect(result.data.cameraTotal).toBe(0)
    })

    it('should handle API error', async () => {
      const mockResponse = {
        code: 500,
        message: '服务器错误',
        data: null
      }
      vi.mocked(request.get).mockResolvedValue(mockResponse)

      const result = await getDashboardStats()

      expect(result.code).toBe(500)
    })
  })
})
