test(dacli): form-lint 单测 T1.1-T1.5 (parsePythonForms, checkBs31/32)

This commit is contained in:
Claude committed 2026-06-14 19:41:40 +08:00
1 parent d55ec5b219
commit 7edf55bc88
3 files changed
+141 -27

No files matched your search

+3 -1
View File
@@ -90,7 +90,9 @@ export function checkBs31(formDataRaw: unknown): BlindspotIssue[] {
}
const MONEY_KEYWORDS = ['price', 'amount', 'money', 'quantity', 'sum', 'total', 'fee', 'cost', 'score', 'qty']
const MONEY_REGEX = new RegExp(`\\b(${MONEY_KEYWORDS.join('|')})\\b`, 'i')
// 用 lookahead/lookbehind 实现"独立 token"匹配: 允许 _/字母数字边界, 防 "consumer" 误中 "sum"
// (?<![a-z0-9]) = 前面不是字母数字; (?![a-z0-9]) = 后面不是字母数字
const MONEY_REGEX = new RegExp(`(?<![a-z0-9])(${MONEY_KEYWORDS.join('|')})(?![a-z0-9])`, 'i')
export function checkBs32(formDataRaw: unknown): BlindspotIssue[] {
const issues: BlindspotIssue[] = []
+42 -26
View File
@@ -24,33 +24,49 @@ export function parsePythonForms(src: string): FormDef[] {
const m = src.match(/FORMS\s*=\s*\[([\s\S]*?)\n\]/m)
if (!m) return []
const body = m[1]
// 找顶层 [ 块 (字段列表)
let i = 0
while (i < body.length) {
if (body[i] === '[') {
const fieldsStart = i + 1
let depth = 1; let j = fieldsStart
while (j < body.length && depth > 0) {
if (body[j] === '[') depth++
else if (body[j] === ']') depth--
j++
}
const fieldsBlock = body.slice(fieldsStart, j - 1)
// 找前 2 个字符串 (en_code, full_name) — 简化: 假设出现在 [ 块前的行
// 这里用更稳: 反向找 ([ 块前的 2 个 '字符串'
const beforeFields = body.slice(Math.max(0, i - 200), i)
const strs = [...beforeFields.matchAll(/'([^']+)'/g)].map(m => m[1])
const enCode = strs[strs.length - 2] || '?'
const fullName = strs[strs.length - 1] || '?'
// 抽每个 (type, vmodel, label, req) 元组 (允许 5 元素含 extra dict)
const fields: FieldDef[] = []
for (const tm of fieldsBlock.matchAll(/\(\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*(True|False)/g)) {
fields.push({ type: tm[1], vmodel: tm[2], label: tm[3], required: tm[4] === 'True' })
}
if (fields.length > 0) forms.push({ enCode, fullName, fields })
i = j
// 第一步: 找所有 form 头元组 (enCode, fullName, [)
// form 头的特征: 行首缩进 + ( + 'enCode', 'fullName', 紧跟 [
// 区别于 field 元组 (4 个字符串 + True/False): 头只有 2 字符串 + [
const formHeaders: Array<{ enCode: string; fullName: string; startIdx: number }> = []
const formHeaderRe = /^\s*\(\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*\[/gm
let mm: RegExpExecArray | null
while ((mm = formHeaderRe.exec(body)) !== null) {
formHeaders.push({ enCode: mm[1], fullName: mm[2], startIdx: mm.index })
}
if (formHeaders.length === 0) return []
// 第二步: 找所有 field 元组 (type, vmodel, label, required[, extra])
// field 元组至少 4 个字符串 + True/False, 可能是 5 元 (含 extra dict)
// label 允许空字符串: 用 '[^']*' (0+ chars) 而非 '[^']+' (1+ chars)
const fieldRe = /\(\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*'([^']*)'\s*,\s*(True|False)/g
const allFields: Array<{ type: string; vmodel: string; label: string; required: boolean; idx: number }> = []
while ((mm = fieldRe.exec(body)) !== null) {
allFields.push({ type: mm[1], vmodel: mm[2], label: mm[3], required: mm[4] === 'True', idx: mm.index })
}
// 第三步: 对每个 form 头, 找其 [ ... ] 内的 field 元组
for (let k = 0; k < formHeaders.length; k++) {
const header = formHeaders[k]
// 从 header.startIdx 之后找 [ 块
const bracketStart = body.indexOf('[', header.startIdx)
if (bracketStart < 0) continue
let depth = 1
let j = bracketStart + 1
while (j < body.length && depth > 0) {
if (body[j] === '[') depth++
else if (body[j] === ']') depth--
j++
}
i++
const bracketEnd = j - 1
// 收集此范围内的 field
const fields: FieldDef[] = []
for (const f of allFields) {
if (f.idx > bracketStart && f.idx < bracketEnd) {
fields.push({ type: f.type, vmodel: f.vmodel, label: f.label, required: f.required })
}
}
if (fields.length > 0) forms.push({ enCode: header.enCode, fullName: header.fullName, fields })
}
return forms
}
+96
View File
@@ -0,0 +1,96 @@
/**
* 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([])
})
})