zhou zhou
15 小时以前 40905cbd04c2e332cd4bc2b9e0c5b3e1da9cccfa
rsf-design/tests/system-user-page-contract.test.mjs
@@ -1,10 +1,209 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { buildUserDialogModel } from '../src/views/system/user/userPage.helpers.js'
test('buildUserDialogModel maps rsf-admin edit data into the dialog model', () => {
  assert.equal(
    buildUserDialogModel({ username: 'root', nickname: '管理员', deptId: 1, roles: [{ id: 3 }] }).username,
    'root'
import {
  buildUserDialogModel,
  buildUserPageQueryParams,
  buildUserSavePayload,
  buildUserSearchParams,
  getUserStatusMeta,
  mergeUserDetailRecord,
  normalizeDeptTreeOptions,
  normalizeRoleOptions,
  normalizeUserListRow
} from '../src/views/system/user/userPage.helpers.js'
test('buildUserSearchParams keeps real user page search fields', () => {
  assert.deepEqual(
    buildUserSearchParams({
      username: 'root',
      nickname: '管理员',
      phone: '13800000000',
      email: 'root@example.com',
      status: 1,
      deptId: 0,
      roleIds: [3, 8],
      code: 'A001',
      sex: 1,
      realName: 'Vincent',
      idCard: '330421199511233211',
      condition: 'root'
    }),
    {
      username: 'root',
      nickname: '管理员',
      phone: '13800000000',
      email: 'root@example.com',
      status: 1,
      deptId: 0,
      roleIds: [3, 8],
      code: 'A001',
      sex: 1,
      realName: 'Vincent',
      idCard: '330421199511233211',
      condition: 'root'
    }
  )
})
test('buildUserPageQueryParams merges paging and search fields', () => {
  assert.deepEqual(
    buildUserPageQueryParams({
      current: 2,
      size: 20,
      username: 'root',
      condition: 'manager'
    }),
    {
      current: 2,
      pageSize: 20,
      username: 'root',
      condition: 'manager'
    }
  )
})
test('buildUserDialogModel normalizes backend edit data into the form model', () => {
  assert.deepEqual(
    buildUserDialogModel({
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      roles: [{ id: 3 }, { roleId: 8 }],
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 0
    }),
    {
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      roleIds: [3, 8],
      userRoleIds: [3, 8],
      password: '',
      confirmPassword: '',
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 0
    }
  )
})
test('buildUserSavePayload submits roleIds and password for the backend', () => {
  assert.deepEqual(
    buildUserSavePayload({
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      userRoleIds: [3, 8],
      password: 'secret',
      confirmPassword: 'secret',
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 1
    }),
    {
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      roleIds: [3, 8],
      password: 'secret',
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 1
    }
  )
})
test('normalizeUserListRow exposes table friendly fields', () => {
  const normalized = normalizeUserListRow({
    username: 'root',
    nickname: '管理员',
    deptLabel: '研发部',
    deptId$: '研发部',
    roleNames: '超级管理员、OPS',
    roles: [{ name: '超级管理员' }, { code: 'OPS' }],
    status: 1,
    createTime$: '2025-03-28 10:00:00',
    updateTime: '2025-03-28 11:00:00'
  })
  assert.equal(normalized.username, 'root')
  assert.equal(normalized.deptLabel, '研发部')
  assert.equal(normalized.roleNames, '超级管理员、OPS')
  assert.equal(normalized.statusBool, true)
  assert.equal(normalized.statusText, '正常')
  assert.equal(normalized.statusType, 'success')
  assert.equal(normalized.createTimeText, '2025-03-28 10:00:00')
  assert.equal(normalized.updateTimeText, '2025-03-28 11:00:00')
})
test('mergeUserDetailRecord keeps list row roles when detail omits them', () => {
  assert.deepEqual(
    mergeUserDetailRecord(
      {
        id: 7,
        username: 'root',
        nickname: '管理员'
      },
      {
        id: 7,
        deptLabel: '研发部',
        roleNames: '超级管理员',
        roles: [{ id: 3, name: '超级管理员' }]
      }
    ),
    {
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptLabel: '研发部',
      roleNames: '超级管理员',
      roles: [{ id: 3, name: '超级管理员' }]
    }
  )
})
test('normalizeDeptTreeOptions and normalizeRoleOptions adapt lookup data', () => {
  assert.deepEqual(
    normalizeDeptTreeOptions([{ id: 1, name: '总部', children: [{ id: 2, name: '研发部' }] }]),
    [{ value: 1, label: '总部', children: [{ value: 2, label: '研发部', children: [] }] }]
  )
  assert.deepEqual(
    normalizeRoleOptions([{ id: 3, name: '管理员' }, { roleId: 8, code: 'OPS' }]),
    [
      { value: 3, label: '管理员' },
      { value: 8, label: 'OPS' }
    ]
  )
})
test('getUserStatusMeta maps enabled and disabled states', () => {
  assert.deepEqual(getUserStatusMeta(1), { type: 'success', text: '正常', bool: true })
  assert.deepEqual(getUserStatusMeta(0), { type: 'danger', text: '禁用', bool: false })
})