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