cmvr-wms/ruoyi-ui-vue3/tests/unit/assistant-authorization-changes.test.mjs

39 lines
3.5 KiB
JavaScript
Raw Normal View History

import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import { assistantAuthorizationChanges, assistantComparedFields } from '../../src/utils/assistant-authorization-changes.js'
import { assistantApplyDraftChanges } from '../../src/utils/assistant-draft-edit.js'
import { assistantColumnWidth } from '../../src/utils/assistant-table-layout.js'
test('authorization comparison uses server IDs and preserves distinct records with identical labels', () => {
const row = { _authorizationChanges: { added: [{ id: 2, label: '查询' }], removed: [{ id: 3, label: '修改' }], unchanged: [{ id: 1, label: '查询' }] } }
const diff = assistantAuthorizationChanges(row, 'role.permissions')
assert.deepEqual(diff.added, [{ id: '2', label: '查询' }]); assert.deepEqual(diff.unchanged, [{ id: '1', label: '查询' }]); assert.equal(diff.removed.length, 1)
})
test('old confirmation cards receive readable add/remove/retained differences, including clear and no change', () => {
const changes = assistantAuthorizationChanges({ previousPermissionNames: '查询;修改', resultPermissionNames: '查询;新增' }, 'role.permissions')
assert.equal(changes.added[0].label, '新增'); assert.equal(changes.removed[0].label, '修改'); assert.equal(changes.unchanged[0].label, '查询')
const clear = assistantAuthorizationChanges({ previousRoleNames: '采购员;仓管', resultRoleNames: '无' }, 'user.roles')
assert.equal(clear.removed.length, 2); assert.equal(clear.added.length, 0)
assert.equal(assistantAuthorizationChanges({ previousRoleNames: '仓管', resultRoleNames: '仓管' }, 'user.roles').unchanged.length, 1)
assert.equal(assistantAuthorizationChanges({}, 'user.roles'), null)
})
test('changes made after preview invalidate the comparison until refreshed; unrelated rows and fields remain intact', () => {
const source = { _rowId: 'r', id: 2, mode: 'ADD', menuIds: [1], _authorizationChanges: { added: [], removed: [], unchanged: [] } }
const result = assistantApplyDraftChanges([source], { rowId: 'r', key: 'mode', value: 'REPLACE' }, { action: 'role.permissions', fields: [{ key: 'mode' }] })
assert.equal(result[0]._authorizationChangesStale, true); assert.equal(assistantAuthorizationChanges(result[0], 'role.permissions'), null); assert.equal(source.mode, 'ADD')
assert.equal(assistantAuthorizationChanges({ ...source, _errors: ['重新校验'] }, 'role.permissions'), null)
})
test('both authorization forms combine the duplicated long text columns without altering other business forms', () => {
const fields = [{ key: 'id' }, { key: 'mode' }, { key: 'previousRoleNames' }, { key: 'resultRoleNames' }]
const compared = assistantComparedFields(fields, 'user.roles')
assert.deepEqual(compared.map(field => field.key), ['id', 'mode', '_authorizationChanges']); assert.equal(compared[2].readonly, true)
assert.equal(assistantComparedFields(fields, 'purchase.update'), fields)
assert.ok(assistantColumnWidth(compared[2], { compact: true }) >= 300)
})
test('change markers use color and words, hide retained lists by default, and remain theme-aware', () => {
const component = fs.readFileSync(new URL('../../src/components/BusinessAssistant/AssistantAuthorizationChanges.vue', import.meta.url), 'utf8')
for (const marker of ['+新增', '-移除', '=保留', '<del>', '<details', '--el-color-success', '--el-color-danger', '--el-text-color-secondary']) assert.ok(component.includes(marker), marker)
assert.doesNotMatch(component, /v-html|<details[^>]*\bopen\b/)
})