import {expect,it} from 'vitest' import {buildInsertedRows,clonePurchaseRows,canAutoAppend} from '../../src/utils/purchase-row-operations' const base=()=>[{_key:'a',id:71,materialName:'物品甲'},{_key:'b',id:72,materialName:'物品乙'}] function factory(){let id=0;return ()=>({_key:`new-${++id}`,id:null,projectId:null,expenseCategoryId:null,materialName:'',quantity:1,finalUnitPrice:null,actualUnitPrice:null,purchaseStatus:0,purchasePurpose:'SELF',productionTaskId:null})} it('inserts before or after the anchored row without mutating the source',()=>{ const rows=base(),snapshot=JSON.stringify(rows) const before=buildInsertedRows({rows,anchorKey:'b',position:'before',count:2,createRow:factory(),defaults:{projectId:7,expenseCategoryId:9}}) expect(before.rows.map(r=>r._key)).toEqual(['a','new-1','new-2','b']);expect(before.index).toBe(1) expect(before.inserted.map(r=>[r.projectId,r.expenseCategoryId])).toEqual([[7,9],[7,9]]) expect(JSON.stringify(rows)).toBe(snapshot) const after=buildInsertedRows({rows,anchorKey:'a',position:'after',count:1,createRow:factory()}) expect(after.rows.map(r=>r._key)).toEqual(['a','new-1','b']);expect(after.rows[0]).toBe(rows[0]) }) it('appends to empty lists and validates counts, anchors and default identifiers atomically',()=>{ expect(buildInsertedRows({rows:[],createRow:factory()}).rows).toHaveLength(1) const rows=base() for(const count of [0,-1,1.5,101,'2',NaN])expect(()=>buildInsertedRows({rows,count,createRow:factory()})).toThrow('1至100') expect(()=>buildInsertedRows({rows,anchorKey:'deleted',createRow:factory()})).toThrow('已变化') expect(()=>buildInsertedRows({rows,anchorKey:'a',position:'middle',createRow:factory()})).toThrow('上方或下方') for(const value of [-1,0,NaN,{},'wrong'])expect(()=>buildInsertedRows({rows,createRow:factory(),defaults:{projectId:value}})).toThrow('默认项目') expect(rows).toEqual(base()) }) it('rejects overflow or duplicate row identities without changing any existing rows',()=>{ const rows=Array.from({length:1999},(_,i)=>({_key:`saved-${i}`})) expect(()=>buildInsertedRows({rows,count:2,createRow:factory()})).toThrow('最多2000行') expect(buildInsertedRows({rows,count:1,createRow:factory()}).rows).toHaveLength(2000) expect(()=>buildInsertedRows({rows:base(),createRow:()=>({_key:'a'})})).toThrow('标识重复') expect(()=>buildInsertedRows({rows:base(),count:2,createRow:()=>({_key:'same'})})).toThrow('标识重复') expect(rows).toHaveLength(1999) }) it('copies only the allowed entry fields and never prices or downstream state',()=>{ const rows=base();Object.assign(rows[0],{projectId:7,expenseCategoryId:8,materialBrand:'品牌',materialParams:'规格',productId:11,productCode:'WP11',unit:'个',category:'零件',quantity:3,purchaseLink:'https://example.test',productionTaskId:14,finalUnitPrice:100,actualUnitPrice:99,historical:true,priceLocked:true,purchaseStatus:3,receivedQuantity:5,acceptedQuantity:5,loanId:6,attachmentIds:['file'],remark:'不复制备注'}) const copy=clonePurchaseRows({rows,sourceKeys:['a'],anchorKey:'b',createRow:factory(),defaults:{expenseCategoryId:90}}).inserted[0] expect(copy).toMatchObject({_key:'new-1',id:null,projectId:7,expenseCategoryId:8,productId:11,productionTaskId:14,quantity:3,finalUnitPrice:null,actualUnitPrice:null,purchaseStatus:0,purchasePurpose:'SELF'}) for(const field of ['historical','priceLocked','receivedQuantity','acceptedQuantity','loanId','attachmentIds','remark'])expect(copy).not.toHaveProperty(field) expect(rows[0].finalUnitPrice).toBe(100) }) it('copies multiple selected rows in sheet order to the requested position',()=>{ const rows=base(),result=clonePurchaseRows({rows,sourceKeys:['b','a','a'],anchorKey:'a',position:'before',createRow:factory()}) expect(result.inserted.map(r=>r.materialName)).toEqual(['物品甲','物品乙']) expect(result.rows.map(r=>r._key)).toEqual(['new-1','new-2','a','b']) expect(()=>clonePurchaseRows({rows,sourceKeys:['a','gone'],createRow:factory()})).toThrow('已变化') expect(()=>clonePurchaseRows({rows,sourceKeys:[],createRow:factory()})).toThrow('选择') }) it('auto-append is available only for a populated physical final row in the unfiltered original view',()=>{ const rows=base(),context={isEditing:true,saving:false,canAdd:true,rows,visibleRows:rows,lastRow:rows[1]} expect(canAutoAppend(context)).toBe(true) for(const change of [{isEditing:false},{saving:true},{canAdd:false},{rowSort:'nameAsc'},{rowFilter:'关键词'},{rowQueue:'quote'},{onlyInvalid:true},{lastRow:rows[0]},{visibleRows:[rows[1]]},{visibleRows:[rows[1],rows[0]]}])expect(canAutoAppend({...context,...change})).toBe(false) for(const materialName of ['', ' ', '\u200B\uFEFF']){const blank={_key:'empty',materialName,quantity:1,projectId:9};expect(canAutoAppend({...context,rows:[...rows,blank],visibleRows:[...rows,blank],lastRow:blank})).toBe(false)} const full=Array.from({length:2000},(_,i)=>({_key:String(i),materialName:'物品'}));expect(canAutoAppend({...context,rows:full,visibleRows:full,lastRow:full.at(-1)})).toBe(false) })