cmvr-wms/scripts/projectdev/vue3-purchase-queues-regression.cjs

68 lines
9.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path')
const {chromium,frontend,backend,username,password}=require('./vue3-test-runtime.cjs')
const output=path.resolve(__dirname,'../../.codex-tmp/purchase-queues-regression');fs.mkdirSync(output,{recursive:true})
;(async()=>{
const browser=await chromium.launch(),writes=[],errors=[];let page
const row=n=>({id:998000+n,productId:998100+n,productCode:'T'+n,requestNo:'CG-TEST-'+n,materialName:'测试零件'+n,materialParams:'规格'+n,unit:'个',category:'耗材',quantity:5,projectId:1,projectName:'测试项目',expenseCategoryId:1,expenseCategoryName:'材料费',supplierId:1,supplierName:'测试供应商',purchaseBatchId:99801,purchaseBatchNo:'CG-TEST',finalUnitPrice:null,priceLocked:false,purchaseStatus:0,acceptedQuantity:0,pendingArrivalQuantity:0,receivedQuantity:0,remainingQuantity:5})
let sheet={batch:{id:99801,batchNo:'CG-TEST',batchName:'混合状态清单',supplierId:1,supplierName:'测试供应商',businessDate:'2026-10-14'},version:'v1',rows:Array.from({length:9},(_,i)=>row(i+1))}
Object.assign(sheet.rows[2],{finalUnitPrice:0});Object.assign(sheet.rows[3],{finalUnitPrice:3,priceLocked:true,purchaseStatus:1})
Object.assign(sheet.rows[4],{finalUnitPrice:10,priceLocked:true,pendingArrivalQuantity:2,remainingQuantity:0})
Object.assign(sheet.rows[5],{finalUnitPrice:20,priceLocked:true,acceptedQuantity:5,remainingQuantity:0})
Object.assign(sheet.rows[6],{finalUnitPrice:30,priceLocked:true,acceptedQuantity:5,receivedQuantity:5,remainingQuantity:0,purchaseStatus:4})
Object.assign(sheet.rows[7],{finalUnitPrice:40,purchaseStatus:5});Object.assign(sheet.rows[8],{finalUnitPrice:50,historical:true,purchaseStatus:4})
const arrivalRows=Array.from({length:26},(_,i)=>({...row(i+20),finalUnitPrice:3,priceLocked:true,purchaseStatus:1,...(i%2?{remainingQuantity:0,pendingArrivalQuantity:2}:{remainingQuantity:5,pendingArrivalQuantity:0})}))
try{
const context=await browser.newContext({viewport:{width:1366,height:900}}),login=await(await context.request.post(backend+'/login',{data:{username,password}})).json();assert.equal(login.code,200)
await context.addCookies([{name:'Admin-Token',value:login.token,url:frontend}])
await context.route('**/dev-api/**',async route=>{
const req=route.request(),url=new URL(req.url()),endpoint=url.pathname.replace('/dev-api',''),ok=data=>route.fulfill({json:{code:200,data}})
if(endpoint==='/warehouse/purchase/sheet/99801'&&req.method()==='GET')return ok(sheet)
if(endpoint==='/warehouse/purchase/sheet'&&req.method()==='POST'){
const data=req.postDataJSON();writes.push({endpoint,data});sheet={...sheet,version:'v'+(writes.length+1),rows:sheet.rows.map(r=>({...r,...data.rows.find(s=>s.id===r.id)}))};return ok(sheet)
}
if(endpoint==='/warehouse/purchase/sheet/99801/confirm'){
const data=req.postDataJSON();writes.push({endpoint,data});assert.ok(data.rowIds?.length,'filtered confirmation must send explicit row IDs');sheet={...sheet,version:'v'+(writes.length+1),rows:sheet.rows.map(r=>data.rowIds.includes(r.id)?{...r,priceLocked:true,purchaseStatus:1}:r)};return ok(sheet)
}
if(endpoint==='/warehouse/purchase/sheet/99801/quote-preview'){writes.push({endpoint});return ok([{line:2,productCode:'T2',name:'测试零件2',price:'9.50',unitPrice:9.5,rowIds:[998002],error:null},{line:3,productCode:'BAD',price:'1',rowIds:[],error:'当前批次找不到该物品'}])}
if(!['GET','HEAD'].includes(req.method()))throw Error('Unexpected business write blocked: '+endpoint)
if(endpoint==='/warehouse/purchase/list')return route.fulfill({json:{code:200,total:arrivalRows.length,rows:arrivalRows.slice(0,20)}})
if(endpoint==='/warehouse/purchase/filtered')return ok(arrivalRows)
if(endpoint==='/warehouse/purchase/filtered-totals')return ok({count:arrivalRows.length})
if(endpoint==='/warehouse/purchase/summary')return ok({})
if(/\/warehouse\/purchase\/\d+$/.test(endpoint))return ok(arrivalRows.find(r=>r.id===Number(endpoint.split('/').pop())))
if(/\/warehouse\/purchase\/arrival\/\d+$/.test(endpoint))return ok([])
return route.continue()
})
page=await context.newPage();page.setDefaultTimeout(15000);page.on('pageerror',e=>errors.push(e.message))
await page.goto(frontend+'/procurement/details?purchaseBatchId=99801',{waitUntil:'domcontentloaded'})
const rows=()=>page.locator('.sheet-grid .el-table__body tr'),queue=label=>page.locator('.sheet-queues .el-radio-button').filter({hasText:new RegExp('^'+label+'\\s')})
await rows().first().waitFor();assert.equal(await rows().count(),9)
await queue('待报价').click();assert.equal(await rows().count(),2)
await page.getByRole('button',{name:'编辑',exact:true}).click();await page.locator('.sheet-cell[data-field="finalUnitPrice"]').first().dblclick();await page.locator('.sheet-cell[data-field="finalUnitPrice"] input:visible').fill('5')
assert.equal(await rows().count(),2,'quoting must not remove the actively edited row')
await page.getByRole('button',{name:'保存清单',exact:true}).click();await until(()=>assert.equal(writes.length,1));await until(async()=>assert.equal(await rows().count(),1))
assert.equal(writes[0].data.rows.find(r=>r.id===998001).finalUnitPrice,5)
await queue('待确认').click();assert.equal(await rows().count(),2)
await page.getByRole('button',{name:'确认采购',exact:true}).click();await page.locator('.el-message-box').getByRole('button',{name:'确定',exact:true}).click()
await until(()=>assert.equal(writes.length,2));assert.deepEqual(writes[1].data.rowIds.sort(),[998001,998003])
await page.getByRole('button',{name:'重置',exact:true}).click();await page.locator('.sheet-filter-inputs .el-select').click();await page.getByRole('option',{name:'单价升序',exact:true}).click();assert.ok((await rows().first().textContent()).includes('测试零件3'))
for(const [label,count]of [['待到货',3],['待验收',1],['待入库',1],['已完成',2],['已取消',1]]){await queue(label).click();assert.equal(await rows().count(),count,label)}
await page.getByRole('button',{name:'重置',exact:true}).click()
await page.getByRole('button',{name:'回填供应商报价',exact:true}).click();const importer=page.getByRole('dialog',{name:'供应商询价与报价回填',exact:true});await importer.waitFor();assert.ok((await importer.textContent()).includes('供应商填写黄色报价栏'))
await importer.locator('input[type=file]').setInputFiles({name:'mock.xlsx',mimeType:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',buffer:Buffer.from('mock preview intercepted')})
const preview=importer;await preview.getByText('当前批次找不到该物品',{exact:true}).waitFor();await preview.locator('.el-checkbox').filter({hasText:'仅回填校验通过项,异常项保留原报价'}).click();await preview.getByRole('button',{name:'回填有效报价(1 项)',exact:true}).click()
const price=page.locator('.sheet-cell[data-row="1"][data-field="finalUnitPrice"]');await price.dblclick();assert.equal(await price.locator('input:not(.sheet-keyboard-input)').inputValue(),'9.5');assert.equal(writes.filter(w=>w.endpoint==='/warehouse/purchase/sheet').length,1,'import preview does not auto-save')
await page.getByRole('button',{name:'取消编辑',exact:true}).click();await page.locator('.el-message-box').getByRole('button',{name:'放弃修改',exact:true}).click()
await page.screenshot({path:path.join(output,'queues.png'),animations:'disabled'})
await page.goto(frontend+'/procurement/arrival',{waitUntil:'domcontentloaded'});await page.locator('.business-list-page .el-table__body tr').first().waitFor()
await page.getByRole('button',{name:'选择待到货项',exact:true}).click();await page.getByText('已选 13 项 · 全部筛选页',{exact:true}).waitFor();assert.equal(await page.locator('.el-table__body .el-checkbox.is-checked').count(),10,'visible checkboxes must reflect cross-page selection')
await page.getByRole('button',{name:'选择待验收项',exact:true}).click();await page.getByText('已选 13 项 · 全部筛选页',{exact:true}).waitFor();await until(async()=>assert.equal(await page.locator('.el-table__body .el-checkbox.is-checked').count(),10))
await page.getByRole('button',{name:'清空',exact:true}).click();assert.equal(await page.locator('.el-table__body .el-checkbox.is-checked').count(),0)
await page.locator('.business-operation-cell').getByRole('button',{name:'登记到货',exact:true}).first().click();const arrival=page.locator('.arrival-dialog:visible');await arrival.waitFor();await page.waitForTimeout(400)
const metric=await arrival.evaluate(el=>{const body=el.querySelector('.el-dialog__body'),table=el.querySelector('.arrival-table .el-scrollbar__wrap');return {body:body.scrollWidth-body.clientWidth,table:table.scrollWidth-table.clientWidth,bottom:el.getBoundingClientRect().bottom,height:innerHeight}})
assert.ok(metric.body<=2&&metric.table<=2&&metric.bottom<=metric.height,JSON.stringify(metric));await page.screenshot({path:path.join(output,'arrival-dialog.png'),animations:'disabled'})
assert.deepEqual(errors,[]);console.log(JSON.stringify({passed:true,mockRequests:writes.length,realBusinessWrites:0,scenarios:['quick queues','stable quote editing','filtered explicit confirmation','numeric sorting','quote import preview','visible cross-page selection','arrival dialog fits viewport']}))
}catch(error){await page?.screenshot({path:path.join(output,'failure.png')}).catch(()=>{});throw error}finally{await browser.close()}
})().catch(error=>{console.error(error);process.exitCode=1})
async function until(check){let error;for(let n=0;n<100;n++){try{return await check()}catch(e){error=e;await new Promise(r=>setTimeout(r,100))}}throw error}