Files
crawler-plugin/admin-frontend-vue/tests/task-48.test.ts
T
huangzd1997 fba8e35118 task-48(账号/权限页面): 实现用户角色字段映射
新增 user-role-map.ts 纯映射:roleDomainOf 行 role/is_admin → 角色域(空 role 按 is_admin
推断、未知归 normal)、selectRoleOf 超管不可选、USER_ROLE_OPTIONS 下拉候选 admin|normal、
userRoleLockMessage 镜像后端约束(超管不可改、admin 不可调角色)。8 用例全过,347 单测 + build 绿。
2026-09-05 15:04:35 +08:00

68 lines
3.0 KiB
TypeScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readSource } from './helpers.ts'
import {
roleDomainOf,
selectRoleOf,
userRoleLockMessage,
roleDomainLabel,
USER_ROLE_OPTIONS,
} from '../src/pages/account/user-role-map.ts'
test('test_task_048_user_role_map_normal_primary_path', () => {
// 正常主路径:role 字段直接映射为角色域。
assert.equal(roleDomainOf({ role: 'admin' }), 'admin')
assert.equal(roleDomainOf({ role: 'normal' }), 'normal')
assert.equal(roleDomainOf({ role: 'super_admin' }), 'super_admin')
})
test('test_task_048_user_role_map_normal_variant_input', () => {
// 正常变体:role 为空时按 is_admin 布尔推断(1=管理员);未知角色归 normal。
assert.equal(roleDomainOf({ role: '', isAdmin: true }), 'admin')
assert.equal(roleDomainOf({ role: '', isAdmin: false }), 'normal')
assert.equal(roleDomainOf({ role: 'biz', isAdmin: false }), 'normal')
})
test('test_task_048_user_role_map_normal_repeated_operation_is_idempotent', () => {
// 正常重复:映射为纯函数、不改输入、结果稳定。
const row = { role: 'admin', isAdmin: true }
assert.equal(roleDomainOf(row), 'admin')
assert.equal(roleDomainOf(row), 'admin')
assert.equal(row.role, 'admin')
})
test('test_task_048_user_role_map_boundary_empty_input', () => {
// 边界空值:缺省/空角色行回退 normal,不崩溃。
assert.equal(roleDomainOf({}), 'normal')
assert.equal(selectRoleOf({}), 'normal')
})
test('test_task_048_user_role_map_boundary_single_item', () => {
// 边界单元素:admin 单行 → 编辑可选 admin。
assert.equal(selectRoleOf({ role: 'admin' }), 'admin')
})
test('test_task_048_user_role_map_boundary_limit_or_missing_field', () => {
// 边界上限:超级管理员不可作为可编辑角色(null);候选不含 super_admin。
assert.equal(selectRoleOf({ role: 'super_admin' }), null)
const values = USER_ROLE_OPTIONS.map((o) => o.value)
assert.deepEqual([...values].sort(), ['admin', 'normal'])
assert.equal(values.includes('super_admin'), false)
})
test('test_task_048_user_role_map_invalid_input_rejected', () => {
// 异常输入:目标为超管/操作者非超管给出可操作锁定原因。
assert.match(userRoleLockMessage('super_admin', { role: 'super_admin' }) || '', /超级管理员/)
assert.match(userRoleLockMessage('admin', { role: 'normal' }) || '', /超管|管理员/)
assert.equal(userRoleLockMessage('super_admin', { role: 'normal' }), undefined)
})
test('test_task_048_user_role_map_dependency_failure_returns_actionable_message', () => {
// 依赖失败/可操作:角色映射为纯逻辑、无 axios/http;展示文案与域一一对应。
const mod = readSource('src/pages/account/user-role-map.ts')
assert.equal(/axios|http\./.test(mod), false, '角色映射保持纯逻辑')
assert.equal(roleDomainLabel('admin'), '管理员')
assert.equal(roleDomainLabel('normal'), '普通用户')
assert.equal(roleDomainLabel('super_admin'), '超级管理员')
})