fix(前端): 后台摘掉公司这一维度——列表列/筛选/建号选择/顶栏旁白全部去掉
用户定案:隔离该由数据和后端自己做,不该把"公司"摆到操作员眼前多一个选错的机会。 于是原计划里给品牌库/去重库两页加公司列(Task 9)反向执行,顺带把一期已上线的展示也收掉。 建号归属改为请求体不带 company_id,由后端 resolveTargetCompanyId 落操作者自己的公司。 这条前端删下拉的安全依据完全靠后端那两条分支撑着,所以用跨仓读 Java 源码的方式钉成可执行断言: 谁把那边改成"必填公司"或删掉任一分支,这边立刻红。 副作用要说清——超管建的号一律归公司 1,给第 2 家建号得用那家公司自己的管理员登录。 「公司管理」页的引导语原写"建号时选哪家公司",下拉没了就成了错的承诺; "停用后不能再用于新建账号"也不再成立(requireActive 两个调用点都要求显式传 company_id, 而页面现在一律不传)。文案改成真实口径,并断言这三句失效说法不得回流。 1683 例 / 0 红,vue-tsc 干净,变异 6/6 被杀。
This commit is contained in:
@@ -1,72 +1,114 @@
|
||||
import { test } from 'node:test'
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
import {
|
||||
initialCompanyId,
|
||||
selectableCompanies,
|
||||
createEmptyCreateUserForm,
|
||||
toCreateUserRequest,
|
||||
validateCreateUserForm,
|
||||
createEmptyCreateUserForm,
|
||||
} from '../src/pages/account/user-create-model.ts'
|
||||
import { COMPANY_STATUS_ACTIVE, COMPANY_STATUS_DISABLED, type CompanyItem } from '../src/api/companies-dto.ts'
|
||||
import { OPERATION_GUIDES } from '../src/layout/operation-guides.ts'
|
||||
|
||||
/** 建号表单的公司归属(设计文档 §2.2):超管必须显式选一家;公司内 admin 只能落自己公司。 */
|
||||
|
||||
const xiamen: CompanyItem = { id: 1, companyName: '厦门米椿花', status: COMPANY_STATUS_ACTIVE, remark: null, createdAt: null, updatedAt: null }
|
||||
const fuzhou: CompanyItem = { id: 2, companyName: '福州数富', status: COMPANY_STATUS_ACTIVE, remark: null, createdAt: null, updatedAt: null }
|
||||
const stopped: CompanyItem = { id: 3, companyName: '已停用公司', status: COMPANY_STATUS_DISABLED, remark: null, createdAt: null, updatedAt: null }
|
||||
const ALL = [xiamen, fuzhou, stopped]
|
||||
|
||||
const SUPER = { role: 'super_admin', companyId: 1 }
|
||||
const ADMIN = { role: 'admin', companyId: 2 }
|
||||
/**
|
||||
* 建号不再让用户挑公司(2026-10-04 15:4x 定案)。
|
||||
*
|
||||
* <p>归属这件事改由后端单点决定:请求体不带 company_id 时,`resolveTargetCompanyId` 落操作者自己的公司。
|
||||
* 所以"前端删掉下拉是安全的"这个结论完全靠那条后端分支撑着 —— 下面最后一用例把它钉成可执行断言,
|
||||
* 谁把那边改成"必填公司",这边立刻红。</p>
|
||||
*/
|
||||
|
||||
function form(overrides: Record<string, unknown> = {}) {
|
||||
return { ...createEmptyCreateUserForm(), username: 'zhangsan', password: 'secret123', ...overrides }
|
||||
}
|
||||
|
||||
test('超管可选公司 = 启用中的公司,停用的不出现', () => {
|
||||
assert.deepEqual(selectableCompanies(ALL, SUPER).map((c) => c.id), [1, 2])
|
||||
test('空表单里没有公司这一项', () => {
|
||||
const blank = createEmptyCreateUserForm()
|
||||
assert.equal('companyId' in blank, false)
|
||||
assert.deepEqual(blank, { username: '', password: '', role: 'normal', columnIds: [], createdBy: null })
|
||||
})
|
||||
|
||||
test('公司内 admin 只列自己公司,不看状态', () => {
|
||||
// 后端对非超管直接落 operator.companyId、不校验启用状态;前端跟着把本公司列出,
|
||||
// 否则会出现「下拉里没得选、提交却能建号」的自相矛盾。
|
||||
assert.deepEqual(selectableCompanies(ALL, ADMIN).map((c) => c.id), [2])
|
||||
const ownStopped = [{ ...xiamen, status: COMPANY_STATUS_DISABLED }, fuzhou]
|
||||
assert.deepEqual(selectableCompanies(ownStopped, { role: 'admin', companyId: 1 }).map((c) => c.id), [1])
|
||||
})
|
||||
|
||||
test('操作者没有公司归属时不给出任何可选项', () => {
|
||||
assert.deepEqual(selectableCompanies(ALL, { role: 'admin', companyId: null }), [])
|
||||
assert.deepEqual(selectableCompanies(ALL, { role: 'admin' }), [])
|
||||
})
|
||||
|
||||
test('超管不选公司就是校验不过,不能静默落自己公司', () => {
|
||||
const missing = validateCreateUserForm(form({ companyId: null }), SUPER)
|
||||
assert.equal(missing.valid, false)
|
||||
assert.match(missing.errors.company || '', /公司/)
|
||||
assert.equal(validateCreateUserForm(form({ companyId: 2 }), SUPER).valid, true)
|
||||
})
|
||||
|
||||
test('非超管不要求选公司(后端强制落自己公司)', () => {
|
||||
assert.equal(validateCreateUserForm(form({ companyId: null }), ADMIN).valid, true)
|
||||
})
|
||||
|
||||
test('不带操作者时保持原有校验口径(老调用方不受影响)', () => {
|
||||
assert.equal(validateCreateUserForm(form()).valid, true)
|
||||
test('校验只看用户名与密码,不再要求选公司', () => {
|
||||
assert.deepEqual(validateCreateUserForm(form()), { valid: true, errors: {} })
|
||||
assert.equal(validateCreateUserForm(form({ username: 'a' })).errors.username !== undefined, true)
|
||||
assert.equal(validateCreateUserForm(form({ password: '123' })).errors.password !== undefined, true)
|
||||
})
|
||||
|
||||
test('请求体带 camelCase companyId,未选时不带该键', () => {
|
||||
assert.equal(toCreateUserRequest(form({ companyId: 2 })).companyId, 2)
|
||||
assert.equal('companyId' in toCreateUserRequest(form({ companyId: null })), false)
|
||||
assert.equal('companyId' in toCreateUserRequest(form({ companyId: 0 })), false)
|
||||
test('请求体不带 companyId,其余字段照旧', () => {
|
||||
assert.deepEqual(toCreateUserRequest(form({ columnIds: [3, 1] })), {
|
||||
username: 'zhangsan',
|
||||
password: 'secret123',
|
||||
role: 'normal',
|
||||
columnIds: [3, 1],
|
||||
})
|
||||
// 所属管理员仍按原语义携带(仅 normal 角色)
|
||||
assert.equal(toCreateUserRequest(form({ role: 'normal', createdBy: 5 })).createdById, 5)
|
||||
assert.equal('createdById' in toCreateUserRequest(form({ role: 'admin', createdBy: 5 })), false)
|
||||
assert.equal('companyId' in toCreateUserRequest(form()), false,
|
||||
'不传 company_id 才会走后端"落操作者公司"那条分支')
|
||||
})
|
||||
|
||||
test('弹窗打开时的默认公司:候选只有一个就落它,多个则不替超管决定', () => {
|
||||
// 公司内 admin 只有一家在选,预选上省一步,也不会出现"没选就提交"的空值。
|
||||
assert.equal(initialCompanyId(ADMIN, ALL), 2)
|
||||
// 超管面对多家时必须自己选:默认值等于替他决定账号挂在哪家,这是会写进库的判断。
|
||||
assert.equal(initialCompanyId(SUPER, ALL), null)
|
||||
// 本公司压根不在列表里时不猜(返回 null 让后端按 operator 归属落库)。
|
||||
assert.equal(initialCompanyId(ADMIN, [xiamen]), null)
|
||||
test('建号模型源码里公司口径已摘干净', () => {
|
||||
const mod = readSource('src/pages/account/user-create-model.ts')
|
||||
assert.equal(/companyId|CompanyItem|selectableCompanies|initialCompanyId|CreateOperatorScope/.test(mod), false,
|
||||
'公司相关的类型、可选项收窄函数、预选函数都应整体删除')
|
||||
const dialog = readSource('src/pages/account/CreateUserDialog.vue')
|
||||
assert.equal(/companyId|CompanyItem|所属公司|selectableCompanies/.test(dialog), false,
|
||||
'弹窗不再有公司选择;操作者角色门控(operatorSuper)保留,它管的是角色与所属管理员下拉')
|
||||
assert.match(dialog, /operatorSuper/, '角色/所属管理员仍按操作者是否超管收窄')
|
||||
})
|
||||
|
||||
/**
|
||||
* 「公司管理」页那段引导文案原先写着"建号时选哪家公司"。下拉已经删了,这句话就成了错的承诺:
|
||||
* 管理员会在新建账号弹窗里找一个根本不存在的选择器。
|
||||
* 同理"停用后不能再用于新建账号"也不成立了——requireActive 只在显式传 company_id 时才走,
|
||||
* 而页面现在一律不传(接口直连的老调用方仍受它保护,那条由上一用例盯着)。
|
||||
*/
|
||||
test('「公司管理」引导文案说的是自动继承,不再让操作员挑公司', () => {
|
||||
const guide = OPERATION_GUIDES.admin_companies.text
|
||||
assert.match(guide, /操作者自己所在的公司/, '要写清归属的真实口径:落在操作者本公司')
|
||||
assert.match(guide, /请用那个公司自己的管理员登录|用该公司自己的管理员登录/,
|
||||
'要给别家建号只能换账号登录——不写这句,超管会以为自己建的号能归到别家')
|
||||
for (const stale of ['建号时选哪家公司', '选哪家公司', '停用后不能再用于新建账号']) {
|
||||
assert.equal(guide.includes(stale), false, `文案仍含已失效的说法:${stale}`)
|
||||
}
|
||||
})
|
||||
|
||||
/** 从 Java 源码里按大括号配平切出一个方法体(只看 resolveTargetCompanyId,别把邻居方法的 throw 算进来)。 */
|
||||
function javaMethod(source: string, signature: string): string {
|
||||
const start = source.indexOf(signature)
|
||||
assert.ok(start >= 0, `找不到方法签名:${signature}`)
|
||||
let depth = 0
|
||||
let seen = false
|
||||
for (let i = source.indexOf('{', start); i < source.length; i++) {
|
||||
if (source[i] === '{') {
|
||||
depth++
|
||||
seen = true
|
||||
} else if (source[i] === '}') {
|
||||
depth--
|
||||
if (seen && depth === 0) return source.slice(start, i + 1)
|
||||
}
|
||||
}
|
||||
assert.fail(`${signature} 大括号不配平,切片失败`)
|
||||
return ''
|
||||
}
|
||||
|
||||
test('后端仍然把"没传公司"落成操作者自己公司(前端删下拉的前提)', () => {
|
||||
const java = readSource(
|
||||
'../backend-java/src/main/java/com/nanri/aiimage/modules/admin/service/AdminUserService.java',
|
||||
)
|
||||
// 只切 resolveTargetCompanyId:resolveCompanyMove 里也有 "super_admin".equals(operatorRole) + throw,
|
||||
// 拿整份文件做正则会把它算进来,那条本来就该抛。
|
||||
const body = javaMethod(java, 'private Long resolveTargetCompanyId(').replace(/\s+/g, ' ')
|
||||
|
||||
// 两条分支都必须"落操作者公司":超管不传=自己公司,非超管不传=自己公司。
|
||||
// 少任何一条,页面上就没有别的办法决定归属了——建号会直接失败,而不是退回"归到某一家"。
|
||||
assert.equal(
|
||||
(body.match(/return operator\.getCompanyId\(\);/g) || []).length,
|
||||
2,
|
||||
'resolveTargetCompanyId 应有两处"落操作者公司"(非超管分支 + 超管未传分支),实际 ' + body,
|
||||
)
|
||||
assert.match(body, /if \(!"super_admin"\.equals\(operatorRole\)\) \{.*return operator\.getCompanyId\(\);/)
|
||||
assert.match(body, /if \(requestedCompanyId == null\) \{ return operator\.getCompanyId\(\); \}/,
|
||||
'"没传公司"这条分支是"新账号归操作者本公司"的唯一依据,删掉它前端就没有任何地方能定归属')
|
||||
assert.match(body, /companyService\.requireActive\(requestedCompanyId\)/,
|
||||
'显式传公司时仍要校验启用状态(接口直连的老调用方不会被绕过)')
|
||||
})
|
||||
|
||||
@@ -7,79 +7,86 @@ import {
|
||||
createUserFilterState,
|
||||
isFilterActive,
|
||||
toUserListParams,
|
||||
withFilterReset,
|
||||
} from '../src/pages/account/users-filter.ts'
|
||||
import { topbarUserOf } from '../src/layout/topbar-model.ts'
|
||||
|
||||
/** 公司维度进账号页(设计文档 §2.3/§5.1):列表列、按公司筛选(仅超管)、顶栏公司名。 */
|
||||
/**
|
||||
* 公司维度不出现在后台前端(2026-10-04 15:4x 定案,推翻一期 §2.3/§5.1 的"列表加公司列 + 筛选 + 顶栏公司名")。
|
||||
*
|
||||
* <p>不显示 ≠ 不存在:建号请求体不带 company_id,归属由后端
|
||||
* `AdminUserService#resolveTargetCompanyId` 落操作者自己的公司;跨公司隔离发生在消费侧与读取侧
|
||||
* (二期批次 2/3),不依赖页面把公司名渲染出来。所以这里的断言全部打在"不得回流"上:
|
||||
* 任何一处重新渲染公司,本文件就红。</p>
|
||||
*/
|
||||
|
||||
test('company_user_list_item_reads_snake_case_company_columns', () => {
|
||||
test('公司列不再写进列表项,但其余字段必须照常解析', () => {
|
||||
const item = toAdminUserItem({
|
||||
id: 7,
|
||||
username: '张三',
|
||||
role: 'normal',
|
||||
company_id: 2,
|
||||
company_name: '福州数富',
|
||||
created_by_id: 5,
|
||||
creator_username: 'admin',
|
||||
})
|
||||
assert.equal(item?.companyId, 2)
|
||||
assert.equal(item?.companyName, '福州数富')
|
||||
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('company_user_list_item_ignores_camel_case_company_keys', () => {
|
||||
// 反向排他:后端 AdminUserItemVo 走 @JsonProperty snake_case,解析器若跟着读 camelCase 就是双向猜。
|
||||
const item = toAdminUserItem({ id: 7, username: '张三', role: 'normal', companyId: 9, companyName: '别家' })
|
||||
assert.equal(item?.companyId, undefined)
|
||||
assert.equal(item?.companyName, undefined)
|
||||
test('筛选状态与查询参数里都没有公司这一维', () => {
|
||||
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, '绝不下发 company_id:一旦下发就是"只看这家"的过滤')
|
||||
})
|
||||
|
||||
test('company_user_list_filter_state_defaults_to_no_company', () => {
|
||||
assert.equal(createUserFilterState().companyId, null)
|
||||
// 改了公司筛选要回第一页,与其他筛选条件同规则
|
||||
const next = withFilterReset({ ...createUserFilterState(), page: 5 }, { companyId: 3 })
|
||||
assert.equal(next.companyId, 3)
|
||||
assert.equal(next.page, 1)
|
||||
assert.equal(isFilterActive({ ...createUserFilterState(), companyId: 3 }), true)
|
||||
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)
|
||||
})
|
||||
|
||||
test('company_user_list_query_sends_company_id_only_when_selected', () => {
|
||||
const withCompany = toUserListQuery(normalizeUserPageParams({ page: 1, pageSize: 10, companyId: 3 }))
|
||||
assert.equal(withCompany.company_id, 3)
|
||||
assert.equal(
|
||||
'company_id' in toUserListQuery(normalizeUserPageParams({ page: 1, pageSize: 10 })),
|
||||
false,
|
||||
'未选公司时不下发 company_id,否则后端会当成"只看这家公司"过滤',
|
||||
)
|
||||
})
|
||||
|
||||
test('company_user_list_params_adapter_carries_company_id', () => {
|
||||
const params = toUserListParams({ ...createUserFilterState(), companyId: 2 })
|
||||
assert.equal(params.companyId, 2)
|
||||
})
|
||||
|
||||
test('company_topbar_shows_company_name', () => {
|
||||
assert.equal(
|
||||
topbarUserOf({ id: 1, username: 'admin', role: 'admin', companyName: ' 厦门米椿花 ' }).company,
|
||||
'厦门米椿花',
|
||||
)
|
||||
// 未登录空态与"后端没带公司"都必须是不显示,而不是渲染出 undefined
|
||||
assert.equal(topbarUserOf(null).company, '')
|
||||
assert.equal(topbarUserOf({ id: 1, username: 'admin', role: 'admin' }).company, '')
|
||||
})
|
||||
|
||||
test('company_user_list_page_ui_wiring', () => {
|
||||
test('账号页与顶栏的源码里公司维度已摘干净', () => {
|
||||
const page = readSource('src/pages/account/UsersPage.vue')
|
||||
assert.match(page, /所属公司/, '账号页要有公司这一列/筛选的中文标签')
|
||||
assert.match(page, /fetchCompanies/, '公司候选来自 /api/admin/companies')
|
||||
assert.match(page, /:companies="companies"/, '整份公司目录交给弹窗,由弹窗按操作者收窄')
|
||||
assert.match(page, /:operator="operatorScope"/, '弹窗要拿到操作者角色与归属才能判必选')
|
||||
assert.match(page, /colspan="8"/, '加了公司列后空态横跨整表')
|
||||
assert.equal(/所属公司/.test(page), false, '账号页不该再出现「所属公司」标签')
|
||||
assert.equal(/companyId|companyName|companies/.test(page), false, '页面不再持有公司目录或公司筛选状态')
|
||||
assert.equal(/fetchCompanies/.test(page), false, '公司目录接口不该再被账号页调用')
|
||||
|
||||
// 「删了列忘了改占位行 colspan」是这类改动最常留下的残行:按表头列数交叉核对
|
||||
// (`<th` 是 `<thead` 的前缀,必须带上后随字符才数得准)
|
||||
const headers = (page.match(/<th[\s>]/g) || []).length
|
||||
const spans = [...page.matchAll(/colspan="(\d+)"/g)].map((m) => Number(m[1]))
|
||||
assert.ok(spans.length >= 1, '列表 tbody 没有 colspan 占位行')
|
||||
for (const span of spans) assert.equal(span, headers, `占位行 colspan=${span} 与表头列数 ${headers} 不符`)
|
||||
|
||||
const dialog = readSource('src/pages/account/CreateUserDialog.vue')
|
||||
assert.match(dialog, /selectableCompanies\(/, '下拉选项走同一条后端收窄规则')
|
||||
assert.match(dialog, /initialCompanyId\(/, '打开弹窗时的默认公司走纯函数,不在模板里现写')
|
||||
assert.match(dialog, /请选择所属公司/, '下拉占位与校验文案同源')
|
||||
assert.match(dialog, /validateCreateUserForm\(form, props\.operator\)/, '校验必须带上操作者,否则"超管必选公司"永远不触发')
|
||||
assert.equal(/所属公司|selectableCompanies|initialCompanyId|companyId/.test(dialog), false,
|
||||
'建号弹窗不该再有公司选择')
|
||||
|
||||
const layout = readSource('src/layout/AdminLayout.vue')
|
||||
assert.match(layout, /userVm\.company/, '顶栏走同一条公司名视图模型')
|
||||
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')
|
||||
assert.equal(/company_id|companyId/.test(dto), false)
|
||||
const model = readSource('src/api/users-model.ts')
|
||||
assert.equal(/company_id|company_name/.test(model), false)
|
||||
})
|
||||
|
||||
@@ -57,9 +57,8 @@ test('test_task_043_user_filter_state_invalid_input_rejected', () => {
|
||||
// 异常输入:仅空白关键字不视为激活;筛选与分页参数映射正确。
|
||||
assert.equal(isFilterActive({ ...createUserFilterState(), keyword: ' ' }), false)
|
||||
const params = toUserListParams({ ...createUserFilterState(), keyword: ' x ', page: 2, pageSize: 20 })
|
||||
// companyId 自 V143(多公司一期)起是筛选状态的一等字段:未选时同样以 null 出现在参数里,
|
||||
// 是否下发由 toUserListQuery 决定(见 company-user-list.test.ts)。
|
||||
assert.deepEqual(params, { page: 2, pageSize: 20, keyword: 'x', createdById: null, companyId: null })
|
||||
// 公司维度已于 2026-10-04 从前端摘掉(见 company-user-list.test.ts),筛选状态只剩这三项。
|
||||
assert.deepEqual(params, { page: 2, pageSize: 20, keyword: 'x', createdById: null })
|
||||
})
|
||||
|
||||
test('test_task_043_user_filter_state_dependency_failure_returns_actionable_message', () => {
|
||||
|
||||
Reference in New Issue
Block a user