import { vi } from "vitest" import { beforeEach, describe, expect, it } from 'vitest' import { accountApi } from '@/api/account' vi.stubEnv('VITE_API_BASE_URL', 'http://localhost/api/v1') // msw 内存账号库经 handlers 维护(测试间状态共享,顺序用例) describe('账号 API(Mock 契约,对齐后端 Day 21+)', () => { beforeEach(() => { localStorage.clear() }) it('列表:默认 4 条(分页)', async () => { const page = await accountApi.list({ page: 1, page_size: 10 }) expect(page.total).toBeGreaterThanOrEqual(4) expect(page.list[0].username).toBe('admin') }) it('列表:username 过滤', async () => { const page = await accountApi.list({ page: 1, page_size: 10, username: 'customer' }) expect(page.list.length).toBe(1) expect(page.list[0].username).toBe('customer') }) it('开户:位掩码(5=sms|video),重复用户名拒绝', async () => { const created = await accountApi.create({ username: 'fe1-user', password: 'secret66', role: 'customer', business: 5, credit_balance: 200, }) expect(created.id).toBeGreaterThan(1000) await expect( accountApi.create({ username: 'fe1-user', password: 'x12345', role: 'customer', business: 1 }), ).rejects.toMatchObject({ code: 'ISC:400' }) }) it('状态切换 + 充值余额累加 + 流水', async () => { const page = await accountApi.list({ page: 1, page_size: 10, username: 'vip-shop' }) const acc = page.list[0] const before = acc.balance await accountApi.setStatus(acc.id, 0) const r = await accountApi.recharge(acc.id, 300, 'FE-1 测试') expect(r.balance).toBe(before + 300) const logs = await accountApi.balanceLogs(acc.id) expect(logs.list.some((l) => l.remark === 'FE-1 测试' && l.amount === 300)).toBe(true) }) it('删除:活跃账号(余额>0)业务拒绝 ISC:032', async () => { const page = await accountApi.list({ page: 1, page_size: 10, username: 'vip-shop' }) await expect(accountApi.remove(page.list[0].id)).rejects.toMatchObject({ code: 'ISC:032' }) }) it('角色列表 5 个内置;登录审计可按用户名过滤', async () => { const roles = await accountApi.roles() expect(roles.list.map((r) => r.code)).toContain('customer') const audit = await accountApi.loginAudit('hacker') expect(audit.list.length).toBe(1) expect(audit.list[0].ok).toBe(0) }) // Day46/W6 修复循环:详情端点契约(账号+业务配置;audit_mode 三态) it('详情:账号 1001 业务配置 business=sms/audit_mode=2(自动审核黑词);不存在 ISC:404', async () => { const d = await accountApi.detail(1001) expect(d.account.username).toBe('customer') expect(d.configs.length).toBe(1) expect(d.configs[0].business).toBe('sms') expect(d.configs[0].audit_mode).toBe(2) // 多业务账号(vip-shop business=5=sms|video)逐业务一行,sms=2/video=2 const d2 = await accountApi.detail(1002) expect(d2.configs.map((c) => c.business).sort()).toEqual(['sms', 'video']) await expect(accountApi.detail(99999)).rejects.toMatchObject({ code: 'ISC:404' }) }) })