按 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]>
311 lines
12 KiB
TypeScript
311 lines
12 KiB
TypeScript
/**
|
|
* blindspot_guards.test.ts — _blindspot_guards.ts 单测 (P2.1)
|
|
*
|
|
* 覆盖:
|
|
* - 22 条 guard 全部存在 (BS-1/4/5/7/8/14/15b/28/32b/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51)
|
|
* - 关键 ERROR guard 触发 (BS-48/49/47/50/4/1/7/38/39/8/5/32b/51)
|
|
* - 正常场景 0 ERROR
|
|
* - 占位符插值 (${enCode}/${table}/${formId})
|
|
* - 容错: ctx=null 不抛
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import { BLINDSPOT_GUARDS, runGuards, hasErrors, formatIssues, type BlindspotIssue } from '../../src/commands/_blindspot_guards.ts'
|
|
import type { AppContext } from '../../src/core/types.ts'
|
|
|
|
const baseCtx: AppContext = {
|
|
appCode: 'tds',
|
|
dbLinkId: '446561733648563553',
|
|
tokenStatus: 'valid',
|
|
existingEnCodes: ['leaveApply', 'project', 'tds_leave', 'f_description_conflict'],
|
|
unreleasedForms: ['form_draft1'],
|
|
releasedForms: ['form_leaveApply', 'form_project', 'form_tds_leave'],
|
|
modifiedAfterPublishForms: [],
|
|
workflowList: [],
|
|
existingTables: ['tds_leave', 'tds_project'],
|
|
menus: ['leaveApply', 'project'],
|
|
currentVersion: 'v0.9',
|
|
warnings: [],
|
|
}
|
|
|
|
describe('blindspot guards — 完整性', () => {
|
|
it('guard 数量 ≥ 22 (高频盲点覆盖)', () => {
|
|
expect(BLINDSPOT_GUARDS.length).toBeGreaterThanOrEqual(22)
|
|
})
|
|
|
|
it('guard id 唯一', () => {
|
|
const ids = BLINDSPOT_GUARDS.map(g => g.id)
|
|
expect(new Set(ids).size).toBe(ids.length)
|
|
})
|
|
|
|
it('必备 id 都存在 (BS-1/4/5/7/8/14/15b/28/38/39/47/48/49/50/51)', () => {
|
|
const ids = BLINDSPOT_GUARDS.map(g => g.id)
|
|
const required = ['BS-1', 'BS-4', 'BS-5', 'BS-7', 'BS-8', 'BS-14', 'BS-15b', 'BS-28', 'BS-38', 'BS-39', 'BS-47', 'BS-48', 'BS-49', 'BS-50', 'BS-51']
|
|
for (const r of required) {
|
|
expect(ids).toContain(r)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('blindspot guards — 关键 ERROR 触发', () => {
|
|
it('BS-48: dbLinkId="0" 应触发', () => {
|
|
const issues = runGuards(baseCtx, { dbLinkId: '0' })
|
|
expect(issues.some(i => i.id === 'BS-48' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-48: type=4 + dbLinkId="0" 不应触发 (视图允许)', () => {
|
|
const issues = runGuards(baseCtx, { dbLinkId: '0', type: 4 })
|
|
expect(issues.some(i => i.id === 'BS-48')).toBe(false)
|
|
})
|
|
|
|
it('BS-48: dbLinkId="default" 应触发 (v0.11 别名拦截)', () => {
|
|
const issues = runGuards(baseCtx, { dbLinkId: 'default' })
|
|
expect(issues.some(i => i.id === 'BS-48' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-48: dbLinkId="DEFAULT" (大写) 应触发 (case-insensitive)', () => {
|
|
const issues = runGuards(baseCtx, { dbLinkId: 'DEFAULT' })
|
|
expect(issues.some(i => i.id === 'BS-48' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-48: 显式有效 dbLinkId 不应触发', () => {
|
|
const issues = runGuards(baseCtx, { dbLinkId: '2068901937849257984' })
|
|
expect(issues.some(i => i.id === 'BS-48')).toBe(false)
|
|
})
|
|
|
|
it('BS-49: enCode 重复应触发', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'leaveApply' })
|
|
expect(issues.some(i => i.id === 'BS-49' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-47: dict fullName 重复应触发', () => {
|
|
const issues = runGuards(baseCtx, { fullName: 'leaveApply' })
|
|
expect(issues.some(i => i.id === 'BS-47' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-50: menu create --formId 不可见 form 应触发', () => {
|
|
const issues = runGuards(baseCtx, { formId: '999999999' })
|
|
expect(issues.some(i => i.id === 'BS-50' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-4: form 未发布就绑菜单应触发', () => {
|
|
const issues = runGuards(baseCtx, { formId: 'form_draft1' })
|
|
expect(issues.some(i => i.id === 'BS-4' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-1: 业务表 + dbLinkId="0" 应触发', () => {
|
|
const issues = runGuards(baseCtx, { table: 'tds_xxx', dbLinkId: '0' })
|
|
expect(issues.some(i => i.id === 'BS-1' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-7: type=2 + enableFlow=true 应触发', () => {
|
|
const issues = runGuards(baseCtx, { type: 2, enableFlow: true })
|
|
expect(issues.some(i => i.id === 'BS-7' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-7: type=3 + enableFlow=true 应触发', () => {
|
|
const issues = runGuards(baseCtx, { type: 3, enableFlow: '1' })
|
|
expect(issues.some(i => i.id === 'BS-7' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-38: 业务字段含 f_description 应触发', () => {
|
|
const issues = runGuards(baseCtx, { fields: [{ field: 'f_description', fieldName: '描述' }] })
|
|
expect(issues.some(i => i.id === 'BS-38' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-39: 业务字段含 f_version 应触发', () => {
|
|
const issues = runGuards(baseCtx, { fields: [{ field: 'f_version', __vModel__: 'f_version' }] })
|
|
expect(issues.some(i => i.id === 'BS-39' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-8: token expired 应触发', () => {
|
|
const ctx: AppContext = { ...baseCtx, tokenStatus: 'expired' }
|
|
const issues = runGuards(ctx, {})
|
|
expect(issues.some(i => i.id === 'BS-8' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-5: AppContext 缺 existingEnCodes 应触发', () => {
|
|
const ctx: AppContext = { ...baseCtx, existingEnCodes: [] }
|
|
const issues = runGuards(ctx, {})
|
|
expect(issues.some(i => i.id === 'BS-5' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-32b: 依赖 create 响应 id 应触发', () => {
|
|
const issues = runGuards(baseCtx, { expectResponseId: true })
|
|
expect(issues.some(i => i.id === 'BS-32b' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('BS-51: 简化 columnList (fields+无 tables) 应触发', () => {
|
|
const issues = runGuards(baseCtx, { fields: [{ field: 'f_name' }] })
|
|
expect(issues.some(i => i.id === 'BS-51' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('blindspot guards — WARN 触发', () => {
|
|
it('BS-28: --autoRelease bare flag 应触发 WARN', () => {
|
|
const issues = runGuards(baseCtx, { autoRelease: true })
|
|
expect(issues.some(i => i.id === 'BS-28' && i.level === 'WARN')).toBe(true)
|
|
})
|
|
|
|
it('BS-15b: 缺系统列应触发 WARN', () => {
|
|
const issues = runGuards(baseCtx, { fields: [{ field: 'f_name', fieldName: '名称' }] })
|
|
expect(issues.some(i => i.id === 'BS-15b' && i.level === 'WARN')).toBe(true)
|
|
})
|
|
|
|
it('BS-14: type=4 + autoRelease 应触发 WARN', () => {
|
|
const issues = runGuards(baseCtx, { type: 4, autoRelease: true })
|
|
expect(issues.some(i => i.id === 'BS-14' && i.level === 'WARN')).toBe(true)
|
|
})
|
|
|
|
it('BS-37: form 已发布但未挂菜单应触发 WARN', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'tds_leave' })
|
|
// tds_leave 在 releasedForms 但不在 menus → 应触发
|
|
expect(issues.some(i => i.id === 'BS-37' && i.level === 'WARN')).toBe(true)
|
|
})
|
|
|
|
it('BS-44: type=1 不传 tables 应触发 WARN', () => {
|
|
const issues = runGuards(baseCtx, { type: 1 })
|
|
expect(issues.some(i => i.id === 'BS-44' && i.level === 'WARN')).toBe(true)
|
|
})
|
|
|
|
it('BS-43: v0.6.x 版本 form release 应触发 WARN', () => {
|
|
const ctx: AppContext = { ...baseCtx, currentVersion: 'v0.6.5' }
|
|
const issues = runGuards(ctx, {})
|
|
expect(issues.some(i => i.id === 'BS-43' && i.level === 'WARN')).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('blindspot guards — 正常场景不误报', () => {
|
|
it('标准 form create (type=1, webType=2, 业务库, 唯一 enCode, 带 fields+tables) 应 0 ERROR', () => {
|
|
const issues = runGuards(baseCtx, {
|
|
enCode: 'new_form',
|
|
fullName: '新表单',
|
|
dbLinkId: '446561733648563553',
|
|
table: 'tds_new_form',
|
|
type: 1,
|
|
webType: 2,
|
|
fields: [
|
|
{ field: 'f_id', __vModel__: 'f_id' },
|
|
{ field: 'f_tenant_id', __vModel__: 'f_tenant_id' },
|
|
{ field: 'f_name', __vModel__: 'f_name', __config__: { jnpfKey: 'input', label: '名称' } },
|
|
],
|
|
tables: [{ table: 'tds_new_form', tableName: '新表单', fields: [{ field: 'f_id' }, { field: 'f_tenant_id' }, { field: 'f_name' }] }],
|
|
})
|
|
const errors = issues.filter(i => i.level === 'ERROR')
|
|
expect(errors).toHaveLength(0)
|
|
})
|
|
|
|
it('空 opts 应 0 ERROR (ctx 有效, 触发依赖 ctx 的 guard)', () => {
|
|
const issues = runGuards(baseCtx, {})
|
|
const errors = issues.filter(i => i.level === 'ERROR')
|
|
expect(errors).toHaveLength(0)
|
|
})
|
|
})
|
|
|
|
describe('blindspot guards — 占位符插值', () => {
|
|
it('BS-49 message 应含 enCode 实际值', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'leaveApply' })
|
|
const issue = issues.find(i => i.id === 'BS-49')
|
|
expect(issue?.message).toContain('leaveApply')
|
|
})
|
|
|
|
it('BS-1 message 应含 table 实际值', () => {
|
|
const issues = runGuards(baseCtx, { table: 'tds_x', dbLinkId: '0' })
|
|
const issue = issues.find(i => i.id === 'BS-1')
|
|
expect(issue?.message).toContain('tds_x')
|
|
})
|
|
|
|
it('BS-50 message 应含 formId 实际值', () => {
|
|
const issues = runGuards(baseCtx, { formId: '999999' })
|
|
const issue = issues.find(i => i.id === 'BS-50')
|
|
expect(issue?.message).toContain('999999')
|
|
})
|
|
})
|
|
|
|
describe('blindspot guards — 容错', () => {
|
|
it('ctx=null 不应抛异常', () => {
|
|
expect(() => runGuards(null, { enCode: 'X' })).not.toThrow()
|
|
})
|
|
|
|
it('ctx=null + 触发 ctx 依赖的 guard 应被跳过 (不抛)', () => {
|
|
const issues = runGuards(null, { enCode: 'X' })
|
|
expect(Array.isArray(issues)).toBe(true)
|
|
})
|
|
|
|
it('opts=null 应不抛', () => {
|
|
expect(() => runGuards(baseCtx)).not.toThrow()
|
|
})
|
|
|
|
it('guard check 内部抛错应被捕获 (容错单个 guard)', () => {
|
|
// 用一个会 throw 的 guard 测试容错
|
|
// 这里通过现有 guard 验证: 传 weird 类型的 opts 不应让 runGuards 整体挂掉
|
|
const issues = runGuards(baseCtx, { fields: null as any, tables: undefined })
|
|
expect(Array.isArray(issues)).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('hasErrors / formatIssues 工具方法', () => {
|
|
it('hasErrors: 有 ERROR → true', () => {
|
|
const issues: BlindspotIssue[] = [{ id: 'BS-X', level: 'ERROR', trigger: 't', message: 'm', fix: 'f' }]
|
|
expect(hasErrors(issues)).toBe(true)
|
|
})
|
|
|
|
it('hasErrors: 仅 WARN → false', () => {
|
|
const issues: BlindspotIssue[] = [{ id: 'BS-X', level: 'WARN', trigger: 't', message: 'm', fix: 'f' }]
|
|
expect(hasErrors(issues)).toBe(false)
|
|
})
|
|
|
|
it('hasErrors: 空 → false', () => {
|
|
expect(hasErrors([])).toBe(false)
|
|
})
|
|
|
|
it('formatIssues: 空 → 空串', () => {
|
|
expect(formatIssues([])).toBe('')
|
|
})
|
|
|
|
it('formatIssues: 有 issues → 含 [ERROR]/[WARN] + id + fix', () => {
|
|
const issues: BlindspotIssue[] = [
|
|
{ id: 'BS-48', level: 'ERROR', trigger: 't', message: 'm1', fix: 'f1' },
|
|
{ id: 'BS-28', level: 'WARN', trigger: 't', message: 'm2', fix: 'f2' },
|
|
]
|
|
const out = formatIssues(issues)
|
|
expect(out).toContain('[ERROR]')
|
|
expect(out).toContain('BS-48')
|
|
expect(out).toContain('[WARN]')
|
|
expect(out).toContain('BS-28')
|
|
expect(out).toContain('f1')
|
|
expect(out).toContain('f2')
|
|
})
|
|
})
|
|
|
|
describe('blindspot guards — BS-60 vmodel 缺失', () => {
|
|
it('BS-60: 字段没 vmodel/field 且无 f_xxx 占位应 WARN', () => {
|
|
const issues = runGuards(baseCtx, {
|
|
fields: [
|
|
{ __config__: { label: '设备名称', jnpfKey: 'input' } }, // 无 __vModel__/field/vmodel/f_xxx
|
|
],
|
|
type: 1, webType: 1,
|
|
})
|
|
expect(issues.some(i => i.id === 'BS-60' && i.level === 'WARN')).toBe(true)
|
|
})
|
|
|
|
it('BS-60: 字段有 f_xxx 占位不应触发 (修复后路径)', () => {
|
|
const issues = runGuards(baseCtx, {
|
|
fields: [
|
|
{ f_device_name: '', __config__: { label: '设备名称', jnpfKey: 'input' } },
|
|
],
|
|
type: 1, webType: 1,
|
|
})
|
|
expect(issues.some(i => i.id === 'BS-60')).toBe(false)
|
|
})
|
|
|
|
it('BS-60: 字段显式 __vModel__ 不应触发', () => {
|
|
const issues = runGuards(baseCtx, {
|
|
fields: [
|
|
{ __vModel__: 'f_name', __config__: { label: 'x', jnpfKey: 'input' } },
|
|
],
|
|
type: 1, webType: 1,
|
|
})
|
|
expect(issues.some(i => i.id === 'BS-60')).toBe(false)
|
|
})
|
|
})
|