Files
dacli/tests/unit/role_authorize.test.ts
sushub f403a7e80e import: ssw/dacli@eea0ebc (2026-08-12) 起点版本
本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的
dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。

- 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12)
- 历史未带 ssw main 整体历史(避免大仓污染)
- 此为新仓基线 commit,未来以本仓历史为主
2026-08-12 15:58:06 +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([])
})
})