camera.spec.ts 6.7 KB

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