This commit is contained in:
2026-08-27 16:34:32 +08:00
parent 690f386bff
commit a90d5fbe39
9 changed files with 226 additions and 61 deletions
@@ -3,14 +3,14 @@ import { createPinia } from 'pinia'
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ApiResponseError } from '@/shared/api/http'
import DataTable from '@/shared/components/DataTable.vue'
import { convertLastMileOrderToInboundPlan, listLastMileOrders } from '../api/lastMileOrders'
import { convertLastMileContainerToInboundPlans, convertLastMileOrderToInboundPlan, listLastMileOrders } from '../api/lastMileOrders'
import LastMileOrdersView from './LastMileOrdersView.vue'
vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can: () => true }) }))
vi.mock('vue-router', () => ({ useRouter: () => ({ push: vi.fn() }) }))
vi.mock('../api/lastMileOrders', () => ({
listLastMileOrders: vi.fn(),
convertLastMileContainerToInboundPlans: vi.fn(),
convertLastMileOrderToInboundPlan: vi.fn(),
transferLastMileOrders: vi.fn(),
}))
@@ -28,22 +28,25 @@ function memoryStorage(): Storage {
enableAutoUnmount(afterEach)
afterAll(() => vi.unstubAllGlobals())
function selectedRow(customerId = '2092618717861048321') {
return { id: '2092873463568596994', customerId, orderNo: 'LMO-260827-0001' }
}
async function mountView() {
const wrapper = mount(LastMileOrdersView, { attachTo: document.body, global: { plugins: [createPinia(), ElementPlus] } })
await flushPromises()
return wrapper
}
async function selectAndConvert(wrapper: Awaited<ReturnType<typeof mountView>>, row = selectedRow()) {
wrapper.findComponent(DataTable).vm.$emit('selection-change', [row])
await wrapper.vm.$nextTick()
const button = wrapper.findAll('button').find((item) => item.text().includes('生成入库计划'))
expect(button).toBeDefined()
await button!.trigger('click')
const containerRecords = [
{ id: '1', customerId: '100', sourceContainerBookingId: '2092873463568597001', orderNo: 'LMO-1', inboundCode: 'IN-1', containerNo: 'CONT-1', packageCount: 10, palletCount: 1, volumeCbm: 2, currentStatus: 'PENDING_ASSIGNMENT' },
{ id: '2', customerId: '200', sourceContainerBookingId: '2092873463568597001', orderNo: 'LMO-2', inboundCode: 'IN-2', containerNo: 'CONT-1', packageCount: 5, palletCount: 2, volumeCbm: 1, currentStatus: 'PENDING_ASSIGNMENT' },
]
function plan(id: string, customerId: string) {
return { id, planNo: `LIP-${id}`, customerId, sourceOrderId: id, containerNo: 'CONT-1', status: 'PENDING_ASSIGNMENT', lineCount: 1, palletCount: 1, totalVolumeCbm: 1, lines: [], pallets: [], allocations: [] }
}
async function openPendingContainers(wrapper: Awaited<ReturnType<typeof mountView>>) {
const pendingTab = wrapper.findAll('.el-radio-button').find((item) => item.text().includes('待分配'))
expect(pendingTab).toBeDefined()
await pendingTab!.trigger('click')
await flushPromises()
}
@@ -53,50 +56,48 @@ describe('LastMileOrdersView inbound-plan conversion', () => {
vi.stubGlobal('localStorage', memoryStorage())
vi.mocked(listLastMileOrders).mockReset().mockResolvedValue({ records: [], page: 1, size: 10, total: 0, pages: 0 })
vi.mocked(convertLastMileOrderToInboundPlan).mockReset().mockResolvedValue({})
vi.mocked(convertLastMileContainerToInboundPlans).mockReset().mockResolvedValue([plan('1', '100'), plan('2', '200')])
})
it('submits the selected long customer id without coercing it to a number', async () => {
const wrapper = await mountView()
await selectAndConvert(wrapper)
expect(convertLastMileOrderToInboundPlan).toHaveBeenCalledWith({
orderId: '2092873463568596994', customerId: '2092618717861048321',
})
})
it('refreshes the list and displays INBOUND_PLANNED after a successful conversion', async () => {
const base = {
id: '2092873463568596994', customerId: '2092618717861048321', customerName: '华远贸易',
orderNo: 'LMO-260827-0001', orderType: 'FIRST_LEG',
}
it('calls the atomic container endpoint once and refreshes all affected orders', async () => {
vi.mocked(listLastMileOrders)
.mockResolvedValueOnce({ records: [{ ...base, currentStatus: 'PENDING_ASSIGNMENT' }], page: 1, size: 10, total: 1, pages: 1 })
.mockResolvedValueOnce({ records: [{ ...base, currentStatus: 'INBOUND_PLANNED', inboundOrderNo: 'IP-260827-0001' }], page: 1, size: 10, total: 1, pages: 1 })
.mockResolvedValueOnce({ records: [], page: 1, size: 10, total: 0, pages: 0 })
.mockResolvedValueOnce({ records: containerRecords, page: 1, size: 10, total: 2, pages: 1 })
.mockResolvedValueOnce({ records: [], page: 1, size: 10, total: 0, pages: 0 })
const wrapper = await mountView()
expect(wrapper.text()).toContain('待分配')
await selectAndConvert(wrapper)
expect(listLastMileOrders).toHaveBeenCalledTimes(2)
expect(wrapper.text()).toContain('已生成入库计划')
expect(wrapper.text()).toContain('IP-260827-0001')
})
it('fails closed when the selected row has no customer scope', async () => {
const wrapper = await mountView()
await selectAndConvert(wrapper, selectedRow(''))
await openPendingContainers(wrapper)
expect(wrapper.text()).toContain('CONT-1')
expect(wrapper.text()).toContain('将为 2 个尾程订单生成 2 个独立入库计划,PDA仍按同柜作业')
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
expect(button).toBeDefined()
expect(button!.attributes('disabled')).toBeUndefined()
await button!.trigger('click')
await flushPromises()
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledTimes(1)
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledWith({ sourceContainerBookingId: '2092873463568597001' })
expect(convertLastMileOrderToInboundPlan).not.toHaveBeenCalled()
expect(wrapper.text()).toContain('所选尾程订单缺少客户标识')
expect(listLastMileOrders).toHaveBeenCalledTimes(3)
expect(document.body.textContent).toContain('已为2个尾程订单生成2个入库计划')
})
it.each([
['A0404', 'Inbound order not found'],
['A0409', 'Inbound order version conflict'],
])('keeps %s request metadata visible after conversion failure', async (code, message) => {
vi.mocked(convertLastMileOrderToInboundPlan).mockRejectedValueOnce(new ApiResponseError(
['A0404', 'Container orders not found'],
['A0409', 'Container conversion conflict'],
])('keeps %s request evidence after the atomic operation fails', async (code, message) => {
vi.mocked(listLastMileOrders)
.mockResolvedValueOnce({ records: [], page: 1, size: 10, total: 0, pages: 0 })
.mockResolvedValueOnce({ records: containerRecords, page: 1, size: 10, total: 2, pages: 1 })
vi.mocked(convertLastMileContainerToInboundPlans).mockRejectedValueOnce(new ApiResponseError(
message, code, 200, undefined, `request-${code}`, `trace-${code}`,
))
const wrapper = await mountView()
await selectAndConvert(wrapper)
await openPendingContainers(wrapper)
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
await button!.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain(code)
expect(wrapper.text()).toContain(`request-${code}`)
expect(wrapper.text()).toContain(`trace-${code}`)
expect(convertLastMileOrderToInboundPlan).not.toHaveBeenCalled()
})
})