2026-10-19 15:50:39 +08:00
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { runInNewContext } from 'node:vm'
import test from 'node:test'
import ts from 'typescript'
const source = readFileSync ( new URL ( './transportDemandForm.ts' , import . meta . url ), 'utf8' )
const code = ts . transpileModule ( source , { compilerOptions : { target : ts . ScriptTarget . ES2022 , module : ts . ModuleKind . ESNext } }). outputText . replace ( /^export /gm , '' )
const { createTransportDemandForm , transportDemandError , buildTransportDemand } = runInNewContext ( ` ${ code } \n({ createTransportDemandForm, transportDemandError, buildTransportDemand })` )
2026-11-02 17:15:55 +08:00
const valid = () => ({ ... createTransportDemandForm (), deliveryMode : 'WAREHOUSE_PICKUP' , operatingWarehouseId : '9' , destinationType : 'FBA' , packageCount : 8 , vehicleType : 'PALLET_15' , estimatedPalletCount : 3 , budgetAmount : 100 , origin : ' LA ' , originAddress : '123 Test Road' , destination : ' ONT ' , originContactName : ' 发货人 ' , originContactPhone : ' +1-555-0100 ' , destinationContactName : ' 收货人 ' , destinationContactPhone : ' +1-555-0101 ' , pickupAt : '2026-10-20T09:30:45' , deliveryAt : '2026-10-20T15:45:12' })
test ( 'delivery to warehouse permits no tracking number while requiring receiving contacts and arrival time' , () => {
const form = { ... valid (), deliveryMode : 'DELIVERY_TO_WAREHOUSE' , deliveryContactName : '仓库收货员' , deliveryContactPhone : '10003' , estimatedArrivalAt : '2026-10-20T09:00:00' , trackingNo : '' }
assert . equal ( transportDemandError ( 'looseboard' , form ), undefined )
assert . equal ( buildTransportDemand ( 'looseboard' , form ). trackingNo , undefined )
for ( const field of [ 'deliveryContactName' , 'deliveryContactPhone' , 'estimatedArrivalAt' ]) assert . ok ( transportDemandError ( 'looseboard' , { ... form , [ field ] : '' }))
})
2026-10-19 15:50:39 +08:00
test ( 'initial form leaves contacts and times blank, zeroes cargo and distinguishes blank budget from zero' , () => {
const form = createTransportDemandForm ()
for ( const key of [ 'originContactName' , 'originContactPhone' , 'destinationContactName' , 'destinationContactPhone' , 'pickupAt' , 'deliveryAt' ]) assert . equal ( form [ key ], '' )
for ( const key of [ 'estimatedPalletCount' , 'packageCount' , 'totalWeightKg' , 'totalVolumeCbm' ]) assert . equal ( form [ key ], 0 )
assert . equal ( form . budgetAmount , undefined )
assert . equal ( form . currency , 'USD' )
2026-10-29 13:37:28 +08:00
assert . throws (() => buildTransportDemand ( 'truck' , { ... valid (), budgetAmount : undefined }), /预算/ )
assert . throws (() => buildTransportDemand ( 'truck' , { ... valid (), budgetAmount : 0 }), /预算/ )
2026-10-19 15:50:39 +08:00
})
for ( const kind of [ 'truck' , 'looseboard' ]) {
test ( ` ${ kind } requires both route contacts and both service times before building a payload` , () => {
for ( const field of [ 'origin' , 'destination' , 'originContactName' , 'originContactPhone' , 'destinationContactName' , 'destinationContactPhone' , 'pickupAt' , 'deliveryAt' ]) {
const form = { ... valid (), [ field ] : '' }
assert . ok ( transportDemandError ( kind , form ), field )
assert . throws (() => buildTransportDemand ( kind , form ), undefined , field )
}
})
test ( ` ${ kind } rejects negative cargo, fractional counts, invalid budgets and reversed times` , () => {
for ( const field of [ 'packageCount' , 'totalWeightKg' , 'totalVolumeCbm' , 'budgetAmount' ]) {
assert . ok ( transportDemandError ( kind , { ... valid (), [ field ] : - 1 }), field )
assert . ok ( transportDemandError ( kind , { ... valid (), [ field ] : Number . NaN }), field )
}
assert . ok ( transportDemandError ( kind , { ... valid (), packageCount : 1.5 }))
assert . ok ( transportDemandError ( kind , { ... valid (), deliveryAt : '2026-10-20T09:29:00' }))
assert . ok ( transportDemandError ( kind , { ... valid (), currency : 'EUR' }))
})
test ( ` ${ kind } sends minute wall-clock values, trims contacts, and excludes customer and old fields` , () => {
const payload = buildTransportDemand ( kind , { ... valid (), customerId : 'forged' , tenantId : 'forged' , transportMode : 'OLD' , containerType : '40HQ' })
assert . equal ( payload . pickupAt , '2026-10-20T09:30:00' )
assert . equal ( payload . deliveryAt , '2026-10-20T15:45:00' )
assert . equal ( payload . origin , 'LA' )
assert . equal ( payload . originContactName , '发货人' )
assert . equal ( payload . destinationContactPhone , '+1-555-0101' )
for ( const key of [ 'customerId' , 'tenantId' , 'transportMode' , 'transportType' , 'containerType' , 'contactName' , 'contactInfo' , 'requestedDate' ]) assert . equal ( Object . hasOwn ( payload , key ), false , key )
assert . equal ( transportDemandError ( kind , { ... valid (), deliveryAt : '2026-10-20T09:30:01' }), undefined )
})
}
test ( 'truck accepts only the two vehicle types and does not submit looseboard cargo forecasts' , () => {
assert . ok ( transportDemandError ( 'truck' , { ... valid (), vehicleType : 'PALLET_99' }))
const truck = buildTransportDemand ( 'truck' , { ... valid (), productName : 'cargo' , estimatedPalletCount : 3 })
assert . equal ( Object . hasOwn ( truck , 'productName' ), false )
assert . equal ( Object . hasOwn ( truck , 'estimatedPalletCount' ), false )
2026-10-29 13:37:28 +08:00
assert . equal ( truck . palletCount , 3 )
assert . equal ( Object . hasOwn ( truck , 'packageCount' ), false )
2026-10-19 15:50:39 +08:00
const loose = buildTransportDemand ( 'looseboard' , { ... valid (), vehicleType : '' , productName : ' cargo ' , estimatedPalletCount : 3 , packageCount : 8 })
assert . equal ( Object . hasOwn ( loose , 'vehicleType' ), false )
assert . equal ( loose . productName , 'cargo' )
assert . equal ( loose . estimatedPalletCount , 3 )
assert . equal ( loose . packageCount , 8 )
})
2026-11-02 17:15:55 +08:00
test ( 'warehouse delivery clears hidden origin data and pickup requires the full origin address' , () => {
const form = {... valid (), deliveryMode : 'DELIVERY_TO_WAREHOUSE' , deliveryContactName : 'warehouse' , deliveryContactPhone : '000' , estimatedArrivalAt : '2026-10-20T09:00:00' }
for ( const field of [ 'origin' , 'originAddress' , 'originContactName' , 'originContactPhone' ]) assert . equal ( transportDemandError ( 'looseboard' ,{... form ,[ field ] : '' }), undefined )
const payload = buildTransportDemand ( 'looseboard' , form )
assert . equal ( payload . origin , '' ); assert . equal ( payload . originAddress , undefined ); assert . equal ( payload . originContactName , '' ); assert . equal ( payload . originContactPhone , '' )
assert . match ( transportDemandError ( 'looseboard' ,{... valid (), originAddress : '' }), /详细地址/ )
})