/**
 * doctors 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 { doctorApi } from './doctors'

beforeEach(() => {
  vi.clearAllMocks()
})

describe('doctorApi', () => {
  const fakeRes = { data: { success: true, data: {} } }

  it('list 应调用 GET /health/doctors 并传递查询参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await doctorApi.list({ page: 1, page_size: 10, search: '王', department: '内科' })

    expect(mockGet).toHaveBeenCalledWith('/health/doctors', {
      params: { page: 1, page_size: 10, search: '王', department: '内科' },
    })
  })

  it('get 应调用 GET /health/doctors/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await doctorApi.get('d-001')

    expect(mockGet).toHaveBeenCalledWith('/health/doctors/d-001')
  })

  it('create 应调用 POST /health/doctors 并传递请求体', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { name: '王医生', department: '内科', title: '主任医师' }
    await doctorApi.create(req)

    expect(mockPost).toHaveBeenCalledWith('/health/doctors', req)
  })

  it('update 应调用 PUT /health/doctors/:id 并传递请求体含 version', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { title: '副主任医师', version: 1 }
    await doctorApi.update('d-001', req)

    expect(mockPut).toHaveBeenCalledWith('/health/doctors/d-001', req)
  })

  it('delete 应调用 DELETE /health/doctors/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await doctorApi.delete('d-001', 1)

    expect(mockDelete).toHaveBeenCalledWith('/health/doctors/d-001')
  })
})
