Files
dacli/tests/e2e/step-machine/error-codes.test.ts
T
ssw b00bf967cc feat(d2-validation): v0.11e+ Phase D2 E2E + drift-check + coverage
新建 3 个 Phase D2 文件:
1. tests/e2e/step-machine/error-codes.test.ts (~100 行 vitest):
   - 7 个 describe 块, 18 个 it 用例
   - 测试 classify/format/aggregate/enhanceFromBackend/ALL_ERROR_CODES 5 大功能
   - 纯函数测试, 无副作用, 可直接 vitest run

2. src/commands/_drift_check.ts (~150 行):
   - buildDriftCheckCommand() 顶层命令
   - scanApiDrift/scanSchemaDrift/scanValidationDrift/scanErrorCodeDrift
   - 输出 drift diff 报告 (API/Schema/Validation/ErrorCode)
   - DRIFT_CHECK_CRON_CONFIG = '0 9 * * 1' (每周一 9 点)

3. src/commands/_coverage.ts (~170 行):
   - buildCoverageCommand() 顶层命令
   - scanCommand() 静态扫描命令文件, 输出 5 大机制覆盖
   - 输出总镜像率 + 命令级详情 + 优先级分组
   - 集成到 cli.ts 顶层命令

cli.ts 集成:
- import buildDriftCheckCommand/buildCoverageCommand
- addCommand 注册为顶层命令

cron 配置 (D2):
- '0 9 * * 1' 每周一跑 drift-check
- CI 集成: GH Actions 阻塞合并

E2E 自动化覆盖率:
- 18 个 test 用例覆盖 _error_codes.ts 5 大功能
- 后续 session 扩展: form/menu/dm/workflow/dict/role/di step machine 各 5+ 用例

镜像率 (dacli coverage 报告):
- 5 大机制: 校验 6% / 设计 7% / 错误反馈 8% / 授权 4% / 数据流 3%
- 注: 工具扫描所有 .ts (含 info/list/release), 真实镜像率更高 (创建/更新命令)
2026-06-27 03:27:42 +08:00

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)
}
})
})