Files
go-sms-V2/web/tests/channel-policy.spec.ts
T

118 lines
6.2 KiB
TypeScript

import { vi } from "vitest"
import { beforeEach, describe, expect, it } from 'vitest'
import { channelApi } from '@/api/channel'
import { policyApi } from '@/api/policy'
vi.stubEnv('VITE_API_BASE_URL', 'http://localhost/api/v1')
// D12-D16 验收:通道/通道组/策略中心/导流/报备(Mock 契约)
describe('通道与策略(D12-D16 Mock 契约)', () => {
beforeEach(() => {
localStorage.clear()
})
it('D12 通道:筛选/启停/测试/单查/CRUD', async () => {
// 筛选:协议 http 只有 1 条
const page = await channelApi.list({ page: 1, page_size: 10, protocol: 'http' })
expect(page.total).toBe(1)
expect(page.list[0].protocol).toBe('http')
// 单查回显(D13)
const ch = await channelApi.get(1)
expect(ch.code).toBe('CM-001')
// 测试:启用通道返回 msg 信息(真实后端需 mobile/content,Mock 兼容)
const t = await channelApi.test(1, { mobile: '13800001111', content: 'x' })
expect(t.ok).toBe(true)
expect(t.latency_ms).toBeGreaterThan(0)
// 停用通道不可测
await channelApi.update(3, { status: 0 })
await expect(channelApi.test(3, { mobile: '13800001111', content: 'x' })).rejects.toMatchObject({ code: 'ISC:400' })
// 创建:缺必填拒/code 重复拒
await expect(channelApi.create({ name: 'x', code: '', host: 'h' })).rejects.toMatchObject({ code: 'ISC:400' })
await expect(channelApi.create({ name: 'y', code: 'CM-001', host: 'h', protocol: 'http' })).rejects.toMatchObject({ code: 'ISC:400' })
// 创建→更新→停用→删除闭环
const c = await channelApi.create({ name: 'D12通道', code: 'D12-001', host: 'http://x', protocol: 'http' })
expect(c.status).toBe(1)
const u = await channelApi.update(c.id, { speed: 999 })
expect(u.speed).toBe(999)
await channelApi.update(c.id, { status: 0 })
await channelApi.remove(c.id)
})
it('D14 通道组:成员联通道名/权重和校验/排序持久化', async () => {
const groups = await channelApi.groupList()
const g0 = groups.list.find((g) => g.name === '主路由组')
expect(g0).toBeTruthy()
// 成员带 channel_name(联显)
expect(g0!.items!.some((i) => i.channel_name === '移动全国')).toBe(true)
// step3 权重和 = 7+3 = 10(7:3 比例)
const sum = g0!.items!.filter((i) => i.step === 3).reduce((a, i) => a + i.weight, 0)
expect(sum).toBe(10)
// 权重和将超 100 → 拒绝(10+91=101;此时 ch3 尚未入组)
await expect(
channelApi.groupItemAdd(g0!.id, { channel_id: 3, step: 3, weight: 91 }),
).rejects.toMatchObject({ code: 'ISC:400' })
// 排序持久化:新建两个通道加入 step2 → 交换 priority → 落库
const nc1 = await channelApi.create({ name: '排序甲', code: 'ORD-A', host: 'http://a', protocol: 'http' })
const nc2 = await channelApi.create({ name: '排序乙', code: 'ORD-B', host: 'http://b', protocol: 'http' })
const a = await channelApi.groupItemAdd(g0!.id, { channel_id: nc1.id, step: 2, priority: 10 })
const b = await channelApi.groupItemAdd(g0!.id, { channel_id: nc2.id, step: 2, priority: 5 })
await channelApi.groupItemUpdate(g0!.id, a.id, { priority: 5 })
await channelApi.groupItemUpdate(g0!.id, b.id, { priority: 10 })
const g1 = (await channelApi.groupList()).list.find((g) => g.id === g0!.id)
const pa = g1!.items!.find((i) => i.id === a.id)!.priority
const pb = g1!.items!.find((i) => i.id === b.id)!.priority
expect(pa).toBe(5); expect(pb).toBe(10)
// 清理(含新建通道)
await channelApi.groupItemRemove(g0!.id, a.id)
await channelApi.groupItemRemove(g0!.id, b.id)
await channelApi.update(nc1.id, { status: 0 })
await channelApi.update(nc2.id, { status: 0 })
await channelApi.remove(nc1.id)
await channelApi.remove(nc2.id)
// 非空组拒删
await expect(channelApi.groupRemove(g0!.id)).rejects.toMatchObject({ code: 'ISC:400' })
})
it('D15 策略中心:黑名单批量导入/频次/码号/关键字', async () => {
// 批量导入:2 新 + 1 重复 + 1 非法 → imported=2
const r = await policyApi.blacklistImport({ mobiles: ['13611112222', '13633334444', '13700000001', 'abc'] })
expect(r.imported).toBe(2)
// 单加:格式拒
await expect(policyApi.blacklistAdd({ mobile: '123' })).rejects.toMatchObject({ code: 'ISC:400' })
// 频次:max_count<1 拒 → 创建 → 停用
await expect(policyApi.frequencyCreate({ dim: 1, window_unit: 1, max_count: 0 })).rejects.toMatchObject({ code: 'ISC:400' })
const f = await policyApi.frequencyCreate({ dim: 2, window_unit: 2, max_count: 8 })
expect(f.max_count).toBe(8)
const fOff = await policyApi.frequencyUpdate(f.id, { status: 0 })
expect(fOff.status).toBe(0)
// 码号:缺通道拒 → 添加 → 删除
await expect(policyApi.blockedCodeAdd({ channel_id: 0, code: 'X' })).rejects.toMatchObject({ code: 'ISC:400' })
const bc = await policyApi.blockedCodeAdd({ channel_id: 1, code: '10657_BAD99' })
await policyApi.blockedCodeRemove(bc.id)
// 关键字:级别筛选
const kw = await policyApi.keywords({ page: 1, page_size: 50, level: 1 })
expect(kw.list.every((k) => k.level === 1)).toBe(true)
})
it('D16 报备:流转状态机(待审→通过/驳回→重提)', async () => {
// 非法类型拒
await expect(policyApi.reportCreate({ type: 'hack' as 'sign', content: 'x' })).rejects.toMatchObject({ code: 'ISC:400' })
// 提报 → 待审
const r = await policyApi.reportCreate({ type: 'sign', content: '【D16验证】' })
expect(r.status).toBe(0)
// 通过 → 终态:再审核拒
const ok1 = await policyApi.reportReview(r.id, true)
expect(ok1.status).toBe(1)
await expect(policyApi.reportReview(r.id, true)).rejects.toMatchObject({ code: 'ISC:400' })
// 驳回流转:提报 → 驳回 → 重提 → 通过
const r2 = await policyApi.reportCreate({ type: 'code', content: '10690999' })
const rej = await policyApi.reportReview(r2.id, false)
expect(rej.status).toBe(2)
const re = await policyApi.reportResubmit(r2.id)
expect(re.status).toBe(0)
// 非驳回态重提拒
await policyApi.reportReview(r2.id, true)
await expect(policyApi.reportResubmit(r2.id)).rejects.toMatchObject({ code: 'ISC:400' })
})
})