20260920220520

This commit is contained in:
oneao committed 2026-09-20 22:05:20 +08:00
1 parent 6f03d92425
commit b3364fe6dc
44 files changed
+2313 -2737

No files matched your search

@@ -5,11 +5,13 @@
*/
import { describe, expect, it } from 'vitest'
import {
GROUP_CODE_MAX_LENGTH,
collectGroupIds,
nextGroupId,
normalizeGroupIds,
parseSchema,
schemaToJsonText,
validateGroupCode,
} from '@/utils/schemaJson'
const group = (over = {}) => ({ type: 'group', title: '分组', children: [], ...over })
@@ -89,3 +91,72 @@ describe('normalizeGroupIds', () => {
expect(() => normalizeGroupIds('view', parseSchema('view', '{bad json'))).not.toThrow()
})
})
describe('validateGroupCode', () => {
const REQUIRED = '分组编码不能为空'
const FORMAT = '分组编码须以小写字母开头,且只能包含小写字母、数字和下划线'
const LENGTH = `分组编码长度需为 2-${GROUP_CODE_MAX_LENGTH} 个字符`
it('空值报不能为空', () => {
expect(validateGroupCode('')).toBe(REQUIRED)
expect(validateGroupCode(' ')).toBe(REQUIRED)
expect(validateGroupCode(null)).toBe(REQUIRED)
expect(validateGroupCode(undefined)).toBe(REQUIRED)
})
it('长度需落在 2 到上限之间(含边界)', () => {
expect(validateGroupCode('a')).toBe(LENGTH)
expect(validateGroupCode('a'.repeat(GROUP_CODE_MAX_LENGTH + 1))).toBe(LENGTH)
expect(validateGroupCode('ab')).toBe(true)
expect(validateGroupCode('a'.repeat(GROUP_CODE_MAX_LENGTH))).toBe(true)
})
it('格式与模块编码同构:小写字母开头,只含小写字母、数字与下划线', () => {
for (const code of ['1abc', 'Abc', 'a-b', 'a b', '分组']) {
expect(validateGroupCode(code)).toBe(FORMAT)
}
})
it('与本配置已有编码冲突时提示已被使用', () => {
const message = validateGroupCode('basic', { existingIds: ['basic'] })
expect(message).toContain('basic')
expect(message).toContain('已被本配置的其他分组使用')
expect(message).not.toContain('多语言')
})
it('与多语言资源里残留的编码冲突时给出可区分的提示', () => {
const message = validateGroupCode('group2', { reservedIds: ['group2'] })
expect(message).toContain('多语言')
})
it('合法自定义编码通过', () => {
expect(validateGroupCode('basic_info')).toBe(true)
expect(validateGroupCode('cw_fee_detail')).toBe(true)
})
it('group{n} 形式不冲突即允许', () => {
expect(validateGroupCode('group7', { existingIds: ['group1'], reservedIds: ['group2'] })).toBe(
true,
)
})
it('编码集合同时兼容数组与 Set', () => {
expect(validateGroupCode('basic', { existingIds: new Set(['basic']) })).toBe(
validateGroupCode('basic', { existingIds: ['basic'] }),
)
})
it('输入首尾空白归一后再比对与判定', () => {
expect(validateGroupCode(' basic ', { existingIds: ['basic'] })).toContain('basic')
expect(validateGroupCode(' ok_code ')).toBe(true)
})
it('保留字 ungrouped 不可占用(结构树虚拟未分组节点用 g:ungrouped)', () => {
expect(validateGroupCode('ungrouped')).toContain('系统保留字')
})
it('长度上限可覆盖', () => {
expect(validateGroupCode('a'.repeat(11), { maxLength: 10 })).toContain('2-10')
expect(validateGroupCode('a'.repeat(10), { maxLength: 10 })).toBe(true)
})
})