This commit is contained in:
2026-08-27 17:49:05 +08:00
parent 929f50dc55
commit 44d05e77cb
3 changed files with 182 additions and 126 deletions
@@ -3,14 +3,20 @@ 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 { convertLastMileContainerToInboundPlans, convertLastMileOrderToInboundPlan, listLastMileOrders, listPendingContainerSummaries } from '../api/lastMileOrders'
import DataTable from '@/shared/components/DataTable.vue'
import {
convertLastMileContainerToInboundPlans,
convertLastMileOrderToInboundPlan,
listLastMileOrders,
listPendingContainerSummaries,
} 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(), listPendingContainerSummaries: vi.fn(),
convertLastMileContainerToInboundPlans: vi.fn(), convertLastMileOrderToInboundPlan: vi.fn(), transferLastMileOrders: vi.fn(),
convertLastMileOrderToInboundPlan: vi.fn(), convertLastMileContainerToInboundPlans: vi.fn(), transferLastMileOrders: vi.fn(),
}))
class ResizeObserverStub implements ResizeObserver { observe() {} unobserve() {} disconnect() {} }
@@ -22,94 +28,126 @@ function memoryStorage(): Storage {
}
}
const bookingId = '2092873463568597001'
const customerId = '2092618717861048321'
const summary = {
sourceContainerBookingId: '2092873463568597001', containerNo: 'CONT-15', orderCount: 15,
sourceContainerBookingId: bookingId, containerNo: 'CONT-15', orderCount: 15,
inboundCodeCount: 15, customerCount: 3, totalPackageCount: 150, totalPalletCount: 30,
totalVolumeCbm: 45.5, eligible: true, ineligibleReason: null,
}
function plans(count = 15) {
return Array.from({ length: count }, (_, index) => ({
id: String(index + 1), planNo: `LIP-${index + 1}`, customerId: String(index % 3 + 1), sourceOrderId: String(index + 1),
containerNo: 'CONT-15', status: 'PENDING_ASSIGNMENT', lineCount: 1, palletCount: 1, totalVolumeCbm: 1,
lines: [], pallets: [], allocations: [],
}))
function selectedRow(overrides: Record<string, unknown> = {}) {
return { id: '2092873463568596994', customerId, sourceContainerBookingId: bookingId, orderNo: 'LMO-260827-0001', ...overrides }
}
enableAutoUnmount(afterEach)
afterAll(() => vi.unstubAllGlobals())
async function openPending() {
async function mountView() {
const wrapper = mount(LastMileOrdersView, { attachTo: document.body, global: { plugins: [createPinia(), ElementPlus] } })
await flushPromises()
const pendingTab = wrapper.findAll('.el-radio-button').find((item) => item.text().includes('待分配'))
return wrapper
}
async function openPending() {
const wrapper = await mountView()
const pendingTab = wrapper.findAll('.el-radio-button').find((item) => item.text() === '待分配')
await pendingTab!.trigger('click')
await flushPromises()
return wrapper
}
describe('LastMileOrdersView server-side container summaries', () => {
async function selectRows(wrapper: Awaited<ReturnType<typeof mountView>>, rows: object[]) {
wrapper.findComponent(DataTable).vm.$emit('selection-change', rows)
await wrapper.vm.$nextTick()
}
function button(wrapper: Awaited<ReturnType<typeof mountView>>, text: string) {
return wrapper.findAll('button').find((item) => item.text().includes(text))
}
async function openContainerDialog(wrapper: Awaited<ReturnType<typeof mountView>>) {
await button(wrapper, '按柜批量生成')!.trigger('click')
await flushPromises()
}
async function chooseContainer(wrapper: Awaited<ReturnType<typeof mountView>>) {
const selects = wrapper.findAllComponents({ name: 'ElSelect' })
const containerSelect = selects[selects.length - 1]
expect(containerSelect).toBeDefined()
containerSelect.vm.$emit('update:modelValue', bookingId)
await wrapper.vm.$nextTick()
}
describe('LastMileOrdersView pending order and container conversion', () => {
beforeEach(() => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
vi.stubGlobal('localStorage', memoryStorage())
vi.mocked(listLastMileOrders).mockReset().mockResolvedValue({ records: [], page: 1, size: 10, total: 0, pages: 0 })
vi.mocked(listPendingContainerSummaries).mockReset().mockResolvedValue({ records: [summary], page: 1, size: 10, total: 21, pages: 3 })
vi.mocked(convertLastMileContainerToInboundPlans).mockReset().mockResolvedValue(plans())
vi.mocked(convertLastMileOrderToInboundPlan).mockReset()
vi.mocked(listPendingContainerSummaries).mockReset().mockResolvedValue({ records: [summary], page: 1, size: 50, total: 1, pages: 1 })
vi.mocked(convertLastMileOrderToInboundPlan).mockReset().mockResolvedValue({})
vi.mocked(convertLastMileContainerToInboundPlans).mockReset().mockResolvedValue([{ planNo: 'LIP-1' }, { planNo: 'LIP-2' }] as never)
})
it('uses one server summary for a 15-order container that could span order pages', async () => {
it('keeps the original pending order table and does not render a view switch or container table', async () => {
const wrapper = await openPending()
expect(listPendingContainerSummaries).toHaveBeenCalledWith({ page: 1, size: 10, containerNo: undefined, createdFrom: undefined, createdTo: undefined })
expect(wrapper.text()).toContain('CONT-15')
expect(wrapper.text()).toContain('15')
expect(wrapper.text()).toContain('将为 15 个尾程订单生成 15 个独立入库计划')
expect(listLastMileOrders).toHaveBeenCalledTimes(1)
expect(wrapper.text()).toContain('尾程单号')
expect(wrapper.text()).toContain('海外仓')
expect(wrapper.text()).toContain('转仓')
expect(wrapper.text()).toContain('生成入库计划')
expect(wrapper.text()).toContain('按柜批量生成')
expect(wrapper.text()).not.toContain('按订单')
expect(wrapper.text()).not.toContain('待分配整柜')
expect(listLastMileOrders).toHaveBeenLastCalledWith(expect.objectContaining({ page: 1, size: 10, currentStatus: 'PENDING_ASSIGNMENT' }))
expect(listPendingContainerSummaries).not.toHaveBeenCalled()
})
it('requests the next server summary page instead of deriving containers from order rows', async () => {
it('keeps single-order conversion isolated with long order and customer ids', async () => {
const wrapper = await openPending()
const nextPage = wrapper.find('button[aria-label="第 2 页"]')
expect(nextPage.exists()).toBe(true)
await nextPage.trigger('click')
await selectRows(wrapper, [selectedRow()])
await button(wrapper, '生成入库计划')!.trigger('click')
await flushPromises()
expect(listPendingContainerSummaries).toHaveBeenLastCalledWith({
page: 2, size: 10, containerNo: undefined, createdFrom: undefined, createdTo: undefined,
})
expect(listLastMileOrders).toHaveBeenCalledTimes(1)
expect(convertLastMileOrderToInboundPlan).toHaveBeenCalledTimes(1)
expect(convertLastMileOrderToInboundPlan).toHaveBeenCalledWith({ orderId: '2092873463568596994', customerId })
expect(convertLastMileContainerToInboundPlans).not.toHaveBeenCalled()
expect(listPendingContainerSummaries).not.toHaveBeenCalled()
})
it('calls convert-container once, validates N plans and refreshes the server summary', async () => {
it('opens a cabinet selector without requiring order selection and loads server summaries only then', async () => {
const wrapper = await openPending()
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
await button!.trigger('click')
await flushPromises()
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledTimes(1)
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledWith({ sourceContainerBookingId: '2092873463568597001' })
expect(convertLastMileOrderToInboundPlan).not.toHaveBeenCalled()
expect(listPendingContainerSummaries).toHaveBeenCalledTimes(2)
expect(document.body.textContent).toContain('已为15个尾程订单生成15个入库计划')
})
it('does not report success or refresh when N returned plans do not match N orders', async () => {
vi.mocked(convertLastMileContainerToInboundPlans).mockResolvedValueOnce(plans(14))
const wrapper = await openPending()
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
await button!.trigger('click')
await flushPromises()
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledTimes(1)
await openContainerDialog(wrapper)
expect(listPendingContainerSummaries).toHaveBeenCalledTimes(1)
expect(wrapper.text()).toContain('返回 14 个计划')
expect(document.body.textContent).not.toContain('已为15个尾程订单生成14个入库计划')
expect(listPendingContainerSummaries).toHaveBeenCalledWith({ page: 1, size: 50, containerNo: undefined })
expect(document.body.textContent).toContain('选择待分配柜')
expect(document.body.textContent).toContain('将为所选柜内所有待分配尾程订单分别生成独立入库计划')
expect(convertLastMileContainerToInboundPlans).not.toHaveBeenCalled()
})
it.each(['A0404', 'A0409'])('keeps %s request evidence visible', async (code) => {
it('selects one cabinet and sends one long-id convert-container request without using current-page row counts', async () => {
const wrapper = await openPending()
await openContainerDialog(wrapper)
await chooseContainer(wrapper)
const confirm = [...document.body.querySelectorAll('button')].find((item) => item.textContent?.includes('确认按柜生成')) as HTMLButtonElement
expect(confirm.disabled).toBe(false)
confirm.click()
await flushPromises()
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledTimes(1)
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledWith({ sourceContainerBookingId: bookingId })
expect(convertLastMileOrderToInboundPlan).not.toHaveBeenCalled()
expect(document.body.textContent).toContain('已按柜生成 2 个独立入库计划')
expect(listLastMileOrders).toHaveBeenCalledTimes(3)
})
it.each(['A0404', 'A0409'])('keeps %s request evidence visible after cabinet conversion failure', async (code) => {
vi.mocked(convertLastMileContainerToInboundPlans).mockRejectedValueOnce(new ApiResponseError(
'柜级生成失败', code, 200, undefined, `request-${code}`, `trace-${code}`,
))
const wrapper = await openPending()
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
await button!.trigger('click')
await openContainerDialog(wrapper)
await chooseContainer(wrapper)
const confirm = [...document.body.querySelectorAll('button')].find((item) => item.textContent?.includes('确认按柜生成')) as HTMLButtonElement
confirm.click()
await flushPromises()
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledTimes(1)
expect(wrapper.text()).toContain(code)
expect(wrapper.text()).toContain(`request-${code}`)
expect(wrapper.text()).toContain(`trace-${code}`)