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) })