Files
dacli/tests/unit/role_authorize.test.ts
T
sswandClaude a4fec463ca feat(dacli): mirror authority and read-only form controls
- dacli/src/webapi/authority.ts: 真镜像 web/apps/jnpf-web-apps-main/src/api/permission/auth.ts (getAuthValues/updateAuthList, 0 复刻 0 漂移)
- dacli/src/commands/role/authorize.ts + _authorize.ts: 角色绑定菜单 (sswflow M1 §11 必需)
- dacli/src/commands/form/update.ts + _readonly.ts: form update --read-only 锁定表单为只读 (设计 §4.3 受保护字段)
- 18/18 单测 PASS (validateAuthorizeInput/buildAuthorizePayload/makeFormReadOnly)

Co-Authored-By: Claude <[email protected]>
2026-08-01 01:59:27 +08:00

94 lines
3.0 KiB
TypeScript

/**
* tests/unit/role_authorize.test.ts — sswflow M1 Task 9
*
* 镜像架构 0 复刻 0 漂移: 测试必须断言契约 (id 校验 + payload shape),
* 命令执行通过 webapi 真镜像 (不在本测试覆盖范围, 已有 manual 跑过).
*/
import { describe, it, expect } from 'vitest'
import {
validateAuthorizeInput,
buildAuthorizePayload,
} from '../../src/commands/role/_authorize.ts'
describe('role authorize — validateAuthorizeInput (sswflow M1 Task 9)', () => {
it('menuIds 解析为字符串数组', () => {
const r = validateAuthorizeInput({
roleId: '643238393602117',
menuIds: ['12345678901', '12345678902', '12345678903'],
})
expect(r.ok).toBe(true)
if (r.ok) expect(r.ids).toEqual(['12345678901', '12345678902', '12345678903'])
})
it('--moduleIds 与 --menuIds 等价 (web 字段名 moduleId)', () => {
const r = validateAuthorizeInput({
roleId: '643238393602117',
moduleIds: ['12345678901', '12345678902'],
})
expect(r.ok).toBe(true)
if (r.ok) expect(r.ids).toEqual(['12345678901', '12345678902'])
})
it('拒绝非数字 roleId 提前拦下 (避免 500)', () => {
const r = validateAuthorizeInput({ roleId: 'admin', menuIds: ['12345678901'] })
expect(r.ok).toBe(false)
})
it('要求 menuIds/moduleIds 至少一个', () => {
const r = validateAuthorizeInput({ roleId: '643238393602117' })
expect(r.ok).toBe(false)
})
it('空数组被拒', () => {
const r = validateAuthorizeInput({ roleId: '643238393602117', menuIds: [] })
expect(r.ok).toBe(false)
})
it('单个非数字 menuId 被拒', () => {
const r = validateAuthorizeInput({
roleId: '643238393602117',
menuIds: ['12345678901', 'abc', '12345678903'],
})
expect(r.ok).toBe(false)
})
it('roleId 短于 10 位被拒', () => {
const r = validateAuthorizeInput({ roleId: '123', menuIds: ['12345678901'] })
expect(r.ok).toBe(false)
})
it('menuIds 是逗号分隔字符串时也能解析 (CLI 参数兼容)', () => {
// CLI: --menuIds="12345678901,12345678902"
// 调用方需要先 split(','), 这里 helper 仅接 string[] 入参
const r = validateAuthorizeInput({
roleId: '643238393602117',
menuIds: ['12345678901', '12345678902'],
})
expect(r.ok).toBe(true)
if (r.ok) expect(r.ids).toHaveLength(2)
})
})
describe('role authorize — buildAuthorizePayload (web 后端契约)', () => {
it('镜像 web permission/auth.ts.updateAuthList (data.moduleId = 菜单 ids)', () => {
const p = buildAuthorizePayload('643238393602117', [
'12345678901',
'12345678902',
'12345678903',
])
expect(p.objectId).toBe('643238393602117')
expect(p.data.moduleId).toEqual([
'12345678901',
'12345678902',
'12345678903',
])
})
it('data.button/column/resource/form 留空', () => {
const p = buildAuthorizePayload('643238393602117', ['12345678901'])
expect(p.data.button).toEqual([])
expect(p.data.column).toEqual([])
expect(p.data.resource).toEqual([])
expect(p.data.form).toEqual([])
})
})