Files
dacli/tests/unit/bs54_guard.test.ts
T
ClaudeandClaude 216a02f7f1 fix(dacli): BS-56/BS-42/enableFlow/JNPF 4 步法修复 (v0.11h+, 2026-06-28)
按 CLAUDE.md §2.7.1 4 步法修 4 个 dacli 盲点:

1. BS-56 增强: enCode 禁下划线 — 错误信息明确 + 自动建议别名
   - 新增 src/commands/_encode_validator.ts (统一 ENCODE_REGEXP/FULLNAME_REGEXP + validateEnCode + suggestEnCode)
   - form/dict/menu/role 4 个 _validate.ts 切换到共享 helper
   - 错误从'只能输入英文、数字和小数点且小数点不能放在首尾'升级为
     'BS-56: enCode 只能含英文/数字/小数点, 不能含下划线/连字符/中文. 当前 enCode=X 非法. 建议改成: --enCode=Y (自动去下划线/连字符)'

2. BS-42 升级: dm 系统列冲突清晰提示
   - 加 groupMap 给每个系统列别称 (f_description=描述, f_create_user=创建人 ID 等)
   - 消息包含改名建议 (f_desc/f_remark/f_brief/f_summary) 和 code='BS-42'

3. enableFlow 兜底: BS-EFLOW-NOWF guard
   - 在 _blindspot_guards.ts 加新 guard (WARN 级)
   - enableFlow=1 但 workflow list=0 时给明确警告
   - AppContext 加 workflowList 字段, loadAppContext 调 /workflow/Engine/list 收集
   - types.ts/client.ts/context.ts 全链路补 workflowList

4. JNPF 自检: 新增 core/_health_check.ts
   - app init/status 前置 HEAD baseUrl/ 健康检查 (3s timeout)
   - 不通时给 nohup 重启指引
   - JSON 输出 health: {ok, status, error, durationMs}, 不通时退出码 1

验证 (workSystem/jxcj1 PASS):
- BS-56 form/dict/menu 3 命令输出新格式错误 + 别名建议
- BS-42 dm create f_description 输出 BS-42 清晰信息
- JNPF 自检输出 '✓ JNPF 可达: http://nginx/api/ (status=200, 27ms)'

文件 (16 改 + 2 新增):
- src/commands/_encode_validator.ts (新增)
- src/core/_health_check.ts (新增)
- src/core/types.ts (workflowList)
- src/core/client.ts (resolveDbLinkId 兜底)
- src/commands/_blindspot_guards.ts (BS-EFLOW-NOWF)
- src/commands/app/context.ts (tryFetchWorkflows)
- src/commands/app/init.ts (前置健康检查)
- src/commands/app/status.ts (前置健康检查 + JSON 输出)
- src/commands/form/_validate.ts (共享 ENCODE_REGEXP)
- src/commands/dm/_validate.ts (BS-42 升级)
- src/commands/dict/_validate.ts (共享 + BS-56 增强)
- src/commands/menu/_validate.ts (共享 + BS-56 增强)
- src/commands/role/_validate.ts (共享 + BS-56 增强)
- src/commands/form/codegen.ts (workflowList 兜底)
- tests/unit/{blindspot_guards,bs54_guard,dbLinkId,verify_option}.test.ts (workflowList 兜底)

不违反红线:
- ❌ 不动 web/uniapp/jnpf 源码 (CLAUDE.md §15)
- ❌ 不绕过 dacli
- ✅ 一轮一提交 (CLAUDE.md §14)
- ✅ npx tsc --noEmit 在改动文件无新错

Co-Authored-By: Claude <[email protected]>
2026-06-28 23:50:58 +08:00

125 lines
4.4 KiB
TypeScript

