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 → 严格模式按字段逐条报错', () => { const r = validateRelationFormFields([{ __vModel__: 'f_equipment_code_ref', __config__: { jnpfKey: 'relationForm', modelId: 'eq-id', // 缺 propsValue, relationField }, }]) expect(r.ok).toBe(false) expect(r.errors).toHaveLength(2) const joined = r.errors.join('|') expect(joined).toContain('propsValue') expect(joined).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 字段跳过 (但缺 jnpfKey 字段仍报)', () => { // 严格模式: null/undefined 跳过, 但 { __config__: {} } 缺 jnpfKey 仍报错 const r = validateRelationFormFields([null, undefined, { __config__: {} }]) expect(r.ok).toBe(false) expect(r.errors).toHaveLength(1) expect(r.errors[0]).toMatch(/jnpfKey/) }) it('多字段混合: 1 个 relationForm 缺字段 + 1 个 input → 严格模式逐条报错', () => { 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) // f_fk 缺 propsValue + relationField → 2 条 strict 报错 expect(r.errors).toHaveLength(2) expect(r.errors.every(e => e.includes('f_fk'))).toBe(true) }) })