Files
crawler-plugin/admin-frontend-vue/tests/company-directory.test.ts
T
huangzd1997 076b0cd531 feat(多公司): 两端接上公司身份 —— 后台公司目录页、建号必选公司、顶栏显示所属公司
- 新后台新增「公司管理」页(列/新建/改名/启停,不提供删除),路由 menuKey 与
  columns 表的 admin_companies 逐字一致,由用例钉住;操作指引补该菜单一条。
- 建号弹窗对超管必须显式选一家(后端对"不传"仍落操作者自己公司,是给旧后台兜底);
  admin 只能落自己公司,越权直接 403。账号列表显示 company_name,可按公司筛。
- 客户端登录后把 companyId/companyName 落到 localStorage 并在顶栏显示;
  两个登出出口(handleKicked 与切号/登出)连公司名一起清 —— 残留会把上一个账号
  的归属显示给下一个账号。
- 契约分层:列表端点用 snake_case(company_id/company_name),current-user 与登录
  回包用 camelCase;解析器读到另一套写法必须忽略,防止前后端各猜一套。

admin-frontend-vue 1678/1678、frontend-vue 672/672,两边 build(含 vue-tsc --noEmit)干净。
2026-10-04 07:11:47 +08:00

72 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { parseCompanyList, parseCompany } from '../src/api/companies-model.ts'
import {
COMPANY_NAME_MAX_LENGTH,
toCompanyPayload,
validateCompanyDraft,
} from '../src/api/companies-dto.ts'
/** 公司目录(设计文档 §2.3)前端侧:Java VO 是 snake_case,请求 DTO 是 camelCase。 */
test('parseCompanyList 按 snake_case 线上字段解析公司列表', () => {
const payload = {
success: true,
data: [
{ id: 1, company_name: '厦门米椿花', status: 1, remark: '初始公司', created_at: '2026-10-04 01:00:00', updated_at: '2026-10-04 02:00:00' },
{ id: 2, company_name: '福州数富', status: 0, remark: null },
],
}
assert.deepEqual(parseCompanyList(payload), [
{ id: 1, companyName: '厦门米椿花', status: 1, remark: '初始公司', createdAt: '2026-10-04 01:00:00', updatedAt: '2026-10-04 02:00:00' },
{ id: 2, companyName: '福州数富', status: 0, remark: null, createdAt: null, updatedAt: null },
])
})
test('parseCompanyList 不认 camelCase:证明上一条不是在测兜底', () => {
const rows = parseCompanyList({ success: true, data: [{ id: 3, companyName: '某某公司', status: 1 }] })
assert.equal(rows.length, 1)
assert.equal(rows[0].companyName, '', '线上字段是 company_name,camelCase 不该被读出来')
})
test('parseCompanyList 丢弃无 id 的行并把未知 status 当停用', () => {
const rows = parseCompanyList({ success: true, data: [
{ company_name: '没有 id', status: 1 },
{ id: 4, company_name: '状态未知', status: null },
] })
assert.deepEqual(rows, [{ id: 4, companyName: '状态未知', status: 0, remark: null, createdAt: null, updatedAt: null }])
})
test('parseCompanyList 对空/异常负载返回空数组', () => {
for (const payload of [null, undefined, {}, { success: true, data: null }, 'x']) {
assert.deepEqual(parseCompanyList(payload), [], `负载 ${JSON.stringify(payload)} 应解析为空列表`)
}
})
test('parseCompanyList 把 success=false 的后端文案抛出来', () => {
assert.throws(() => parseCompanyList({ success: false, message: '无权限访问公司目录' }), /无权限访问公司目录/)
})
test('parseCompany 解析单条(新建/改名回包)', () => {
const item = parseCompany({ success: true, data: { id: 5, company_name: '厦门米椿花', status: 1 } })
assert.ok(item)
assert.equal(item.id, 5)
assert.equal(item.companyName, '厦门米椿花')
assert.equal(parseCompany({ success: true, data: { company_name: '无 id' } }), null)
})
test('公司名必填、去空格后不得为空,且不超过列宽', () => {
assert.equal(validateCompanyDraft({ companyName: ' ' }), '公司名称不能为空')
assert.equal(validateCompanyDraft({ companyName: ' 厦门米椿花 ' }), '')
const tooLong = 'x'.repeat(COMPANY_NAME_MAX_LENGTH + 1)
assert.match(validateCompanyDraft({ companyName: tooLong }), /长度/)
})
test('toCompanyPayload 发 camelCase 且 trim 公司名', () => {
assert.deepEqual(toCompanyPayload({ companyName: ' 福州数富 ', status: 1, remark: ' ' }), {
companyName: '福州数富',
status: 1,
remark: null,
})
})