本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的 dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。 - 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12) - 历史未带 ssw main 整体历史(避免大仓污染) - 此为新仓基线 commit,未来以本仓历史为主
110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
/**
|
|
* vitest test: _error_codes.ts (Phase D2 E2E 自动化)
|
|
*
|
|
* 测试 classify/format/aggregate/enhanceFromBackend 5 大功能
|
|
* 触发条件: 无副作用, 纯函数测试, 可直接 vitest run
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import { classify, format, aggregate, enhanceFromBackend, ALL_ERROR_CODES } from '../../../src/commands/_error_codes.ts'
|
|
|
|
describe('_error_codes classify()', () => {
|
|
it('提取 EXIST001 prefix', () => {
|
|
expect(classify('EXIST001 / 名称不能重复')).toBe('EXIST001')
|
|
expect(classify('名称不能重复')).toBe('EXIST001')
|
|
})
|
|
it('提取 EXIST002 (编码不能重复)', () => {
|
|
expect(classify('EXIST002 / 编码不能重复')).toBe('EXIST002')
|
|
})
|
|
it('提取 FA001 (数据不存在)', () => {
|
|
expect(classify('FA001 / 数据不存在')).toBe('FA001')
|
|
})
|
|
it('提取 VS401 (formData 空)', () => {
|
|
expect(classify('VS401 / 该模板内表单内容为空')).toBe('VS401')
|
|
})
|
|
it('未匹配返回 null', () => {
|
|
expect(classify('plain message without prefix')).toBe(null)
|
|
})
|
|
it('未识别 prefix 返回 OTHER', () => {
|
|
expect(classify('XX999 / 未知错误')).toBe('OTHER')
|
|
})
|
|
})
|
|
|
|
describe('_error_codes format()', () => {
|
|
it('EXIST002 格式化含 suggestion', () => {
|
|
const r = format('EXIST002', { entity: 'form', enCode: 'test' })
|
|
expect(r.code).toBe('EXIST002')
|
|
expect(r.category).toBe('duplicate')
|
|
expect(r.suggestion).toContain('test') // 替换 {module} 占位符
|
|
})
|
|
it('EXIST001 格式化含 category=duplicate', () => {
|
|
const r = format('EXIST001')
|
|
expect(r.category).toBe('duplicate')
|
|
})
|
|
it('VS401 格式化含 category=visual_dev', () => {
|
|
const r = format('VS401')
|
|
expect(r.category).toBe('visual_dev')
|
|
})
|
|
it('OTHER fallback', () => {
|
|
const r = format('OTHER')
|
|
expect(r.category).toBe('unknown')
|
|
})
|
|
})
|
|
|
|
describe('_error_codes aggregate()', () => {
|
|
it('空数组返回 total=0', () => {
|
|
const r = aggregate([])
|
|
expect(r.total).toBe(0)
|
|
expect(Object.keys(r.byCode).length).toBe(0)
|
|
})
|
|
it('按 code 分桶', () => {
|
|
const errors = [
|
|
{ field: 'enCode', message: 'm1', code: 'EXIST002' },
|
|
{ field: 'fullName', message: 'm2', code: 'EXIST001' },
|
|
{ field: 'enCode', message: 'm3', code: 'EXIST002' },
|
|
]
|
|
const r = aggregate(errors)
|
|
expect(r.total).toBe(3)
|
|
expect(r.byCode['EXIST002'].length).toBe(2)
|
|
expect(r.byCode['EXIST001'].length).toBe(1)
|
|
expect(r.byCategory.duplicate).toBe(3)
|
|
})
|
|
it('category 统计', () => {
|
|
const errors = [
|
|
{ field: 'f1', message: 'm1', code: 'EXIST001' }, // duplicate
|
|
{ field: 'f2', message: 'm2', code: 'FA001' }, // not_found
|
|
{ field: 'f3', message: 'm3', code: 'VS401' }, // visual_dev
|
|
]
|
|
const r = aggregate(errors)
|
|
expect(r.byCategory.duplicate).toBe(1)
|
|
expect(r.byCategory.not_found).toBe(1)
|
|
expect(r.byCategory.visual_dev).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('_error_codes enhanceFromBackend()', () => {
|
|
it('自动填 code 字段', () => {
|
|
const r = enhanceFromBackend('EXIST002 / 编码不能重复', 'enCode', { entity: 'form' })
|
|
expect(r.field).toBe('enCode')
|
|
expect(r.code).toBe('EXIST002')
|
|
expect(r.message).toContain('编码')
|
|
})
|
|
it('未识别 prefix code=undefined', () => {
|
|
const r = enhanceFromBackend('plain msg', 'field', {})
|
|
expect(r.code).toBe(undefined)
|
|
expect(r.message).toBe('plain msg')
|
|
})
|
|
})
|
|
|
|
describe('_error_codes ALL_ERROR_CODES 完整性', () => {
|
|
it('至少 30 个 codes', () => {
|
|
expect(Object.keys(ALL_ERROR_CODES).length).toBeGreaterThanOrEqual(30)
|
|
})
|
|
it('每个 code 含 category + suggestion', () => {
|
|
for (const [code, info] of Object.entries(ALL_ERROR_CODES)) {
|
|
expect(info.code).toBe(code)
|
|
expect(['duplicate', 'not_found', 'permission', 'invalid_state', 'db', 'workflow', 'visual_dev', 'unknown']).toContain(info.category)
|
|
expect(typeof info.suggestion).toBe('string')
|
|
expect(info.suggestion.length).toBeGreaterThan(5)
|
|
}
|
|
})
|
|
}) |