Files
baiyue-firstleg/scripts/create-pending-review-orders-contract.test.mjs

160 lines
7.7 KiB
JavaScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { PRODUCT_CATALOG, buildOrderPayload, diversifyRoutes, parseArguments, randomOrderOptions, selectSameChannelRoutes, validateCount, pendingSubmitCommand } from './create-pending-review-orders.mjs'
test('submission binds original detail identity version and immutable key',()=>{
const detail={order:{id:'42',version:7}}, command=pendingSubmitCommand(detail,'42','original')
detail.order.version=99
assert.deepEqual(command,{body:{expectedVersion:7},headers:{'Idempotency-Key':'original'}})
assert.ok(Object.isFrozen(command.body))
for(const version of [undefined,null,'7',-1])assert.throws(()=>pendingSubmitCommand({order:{id:'42',version}},'42','key'))
assert.throws(()=>pendingSubmitCommand(detail,'other','key'))
})
const source = readFileSync(new URL('./create-pending-review-orders.mjs', import.meta.url), 'utf8')
test('uses the configured merchant account and stops at pending review', () => {
assert.match(source, /process\.env\.ADMIN_TEST_GATEWAY/)
assert.match(source, /process\.env\.MERCHANT_TEST_USERNAME/)
assert.match(source, /process\.env\.MERCHANT_TEST_PASSWORD/)
assert.match(source, /const DEFAULT_COUNT = 50/)
assert.doesNotMatch(source, /const CONFIG\s*=/)
assert.doesNotMatch(source, /8\.163\.30\.128/)
assert.doesNotMatch(source, /PASSWORD:\s*'[^']+'/)
assert.match(source, /randomInt\(1, 31\)/)
assert.match(source, /loadPricedRoutes/)
assert.match(source, /destinationWarehouseCode/)
assert.match(source, /channelCode/)
assert.match(source, /pricingTaxMode/)
assert.equal(PRODUCT_CATALOG.length, 8)
assert.match(source, /\/api\/merchant\/v1\/orders\/first-leg\/\$\{encodeURIComponent\(orderId\)\}\/submit/)
assert.match(source, /detail\?\.order\?\.status !== 'SUBMITTED'/)
assert.match(source, /\/api\/merchant\/v1\/orders\/\$\{encodeURIComponent\(orderId\)\}/)
assert.match(source, /unifiedOrder\?\.summary\?\.mainStatus !== 'PENDING_REVIEW'/)
assert.match(source, /mainStatus: unifiedOrder\.summary\.mainStatus/)
assert.doesNotMatch(source, /partners\/candidates\?type=SHIPPER/)
assert.doesNotMatch(source, /\/api\/platform\/v1\/iam\/tenants/)
assert.doesNotMatch(source, /\/approve/)
})
test('supports a bounded fixed order count', () => {
assert.equal(validateCount(undefined), 50)
assert.equal(validateCount('1'), 1)
assert.equal(validateCount('10'), 10)
assert.throws(() => validateCount('0'), /between 1 and 100/)
assert.throws(() => validateCount('101'), /between 1 and 100/)
assert.deepEqual(parseArguments(['--count', '5', '--gateway=http://localhost:9000']), {
count: '5', gateway: 'http://localhost:9000',
})
})
test('builds an order payload owned by the fixed profile', () => {
const payload = buildOrderPayload({
marker: 'PENDING-TEST',
index: 2,
profile: { id: '44', tenantName: '固定测试客户' },
master: {
receiving: { id: '11', warehouseName: '国内仓' },
destination: { id: '22', warehouseName: 'ONT8', warehouseCode: 'ONT8', countryCode: 'US' },
channel: { channelCode: 'SEA-US', channelName: '美西海运' },
pricingTaxMode: 'TAX_INCLUDED',
},
product: PRODUCT_CATALOG[0],
packageCount: 7,
options: {
tradeTerm: 'EXW', pickupMethod: 'SELF_TRUCK', taxMode: 'TAX_REFUND',
customsInspection: true, expectedShipDays: 15,
senderLocation: { province: '浙江省', city: '义乌市', district: '稠城街道', postalCode: '322000' },
},
productImageFileId: '33',
pickupAddressId: '55',
})
assert.equal(payload.receivingWarehouseId, '11')
assert.equal(payload.destinationWarehouseId, '22')
assert.equal(payload.customerId, '44')
assert.equal(payload.customerName, '固定测试客户')
assert.equal(payload.cargoes.length, 1)
assert.equal(payload.cargoes[0].productImageFileId, '33')
assert.equal(payload.cargoes[0].woodenProduct, false)
assert.equal(payload.tradeTerm, 'EXW')
assert.equal(payload.pickupMethod, 'SELF_TRUCK')
assert.equal(payload.taxMode, 'TAX_REFUND')
assert.equal(payload.customsInspection, true)
assert.equal(payload.sender, null)
assert.equal(payload.senderAddressId, null)
assert.equal(payload.requestedPickupAt, null)
assert.equal(payload.cargoes[0].cargoName, PRODUCT_CATALOG[0].cargoName)
assert.equal(payload.cargoes[0].packageCount, 7)
assert.equal(payload.cargoes[0].totalQuantity, PRODUCT_CATALOG[0].unitsPerPackage * 7)
assert.equal(payload.cargoes[0].grossWeightKg, Number((PRODUCT_CATALOG[0].cartonGrossWeightKg * 7).toFixed(3)))
assert.equal(payload.cargoes[0].declaredValue, PRODUCT_CATALOG[0].unitDeclaredValue * PRODUCT_CATALOG[0].unitsPerPackage * 7)
})
test('uses the selected customer pickup address and appointment for pickup orders', () => {
const payload = buildOrderPayload({
marker: 'PENDING-PICKUP', index: 1,
profile: { id: '44', tenantName: '固定测试客户' },
master: {
receiving: { id: '11', warehouseName: '国内仓' },
destination: { id: '22', warehouseName: 'ONT8', warehouseCode: 'ONT8', countryCode: 'US' },
channel: { channelCode: 'SEA-US', channelName: '美西海运' }, pricingTaxMode: 'TAX_INCLUDED',
},
product: PRODUCT_CATALOG[0], packageCount: 1, productImageFileId: '33', pickupAddressId: '55',
options: {
tradeTerm: 'FOB', pickupMethod: 'PICKUP', taxMode: 'BUY_DECLARATION',
customsInspection: false, expectedShipDays: 7,
senderLocation: { province: '广东省', city: '深圳市', district: '南山区', postalCode: '518000' },
},
})
assert.equal(payload.sender, null)
assert.equal(payload.senderAddressId, '55')
assert.match(payload.requestedPickupAt, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)
})
test('randomizes only supported basic-order fields', () => {
for (let index = 0; index < 100; index += 1) {
const options = randomOrderOptions()
assert.ok(['FOB', 'EXW', 'CIF', 'DDP'].includes(options.tradeTerm))
assert.ok(['SELF_EXPRESS', 'PICKUP', 'SELF_TRUCK'].includes(options.pickupMethod))
assert.ok(['BUY_DECLARATION', 'TAX_REFUND'].includes(options.taxMode))
assert.equal(typeof options.customsInspection, 'boolean')
assert.ok(options.expectedShipDays >= 7 && options.expectedShipDays <= 60)
}
})
test('interleaves different logistics combinations before repeating one', () => {
const route = (receivingId, channelCode, pricingTaxMode, destinationCode) => ({
receiving: { id: receivingId }, channel: { channelCode }, pricingTaxMode,
destination: { warehouseCode: destinationCode },
})
const diversified = diversifyRoutes([
route('1', 'SEA-A', 'TAX_INCLUDED', 'A1'),
route('1', 'SEA-A', 'TAX_INCLUDED', 'A2'),
route('1', 'SEA-B', 'TAX_EXCLUDED', 'B1'),
route('1', 'SEA-B', 'TAX_EXCLUDED', 'B2'),
])
const firstKey = `${diversified[0].channel.channelCode}|${diversified[0].pricingTaxMode}`
const secondKey = `${diversified[1].channel.channelCode}|${diversified[1].pricingTaxMode}`
assert.notEqual(firstKey, secondKey)
})
test('selects routes from exactly one logistics channel', () => {
const route = (receivingId, channelCode, pricingTaxMode, destinationCode) => ({
receiving: { id: receivingId }, channel: { channelCode }, pricingTaxMode,
destination: { warehouseCode: destinationCode },
})
const selected = selectSameChannelRoutes([
route('1', 'SEA-A', 'TAX_INCLUDED', 'A1'),
route('1', 'SEA-A', 'TAX_INCLUDED', 'A2'),
route('1', 'SEA-B', 'TAX_EXCLUDED', 'B1'),
])
assert.equal(selected.length, 2)
assert.equal(new Set(selected.map((item) => `${item.receiving.id}|${item.channel.channelCode}|${item.pricingTaxMode}`)).size, 1)
})
test('parses same-channel as a boolean flag', () => {
assert.deepEqual(parseArguments(['--count', '10', '--same-channel']), { count: '10', 'same-channel': true })
})