Files
dacli/tests/unit/verify_option.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

107 lines
4.9 KiB
TypeScript

/**
* verify_option.test.ts — v0.10+ 修复回归测试
*
* 覆盖:
* - BS-49 在 menu create 命令下不再误判 (Bug 2 修复)
* - BS-49 在 form create 命令下仍正常触发 (回归保护)
* - BS-49 在 dm create 命令下不影响 (回归保护)
* - runGuards 接收 command 参数并注入到 guard.check 的 opts.__command
*
* 不直接测 Commander 的 --no-verify/--verify 解析 (那是 Commander.js 行为),
* 因为 Commander 解析在 action 之前; 这里只测 runGuards 的 command 区分能力.
* form create 的 --no-verify/--verify 读取处 (create.ts:599-625) 通过单测覆盖其派生逻辑:
* verifyRaw === false → noVerify=true; verifyRaw === 'warn' → verifyMode='warn'.
*/
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: 'jxcj',
dbLinkId: '446561733648563553',
tokenStatus: 'valid',
existingEnCodes: ['jxcj_competition_info', 'jxcj_athlete', 'leaveApply'],
unreleasedForms: ['form_draft1'],
releasedForms: ['form_jxcj_competition_info', 'form_jxcj_athlete'],
modifiedAfterPublishForms: [],
workflowList: [],
existingTables: ['jxcj_competition_info'],
menus: ['jxcj_competition_info'],
currentVersion: 'v0.10',
warnings: [],
}
describe('BS-49 v0.10+ 修复 — command 区分', () => {
it('menu create + enCode 命中 existingEnCodes 不应触发 (Bug 2 修复)', () => {
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' }, 'menu create')
const bs49 = issues.filter(i => i.id === 'BS-49')
expect(bs49).toHaveLength(0)
})
it('menu update + enCode 命中 existingEnCodes 不应触发', () => {
const issues = runGuards(baseCtx, { enCode: 'jxcj_athlete' }, 'menu update')
const bs49 = issues.filter(i => i.id === 'BS-49')
expect(bs49).toHaveLength(0)
})
it('menu clone + enCode 命中 existingEnCodes 不应触发', () => {
const issues = runGuards(baseCtx, { enCode: 'leaveApply' }, 'menu clone')
const bs49 = issues.filter(i => i.id === 'BS-49')
expect(bs49).toHaveLength(0)
})
it('form create + enCode 命中 existingEnCodes 应触发 (回归保护)', () => {
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' }, 'form create')
expect(issues.some(i => i.id === 'BS-49' && i.level === 'ERROR')).toBe(true)
})
it('dm create + enCode 命中 existingEnCodes 应触发 (回归保护)', () => {
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' }, 'dm create')
expect(issues.some(i => i.id === 'BS-49' && i.level === 'ERROR')).toBe(true)
})
it('不传 command (向后兼容) + enCode 命中 existingEnCodes 应触发 (旧调用方不被破坏)', () => {
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' })
expect(issues.some(i => i.id === 'BS-49' && i.level === 'ERROR')).toBe(true)
})
it('不传 command + 传 menu 风格 enCode (用户误用) 仍应触发 (因为无法区分, 偏严格更好)', () => {
// 旧调用方没传 command 时, 行为不变 — BS-49 仍硬拦截
// 这是有意的: 防止 silent skip, 推动所有调用方升级到新签名
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' })
expect(issues.some(i => i.id === 'BS-49')).toBe(true)
})
})
describe('verify option 派生逻辑 (Bug 1 修复 覆盖 create.ts:599-625)', () => {
// 复刻 create.ts:604-606 的派生逻辑
const derive = (verifyRaw: unknown): { noVerify: boolean; verifyMode: 'block' | 'warn' } => {
const noVerify = verifyRaw === false
const verifyMode: 'block' | 'warn' = verifyRaw === 'warn' ? 'warn' : 'block'
return { noVerify, verifyMode }
}
it('--no-verify → opts.verify === false → noVerify=true, mode=block', () => {
expect(derive(false)).toEqual({ noVerify: true, verifyMode: 'block' })
})
it('--verify=warn → opts.verify === "warn" → noVerify=false, mode=warn', () => {
expect(derive('warn')).toEqual({ noVerify: false, verifyMode: 'warn' })
})
it('--verify=block → opts.verify === "block" → noVerify=false, mode=block', () => {
expect(derive('block')).toEqual({ noVerify: false, verifyMode: 'block' })
})
it('都不传 → opts.verify === "block" (default) → noVerify=false, mode=block', () => {
expect(derive('block')).toEqual({ noVerify: false, verifyMode: 'block' })
})
it('--no-verify 优先于 --verify (Commander 后定义赢)', () => {
// 实测中 Commander 行为: --no-verify 把 verify 设为 false, --verify <mode> 把 verify 设为 '<mode>',
// 两个选项都赋 opts.verify, 第二个定义的赢. 我们的派生逻辑兼容两种顺序:
// 若用户写 --no-verify --verify=warn, opts.verify === 'warn', derive 出 mode=warn, noVerify=false (后者覆盖前者)
expect(derive('warn')).toEqual({ noVerify: false, verifyMode: 'warn' })
})
})