camera.spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 GET /camera/list', async () => {
  29. const mockResponse = wrapResponse(mockCameras)
  30. vi.mocked(request.get).mockResolvedValue(mockResponse)
  31. const result = await listCameras()
  32. expect(request.get).toHaveBeenCalledWith('/camera/list', undefined)
  33. expect(result.data).toHaveLength(mockCameras.length)
  34. })
  35. it('should call with machineId filter', async () => {
  36. vi.mocked(request.get).mockResolvedValue(wrapResponse([]))
  37. await listCameras('machine-001')
  38. expect(request.get).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 GET /admin/cameras/list', async () => {
  96. const mockResponse = wrapResponse(mockCameras)
  97. vi.mocked(request.get).mockResolvedValue(mockResponse)
  98. const result = await adminListCameras()
  99. expect(request.get).toHaveBeenCalledWith('/admin/cameras/list', undefined)
  100. expect(result.data).toHaveLength(mockCameras.length)
  101. })
  102. })
  103. describe('adminGetCamera', () => {
  104. it('should call GET /admin/cameras/detail', async () => {
  105. const camera = mockCameras[0]
  106. const mockResponse = wrapResponse(camera)
  107. vi.mocked(request.get).mockResolvedValue(mockResponse)
  108. const result = await adminGetCamera(camera.id)
  109. expect(request.get).toHaveBeenCalledWith('/admin/cameras/detail', { id: camera.id })
  110. expect(result.data.id).toBe(camera.id)
  111. })
  112. })
  113. describe('adminAddCamera', () => {
  114. it('should call POST /admin/cameras/add', async () => {
  115. const newCamera = { ...mockCameras[0], id: 100, cameraId: 'cam-new' }
  116. const mockResponse = wrapResponse(newCamera)
  117. vi.mocked(request.post).mockResolvedValue(mockResponse)
  118. const addData = {
  119. cameraId: 'cam-new',
  120. name: '新摄像头',
  121. ip: '192.168.1.100',
  122. port: 80
  123. }
  124. const result = await adminAddCamera(addData)
  125. expect(request.post).toHaveBeenCalledWith('/admin/cameras/add', addData)
  126. expect(result.data.cameraId).toBe('cam-new')
  127. })
  128. })
  129. describe('adminUpdateCamera', () => {
  130. it('should call POST /admin/cameras/update', async () => {
  131. const updatedCamera = { ...mockCameras[0], name: '更新后名称' }
  132. const mockResponse = wrapResponse(updatedCamera)
  133. vi.mocked(request.post).mockResolvedValue(mockResponse)
  134. const updateData = { id: mockCameras[0].id, name: '更新后名称' }
  135. const result = await adminUpdateCamera(updateData)
  136. expect(request.post).toHaveBeenCalledWith('/admin/cameras/update', updateData)
  137. expect(result.data.name).toBe('更新后名称')
  138. })
  139. })
  140. describe('adminDeleteCamera', () => {
  141. it('should call POST /admin/cameras/delete', async () => {
  142. const mockResponse = wrapResponse(null)
  143. vi.mocked(request.post).mockResolvedValue(mockResponse)
  144. await adminDeleteCamera(1)
  145. expect(request.post).toHaveBeenCalledWith('/admin/cameras/delete', undefined, {
  146. params: { id: 1 }
  147. })
  148. })
  149. })
  150. describe('adminCheckCamera', () => {
  151. it('should call POST /admin/cameras/check', async () => {
  152. const mockResponse = wrapResponse(true)
  153. vi.mocked(request.post).mockResolvedValue(mockResponse)
  154. const result = await adminCheckCamera(1)
  155. expect(request.post).toHaveBeenCalledWith('/admin/cameras/check', undefined, {
  156. params: { id: 1 }
  157. })
  158. expect(result.data).toBe(true)
  159. })
  160. })
  161. })
  162. })