import ElementPlus from 'element-plus' import { defineComponent, h, type PropType } from 'vue' import { flushPromises, mount } from '@vue/test-utils' import { beforeEach, describe, expect, it, vi } from 'vitest' import { getDeliveryOrder, listDeliveryOrders, listDeliveryReturnInboundOptions, markDeliveryReturn } from '../api/deliveryOrders' import { updateDeliveryOrderStatus } from '../api/customerVisits' import DeliveryOrdersView from './DeliveryOrdersView.vue' import { commonPhase5Stubs } from './phase5TestStubs' import { getDeliveryDocuments, deliveryDocumentCategories } from '../api/deliveryDocuments' const canPermission = vi.hoisted(() => vi.fn((_code: string) => false)) vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can: canPermission }) })) vi.mock('vue-router', () => ({ useRouter: () => ({ push: vi.fn() }) })) vi.mock('../api/deliveryOrders', () => ({ getDeliveryOrder: vi.fn(), listDeliveryOrders: vi.fn(), listDeliveryReturnInboundOptions: vi.fn(), markDeliveryReturn: vi.fn() })) vi.mock('../api/customerVisits', () => ({ updateDeliveryOrderStatus: vi.fn() })) vi.mock('../api/pallets', () => ({ listReceivingOverseasWarehouses: vi.fn() })) vi.mock('../api/deliveryDocuments', async importOriginal => ({ ...await importOriginal(), getDeliveryDocuments: vi.fn() })) const row = { version: 2, allowedActions: ['START_DELIVERY', 'COMPLETE_DELIVERY', 'REJECT_DELIVERY', 'CANCEL_DELIVERY'], id: '1', customerId: '100', deliveryNo: 'DEL/1', outboundNo: 'OUT-1', dispatchNo: 'PC-1', status: 'WAIT_DELIVERY', palletCount: 1, packageCount: 2, createdAt: '2026-09-01T10:00:00', } function mountView() { return mount(DeliveryOrdersView, { global: { plugins: [ElementPlus], stubs: commonPhase5Stubs } }) } describe('DeliveryOrdersView permissions and commands', () => { beforeEach(() => { canPermission.mockReset().mockReturnValue(false) vi.mocked(getDeliveryDocuments).mockReset() vi.mocked(getDeliveryOrder).mockReset().mockResolvedValue({ order: { ...row, version: 7 }, skus: [], fees: [], operations: [] }) vi.mocked(listDeliveryOrders).mockReset().mockResolvedValue({ records: [row], page: 1, size: 10, total: 1, pages: 1 }) vi.mocked(listDeliveryReturnInboundOptions).mockReset().mockResolvedValue([{ inboundOrderId: '9', inboundCode: 'IN-1', lastMileOrderNo: 'LM-1', customerId: '100' }]) vi.mocked(markDeliveryReturn).mockReset().mockResolvedValue({ id: '19', exceptionNo: 'EXC-1', status: 'PENDING' }) vi.mocked(updateDeliveryOrderStatus).mockReset().mockResolvedValue({ deliveryNo: 'DEL/1', deliveryStatus: 'WAIT_DELIVERY', replayed: false }) }) it('keeps detail read-only while hiding independent write operations', async () => { const wrapper = mountView() await flushPromises() const tableText = wrapper.find('[data-testid="data-table"]').text() expect(tableText).toContain('详情') expect(tableText).not.toContain('编辑状态') expect(tableText).not.toContain('标记退货') wrapper.unmount() }) it('submits status and return commands with the row customer scope', async () => { canPermission.mockImplementation(code => code.endsWith(':status') || code.endsWith(':return')) const wrapper = mountView() await flushPromises() await wrapper.findAll('button').find(button => button.text().includes('编辑状态'))!.trigger('click') await flushPromises() await wrapper.find('[data-dialog="编辑派送状态"] select').setValue('DELIVERED') await wrapper.find('[data-dialog="编辑派送状态"] .dialog-confirm').trigger('click') await flushPromises() expect(updateDeliveryOrderStatus).toHaveBeenCalledWith('DEL/1', '100', { status: 'DELIVERED', expectedVersion: 7, remark: undefined }) await wrapper.findAll('button').find(button => button.text().includes('标记退货'))!.trigger('click') await flushPromises() expect(listDeliveryReturnInboundOptions).toHaveBeenCalledWith('DEL/1', '100') await wrapper.find('[data-dialog="标记退货"] textarea').setValue(' 客户拒收 ') await wrapper.find('[data-dialog="标记退货"] .dialog-confirm').trigger('click') await flushPromises() expect(markDeliveryReturn).toHaveBeenCalledWith('DEL/1', '100', { inboundOrderId: '9', reason: '客户拒收', returnDirection: 'OVERSEAS_WAREHOUSE', returnLogisticsNo: undefined, packageCount: undefined, skuCode: undefined, }) wrapper.unmount() }) it('does not submit an incomplete return form', async () => { canPermission.mockImplementation(code => code.endsWith(':return')) vi.mocked(listDeliveryReturnInboundOptions).mockResolvedValue([]) const wrapper = mountView() await flushPromises() await wrapper.findAll('button').find(button => button.text().includes('标记退货'))!.trigger('click') await flushPromises() await wrapper.find('[data-dialog="标记退货"] .dialog-confirm').trigger('click') expect(markDeliveryReturn).not.toHaveBeenCalled() wrapper.unmount() }) it('maps delivering separately from completion and blocks duplicate or conflicted submission', async () => { canPermission.mockReturnValue(true) const wrapper = mountView() await flushPromises() await wrapper.findAll('button').find(button => button.text().includes('编辑状态'))!.trigger('click') await flushPromises() const select = wrapper.find('[data-dialog="编辑派送状态"] select') expect(select.find('option[value="DELIVERING"]').text()).toBe('派送中') expect(select.find('option[value="DELIVERED"]').text()).toBe('派送完成') expect(select.find('option[value="WAIT_DELIVERY"]').exists()).toBe(false) await select.setValue('DELIVERING') await wrapper.find('[data-dialog="编辑派送状态"] textarea').setValue('123') let reject!: (error: unknown) => void vi.mocked(updateDeliveryOrderStatus).mockReturnValueOnce(new Promise((_resolve, rejectRequest) => { reject = rejectRequest })) const button = wrapper.find('[data-dialog="编辑派送状态"] .dialog-confirm') await button.trigger('click'); await button.trigger('click') expect(updateDeliveryOrderStatus).toHaveBeenCalledTimes(1) expect(updateDeliveryOrderStatus).toHaveBeenCalledWith('DEL/1', '100', { status: 'DELIVERING', expectedVersion: 7, remark: '123' }) reject({ isAxiosError: true, response: { status: 409, data: { code: 'A0409', message: '版本冲突', requestId: 'req-conflict', traceId: 'trace-conflict' } } }) await flushPromises() const state = wrapper.vm as unknown as { statusConflict: boolean; statusOpen: boolean; statusForm: { remark: string }; refreshStatus: () => Promise } expect(state.statusConflict).toBe(true) expect(state.statusOpen).toBe(true) expect(state.statusForm.remark).toBe('123') await button.trigger('click') expect(updateDeliveryOrderStatus).toHaveBeenCalledTimes(1) vi.mocked(getDeliveryOrder).mockResolvedValue({ order: { ...row, version: 8, allowedActions: ['COMPLETE_DELIVERY'] }, skus: [], fees: [], operations: [] }) await state.refreshStatus(); await flushPromises() expect(select.findAll('option').map(option => option.attributes('value'))).toEqual(['DELIVERED']) wrapper.unmount() }) it('hides status editing when the server supplies no actions', async () => { canPermission.mockReturnValue(true) vi.mocked(listDeliveryOrders).mockResolvedValue({ records: [{ ...row, status: 'DELIVERED', allowedActions: [] }], page: 1, size: 10, total: 1, pages: 1 }) const wrapper = mountView(); await flushPromises() expect(wrapper.find('[data-testid="data-table"]').text()).not.toContain('编辑状态') expect(getDeliveryOrder).not.toHaveBeenCalled() wrapper.unmount() }) it('renders category counts from the list without per-row document requests', async () => { canPermission.mockReturnValue(true) const documentSummary = { uploadedCategoryCount: 1, totalCategoryCount: 3, categories: deliveryDocumentCategories.map(item => ({ category: item.category, status: item.category === 'RECEIPT' ? 'UPLOADED' as const : 'MISSING' as const, fileCount: item.category === 'RECEIPT' ? 3 : 0 })) } vi.mocked(listDeliveryOrders).mockResolvedValue({ records: [{ ...row, documentSummary }], page: 1, size: 10, total: 1, pages: 1 }) const table = defineComponent({ props: { data: Array as PropType }, setup(props, { slots }) { return () => h('div', props.data?.flatMap(item => slots.documents?.({ row: item }) || [])) } }) const wrapper = mount(DeliveryOrdersView, { global: { plugins: [ElementPlus], stubs: { ...commonPhase5Stubs, DataTable: table, DeliveryDocumentsDialog: true } } }) await flushPromises() expect(wrapper.text()).toContain('1/3 已上传') expect(wrapper.text()).toContain('签收文件(POD签收/快递签收) · 已上传') expect(getDeliveryDocuments).not.toHaveBeenCalled() await wrapper.findAll('button').find(button => button.text().includes('管理资料'))!.trigger('click') const dialog = wrapper.findComponent({ name: 'DeliveryDocumentsDialog' }) expect(dialog.props()).toMatchObject({ deliveryNo: 'DEL/1', customerId: '100', modelValue: true }) const calls = vi.mocked(listDeliveryOrders).mock.calls.length dialog.vm.$emit('saved'); await flushPromises() expect(listDeliveryOrders).toHaveBeenCalledTimes(calls + 1) wrapper.unmount() }) })