import { describe, it, expect, vi, beforeEach } from 'vitest' import { listMachines, getMachine, addMachine, updateMachine, deleteMachine } from '@/api/machine' import * as request from '@/utils/request' vi.mock('@/utils/request', () => ({ get: vi.fn(), post: vi.fn() })) describe('Machine API', () => { beforeEach(() => { vi.clearAllMocks() }) describe('listMachines', () => { it('should call GET /admin/machines/list', async () => { const mockResponse = { code: 200, message: 'success', data: [ { id: 1, machineId: 'machine-001', name: '测试机器1', location: '一楼', description: '测试用', enabled: true, cameraCount: 2, createdAt: '2024-01-01 00:00:00', updatedAt: '2024-01-01 00:00:00' } ] } vi.mocked(request.get).mockResolvedValue(mockResponse) const result = await listMachines() expect(request.get).toHaveBeenCalledWith('/admin/machines/list') expect(result.code).toBe(200) expect(result.data).toHaveLength(1) expect(result.data[0].machineId).toBe('machine-001') }) }) describe('getMachine', () => { it('should call GET /admin/machines/detail with id', async () => { const mockResponse = { code: 200, message: 'success', data: { id: 1, machineId: 'machine-001', name: '测试机器1', location: '一楼', description: '测试用', enabled: true, cameraCount: 2 } } vi.mocked(request.get).mockResolvedValue(mockResponse) const result = await getMachine(1) expect(request.get).toHaveBeenCalledWith('/admin/machines/detail', { id: 1 }) expect(result.code).toBe(200) expect(result.data.id).toBe(1) }) }) describe('addMachine', () => { it('should call POST /admin/machines/add with data', async () => { const mockResponse = { code: 200, message: '新增成功', data: { id: 1, machineId: 'machine-002', name: '新机器', location: '二楼', description: '新增测试', enabled: true, cameraCount: 0 } } vi.mocked(request.post).mockResolvedValue(mockResponse) const addData = { machineId: 'machine-002', name: '新机器', location: '二楼', description: '新增测试' } const result = await addMachine(addData) expect(request.post).toHaveBeenCalledWith('/admin/machines/add', addData) expect(result.code).toBe(200) expect(result.data.machineId).toBe('machine-002') }) }) describe('updateMachine', () => { it('should call POST /admin/machines/update with data', async () => { const mockResponse = { code: 200, message: '修改成功', data: { id: 1, machineId: 'machine-001', name: '更新后名称', location: '三楼', description: '已更新', enabled: false, cameraCount: 2 } } vi.mocked(request.post).mockResolvedValue(mockResponse) const updateData = { id: 1, name: '更新后名称', location: '三楼', description: '已更新', enabled: false } const result = await updateMachine(updateData) expect(request.post).toHaveBeenCalledWith('/admin/machines/update', updateData) expect(result.code).toBe(200) expect(result.data.name).toBe('更新后名称') }) }) describe('deleteMachine', () => { it('should call POST /admin/machines/delete with id', async () => { const mockResponse = { code: 200, message: '删除成功', data: null } vi.mocked(request.post).mockResolvedValue(mockResponse) const result = await deleteMachine(1) expect(request.post).toHaveBeenCalledWith('/admin/machines/delete', undefined, { params: { id: 1 } }) expect(result.code).toBe(200) }) it('should handle delete failure', async () => { const mockResponse = { code: 400, message: '该机器下存在摄像头,无法删除', data: null } vi.mocked(request.post).mockResolvedValue(mockResponse) const result = await deleteMachine(1) expect(result.code).toBe(400) expect(result.message).toBe('该机器下存在摄像头,无法删除') }) }) })