| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import { describe, it, expect, vi, beforeEach } from 'vitest'
- import {
- listCameras,
- getCamera,
- switchChannel,
- getCurrentChannel,
- ptzControl,
- adminListCameras,
- adminGetCamera,
- adminAddCamera,
- adminUpdateCamera,
- adminDeleteCamera,
- adminCheckCamera
- } from '@/api/camera'
- import * as request from '@/utils/request'
- import { mockCameras, mockChannels, wrapResponse } from '../../fixtures'
- vi.mock('@/utils/request', () => ({
- get: vi.fn(),
- post: vi.fn()
- }))
- describe('Camera API', () => {
- beforeEach(() => {
- vi.clearAllMocks()
- })
- describe('Controller APIs', () => {
- describe('listCameras', () => {
- it('should call POST /camera/list', async () => {
- const mockResponse = wrapResponse(mockCameras)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await listCameras()
- expect(request.post).toHaveBeenCalledWith('/camera/list', {})
- expect(result.data).toHaveLength(mockCameras.length)
- })
- it('should call with machineId filter', async () => {
- vi.mocked(request.post).mockResolvedValue(wrapResponse([]))
- await listCameras({ machineId: 'machine-001' })
- expect(request.post).toHaveBeenCalledWith('/camera/list', { machineId: 'machine-001' })
- })
- })
- describe('getCamera', () => {
- it('should call GET /cameras/:id', async () => {
- const camera = mockCameras[0]
- const mockResponse = wrapResponse(camera)
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const result = await getCamera({ cameraId: camera.cameraId })
- expect(request.get).toHaveBeenCalledWith(`/cameras/${camera.cameraId}`)
- expect(result.data.cameraId).toBe(camera.cameraId)
- })
- })
- describe('switchChannel', () => {
- it('should call POST /camera/switch', async () => {
- const channel = mockChannels[0]
- const mockResponse = wrapResponse(channel)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const data = { machineId: 'machine-001', channelId: channel.channelId }
- const result = await switchChannel(data)
- expect(request.post).toHaveBeenCalledWith('/camera/switch', data)
- expect(result.data.channelId).toBe(channel.channelId)
- })
- })
- describe('getCurrentChannel', () => {
- it('should call GET /camera/current', async () => {
- const channel = mockChannels[0]
- const mockResponse = wrapResponse(channel)
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const data = { machineId: 'machine-001' }
- const result = await getCurrentChannel(data)
- expect(request.get).toHaveBeenCalledWith('/camera/current', data)
- expect(result.data.channelId).toBe(channel.channelId)
- })
- })
- describe('ptzControl', () => {
- it('should call POST /camera/control/:cameraId/ptz/control', async () => {
- const mockResponse = wrapResponse(null)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const data = { cameraId: 'cam-001', command: 'up', speed: 50 }
- await ptzControl(data)
- expect(request.post).toHaveBeenCalledWith('/camera/control/cam-001/ptz/control', data)
- })
- })
- })
- describe('Admin APIs', () => {
- describe('adminListCameras', () => {
- it('should call POST /admin/cameras/list', async () => {
- const mockResponse = wrapResponse({
- list: mockCameras,
- total: mockCameras.length,
- page: 1,
- size: 20,
- totalPages: 1,
- hasNext: false,
- hasPrevious: false
- })
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await adminListCameras()
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/list', {})
- expect(result.data.list).toHaveLength(mockCameras.length)
- })
- })
- describe('adminGetCamera', () => {
- it('should call GET /admin/cameras/detail', async () => {
- const camera = mockCameras[0]
- const mockResponse = wrapResponse(camera)
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const data = { id: camera.id }
- const result = await adminGetCamera(data)
- expect(request.get).toHaveBeenCalledWith('/admin/cameras/detail', data)
- expect(result.data.id).toBe(camera.id)
- })
- })
- describe('adminAddCamera', () => {
- it('should call POST /admin/cameras/add', async () => {
- const newCamera = { ...mockCameras[0], id: 100, cameraId: 'cam-new' }
- const mockResponse = wrapResponse(newCamera)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const data = {
- cameraId: 'cam-new',
- name: '新摄像头',
- ip: '192.168.1.100',
- port: 80
- }
- const result = await adminAddCamera(data)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/add', data)
- expect(result.data.cameraId).toBe('cam-new')
- })
- })
- describe('adminUpdateCamera', () => {
- it('should call POST /admin/cameras/update', async () => {
- const updatedCamera = { ...mockCameras[0], name: '更新后名称' }
- const mockResponse = wrapResponse(updatedCamera)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const data = { id: mockCameras[0].id, name: '更新后名称' }
- const result = await adminUpdateCamera(data)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/update', data)
- expect(result.data.name).toBe('更新后名称')
- })
- })
- describe('adminDeleteCamera', () => {
- it('should call POST /admin/cameras/delete', async () => {
- const mockResponse = wrapResponse(null)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const data = { id: 1 }
- await adminDeleteCamera(data)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/delete', undefined, { params: data })
- })
- })
- describe('adminCheckCamera', () => {
- it('should call POST /admin/cameras/check', async () => {
- const mockResponse = wrapResponse(true)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const data = { id: 1 }
- const result = await adminCheckCamera(data)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/check', undefined, { params: data })
- expect(result.data).toBe(true)
- })
- })
- })
- })
|