import { vi } from 'vitest' // template.spec.ts D20 验收:模板管理 API 契约(Mock 与后端 Day41/44 状态机对齐)—— // 列表过滤/报备(变量提取)/批量审核(已审跳过)/文件导入(去重+表头启发+非法计数)。 import { beforeEach, describe, expect, it } from 'vitest' import { smsApi } from '@/api/sms' vi.stubEnv('VITE_API_BASE_URL', 'http://localhost/api/v1') describe('模板管理 API(D20 Mock 契约)', () => { beforeEach(() => { localStorage.clear() }) it('列表:种子 3 条;status 过滤待审=1', async () => { const all = await smsApi.templateList({ page: 1, page_size: 50 }) expect(all.total).toBeGreaterThanOrEqual(3) const pending = await smsApi.templateList({ page: 1, page_size: 50, status: 0 }) expect(pending.list.every((t) => t.status === 0)).toBe(true) expect(pending.total).toBeGreaterThanOrEqual(1) }) it('报备:新建即待审+变量自动提取;编辑内容回待审', async () => { const created = await smsApi.templateCreate({ account_id: 1001, name: 'D20测试', content: '【店】{name}您好,{city}天气晴' }) expect(created.status).toBe(0) expect(created.variables.split(',').sort()).toEqual(['city', 'name']) // 编辑内容 → 回待审 await smsApi.templateUpdate(created.id, { content: '【店】{name}新内容' }) const list = await smsApi.templateList({ page: 1, page_size: 50 }) const row = list.list.find((t) => t.id === created.id) expect(row?.status).toBe(0) expect(row?.variables).toBe('name') }) it('批量审核:全过 ok=total;重放 ok=0(已审跳过);批量拒绝带原因', async () => { const c1 = await smsApi.templateCreate({ account_id: 1001, name: '批量1', content: '【店】A' }) const c2 = await smsApi.templateCreate({ account_id: 1001, name: '批量2', content: '【店】B' }) const res = await smsApi.templateBatchReview([c1.id, c2.id], true) expect(res).toMatchObject({ ok: 2, total: 2 }) // 重放:已审跳过 const again = await smsApi.templateBatchReview([c1.id, c2.id], true) expect(again.ok).toBe(0) // 单条拒绝带原因 const c3 = await smsApi.templateCreate({ account_id: 1001, name: '批量3', content: '【店】C' }) await smsApi.templateReview(c3.id, false, 'D20 拒绝样例') const after = await smsApi.templateList({ page: 1, page_size: 50, status: -1 }) expect(after.list.some((t) => t.id === c3.id && t.reason === 'D20 拒绝样例')).toBe(true) }) it('通过态不可删除(ISC:400);拒审态可删', async () => { const c = await smsApi.templateCreate({ account_id: 1001, name: '删除测试', content: '【店】删' }) await smsApi.templateReview(c.id, true) await expect(smsApi.templateRemove(c.id)).rejects.toMatchObject({ code: 'ISC:400' }) await smsApi.templateReview(c.id, false, '转拒') await expect(smsApi.templateRemove(c.id)).resolves.toBeTruthy() }) }) describe('号码文件导入 API(Day 44 Mock 契约)', () => { beforeEach(() => { localStorage.clear() }) /** * 注:导入走 multipart,jsdom 的 FormData 与 undici fetch/MSW interceptor 均存在序列化兼容坑 * (content-type 丢 boundary),故此处手工构造标准 multipart 体直打 MSW; * 浏览器真实路径(axios+XHR FormData)由人工走查 W 节覆盖,后端真实解析由 R 节 curl 冒烟覆盖。 */ async function importFile(name: string, content: string, type: string) { const boundary = '----vitest-day44' const body = [ `--${boundary}`, `Content-Disposition: form-data; name="file"; filename="${name}"`, `Content-Type: ${type}`, '', content, `--${boundary}--`, '', ].join('\r\n') const resp = await fetch('http://localhost/api/v1/files/numbers/import', { method: 'POST', headers: { 'content-type': `multipart/form-data; boundary=${boundary}` }, body, }) return { status: resp.status, body: (await resp.json()) as { code: string; msg: string; data?: Record } } } it('TXT:去重+非法计数;object_key 返回', async () => { const { body } = await importFile('n.txt', '13800138000\n13800138000\nbad\n13800138001', 'text/plain') expect(body.code).toBe('ISC:000') expect(body.data).toMatchObject({ valid: 2, duplicated: 1, invalid: 1 }) expect(body.data?.mobiles).toEqual(['13800138000', '13800138001']) expect(String(body.data?.object_key)).toMatch(/^numbers\//) }) it('CSV:表头启发跳过首行(header_skipped)', async () => { const { body } = await importFile('h.csv', '手机号,姓名\n13800138000,张三\n13800138001,李四', 'text/csv') expect(body.code).toBe('ISC:000') expect(body.data).toMatchObject({ header_skipped: true, valid: 2, invalid: 0 }) }) it('不支持格式/全非法拒绝(ISC:400)', async () => { const bad = await importFile('a.doc', 'x', 'application/msword') expect(bad.body.code).toBe('ISC:400') const none = await importFile('bad.txt', 'abc\ndef', 'text/plain') expect(none.body.code).toBe('ISC:400') }) })