import {afterEach,expect,it} from 'vitest' import {mount,flushPromises} from '@vue/test-utils' import ElementPlus from 'element-plus' import Preview from '@/components/PurchaseSheet/PastePreview.vue' import {inspectPurchasePaste,purchasePasteMapping,buildPurchasePastePayload} from '@/utils/purchase-paste-preview' import {parseClipboardTable,pasteRectangle} from '@/utils/purchase-sheet' const columns=[{field:'materialName',label:'物品名称'},{field:'materialParams',label:'规格型号'},{field:'quantity',label:'数量'},{field:'unit',label:'单位'},{field:'actualUnitPrice',label:'实际单价'},{field:'finalUnitPrice',label:'报价单价'},{field:'projectId',label:'项目'},{field:'materialBrand',label:'品牌'},{field:'productionTaskId',label:'生产计划'}] const mounted=[] afterEach(()=>{for(const wrapper of mounted.splice(0))wrapper.unmount()}) const prepared=(text,startField='materialName')=>inspectPurchasePaste(text,columns,startField) const payload=model=>buildPurchasePastePayload(model.matrix,model.mapping,columns,model.hasHeader) function render(text,extra={}){const wrapper=mount(Preview,{props:{visible:true,text,columns,startField:'materialName',...extra},attachTo:document.body,global:{plugins:[ElementPlus]}});mounted.push(wrapper);return wrapper} it('recognizes headers and reorders mapped columns without treating computed amounts as prices',()=>{ const model=prepared('报价单价\t实际金额\t物品名称\t采购数量\n9.8\t29.4\t螺丝\t3') expect(model.hasHeader).toBe(true);expect(model.mapping).toEqual(['finalUnitPrice','','materialName','quantity']) const result=payload(model);expect(result.fields).toEqual(['materialName','quantity','finalUnitPrice']);expect(parseClipboardTable(result.text)).toEqual([['螺丝','3','9.8']]);expect(result.skippedColumns).toBe(1) }) it('starts unheaded rectangles at the selected field and lets users manually change header treatment',()=>{ const model=prepared('2\t个\n3\t套','quantity') expect(model.hasHeader).toBe(false);expect(model.mapping).toEqual(['quantity','unit']);expect(payload(model).rowCount).toBe(2) expect(purchasePasteMapping([['名称','数量'],['电机','4']],columns,false,'materialParams')).toEqual(['materialParams','quantity']) }) it('keeps skipped mapping gaps untouched when applied through the existing atomic paste engine',()=>{ const model=prepared('报价单价\t实际金额\t物品名称\n11\t22\t新物品') const result=payload(model),original=[{materialName:'旧物品',materialParams:'旧规格',materialBrand:'旧品牌',quantity:2,unit:'个',actualUnitPrice:7,finalUnitPrice:8}] const rows=pasteRectangle(original,result.text,0,0,result.fields,{emptyRow:()=>({}),readonly:()=>false,canEditField:()=>true,fieldLocked:()=>false,items:[],units:['个'],projects:[],categories:[],plans:[]}) expect(rows[0]).toMatchObject({materialName:'新物品',quantity:2,unit:'个',actualUnitPrice:7,finalUnitPrice:11,materialParams:'旧规格',materialBrand:'旧品牌'}) expect(original[0].materialName).toBe('旧物品') expect(()=>pasteRectangle(original,result.text,0,0,result.fields,{emptyRow:()=>({}),readonly:()=>false,canEditField:(_,field)=>field!=='finalUnitPrice',items:[],units:['个']})).toThrow('不可通过粘贴修改') expect(original[0].finalUnitPrice).toBe(8) }) it('serializes quoted multiline cells and keeps blank data rows instead of silently dropping them',()=>{ const model=prepared('物品名称\t数量\n"电机\n附件"\t2\n\t\n风扇\t3') expect(payload(model).rowCount).toBe(3);expect(parseClipboardTable(payload(model).text)).toEqual([['电机\n附件','2'],['',''],['风扇','3']]) }) it('blocks duplicate targets, unknown targets, missing mappings, and nonrectangular data',()=>{ const model=prepared('物品名称\t名称\n电机\t风扇') expect(()=>payload(model)).toThrow('对应了多个源列') expect(()=>buildPurchasePastePayload(model.matrix,['missing',''],columns,true)).toThrow('目标列已变化') expect(()=>buildPurchasePastePayload(model.matrix,['',''],columns,true)).toThrow('至少选择') expect(()=>prepared('甲\t乙\n丙')).toThrow('矩形') expect(()=>prepared('物品名称\n"不完整')).toThrow('引号未闭合') expect(()=>payload(prepared('物品名称'))).toThrow('没有明细') }) it('accepts all 2000 data rows with a header but never truncates oversized pastes',()=>{ const text='物品名称\t数量\n'+Array.from({length:2000},(_,i)=>`物品${i}\t1`).join('\n') expect(payload(prepared(text)).rowCount).toBe(2000) const oversized=prepared(Array.from({length:2001},()=>`电机\t1`).join('\n')) expect(()=>payload(oversized)).toThrow('最多粘贴2000行') expect(()=>prepared(text+'\n多余\t1')).toThrow('最多粘贴2000行') }) it('renders only twenty preview records but emits every mapped row and waits for the parent to close',async()=>{ const w=render('数量\t物品名称\n'+Array.from({length:25},(_,i)=>`${i+1}\t物品${i}`).join('\n'));await flushPromises() expect(w.findComponent({name:'ElTable'}).props('data')).toHaveLength(20) expect(document.body.textContent).toContain('共 25 行') const button=w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='应用到表格');await button.trigger('click') const result=w.emitted('apply')[0][0];expect(result.fields).toEqual(['materialName','quantity']);expect(parseClipboardTable(result.text)).toHaveLength(25) expect(w.emitted('update:visible')).toBeUndefined() }) it('duplicates are visible and disabled until the user explicitly skips one source column',async()=>{ const w=render('物品名称\t名称\n甲\t乙');await flushPromises() const apply=w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='应用到表格');expect(apply.props('disabled')).toBe(true) const selects=w.findAllComponents({name:'ElSelect'});expect(selects).toHaveLength(2);selects[1].vm.$emit('update:modelValue','');await flushPromises() expect(apply.props('disabled')).toBe(false);await apply.trigger('click');expect(w.emitted('apply')[0][0]).toEqual({text:'甲',fields:['materialName']}) }) it('busy state prevents duplicate application and keeps user mappings on an external apply failure',async()=>{ const w=render('数量\n2',{busy:true});await flushPromises() const apply=w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='应用到表格');await apply.trigger('click');expect(w.emitted('apply')).toBeUndefined() await w.setProps({busy:false});await apply.trigger('click');expect(w.emitted('apply')).toHaveLength(1) expect(w.findComponent({name:'ElDialog'}).props('modelValue')).toBe(true) expect(w.findAllComponents({name:'ElSelect'})[0].props('modelValue')).toBe('quantity') })