Files
dacli/tests/unit/form_update_fields.test.ts
T
ssw 0b14466681 fix(dacli): 5 项 tdd8 smoke test 命中修复 — relationForm 4 字段闭环
#1 新增 dacli form relation-form-complete 命令
  - 自动补 relationForm 4 字段 (jnpfKey/modelId/propsValue/relationField)
  - propsValue 从 target 表 f_*_name 字段自动推
  - relationField 默认 = source FK 字段自身
  - --dry-run 模式只推导不写
  - 19 个单测覆盖 (isRelationFormComplete/pickDisplayField/deriveCompletion)

#2 form update --fields 加 --strict 校验 (默认 ON)
  - relationForm 4 字段缺任一 → ctx.fail + 提示用 relation-form-complete 自动补
  - --no-strict 兼容老用法
  - 7 个单测覆盖 (validateRelationFormFields)

#4 --json 模式 log.* 走 stderr
  - log.ts 加 setJsonMode()/isJsonMode()
  - info/ok/warn/dim/raw 在 jsonMode 时走 stderr
  - setupOutputFilter 自动联动
  - 修 menu/info.ts 启用 setupOutputFilter (之前直接 log.* 污染 stdout)
  - 修 4 个测试 mock 补 setJsonMode + isJsonMode

#6 menu info 加 formEnCode 反查
  - 从 propertyJson.moduleId → form list 反查
  - 输出 relatedForms[] 含 formEnCode/formId/fullName
  - 解决 form enCode 与 menu enCode hash 后缀不一致问题

#8 TC-A 报告 PASS 显式标 SKIP/FAIL 区分
  - '通过: 4/5 (4 PASS + 0 FAIL + 1 SKIP)' 而非仅 '4/5'

实测 tdd8 (本轮 smoke test) 43/46 → 应能提升 (TC-C relationForm FAIL 修复)

质量门 (scripts/quality-gate.sh):
  [1/4] tsc --noEmit: 0 错误
  [2/4] vitest run tests/unit: 415 passed (含 26 个新测)
  [3/4] doc:check: 0 drift (272 文件扫过)
  [4/4] backend smoke: dacli form list PASS
2026-07-24 22:29:54 +08:00

112 lines
3.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { mergeFormFieldUpdates, validateRelationFormFields } from '../../src/commands/form/update.ts'
describe('form update field merge', () => {
it('matches __vModel__ and deep-merges __config__ without discarding schema', () => {
const existing = [{
__vModel__: 'f_topic_direction',
__config__: {
jnpfKey: 'select',
label: '赛题方向',
dataType: 'static',
dictionaryType: '',
trigger: 'change',
},
options: [],
}]
const merged = mergeFormFieldUpdates(existing, [{
__vModel__: 'f_topic_direction',
__config__: { dataType: 'dictionary', dictionaryType: 'cat-test-id' },
}])
expect(merged).toHaveLength(1)
expect(merged[0]).toMatchObject({
__vModel__: 'f_topic_direction',
options: [],
__config__: {
jnpfKey: 'select',
label: '赛题方向',
trigger: 'change',
dataType: 'dictionary',
dictionaryType: 'cat-test-id',
},
})
})
})
describe('validateRelationFormFields (v2.4+, 2026-07-24)', () => {
it('4 字段齐全 → ok=true, errors=[]', () => {
const r = validateRelationFormFields([{
__vModel__: 'f_x_ref',
__config__: {
jnpfKey: 'relationForm',
modelId: '123',
propsValue: 'f_x_name',
relationField: 'f_x_ref',
},
}])
expect(r.ok).toBe(true)
expect(r.errors).toEqual([])
})
it('缺 modelId → ok=false, errors 含 modelId', () => {
const r = validateRelationFormFields([{
__vModel__: 'f_x_ref',
__config__: {
jnpfKey: 'relationForm',
propsValue: 'f_x_name',
relationField: 'f_x_ref',
},
}])
expect(r.ok).toBe(false)
expect(r.errors[0]).toContain('f_x_ref')
expect(r.errors[0]).toContain('modelId')
})
it('缺 propsValue + relationField → 1 个字段同时报 2 项', () => {
const r = validateRelationFormFields([{
__vModel__: 'f_equipment_code_ref',
__config__: {
jnpfKey: 'relationForm',
modelId: 'eq-id',
// 缺 propsValue, relationField
},
}])
expect(r.ok).toBe(false)
expect(r.errors[0]).toContain('propsValue')
expect(r.errors[0]).toContain('relationField')
})
it('jnpfKey 不是 relationForm → 跳过校验', () => {
const r = validateRelationFormFields([{
__vModel__: 'f_x',
__config__: { jnpfKey: 'input' },
}])
expect(r.ok).toBe(true)
})
it('空数组 → ok=true', () => {
expect(validateRelationFormFields([]).ok).toBe(true)
})
it('null 字段跳过', () => {
const r = validateRelationFormFields([null, undefined, { __config__: {} }])
expect(r.ok).toBe(true)
})
it('多字段混合: 1 个 relationForm 缺字段 + 1 个 input → 报 1 个 error', () => {
const r = validateRelationFormFields([
{ __vModel__: 'f_input', __config__: { jnpfKey: 'input' } },
{
__vModel__: 'f_fk',
__config__: { jnpfKey: 'relationForm', modelId: '123' }, // 缺 2 字段
},
{ __vModel__: 'f_date', __config__: { jnpfKey: 'datePicker' } },
])
expect(r.ok).toBe(false)
expect(r.errors).toHaveLength(1)
expect(r.errors[0]).toContain('f_fk')
})
})