/** * deviceReadings + devices API 契约测试 */ import { describe, it, expect, vi, beforeEach } from 'vitest' const mockGet = vi.fn() const mockPost = vi.fn() const mockPut = vi.fn() const mockDelete = vi.fn() vi.mock('../client', () => ({ default: { get: (...args: unknown[]) => mockGet(...args), post: (...args: unknown[]) => mockPost(...args), put: (...args: unknown[]) => mockPut(...args), delete: (...args: unknown[]) => mockDelete(...args), }, })) import { deviceReadingApi } from './deviceReadings' import { deviceApi } from './devices' beforeEach(() => { vi.clearAllMocks() }) describe('deviceReadingApi', () => { const fakeRes = { data: { data: {} } } it('batchCreate 应调用 POST /health/patients/:id/device-readings/batch', async () => { mockPost.mockResolvedValue(fakeRes) const data = { device_id: 'dev-001', readings: [ { device_type: 'blood_pressure', values: { systolic: 130, diastolic: 85 }, measured_at: '2026-05-03T08:00:00Z' }, ], } await deviceReadingApi.batchCreate('p-001', data) expect(mockPost).toHaveBeenCalledWith('/health/patients/p-001/device-readings/batch', data) }) it('query 应调用 GET /health/patients/:id/device-readings 并剥离 patient_id', async () => { mockGet.mockResolvedValue(fakeRes) await deviceReadingApi.query({ patient_id: 'p-001', device_type: 'blood_pressure', hours: 24 }) expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/device-readings', { params: { device_type: 'blood_pressure', hours: 24 }, }) }) it('queryHourly 应调用 GET /health/patients/:id/device-readings/hourly', async () => { mockGet.mockResolvedValue(fakeRes) await deviceReadingApi.queryHourly({ patient_id: 'p-001', device_type: 'blood_pressure', days: 7 }) expect(mockGet).toHaveBeenCalledWith('/health/patients/p-001/device-readings/hourly', { params: { device_type: 'blood_pressure', days: 7 }, }) }) }) describe('deviceApi', () => { const fakeRes = { data: { data: {} } } it('listDevices 应调用 GET /health/devices 并传递查询参数', async () => { mockGet.mockResolvedValue(fakeRes) await deviceApi.listDevices({ patient_id: 'p-001', device_type: 'blood_pressure', page: 1, page_size: 10 }) expect(mockGet).toHaveBeenCalledWith('/health/devices', { params: { patient_id: 'p-001', device_type: 'blood_pressure', page: 1, page_size: 10 }, }) }) it('unbindDevice 应调用 DELETE /health/devices/:id 并在 body 传递 version', async () => { mockDelete.mockResolvedValue(fakeRes) await deviceApi.unbindDevice('dev-001', 2) expect(mockDelete).toHaveBeenCalledWith('/health/devices/dev-001', { data: { version: 2 }, }) }) })