Files
dacli/tests/unit/form-lint.test.ts
T

96 lines
4.1 KiB
TypeScript

/**
* form-lint.test.ts — design-lint 命令单测 (spec §5 T1.1-T1.5)
*/
import { describe, it, expect } from 'vitest'
import { parsePythonForms, parseDesignHtml } from '../../src/commands/form/_parsers.ts'
import { checkBs31, checkBs32 } from '../../src/commands/form/_blindspots.ts'
const FIXTURE_SCRIPT = `
FORMS = [
('good_form', '好表单', [
('input', 'f_name', '名称', True),
('inputNumber', 'f_price', '价格', False),
]),
('bad_label_form', 'label 缺失', [
('input', 'f_x', '', True), # label 为空 → BS-31 FAIL
]),
('bad_money_form', '金额用 input', [
('input', 'f_price', '价格', False), # amount-like 字段用 input → BS-32 FAIL
]),
('false_positive_form', 'sum 误匹配', [
('input', 'f_consumer_user_id', '消费用户ID', False), # "consumer" 含 "sum", word-boundary 应不命中
]),
]
`
const FIXTURE_DESIGN = `
<div class="cmd">npx tsx dacli/src/cli.ts dm create --table=tdss_x --dbLinkId=1 --fields='[]'</div>
<div class="cmd">npx tsx dacli/src/cli.ts form create --enCode=tdss_x --webType=2 --dbLinkId=1 --autoTable --autoRelease --fields='[]'</div>
<div class="cmd">npx tsx dacli/src/cli.ts form create --enCode=tdss_y --webType=2 --dbLinkId=1 --autoTable --fields='[]'</div>
`
describe('parsePythonForms', () => {
it('T1.1 抽 4 form 完整, 共 5 字段 (2+1+1+1)', () => {
const forms = parsePythonForms(FIXTURE_SCRIPT)
expect(forms.length).toBe(4)
expect(forms.reduce((s, f) => s + f.fields.length, 0)).toBe(5)
expect(forms[0].fields[0]).toEqual({ type: 'input', vmodel: 'f_name', label: '名称', required: true })
expect(forms[1].fields[0].label).toBe('') // bad label
})
})
describe('parseDesignHtml', () => {
it('T1.7 抽 3 .cmd 块, 标 form/dm kind', () => {
const blocks = parseDesignHtml(FIXTURE_DESIGN)
expect(blocks.length).toBe(3)
expect(blocks[0].commandKind).toBe('dm')
expect(blocks[0].hasDbLinkId).toBe(true)
expect(blocks[1].commandKind).toBe('form')
expect(blocks[1].hasAutoRelease).toBe(true)
expect(blocks[2].hasAutoRelease).toBe(false) // T1.7 缺 --autoRelease
})
})
describe('checkBs31', () => {
it('T1.2 缺 label → BS-31 FAIL', () => {
const forms = parsePythonForms(FIXTURE_SCRIPT)
const bad = forms.find(f => f.enCode === 'bad_label_form')!
const formData = { fields: bad.fields.map(fd => ({ __config__: { label: fd.label }, __vModel__: fd.vmodel })) }
const issues = checkBs31(JSON.stringify(formData))
expect(issues.length).toBe(1)
expect(issues[0].id).toBe('BS-31')
})
it('T1.2b 干净 form → BS-31 PASS', () => {
const forms = parsePythonForms(FIXTURE_SCRIPT)
const good = forms.find(f => f.enCode === 'good_form')!
const formData = { fields: good.fields.map(fd => ({ __config__: { label: fd.label }, __vModel__: fd.vmodel })) }
expect(checkBs31(JSON.stringify(formData))).toEqual([])
})
})
describe('checkBs32', () => {
it('T1.3 金额字段用 input → BS-32 FAIL', () => {
const forms = parsePythonForms(FIXTURE_SCRIPT)
const bad = forms.find(f => f.enCode === 'bad_money_form')!
const formData = { fields: bad.fields.map(fd => ({ __config__: { label: fd.label, jnpfKey: fd.type }, __vModel__: fd.vmodel })) }
const issues = checkBs32(JSON.stringify(formData))
expect(issues.length).toBe(1)
expect(issues[0].id).toBe('BS-32')
})
it('T1.4 金额字段用 inputNumber → BS-32 PASS', () => {
const forms = parsePythonForms(FIXTURE_SCRIPT)
const good = forms.find(f => f.enCode === 'good_form')!
const formData = { fields: good.fields.map(fd => ({ __config__: { label: fd.label, jnpfKey: fd.type }, __vModel__: fd.vmodel })) }
expect(checkBs32(JSON.stringify(formData))).toEqual([])
})
it('T1.5 false positive 修复: f_consumer_user_id 不被 BS-32 命中', () => {
const forms = parsePythonForms(FIXTURE_SCRIPT)
const fp = forms.find(f => f.enCode === 'false_positive_form')!
const formData = { fields: fp.fields.map(fd => ({ __config__: { label: fd.label, jnpfKey: fd.type }, __vModel__: fd.vmodel })) }
const issues = checkBs32(JSON.stringify(formData))
expect(issues).toEqual([])
})
})