import { describe, it, expect, beforeEach, vi } from 'vitest'; import '../helpers/mock-api'; import { api } from '@/services/request'; import { listConsultations, getSession, listMessages, sendMessage, markSessionRead, } from '@/services/consultation'; describe('consultation service', () => { beforeEach(() => { vi.clearAllMocks(); }); describe('listConsultations', () => { it('calls api.get with correct path', async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 }); await listConsultations(); expect(api.get).toHaveBeenCalledWith('/health/consultation-sessions', undefined); }); it('passes pagination params', async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 }); await listConsultations({ page: 2, page_size: 10 }); expect(api.get).toHaveBeenCalledWith('/health/consultation-sessions', { page: 2, page_size: 10, }); }); }); describe('getSession', () => { it('calls api.get with session id', async () => { const mock = { id: 's-1', status: 'active' } as any; vi.mocked(api.get).mockResolvedValueOnce(mock); const result = await getSession('s-1'); expect(api.get).toHaveBeenCalledWith('/health/consultation-sessions/s-1'); expect(result).toEqual(mock); }); }); describe('listMessages', () => { it('calls api.get with session id and params', async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 }); await listMessages('s-1', { page: 1, page_size: 20 }); expect(api.get).toHaveBeenCalledWith( '/health/consultation-sessions/s-1/messages', { page: 1, page_size: 20 }, ); }); it('passes after_id for incremental loading', async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: [], total: 0 }); await listMessages('s-1', { after_id: 'm-50' }); expect(api.get).toHaveBeenCalledWith( '/health/consultation-sessions/s-1/messages', expect.objectContaining({ after_id: 'm-50' }), ); }); }); describe('sendMessage', () => { it('calls api.post with session_id, content_type and content', async () => { const mockMsg = { id: 'm-1', content: 'hello' } as any; vi.mocked(api.post).mockResolvedValueOnce(mockMsg); const result = await sendMessage('s-1', 'hello'); expect(api.post).toHaveBeenCalledWith('/health/consultation-messages', { session_id: 's-1', content_type: 'text', content: 'hello', }); expect(result).toEqual(mockMsg); }); it('supports custom content_type', async () => { vi.mocked(api.post).mockResolvedValueOnce({} as any); await sendMessage('s-1', '', 'html'); expect(api.post).toHaveBeenCalledWith('/health/consultation-messages', { session_id: 's-1', content_type: 'html', content: '', }); }); }); describe('markSessionRead', () => { it('calls api.put with session id', async () => { vi.mocked(api.put).mockResolvedValueOnce(undefined); await markSessionRead('s-1'); expect(api.put).toHaveBeenCalledWith('/health/consultation-sessions/s-1/read'); }); }); });