/**
 * roles 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 * as rolesApi from './roles'

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

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

  it('listRoles 应调用 GET /roles 并传递分页参数', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await rolesApi.listRoles(1, 10)

    expect(mockGet).toHaveBeenCalledWith('/roles', { params: { page: 1, page_size: 10 } })
  })

  it('getRole 应调用 GET /roles/:id', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await rolesApi.getRole('r-001')

    expect(mockGet).toHaveBeenCalledWith('/roles/r-001')
  })

  it('createRole 应调用 POST /roles', async () => {
    mockPost.mockResolvedValue(fakeRes)
    const req = { name: '医生', code: 'doctor', description: '医生角色' }
    await rolesApi.createRole(req)

    expect(mockPost).toHaveBeenCalledWith('/roles', req)
  })

  it('updateRole 应调用 PUT /roles/:id', async () => {
    mockPut.mockResolvedValue(fakeRes)
    const req = { name: '高级医生', version: 1 }
    await rolesApi.updateRole('r-001', req)

    expect(mockPut).toHaveBeenCalledWith('/roles/r-001', req)
  })

  it('deleteRole 应调用 DELETE /roles/:id', async () => {
    mockDelete.mockResolvedValue(undefined)
    await rolesApi.deleteRole('r-001')

    expect(mockDelete).toHaveBeenCalledWith('/roles/r-001')
  })

  it('assignPermissions 应调用 POST /roles/:id/permissions', async () => {
    mockPost.mockResolvedValue(undefined)
    await rolesApi.assignPermissions('r-001', ['p-1', 'p-2'])

    expect(mockPost).toHaveBeenCalledWith('/roles/r-001/permissions', { permission_ids: ['p-1', 'p-2'] })
  })

  it('getRolePermissions 应调用 GET /roles/:id/permissions', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await rolesApi.getRolePermissions('r-001')

    expect(mockGet).toHaveBeenCalledWith('/roles/r-001/permissions')
  })

  it('listPermissions 应调用 GET /permissions', async () => {
    mockGet.mockResolvedValue(fakeRes)
    await rolesApi.listPermissions()

    expect(mockGet).toHaveBeenCalledWith('/permissions')
  })
})
