camera.spec.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { describe, it, expect, vi, beforeEach } from 'vitest'
  2. import {
  3. listCameras,
  4. getCamera,
  5. switchChannel,
  6. getCurrentChannel,
  7. ptzControl,
  8. adminListCameras,
  9. adminGetCamera,
  10. adminAddCamera,
  11. adminUpdateCamera,
  12. adminDeleteCamera,
  13. adminCheckCamera
  14. } from '@/api/camera'
  15. import * as request from '@/utils/request'
  16. import { mockCameras, mockChannels, wrapResponse } from '../../fixtures'
  17. vi.mock('@/utils/request', () => ({
  18. get: vi.fn(),
  19. post: vi.fn()
  20. }))
  21. describe('Camera API', () => {
  22. beforeEach(() => {
  23. vi.clearAllMocks()
  24. })
  25. describe('Controller APIs', () => {
  26. describe('listCameras', () => {
  27. it('should call POST /camera/list', async () => {
  28. const mockResponse = wrapResponse(mockCameras)
  29. vi.mocked(request.post).mockResolvedValue(mockResponse)
  30. const result = await listCameras()
  31. expect(request.post).toHaveBeenCalledWith('/camera/list', {})
  32. expect(result.data).toHaveLength(mockCameras.length)
  33. })
  34. it('should call with machineId filter', async () => {
  35. vi.mocked(request.post).mockResolvedValue(wrapResponse([]))
  36. await listCameras({ machineId: 'machine-001' })
  37. expect(request.post).toHaveBeenCalledWith('/camera/list', { machineId: 'machine-001' })
  38. })
  39. })
  40. describe('getCamera', () => {
  41. it('should call GET /cameras/:id', async () => {
  42. const camera = mockCameras[0]
  43. const mockResponse = wrapResponse(camera)
  44. vi.mocked(request.get).mockResolvedValue(mockResponse)
  45. const result = await getCamera({ cameraId: camera.cameraId })
  46. expect(request.get).toHaveBeenCalledWith(`/cameras/${camera.cameraId}`)
  47. expect(result.data.cameraId).toBe(camera.cameraId)
  48. })
  49. })
  50. describe('switchChannel', () => {
  51. it('should call POST /camera/switch', async () => {
  52. const channel = mockChannels[0]
  53. const mockResponse = wrapResponse(channel)
  54. vi.mocked(request.post).mockResolvedValue(mockResponse)
  55. const data = { machineId: 'machine-001', channelId: channel.channelId }
  56. const result = await switchChannel(data)
  57. expect(request.post).toHaveBeenCalledWith('/camera/switch', data)
  58. expect(result.data.channelId).toBe(channel.channelId)
  59. })
  60. })
  61. describe('getCurrentChannel', () => {
  62. it('should call GET /camera/current', async () => {
  63. const channel = mockChannels[0]
  64. const mockResponse = wrapResponse(channel)
  65. vi.mocked(request.get).mockResolvedValue(mockResponse)
  66. const data = { machineId: 'machine-001' }
  67. const result = await getCurrentChannel(data)
  68. expect(request.get).toHaveBeenCalledWith('/camera/current', data)
  69. expect(result.data.channelId).toBe(channel.channelId)
  70. })
  71. })
  72. describe('ptzControl', () => {
  73. it('should call POST /camera/control/:cameraId/ptz/control', async () => {
  74. const mockResponse = wrapResponse(null)
  75. vi.mocked(request.post).mockResolvedValue(mockResponse)
  76. const data = { cameraId: 'cam-001', command: 'up', speed: 50 }
  77. await ptzControl(data)
  78. expect(request.post).toHaveBeenCalledWith('/camera/control/cam-001/ptz/control', data)
  79. })
  80. })
  81. })
  82. describe('Admin APIs', () => {
  83. describe('adminListCameras', () => {
  84. it('should call POST /admin/cameras/list', async () => {
  85. const mockResponse = wrapResponse({
  86. list: mockCameras,
  87. total: mockCameras.length,
  88. page: 1,
  89. size: 20,
  90. totalPages: 1,
  91. hasNext: false,
  92. hasPrevious: false
  93. })
  94. vi.mocked(request.post).mockResolvedValue(mockResponse)
  95. const result = await adminListCameras()
  96. expect(request.post).toHaveBeenCalledWith('/admin/cameras/list', {})
  97. expect(result.data.list).toHaveLength(mockCameras.length)
  98. })
  99. })
  100. describe('adminGetCamera', () => {
  101. it('should call GET /admin/cameras/detail', async () => {
  102. const camera = mockCameras[0]
  103. const mockResponse = wrapResponse(camera)
  104. vi.mocked(request.get).mockResolvedValue(mockResponse)
  105. const data = { id: camera.id }
  106. const result = await adminGetCamera(data)
  107. expect(request.get).toHaveBeenCalledWith('/admin/cameras/detail', data)
  108. expect(result.data.id).toBe(camera.id)
  109. })
  110. })
  111. describe('adminAddCamera', () => {
  112. it('should call POST /admin/cameras/add', async () => {
  113. const newCamera = { ...mockCameras[0], id: 100, cameraId: 'cam-new' }
  114. const mockResponse = wrapResponse(newCamera)
  115. vi.mocked(request.post).mockResolvedValue(mockResponse)
  116. const data = {
  117. cameraId: 'cam-new',
  118. name: '新摄像头',
  119. ip: '192.168.1.100',
  120. port: 80
  121. }
  122. const result = await adminAddCamera(data)
  123. expect(request.post).toHaveBeenCalledWith('/admin/cameras/add', data)
  124. expect(result.data.cameraId).toBe('cam-new')
  125. })
  126. })
  127. describe('adminUpdateCamera', () => {
  128. it('should call POST /admin/cameras/update', async () => {
  129. const updatedCamera = { ...mockCameras[0], name: '更新后名称' }
  130. const mockResponse = wrapResponse(updatedCamera)
  131. vi.mocked(request.post).mockResolvedValue(mockResponse)
  132. const data = { id: mockCameras[0].id, name: '更新后名称' }
  133. const result = await adminUpdateCamera(data)
  134. expect(request.post).toHaveBeenCalledWith('/admin/cameras/update', data)
  135. expect(result.data.name).toBe('更新后名称')
  136. })
  137. })
  138. describe('adminDeleteCamera', () => {
  139. it('should call POST /admin/cameras/delete', async () => {
  140. const mockResponse = wrapResponse(null)
  141. vi.mocked(request.post).mockResolvedValue(mockResponse)
  142. const data = { id: 1 }
  143. await adminDeleteCamera(data)
  144. expect(request.post).toHaveBeenCalledWith('/admin/cameras/delete', undefined, { params: data })
  145. })
  146. })
  147. describe('adminCheckCamera', () => {
  148. it('should call POST /admin/cameras/check', async () => {
  149. const mockResponse = wrapResponse(true)
  150. vi.mocked(request.post).mockResolvedValue(mockResponse)
  151. const data = { id: 1 }
  152. const result = await adminCheckCamera(data)
  153. expect(request.post).toHaveBeenCalledWith('/admin/cameras/check', undefined, { params: data })
  154. expect(result.data).toBe(true)
  155. })
  156. })
  157. })
  158. })