Files
crawler-plugin/admin-frontend-vue/tests/task-48.test.ts
T

165 lines
8.2 KiB
TypeScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readdirSync } from 'node:fs'
import { join } from 'node:path'
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,
)
// 2026-11-17 V206:弹窗新增「所属平台」单选(amazon/jd 两档)——角色按钮仍是前两枚,
// 平台按钮单独钉,四出口文案对平的意图不变。
assert.deepEqual(buttons.slice(0, 2), [['normal', word], ['admin', '管理员']], '编辑用户弹窗的角色按钮文案')
assert.deepEqual(buttons.slice(2), [['amazon', '亚马逊'], ['jd', '京东']], '所属平台单选两档(V206 翻档归侧的唯一入口)')
})
/**
* 2026-10-05 定稿:admin 档只叫「管理员」,normal 档只叫「普通账号」。
* 旧叫法「普通管理员」「普通用户」当天清了三轮才清干净(列表列、下拉、角色域、弹窗按钮四份出口
* 各写一套,后端还留着 6 条 403 与 Swagger 说明),所以钉成尺子而不是记在脑子里。
* 判据只扫两棵源码树的源码扩展名:`.playwright-cli/*.yml` 那批 09-12 的页面快照里印着旧词,
* 那是历史证据不是口径;`tests/` 也不扫——本文件的禁用清单自己就要写这两个词。
*/
const RETIRED_ROLE_WORDS = ['普通管理员', '普通用户'] as const
/** 扫描面。minFiles 是 2026-10-05 实测分母(216 / 1415)的下限,mustInclude 钉"该扫到的文件真在集合里"。 */
const ROLE_WORD_SCOPE: ReadonlyArray<{
readonly dir: string
readonly exts: readonly string[]
readonly minFiles: number
readonly mustInclude: readonly string[]
}> = [
{
dir: 'src',
exts: ['.ts', '.tsx', '.vue', '.js'],
minFiles: 200,
mustInclude: ['src/types/admin.ts', 'src/pages/account/user-role-map.ts', 'src/pages/account/EditUserDialog.vue'],
},
{
dir: '../backend-java/src',
exts: ['.java'],
minFiles: 1300,
mustInclude: [
'../backend-java/src/main/java/com/nanri/aiimage/modules/permission/service/PermissionMenuService.java',
'../backend-java/src/main/java/com/nanri/aiimage/modules/admin/service/AdminUserService.java',
],
},
]
function walkSourceFiles(dir: string, exts: readonly string[], out: string[] = []): string[] {
for (const entry of readdirSync(join(process.cwd(), dir), { withFileTypes: true })) {
const rel = `${dir}/${entry.name}`
if (entry.isDirectory()) {
if (entry.name === 'node_modules' || entry.name === 'dist' || entry.name.startsWith('.')) continue
walkSourceFiles(rel, exts, out)
} else if (exts.some((ext) => entry.name.endsWith(ext))) {
out.push(rel)
}
}
return out
}
test('test_task_048_retired_role_words_are_gone_from_source_trees', () => {
const problems: string[] = []
for (const scope of ROLE_WORD_SCOPE) {
const files = walkSourceFiles(scope.dir, scope.exts)
assert.ok(
files.length >= scope.minFiles,
`${scope.dir} 只扫到 ${files.length} 个文件(下限 ${scope.minFiles}):目录或扩展名写错了,这条尺子没在判任何东西`,
)
for (const needle of scope.mustInclude) {
assert.ok(files.includes(needle), `${scope.dir} 的扫描集合里没有 ${needle},取样面漏了出口文件`)
}
for (const rel of files) {
const lines = readSource(rel).split('\n')
lines.forEach((line, index) => {
for (const word of RETIRED_ROLE_WORDS) {
if (line.includes(word)) problems.push(`${rel}:${index + 1} 出现旧叫法「${word}」→ ${line.trim().slice(0, 60)}`)
}
})
}
}
assert.deepEqual(problems, [], '角色旧叫法回流:' + problems.join(' | '))
})