152 lines
9.4 KiB
TypeScript
152 lines
9.4 KiB
TypeScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readSource } from './helpers.ts'
|
||
import { normalizeUserPageParams, toUserListQuery } from '../src/api/users-dto.ts'
|
||
import { toAdminUserItem } from '../src/api/users-model.ts'
|
||
import {
|
||
createUserFilterState,
|
||
isFilterActive,
|
||
toUserListParams,
|
||
} from '../src/pages/account/users-filter.ts'
|
||
import { topbarUserOf } from '../src/layout/topbar-model.ts'
|
||
|
||
/**
|
||
* 公司维度不出现在后台前端(2026-10-04 15:4x 定案,推翻一期 §2.3/§5.1 的"列表加公司列 + 筛选 + 顶栏公司名")。
|
||
*
|
||
* <p>不显示 ≠ 不存在:建号请求体不带 company_id,归属由后端
|
||
* `AdminUserService#resolveTargetCompanyId` 落操作者自己的公司;跨公司隔离发生在消费侧与读取侧
|
||
* (二期批次 2/3),不依赖页面把公司名渲染出来。所以这里的断言全部打在"不得回流"上:
|
||
* 任何一处重新渲染公司,本文件就红。</p>
|
||
*
|
||
* <p><b>2026-10-05 批次 4 修订(控制方点名「用户管理也要区分公司 tab 的数据」)</b>:
|
||
* 摘掉的是<b>渲染层</b>的公司维度——行级公司列、「所属公司」标签、筛选状态里的公司、顶栏公司名,这些仍不得回流
|
||
* (界面暴露要单独批准,而且 tab 已经是公司选择器,页面里再加一个公司筛选就是第二套口径)。
|
||
* <b>请求层</b>的 company_id 是这次明确要加回来的,且取值口唯一:`companyTab.requestCompanyId`。
|
||
* 下面 `companyId 的取值口唯一` 那条就是这条新边界的尺子。</p>
|
||
*/
|
||
|
||
test('公司列不再写进列表项,但其余字段必须照常解析', () => {
|
||
const item = toAdminUserItem({
|
||
id: 7,
|
||
username: '张三',
|
||
role: 'normal',
|
||
company_id: 2,
|
||
company_name: '福州数富',
|
||
created_by_id: 5,
|
||
creator_username: 'admin',
|
||
})
|
||
assert.ok(item, '解析结果不该为 null')
|
||
assert.equal('companyId' in item, false, '后端仍会带 company_id/company_name,前端不再往列表项里写')
|
||
assert.equal('companyName' in item, false)
|
||
// 反向自检:不许靠"整条解析都坏了"变绿
|
||
assert.equal(item.id, 7)
|
||
assert.equal(item.username, '张三')
|
||
assert.equal(item.createdById, 5)
|
||
assert.equal(item.creatorUsername, 'admin')
|
||
})
|
||
|
||
test('筛选状态与查询参数里都没有公司这一维(公司值只从 fetchUserList 的原始入参走)', () => {
|
||
const state = createUserFilterState()
|
||
assert.equal('companyId' in state, false)
|
||
assert.deepEqual(state, { keyword: '', createdByAdminId: null, role: '', page: 1, pageSize: 10 })
|
||
|
||
const params = toUserListParams({ ...state, keyword: ' x ', createdByAdminId: 4 })
|
||
assert.equal('companyId' in params, false)
|
||
assert.equal(isFilterActive({ ...state, keyword: ' ' }), false)
|
||
|
||
const query = toUserListQuery(normalizeUserPageParams({ page: 2, pageSize: 20, keyword: 'x' }))
|
||
assert.equal('company_id' in query, false, '两只白名单构造器都不造公司值')
|
||
|
||
// 批次 4 的落点证据:normalizeUserPageParams 是白名单构造(返回对象只点名 page/pageSize/keyword/…),
|
||
// 传进 companyId 也会被丢 ⇒ users.ts 必须读**原始入参** params.companyId。
|
||
// 写成 normalized.companyId 时 TS 照样绿(字段在 UserListParams 上声明着),运行时恒 undefined——
|
||
// 这正是这条要拦的形状,所以它比的是行为不是字符串。
|
||
const dropped = normalizeUserPageParams({ page: 1, pageSize: 10, companyId: 3 })
|
||
assert.equal('companyId' in dropped, false,
|
||
'归一化结果里出现了 companyId ⇒ 白名单构造改了,users.ts 那句读原始入参的注释与实现得一起对账')
|
||
assert.equal(dropped.page, 1, '反向自检:归一化本身没坏(不许靠整只函数报废变绿)')
|
||
})
|
||
|
||
test('顶栏不再渲染公司名', () => {
|
||
const vm = topbarUserOf({ id: 1, username: 'admin', role: 'admin', companyName: '厦门米椿花' } as never)
|
||
assert.equal('company' in vm, false)
|
||
assert.deepEqual(vm, { username: 'admin', role: '管理员', hasUser: true })
|
||
assert.equal(topbarUserOf(null).hasUser, false)
|
||
})
|
||
|
||
// 反向钉:公司维度不得回流到「行渲染/筛选状态/顶栏」(2026-10-04 定口径;2026-10-05 修订——
|
||
// ①建号弹窗里的「公司超级管理员」所属公司下拉,由 company-user-create.test.ts 单独钉住;
|
||
// ②列表请求带 company_id 是批次 4 明确加回来的,尺子换成下面的「取值口唯一」)
|
||
test('账号页与顶栏的源码里公司维度已摘干净', () => {
|
||
const page = readSource('src/pages/account/UsersPage.vue')
|
||
assert.equal(/所属公司/.test(page), false, '账号页不该再出现「所属公司」标签')
|
||
// 渲染层与筛选状态不许有公司维度;snake_case 的 company_id 更不许在页面里手拼(那是 api 层的事)。
|
||
assert.equal(/filters\.company|row\.company|company_id/.test(page), false,
|
||
'行渲染/筛选状态里回流了公司维度,或页面自己拼了 snake_case 查询键')
|
||
assert.match(page, /fetchCompanies/, '公司目录仅供建号弹窗的「公司超管」下拉使用(唯一豁免)')
|
||
assert.match(page, /isSuperAdmin[\s\S]{0,200}fetchCompanies/,
|
||
'拉公司目录必须按站点超管门控,非超管不发这个请求(后端 403 兜底)')
|
||
|
||
// 「删了列忘了改占位行 colspan」是这类改动最常留下的残行:按表头列数交叉核对
|
||
// (`<th` 是 `<thead` 的前缀,必须带上后随字符才数得准)。
|
||
// 2026-11-17 V206:上架上限列 v-if="isJdSide" 按平台动态渲染 ⇒ 表头数 10(含源码里的 v-if th),
|
||
// 占位行走 :colspan 动态绑定;字面量 colspan 在动态列下必然错位一半的 tab,反向钉死。
|
||
const headers = (page.match(/<th[\s>]/g) || []).length
|
||
assert.equal(headers, 10, `用户列表表头应为 10 列(9 列存量 + 京东侧上架上限 v-if 列),实际 ${headers}`)
|
||
assert.match(page, /:colspan="isJdSide \? 10 : 9"/, '空态占位行必须按平台动态切 colspan(静态值与动态列数错位一半 tab)')
|
||
assert.equal(/colspan="\d+"/.test(page), false, '动态列下不许再留字面量 colspan(必有一侧 tab 列数对不上)')
|
||
|
||
const dialog = readSource('src/pages/account/CreateUserDialog.vue')
|
||
assert.match(dialog, /operatorSuper && form\.role === 'company_super_admin'/,
|
||
'建号弹窗的公司下拉只许在「站点超管 + 公司超管角色」时出现(例外口径见 company-user-create.test.ts)')
|
||
assert.equal(/selectableCompanies|initialCompanyId/.test(dialog), false,
|
||
'一次性预选/收窄的旧公司逻辑不许回流')
|
||
|
||
const layout = readSource('src/layout/AdminLayout.vue')
|
||
assert.equal(/userVm\.company|admin-user-company/.test(layout), false, '顶栏旁白只剩用户名与角色')
|
||
|
||
// 视图模型只删映射、留着 company 字段="接口说有、运行时永远空",是最难看出来的一种回流
|
||
const topbar = readSource('src/layout/topbar-model.ts')
|
||
assert.equal(/compan/i.test(topbar), false, '顶栏模型里不该再出现公司字段(连类型一起删)')
|
||
|
||
const filter = readSource('src/pages/account/users-filter.ts')
|
||
assert.equal(/companyId/.test(filter), false)
|
||
const dto = readSource('src/api/users-dto.ts')
|
||
// 批次 4 之后 DTO 里**有**公司字段(UserListParams.companyId / UserListQuery.company_id),
|
||
// 但只许当类型声明用:两只构造函数一旦开始造这个值,页面的 requestCompanyId 就成了第二套口径的摆设。
|
||
for (const fn of ['normalizeUserPageParams', 'toUserListQuery']) {
|
||
assert.doesNotMatch(bodyOf(dto, fn), /compan/i,
|
||
`users-dto 的 ${fn} 里出现了公司值 ⇒ 取值口从页面挪进了构造器`)
|
||
}
|
||
const model = readSource('src/api/users-model.ts')
|
||
assert.equal(/company_id|company_name/.test(model), false, '列表项解析不认后端回传的公司列')
|
||
})
|
||
|
||
/** 花括号配对取函数体(整文件比字符串会被类型声明和注释骗)。 */
|
||
function bodyOf(source: string, name: string): string {
|
||
const at = source.indexOf(`function ${name}(`)
|
||
assert.notEqual(at, -1, `${name} 不在了——先确认它不是被改名/删掉了`)
|
||
const open = source.indexOf('{', at)
|
||
let depth = 0
|
||
for (let i = open; i < source.length; i++) {
|
||
if (source[i] === '{') depth++
|
||
else if (source[i] === '}') { depth--; if (depth === 0) return source.slice(open, i + 1) }
|
||
}
|
||
assert.fail(`${name} 花括号不配对,解析失败`)
|
||
}
|
||
|
||
test('列表请求的公司值取值口唯一:每处 companyId 都指向 tab 的 requestCompanyId', () => {
|
||
const page = readSource('src/pages/account/UsersPage.vue')
|
||
// 分母从真源码现算,不是常量和自己比:0 处=批次 4 的接线被摘掉 ⇒ 三家公司又看到同一份全量列表。
|
||
const assigned = [...page.matchAll(/companyId:\s*([^,\n}]+)/g)].map((m) => m[1].trim())
|
||
assert.ok(assigned.length >= 1, 'UsersPage 已不再把公司值放进列表请求 ⇒ 用户页的公司隔离断了')
|
||
for (const value of assigned) {
|
||
assert.equal(value, 'companyTab.requestCompanyId',
|
||
`companyId 的取值口不唯一(${value}):请求体里的公司值只许来自公司 tab 的唯一取值口`)
|
||
}
|
||
// 建号请求仍然不带 company_id(归属由后端从操作者推导):弹窗那侧不许出现这个键。
|
||
const dialog = readSource('src/pages/account/CreateUserDialog.vue')
|
||
assert.equal(/companyId:|company_id:/.test(dialog), false,
|
||
'建号请求体不许带 company_id——那是 2026-10-04 定死的口径,跟列表读侧无关')
|
||
})
|