68 lines
11 KiB
JavaScript
68 lines
11 KiB
JavaScript
|
|
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 checkControlCells=require('./vue3-cell-overflow-check.cjs')
|
||
|
|
const output=path.resolve(__dirname,'../../.codex-tmp/pending-receipts-regression')
|
||
|
|
fs.mkdirSync(output,{recursive:true})
|
||
|
|
;(async()=>{
|
||
|
|
const browser=await chromium.launch(),writes=[],errors=[],failIds=new Set();let failWrites=true,groupLoads=0,failDetails=false,page
|
||
|
|
const groups=[{key:'B:990801',title:'回归批次 A',supplierName:'回归供应商 A',projects:'回归项目 A',count:140},{key:'B:990802',title:'回归批次 B',supplierName:'回归供应商 B',projects:'回归项目 B',count:90}]
|
||
|
|
const records=groups.flatMap((g,index)=>Array.from({length:g.count},(_,n)=>({id:9908000+index*1000+n,group:g.key,inNo:'RK-TEST-'+index+'-'+String(n+1).padStart(3,'0'),productName:'测试零件 '+index+'-'+String(n+1).padStart(3,'0'),brand:'测试品牌',spec:'用于检验长规格文字能完整查看、不会撑开页面的规格型号 '+n,unit:'个',sourceProjectName:g.projects,sourceNo:g.title,purchaseBatchNo:g.title,supplierName:g.supplierName,inNum:1,inStatus:0,receiptRoot:true,areaId:null})))
|
||
|
|
try{
|
||
|
|
const context=await browser.newContext({viewport:{width:1440,height:960}})
|
||
|
|
let login
|
||
|
|
for(let n=0;n<30;n++){try{login=await(await context.request.post(backend+'/login',{data:{username,password,code:'',uuid:''}})).json();break}catch(error){if(n===29)throw error;await new Promise(resolve=>setTimeout(resolve,500))}}
|
||
|
|
assert.equal(login.code,200,'test login');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()),pathname=url.pathname.replace('/dev-api',''),reply=data=>route.fulfill({json:{code:200,data}})
|
||
|
|
if(req.method()!=='GET'){
|
||
|
|
const data=req.postDataJSON();writes.push({path:pathname,data})
|
||
|
|
if(pathname.startsWith('/warehouse/in/receive/')){const id=Number(pathname.split('/').pop());if(failWrites||failIds.has(id))return route.fulfill({json:{code:500,msg:'测试:入库接口暂时失败'}});const row=records.find(r=>r.id===id);assert.ok(row);row.inStatus=1;return reply(1)}
|
||
|
|
throw new Error('Unexpected write blocked: '+pathname)
|
||
|
|
}
|
||
|
|
if(pathname==='/warehouse/in/pending-groups'){groupLoads++;return reply({total:groups.length,itemTotal:records.filter(r=>r.inStatus===0).length,rows:groups.map(g=>({...g,recordCount:records.filter(r=>r.group===g.key&&r.inStatus===0).length})).filter(g=>g.recordCount>0)})}
|
||
|
|
if(pathname==='/warehouse/in/list'){const rows=records.filter(r=>r.inStatus===0),page=Number(url.searchParams.get('pageNum')||1),size=Number(url.searchParams.get('pageSize')||20);return route.fulfill({json:{code:200,total:rows.length,rows:rows.slice((page-1)*size,page*size)}})}
|
||
|
|
if(pathname.startsWith('/warehouse/in/pending-groups/')){if(failDetails)return route.fulfill({json:{code:500,msg:'测试:明细加载失败'}});const key=decodeURIComponent(pathname.split('/').pop());return reply(records.filter(r=>r.group===key&&r.inStatus===0))}
|
||
|
|
if(/^\/warehouse\/in\/\d+$/.test(pathname))return reply(records.find(r=>r.id===Number(pathname.split('/').pop())))
|
||
|
|
return route.continue()
|
||
|
|
})
|
||
|
|
page=await context.newPage();page.setDefaultTimeout(15000);page.on('pageerror',error=>errors.push(error.message))
|
||
|
|
await page.goto(frontend+'/warehouse/pending',{waitUntil:'domcontentloaded'})
|
||
|
|
const details=page.locator('.pending-details'),rowTable=()=>details.locator('.el-table__body tr'),modal=()=>page.locator('.receipt-batch-dialog:visible')
|
||
|
|
await rowTable().first().waitFor();assert.equal(await rowTable().count(),20,'large batches render one page, not all nested rows')
|
||
|
|
await details.getByRole('button',{name:'下一页',exact:true}).click();await assertText(rowTable().first(),'021')
|
||
|
|
await rowTable().first().locator('.el-checkbox').click();await details.getByRole('button',{name:'上一页',exact:true}).click();await assertText(rowTable().first(),'001');assert.ok(await details.getByText('已选 1 项',{exact:true}).isVisible(),'selection survives detail pagination')
|
||
|
|
await page.locator('.pending-source').filter({hasText:'回归批次 B'}).click();await assertText(rowTable().first(),'1-001')
|
||
|
|
await details.getByRole('button',{name:'整批入库',exact:true}).click();await modal().locator('.el-table__body tr').first().waitFor();assert.equal(await modal().locator('.el-table__body tr').count(),20)
|
||
|
|
const initialLoads=groupLoads;await modal().getByRole('button',{name:'确认本次入库',exact:true}).click();await page.getByText('请先为 90 项选择库区,可使用统一库区',{exact:true}).waitFor();assert.equal(writes.length,0);assert.equal(groupLoads,initialLoads);assert.equal(await modal().locator('.el-table__body tr').count(),20)
|
||
|
|
await modal().getByRole('button',{name:'下一页',exact:true}).click();await assertText(modal().locator('.el-table__body tr').first(),'1-021')
|
||
|
|
await modal().getByRole('button',{name:'关闭',exact:true}).click();await modal().waitFor({state:'hidden'});assert.equal(await rowTable().count(),20);assert.equal(groupLoads,initialLoads)
|
||
|
|
await rowTable().first().locator('.el-checkbox').click();await page.locator('.business-list-page__header').getByRole('button',{name:'批量入库',exact:true}).click();await modal().locator('.el-table__body tr').first().waitFor()
|
||
|
|
await modal().locator('.el-form-item').filter({hasText:'统一库区'}).locator('.el-select').click();await page.locator('.el-select-dropdown__item:visible').first().click();await modal().locator('.el-dialog__header').click()
|
||
|
|
await modal().getByRole('button',{name:'确认本次入库',exact:true}).click();await modal().getByText('测试:入库接口暂时失败',{exact:true}).waitFor();assert.equal(writes.length,1);assert.equal(groupLoads,initialLoads,'all-server-failure must not reset parent data')
|
||
|
|
const key=writes[0].data.key;failWrites=false;await modal().getByRole('button',{name:'重试失败',exact:true}).click();await modal().getByText('已入库 1',{exact:true}).waitFor();await assertText(details.locator('.pending-details__identity strong'),'回归批次 B');await page.waitForFunction(()=>document.querySelector('.pending-source.is-active')?.textContent.includes('89 项'));assert.equal(writes.length,2);assert.equal(writes[1].data.key,key)
|
||
|
|
await modal().getByRole('button',{name:'关闭',exact:true}).click();await assertText(rowTable().first(),'1-002');assert.equal(await rowTable().count(),20,'remaining records automatically reload without folding/reopening')
|
||
|
|
await page.locator('.receipt-batch-dialog').waitFor({state:'hidden'})
|
||
|
|
// Mixed result: success refreshes the parent but preserves the failed row for retry.
|
||
|
|
await rowTable().nth(0).locator('.el-checkbox').click();await rowTable().nth(1).locator('.el-checkbox').click()
|
||
|
|
const failedId=records.filter(r=>r.group===groups[1].key&&r.inStatus===0)[1].id;failIds.add(failedId)
|
||
|
|
await page.locator('.business-list-page__header').getByRole('button',{name:'批量入库',exact:true}).click();await modal().locator('.el-table__body tr').first().waitFor()
|
||
|
|
await modal().locator('.el-form-item').filter({hasText:'统一库区'}).locator('.el-select').click();await page.locator('.el-select-dropdown__item:visible').first().click();await modal().locator('.el-dialog__header').click()
|
||
|
|
await checkControlCells(page,'批量入库弹窗');await modal().getByRole('button',{name:'确认本次入库',exact:true}).click();await modal().getByText('测试:入库接口暂时失败',{exact:true}).waitFor();await page.waitForFunction(()=>document.querySelector('.pending-source.is-active')?.textContent.includes('88 项'))
|
||
|
|
const firstSuccessId=writes[2].path,failedKey=writes[3].data.key;failIds.clear()
|
||
|
|
await modal().getByRole('button',{name:'重试失败',exact:true}).click();await assertEventually(()=>assert.equal(writes.length,5));await page.waitForFunction(()=>document.querySelector('.pending-source.is-active')?.textContent.includes('87 项'))
|
||
|
|
assert.equal(writes.filter(w=>w.path===firstSuccessId).length,1,'successful row is never re-submitted');assert.equal(writes[4].data.key,failedKey)
|
||
|
|
await modal().getByRole('button',{name:'关闭',exact:true}).click();await page.locator('.receipt-batch-dialog').waitFor({state:'hidden'})
|
||
|
|
await page.locator('.el-message').waitFor({state:'hidden'});await page.screenshot({path:path.join(output,'desktop.png'),animations:'disabled'})
|
||
|
|
// A failed details fetch stays retryable and can never enable stale rows.
|
||
|
|
failDetails=true;await page.locator('.pending-source').filter({hasText:'回归批次 A'}).click();await details.getByRole('alert').waitFor();assert.equal(await details.getByRole('button',{name:'整批入库',exact:true}).isEnabled(),false)
|
||
|
|
failDetails=false;await details.getByRole('button',{name:'重试',exact:true}).click();await rowTable().first().waitFor();await assertText(rowTable().first(),'0-001')
|
||
|
|
await page.locator('.pending-source').filter({hasText:/全部/}).click();await assertText(details.locator('.pending-details__identity strong'),'全部待入库');assert.equal(await details.getByRole('button',{name:'整批入库',exact:true}).count(),0)
|
||
|
|
await rowTable().first().locator('.el-checkbox').click();await details.getByRole('button',{name:'下一页',exact:true}).click();await assertText(rowTable().first(),'0-021');assert.ok(await details.getByText('已选 1 项',{exact:true}).isVisible());assert.equal(await rowTable().count(),20)
|
||
|
|
await page.setViewportSize({width:1024,height:768});await page.locator('.pending-source-select').waitFor({state:'visible'});assert.equal(await page.locator('.pending-sources').isVisible(),false);await page.locator('.el-message').waitFor({state:'hidden'});await page.screenshot({path:path.join(output,'narrow.png'),animations:'disabled'})
|
||
|
|
const overflow=await page.evaluate(()=>document.documentElement.scrollHeight>innerHeight+2||document.documentElement.scrollWidth>innerWidth+2);assert.equal(overflow,false,'page stays in viewport')
|
||
|
|
assert.deepEqual(errors,[]);console.log(JSON.stringify({passed:true,scenarios:['detail paging and retained selection','large-batch paging','missing area: zero submits, retained data','server failure retains data','stable-key retry','partial success: retry failed rows only','success auto reload retains batch','detail load retry','narrow layout and viewport'],mockWrites:writes.length,realWrites:0,pageErrors:errors.length}))
|
||
|
|
}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 assertText(locator,text){await assertEventually(async()=>assert.ok((await locator.textContent()).includes(text)))}
|
||
|
|
async function assertEventually(callback){let error;for(let n=0;n<80;n++){try{return await callback()}catch(e){error=e;await new Promise(r=>setTimeout(r,100))}}throw error}
|