diff --git a/src/commands/form/_codegen_schema.ts b/src/commands/form/_codegen_schema.ts new file mode 100644 index 0000000..c2d3237 --- /dev/null +++ b/src/commands/form/_codegen_schema.ts @@ -0,0 +1,106 @@ +export interface CodegenSearchRepairResult { + columnData: Record + changed: number +} + +/** + * Repair search-list entries before JNPF CodePreview consumes them. + * + * CodeGenerateFactoryV3 dereferences SearchTypeModel.id without a null guard. + * The table-path form builder historically passed column-list entries into a + * helper that only read `field`, so persisted search entries lost both id and + * prop. Prefer the explicit entry value, then columnList, then form fields. + * + * Walks both the top-level `searchList` and any nested `children[*].searchList` + * / `tableList[*].searchList` payloads so a single repair call covers + * main-sub forms and any appsColumnData sub-tables. + */ +export function normalizeCodegenSearchList( + columnData: Record | null | undefined, + formData?: Record | null, +): CodegenSearchRepairResult { + const next = { ...(columnData || {}) } + let changed = 0 + + const fields = Array.isArray(formData?.fields) ? formData!.fields : [] + changed += repairSearchList(next, fields) + + if (Array.isArray(next.children)) { + next.children = (next.children as Array>).map((child) => { + const copy = { ...child } + const childFields = collectNestedFields(child) || fields + changed += repairSearchList(copy, childFields) + return copy + }) + } + if (Array.isArray(next.tableList)) { + next.tableList = (next.tableList as Array>).map((row) => { + const copy = { ...row } + const rowFields = collectNestedFields(row) || fields + changed += repairSearchList(copy, rowFields) + return copy + }) + } + + return { columnData: next, changed } +} + +function repairSearchList( + container: Record, + fields: Array>, +): number { + const searchList = Array.isArray(container.searchList) ? container.searchList.map((entry: any) => ({ ...entry })) : [] + const columnList = Array.isArray(container.columnList) ? container.columnList : [] + let changed = 0 + + for (let index = 0; index < searchList.length; index++) { + const entry = searchList[index] + if (!entry || typeof entry !== 'object') continue + const column = columnList[index] || {} + const field = fields[index] || {} + const candidate = entry.id || entry.prop || entry.__vModel__ || entry.field + || column.prop || column.id || column.__vModel__ || field.__vModel__ + if (!candidate) continue + + let entryChanged = false + if (!entry.id) { + entry.id = candidate + entryChanged = true + } + if (!entry.prop) { + entry.prop = candidate + entryChanged = true + } + if (!entry.__vModel__) { + entry.__vModel__ = candidate + entryChanged = true + } + const label = entry.label || column.label || column.fullName || field.__config__?.label + if (!entry.label && label) { + entry.label = label + entryChanged = true + } + if (entryChanged) changed++ + } + + container.searchList = searchList + return changed +} + +/** + * Find a per-scope field list: prefer the nested `fieldList`, then derive from + * the `__config__.children` of any table container. Returns undefined so + * callers can fall back to the top-level form fields. + */ +function collectNestedFields(scope: Record): Array> | undefined { + if (!scope || typeof scope !== 'object') return undefined + if (Array.isArray(scope.fieldList) && scope.fieldList.length > 0) { + return scope.fieldList as Array> + } + const cfg = scope.__config__ || {} + const children = Array.isArray(cfg.children) ? cfg.children : [] + if (children.length > 0) { + return children as Array> + } + return undefined +} diff --git a/tests/unit/di_list_encode.test.ts b/tests/unit/di_list_encode.test.ts new file mode 100644 index 0000000..efb83d5 --- /dev/null +++ b/tests/unit/di_list_encode.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from 'vitest' +import { enrichDataInterfaceListWithEnCode } from '../../src/commands/di/list.ts' + +describe('di list enCode enrichment', () => { + it('fills missing enCode from an id→enCode map', () => { + const selector = [ + { + id: 'cat-a', + category: '0', + children: [ + { id: 'di-1', category: '1', fullName: '状态查询' }, + { id: 'di-2', category: '1', fullName: '审核查询' }, + ], + }, + ] + + const result = enrichDataInterfaceListWithEnCode(selector, { + 'di-1': 'statusQuery', + 'di-2': 'auditQuery', + }) + + expect(result).toEqual([ + { id: 'di-1', category: '1', fullName: '状态查询', enCode: 'statusQuery' }, + { id: 'di-2', category: '1', fullName: '审核查询', enCode: 'auditQuery' }, + ]) + }) + + it('leaves enCode alone when the selector already carries it', () => { + const result = enrichDataInterfaceListWithEnCode( + [{ id: 'di-1', category: '1', enCode: 'explicit', fullName: '已带编码' }], + { 'di-1': 'overridden' }, + ) + expect(result[0].enCode).toBe('explicit') + }) +}) diff --git a/tests/unit/di_list_selector.test.ts b/tests/unit/di_list_selector.test.ts new file mode 100644 index 0000000..fb70f9c --- /dev/null +++ b/tests/unit/di_list_selector.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from 'vitest' +import { flattenDataInterfaceSelector } from '../../src/commands/di/list.ts' + +describe('di list selector fallback', () => { + it('flattens interface leaves and keeps category folders out of the result', () => { + const result = flattenDataInterfaceSelector([ + { + id: 'category-a', + category: '0', + fullName: '报名接口', + children: [ + { id: 'di-1', category: '1', enCode: 'statusQuery', fullName: '状态查询' }, + { id: 'di-2', category: 1, enCode: 'auditQuery', fullName: '审核查询' }, + ], + }, + { id: 'category-b', category: '0', children: [] }, + ]) + + expect(result).toEqual([ + { id: 'di-1', category: '1', enCode: 'statusQuery', fullName: '状态查询' }, + { id: 'di-2', category: 1, enCode: 'auditQuery', fullName: '审核查询' }, + ]) + }) + + it('filters by keyword and applies pagination after flattening', () => { + const selector = [ + { + category: '0', + children: [ + { id: 'di-1', category: '1', enCode: 'statusQuery', fullName: '状态查询' }, + { id: 'di-2', category: '1', enCode: 'auditQuery', fullName: '审核查询' }, + { id: 'di-3', category: '1', enCode: 'statusDetail', fullName: '状态详情' }, + ], + }, + ] + + const all = flattenDataInterfaceSelector(selector) + const filtered = all.filter((item) => `${item.enCode} ${item.fullName}`.includes('status')) + expect(filtered.slice(1, 2).map((item) => item.id)).toEqual(['di-3']) + }) +}) diff --git a/tests/unit/form_codegen_search_list.test.ts b/tests/unit/form_codegen_search_list.test.ts new file mode 100644 index 0000000..86a8a6b --- /dev/null +++ b/tests/unit/form_codegen_search_list.test.ts @@ -0,0 +1,113 @@ +import { describe, expect, it } from 'vitest' +import { normalizeCodegenSearchList } from '../../src/commands/form/_codegen_schema.ts' + +describe('codegen search-list schema repair', () => { + it('fills missing search ids from columnList props before CodePreview', () => { + const columnData = { + columnList: [ + { id: 'f_team_name', prop: 'f_team_name', label: '团队名称' }, + { id: 'f_topic_direction', prop: 'f_topic_direction', label: '赛题方向' }, + ], + searchList: [ + { label: '', __config__: { jnpfKey: 'input' } }, + { label: '', __config__: { jnpfKey: 'select' } }, + ], + } + + const result = normalizeCodegenSearchList(columnData) + + expect(result.changed).toBe(2) + expect(result.columnData.searchList).toEqual([ + expect.objectContaining({ id: 'f_team_name', prop: 'f_team_name' }), + expect.objectContaining({ id: 'f_topic_direction', prop: 'f_topic_direction' }), + ]) + }) + + it('uses form fields when a column entry is unavailable', () => { + const result = normalizeCodegenSearchList( + { searchList: [{ __config__: { jnpfKey: 'input' } }] }, + { fields: [{ __vModel__: 'f_team_name', __config__: { label: '团队名称' } }] }, + ) + + expect(result.changed).toBe(1) + expect(result.columnData.searchList[0].id).toBe('f_team_name') + expect(result.columnData.searchList[0].prop).toBe('f_team_name') + }) + + it('repairs nested searchList entries inside children and tableList', () => { + const columnData = { + columnList: [ + { id: 'f_team_name', prop: 'f_team_name', label: '团队名称' }, + { id: 'f_topic_direction', prop: 'f_topic_direction', label: '赛题方向' }, + ], + searchList: [ + { label: '', __config__: { jnpfKey: 'input' } }, + ], + children: [ + { + searchList: [ + { label: '', __config__: { jnpfKey: 'select' } }, + ], + }, + ], + tableList: [ + { + searchList: [ + { label: '', __config__: { jnpfKey: 'input' } }, + { label: '', __config__: { jnpfKey: 'select' } }, + ], + }, + ], + } + + const result = normalizeCodegenSearchList(columnData) + + // Top-level: searchList[0] falls back to columnList[0] → f_team_name → 1 fix + // children[0]: columnList=[] so falls back to fields; entry count 1 → 0 fix + // tableList[0]: same as children → 0 fix + expect(result.changed).toBe(1) + expect(result.columnData.searchList[0].id).toBe('f_team_name') + }) + + it('repairs nested entries with per-scope columnList fallback', () => { + const columnData = { + columnList: [ + { id: 'f_team_name', prop: 'f_team_name', label: '团队名称' }, + { id: 'f_topic_direction', prop: 'f_topic_direction', label: '赛题方向' }, + ], + searchList: [ + { label: '', __config__: { jnpfKey: 'input' } }, + ], + children: [ + { + columnList: [ + { id: 'f_topic_direction', prop: 'f_topic_direction', label: '赛题方向' }, + ], + searchList: [ + { label: '', __config__: { jnpfKey: 'select' } }, + ], + }, + ], + tableList: [ + { + columnList: [ + { id: 'f_team_name', prop: 'f_team_name', label: '团队名称' }, + { id: 'f_topic_direction', prop: 'f_topic_direction', label: '赛题方向' }, + ], + searchList: [ + { label: '', __config__: { jnpfKey: 'input' } }, + { label: '', __config__: { jnpfKey: 'select' } }, + ], + }, + ], + } + + const result = normalizeCodegenSearchList(columnData) + + expect(result.changed).toBe(4) + expect(result.columnData.searchList[0].id).toBe('f_team_name') + expect(result.columnData.children[0].searchList[0].id).toBe('f_topic_direction') + expect(result.columnData.tableList[0].searchList[0].id).toBe('f_team_name') + expect(result.columnData.tableList[0].searchList[1].id).toBe('f_topic_direction') + }) +}) diff --git a/tests/unit/form_create_smartprops_ai.test.ts b/tests/unit/form_create_smartprops_ai.test.ts new file mode 100644 index 0000000..091e1ba --- /dev/null +++ b/tests/unit/form_create_smartprops_ai.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from 'vitest' +import { shouldRunFinalSmartProps } from '../../src/commands/form/create.ts' + +function withBuild(opts: Record) { + return { ...opts, __aiBuildFormData: { formData: '{}' } } +} + +describe('form create final smartProps gate', () => { + it('runs when --ai supplies a form data builder', () => { + expect(shouldRunFinalSmartProps(withBuild({ ai: 'demo' }))).toBe(true) + }) + it('runs when --table supplies a form data builder', () => { + expect(shouldRunFinalSmartProps(withBuild({ table: 'x' }))).toBe(true) + }) + it('runs when both --ai and --table are present', () => { + expect(shouldRunFinalSmartProps(withBuild({ ai: 'demo', table: 'x' }))).toBe(true) + }) + it('does not run when neither --ai nor --table is set (defensive)', () => { + expect(shouldRunFinalSmartProps({} as any)).toBe(false) + }) + it('does not run when --ai is set but no builder output is attached', () => { + expect(shouldRunFinalSmartProps({ ai: 'demo' } as any)).toBe(false) + }) +}) diff --git a/tests/unit/form_info_bindings.test.ts b/tests/unit/form_info_bindings.test.ts new file mode 100644 index 0000000..8210ee4 --- /dev/null +++ b/tests/unit/form_info_bindings.test.ts @@ -0,0 +1,80 @@ +import { describe, expect, it } from 'vitest' +import { projectFormBindings } from '../../src/commands/form/info.ts' + +describe('form info --bindings projection', () => { + it('flattens select fields with dictionary metadata', () => { + const bindings = projectFormBindings({ + formData: { + fields: [ + { + __vModel__: 'f_topic_direction', + __config__: { + jnpfKey: 'select', + label: '赛题方向', + dataType: 'dictionary', + dictionaryType: 'dict-1', + }, + }, + { + __vModel__: 'f_team_name', + __config__: { + jnpfKey: 'input', + label: '团队名称', + required: true, + }, + }, + ], + }, + }) + expect(bindings).toEqual([ + { + field: 'f_topic_direction', + jnpfKey: 'select', + label: '赛题方向', + dataType: 'dictionary', + dictionaryType: 'dict-1', + required: false, + modelId: null, + }, + { + field: 'f_team_name', + jnpfKey: 'input', + label: '团队名称', + dataType: null, + dictionaryType: null, + required: true, + modelId: null, + }, + ]) + }) + + it('skips entries without __vModel__', () => { + const bindings = projectFormBindings({ + formData: { + fields: [ + { __config__: { jnpfKey: 'input' } }, + ], + }, + }) + expect(bindings).toEqual([]) + }) + + it('keeps entries with __vModel__ but missing __config__', () => { + const bindings = projectFormBindings({ + formData: { + fields: [{ __vModel__: 'f_a' }], + }, + }) + expect(bindings).toEqual([ + { + field: 'f_a', + jnpfKey: undefined, + label: undefined, + dataType: null, + dictionaryType: null, + required: false, + modelId: null, + }, + ]) + }) +}) diff --git a/tests/unit/form_update_fields.test.ts b/tests/unit/form_update_fields.test.ts new file mode 100644 index 0000000..c6ee935 --- /dev/null +++ b/tests/unit/form_update_fields.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from 'vitest' +import { mergeFormFieldUpdates } 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', + }, + }) + }) +}) diff --git a/tests/unit/test_run_choice_value.test.ts b/tests/unit/test_run_choice_value.test.ts new file mode 100644 index 0000000..9f3777b --- /dev/null +++ b/tests/unit/test_run_choice_value.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, it } from 'vitest' +import { pickChoiceValue } from '../../src/commands/test/run.ts' + +describe('test run pickChoiceValue', () => { + it('returns first option value when no dictionary binding exists', async () => { + const value = await pickChoiceValue( + {} as any, + new Map(), + { options: [{ value: 'finance' }] }, + 'f_topic_direction', + ) + expect(value).toBe('finance') + }) + + it('falls back to "1" when both options and dictionary are missing', async () => { + const value = await pickChoiceValue( + {} as any, + new Map(), + {}, + 'f_topic_direction', + ) + expect(value).toBe('1') + }) + + it('returns cached dictionary enCode on subsequent calls', async () => { + const cache = new Map>>([ + ['dict-id', [{ enCode: 'pending' }, { enCode: 'approved' }]], + ]) + const value = await pickChoiceValue( + {} as any, + cache, + { dataType: 'dictionary', dictionaryType: 'dict-id' }, + 'f_registration_status', + ) + expect(value).toBe('pending') + }) +})