47 lines
2.4 KiB
TypeScript
47 lines
2.4 KiB
TypeScript
// personal.spec.ts Day 42(D18 验收):变量校验({name} 规则)+模板套用渲染+预检 API 载荷。
|
|
import { describe, expect, it } from 'vitest'
|
|
import { extractVariables, isValidMobile, renderTemplate, smsLength } from '../src/utils/sms'
|
|
import type { BlockItem } from '../src/api/sms'
|
|
|
|
describe('D18 变量校验({name} 规则,与后端 variableRe 同源)', () => {
|
|
it('提取模板变量:字母开头/字母数字下划线,去重保序', () => {
|
|
expect(extractVariables('【云】{name}您好,订单{order_id}已发,{name}')).toEqual(['name', 'order_id'])
|
|
expect(extractVariables('无占位符')).toEqual([])
|
|
expect(extractVariables('{1abc} {_x}')).toEqual([]) // 数字/下划线开头不计
|
|
})
|
|
it('手机号校验:1[3-9] 共 11 位', () => {
|
|
expect(isValidMobile('13800138000')).toBe(true)
|
|
expect(isValidMobile('19912345678')).toBe(true)
|
|
expect(isValidMobile('12345678901')).toBe(false) // 12 开头
|
|
expect(isValidMobile('1380013800')).toBe(false) // 10 位
|
|
expect(isValidMobile('')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('D18 模板套用预览(与后端 service/template Render 同源)', () => {
|
|
const tpl = '【云端科技】{name}您好,会员日 {off} 折,拒收请回复R'
|
|
it('变量替换渲染', () => {
|
|
expect(renderTemplate(tpl, { name: '张三', off: '5' })).toBe('【云端科技】张三您好,会员日 5 折,拒收请回复R')
|
|
})
|
|
it('缺参保留占位符(服务端拒绝路径另有 400;前端预览可见未填项)', () => {
|
|
expect(renderTemplate(tpl, { name: '李四' })).toBe('【云端科技】李四您好,会员日 {off} 折,拒收请回复R')
|
|
})
|
|
it('渲染后拆分计费(签名+正文一体口径)', () => {
|
|
const out = renderTemplate('【云】{v}', { v: '汉'.repeat(66) }) // 3+66=69 字=138B
|
|
expect(smsLength(out)).toEqual({ chars: 69, segments: 2 }) // 决策②:138B>134B 有效载荷,上游实付 2 片
|
|
})
|
|
})
|
|
|
|
describe('D18 预检 API(POST /send/precheck,Day 42 拦截器链 DryRun)', () => {
|
|
it('api 定义存在且路径正确(请求结构由 openapi 契约锁定)', async () => {
|
|
const mod = await import('../src/api/sms')
|
|
expect(typeof mod.smsApi.precheck).toBe('function')
|
|
// 预检响应结构(拦截项字段)
|
|
const item: BlockItem = {
|
|
mobile: '13800138000', interceptor: 'blacklist', code: 'ISC:006', msg: '黑名单号码',
|
|
}
|
|
expect(item.interceptor).toBe('blacklist')
|
|
expect(item.code).toBe('ISC:006')
|
|
})
|
|
})
|