130 lines
5.9 KiB
TypeScript
130 lines
5.9 KiB
TypeScript
import { vi } from 'vitest'
|
|
import { describe, expect, it } from 'vitest'
|
|
import type { Channel } from '@/api/channel'
|
|
import { channelApi } from '@/api/channel'
|
|
import {
|
|
emptyForm, toForm, applyTemplate, buildPayload, channelRules,
|
|
carrierToList, carrierFromList, nightToState, nightFromState,
|
|
} from '@/views/channel/channel-form'
|
|
|
|
vi.stubEnv('VITE_API_BASE_URL', 'http://localhost/api/v1')
|
|
|
|
/** D13 迭代(Day 31):通道表单视图模型/映射/校验纯函数 + 新字段 Mock 契约。 */
|
|
describe('通道表单 channel-form(Day 31)', () => {
|
|
it('emptyForm:按协议给出版本/端口缺省', () => {
|
|
expect(emptyForm('cmpp').protocol_version).toBe(20)
|
|
expect(emptyForm('sgip').protocol_version).toBe(12)
|
|
expect(emptyForm('smpp').protocol_version).toBe(34)
|
|
expect(emptyForm('smpp').port).toBe(2775)
|
|
expect(emptyForm('http').protocol_version).toBe(0)
|
|
expect(emptyForm().billing_length).toBe(70)
|
|
expect(emptyForm().heartbeat_interval).toBe(30)
|
|
})
|
|
|
|
it('toForm:密码不回显;版本缺省回填', () => {
|
|
const ch = { id: 9, name: 'x', code: 'C9', protocol: 'cmpp' } as Channel
|
|
const f = toForm(ch)
|
|
expect(f.password).toBe('')
|
|
expect(f.protocol_version).toBe(20) // 未返回时按协议回填
|
|
})
|
|
|
|
it('applyTemplate:模板预填保留名称/通道号/状态,其余继承', () => {
|
|
const base = { ...emptyForm('cmpp'), name: '新通道', code: 'NEW-1', status: 0 }
|
|
const tpl = {
|
|
id: 1, name: '移动全国', code: 'CM-001', protocol: 'cmpp' as const,
|
|
host: '10.0.1.10', port: 7890, username: 'go-sms', password: 'secret',
|
|
sp_id: '901234', heartbeat_interval: 45, carrier: 'cmcc', price: 0.045,
|
|
day_limit: 500000, night_limit: 1, block_regions: '新疆,西藏',
|
|
} as unknown as Channel
|
|
const f = applyTemplate(base, tpl)
|
|
expect(f.name).toBe('新通道') // 保留
|
|
expect(f.code).toBe('NEW-1')
|
|
expect(f.status).toBe(0)
|
|
expect(f.host).toBe('10.0.1.10') // 继承
|
|
expect(f.sp_id).toBe('901234')
|
|
expect(f.heartbeat_interval).toBe(45)
|
|
expect(f.carrier).toBe('cmcc')
|
|
expect(f.price).toBe(0.045)
|
|
expect(f.day_limit).toBe(500000)
|
|
expect(f.password).toBe('') // 密码不预填
|
|
})
|
|
|
|
it('运营商/夜间管控视图映射', () => {
|
|
expect(carrierToList('cmcc,cucc')).toEqual(['cmcc', 'cucc'])
|
|
expect(carrierToList('')).toEqual([])
|
|
expect(carrierFromList(['cmcc', 'ctcc'])).toBe('cmcc,ctcc')
|
|
expect(nightToState(0)).toBe('none')
|
|
expect(nightToState(100)).toBe('block') // 后端语义:>0 即禁发
|
|
expect(nightFromState('block')).toBe(1)
|
|
expect(nightFromState('none')).toBe(0)
|
|
})
|
|
|
|
it('buildPayload:编辑模式空密码剔除(=不修改)+白名单外字段剔除;新建全量', () => {
|
|
const f = { ...emptyForm('cmpp'), password: '', name: '编辑通道' }
|
|
const payload = buildPayload(f, true) as Record<string, unknown>
|
|
expect(payload.password).toBeUndefined()
|
|
// 后端 Update 白名单外字段必须剔除(否则 ISC:400「不允许更新字段」)。
|
|
for (const k of ['id', 'created_at', 'updated_at', 'upstream_name', 'protocol', 'code', 'status']) {
|
|
expect(payload[k]).toBeUndefined()
|
|
}
|
|
expect(payload.name).toBe('编辑通道') // 白名单内字段保留
|
|
const payloadCreate = buildPayload({ ...f, password: 'pw' }, false) as Record<string, unknown>
|
|
expect(payloadCreate.password).toBe('pw')
|
|
expect(payloadCreate.protocol).toBe('cmpp') // 新建需要 protocol
|
|
})
|
|
|
|
it('sp_id 校验器:超 6 字节拒绝(前后端双保险)', () => {
|
|
const rules = channelRules('cmpp')
|
|
const validator = rules.sp_id![1].validator as (r: unknown, v: string, cb: (e?: Error) => void) => void
|
|
let err: Error | undefined
|
|
validator({}, '1234567', (e) => { err = e })
|
|
expect(err?.message).toContain('6 字节')
|
|
validator({}, '901234', (e) => { err = e })
|
|
expect(err).toBeUndefined()
|
|
// cmpp 下必填
|
|
expect(rules.sp_id![0].required).toBe(true)
|
|
// http 下非必填
|
|
expect(channelRules('http').sp_id![0].required).toBe(false)
|
|
})
|
|
|
|
it('Mock 契约:CMPP3.0 创建被明确拒绝(不提供假支持)', async () => {
|
|
await expect(
|
|
channelApi.create({ name: 'v30', code: 'EXT-V30', protocol: 'cmpp', host: '10.0.0.9', protocol_version: 30 }),
|
|
).rejects.toMatchObject({ code: 'ISC:400' })
|
|
})
|
|
|
|
it('Mock 契约:Day 31 新字段 保存→单查 回显 100% 一致', async () => {
|
|
const c = await channelApi.create({
|
|
name: '全字段通道', code: 'EXT-001', protocol: 'cmpp', host: '10.0.0.8', port: 7890,
|
|
username: 'hw', password: 'pw', sp_id: '901234', protocol_version: 20,
|
|
heartbeat_interval: 45, reconnect_max: 60, service_id: 'MYSVC',
|
|
carrier: 'cmcc,cucc', billing_length: 67, payment_type: 'prepay_monthly',
|
|
billing_mode: 'success_unknown', price: 0.05, category: '验证码',
|
|
number_rule: 1, code_truncate: 3, ext_params: 'k=v', service_remark: '客服备注',
|
|
day_limit: 800000, night_limit: 1, block_regions: '新疆,西藏', black_keywords: '博彩,贷款',
|
|
})
|
|
const got = await channelApi.get(c.id)
|
|
expect(got.sp_id).toBe('901234')
|
|
expect(got.protocol_version).toBe(20)
|
|
expect(got.heartbeat_interval).toBe(45)
|
|
expect(got.reconnect_max).toBe(60)
|
|
expect(got.service_id).toBe('MYSVC')
|
|
expect(got.carrier).toBe('cmcc,cucc')
|
|
expect(got.billing_length).toBe(67)
|
|
expect(got.payment_type).toBe('prepay_monthly')
|
|
expect(got.billing_mode).toBe('success_unknown')
|
|
expect(got.price).toBe(0.05)
|
|
expect(got.category).toBe('验证码')
|
|
expect(got.number_rule).toBe(1)
|
|
expect(got.code_truncate).toBe(3)
|
|
expect(got.ext_params).toBe('k=v')
|
|
expect(got.service_remark).toBe('客服备注')
|
|
expect(got.day_limit).toBe(800000)
|
|
// 更新透传:改心跳/价格
|
|
await channelApi.update(c.id, { heartbeat_interval: 90, price: 0.06 })
|
|
const u = await channelApi.get(c.id)
|
|
expect(u.heartbeat_interval).toBe(90)
|
|
expect(u.price).toBe(0.06)
|
|
})
|
|
})
|