Files
dacli/tests/unit/form_preview.test.ts
T
sswandClaude 3f1cb3a03b feat(dacli): JNPF7 验证闭环 Stage 0 + P0-6 preview + workflow canvas 回归修复
Stage 0 (验证基础设施):
- 5 层验证工具链 (tools/) + JSON Schema + validate-baseline.sh CI gate
- P0-9: components-whitelist.yaml 单一来源 + parseFlowXml 消漂移 (create.ts tag 改消费 component-map.json)
- E-2: docs/components.md 文档化 6 个 pass-through 组件
- P1-16: validate-baseline 接入 quality-gate.sh
- L3 回归测试 (bs117 / bpmn-bs / antipattern-output) + mock LLM 库

P0-6 (user-in-the-loop):
- form create --ai --preview: 提交前渲染字段表格 (CJK 对齐) + TTY 交互确认 (_preview.ts)

workflow canvas 回归修复 (v0.22+ SSW 迁移引入):
- parseFlowXml 命名空间无关 (flowNodes 缺口)
- _autoLayout 无 DI 时合成完整 BPMNDiagram+Shape+Edge + bpmn2 归一化 (canvas 缺口)
- 真实后端 + 浏览器设计器验证画布渲染; 详见 jnpf7-analysis §14

quality-gate 全绿: tsc 0 · vitest 483 · doc:check · baseline L1-L5

Co-Authored-By: Claude <[email protected]>
2026-07-27 12:49:44 +08:00

77 lines
3.1 KiB
TypeScript

/**
* tests/unit/form_preview.test.ts — P0-6 --preview 渲染 + 展平单元测试
*/
import { describe, it, expect } from 'vitest'
import {
strWidth,
collectPreviewRows,
renderFormPreview,
confirmApply,
} from '../../src/commands/form/_preview.ts'
const sampleFormData = {
fields: [
{ __vModel__: 'f_leave_type', options: [], __config__: { jnpfKey: 'select', label: '请假类型', required: true, dataType: 'dictionary', dictionaryType: '850666694069192837' } },
{ __vModel__: 'f_start_date', format: 'yyyy-MM-dd', __config__: { jnpfKey: 'datePicker', label: '开始日期', required: false } },
{ __vModel__: 'f_days', min: 1, max: 365, __config__: { jnpfKey: 'inputNumber', label: '请假天数', required: false } },
{ __vModel__: 'f_reason', __config__: { jnpfKey: 'textarea', label: '请假事由', required: false } },
// 布局容器: 不产生行, 但递归子字段
{ __config__: { jnpfKey: 'row', label: '', children: [
{ __vModel__: 'f_agent', __config__: { jnpfKey: 'input', label: '代理人', required: false } },
] } },
],
}
describe('P0-6 _preview: strWidth (CJK 宽度)', () => {
it('ASCII 1 宽, CJK 2 宽', () => {
expect(strWidth('abc')).toBe(3)
expect(strWidth('请假')).toBe(4)
expect(strWidth('a请b')).toBe(4)
})
})
describe('P0-6 _preview: collectPreviewRows', () => {
it('展平字段, 跳过布局容器但递归其子字段', () => {
const rows = collectPreviewRows(sampleFormData)
const names = rows.map(r => r.name)
expect(names).toContain('f_leave_type')
expect(names).toContain('f_agent') // 来自 row 容器内
expect(names).not.toContain('') // row 容器本身不入行
expect(rows.length).toBe(5)
})
it('提取 label/component/required', () => {
const rows = collectPreviewRows(sampleFormData)
const leave = rows.find(r => r.name === 'f_leave_type')!
expect(leave.label).toBe('请假类型')
expect(leave.component).toBe('select')
expect(leave.required).toBe(true)
})
it('describeExtra: 字典 / 数值范围 / 日期格式', () => {
const rows = collectPreviewRows(sampleFormData)
expect(rows.find(r => r.name === 'f_leave_type')!.extra).toContain('字典')
expect(rows.find(r => r.name === 'f_days')!.extra).toContain('范围')
expect(rows.find(r => r.name === 'f_start_date')!.extra).toContain('yyyy-MM-dd')
})
})
describe('P0-6 _preview: renderFormPreview', () => {
it('渲染盒线表格, 含表头与字段名', () => {
const table = renderFormPreview(sampleFormData, { fullName: '请假申请', enCode: 'leaveForm', webType: '2', autoRelease: true })
expect(table).toContain('leaveForm')
expect(table).toContain('字段标签')
expect(table).toContain('f_leave_type')
expect(table).toContain('┌') // 盒线
expect(table).toContain('└')
expect(table).toContain('字段数=5')
})
})
describe('P0-6 _preview: confirmApply', () => {
it('非 TTY 环境返回 false (dry-run 安全默认)', async () => {
// vitest 环境 process.stdin.isTTY 为 undefined/false
expect(await confirmApply()).toBe(false)
})
})