import test from 'node:test' import assert from 'node:assert/strict' import { parseClipboardTable, pasteRectangle, collectRowErrors, validateRows } from '../../src/utils/purchase-sheet.js' test('purchase drafts reject unmaintained dictionaries but preserve existing catalog references', () => { const row={materialName:'电机',unit:'临时单位',category:'临时分类',projectId:1,expenseCategoryId:1,quantity:1} assert.deepEqual(collectRowErrors([row],[],[],['个'],['机械件']).map(e=>e.field),['category','unit']) assert.deepEqual(collectRowErrors([{...row,unit:'个',category:'机械件'}],[],[],['个'],['机械件']),[]) const item={id:1,productName:'电机',unit:'临时单位',category:'临时分类'} assert.deepEqual(collectRowErrors([{...row,productId:1}],[item],[],['个'],['机械件']),[]) }) test('Excel quoted TSV keeps newlines, tabs, escaped quotes and empty cells in their cells', () => { assert.deepEqual(parseClipboardTable('名称\t备注\r\n电机\t"第一行\n第二行\t\"\"尺寸\"\""\r\n'), [['名称','备注'],['电机','第一行\n第二行\t"尺寸"']]) assert.deepEqual(parseClipboardTable('a\t\t\n'), [['a','','']]) assert.throws(()=>parseClipboardTable('"unterminated'), /未闭合/) assert.throws(()=>parseClipboardTable('"closed"bad'), /格式不正确/) }) test('multiline Excel cell never creates unintended purchase rows; failed paste is atomic', () => { const rows=[{_key:'a',materialName:'电机',unit:'个',quantity:1}] const context={emptyRow:()=>({_key:'b',quantity:1,unit:'个'}),readonly:()=>false,items:[],units:['个']} const pasted=pasteRectangle(rows,'"第一行\n第二行"',0,0,['remark'],context) assert.equal(pasted.length,1) assert.equal(pasted[0].remark,'第一行\n第二行') assert.equal(rows[0].remark,undefined) assert.throws(()=>pasteRectangle(rows,'"未闭合',0,0,['remark'],context)) assert.equal(rows[0].remark,undefined) }) test('all row and field errors are collected with stable keys and original line numbers', () => { const rows=[{_key:'first',materialName:'电机',quantity:0,unit:'个',category:'材料'}, {_key:'second',materialName:'控制器',projectId:1,expenseCategoryId:1,quantity:1,unit:'个',category:'材料',finalUnitPrice:-1,purchasePurpose:'REPAY'}] const errors=collectRowErrors(rows,[],[11,25],['个']) assert.deepEqual(errors.map(e=>[e.rowKey,e.line,e.field]),[['first',11,'projectId'],['first',11,'expenseCategoryId'],['first',11,'quantity'],['second',25,'finalUnitPrice'],['second',25,'loanId']]) assert.equal(rows[0].productId,undefined) assert.throws(()=>validateRows(rows,[],[11,25]),error=>error.fields.length===5&&error.message.includes('第 11 行')) }) test('resolved item categories and zero price remain valid, conflicting units mark both rows', () => { const item={id:1,productName:'电机',brand:'A',spec:'10',unit:'个',category:'设备'} const row={_key:'a',materialName:'电机',materialBrand:'A',materialParams:'10',unit:'个',category:'',projectId:1,expenseCategoryId:1,quantity:1,finalUnitPrice:0} assert.deepEqual(collectRowErrors([row],[item]),[]) validateRows([row],[item]);assert.equal(row.productId,1) const conflict=collectRowErrors([row,{...row,_key:'b',unit:'件'}],[]) assert.deepEqual(new Set(conflict.filter(e=>e.field==='unit').map(e=>e.rowKey)),new Set(['a','b'])) }) test('saved catalog category corrections survive matching and paste while invalid dictionary values are rejected', () => { const item={id:1,productName:'电机',brand:'A',spec:'10',unit:'个',category:'设备'} const row={_key:'saved',id:8,productId:1,materialName:'电机',materialBrand:'A',materialParams:'10',unit:'个',category:'配件',projectId:1,expenseCategoryId:1,quantity:2,finalUnitPrice:12} assert.deepEqual(collectRowErrors([row],[item],[],['个'],['设备','配件']),[]) validateRows([row],[item]);assert.equal(row.category,'配件');assert.equal(row.productId,1);assert.equal(row.quantity,2);assert.equal(row.finalUnitPrice,12) const pasted=pasteRectangle([row],'设备',0,0,['category'],{readonly:()=>false,items:[item],units:['个']}) assert.equal(pasted[0].category,'设备');assert.equal(row.category,'配件') assert.equal(collectRowErrors([{...row,category:'未维护类别'}],[item],[],['个'],['设备','配件'])[0].field,'category') })