115 lines
5.4 KiB
TypeScript
115 lines
5.4 KiB
TypeScript
/**
|
|
* 字段类型清单的"两边一不一样"闸门。
|
|
*
|
|
* 同一份类型清单过去散在十来个文件里:前端一份、后端一份,明细列再一份。
|
|
* 少改一处不会报错,只会在界面上悄悄少一格(手写签名就是这么丢的:控件、只读态、
|
|
* 导出都写好了,唯独左侧字段库没登记,于是谁也加不出这一类字段)。
|
|
* 这里把"前端有的后端也得有、后端认的前端也要能渲染"钉成断言,
|
|
* 加一类字段却漏登记时,测试直接点名漏在哪一处。
|
|
*/
|
|
import { readFileSync } from 'node:fs'
|
|
import { describe, expect, it } from 'vitest'
|
|
import en from '@/i18n/locales/en'
|
|
import zh from '@/i18n/locales/zh'
|
|
import { FIELD_TYPES, LABELLESS_TYPES, NON_FILLABLE_TYPES } from '@/utils/formLayout'
|
|
import { RUNTIME_NON_INPUT_TYPES, TABLE_COLUMN_MIN_WIDTHS } from '@/utils/formRuntime'
|
|
import { BAG_TYPES, DATE_TYPES, FLAG_TYPES, LAYOUT_TYPES, NUMBER_TYPES, TABLE_TYPES, TEXT_TYPES } from '@/utils/fieldCapabilities'
|
|
import { TABLE_COLUMN_TYPES } from '@/types'
|
|
|
|
/**
|
|
* 取 Python 源码里那一段字面量集合 / 元组的内容(``X = {`` 与 ``X = (`` 两种写法都认)。
|
|
*
|
|
* 括号按深度配对来找,单行写法也读得出来;注释行不参与提取,
|
|
* 免得注释里的引号被当成一个类型名。
|
|
*/
|
|
function pythonLiterals(source: string, marker: string): string[] {
|
|
const start = source.indexOf(marker)
|
|
if (start < 0) throw new Error(`后端里找不到 ${marker}`)
|
|
const openAt = source.indexOf(marker, start) + marker.length - 1
|
|
const close = source[openAt] === '{' ? '}' : ')'
|
|
let end = openAt
|
|
let depth = 0
|
|
while (end < source.length) {
|
|
const char = source[end]
|
|
if (char === '{' || char === '(') depth += 1
|
|
else if (char === '}' || char === ')') {
|
|
depth -= 1
|
|
if (depth === 0) break
|
|
}
|
|
end += 1
|
|
}
|
|
if (end >= source.length) throw new Error(`${marker} 那段没有收尾的 ${close}`)
|
|
return source
|
|
.slice(openAt + 1, end)
|
|
.split('\n')
|
|
.filter((line) => !line.trim().startsWith('#'))
|
|
.map((line) => [...line.matchAll(/"([^"]+)"/g)].map((match) => match[1]))
|
|
.flat()
|
|
}
|
|
|
|
function backend(name: string): string {
|
|
return readFileSync(new URL(`../../../backend/${name}`, import.meta.url), 'utf8')
|
|
}
|
|
|
|
const BE_DESIGN = backend('app/api/design.py')
|
|
const BE_RUNTIMER = backend('app/service/formruntime.py')
|
|
const BE_FIELD_TYPES = pythonLiterals(BE_DESIGN, 'FIELD_TYPES = {')
|
|
const BE_TABLE_COLUMN_TYPES = pythonLiterals(BE_RUNTIMER, 'TABLE_COLUMN_TYPES = (')
|
|
const BE_NON_INPUT_TYPES = pythonLiterals(BE_RUNTIMER, 'NON_INPUT_TYPES = (')
|
|
const BE_LABELLESS_TYPES = pythonLiterals(BE_DESIGN, 'LABELLESS_TYPES = {')
|
|
const BE_TEXT_LIMIT_TYPES = pythonLiterals(BE_RUNTIMER, 'TEXT_LIMIT_TYPES = (')
|
|
const BE_FILE_LIMIT_TYPES = pythonLiterals(BE_RUNTIMER, 'FILE_LIMIT_TYPES = (')
|
|
|
|
const label = (type: string) => `fieldType${type.charAt(0).toUpperCase()}${type.slice(1)}`
|
|
|
|
describe('字段类型清单:前后端一份话', () => {
|
|
it('后端认的类型与前端能渲染的一一对应,谁也不多谁也不少', () => {
|
|
expect(BE_FIELD_TYPES.slice().sort()).toEqual(FIELD_TYPES.slice().sort())
|
|
})
|
|
|
|
it('明细列两边的清单一致(列与主表共用一只控件,清单分叉等于列子里渲染不出控件)', () => {
|
|
expect(BE_TABLE_COLUMN_TYPES.slice().sort()).toEqual([...TABLE_COLUMN_TYPES].sort())
|
|
})
|
|
|
|
it('「没有录入控件」那一份两边一致,必填那道闸才不会各判各的', () => {
|
|
expect(BE_NON_INPUT_TYPES.slice().sort()).toEqual([...RUNTIME_NON_INPUT_TYPES].sort())
|
|
})
|
|
|
|
it('文本与附件的限制口径两边一致', () => {
|
|
expect(BE_TEXT_LIMIT_TYPES).toEqual(['text', 'textarea', 'richtext'])
|
|
expect(BE_FILE_LIMIT_TYPES).toEqual(['attachment', 'image'])
|
|
})
|
|
})
|
|
|
|
describe('新加一类字段时必须登记的那几处', () => {
|
|
it('每一类都有中英两名,切英文不会看到裸的 key', () => {
|
|
FIELD_TYPES.forEach((type) => {
|
|
expect((zh.form as Record<string, string>)[label(type)], `${type} 缺中文名`).toBeTruthy()
|
|
expect((en.form as Record<string, string>)[label(type)], `${type} 缺英文名`).toBeTruthy()
|
|
})
|
|
})
|
|
|
|
it('每一类都归进了能力分组,阈值与额度面板才不会对它一无所知', () => {
|
|
const grouped = [...NUMBER_TYPES, ...DATE_TYPES, ...TEXT_TYPES, ...FLAG_TYPES, ...BAG_TYPES, ...TABLE_TYPES, ...LAYOUT_TYPES]
|
|
FIELD_TYPES.forEach((type) => {
|
|
// relation 的归类走 TEXT/BAG 那几组,这里只保证"每一类都归了组"
|
|
expect(grouped, `${type} 没归进 utils/fieldCapabilities 的任何一组`).toContain(type)
|
|
})
|
|
})
|
|
|
|
it('能进明细表的每一列都有底宽,一列窄到点不开的情况就没了', () => {
|
|
TABLE_COLUMN_TYPES.forEach((type) => {
|
|
expect(TABLE_COLUMN_MIN_WIDTHS[type], `${type} 列缺最小宽度`).toBeGreaterThan(0)
|
|
})
|
|
})
|
|
|
|
it('不参与填写的类型都在 NON_FILLABLE 里,必填校验不会去问一格排版', () => {
|
|
RUNTIME_NON_INPUT_TYPES.forEach((type) => expect(NON_FILLABLE_TYPES).toContain(type))
|
|
})
|
|
|
|
it('「可以不填标题」那一份两边一致,设计器放得下的格子保存时不会被自己挡回来', () => {
|
|
// 空白格不需要标题:前端放行、后端要标题,结果是设计器里加完点保存才吃一句报错
|
|
expect(BE_LABELLESS_TYPES.slice().sort()).toEqual([...LABELLESS_TYPES].sort())
|
|
})
|
|
})
|