Files
admin-web/src/modules/overseas-warehouse/api/lastMileOrders.test.ts
T
2026-08-31 14:42:28 +08:00

113 lines
6.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { overseasHttp } from '@/shared/api/overseasHttp'
import { convertLastMileOrderToInboundPlan, downloadLastMileOrders, getLastMileOrder, listLastMileOrders, setLastMileOrderDetained, setLastMileOrderPalletMergeBatch, setLastMileOrderPalletMergeRequired, transferLastMileOrders } from './lastMileOrders'
vi.mock('@/shared/api/overseasHttp', () => ({ overseasHttp: { get: vi.fn(), put: vi.fn(), post: vi.fn() } }))
describe('last-mile order overseas API', () => {
beforeEach(() => vi.mocked(overseasHttp.get).mockReset())
it('loads all platform orders without a customer scope', async () => {
vi.mocked(overseasHttp.get).mockResolvedValue({ code: '200', message: 'success', data: { records: [], page: '2', size: '20', total: '0', pages: '0' } })
await expect(listLastMileOrders({ page: 2, size: 20, orderType: 'FIRST_LEG', currentStatus: 'PENDING_ASSIGNMENT', warehouse: 'LAX' })).resolves.toEqual({ records: [], page: 2, size: 20, total: 0, pages: 0 })
expect(overseasHttp.get).toHaveBeenCalledWith('/platform/v1/last-leg/orders', {
params: { page: 2, size: 20, orderType: 'FIRST_LEG', currentStatus: 'PENDING_ASSIGNMENT', warehouse: 'LAX' },
})
})
it('preserves a long order id as a string in the detail path', async () => {
vi.mocked(overseasHttp.get).mockResolvedValue({ code: '200', message: 'success', data: { order: { id: '190000000000000099' }, cargo: [], statusHistory: [] } })
await getLastMileOrder('190000000000000099')
expect(overseasHttp.get).toHaveBeenCalledWith('/platform/v1/last-leg/orders/190000000000000099')
})
it('does not call outbound or delivery endpoints', () => {
const source = `${listLastMileOrders}${getLastMileOrder}`
expect(source).not.toMatch(/outbound|delivery|dispatch/)
})
it('passes all supported list filters and pagination without customerId', async () => {
vi.mocked(overseasHttp.get).mockResolvedValue({ code: '200', message: 'success', data: { records: [], page: 2, size: 50, total: 0, pages: 0 } })
await listLastMileOrders({
page: 2, size: 50, orderNo: 'LM-1', inboundCode: 'IN-1', sourceOrderNo: 'SRC-1',
containerNo: 'CONT-1', status: 'PENDING_DELIVERY', transferWarehouseCode: 'ONT8',
abnormal: true, detained: false, createdFrom: '2026-08-01T00:00:00', createdTo: '2026-08-31T23:59:59',
})
expect(overseasHttp.get).toHaveBeenCalledWith('/platform/v1/last-leg/orders', {
params: expect.objectContaining({ page: 2, size: 50, sourceOrderNo: 'SRC-1', transferWarehouseCode: 'ONT8', abnormal: true, detained: false }),
})
expect(JSON.stringify(vi.mocked(overseasHttp.get).mock.calls[0]?.[1])).not.toContain('customerId')
})
it('uses the real destination transfer endpoint and edit contract', async () => {
vi.mocked(overseasHttp.put).mockResolvedValue({ code: '200', message: 'success', data: 1 })
await transferLastMileOrders({
orderIds: ['190000000000000099'], warehouseId: 'WH-1', warehouseCode: 'ONT8', warehouseName: 'Ontario',
})
expect(overseasHttp.put).toHaveBeenCalledWith('/platform/v1/last-leg/orders/destination', expect.objectContaining({ orderIds: ['190000000000000099'] }))
})
it('returns the exported CSV blob without reading data twice', async () => {
const blob = new Blob(['orders'], { type: 'text/csv' })
vi.mocked(overseasHttp.get).mockResolvedValue(blob)
await expect(downloadLastMileOrders({ orderNo: 'LM-1' })).resolves.toBe(blob)
expect(overseasHttp.get).toHaveBeenCalledWith('/platform/v1/last-leg/orders/export', {
params: { orderNo: 'LM-1' }, responseType: 'blob',
})
})
it('exports all authorized orders without pagination or empty filters', async () => {
const blob = new Blob(['orders'], { type: 'text/csv' })
vi.mocked(overseasHttp.get).mockResolvedValue(blob)
await downloadLastMileOrders({ page: 1, size: 10, orderNo: '', createdFrom: undefined })
expect(overseasHttp.get).toHaveBeenCalledWith('/platform/v1/last-leg/orders/export', {
params: {}, responseType: 'blob',
})
})
it('rejects a JSON business error instead of saving it as CSV', async () => {
const blob = new Blob([JSON.stringify({
code: 'S0500', message: '系统内部错误', data: null, requestId: 'req-1', traceId: 'trace-1',
})], { type: 'application/json' })
vi.mocked(overseasHttp.get).mockResolvedValue(blob)
await expect(downloadLastMileOrders({})).rejects.toMatchObject({
code: 'S0500', message: '系统内部错误', requestId: 'req-1', traceId: 'trace-1',
})
})
it('uses the order hold endpoint with the required operation remark', async () => {
vi.mocked(overseasHttp.put).mockResolvedValue({ code: '200', message: 'success', data: null })
await setLastMileOrderDetained('190000000000000099', { detained: true, remark: '等待质检确认' })
expect(overseasHttp.put).toHaveBeenCalledWith('/platform/v1/last-leg/orders/190000000000000099/detained', {
detained: true, remark: '等待质检确认',
})
})
it('marks an order for a PDA pallet-merge reminder', async () => {
vi.mocked(overseasHttp.put).mockResolvedValue({ code: '200', message: 'success', data: null })
await setLastMileOrderPalletMergeRequired('190000000000000099', { palletMergeRequired: true })
expect(overseasHttp.put).toHaveBeenCalledWith('/platform/v1/last-leg/orders/190000000000000099/pallet-merge-required', {
palletMergeRequired: true,
})
})
it('marks selected orders as one visible pallet-merge group', async () => {
vi.mocked(overseasHttp.put).mockResolvedValue({ code: '200', message: 'success', data: null })
await setLastMileOrderPalletMergeBatch(['11', '12'], true)
expect(overseasHttp.put).toHaveBeenCalledWith('/platform/v1/last-leg/orders/pallet-merge-required/batch', {
orderIds: ['11', '12'], palletMergeRequired: true,
})
})
it('uses the inbound-plan conversion endpoint without customerId', async () => {
vi.mocked(overseasHttp.post).mockResolvedValue({ code: '200', message: 'success', data: {} })
await convertLastMileOrderToInboundPlan({ orderId: '190000000000000099' })
expect(overseasHttp.post).toHaveBeenCalledWith('/platform/v1/last-leg/inbound-plans/convert', { orderId: '190000000000000099' })
expect(JSON.stringify(vi.mocked(overseasHttp.post).mock.calls[0]?.[1])).not.toContain('customerId')
})
})