fix(order): enforce versioned platform commands and submissions

This commit is contained in:
2026-10-02 23:14:24 +08:00
parent 25580dab86
commit c55ecabbfb
27 changed files with 540 additions and 74 deletions
+28 -2
View File
@@ -6,6 +6,32 @@ import { normalizeRows, channelName, matchRoute, measurementShape, buildSourceDr
const header = ['进仓编号', '客户', '渠道', '亚马逊仓', '实际件数', '实际毛重', '实际立方', '预报件数', '买单/退税', '品名', 'SKU', 'FBA货件编号', '跟踪编号']
const row = ['ABCD2606010001', 'Example', '合德海卡包税', 'ABC1', 31, 100, 2, 31, '买单', 'Goods', 'SKU1', 'SHIP1', 'REF1']
const order = () => normalizeRows([header, row]).orders[0]
test('Excel submit saves original key/version before uncertain transport and never auto-refreezes failed entries',async t=>{
const originalExitCode=process.exitCode
t.after(()=>{process.exitCode=originalExitCode})
const value=order(),route=matchRoute(value,catalog),state={identity:{batchId:'frozen-submit'},orders:{}}
const saved=[]
const api=async(method,path,body,_token,headers)=>{
if(path.endsWith('/pricing/estimate'))return {channelCode:route.channel.channelCode,pricingTaxMode:value.pricingTaxMode}
if(method==='POST'&&path.endsWith('/first-leg'))return 'order-frozen'
if(method==='GET')return {order:{id:'order-frozen',version:8,orderNo:'FL',customerRemark:state.orders[value.key].marker},cargoes:[{packageCount:value.packageCount,grossWeightKg:value.weightKg,volumeCbm:value.volumeCbm}]}
if(path.endsWith('/submit')){
const checkpoint=saved.at(-1).orders[value.key]
assert.equal(checkpoint.stage,'submit');assert.deepEqual(checkpoint.submitCommand,{body,headers})
assert.deepEqual(body,{expectedVersion:8});throw new Error('transport lost')
}
throw new Error('unexpected call')
}
const options={candidates:[{order:value,route}],limit:1,state,save:async()=>saved.push(structuredClone(state)),api,
ctx:{},merchant:'merchant',profile:{tenantName:'Example'},sharedResources:{productImageFileId:'image'},stage:'pending-review'}
await executeCandidates(options)
assert.equal(process.exitCode,1)
assert.equal(state.orders[value.key].status,'FAILED')
assert.equal(state.orders[value.key].submitCommand.body.expectedVersion,8)
await executeCandidates({...options,api:async()=>{assert.fail('failed checkpoint must not issue another request')}})
assert.equal(state.orders[value.key].submitCommand.body.expectedVersion,8)
})
const catalog = { receiving: { id: '1', warehouseName: 'Origin' },
channels: [{ channelName: '合德海卡', channelCode: 'CHANNEL', pricingTaxModes: ['TAX_INCLUDED'] }],
destinations: [{ id: '2', warehouseType: 'FBA', warehouseCode: 'ABC1', warehouseName: 'ABC1', countryCode: 'US' }] }
@@ -150,7 +176,7 @@ test('pending-review stage submits but never calls approval or measurement APIs'
if (path === '/api/merchant/v1/orders/first-leg/order-1' && method === 'GET') {
detailReads++
return { order: { id: 'order-1', orderNo: 'FL-1', customerRemark: Object.values(state.orders)[0].marker,
status: detailReads > 1 ? 'SUBMITTED' : 'DRAFT' },
status: detailReads > 1 ? 'SUBMITTED' : 'DRAFT', version: 7 },
cargoes: [{ packageCount: value.packageCount, grossWeightKg: value.weightKg, volumeCbm: value.volumeCbm }] }
}
if (path === '/api/merchant/v1/orders/order-1') return { summary: { mainStatus: 'PENDING_REVIEW' } }
@@ -175,7 +201,7 @@ test('pending-review retries an estimate-only pricing failure and records it as
if (path === '/api/merchant/v1/orders/first-leg/order-2' && method === 'GET') {
detailReads++
return { order: { id: 'order-2', orderNo: 'FL-2', customerRemark: Object.values(state.orders)[0].marker,
status: detailReads > 1 ? 'SUBMITTED' : 'DRAFT' },
status: detailReads > 1 ? 'SUBMITTED' : 'DRAFT', version: 7 },
cargoes: [{ packageCount: value.packageCount, grossWeightKg: value.weightKg, volumeCbm: value.volumeCbm }] }
}
if (path === '/api/merchant/v1/orders/order-2') return { summary: { mainStatus: 'PENDING_REVIEW' } }