cmvr-wms/mobile-app/tests/stock-operations.test.mjs

60 lines
5.5 KiB
JavaScript
Raw Normal View History

import test from 'node:test';
import assert from 'node:assert/strict';
import {available,validateOperation,operationPayload,runOperations} from '../src/core/stock-operations.ts';
const form={date:'2026-10-28',remark:'现场测试',project:{id:2}};
const source={id:1,projectId:1,productId:3,areaId:4,stockNum:10,inNum:10,receiptRoot:true,productName:'测试物品'};
const row=(patch={})=>({source:{...source},quantity:'4',area:{id:5},key:'stable-request',status:'editing',error:'',...patch});
test('quantities preserve 4 decimals and identify original invalid row',()=>{
assert.equal(validateOperation('use',[row({quantity:'0.0001'})],form),'');
for(const invalid of ['0','-1','1.00001','Infinity','1e1','11','abc'])assert.match(validateOperation('use',[row(),row({quantity:invalid})],form),/第 2 项/);
assert.match(validateOperation('use',[row(),row({quantity:'7'})],form),/合计数量超过/);
assert.match(validateOperation('use',[row()],{...form,date:'2026-02-30'}),/日期/);
assert.equal(validateOperation('use',[row({source:{...source,stockNum:0.3},quantity:'0.1'}),row({source:{...source,stockNum:0.3},quantity:'0.2'})],form),'');
});
test('movement, borrowing and historical receipt maintain business restrictions',()=>{
assert.match(validateOperation('move',[row({area:{id:4}})],form),/不能与来源相同/);
assert.match(validateOperation('borrow',[row()],{...form,project:{id:1}}),/不同的借入项目/);
assert.match(validateOperation('receive',[row({source:{...source,receiptRoot:false}})],form),/整单/);
assert.equal(validateOperation('check',[row({quantity:'0'})],form),'');
assert.match(validateOperation('check',[row({quantity:'0'})],{...form,remark:''}),/差异/);
});
test('return quantity is limited by both outstanding loan and current source stock',()=>{
const loan={id:5,active:true,outstandingQuantity:8};const stock={id:99,stockNum:5,areaId:6};
assert.equal(available('return',row({source:loan,stock})),5);
assert.match(validateOperation('return',[row({source:loan,stock,quantity:'6'})],form),/超过未还数量或取料库存/);
assert.match(validateOperation('return',[row({source:loan,stock,quantity:'5'}),row({source:loan,stock:{...stock,id:100},quantity:'4'})],form),/合计归还数量超过/);
assert.equal(validateOperation('return',[row({source:loan,stock,quantity:'5'}),row({source:loan,stock:{...stock,id:100},quantity:'3'})],form),'');
});
test('requests retain all warehouse and project identifiers',()=>{
assert.deepEqual(operationPayload('move',row(),form)[0].body,{quantity:4,key:'stable-request',date:form.date,remark:form.remark,projectId:1,itemId:3,fromAreaId:4,toAreaId:5});
assert.equal(operationPayload('use',row(),form)[0].body.outNo,'MOUT-stable-request');
assert.equal(operationPayload('borrow',row(),form)[0].body.borrowerProjectId,2);
assert.equal(operationPayload('receive',row(),form)[0].path,'/warehouse/in/receive/1');
const returned=operationPayload('return',row({stock:{areaId:9}}),form)[0];assert.equal(returned.path,'/project/loans/1/return');assert.equal(returned.body.fromAreaId,9);
});
test('partial failure retries only unsuccessful rows using exactly the original payload',async()=>{
const rows=[row(),row({source:{...source,id:2},key:'second'})],calls=[],saved=[];
const first=await runOperations('use',rows,form,async(path,options)=>{calls.push(options.body);if(options.body.id===2)throw new Error('response lost');},async next=>saved.push(JSON.parse(JSON.stringify(next))));
assert.equal(first[0].status,'success');assert.equal(first[1].status,'failed');assert.ok(saved.some(s=>s[1].status==='pending'));
const second=await runOperations('use',first,{...form,date:'2027-01-01'},async(path,options)=>calls.push(options.body),async()=>{});
assert.equal(calls.length,3);assert.deepEqual(calls[1],calls[2]);assert.ok(second.every(r=>r.status==='success'));
});
test('persisting request identity must succeed before any write',async()=>{
let writes=0;await assert.rejects(runOperations('move',[row()],form,async()=>writes++,async()=>{throw new Error('disk full');}),/disk full/);assert.equal(writes,0);
});
test('100 idempotent rows need only initial and final durable snapshots, not 200 rewrites',async()=>{
const rows=Array.from({length:100},(_,i)=>row({source:{...source,id:i+1},key:'batch-'+i}));let writes=0,durable=0;
await runOperations('use',rows,form,async()=>writes++,async(next,persist)=>{if(persist)durable++;});assert.equal(writes,100);assert.equal(durable,2);
});
test('uncertain stock-count requests are never automatically retried',async()=>{
let writes=0;const first=await runOperations('check',[row()],form,async()=>{writes++;throw new Error('timeout');},async()=>{});
assert.equal(first[0].status,'uncertain');await runOperations('check',first,form,async()=>writes++,async()=>{});assert.equal(writes,1);
// A crash can leave a persisted pending record; this is also not retryable.
await runOperations('check',[{...first[0],status:'pending'}],form,async()=>writes++,async()=>{});assert.equal(writes,1);
});
test('legacy receipt partial transition does not repeat confirmation after lost response',async()=>{
const calls=[];const first=await runOperations('receive',[row({quantity:'10',source:{...source,receiptRoot:false}})],form,async path=>{calls.push(path);if(path.includes('confirm'))throw new Error('timeout');},async()=>{});
assert.deepEqual(calls,['/warehouse/in','/warehouse/in/confirm/1']);assert.equal(first[0].status,'uncertain');
await runOperations('receive',first,form,async path=>calls.push(path),async()=>{});assert.equal(calls.length,2);
});