/** * auth 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 authApi from './auth' beforeEach(() => { vi.clearAllMocks() }) describe('auth API', () => { const fakeRes = { data: { success: true, data: {} } } it('login 应调用 POST /auth/login 并传递用户名密码', async () => { mockPost.mockResolvedValue(fakeRes) await authApi.login({ username: 'admin', password: '123456' }) expect(mockPost).toHaveBeenCalledWith('/auth/login', { username: 'admin', password: '123456', }) }) it('logout 应调用 POST /auth/logout', async () => { mockPost.mockResolvedValue(undefined) await authApi.logout() expect(mockPost).toHaveBeenCalledWith('/auth/logout') }) it('changePassword 应调用 POST /auth/change-password', async () => { mockPost.mockResolvedValue(undefined) await authApi.changePassword('oldPass', 'newPass') expect(mockPost).toHaveBeenCalledWith('/auth/change-password', { current_password: 'oldPass', new_password: 'newPass', }) }) })