cmvr-wms/ruoyi-ui-vue3/tests/unit/purchase-selection-stats.test.mjs
lixiaolong ebac97185e feat(assistant): 新增用户核对功能并优化对话流程
- 新增 user.check 功能用于核对人员是否已有系统账号
- 在 assistant-table-layout.js 中为用户表格添加 checkedIdentity 和 matchResult 字段
- 优化 AssistantConversationService 中的任务记忆和澄清流程
- 支持对草稿特定行进行修改,无需重新填写整张表
- 限制 user.check 操作最大查询数量为 100 条记录
- 实现中断任务恢复机制,保持原始请求上下文
- 添加针对多行数据选择性修改的目标行标识支持
2026-10-23 17:00:47 +08:00

75 lines
5.7 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { purchaseSelectionStats } from '../../src/utils/purchase-selection-stats.js'
const columns = [{ field: 'materialName', label: '物品' }, { field: 'quantity', label: '数量' }, { field: 'actualUnitPrice', label: '实际单价' }, { field: 'finalUnitPrice', label: '报价单价' }]
const range = (top, bottom, left, right = left) => ({ top, bottom, left, right })
test('counts only the current visible rectangle and does not modify rows or columns', () => {
const rows = [{ quantity: 999 }, { materialName: '一', quantity: '0.1', unit: '个', actualUnitPrice: '1.005', finalUnitPrice: '2' }, { materialName: '二', quantity: '0.2', unit: '个', actualUnitPrice: '0', finalUnitPrice: null }, { quantity: 888 }]
const before = JSON.stringify({ rows, columns })
const stats = purchaseSelectionStats({ rows, columns, range: range(1, 2, 1) })
assert.equal(stats.cellCount, 2); assert.equal(stats.rowCount, 2); assert.equal(stats.numericCount, 2)
assert.deepEqual(stats.numeric, { field: 'quantity', label: '数量', count: 2, sum: '0.3', average: '0.15' })
assert.deepEqual(stats.quantities, [{ unit: '个', total: '0.3', count: 2 }])
assert.deepEqual(stats.quote, { amount: '0.20', pricedCount: 1, unpricedCount: 1 })
assert.deepEqual(stats.actual, { amount: '0.10', pricedCount: 2, unpricedCount: 0 })
assert.equal(JSON.stringify({ rows, columns }), before)
})
test('different numeric fields cannot produce a meaningless combined sum or average', () => {
const stats = purchaseSelectionStats({ rows: [{ materialName: '123', quantity: 2, actualUnitPrice: 3, finalUnitPrice: 4 }], columns, range: range(0, 0, 0, 3) })
assert.equal(stats.cellCount, 4); assert.equal(stats.numericCount, 3); assert.equal(stats.numeric, null)
assert.equal(stats.actual.amount, '6.00'); assert.equal(stats.quote.amount, '8.00')
})
test('quantities are grouped by unit rather than added across unrelated units', () => {
const stats = purchaseSelectionStats({ rows: [{ quantity: '0.1', unit: '个' }, { quantity: '0.2', unit: '个' }, { quantity: 2, unit: '套' }, { quantity: 3, unit: '' }, { quantity: null, unit: '盒' }, { quantity: -1, unit: '个' }], columns, range: range(0, 5, 0) })
assert.deepEqual(stats.quantities, [{ unit: '个', total: '0.3', count: 2 }, { unit: '套', total: '2', count: 1 }, { unit: '未填单位', total: '3', count: 1 }])
assert.equal(stats.numeric, null); assert.equal(stats.numericCount, 0)
})
test('a quantity-only selection with mixed units has no cross-unit sum or average', () => {
const rows = [{ quantity: '0.1', unit: '个' }, { quantity: '0.2', unit: ' 个 ' }, { quantity: 2, unit: '套' }, { quantity: 3, unit: '' }, { quantity: null, unit: '盒' }]
const stats = purchaseSelectionStats({ rows, columns, range: range(0, 4, 1) })
assert.equal(stats.numeric, null); assert.equal(stats.numericCount, 4)
assert.deepEqual(stats.quantities, [{ unit: '个', total: '0.3', count: 2 }, { unit: '套', total: '2', count: 1 }, { unit: '未填单位', total: '3', count: 1 }])
const sameUnit = purchaseSelectionStats({ rows, columns, range: range(0, 1, 1) })
assert.equal(sameUnit.numeric.sum, '0.3'); assert.equal(sameUnit.numeric.average, '0.15')
})
test('missing or malformed prices stay unknown, while explicit zero is priced', () => {
const rows = [null, undefined, '', 'abc', Infinity, -1].map(price => ({ quantity: 1, finalUnitPrice: price }))
let stats = purchaseSelectionStats({ rows, columns, range: range(0, rows.length - 1, 3) })
assert.deepEqual(stats.quote, { amount: null, pricedCount: 0, unpricedCount: 6 })
rows.push({ quantity: 1, finalUnitPrice: 0 })
stats = purchaseSelectionStats({ rows, columns, range: range(0, rows.length - 1, 3) })
assert.deepEqual(stats.quote, { amount: '0.00', pricedCount: 1, unpricedCount: 6 })
})
test('monetary totals round each row half-up and accumulate using exact decimal cents', () => {
const rows = [{ quantity: 1, finalUnitPrice: '1.005', actualUnitPrice: '99999999999999.999999' }, { quantity: 1, finalUnitPrice: '1.005', actualUnitPrice: '0.005' }]
const stats = purchaseSelectionStats({ rows, columns, range: range(0, 1, 1) })
assert.equal(stats.quote.amount, '2.02'); assert.equal(stats.actual.amount, '100000000000000.01')
})
test('averages use six-place decimal half-up rounding without floating-point noise', () => {
const stats = purchaseSelectionStats({ rows: [{ quantity: '1' }, { quantity: '0' }, { quantity: '0' }], columns, range: range(0, 2, 1) })
assert.equal(stats.numeric.sum, '1'); assert.equal(stats.numeric.average, '0.333333')
assert.equal(purchaseSelectionStats({ rows: [{ quantity: 0 }, { quantity: null }], columns, range: range(0, 1, 1) }).numeric.average, '0')
})
test('compact amount mode never exposes actual amount fields or actual-price numeric values', () => {
const rows = [{ quantity: 2, actualUnitPrice: '123456.78', finalUnitPrice: 3 }]
const stats = purchaseSelectionStats({ rows, columns, range: range(0, 0, 1, 3), compactAmounts: true })
assert.equal(Object.hasOwn(stats, 'actual'), false); assert.equal(stats.cellCount, 2); assert.equal(stats.numericCount, 2)
assert.equal(JSON.stringify(stats).includes('123456'), false)
assert.equal(purchaseSelectionStats({ rows, columns, range: range(0, 0, 2), compactAmounts: true }), null)
})
test('invalid stale rectangles cannot silently include invisible or unexpected records', () => {
const rows = [{ quantity: 1 }]
for (const invalid of [null, range(-1, 0, 1), range(0, 2, 1), range(0, 0, -1), range(0, 0, 9), range(1, 0, 1), range(0, 0, 2, 1), { top: 0.5, bottom: 0, left: 1, right: 1 }]) assert.equal(purchaseSelectionStats({ rows, columns, range: invalid }), null)
assert.equal(purchaseSelectionStats(), null)
})