| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- import { describe, it, expect, vi, beforeEach } from 'vitest'
- import {
- listCameras,
- getCamera,
- switchChannel,
- getCurrentChannel,
- ptzStart,
- ptzStop,
- adminListCameras,
- adminGetCamera,
- adminAddCamera,
- adminUpdateCamera,
- adminDeleteCamera,
- adminCheckCamera
- } from '@/api/camera'
- import * as request from '@/utils/request'
- vi.mock('@/utils/request', () => ({
- get: vi.fn(),
- post: vi.fn()
- }))
- describe('Camera API', () => {
- beforeEach(() => {
- vi.clearAllMocks()
- })
- describe('Controller APIs', () => {
- describe('listCameras', () => {
- it('should call GET /camera/list', async () => {
- const mockResponse = {
- code: 200,
- data: [
- {
- cameraId: 'cam-001',
- name: '摄像头1',
- machineId: 'machine-001',
- status: 'ONLINE',
- capability: 'ptz_enabled',
- ptzSupported: true,
- channels: []
- }
- ]
- }
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const result = await listCameras()
- expect(request.get).toHaveBeenCalledWith('/camera/list', undefined)
- expect(result.data).toHaveLength(1)
- })
- it('should call with machineId filter', async () => {
- vi.mocked(request.get).mockResolvedValue({ code: 200, data: [] })
- await listCameras('machine-001')
- expect(request.get).toHaveBeenCalledWith('/camera/list', { machineId: 'machine-001' })
- })
- })
- describe('getCamera', () => {
- it('should call GET /camera/:id', async () => {
- const mockResponse = {
- code: 200,
- data: { cameraId: 'cam-001', name: '摄像头1' }
- }
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const result = await getCamera('cam-001')
- expect(request.get).toHaveBeenCalledWith('/camera/cam-001')
- expect(result.data.cameraId).toBe('cam-001')
- })
- })
- describe('switchChannel', () => {
- it('should call POST /camera/switch', async () => {
- const mockResponse = {
- code: 200,
- data: { channelId: 'ch-001', name: '通道1' }
- }
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await switchChannel({ machineId: 'machine-001', channelId: 'ch-001' })
- expect(request.post).toHaveBeenCalledWith('/camera/switch', {
- machineId: 'machine-001',
- channelId: 'ch-001'
- })
- expect(result.data.channelId).toBe('ch-001')
- })
- })
- describe('getCurrentChannel', () => {
- it('should call GET /camera/current', async () => {
- const mockResponse = {
- code: 200,
- data: { channelId: 'ch-001', name: '当前通道' }
- }
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const result = await getCurrentChannel('machine-001')
- expect(request.get).toHaveBeenCalledWith('/camera/current', { machineId: 'machine-001' })
- expect(result.data.channelId).toBe('ch-001')
- })
- })
- describe('ptzStart', () => {
- it('should call POST /camera/:id/ptz/start', async () => {
- const mockResponse = { code: 200, data: null }
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- await ptzStart('cam-001', 'up', 50)
- expect(request.post).toHaveBeenCalledWith('/camera/cam-001/ptz/start', undefined, {
- params: { action: 'up', speed: 50 }
- })
- })
- })
- describe('ptzStop', () => {
- it('should call POST /camera/:id/ptz/stop', async () => {
- const mockResponse = { code: 200, data: null }
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- await ptzStop('cam-001')
- expect(request.post).toHaveBeenCalledWith('/camera/cam-001/ptz/stop')
- })
- })
- })
- describe('Admin APIs', () => {
- describe('adminListCameras', () => {
- it('should call GET /admin/cameras/list', async () => {
- const mockResponse = {
- code: 200,
- data: [{ id: 1, cameraId: 'cam-001', name: '摄像头1' }]
- }
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const result = await adminListCameras()
- expect(request.get).toHaveBeenCalledWith('/admin/cameras/list', undefined)
- expect(result.data).toHaveLength(1)
- })
- })
- describe('adminGetCamera', () => {
- it('should call GET /admin/cameras/detail', async () => {
- const mockResponse = {
- code: 200,
- data: { id: 1, cameraId: 'cam-001' }
- }
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const result = await adminGetCamera(1)
- expect(request.get).toHaveBeenCalledWith('/admin/cameras/detail', { id: 1 })
- expect(result.data.id).toBe(1)
- })
- })
- describe('adminAddCamera', () => {
- it('should call POST /admin/cameras/add', async () => {
- const mockResponse = {
- code: 200,
- data: { id: 1, cameraId: 'cam-002', name: '新摄像头' }
- }
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const addData = {
- cameraId: 'cam-002',
- name: '新摄像头',
- ip: '192.168.1.100',
- port: 80
- }
- const result = await adminAddCamera(addData)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/add', addData)
- expect(result.data.cameraId).toBe('cam-002')
- })
- })
- describe('adminUpdateCamera', () => {
- it('should call POST /admin/cameras/update', async () => {
- const mockResponse = {
- code: 200,
- data: { id: 1, name: '更新后名称' }
- }
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const updateData = { id: 1, name: '更新后名称' }
- const result = await adminUpdateCamera(updateData)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/update', updateData)
- expect(result.data.name).toBe('更新后名称')
- })
- })
- describe('adminDeleteCamera', () => {
- it('should call POST /admin/cameras/delete', async () => {
- const mockResponse = { code: 200, data: null }
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- await adminDeleteCamera(1)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/delete', undefined, {
- params: { id: 1 }
- })
- })
- })
- describe('adminCheckCamera', () => {
- it('should call POST /admin/cameras/check', async () => {
- const mockResponse = { code: 200, data: true }
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await adminCheckCamera(1)
- expect(request.post).toHaveBeenCalledWith('/admin/cameras/check', undefined, {
- params: { id: 1 }
- })
- expect(result.data).toBe(true)
- })
- })
- })
- })
|