cmvr-wms/ruoyi-ui-vue3/tests/unit/business-assistant-draft-edit.test.mjs
lixiaolong 8bc6c3b10f feat(assistant): 支持物品档案类别的更正与同步更新
- 在业务助手中添加对物品类别更正的支持,允许用户手动纠正类别
- 实现类别更正后的档案同步更新功能,确保数据一致性
- 添加权限控制验证,仅授权用户可执行类别更正操作
- 优化采购单编辑界面,显示类别更正的相关提示信息
- 修复批量项目设置时跳过已执行行的问题
- 增强物品档案匹配逻辑,保留显式类别更正而不被覆盖
- 完善单元测试覆盖类别更正的各种场景
2026-10-16 17:17:06 +08:00

178 lines
15 KiB
JavaScript
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 { readFileSync } from 'node:fs'
import { assistantApplyDraftChanges, assistantBulkChanges, assistantFieldLocked, assistantItemResolution, assistantPurchaseGroups, assistantRecordId, assistantReferenceUnresolved } from '../../src/utils/assistant-draft-edit.js'
import { assistantRowsSignature, canConfirmAssistantDraft } from '../../src/utils/business-assistant.js'
const fields = [
{ key: 'productId', ref: 'item' }, { key: 'materialName' }, { key: 'materialParams' }, { key: 'materialBrand' },
{ key: 'unit', ref: 'unit', batch: true }, { key: 'category', ref: 'category', batch: true }, { key: 'quantity', type: 'number' }, { key: 'finalUnitPrice', type: 'number' },
{ key: 'projectId', ref: 'project', batch: true }, { key: 'expenseCategoryId', ref: 'expense', batch: true },
{ key: 'purchaseBatchId', ref: 'batch', batch: true }, { key: 'batchName', batch: true }, { key: 'supplierId', ref: 'supplier', batch: true }, { key: 'businessDate', type: 'date', batch: true },
{ key: 'purchasePurpose', batch: true }, { key: 'productionTaskId', ref: 'task', batch: true }, { key: 'loanId', ref: 'loan' }, { key: 'purchaseLink' }, { key: 'remark' }
]
const readyDraft = { id: 'draft', action: 'purchase.create', status: 'WAIT_CONFIRM', version: 1, ready: true, rows: [], results: [] }
const context = draft => ({ action: draft.action, fields, draft })
test('unmatched record names are not IDs and do not lock catalog or batch fields', () => {
for (const value of [1, '1', '001', ' 12 ']) assert.equal(assistantRecordId(value), true)
for (const value of ['便携屏', '新一批', '1e3', 0, -1, 1.5, null, {}, Infinity, true]) assert.equal(assistantRecordId(value), false)
const unresolved = { productId: '便携屏', category: '设备', purchaseBatchId: '新一批' }
for (const key of ['unit', 'category', 'supplierId', 'businessDate']) assert.equal(assistantFieldLocked('purchase.create', unresolved, fields.find(field => field.key === key)), false)
assert.equal(assistantFieldLocked('purchase.create', { ...unresolved, productId: '12' }, fields.find(field => field.key === 'unit')), true)
})
test('fill-only recognizes unresolved names and candidate-marked dictionary values but preserves confirmed scalar values', () => {
const project = fields.find(field => field.key === 'projectId'), unit = fields.find(field => field.key === 'unit')
const rows = [{ _rowId: 'name', projectId: '1025底盘项目', _candidates: { projectId: [{ value: 9, label: '完整项目名' }] } }, { _rowId: 'plain-name', projectId: '另一项目' }, { _rowId: 'id', projectId: 8 }, { _rowId: 'id-string', projectId: '9' }]
assert.deepEqual(assistantBulkChanges(rows, project, { value: 9, label: '完整项目名' }, context(readyDraft)).map(row => row.rowId), ['name', 'plain-name'])
assert.equal(assistantReferenceUnresolved({ unit: '个' }, unit), false)
assert.equal(assistantReferenceUnresolved({ unit: 'pcs', _candidates: { unit: [] } }, unit), true)
const [selected] = assistantApplyDraftChanges([rows[0]], { rowId: 'name', key: 'projectId', value: 9, label: '完整项目名' }, context(readyDraft))
assert.equal(selected._candidates.projectId, undefined)
assert.equal(assistantReferenceUnresolved(selected, project), false)
assert.deepEqual(assistantBulkChanges([selected], project, { value: 10 }, context(readyDraft)), [])
assert.deepEqual(assistantBulkChanges(rows, project, { value: '未匹配名称' }, context(readyDraft)), [])
})
test('purchase column groups retain every editable field without putting dictionaries or core item fields into extra columns', () => {
const groups = assistantPurchaseGroups([...fields, { key: 'newBusinessField' }, { key: 'id' }])
assert.deepEqual(groups.map(group => group.label), ['物品信息', '采购归属', '批次信息', '补充信息'])
assert.ok(['productId', 'materialName', 'materialParams', 'materialBrand', 'unit', 'category', 'quantity', 'finalUnitPrice'].every(key => groups[0].fields.some(field => field.key === key)))
assert.deepEqual(groups[2].fields.map(field => field.key), ['purchaseBatchId', 'batchName', 'supplierId', 'businessDate'])
assert.equal(groups[3].fields.at(-1).key, 'newBusinessField')
const keys = groups.flatMap(group => group.fields.map(field => field.key))
assert.equal(new Set(keys).size, fields.length + 1)
assert.equal(keys.includes('id'), false)
})
test('selecting a catalog item brings only known editable item data, never quantity, price, project or execution metadata', () => {
const original = { _rowId: '1', materialName: '识别名称', quantity: 12, finalUnitPrice: 0, projectId: 9, _errors: ['原校验'] }
const data = { name: '螺丝', spec: 'M6×20', brand: '测试牌', unit: '个', category: '紧固件', quantity: 999, projectId: 1, _rowId: 'tampered', _errors: [] }
const rows = assistantApplyDraftChanges([original], { rowId: '1', key: 'productId', value: 7, label: '螺丝 / M6×20', data }, context(readyDraft))
assert.equal(rows[0].productId, 7)
assert.deepEqual([rows[0].materialName, rows[0].materialParams, rows[0].materialBrand, rows[0].unit, rows[0].category], ['螺丝', 'M6×20', '测试牌', '个', '紧固件'])
assert.deepEqual([rows[0]._rowId, rows[0].quantity, rows[0].finalUnitPrice, rows[0].projectId], ['1', 12, 0, 9])
assert.deepEqual(rows[0]._errors, ['原校验'])
assert.equal(rows[0]._labels.productId, '螺丝 / M6×20')
assert.equal(original.productId, undefined)
assert.equal(assistantFieldLocked(readyDraft.action, rows[0], fields.find(field => field.key === 'unit')), true)
assert.equal(assistantFieldLocked(readyDraft.action, rows[0], fields.find(field => field.key === 'category')), true)
})
test('editing item identity releases the old catalog ID while preserving quoted quantity and maintained dictionaries for revalidation', () => {
for (const key of ['materialName', 'materialBrand', 'materialParams']) {
const original = { _rowId: '1', productId: 7, materialName: '螺丝', materialBrand: '测试牌', materialParams: 'M6', quantity: 12, unit: '个', category: '紧固件', loanId: 5, _assistantCatalogCategory: '紧固件', _labels: { productId: '旧物品' } }
const [changed] = assistantApplyDraftChanges([original], { rowId: '1', key, value: '新内容' }, context(readyDraft))
assert.equal(changed.productId, null)
assert.equal(changed._labels.productId, '')
assert.equal(changed.loanId, null)
assert.equal(changed.quantity, 12)
assert.equal(changed.unit, '个')
assert.equal(assistantItemResolution(changed).label, '待重新匹配')
assert.equal(assistantFieldLocked(readyDraft.action, changed, fields.find(field => field.key === 'category')), false)
}
})
test('catalog with no category allows filling it but never changing the catalog unit', () => {
const row = { _rowId: '1', productId: 7, unit: '个', category: '', _itemResolution: { kind: 'FILL_CATEGORY', label: '补全档案类别' } }
const [changed] = assistantApplyDraftChanges([row], { rowId: '1', key: 'category', value: '紧固件' }, context(readyDraft))
assert.equal(changed.category, '紧固件')
assert.equal(assistantFieldLocked(readyDraft.action, changed, fields.find(field => field.key === 'category')), false)
assert.equal(assistantApplyDraftChanges([changed], { rowId: '1', key: 'unit', value: '套' }, context(readyDraft))[0].unit, '个')
assert.equal(assistantItemResolution(changed).label, '补全档案类别')
assert.equal(assistantItemResolution({ _itemResolution: { kind: 'CREATE' } }).label, '确认后新建档案')
})
test('server-authorized category correction stays editable and does not release item identity or clear quotes', () => {
const row={_rowId:'1',productId:7,category:'设备',unit:'个',quantity:2,finalUnitPrice:12,_assistantCanEditCategory:true,_assistantCatalogCategory:'设备'}
const field=fields.find(field=>field.key==='category')
assert.equal(assistantFieldLocked('purchase.update',row,field),false)
const [changed]=assistantApplyDraftChanges([row],{rowId:'1',key:'category',value:'配件'},context({...readyDraft,action:'purchase.update'}))
assert.equal(changed.category,'配件');assert.equal(changed.productId,7);assert.equal(changed.quantity,2);assert.equal(changed.finalUnitPrice,12)
assert.equal(assistantFieldLocked('purchase.update',{...row,_assistantCanEditCategory:false},field),true)
assert.match(assistantItemResolution({_itemResolution:{kind:'UPDATE_CATEGORY'}}).label,/同步更正/)
})
test('batch setting defaults to filling missing and unresolved references without changing already selected projects or successful rows', () => {
const rows = [{ _rowId: 'a', projectId: null }, { _rowId: 'b', projectId: { keyword: '1025底盘项目' } }, { _rowId: 'c', projectId: 6 }, { _rowId: 'done', projectId: null }]
const draft = { ...readyDraft, rows, results: [{ rowId: 'done', status: 'SUCCEEDED' }] }
const field = fields.find(field => field.key === 'projectId')
const input = { value: 9, label: '1025底盘测试项目' }
const changes = assistantBulkChanges(rows, field, input, { action: draft.action, draft })
assert.deepEqual(changes.map(change => change.rowId), ['a', 'b'])
const updated = assistantApplyDraftChanges(rows, { changes }, context(draft))
assert.deepEqual(updated.map(row => row.projectId), [9, 9, 6, null])
assert.equal(updated[1]._labels.projectId, '1025底盘测试项目')
assert.equal(rows[1].projectId.keyword, '1025底盘项目')
assert.deepEqual(assistantBulkChanges(rows, field, input, { action: draft.action, draft, overwrite: true, selectedIds: ['c', 'done'] }).map(change => change.rowId), ['c'])
assert.deepEqual(assistantBulkChanges(rows, field, input, { action: draft.action, draft, selectedIds: [] }), [])
})
test('bulk editing honors schema, readonly drafts, field locks and explicit scope; zero remains a valid value', () => {
const rows = [{ _rowId: 'a', unit: '个', productId: 7 }, { _rowId: 'b', unit: '', productId: null }]
const unit = fields.find(field => field.key === 'unit')
assert.deepEqual(assistantBulkChanges(rows, unit, { value: '套' }, { action: readyDraft.action, draft: readyDraft, overwrite: true }).map(change => change.rowId), ['b'])
for (const draft of [{ ...readyDraft, readOnly: true }, { ...readyDraft, permissionDenied: true }, { ...readyDraft, status: 'SUCCEEDED' }, { ...readyDraft, status: 'CANCELLED' }]) {
assert.deepEqual(assistantBulkChanges(rows, unit, { value: '套' }, { action: draft.action, draft }), [])
assert.equal(assistantApplyDraftChanges(rows, { rowId: 'b', key: 'unit', value: '套' }, context(draft)), rows)
}
for (const field of [{ key: 'quantity' }, { key: '_snapshot', batch: true }, { key: 'unit', readonly: true, batch: true }, { key: 'lines', batch: true, type: 'table' }]) assert.deepEqual(assistantBulkChanges(rows, field, { value: 0 }, { action: readyDraft.action, draft: readyDraft }), [])
assert.equal(assistantBulkChanges([{ _rowId: 'a' }], { key: 'price', batch: true }, { value: 0 }, { draft: readyDraft }).length, 1)
})
test('dependent project, purpose and batch choices remain consistent and never move an existing purchase to another batch', () => {
const source = { _rowId: 'a', projectId: 1, productionTaskId: 11, purchasePurpose: 'REPAY', loanId: 21, batchName: '拟建批次', supplierId: 31, businessDate: '2026-10-15' }
const [project] = assistantApplyDraftChanges([source], { rowId: 'a', key: 'projectId', value: 2 }, context(readyDraft))
assert.equal(project.productionTaskId, null); assert.equal(project.loanId, null)
const [purpose] = assistantApplyDraftChanges([source], { rowId: 'a', key: 'purchasePurpose', value: 'SELF' }, context(readyDraft))
assert.equal(purpose.loanId, null)
const [batch] = assistantApplyDraftChanges([source], { rowId: 'a', key: 'purchaseBatchId', value: 3 }, context(readyDraft))
assert.deepEqual([batch.batchName, batch.supplierId, batch.businessDate], [null, null, null])
const [newBatch] = assistantApplyDraftChanges([batch], { rowId: 'a', key: 'batchName', value: '新批次' }, context(readyDraft))
assert.equal(newBatch.purchaseBatchId, null)
const updated = assistantApplyDraftChanges([batch], { rowId: 'a', key: 'purchaseBatchId', value: 9 }, context({ ...readyDraft, action: 'purchase.update' }))
assert.equal(updated[0].purchaseBatchId, 3)
})
test('fill-only batch settings do not silently replace a chosen existing or new batch', () => {
const rows = [{ _rowId: 'existing', purchaseBatchId: 3 }, { _rowId: 'new', batchName: '已填写批次' }, { _rowId: 'empty' }]
const options = { action: readyDraft.action, draft: readyDraft }
const name = fields.find(field => field.key === 'batchName'), existing = fields.find(field => field.key === 'purchaseBatchId')
assert.deepEqual(assistantBulkChanges(rows, name, { value: '统一批次' }, options).map(row => row.rowId), ['empty'])
assert.deepEqual(assistantBulkChanges(rows, existing, { value: 5 }, options).map(row => row.rowId), ['empty'])
assert.deepEqual(assistantBulkChanges(rows, name, { value: '统一批次' }, { ...options, overwrite: true }).map(row => row.rowId), ['existing', 'new', 'empty'])
})
test('local edits always invalidate confirmation and never erase successful rows, even if a forged change event targets them', () => {
const rows = [{ _rowId: 'a', materialName: '旧物品' }, { _rowId: 'done', materialName: '已执行物品' }]
const draft = { ...readyDraft, rows, results: [{ rowId: 'done', status: 'SUCCEEDED' }] }
const changes = [{ rowId: 'a', key: 'materialName', value: '新物品' }, { rowId: 'done', key: 'materialName', value: '不能修改' }, { rowId: 'a', key: '_errors', value: [] }, { rowId: 'a', key: 'unknownField', value: 'not allowed' }]
const updated = assistantApplyDraftChanges(rows, { changes }, context(draft))
assert.equal(updated[1], rows[1])
assert.equal(updated[0].unknownField, undefined)
assert.equal(updated[0]._errors, undefined)
assert.equal(canConfirmAssistantDraft(draft, { dirty: assistantRowsSignature(updated) !== assistantRowsSignature(rows) }), false)
})
test('purchase bulk controls stay inline, core fields remain accessible and floating launcher shows only the avatar', () => {
const draft = readFileSync(new URL('../../src/components/BusinessAssistant/AssistantDraft.vue', import.meta.url), 'utf8')
const bulk = readFileSync(new URL('../../src/components/BusinessAssistant/AssistantBulkEdit.vue', import.meta.url), 'utf8')
const main = readFileSync(new URL('../../src/components/BusinessAssistant/index.vue', import.meta.url), 'utf8')
const field = readFileSync(new URL('../../src/components/BusinessAssistant/AssistantField.vue', import.meta.url), 'utf8')
assert.match(draft, /:field="nameField"/)
assert.match(draft, /:field="catalogField"/)
assert.match(draft, /全部字段/)
assert.match(bulk, /mode = ref\('missing'\)/)
assert.match(bulk, /仅补空缺或未匹配值/)
assert.doesNotMatch(bulk, /<el-dialog|executeAssistantDraft|\.execute\(/)
assert.match(field, /data: selected\?\.data/)
const launcher = readFileSync(new URL('../../src/components/BusinessAssistant/AssistantLauncher.vue', import.meta.url), 'utf8')
assert.match(main, /<assistant-launcher v-if="visible && !assistant.open" :avatar="assistantAvatar"/)
assert.match(launcher, /恢复并打开业务助手/)
assert.match(launcher, /<img[^>]+:src="avatar"/)
assert.match(launcher, /<el-badge v-if="pending"/)
assert.doesNotMatch(launcher, />业务助手</)
assert.match(main, /id="business-assistant-title">业务助手/)
})