171 lines
16 KiB
TypeScript
171 lines
16 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const overseas = vi.hoisted(() => ({ get: vi.fn(), patch: vi.fn(), post: vi.fn(), put: vi.fn() }))
|
|
vi.mock('@/shared/api/http', async (importOriginal) => ({ ...(await importOriginal<typeof import('@/shared/api/http')>()), http: overseas }))
|
|
|
|
import {
|
|
createLocalLooseboardOrder, createUsTruckOrder, getLocalLooseboardOrder, getOverseasPurchase, getOverseasPurchaseEditor, getUsTruckOrder,
|
|
listLocalLooseboardOrders, listOverseasPurchaseCustomerOptions, listOverseasPurchases,
|
|
listOverseasPurchaseWarehouseOptions, listUsTruckOrders, updateLocalLooseboardOrder, updateOverseasPurchase,
|
|
updateUsTruckOrder, executeUsTruckCommand,
|
|
} from './platformOverseasBusiness'
|
|
import type { UpdateLocalTransportOrderRequest } from '../types'
|
|
import { readPendingProcurement } from '../procurementPending'
|
|
import type { TruckOperation } from '../truckWorkflow'
|
|
|
|
const envelope = <T>(data: T) => ({ code: '200', message: 'success', data })
|
|
const purchaseSummary = {
|
|
id: '9007199254740993', orderNo: 'OP-1', customerId: '10001', customerName: '客户一', purchaseCategory: '服装', productName: '外套',
|
|
productSpecification: null, purchaseQuantity: 10, budgetUnitPrice: 12.34, currency: 'USD', purchaseWarehouseId: '9007199254740995',
|
|
purchaseWarehouseName: '采购仓', status: 'WAIT_PACKING', version: 4,
|
|
createdAt: '2026-08-01T00:00:00', updatedAt: '2026-08-02T00:00:00',
|
|
}
|
|
const truckSummary = {
|
|
id: '9007199254740997', customerId: '10002', orderNo: 'TR-1', vehicleType: 'PALLET_15', origin: 'LA', destination: 'ONT',
|
|
palletCount: 2, packageCount: 10, receivedPalletCount: 1, allocatedPalletCount: 0, shippedPalletCount: 0,
|
|
totalWeightKg: 123.45, totalVolumeCbm: 6.7, budgetAmount: 88.5, currency: 'USD', originContactName: 'A', originContactPhone: '1', destinationContactName: 'B', destinationContactPhone: '2',
|
|
pickupAt: '2026-08-20T09:30:00', deliveryAt: '2026-08-20T15:45:00', status: 'PENDING_REVIEW', version: 2, createdAt: '2026-08-01T00:00:00', updatedAt: '2026-08-02T00:00:00',
|
|
}
|
|
const looseboardSummary = {
|
|
id: '9007199254741005', customerId: '10003', orderNo: 'LB-1', palletRequired: 'YES', transportType: 'WAREHOUSE_TO_WAREHOUSE',
|
|
origin: 'LA', destination: 'ONT', productName: '家居用品', estimatedPalletCount: 2, packageCount: 8, totalWeightKg: 80.5, totalVolumeCbm: 3.2,
|
|
budgetAmount: 50, currency: 'USD', originContactName: '发货人', originContactPhone: '1', destinationContactName: '收货人', destinationContactPhone: '2', pickupAt: '2026-10-20T09:30:00', deliveryAt: '2026-10-20T15:45:00', status: 'PENDING_ACCEPTANCE', version: 0,
|
|
createdAt: '2026-08-01T00:00:00', updatedAt: '2026-08-01T00:00:00',
|
|
}
|
|
|
|
describe('platform overseas business API adapter', () => {
|
|
beforeEach(() => vi.clearAllMocks())
|
|
|
|
it('normalizes wire integer strings before building replayable procurement commands', async () => {
|
|
overseas.get.mockResolvedValue(envelope({
|
|
customerView: { order: { ...purchaseSummary, version: '0', purchaseQuantity: '1' }, boxes: [{ quantity: '1' }], shipments: [{ quantity: '1' }], fees: [{ version: '2' }] },
|
|
purchaseFacts: { purchased_quantity: '1' }, physical: { packingVersion: '3', dispositions: [{ id: '901', quantity: '1' }], receipts: [{ id: '902', goodQuantity: '1', damagedQuantity: '0', extraQuantity: '0' }], cartons: [{ id: '903', quantity: '1', sources: [{ receiptId: '902', quantity: '1' }] }] },
|
|
fees: [{ id: '904', feeVersion: '2' }], controls: { notes: [], remaining: [{ quantity: '1' }], closures: [], closureItems: [{ quantity: '1' }] }, files: [{ id: '905', fileId: '9007199254740995', sizeBytes: '123' }],
|
|
transport: { overseasCargo: [{ cartonId: '903', quantity: '1', sources: [{ receiptId: '902', quantity: '1' }] }], domesticCargo: [], overseasLabels: { id: '906', manifestId: '907', version: '4', labels: [{ cartonId: '903', fileId: '9007199254740995', page: '1' }] }, domesticLabels: null, shipments: [] },
|
|
}))
|
|
const detail = await getOverseasPurchase('OP-1', '10001')
|
|
const payload = { version: detail.customerView.order.version, idempotencyKey: 'review-wire', decision: 'APPROVE', purchaseWarehouseId: '9007199254740995' }
|
|
expect(() => readPendingProcurement(JSON.stringify({ command: 'review', payload }))).not.toThrow()
|
|
expect(detail.customerView.order.purchaseQuantity).toBe(1)
|
|
expect(detail.physical.receipts[0].goodQuantity + detail.physical.receipts[0].damagedQuantity).toBe(1)
|
|
expect(detail.physical.packingVersion).toBe(3)
|
|
expect(detail.physical.cartons[0].sources[0].quantity).toBe(1)
|
|
expect(detail.transport.overseasCargo[0].quantity).toBe(1)
|
|
expect(detail.transport.overseasLabels?.version).toBe(4)
|
|
expect(detail.transport.overseasLabels?.labels[0].page).toBe(1)
|
|
expect(detail.files[0]).toMatchObject({ fileId: '9007199254740995', sizeBytes: 123 })
|
|
expect(detail.fees[0].feeVersion).toBe(2)
|
|
expect(detail.controls.remaining[0].quantity).toBe(1)
|
|
expect(detail.controls.closureItems[0].quantity).toBe(1)
|
|
})
|
|
|
|
it('loads procurement candidates and normalizes procurement pages', async () => {
|
|
overseas.get
|
|
.mockResolvedValueOnce(envelope([{ customerId: '10001', customerName: '客户一' }]))
|
|
.mockResolvedValueOnce(envelope([{ warehouseId: '9007199254740995', warehouseCode: 'LAX', warehouseName: '采购仓', countryCode: 'US' }]))
|
|
.mockResolvedValueOnce(envelope({ records: [purchaseSummary], page: 1, size: 20, total: 1, pages: 1 }))
|
|
expect(await listOverseasPurchaseCustomerOptions(' 客户 ', 50)).toEqual([{ customerId: '10001', customerName: '客户一' }])
|
|
expect(overseas.get).toHaveBeenNthCalledWith(1, '/platform/v1/last-leg/overseas-procurements/customer-options', { params: { keyword: '客户', limit: 50 } })
|
|
expect((await listOverseasPurchaseWarehouseOptions())[0]?.warehouseId).toBe('9007199254740995')
|
|
const result = await listOverseasPurchases({ page: 1, size: 20, customerId: '10001', orderNo: ' OP-1 ', purchaseWarehouseId: '9007199254740995' })
|
|
expect(overseas.get).toHaveBeenNthCalledWith(3, '/platform/v1/last-leg/overseas-procurements', { params: expect.objectContaining({ page: 1, size: 20, customerId: '10001', orderNo: 'OP-1', purchaseWarehouseId: '9007199254740995' }) })
|
|
expect(result.records[0]).toMatchObject({ id: '9007199254740993', purchaseWarehouseId: '9007199254740995', budgetUnitPrice: '12.34' })
|
|
})
|
|
|
|
it('loads and patches procurement detail with three-state null payloads', async () => {
|
|
const detail = { summary: purchaseSummary, productUrl: null, histories: [{ operationType: 'UPDATE', fromStatus: 'WAIT_PURCHASE', toStatus: 'WAIT_PURCHASE', operatorId: '7', changeDetail: '{}', remark: null, createdAt: '2026-08-02T00:00:00' }] }
|
|
overseas.get.mockResolvedValue(envelope({ customerView: { order: purchaseSummary, boxes: [], shipments: [], fees: [] }, reviewHistory: detail.histories, physical: { dispositions: [], receipts: [], cartons: [] }, fees: [], feeCorrections: [], controls: { notes: [], remaining: [], closures: [], closureItems: [] }, files: [], transport: { overseasCargo: [], domesticCargo: [], overseasLabels: null, domesticLabels: null, shipments: [] } }))
|
|
expect((await getOverseasPurchase('OP/1', '10001')).reviewHistory[0]?.operationType).toBe('UPDATE')
|
|
overseas.patch.mockResolvedValue(envelope(detail))
|
|
const payload = { version: 4, remark: null }
|
|
await updateOverseasPurchase('OP-1', '10001', payload)
|
|
expect(overseas.patch).toHaveBeenCalledWith('/platform/v1/last-leg/overseas-procurements/OP-1', payload, { params: { customerId: '10001' } })
|
|
})
|
|
|
|
it('reads the aggregate editor context and strips obsolete mutation fields', async () => {
|
|
const detail = { customerView: { order: { orderNo: 'OP/1', customerId: '10001', productName: '外套', purchaseQuantity: 10, version: 4 } }, internalRemark: '内部' }
|
|
overseas.get.mockResolvedValue(envelope(detail))
|
|
expect(await getOverseasPurchaseEditor('OP/1', '10001')).toEqual(detail)
|
|
expect(overseas.get).toHaveBeenCalledWith('/platform/v1/last-leg/overseas-procurements/OP%2F1', { params: { customerId: '10001' } })
|
|
overseas.patch.mockResolvedValue(envelope({ summary: purchaseSummary }))
|
|
const obsolete = { version: 4, remark: '内部', inboundQuantity: 200, purchaseWarehouseId: 'other', productName: 'changed' }
|
|
await updateOverseasPurchase('OP/1', '10001', obsolete)
|
|
expect(overseas.patch).toHaveBeenCalledWith('/platform/v1/last-leg/overseas-procurements/OP%2F1', { version: 4, remark: '内部' }, { params: { customerId: '10001' } })
|
|
})
|
|
|
|
it('supports platform truck list/detail/create/update', async () => {
|
|
const detail = { summary: truckSummary, pallets: [{ id: '9007199254741001', warehousePalletId: '9007199254741003', palletNo: 'P-1' }], histories: [] }
|
|
overseas.get.mockResolvedValueOnce(envelope({ records: [truckSummary], page: '1', size: '20', total: '1', pages: '1' })).mockResolvedValueOnce(envelope(detail))
|
|
expect((await listUsTruckOrders({ routeKeyword: ' LA ' })).records[0]?.totalWeightKg).toBe('123.45')
|
|
expect((await getUsTruckOrder('TR-1', '10002')).pallets[0]?.warehousePalletId).toBe('9007199254741003')
|
|
const create = { vehicleType: 'PALLET_15' as const, origin: 'LA', destination: 'ONT', packageCount: 10, totalWeightKg: 123.45, totalVolumeCbm: 6.7, originContactName: 'A', originContactPhone: '1', destinationContactName: 'B', destinationContactPhone: '2', pickupAt: '2026-08-20T09:30:00', deliveryAt: '2026-08-20T15:45:00', idempotencyKey: 'HTTP-20260824-MARKER-truck' }
|
|
overseas.post.mockResolvedValue(envelope(detail)); await createUsTruckOrder('9007199254741999', create)
|
|
expect(overseas.post).toHaveBeenCalledWith('/platform/v1/last-leg/us-truck-orders', create, { params: { customerId: '9007199254741999' } })
|
|
const { idempotencyKey: _truckKey, ...truckFields } = create
|
|
const update = { ...truckFields, version: 2 }
|
|
overseas.patch.mockResolvedValue(envelope(detail)); await updateUsTruckOrder('TR-1', '9007199254741999', update)
|
|
expect(overseas.patch).toHaveBeenCalledWith('/platform/v1/last-leg/us-truck-orders/TR-1', update, { params: { customerId: '9007199254741999' } })
|
|
})
|
|
|
|
it.each<TruckOperation>([
|
|
{ command: 'review', payload: { version: 2, idempotencyKey: 'review-key', decision: 'APPROVE' } },
|
|
{ command: 'review', payload: { version: 2, idempotencyKey: 'reject-key', decision: 'REJECT', remark: '资料不全' } },
|
|
{ command: 'receive-pallets', payload: { version: 2, idempotencyKey: 'receive-key', actualPalletCount: 3, packageCount: 10 } },
|
|
{ command: 'review-pallet-overage', payload: { version: 2, idempotencyKey: 'overage-key', decision: 'APPROVE' } },
|
|
{ command: 'allocate-pallets', payload: { version: 2, idempotencyKey: 'allocate-key', palletIds: ['9007199254741001'] } },
|
|
...(['prepare-dispatch', 'dispatch', 'start-transit', 'complete'] as const).map(command => ({ command, payload: { version: 2, idempotencyKey: command } })),
|
|
{ command: 'cancel', payload: { version: 2, idempotencyKey: 'cancel-key', status: 'CANCELLED', remark: '客户取消' } },
|
|
])('sends the scoped named truck command $command with version and unchanged replay key', async operation => {
|
|
const response = envelope({ summary: { ...truckSummary, version: '3', palletCount: '2', packageCount: '10', receivedPalletCount: '1', allocatedPalletCount: '0' }, pallets: [{ id: '9007199254741001', warehousePalletId: null }], histories: [] })
|
|
overseas.post.mockResolvedValue(response); overseas.patch.mockResolvedValue(response)
|
|
const result = await executeUsTruckCommand('TR/1', '9007199254741999', operation)
|
|
const mutation = operation.command === 'cancel' ? overseas.patch : overseas.post
|
|
expect(mutation).toHaveBeenCalledWith(`/platform/v1/last-leg/us-truck-orders/TR%2F1/${operation.command === 'cancel' ? 'status' : operation.command}`, operation.payload, { params: { customerId: '9007199254741999' } })
|
|
expect(result).toMatchObject({ version: 3, palletCount: 2, receivedPalletCount: 1, allocatedPalletCount: 0 })
|
|
expect(result.pallets[0].id).toBe('9007199254741001')
|
|
expect(operation.command === 'cancel' ? overseas.post : overseas.patch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('propagates a truck command version conflict without fallback status writes', async () => {
|
|
overseas.post.mockResolvedValue({ code: 'A0409', message: '版本冲突', data: null, requestId: 'TRUCK-409' })
|
|
await expect(executeUsTruckCommand('TR-1', '10002', { command: 'complete', payload: { version: 2, idempotencyKey: 'original' } })).rejects.toMatchObject({ code: 'A0409', requestId: 'TRUCK-409' })
|
|
expect(overseas.post).toHaveBeenCalledTimes(1)
|
|
expect(overseas.patch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('supports looseboard list/detail/create and PUT update', async () => {
|
|
const detail = { summary: looseboardSummary, originAddress: null, destinationAddress: null, remark: null, createdBy: '7', histories: [] }
|
|
overseas.get.mockResolvedValueOnce(envelope({ records: [looseboardSummary], page: 1, size: 20, total: 1, pages: 1 })).mockResolvedValueOnce(envelope(detail))
|
|
expect((await listLocalLooseboardOrders({ status: 'PENDING_ACCEPTANCE' })).records[0]?.id).toBe('9007199254741005')
|
|
expect((await getLocalLooseboardOrder('LB-1', '10003')).status).toBe('PENDING_ACCEPTANCE')
|
|
const create = { productName: '家居用品', estimatedPalletCount: 2, originContactName: 'A', originContactPhone: '1', destinationContactName: 'B', destinationContactPhone: '2', pickupAt: '2026-10-20T09:30:00', deliveryAt: '2026-10-20T15:45:00', origin: 'LA', destination: 'ONT', packageCount: 8, totalWeightKg: 80.5, totalVolumeCbm: 3.2, idempotencyKey: 'HTTP-20260824-MARKER-looseboard' }
|
|
overseas.post.mockResolvedValue(envelope(detail)); await createLocalLooseboardOrder('9007199254742001', create)
|
|
expect(overseas.post).toHaveBeenCalledWith('/platform/v1/last-leg/local-looseboard-orders', create, { params: { customerId: '9007199254742001' } })
|
|
const { idempotencyKey: _looseKey, ...looseboardFields } = create
|
|
const update = { ...looseboardFields, version: 0 }
|
|
overseas.put.mockResolvedValue(envelope(detail)); await updateLocalLooseboardOrder('LB/1', '9007199254742001', update)
|
|
expect(overseas.put).toHaveBeenCalledWith('/platform/v1/last-leg/local-looseboard-orders/LB%2F1', update, { params: { customerId: '9007199254742001' } })
|
|
})
|
|
|
|
it('propagates a 409 envelope without issuing any fallback write', async () => {
|
|
const payload: UpdateLocalTransportOrderRequest = {
|
|
version: 2, vehicleType: 'PALLET_15', origin: 'LA', destination: 'ONT-EDIT',
|
|
packageCount: 10, totalWeightKg: 123.45, totalVolumeCbm: 6.7, originContactName: 'A', originContactPhone: '1', destinationContactName: 'B', destinationContactPhone: '2',
|
|
pickupAt: '2026-08-20T09:30:00', deliveryAt: '2026-08-20T15:45:00',
|
|
}
|
|
overseas.patch.mockResolvedValue({ code: 'B0409', message: 'Order was changed', data: null, requestId: 'REQ-409' })
|
|
await expect(updateUsTruckOrder('TR-1', '9007199254741999', payload)).rejects.toMatchObject({ code: 'B0409', requestId: 'REQ-409' })
|
|
expect(overseas.patch).toHaveBeenCalledTimes(1)
|
|
expect(overseas.post).not.toHaveBeenCalled()
|
|
expect(overseas.put).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('preserves unknown pool pallet service and product name in list and detail responses', async () => {
|
|
const summary = { ...looseboardSummary, palletRequired: null, productName: null }
|
|
overseas.get.mockResolvedValueOnce(envelope({ records: [summary], total: 1, page: 1, size: 20, pages: 1 })).mockResolvedValueOnce(envelope({ summary, histories: [] }))
|
|
expect((await listLocalLooseboardOrders({})).records[0]).toMatchObject({ palletRequired: null, productName: null })
|
|
expect(await getLocalLooseboardOrder('LB-1', '10003')).toMatchObject({ palletRequired: null, productName: null })
|
|
expect(overseas.get).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|