136 lines
6.7 KiB
JavaScript
136 lines
6.7 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import test from 'node:test'
|
|
|
|
import { eligibleOrderDestinations, findPricedOrderRoute, findPricedOrderRouteAcrossOrigins } 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, /records\(domesticPage\)\[0\]/)
|
|
assert.match(source, /findPricedOrderRouteAcrossOrigins/)
|
|
assert.match(source, /warehouse\?\.status === 'ACTIVE'/)
|
|
})
|
|
|
|
test('private addresses select an authoritative channel without requiring a warehouse destination rate row', async () => {
|
|
const result = await findPricedOrderRouteAcrossOrigins({
|
|
receivings: [{ id: 'origin' }], destinations: [], destinationType: 'PRIVATE',
|
|
loadChannels: async () => ({ http: 200, body: { code: '200', data: [{ channelCode: 'C', pricingTaxModes: ['TAX_INCLUDED'] }] } }),
|
|
estimate: async () => { throw new Error('private address is not a warehouse destination') },
|
|
})
|
|
assert.equal(result.priced.channel.channelCode, 'C')
|
|
assert.equal(result.priced.destination, null)
|
|
assert.equal(result.attempts, 0)
|
|
})
|
|
|
|
test('FBA order fixture excludes warehouses without the internal code required by pallet labels', () => {
|
|
const warehouses = [
|
|
{ id: 'abe3', warehouseCode: 'ABE3', status: 'ACTIVE', internalCode: null },
|
|
{ id: 'abe4', warehouseCode: 'ABE4', status: 'ACTIVE', internalCode: '126' },
|
|
{ id: 'abe8', warehouseCode: 'ABE8', status: 'INACTIVE', internalCode: '39' },
|
|
]
|
|
assert.deepEqual(eligibleOrderDestinations(warehouses, 'FBA').map((item) => item.warehouseCode), ['ABE4'])
|
|
assert.deepEqual(eligibleOrderDestinations(warehouses, 'OVERSEAS').map((item) => item.warehouseCode), ['ABE3', 'ABE4'])
|
|
})
|
|
|
|
test('pricing selection skips an ACTIVE domestic warehouse with an empty channel catalog', async () => {
|
|
const channelCalls = []
|
|
const result = await findPricedOrderRouteAcrossOrigins({
|
|
receivings: [{ id: 'unpriced', status: 'ACTIVE' }, { id: 'priced', status: 'ACTIVE' }],
|
|
destinations: [{ id: 'abe3', warehouseCode: 'ABE3', warehouseName: 'ABE3' }],
|
|
loadChannels: async (receiving) => {
|
|
channelCalls.push(receiving.id)
|
|
return receiving.id === 'unpriced'
|
|
? { http: 200, body: { code: '200', data: [] }, requestId: 'channels-empty', traceId: 'trace-empty' }
|
|
: { http: 200, body: { code: '200', data: [{ priceBookVersionId: '19', channelCode: 'EXX_RAIL_NY', pricingTaxModes: ['TAX_INCLUDED'] }] } }
|
|
},
|
|
estimate: async (request) => ({ http: 200, body: { code: '200', data: {
|
|
priceBookVersionId: '19', channelCode: request.channelCode, pricingTaxMode: request.pricingTaxMode,
|
|
} } }),
|
|
})
|
|
assert.deepEqual(channelCalls, ['unpriced', 'priced'])
|
|
assert.equal(result.receiving.id, 'priced')
|
|
assert.equal(result.priced.destination.warehouseCode, 'ABE3')
|
|
assert.equal(result.originFailures[0].requestId, 'channels-empty')
|
|
})
|
|
|
|
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)
|
|
})
|