feat(dacli): hard-bind form smartProps, DI list enCode, codegen sub-table searchList, form info --bindings, test run dictionary mapping
- form create: extract applyFinalSmartProps/shouldRunFinalSmartProps so
--ai-only paths also re-run smart props after the final buildFormData.
Adds unit test for the gate predicate.
- form update: mergeFormFieldUpdates deep-merges __config__ and matches
vmodel/__vModel__, preventing schema loss on partial diffs.
- form info: new --bindings projection flattens field configs to
{field, jnpfKey, dataType, dictionaryType, modelId, required} for
callers without jq piercing the JNPF nested formData JSON.
- form codegen: normalizeCodegenSearchList now walks children/tableList
payloads; new _codegen_schema helper repairs searchList id/prop across
nested scopes, avoiding SearchTypeModel.id NPE in JNPF CodeGenerateFactoryV3.
- di list: enrichDataInterfaceListWithEnCode backfills the enCode field
selector omits; walks every category folder so callers can filter on
enCode reliably.
- test run: pickChoiceValue routes select/radio/checkbox through
loadDict.getDictionaryDataSelector (cached) before falling back to
options[0].value, removing the brittle dependency on option order.
Tests: 56/56 PASS across 9 unit specs.
This commit is contained in:
1 parent
4a3dd4b9d5
commit
51ffaf00ba
8 files changed
+472
No files matched your search
@@ -0,0 +1,106 @@
|
||||
export interface CodegenSearchRepairResult {
|
||||
columnData: Record<string, any>
|
||||
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<string, any> | null | undefined,
|
||||
formData?: Record<string, any> | 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<Record<string, any>>).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<Record<string, any>>).map((row) => {
|
||||
const copy = { ...row }
|
||||
const rowFields = collectNestedFields(row) || fields
|
||||
changed += repairSearchList(copy, rowFields)
|
||||
return copy
|
||||
})
|
||||
}
|
||||
|
||||
return { columnData: next, changed }
|
||||
}
|
||||
|
||||
function repairSearchList(
|
||||
container: Record<string, any>,
|
||||
fields: Array<Record<string, any>>,
|
||||
): 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<string, any>): Array<Record<string, any>> | undefined {
|
||||
if (!scope || typeof scope !== 'object') return undefined
|
||||
if (Array.isArray(scope.fieldList) && scope.fieldList.length > 0) {
|
||||
return scope.fieldList as Array<Record<string, any>>
|
||||
}
|
||||
const cfg = scope.__config__ || {}
|
||||
const children = Array.isArray(cfg.children) ? cfg.children : []
|
||||
if (children.length > 0) {
|
||||
return children as Array<Record<string, any>>
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
@@ -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'])
|
||||
})
|
||||
})
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { shouldRunFinalSmartProps } from '../../src/commands/form/create.ts'
|
||||
|
||||
function withBuild(opts: Record<string, unknown>) {
|
||||
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)
|
||||
})
|
||||
})
|
||||
@@ -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,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -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',
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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<string, Array<Record<string, unknown>>>([
|
||||
['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')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user