优化
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import test from 'node:test'
|
||||
|
||||
import { findPricedOrderRoute } from './orders.mjs'
|
||||
|
||||
const source = readFileSync(new URL('orders.mjs', import.meta.url), 'utf8')
|
||||
|
||||
test('orders fixture verifies the complete pricing tuple before selecting an FBA warehouse', () => {
|
||||
assert.match(source, /\/api\/merchant\/v1\/first-leg\/pricing\/estimate/)
|
||||
for (const field of ['originWarehouseId', 'channelCode', 'destinationCode', 'destinationType',
|
||||
'destinationWarehouseId', 'pricingTaxMode', 'actualWeightKg', 'actualVolumeCbm']) {
|
||||
assert.match(source, new RegExp(`${field}:`))
|
||||
}
|
||||
assert.match(source, /estimateMatches\(response, candidate\)/)
|
||||
assert.doesNotMatch(source, /const destination = records\(fbaPage\)\[0\]/)
|
||||
})
|
||||
|
||||
test('order payload reuses the tax mode from the successful estimate', () => {
|
||||
assert.match(source, /pricingTaxMode: master\.pricingTaxMode/)
|
||||
assert.doesNotMatch(source, /pricingTaxMode: master\.channel\.pricingTaxModes\[0\]/)
|
||||
assert.match(source, /没有可计价组合/)
|
||||
})
|
||||
|
||||
test('pricing route accepts only an estimate matching the active version channel and tax tuple', async () => {
|
||||
const requests = []
|
||||
const result = await findPricedOrderRoute({
|
||||
receiving: { id: '100' },
|
||||
destinations: [
|
||||
{ id: '201', warehouseCode: 'ABE2', warehouseName: 'ABE2' },
|
||||
{ id: '202', warehouseCode: 'ABE3', warehouseName: 'ABE3' },
|
||||
],
|
||||
channels: [{ priceBookVersionId: '300', channelCode: 'EXX_RAIL_NY',
|
||||
pricingTaxModes: ['TAX_INCLUDED'] }],
|
||||
concurrency: 1,
|
||||
estimate: async (request) => {
|
||||
requests.push(request)
|
||||
if (request.destinationCode === 'ABE2') {
|
||||
return { http: 200, body: { code: 'A0404' }, requestId: 'request-abe2', traceId: 'trace-abe2' }
|
||||
}
|
||||
return { http: 200, body: { code: '200', data: {
|
||||
priceBookVersionId: '300', channelCode: 'EXX_RAIL_NY', pricingTaxMode: 'TAX_INCLUDED',
|
||||
} } }
|
||||
},
|
||||
})
|
||||
assert.equal(result.priced.destination.warehouseCode, 'ABE3')
|
||||
assert.equal(result.attempts, 2)
|
||||
assert.deepEqual(requests[1], {
|
||||
originWarehouseId: '100', channelCode: 'EXX_RAIL_NY', destinationCode: 'ABE3',
|
||||
destinationType: 'FBA', destinationWarehouseId: '202', destinationName: 'ABE3',
|
||||
pricingTaxMode: 'TAX_INCLUDED', actualWeightKg: 46, actualVolumeCbm: 0.278,
|
||||
})
|
||||
assert.equal(result.failures[0].requestId, 'request-abe2')
|
||||
})
|
||||
|
||||
test('pricing route rejects a nominal success from a different version or tuple', async () => {
|
||||
const result = await findPricedOrderRoute({
|
||||
receiving: { id: '100' },
|
||||
destinations: [{ id: '202', warehouseCode: 'ABE3', warehouseName: 'ABE3' }],
|
||||
channels: [{ priceBookVersionId: '300', channelCode: 'EXX_RAIL_NY',
|
||||
pricingTaxModes: ['TAX_INCLUDED'] }],
|
||||
estimate: async () => ({ http: 200, body: { code: '200', data: {
|
||||
priceBookVersionId: 'old-version', channelCode: 'OTHER', pricingTaxMode: 'TAX_EXCLUDED',
|
||||
} }, requestId: 'request-drift', traceId: 'trace-drift' }),
|
||||
})
|
||||
assert.equal(result.priced, null)
|
||||
assert.equal(result.attempts, 1)
|
||||
assert.match(result.failures[0].mismatch, /version=old-version,channel=OTHER,tax=TAX_EXCLUDED/)
|
||||
})
|
||||
|
||||
test('pricing route bounds parallel probes and keeps deterministic candidate selection', async () => {
|
||||
let active = 0; let maximumActive = 0
|
||||
const result = await findPricedOrderRoute({
|
||||
receiving: { id: '100' }, concurrency: 50,
|
||||
destinations: Array.from({ length: 8 }, (_, index) => ({
|
||||
id: String(200 + index), warehouseCode: `FBA${index}`, warehouseName: `FBA ${index}`,
|
||||
})),
|
||||
channels: [{ priceBookVersionId: '300', channelCode: 'CHANNEL', pricingTaxModes: ['TAX_INCLUDED'] }],
|
||||
estimate: async (request) => {
|
||||
active += 1; maximumActive = Math.max(maximumActive, active)
|
||||
await new Promise((resolve) => setTimeout(resolve, request.destinationCode === 'FBA0' ? 5 : 1))
|
||||
active -= 1
|
||||
return { http: 200, body: { code: '200', data: {
|
||||
priceBookVersionId: '300', channelCode: 'CHANNEL', pricingTaxMode: 'TAX_INCLUDED',
|
||||
} } }
|
||||
},
|
||||
})
|
||||
assert.equal(maximumActive, 6)
|
||||
assert.equal(result.priced.destination.warehouseCode, 'FBA0')
|
||||
assert.equal(result.attempts, 6)
|
||||
})
|
||||
Reference in New Issue
Block a user