camera.spec.ts 6.8 KB

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