Files
crawler-plugin/admin-frontend-vue/tests/task-48.test.ts
T
huangzd1997 b4db01d320 fix(账号文案): normal 的中文口径四出口对平为「普通账号」,并加对平护栏
「编辑用户」弹窗写「普通账号」,而同一页的角色列/顶部栏(roleLabel)、新建下拉
(USER_ROLE_OPTIONS)、roleDomainLabel 三份映射写「普通用户」——同一页自相矛盾,且
没有任何尺子拦得住(各写各的,改一处不会让别处红)。口径按 2026-10-05 定为「普通账号」。

改三处 src:types/admin.ts roleLabel('normal')、user-role-map.ts 的 USER_ROLE_OPTIONS
与 ROLE_DOMAIN_LABELS。EditUserDialog 的两个角色按钮本来就写「普通账号」,一字未动。

新增 test_task_048_normal_role_wording_agrees_across_all_exits 把四出口对平:三份映射表
按值断言,弹窗那侧钉模板形状(<el-radio-button value="xxx">文案< 逐条 deepEqual),
不钉 token——只出现过"普通账号"四个字是判不出按钮丢没丢的。

变异自证 3 发全杀:① roleLabel('normal') 退回「普通用户」⇒ 红;② 弹窗按钮文案改成
「普通用户」⇒ 红,且报出 actual [['normal','普通用户'],['admin','管理员']];
③ 弹窗按钮写法换成 :value="'normal'"(正则抓不到那一条)⇒ 红,actual 只剩 admin 一条
——这一发是证明它不是恒绿:形状对不上时缺的那条会以"少一条"现形,而不是静默通过。
三次注入均按原字节还原,EditUserDialog.vue 的 git diff 为空。

验证:node --test 全量 1738 例 0 红、vue-tsc --noEmit rc=0、浏览器实测三处可见出口
(列表角色列 / 新建下拉 / 编辑弹窗)均显示「普通账号」。
2026-10-05 22:44:21 +08:00

91 lines
4.8 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'
import { roleLabel } from '../src/types/admin.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')
// 2026-10-05 新增角色:公司超级管理员(数据只看本公司,由站点超管开设)
assert.equal(roleDomainOf({ role: 'company_super_admin' }), 'company_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);候选含公司超管但绝不含站点超管。
assert.equal(selectRoleOf({ role: 'super_admin' }), null)
assert.equal(selectRoleOf({ role: 'company_super_admin' }), null)
const values = USER_ROLE_OPTIONS.map((o) => o.value)
assert.deepEqual([...values].sort(), ['admin', 'company_super_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)
// 2026-10-05:公司超管目标只有站点超管能编辑(改密码/到期时间做续费),角色锁定要单独说清
assert.match(userRoleLockMessage('super_admin', { role: 'company_super_admin' }) || '', /公司超级管理员/)
})
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'), '超级管理员')
assert.equal(roleDomainLabel('company_super_admin'), '公司超级管理员')
})
test('test_task_048_normal_role_wording_agrees_across_all_exits', () => {
// 2026-10-05 定口径:normal 一律显示「普通账号」。这句文案此前有四份出口 —— 列表角色列与顶部栏
// 走 roleLabel、新建下拉走 USER_ROLE_OPTIONS、还有 roleDomainLabel,而编辑弹窗的两个角色按钮是
// 硬编码在模板里的。前三份写「普通用户」、第四份写「普通账号」,同一页自相矛盾且没有尺子拦得住。
// 这条把四出口对平:任何一处换词、或弹窗那侧改成别的写法,都会红。
const word = '普通账号'
assert.equal(roleLabel('normal'), word, '用户列表角色列与顶部栏(roleLabel)')
assert.equal(roleDomainLabel('normal'), word, '角色域展示文案(roleDomainLabel)')
assert.equal(USER_ROLE_OPTIONS.find((o) => o.value === 'normal')?.label, word, '新建用户下拉候选')
const dialog = readSource('src/pages/account/EditUserDialog.vue')
const buttons = [...dialog.matchAll(/<el-radio-button\s+value="([a-z_]+)">([^<]*)</g)].map(
(m) => [m[1], m[2]] as const,
)
assert.deepEqual(buttons, [['normal', word], ['admin', '管理员']], '编辑用户弹窗的角色按钮文案')
})