From c203bd2277637a0cbf89bc70d0ed721e80ead26f Mon Sep 17 00:00:00 2001 From: ssw Date: Wed, 24 Jun 2026 14:36:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(guards):=20BS-54=20L2=20guard=20(select/re?= =?UTF-8?q?lationForm=20=E5=BF=85=E5=A1=AB=E5=AD=90=E9=94=AE=20ERROR)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 红: tests/unit/bs54_guard.test.ts 5 FAIL - 绿: checkBS54 加到 _blindspot_guards.ts (22→23 条) - select 字段需 (dataType=static + options) / (dataType=dictionary + dictionaryType) / (dataType=dynamic + propsUrl) 之一 - relationForm 字段需 modelId 非空 - 通用规则, 不含应用特定 vmodel→enCode 映射 触发场景: form create --fields 直接传 JSON (绕开 FieldBuilder.build()) 时兜底 Co-Authored-By: Claude --- src/commands/_blindspot_guards.ts | 37 +++++++++ tests/unit/bs54_guard.test.ts | 124 ++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 tests/unit/bs54_guard.test.ts diff --git a/src/commands/_blindspot_guards.ts b/src/commands/_blindspot_guards.ts index 3cbca93..394ed8a 100644 --- a/src/commands/_blindspot_guards.ts +++ b/src/commands/_blindspot_guards.ts @@ -624,6 +624,43 @@ export const BLINDSPOT_GUARDS: BlindspotGuard[] = [ message: 'v0.11+ dacli form create 自动注入 web 默认按钮 (btnsList+columnBtnsList), 不需手动加"新增/编辑"', fix: '去掉 --skip-default-btns 类显式禁用; 若想自定义, 仿造 web config.ts:74-153 默认 schema 后传 --columnData', }, + + // BS-54 (2026-06-24): select/relationForm 字段缺必填子键 → 浏览器层坏掉 + // 根因: select 字段无 dataType + options/dictionaryType/propsUrl → 下拉框空白; + // relationForm 字段无 modelId → 弹窗 return (RelationForm.vue:201). + // FieldBuilder.build() 已默认补齐, 但 fields 直接传 JSON (绕开 FieldBuilder) 时仍会缺. + // 通用规则, 不含应用特定 vmodel→enCode 映射 (映射由应用脚本补). + { + id: 'BS-54', + trigger: 'select/relationForm 字段缺必填子键 (dataType/options/dictionaryType/propsUrl/modelId)', + level: 'ERROR', + check: (_ctx, opts) => { + const fields = opts?.fields as Array> | undefined + if (!fields || !Array.isArray(fields)) return false + for (const f of fields) { + const cfg = (f.__config__ as Record) || {} + const jk = cfg.jnpfKey as string | undefined + if (jk === 'select') { + const dataType = cfg.dataType as string | undefined + const optsArr = f.options as unknown[] | undefined + const dictionaryType = cfg.dictionaryType as string | undefined + const propsUrl = cfg.propsUrl as string | undefined + // 满足任一即可: static + options / dictionary + 非空字典 / dynamic + 非空 propsUrl + const okStatic = dataType === 'static' && Array.isArray(optsArr) && optsArr.length > 0 + const okDict = dataType === 'dictionary' && !!dictionaryType && dictionaryType.length > 0 + const okDynamic = dataType === 'dynamic' && !!propsUrl && propsUrl.length > 0 + if (!okStatic && !okDict && !okDynamic) return true + } + if (jk === 'relationForm') { + const modelId = cfg.modelId as string | undefined + if (!modelId || modelId.length === 0) return true + } + } + return false + }, + message: 'BS-54: select 字段需 (dataType=static + options) / (dataType=dictionary + dictionaryType) / (dataType=dynamic + propsUrl) 之一; relationForm 字段需 modelId 非空 (RelationForm.vue:201 缺 modelId 直接 return)', + fix: '用 FieldBuilder.build("select"/"relationForm") 自动补默认值; 或显式传 dataType/options/dictionaryType/propsUrl/modelId', + }, ] /** diff --git a/tests/unit/bs54_guard.test.ts b/tests/unit/bs54_guard.test.ts new file mode 100644 index 0000000..923e0a1 --- /dev/null +++ b/tests/unit/bs54_guard.test.ts @@ -0,0 +1,124 @@ +/** + * 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: [], + 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) + }) +}) \ No newline at end of file