/**
* tests/unit/bs54_guard.test.ts — BS-54 guard 单测
*
* BS-54: select 字段缺 dataType + options/dictionaryType/propsUrl 时, 浏览器层下拉框空白;
* relationForm 字段缺 modelId 时, 弹窗直接 return 不渲染 (RelationForm.vue:201).
*
* guard 触发场景: form create --fields 直接传 select/relationForm 但没补字段
* (FieldBuilder.build() 已默认补齐, 但 fields 直接传 JSON 时不会走 FieldBuilder)
*
* 测试:
* 1. select 字段无 dataType + 无 options → ERROR
* 2. select 字段有 dataType='dictionary' + dictionaryType='' → ERROR (空字典)
* 3. select 字段 dataType='static' + 2 项 options → 不触发
* 4. select 字段 dataType='dynamic' + propsUrl='api/x' → 不触发
* 5. relationForm 字段无 modelId → ERROR
* 6. relationForm 字段 modelId='' (空字符串) → ERROR
* 7. relationForm 字段 modelId='jxcj_xxx' → 不触发
* 8. 普通 input 字段 → 不触发
*/
import { describe, it, expect } from 'vitest'
import { runGuards } from '../../src/commands/_blindspot_guards.ts'
import type { AppContext } from '../../src/core/types.ts'
const baseCtx: AppContext = {
appCode: 'test',
dbLinkId: '123456',
tokenStatus: 'valid',
existingEnCodes: ['test_form'],
unreleasedForms: [],
releasedForms: ['test_form'],
modifiedAfterPublishForms: [],
workflowList: [],
existingTables: ['test_table'],
menus: ['test_form'],
currentVersion: 'v0.10',
warnings: [],
}
describe('BS-54 guard — select/relationForm 字段必填校验', () => {
it('select 字段无 dataType + 无 options → ERROR', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_state',
__config__: { jnpfKey: 'select', label: '状态' },
}],
})
expect(issues.some(i => i.id === 'BS-54' && i.level === 'ERROR')).toBe(true)
})
it('select 字段 dataType="dictionary" + dictionaryType="" (空) → ERROR', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_state',
__config__: { jnpfKey: 'select', label: '状态', dataType: 'dictionary', dictionaryType: '' },
}],
})
expect(issues.some(i => i.id === 'BS-54' && i.level === 'ERROR')).toBe(true)
})
it('select 字段 dataType="static" + 2 项 options → 不触发', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_state',
__config__: { jnpfKey: 'select', label: '状态', dataType: 'static' },
options: [{ fullName: 'A', id: '1' }, { fullName: 'B', id: '2' }],
}],
})
expect(issues.some(i => i.id === 'BS-54')).toBe(false)
})
it('select 字段 dataType="dynamic" + propsUrl → 不触发', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_state',
__config__: { jnpfKey: 'select', label: '状态', dataType: 'dynamic', propsUrl: '/api/xxx' },
}],
})
expect(issues.some(i => i.id === 'BS-54')).toBe(false)
})
it('relationForm 字段无 modelId key → ERROR', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_team_id',
__config__: { jnpfKey: 'relationForm', label: '关联团队' },
}],
})
expect(issues.some(i => i.id === 'BS-54' && i.level === 'ERROR')).toBe(true)
})
it('relationForm 字段 modelId="" (空字符串占位) → ERROR', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_team_id',
__config__: { jnpfKey: 'relationForm', label: '关联团队', modelId: '' },
}],
})
expect(issues.some(i => i.id === 'BS-54' && i.level === 'ERROR')).toBe(true)
})
it('relationForm 字段 modelId="jxcj_team" → 不触发', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_team_id',
__config__: { jnpfKey: 'relationForm', label: '关联团队', modelId: 'jxcj_team' },
}],
})
expect(issues.some(i => i.id === 'BS-54')).toBe(false)
})
it('普通 input 字段 → 不触发', () => {
const issues = runGuards(baseCtx, {
fields: [{
__vModel__: 'f_name',
__config__: { jnpfKey: 'input', label: '姓名' },
}],
})
expect(issues.some(i => i.id === 'BS-54')).toBe(false)
})
it('BS-54 应在 BLINDSPOT_GUARDS 数组中存在', async () => {
const mod = await import('../../src/commands/_blindspot_guards.ts')
expect(mod.BLINDSPOT_GUARDS.some(g => g.id === 'BS-54')).toBe(true)
})
})